perf_counter: change event definition
[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
79f14641
PZ
747static void perf_counter_refresh(struct perf_counter *counter, int refresh)
748{
749 atomic_add(refresh, &counter->event_limit);
750 perf_counter_enable(counter);
751}
752
d859e29f
PM
753/*
754 * Enable a counter and all its children.
755 */
756static void perf_counter_enable_family(struct perf_counter *counter)
757{
758 struct perf_counter *child;
759
760 perf_counter_enable(counter);
761
762 /*
763 * Lock the mutex to protect the list of children
764 */
765 mutex_lock(&counter->mutex);
766 list_for_each_entry(child, &counter->child_list, child_list)
767 perf_counter_enable(child);
768 mutex_unlock(&counter->mutex);
04289bb9
IM
769}
770
235c7fc7
IM
771void __perf_counter_sched_out(struct perf_counter_context *ctx,
772 struct perf_cpu_context *cpuctx)
773{
774 struct perf_counter *counter;
3cbed429 775 u64 flags;
235c7fc7 776
d859e29f
PM
777 spin_lock(&ctx->lock);
778 ctx->is_active = 0;
235c7fc7 779 if (likely(!ctx->nr_counters))
d859e29f 780 goto out;
53cfbf59 781 update_context_time(ctx, 0);
235c7fc7 782
3cbed429 783 flags = hw_perf_save_disable();
235c7fc7
IM
784 if (ctx->nr_active) {
785 list_for_each_entry(counter, &ctx->counter_list, list_entry)
786 group_sched_out(counter, cpuctx, ctx);
787 }
3cbed429 788 hw_perf_restore(flags);
d859e29f 789 out:
235c7fc7
IM
790 spin_unlock(&ctx->lock);
791}
792
0793a61d
TG
793/*
794 * Called from scheduler to remove the counters of the current task,
795 * with interrupts disabled.
796 *
797 * We stop each counter and update the counter value in counter->count.
798 *
7671581f 799 * This does not protect us against NMI, but disable()
0793a61d
TG
800 * sets the disabled bit in the control field of counter _before_
801 * accessing the counter control register. If a NMI hits, then it will
802 * not restart the counter.
803 */
804void perf_counter_task_sched_out(struct task_struct *task, int cpu)
805{
806 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
807 struct perf_counter_context *ctx = &task->perf_counter_ctx;
4a0deca6 808 struct pt_regs *regs;
0793a61d
TG
809
810 if (likely(!cpuctx->task_ctx))
811 return;
812
4a0deca6
PZ
813 regs = task_pt_regs(task);
814 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
235c7fc7
IM
815 __perf_counter_sched_out(ctx, cpuctx);
816
0793a61d
TG
817 cpuctx->task_ctx = NULL;
818}
819
235c7fc7 820static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
04289bb9 821{
235c7fc7 822 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
04289bb9
IM
823}
824
7995888f 825static int
04289bb9
IM
826group_sched_in(struct perf_counter *group_counter,
827 struct perf_cpu_context *cpuctx,
828 struct perf_counter_context *ctx,
829 int cpu)
830{
95cdd2e7 831 struct perf_counter *counter, *partial_group;
3cbed429
PM
832 int ret;
833
834 if (group_counter->state == PERF_COUNTER_STATE_OFF)
835 return 0;
836
837 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
838 if (ret)
839 return ret < 0 ? ret : 0;
04289bb9 840
c07c99b6 841 group_counter->prev_state = group_counter->state;
95cdd2e7
IM
842 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
843 return -EAGAIN;
04289bb9
IM
844
845 /*
846 * Schedule in siblings as one group (if any):
847 */
7995888f 848 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
c07c99b6 849 counter->prev_state = counter->state;
95cdd2e7
IM
850 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
851 partial_group = counter;
852 goto group_error;
853 }
95cdd2e7
IM
854 }
855
3cbed429 856 return 0;
95cdd2e7
IM
857
858group_error:
859 /*
860 * Groups can be scheduled in as one unit only, so undo any
861 * partial group before returning:
862 */
863 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
864 if (counter == partial_group)
865 break;
866 counter_sched_out(counter, cpuctx, ctx);
7995888f 867 }
95cdd2e7 868 counter_sched_out(group_counter, cpuctx, ctx);
7995888f 869
95cdd2e7 870 return -EAGAIN;
04289bb9
IM
871}
872
235c7fc7
IM
873static void
874__perf_counter_sched_in(struct perf_counter_context *ctx,
875 struct perf_cpu_context *cpuctx, int cpu)
0793a61d 876{
0793a61d 877 struct perf_counter *counter;
3cbed429 878 u64 flags;
dd0e6ba2 879 int can_add_hw = 1;
0793a61d 880
d859e29f
PM
881 spin_lock(&ctx->lock);
882 ctx->is_active = 1;
0793a61d 883 if (likely(!ctx->nr_counters))
d859e29f 884 goto out;
0793a61d 885
53cfbf59
PM
886 /*
887 * Add any time since the last sched_out to the lost time
888 * so it doesn't get included in the total_time_enabled and
889 * total_time_running measures for counters in the context.
890 */
891 ctx->time_lost = get_context_time(ctx, 0) - ctx->time_now;
892
3cbed429 893 flags = hw_perf_save_disable();
3b6f9e5c
PM
894
895 /*
896 * First go through the list and put on any pinned groups
897 * in order to give them the best chance of going on.
898 */
899 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
900 if (counter->state <= PERF_COUNTER_STATE_OFF ||
901 !counter->hw_event.pinned)
902 continue;
903 if (counter->cpu != -1 && counter->cpu != cpu)
904 continue;
905
906 if (group_can_go_on(counter, cpuctx, 1))
907 group_sched_in(counter, cpuctx, ctx, cpu);
908
909 /*
910 * If this pinned group hasn't been scheduled,
911 * put it in error state.
912 */
53cfbf59
PM
913 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
914 update_group_times(counter);
3b6f9e5c 915 counter->state = PERF_COUNTER_STATE_ERROR;
53cfbf59 916 }
3b6f9e5c
PM
917 }
918
04289bb9 919 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
3b6f9e5c
PM
920 /*
921 * Ignore counters in OFF or ERROR state, and
922 * ignore pinned counters since we did them already.
923 */
924 if (counter->state <= PERF_COUNTER_STATE_OFF ||
925 counter->hw_event.pinned)
926 continue;
927
04289bb9
IM
928 /*
929 * Listen to the 'cpu' scheduling filter constraint
930 * of counters:
931 */
0793a61d
TG
932 if (counter->cpu != -1 && counter->cpu != cpu)
933 continue;
934
3b6f9e5c 935 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
dd0e6ba2
PM
936 if (group_sched_in(counter, cpuctx, ctx, cpu))
937 can_add_hw = 0;
3b6f9e5c 938 }
0793a61d 939 }
3cbed429 940 hw_perf_restore(flags);
d859e29f 941 out:
0793a61d 942 spin_unlock(&ctx->lock);
235c7fc7
IM
943}
944
945/*
946 * Called from scheduler to add the counters of the current task
947 * with interrupts disabled.
948 *
949 * We restore the counter value and then enable it.
950 *
951 * This does not protect us against NMI, but enable()
952 * sets the enabled bit in the control field of counter _before_
953 * accessing the counter control register. If a NMI hits, then it will
954 * keep the counter running.
955 */
956void perf_counter_task_sched_in(struct task_struct *task, int cpu)
957{
958 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
959 struct perf_counter_context *ctx = &task->perf_counter_ctx;
04289bb9 960
235c7fc7 961 __perf_counter_sched_in(ctx, cpuctx, cpu);
0793a61d
TG
962 cpuctx->task_ctx = ctx;
963}
964
235c7fc7
IM
965static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
966{
967 struct perf_counter_context *ctx = &cpuctx->ctx;
968
969 __perf_counter_sched_in(ctx, cpuctx, cpu);
970}
971
1d1c7ddb
IM
972int perf_counter_task_disable(void)
973{
974 struct task_struct *curr = current;
975 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
976 struct perf_counter *counter;
aa9c4c0f 977 unsigned long flags;
1d1c7ddb
IM
978 u64 perf_flags;
979 int cpu;
980
981 if (likely(!ctx->nr_counters))
982 return 0;
983
aa9c4c0f 984 curr_rq_lock_irq_save(&flags);
1d1c7ddb
IM
985 cpu = smp_processor_id();
986
aa9c4c0f
IM
987 /* force the update of the task clock: */
988 __task_delta_exec(curr, 1);
989
1d1c7ddb
IM
990 perf_counter_task_sched_out(curr, cpu);
991
992 spin_lock(&ctx->lock);
993
994 /*
995 * Disable all the counters:
996 */
997 perf_flags = hw_perf_save_disable();
998
3b6f9e5c 999 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
53cfbf59
PM
1000 if (counter->state != PERF_COUNTER_STATE_ERROR) {
1001 update_group_times(counter);
3b6f9e5c 1002 counter->state = PERF_COUNTER_STATE_OFF;
53cfbf59 1003 }
3b6f9e5c 1004 }
9b51f66d 1005
1d1c7ddb
IM
1006 hw_perf_restore(perf_flags);
1007
1008 spin_unlock(&ctx->lock);
1009
aa9c4c0f 1010 curr_rq_unlock_irq_restore(&flags);
1d1c7ddb
IM
1011
1012 return 0;
1013}
1014
1015int perf_counter_task_enable(void)
1016{
1017 struct task_struct *curr = current;
1018 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1019 struct perf_counter *counter;
aa9c4c0f 1020 unsigned long flags;
1d1c7ddb
IM
1021 u64 perf_flags;
1022 int cpu;
1023
1024 if (likely(!ctx->nr_counters))
1025 return 0;
1026
aa9c4c0f 1027 curr_rq_lock_irq_save(&flags);
1d1c7ddb
IM
1028 cpu = smp_processor_id();
1029
aa9c4c0f
IM
1030 /* force the update of the task clock: */
1031 __task_delta_exec(curr, 1);
1032
235c7fc7
IM
1033 perf_counter_task_sched_out(curr, cpu);
1034
1d1c7ddb
IM
1035 spin_lock(&ctx->lock);
1036
1037 /*
1038 * Disable all the counters:
1039 */
1040 perf_flags = hw_perf_save_disable();
1041
1042 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
3b6f9e5c 1043 if (counter->state > PERF_COUNTER_STATE_OFF)
1d1c7ddb 1044 continue;
6a930700 1045 counter->state = PERF_COUNTER_STATE_INACTIVE;
53cfbf59
PM
1046 counter->tstamp_enabled = ctx->time_now -
1047 counter->total_time_enabled;
aa9c4c0f 1048 counter->hw_event.disabled = 0;
1d1c7ddb
IM
1049 }
1050 hw_perf_restore(perf_flags);
1051
1052 spin_unlock(&ctx->lock);
1053
1054 perf_counter_task_sched_in(curr, cpu);
1055
aa9c4c0f 1056 curr_rq_unlock_irq_restore(&flags);
1d1c7ddb
IM
1057
1058 return 0;
1059}
1060
235c7fc7
IM
1061/*
1062 * Round-robin a context's counters:
1063 */
1064static void rotate_ctx(struct perf_counter_context *ctx)
0793a61d 1065{
0793a61d 1066 struct perf_counter *counter;
5c92d124 1067 u64 perf_flags;
0793a61d 1068
235c7fc7 1069 if (!ctx->nr_counters)
0793a61d
TG
1070 return;
1071
0793a61d 1072 spin_lock(&ctx->lock);
0793a61d 1073 /*
04289bb9 1074 * Rotate the first entry last (works just fine for group counters too):
0793a61d 1075 */
01b2838c 1076 perf_flags = hw_perf_save_disable();
04289bb9 1077 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
75564232 1078 list_move_tail(&counter->list_entry, &ctx->counter_list);
0793a61d
TG
1079 break;
1080 }
01b2838c 1081 hw_perf_restore(perf_flags);
0793a61d
TG
1082
1083 spin_unlock(&ctx->lock);
235c7fc7
IM
1084}
1085
1086void perf_counter_task_tick(struct task_struct *curr, int cpu)
1087{
1088 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1089 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1090 const int rotate_percpu = 0;
1091
1092 if (rotate_percpu)
1093 perf_counter_cpu_sched_out(cpuctx);
1094 perf_counter_task_sched_out(curr, cpu);
0793a61d 1095
235c7fc7
IM
1096 if (rotate_percpu)
1097 rotate_ctx(&cpuctx->ctx);
1098 rotate_ctx(ctx);
1099
1100 if (rotate_percpu)
1101 perf_counter_cpu_sched_in(cpuctx, cpu);
0793a61d
TG
1102 perf_counter_task_sched_in(curr, cpu);
1103}
1104
0793a61d
TG
1105/*
1106 * Cross CPU call to read the hardware counter
1107 */
7671581f 1108static void __read(void *info)
0793a61d 1109{
621a01ea 1110 struct perf_counter *counter = info;
53cfbf59 1111 struct perf_counter_context *ctx = counter->ctx;
aa9c4c0f 1112 unsigned long flags;
621a01ea 1113
aa9c4c0f 1114 curr_rq_lock_irq_save(&flags);
53cfbf59
PM
1115 if (ctx->is_active)
1116 update_context_time(ctx, 1);
7671581f 1117 counter->hw_ops->read(counter);
53cfbf59 1118 update_counter_times(counter);
aa9c4c0f 1119 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
1120}
1121
04289bb9 1122static u64 perf_counter_read(struct perf_counter *counter)
0793a61d
TG
1123{
1124 /*
1125 * If counter is enabled and currently active on a CPU, update the
1126 * value in the counter structure:
1127 */
6a930700 1128 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
0793a61d 1129 smp_call_function_single(counter->oncpu,
7671581f 1130 __read, counter, 1);
53cfbf59
PM
1131 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1132 update_counter_times(counter);
0793a61d
TG
1133 }
1134
ee06094f 1135 return atomic64_read(&counter->count);
0793a61d
TG
1136}
1137
0793a61d
TG
1138static void put_context(struct perf_counter_context *ctx)
1139{
1140 if (ctx->task)
1141 put_task_struct(ctx->task);
1142}
1143
1144static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1145{
1146 struct perf_cpu_context *cpuctx;
1147 struct perf_counter_context *ctx;
1148 struct task_struct *task;
1149
1150 /*
1151 * If cpu is not a wildcard then this is a percpu counter:
1152 */
1153 if (cpu != -1) {
1154 /* Must be root to operate on a CPU counter: */
1155 if (!capable(CAP_SYS_ADMIN))
1156 return ERR_PTR(-EACCES);
1157
1158 if (cpu < 0 || cpu > num_possible_cpus())
1159 return ERR_PTR(-EINVAL);
1160
1161 /*
1162 * We could be clever and allow to attach a counter to an
1163 * offline CPU and activate it when the CPU comes up, but
1164 * that's for later.
1165 */
1166 if (!cpu_isset(cpu, cpu_online_map))
1167 return ERR_PTR(-ENODEV);
1168
1169 cpuctx = &per_cpu(perf_cpu_context, cpu);
1170 ctx = &cpuctx->ctx;
1171
0793a61d
TG
1172 return ctx;
1173 }
1174
1175 rcu_read_lock();
1176 if (!pid)
1177 task = current;
1178 else
1179 task = find_task_by_vpid(pid);
1180 if (task)
1181 get_task_struct(task);
1182 rcu_read_unlock();
1183
1184 if (!task)
1185 return ERR_PTR(-ESRCH);
1186
1187 ctx = &task->perf_counter_ctx;
1188 ctx->task = task;
1189
1190 /* Reuse ptrace permission checks for now. */
1191 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1192 put_context(ctx);
1193 return ERR_PTR(-EACCES);
1194 }
1195
1196 return ctx;
1197}
1198
592903cd
PZ
1199static void free_counter_rcu(struct rcu_head *head)
1200{
1201 struct perf_counter *counter;
1202
1203 counter = container_of(head, struct perf_counter, rcu_head);
1204 kfree(counter);
1205}
1206
925d519a
PZ
1207static void perf_pending_sync(struct perf_counter *counter);
1208
f1600952
PZ
1209static void free_counter(struct perf_counter *counter)
1210{
925d519a
PZ
1211 perf_pending_sync(counter);
1212
e077df4f
PZ
1213 if (counter->destroy)
1214 counter->destroy(counter);
1215
f1600952
PZ
1216 call_rcu(&counter->rcu_head, free_counter_rcu);
1217}
1218
0793a61d
TG
1219/*
1220 * Called when the last reference to the file is gone.
1221 */
1222static int perf_release(struct inode *inode, struct file *file)
1223{
1224 struct perf_counter *counter = file->private_data;
1225 struct perf_counter_context *ctx = counter->ctx;
1226
1227 file->private_data = NULL;
1228
d859e29f 1229 mutex_lock(&ctx->mutex);
0793a61d
TG
1230 mutex_lock(&counter->mutex);
1231
04289bb9 1232 perf_counter_remove_from_context(counter);
0793a61d
TG
1233
1234 mutex_unlock(&counter->mutex);
d859e29f 1235 mutex_unlock(&ctx->mutex);
0793a61d 1236
f1600952 1237 free_counter(counter);
5af75917 1238 put_context(ctx);
0793a61d
TG
1239
1240 return 0;
1241}
1242
1243/*
1244 * Read the performance counter - simple non blocking version for now
1245 */
1246static ssize_t
1247perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1248{
53cfbf59
PM
1249 u64 values[3];
1250 int n;
0793a61d 1251
3b6f9e5c
PM
1252 /*
1253 * Return end-of-file for a read on a counter that is in
1254 * error state (i.e. because it was pinned but it couldn't be
1255 * scheduled on to the CPU at some point).
1256 */
1257 if (counter->state == PERF_COUNTER_STATE_ERROR)
1258 return 0;
1259
0793a61d 1260 mutex_lock(&counter->mutex);
53cfbf59
PM
1261 values[0] = perf_counter_read(counter);
1262 n = 1;
1263 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1264 values[n++] = counter->total_time_enabled +
1265 atomic64_read(&counter->child_total_time_enabled);
1266 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1267 values[n++] = counter->total_time_running +
1268 atomic64_read(&counter->child_total_time_running);
0793a61d
TG
1269 mutex_unlock(&counter->mutex);
1270
53cfbf59
PM
1271 if (count < n * sizeof(u64))
1272 return -EINVAL;
1273 count = n * sizeof(u64);
1274
1275 if (copy_to_user(buf, values, count))
1276 return -EFAULT;
1277
1278 return count;
0793a61d
TG
1279}
1280
0793a61d
TG
1281static ssize_t
1282perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1283{
1284 struct perf_counter *counter = file->private_data;
1285
7b732a75 1286 return perf_read_hw(counter, buf, count);
0793a61d
TG
1287}
1288
1289static unsigned int perf_poll(struct file *file, poll_table *wait)
1290{
1291 struct perf_counter *counter = file->private_data;
c7138f37
PZ
1292 struct perf_mmap_data *data;
1293 unsigned int events;
1294
1295 rcu_read_lock();
1296 data = rcu_dereference(counter->data);
1297 if (data)
1298 events = atomic_xchg(&data->wakeup, 0);
1299 else
1300 events = POLL_HUP;
1301 rcu_read_unlock();
0793a61d
TG
1302
1303 poll_wait(file, &counter->waitq, wait);
1304
0793a61d
TG
1305 return events;
1306}
1307
d859e29f
PM
1308static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1309{
1310 struct perf_counter *counter = file->private_data;
1311 int err = 0;
1312
1313 switch (cmd) {
1314 case PERF_COUNTER_IOC_ENABLE:
1315 perf_counter_enable_family(counter);
1316 break;
1317 case PERF_COUNTER_IOC_DISABLE:
1318 perf_counter_disable_family(counter);
1319 break;
79f14641
PZ
1320 case PERF_COUNTER_IOC_REFRESH:
1321 perf_counter_refresh(counter, arg);
1322 break;
d859e29f
PM
1323 default:
1324 err = -ENOTTY;
1325 }
1326 return err;
1327}
1328
38ff667b
PZ
1329/*
1330 * Callers need to ensure there can be no nesting of this function, otherwise
1331 * the seqlock logic goes bad. We can not serialize this because the arch
1332 * code calls this from NMI context.
1333 */
1334void perf_counter_update_userpage(struct perf_counter *counter)
37d81828 1335{
38ff667b
PZ
1336 struct perf_mmap_data *data;
1337 struct perf_counter_mmap_page *userpg;
1338
1339 rcu_read_lock();
1340 data = rcu_dereference(counter->data);
1341 if (!data)
1342 goto unlock;
1343
1344 userpg = data->user_page;
37d81828 1345
7b732a75
PZ
1346 /*
1347 * Disable preemption so as to not let the corresponding user-space
1348 * spin too long if we get preempted.
1349 */
1350 preempt_disable();
37d81828 1351 ++userpg->lock;
92f22a38 1352 barrier();
37d81828
PM
1353 userpg->index = counter->hw.idx;
1354 userpg->offset = atomic64_read(&counter->count);
1355 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1356 userpg->offset -= atomic64_read(&counter->hw.prev_count);
7b732a75 1357
92f22a38 1358 barrier();
37d81828 1359 ++userpg->lock;
7b732a75 1360 preempt_enable();
38ff667b 1361unlock:
7b732a75 1362 rcu_read_unlock();
37d81828
PM
1363}
1364
1365static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1366{
1367 struct perf_counter *counter = vma->vm_file->private_data;
7b732a75
PZ
1368 struct perf_mmap_data *data;
1369 int ret = VM_FAULT_SIGBUS;
1370
1371 rcu_read_lock();
1372 data = rcu_dereference(counter->data);
1373 if (!data)
1374 goto unlock;
1375
1376 if (vmf->pgoff == 0) {
1377 vmf->page = virt_to_page(data->user_page);
1378 } else {
1379 int nr = vmf->pgoff - 1;
37d81828 1380
7b732a75
PZ
1381 if ((unsigned)nr > data->nr_pages)
1382 goto unlock;
37d81828 1383
7b732a75
PZ
1384 vmf->page = virt_to_page(data->data_pages[nr]);
1385 }
37d81828 1386 get_page(vmf->page);
7b732a75
PZ
1387 ret = 0;
1388unlock:
1389 rcu_read_unlock();
1390
1391 return ret;
1392}
1393
1394static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1395{
1396 struct perf_mmap_data *data;
1397 unsigned long size;
1398 int i;
1399
1400 WARN_ON(atomic_read(&counter->mmap_count));
1401
1402 size = sizeof(struct perf_mmap_data);
1403 size += nr_pages * sizeof(void *);
1404
1405 data = kzalloc(size, GFP_KERNEL);
1406 if (!data)
1407 goto fail;
1408
1409 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1410 if (!data->user_page)
1411 goto fail_user_page;
1412
1413 for (i = 0; i < nr_pages; i++) {
1414 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1415 if (!data->data_pages[i])
1416 goto fail_data_pages;
1417 }
1418
1419 data->nr_pages = nr_pages;
1420
1421 rcu_assign_pointer(counter->data, data);
1422
37d81828 1423 return 0;
7b732a75
PZ
1424
1425fail_data_pages:
1426 for (i--; i >= 0; i--)
1427 free_page((unsigned long)data->data_pages[i]);
1428
1429 free_page((unsigned long)data->user_page);
1430
1431fail_user_page:
1432 kfree(data);
1433
1434fail:
1435 return -ENOMEM;
1436}
1437
1438static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1439{
1440 struct perf_mmap_data *data = container_of(rcu_head,
1441 struct perf_mmap_data, rcu_head);
1442 int i;
1443
1444 free_page((unsigned long)data->user_page);
1445 for (i = 0; i < data->nr_pages; i++)
1446 free_page((unsigned long)data->data_pages[i]);
1447 kfree(data);
1448}
1449
1450static void perf_mmap_data_free(struct perf_counter *counter)
1451{
1452 struct perf_mmap_data *data = counter->data;
1453
1454 WARN_ON(atomic_read(&counter->mmap_count));
1455
1456 rcu_assign_pointer(counter->data, NULL);
1457 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1458}
1459
1460static void perf_mmap_open(struct vm_area_struct *vma)
1461{
1462 struct perf_counter *counter = vma->vm_file->private_data;
1463
1464 atomic_inc(&counter->mmap_count);
1465}
1466
1467static void perf_mmap_close(struct vm_area_struct *vma)
1468{
1469 struct perf_counter *counter = vma->vm_file->private_data;
1470
1471 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1472 &counter->mmap_mutex)) {
ebb3c4c4 1473 vma->vm_mm->locked_vm -= counter->data->nr_pages + 1;
7b732a75
PZ
1474 perf_mmap_data_free(counter);
1475 mutex_unlock(&counter->mmap_mutex);
1476 }
37d81828
PM
1477}
1478
1479static struct vm_operations_struct perf_mmap_vmops = {
ebb3c4c4 1480 .open = perf_mmap_open,
7b732a75 1481 .close = perf_mmap_close,
37d81828
PM
1482 .fault = perf_mmap_fault,
1483};
1484
1485static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1486{
1487 struct perf_counter *counter = file->private_data;
7b732a75
PZ
1488 unsigned long vma_size;
1489 unsigned long nr_pages;
1490 unsigned long locked, lock_limit;
1491 int ret = 0;
37d81828
PM
1492
1493 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1494 return -EINVAL;
7b732a75
PZ
1495
1496 vma_size = vma->vm_end - vma->vm_start;
1497 nr_pages = (vma_size / PAGE_SIZE) - 1;
1498
7730d865
PZ
1499 /*
1500 * If we have data pages ensure they're a power-of-two number, so we
1501 * can do bitmasks instead of modulo.
1502 */
1503 if (nr_pages != 0 && !is_power_of_2(nr_pages))
37d81828
PM
1504 return -EINVAL;
1505
7b732a75 1506 if (vma_size != PAGE_SIZE * (1 + nr_pages))
37d81828
PM
1507 return -EINVAL;
1508
7b732a75
PZ
1509 if (vma->vm_pgoff != 0)
1510 return -EINVAL;
37d81828 1511
ebb3c4c4
PZ
1512 mutex_lock(&counter->mmap_mutex);
1513 if (atomic_inc_not_zero(&counter->mmap_count)) {
1514 if (nr_pages != counter->data->nr_pages)
1515 ret = -EINVAL;
1516 goto unlock;
1517 }
1518
1519 locked = vma->vm_mm->locked_vm;
1520 locked += nr_pages + 1;
7b732a75
PZ
1521
1522 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1523 lock_limit >>= PAGE_SHIFT;
1524
ebb3c4c4
PZ
1525 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1526 ret = -EPERM;
1527 goto unlock;
1528 }
7b732a75
PZ
1529
1530 WARN_ON(counter->data);
1531 ret = perf_mmap_data_alloc(counter, nr_pages);
ebb3c4c4
PZ
1532 if (ret)
1533 goto unlock;
1534
1535 atomic_set(&counter->mmap_count, 1);
1536 vma->vm_mm->locked_vm += nr_pages + 1;
1537unlock:
7b732a75 1538 mutex_unlock(&counter->mmap_mutex);
37d81828
PM
1539
1540 vma->vm_flags &= ~VM_MAYWRITE;
1541 vma->vm_flags |= VM_RESERVED;
1542 vma->vm_ops = &perf_mmap_vmops;
7b732a75
PZ
1543
1544 return ret;
37d81828
PM
1545}
1546
3c446b3d
PZ
1547static int perf_fasync(int fd, struct file *filp, int on)
1548{
1549 struct perf_counter *counter = filp->private_data;
1550 struct inode *inode = filp->f_path.dentry->d_inode;
1551 int retval;
1552
1553 mutex_lock(&inode->i_mutex);
1554 retval = fasync_helper(fd, filp, on, &counter->fasync);
1555 mutex_unlock(&inode->i_mutex);
1556
1557 if (retval < 0)
1558 return retval;
1559
1560 return 0;
1561}
1562
0793a61d
TG
1563static const struct file_operations perf_fops = {
1564 .release = perf_release,
1565 .read = perf_read,
1566 .poll = perf_poll,
d859e29f
PM
1567 .unlocked_ioctl = perf_ioctl,
1568 .compat_ioctl = perf_ioctl,
37d81828 1569 .mmap = perf_mmap,
3c446b3d 1570 .fasync = perf_fasync,
0793a61d
TG
1571};
1572
925d519a
PZ
1573/*
1574 * Perf counter wakeup
1575 *
1576 * If there's data, ensure we set the poll() state and publish everything
1577 * to user-space before waking everybody up.
1578 */
1579
1580void perf_counter_wakeup(struct perf_counter *counter)
1581{
1582 struct perf_mmap_data *data;
1583
1584 rcu_read_lock();
1585 data = rcu_dereference(counter->data);
1586 if (data) {
3c446b3d 1587 atomic_set(&data->wakeup, POLL_IN);
38ff667b
PZ
1588 /*
1589 * Ensure all data writes are issued before updating the
1590 * user-space data head information. The matching rmb()
1591 * will be in userspace after reading this value.
1592 */
1593 smp_wmb();
1594 data->user_page->data_head = atomic_read(&data->head);
925d519a
PZ
1595 }
1596 rcu_read_unlock();
1597
1598 wake_up_all(&counter->waitq);
4c9e2542
PZ
1599
1600 if (counter->pending_kill) {
1601 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1602 counter->pending_kill = 0;
1603 }
925d519a
PZ
1604}
1605
1606/*
1607 * Pending wakeups
1608 *
1609 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1610 *
1611 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1612 * single linked list and use cmpxchg() to add entries lockless.
1613 */
1614
79f14641
PZ
1615static void perf_pending_counter(struct perf_pending_entry *entry)
1616{
1617 struct perf_counter *counter = container_of(entry,
1618 struct perf_counter, pending);
1619
1620 if (counter->pending_disable) {
1621 counter->pending_disable = 0;
1622 perf_counter_disable(counter);
1623 }
1624
1625 if (counter->pending_wakeup) {
1626 counter->pending_wakeup = 0;
1627 perf_counter_wakeup(counter);
1628 }
1629}
1630
671dec5d 1631#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
925d519a 1632
671dec5d 1633static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
925d519a
PZ
1634 PENDING_TAIL,
1635};
1636
671dec5d
PZ
1637static void perf_pending_queue(struct perf_pending_entry *entry,
1638 void (*func)(struct perf_pending_entry *))
925d519a 1639{
671dec5d 1640 struct perf_pending_entry **head;
925d519a 1641
671dec5d 1642 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
925d519a
PZ
1643 return;
1644
671dec5d
PZ
1645 entry->func = func;
1646
1647 head = &get_cpu_var(perf_pending_head);
925d519a
PZ
1648
1649 do {
671dec5d
PZ
1650 entry->next = *head;
1651 } while (cmpxchg(head, entry->next, entry) != entry->next);
925d519a
PZ
1652
1653 set_perf_counter_pending();
1654
671dec5d 1655 put_cpu_var(perf_pending_head);
925d519a
PZ
1656}
1657
1658static int __perf_pending_run(void)
1659{
671dec5d 1660 struct perf_pending_entry *list;
925d519a
PZ
1661 int nr = 0;
1662
671dec5d 1663 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
925d519a 1664 while (list != PENDING_TAIL) {
671dec5d
PZ
1665 void (*func)(struct perf_pending_entry *);
1666 struct perf_pending_entry *entry = list;
925d519a
PZ
1667
1668 list = list->next;
1669
671dec5d
PZ
1670 func = entry->func;
1671 entry->next = NULL;
925d519a
PZ
1672 /*
1673 * Ensure we observe the unqueue before we issue the wakeup,
1674 * so that we won't be waiting forever.
1675 * -- see perf_not_pending().
1676 */
1677 smp_wmb();
1678
671dec5d 1679 func(entry);
925d519a
PZ
1680 nr++;
1681 }
1682
1683 return nr;
1684}
1685
1686static inline int perf_not_pending(struct perf_counter *counter)
1687{
1688 /*
1689 * If we flush on whatever cpu we run, there is a chance we don't
1690 * need to wait.
1691 */
1692 get_cpu();
1693 __perf_pending_run();
1694 put_cpu();
1695
1696 /*
1697 * Ensure we see the proper queue state before going to sleep
1698 * so that we do not miss the wakeup. -- see perf_pending_handle()
1699 */
1700 smp_rmb();
671dec5d 1701 return counter->pending.next == NULL;
925d519a
PZ
1702}
1703
1704static void perf_pending_sync(struct perf_counter *counter)
1705{
1706 wait_event(counter->waitq, perf_not_pending(counter));
1707}
1708
1709void perf_counter_do_pending(void)
1710{
1711 __perf_pending_run();
1712}
1713
394ee076
PZ
1714/*
1715 * Callchain support -- arch specific
1716 */
1717
9c03d88e 1718__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
394ee076
PZ
1719{
1720 return NULL;
1721}
1722
0322cd6e
PZ
1723/*
1724 * Output
1725 */
1726
b9cacc7b
PZ
1727struct perf_output_handle {
1728 struct perf_counter *counter;
1729 struct perf_mmap_data *data;
1730 unsigned int offset;
63e35b25 1731 unsigned int head;
b9cacc7b 1732 int wakeup;
78d613eb 1733 int nmi;
4c9e2542 1734 int overflow;
b9cacc7b
PZ
1735};
1736
78d613eb
PZ
1737static inline void __perf_output_wakeup(struct perf_output_handle *handle)
1738{
671dec5d 1739 if (handle->nmi) {
79f14641 1740 handle->counter->pending_wakeup = 1;
671dec5d 1741 perf_pending_queue(&handle->counter->pending,
79f14641 1742 perf_pending_counter);
671dec5d 1743 } else
78d613eb
PZ
1744 perf_counter_wakeup(handle->counter);
1745}
1746
b9cacc7b 1747static int perf_output_begin(struct perf_output_handle *handle,
78d613eb 1748 struct perf_counter *counter, unsigned int size,
4c9e2542 1749 int nmi, int overflow)
0322cd6e 1750{
7b732a75 1751 struct perf_mmap_data *data;
b9cacc7b 1752 unsigned int offset, head;
0322cd6e 1753
7b732a75 1754 rcu_read_lock();
7b732a75
PZ
1755 data = rcu_dereference(counter->data);
1756 if (!data)
1757 goto out;
1758
4c9e2542
PZ
1759 handle->counter = counter;
1760 handle->nmi = nmi;
1761 handle->overflow = overflow;
78d613eb 1762
7b732a75 1763 if (!data->nr_pages)
78d613eb 1764 goto fail;
7b732a75 1765
7b732a75
PZ
1766 do {
1767 offset = head = atomic_read(&data->head);
c7138f37 1768 head += size;
7b732a75
PZ
1769 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1770
b9cacc7b
PZ
1771 handle->data = data;
1772 handle->offset = offset;
63e35b25 1773 handle->head = head;
b9cacc7b 1774 handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
0322cd6e 1775
b9cacc7b 1776 return 0;
7b732a75 1777
78d613eb
PZ
1778fail:
1779 __perf_output_wakeup(handle);
b9cacc7b
PZ
1780out:
1781 rcu_read_unlock();
7b732a75 1782
b9cacc7b
PZ
1783 return -ENOSPC;
1784}
7b732a75 1785
b9cacc7b
PZ
1786static void perf_output_copy(struct perf_output_handle *handle,
1787 void *buf, unsigned int len)
1788{
1789 unsigned int pages_mask;
1790 unsigned int offset;
1791 unsigned int size;
1792 void **pages;
1793
1794 offset = handle->offset;
1795 pages_mask = handle->data->nr_pages - 1;
1796 pages = handle->data->data_pages;
1797
1798 do {
1799 unsigned int page_offset;
1800 int nr;
1801
1802 nr = (offset >> PAGE_SHIFT) & pages_mask;
1803 page_offset = offset & (PAGE_SIZE - 1);
1804 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1805
1806 memcpy(pages[nr] + page_offset, buf, size);
1807
1808 len -= size;
1809 buf += size;
1810 offset += size;
1811 } while (len);
1812
1813 handle->offset = offset;
63e35b25
PZ
1814
1815 WARN_ON_ONCE(handle->offset > handle->head);
b9cacc7b
PZ
1816}
1817
5c148194
PZ
1818#define perf_output_put(handle, x) \
1819 perf_output_copy((handle), &(x), sizeof(x))
1820
78d613eb 1821static void perf_output_end(struct perf_output_handle *handle)
b9cacc7b 1822{
c457810a
PZ
1823 int wakeup_events = handle->counter->hw_event.wakeup_events;
1824
4c9e2542 1825 if (handle->overflow && wakeup_events) {
c457810a
PZ
1826 int events = atomic_inc_return(&handle->data->events);
1827 if (events >= wakeup_events) {
1828 atomic_sub(wakeup_events, &handle->data->events);
1829 __perf_output_wakeup(handle);
1830 }
1831 } else if (handle->wakeup)
78d613eb 1832 __perf_output_wakeup(handle);
7b732a75 1833 rcu_read_unlock();
b9cacc7b
PZ
1834}
1835
f6c7d5fe
PZ
1836static void perf_counter_output(struct perf_counter *counter,
1837 int nmi, struct pt_regs *regs)
7b732a75 1838{
5ed00415 1839 int ret;
8a057d84 1840 u64 record_type = counter->hw_event.record_type;
5ed00415
PZ
1841 struct perf_output_handle handle;
1842 struct perf_event_header header;
1843 u64 ip;
5c148194 1844 struct {
ea5d20cf 1845 u32 pid, tid;
5ed00415 1846 } tid_entry;
8a057d84
PZ
1847 struct {
1848 u64 event;
1849 u64 counter;
1850 } group_entry;
394ee076
PZ
1851 struct perf_callchain_entry *callchain = NULL;
1852 int callchain_size = 0;
339f7c90 1853 u64 time;
7b732a75 1854
8a057d84 1855 header.type = PERF_EVENT_COUNTER_OVERFLOW;
5ed00415 1856 header.size = sizeof(header);
7b732a75 1857
8a057d84
PZ
1858 if (record_type & PERF_RECORD_IP) {
1859 ip = instruction_pointer(regs);
1860 header.type |= __PERF_EVENT_IP;
1861 header.size += sizeof(ip);
1862 }
ea5d20cf 1863
8a057d84 1864 if (record_type & PERF_RECORD_TID) {
ea5d20cf 1865 /* namespace issues */
5ed00415
PZ
1866 tid_entry.pid = current->group_leader->pid;
1867 tid_entry.tid = current->pid;
1868
1869 header.type |= __PERF_EVENT_TID;
1870 header.size += sizeof(tid_entry);
1871 }
1872
8a057d84
PZ
1873 if (record_type & PERF_RECORD_GROUP) {
1874 header.type |= __PERF_EVENT_GROUP;
1875 header.size += sizeof(u64) +
1876 counter->nr_siblings * sizeof(group_entry);
1877 }
1878
1879 if (record_type & PERF_RECORD_CALLCHAIN) {
394ee076
PZ
1880 callchain = perf_callchain(regs);
1881
1882 if (callchain) {
9c03d88e 1883 callchain_size = (1 + callchain->nr) * sizeof(u64);
394ee076
PZ
1884
1885 header.type |= __PERF_EVENT_CALLCHAIN;
1886 header.size += callchain_size;
1887 }
1888 }
1889
339f7c90
PZ
1890 if (record_type & PERF_RECORD_TIME) {
1891 /*
1892 * Maybe do better on x86 and provide cpu_clock_nmi()
1893 */
1894 time = sched_clock();
1895
1896 header.type |= __PERF_EVENT_TIME;
1897 header.size += sizeof(u64);
1898 }
1899
4c9e2542 1900 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
5ed00415
PZ
1901 if (ret)
1902 return;
ea5d20cf 1903
5ed00415 1904 perf_output_put(&handle, header);
5c148194 1905
8a057d84
PZ
1906 if (record_type & PERF_RECORD_IP)
1907 perf_output_put(&handle, ip);
5c148194 1908
8a057d84
PZ
1909 if (record_type & PERF_RECORD_TID)
1910 perf_output_put(&handle, tid_entry);
5c148194 1911
8a057d84
PZ
1912 if (record_type & PERF_RECORD_GROUP) {
1913 struct perf_counter *leader, *sub;
1914 u64 nr = counter->nr_siblings;
5c148194 1915
8a057d84 1916 perf_output_put(&handle, nr);
0322cd6e 1917
8a057d84
PZ
1918 leader = counter->group_leader;
1919 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1920 if (sub != counter)
1921 sub->hw_ops->read(sub);
7b732a75 1922
8a057d84
PZ
1923 group_entry.event = sub->hw_event.config;
1924 group_entry.counter = atomic64_read(&sub->count);
7b732a75 1925
8a057d84
PZ
1926 perf_output_put(&handle, group_entry);
1927 }
0322cd6e 1928 }
5c148194 1929
8a057d84
PZ
1930 if (callchain)
1931 perf_output_copy(&handle, callchain, callchain_size);
0322cd6e 1932
339f7c90
PZ
1933 if (record_type & PERF_RECORD_TIME)
1934 perf_output_put(&handle, time);
1935
8a057d84 1936 perf_output_end(&handle);
0322cd6e
PZ
1937}
1938
0a4a9391
PZ
1939/*
1940 * mmap tracking
1941 */
1942
1943struct perf_mmap_event {
1944 struct file *file;
1945 char *file_name;
1946 int file_size;
1947
1948 struct {
1949 struct perf_event_header header;
1950
1951 u32 pid;
1952 u32 tid;
1953 u64 start;
1954 u64 len;
1955 u64 pgoff;
1956 } event;
1957};
1958
1959static void perf_counter_mmap_output(struct perf_counter *counter,
1960 struct perf_mmap_event *mmap_event)
1961{
1962 struct perf_output_handle handle;
1963 int size = mmap_event->event.header.size;
4c9e2542 1964 int ret = perf_output_begin(&handle, counter, size, 0, 0);
0a4a9391
PZ
1965
1966 if (ret)
1967 return;
1968
1969 perf_output_put(&handle, mmap_event->event);
1970 perf_output_copy(&handle, mmap_event->file_name,
1971 mmap_event->file_size);
78d613eb 1972 perf_output_end(&handle);
0a4a9391
PZ
1973}
1974
1975static int perf_counter_mmap_match(struct perf_counter *counter,
1976 struct perf_mmap_event *mmap_event)
1977{
1978 if (counter->hw_event.mmap &&
1979 mmap_event->event.header.type == PERF_EVENT_MMAP)
1980 return 1;
1981
1982 if (counter->hw_event.munmap &&
1983 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
1984 return 1;
1985
1986 return 0;
1987}
1988
1989static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
1990 struct perf_mmap_event *mmap_event)
1991{
1992 struct perf_counter *counter;
1993
1994 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
1995 return;
1996
1997 rcu_read_lock();
1998 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
1999 if (perf_counter_mmap_match(counter, mmap_event))
2000 perf_counter_mmap_output(counter, mmap_event);
2001 }
2002 rcu_read_unlock();
2003}
2004
2005static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2006{
2007 struct perf_cpu_context *cpuctx;
2008 struct file *file = mmap_event->file;
2009 unsigned int size;
2010 char tmp[16];
2011 char *buf = NULL;
2012 char *name;
2013
2014 if (file) {
2015 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2016 if (!buf) {
2017 name = strncpy(tmp, "//enomem", sizeof(tmp));
2018 goto got_name;
2019 }
2020 name = dentry_path(file->f_dentry, buf, PATH_MAX);
2021 if (IS_ERR(name)) {
2022 name = strncpy(tmp, "//toolong", sizeof(tmp));
2023 goto got_name;
2024 }
2025 } else {
2026 name = strncpy(tmp, "//anon", sizeof(tmp));
2027 goto got_name;
2028 }
2029
2030got_name:
2031 size = ALIGN(strlen(name), sizeof(u64));
2032
2033 mmap_event->file_name = name;
2034 mmap_event->file_size = size;
2035
2036 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2037
2038 cpuctx = &get_cpu_var(perf_cpu_context);
2039 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2040 put_cpu_var(perf_cpu_context);
2041
2042 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2043
2044 kfree(buf);
2045}
2046
2047void perf_counter_mmap(unsigned long addr, unsigned long len,
2048 unsigned long pgoff, struct file *file)
2049{
2050 struct perf_mmap_event mmap_event = {
2051 .file = file,
2052 .event = {
2053 .header = { .type = PERF_EVENT_MMAP, },
2054 .pid = current->group_leader->pid,
2055 .tid = current->pid,
2056 .start = addr,
2057 .len = len,
2058 .pgoff = pgoff,
2059 },
2060 };
2061
2062 perf_counter_mmap_event(&mmap_event);
2063}
2064
2065void perf_counter_munmap(unsigned long addr, unsigned long len,
2066 unsigned long pgoff, struct file *file)
2067{
2068 struct perf_mmap_event mmap_event = {
2069 .file = file,
2070 .event = {
2071 .header = { .type = PERF_EVENT_MUNMAP, },
2072 .pid = current->group_leader->pid,
2073 .tid = current->pid,
2074 .start = addr,
2075 .len = len,
2076 .pgoff = pgoff,
2077 },
2078 };
2079
2080 perf_counter_mmap_event(&mmap_event);
2081}
2082
f6c7d5fe
PZ
2083/*
2084 * Generic counter overflow handling.
2085 */
2086
2087int perf_counter_overflow(struct perf_counter *counter,
2088 int nmi, struct pt_regs *regs)
2089{
79f14641
PZ
2090 int events = atomic_read(&counter->event_limit);
2091 int ret = 0;
2092
4c9e2542 2093 counter->pending_kill = POLL_IN;
79f14641
PZ
2094 if (events && atomic_dec_and_test(&counter->event_limit)) {
2095 ret = 1;
4c9e2542 2096 counter->pending_kill = POLL_HUP;
79f14641
PZ
2097 if (nmi) {
2098 counter->pending_disable = 1;
2099 perf_pending_queue(&counter->pending,
2100 perf_pending_counter);
2101 } else
2102 perf_counter_disable(counter);
2103 }
2104
f6c7d5fe 2105 perf_counter_output(counter, nmi, regs);
79f14641 2106 return ret;
f6c7d5fe
PZ
2107}
2108
15dbf27c
PZ
2109/*
2110 * Generic software counter infrastructure
2111 */
2112
2113static void perf_swcounter_update(struct perf_counter *counter)
2114{
2115 struct hw_perf_counter *hwc = &counter->hw;
2116 u64 prev, now;
2117 s64 delta;
2118
2119again:
2120 prev = atomic64_read(&hwc->prev_count);
2121 now = atomic64_read(&hwc->count);
2122 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2123 goto again;
2124
2125 delta = now - prev;
2126
2127 atomic64_add(delta, &counter->count);
2128 atomic64_sub(delta, &hwc->period_left);
2129}
2130
2131static void perf_swcounter_set_period(struct perf_counter *counter)
2132{
2133 struct hw_perf_counter *hwc = &counter->hw;
2134 s64 left = atomic64_read(&hwc->period_left);
2135 s64 period = hwc->irq_period;
2136
2137 if (unlikely(left <= -period)) {
2138 left = period;
2139 atomic64_set(&hwc->period_left, left);
2140 }
2141
2142 if (unlikely(left <= 0)) {
2143 left += period;
2144 atomic64_add(period, &hwc->period_left);
2145 }
2146
2147 atomic64_set(&hwc->prev_count, -left);
2148 atomic64_set(&hwc->count, -left);
2149}
2150
d6d020e9
PZ
2151static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2152{
f6c7d5fe 2153 enum hrtimer_restart ret = HRTIMER_RESTART;
d6d020e9
PZ
2154 struct perf_counter *counter;
2155 struct pt_regs *regs;
2156
2157 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2158 counter->hw_ops->read(counter);
2159
2160 regs = get_irq_regs();
2161 /*
2162 * In case we exclude kernel IPs or are somehow not in interrupt
2163 * context, provide the next best thing, the user IP.
2164 */
2165 if ((counter->hw_event.exclude_kernel || !regs) &&
2166 !counter->hw_event.exclude_user)
2167 regs = task_pt_regs(current);
2168
f6c7d5fe
PZ
2169 if (regs) {
2170 if (perf_counter_overflow(counter, 0, regs))
2171 ret = HRTIMER_NORESTART;
2172 }
d6d020e9
PZ
2173
2174 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2175
f6c7d5fe 2176 return ret;
d6d020e9
PZ
2177}
2178
2179static void perf_swcounter_overflow(struct perf_counter *counter,
2180 int nmi, struct pt_regs *regs)
2181{
b8e83514
PZ
2182 perf_swcounter_update(counter);
2183 perf_swcounter_set_period(counter);
f6c7d5fe
PZ
2184 if (perf_counter_overflow(counter, nmi, regs))
2185 /* soft-disable the counter */
2186 ;
2187
d6d020e9
PZ
2188}
2189
15dbf27c 2190static int perf_swcounter_match(struct perf_counter *counter,
b8e83514
PZ
2191 enum perf_event_types type,
2192 u32 event, struct pt_regs *regs)
15dbf27c
PZ
2193{
2194 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2195 return 0;
2196
f4a2deb4 2197 if (perf_event_raw(&counter->hw_event))
b8e83514
PZ
2198 return 0;
2199
f4a2deb4 2200 if (perf_event_type(&counter->hw_event) != type)
15dbf27c
PZ
2201 return 0;
2202
f4a2deb4 2203 if (perf_event_id(&counter->hw_event) != event)
15dbf27c
PZ
2204 return 0;
2205
2206 if (counter->hw_event.exclude_user && user_mode(regs))
2207 return 0;
2208
2209 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2210 return 0;
2211
2212 return 1;
2213}
2214
d6d020e9
PZ
2215static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2216 int nmi, struct pt_regs *regs)
2217{
2218 int neg = atomic64_add_negative(nr, &counter->hw.count);
2219 if (counter->hw.irq_period && !neg)
2220 perf_swcounter_overflow(counter, nmi, regs);
2221}
2222
15dbf27c 2223static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
b8e83514
PZ
2224 enum perf_event_types type, u32 event,
2225 u64 nr, int nmi, struct pt_regs *regs)
15dbf27c
PZ
2226{
2227 struct perf_counter *counter;
15dbf27c 2228
01ef09d9 2229 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
15dbf27c
PZ
2230 return;
2231
592903cd
PZ
2232 rcu_read_lock();
2233 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
b8e83514 2234 if (perf_swcounter_match(counter, type, event, regs))
d6d020e9 2235 perf_swcounter_add(counter, nr, nmi, regs);
15dbf27c 2236 }
592903cd 2237 rcu_read_unlock();
15dbf27c
PZ
2238}
2239
96f6d444
PZ
2240static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2241{
2242 if (in_nmi())
2243 return &cpuctx->recursion[3];
2244
2245 if (in_irq())
2246 return &cpuctx->recursion[2];
2247
2248 if (in_softirq())
2249 return &cpuctx->recursion[1];
2250
2251 return &cpuctx->recursion[0];
2252}
2253
b8e83514
PZ
2254static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2255 u64 nr, int nmi, struct pt_regs *regs)
15dbf27c
PZ
2256{
2257 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
96f6d444
PZ
2258 int *recursion = perf_swcounter_recursion_context(cpuctx);
2259
2260 if (*recursion)
2261 goto out;
2262
2263 (*recursion)++;
2264 barrier();
15dbf27c 2265
b8e83514
PZ
2266 perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
2267 if (cpuctx->task_ctx) {
2268 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2269 nr, nmi, regs);
2270 }
15dbf27c 2271
96f6d444
PZ
2272 barrier();
2273 (*recursion)--;
2274
2275out:
15dbf27c
PZ
2276 put_cpu_var(perf_cpu_context);
2277}
2278
b8e83514
PZ
2279void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
2280{
2281 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
2282}
2283
15dbf27c
PZ
2284static void perf_swcounter_read(struct perf_counter *counter)
2285{
2286 perf_swcounter_update(counter);
2287}
2288
2289static int perf_swcounter_enable(struct perf_counter *counter)
2290{
2291 perf_swcounter_set_period(counter);
2292 return 0;
2293}
2294
2295static void perf_swcounter_disable(struct perf_counter *counter)
2296{
2297 perf_swcounter_update(counter);
2298}
2299
ac17dc8e
PZ
2300static const struct hw_perf_counter_ops perf_ops_generic = {
2301 .enable = perf_swcounter_enable,
2302 .disable = perf_swcounter_disable,
2303 .read = perf_swcounter_read,
2304};
2305
15dbf27c
PZ
2306/*
2307 * Software counter: cpu wall time clock
2308 */
2309
9abf8a08
PM
2310static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2311{
2312 int cpu = raw_smp_processor_id();
2313 s64 prev;
2314 u64 now;
2315
2316 now = cpu_clock(cpu);
2317 prev = atomic64_read(&counter->hw.prev_count);
2318 atomic64_set(&counter->hw.prev_count, now);
2319 atomic64_add(now - prev, &counter->count);
2320}
2321
d6d020e9
PZ
2322static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2323{
2324 struct hw_perf_counter *hwc = &counter->hw;
2325 int cpu = raw_smp_processor_id();
2326
2327 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
039fc91e
PZ
2328 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2329 hwc->hrtimer.function = perf_swcounter_hrtimer;
d6d020e9 2330 if (hwc->irq_period) {
d6d020e9
PZ
2331 __hrtimer_start_range_ns(&hwc->hrtimer,
2332 ns_to_ktime(hwc->irq_period), 0,
2333 HRTIMER_MODE_REL, 0);
2334 }
2335
2336 return 0;
2337}
2338
5c92d124
IM
2339static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2340{
d6d020e9 2341 hrtimer_cancel(&counter->hw.hrtimer);
9abf8a08 2342 cpu_clock_perf_counter_update(counter);
5c92d124
IM
2343}
2344
2345static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2346{
9abf8a08 2347 cpu_clock_perf_counter_update(counter);
5c92d124
IM
2348}
2349
2350static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
7671581f
IM
2351 .enable = cpu_clock_perf_counter_enable,
2352 .disable = cpu_clock_perf_counter_disable,
2353 .read = cpu_clock_perf_counter_read,
5c92d124
IM
2354};
2355
15dbf27c
PZ
2356/*
2357 * Software counter: task time clock
2358 */
2359
aa9c4c0f
IM
2360/*
2361 * Called from within the scheduler:
2362 */
2363static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
bae43c99 2364{
aa9c4c0f
IM
2365 struct task_struct *curr = counter->task;
2366 u64 delta;
2367
aa9c4c0f
IM
2368 delta = __task_delta_exec(curr, update);
2369
2370 return curr->se.sum_exec_runtime + delta;
2371}
2372
2373static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2374{
2375 u64 prev;
8cb391e8
IM
2376 s64 delta;
2377
2378 prev = atomic64_read(&counter->hw.prev_count);
8cb391e8
IM
2379
2380 atomic64_set(&counter->hw.prev_count, now);
2381
2382 delta = now - prev;
8cb391e8
IM
2383
2384 atomic64_add(delta, &counter->count);
bae43c99
IM
2385}
2386
95cdd2e7 2387static int task_clock_perf_counter_enable(struct perf_counter *counter)
8cb391e8 2388{
d6d020e9
PZ
2389 struct hw_perf_counter *hwc = &counter->hw;
2390
2391 atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
039fc91e
PZ
2392 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2393 hwc->hrtimer.function = perf_swcounter_hrtimer;
d6d020e9 2394 if (hwc->irq_period) {
d6d020e9
PZ
2395 __hrtimer_start_range_ns(&hwc->hrtimer,
2396 ns_to_ktime(hwc->irq_period), 0,
2397 HRTIMER_MODE_REL, 0);
2398 }
95cdd2e7
IM
2399
2400 return 0;
8cb391e8
IM
2401}
2402
2403static void task_clock_perf_counter_disable(struct perf_counter *counter)
bae43c99 2404{
d6d020e9
PZ
2405 hrtimer_cancel(&counter->hw.hrtimer);
2406 task_clock_perf_counter_update(counter,
2407 task_clock_perf_counter_val(counter, 0));
2408}
aa9c4c0f 2409
d6d020e9
PZ
2410static void task_clock_perf_counter_read(struct perf_counter *counter)
2411{
2412 task_clock_perf_counter_update(counter,
2413 task_clock_perf_counter_val(counter, 1));
bae43c99
IM
2414}
2415
2416static const struct hw_perf_counter_ops perf_ops_task_clock = {
7671581f
IM
2417 .enable = task_clock_perf_counter_enable,
2418 .disable = task_clock_perf_counter_disable,
2419 .read = task_clock_perf_counter_read,
bae43c99
IM
2420};
2421
15dbf27c
PZ
2422/*
2423 * Software counter: cpu migrations
2424 */
2425
23a185ca 2426static inline u64 get_cpu_migrations(struct perf_counter *counter)
6c594c21 2427{
23a185ca
PM
2428 struct task_struct *curr = counter->ctx->task;
2429
2430 if (curr)
2431 return curr->se.nr_migrations;
2432 return cpu_nr_migrations(smp_processor_id());
6c594c21
IM
2433}
2434
2435static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2436{
2437 u64 prev, now;
2438 s64 delta;
2439
2440 prev = atomic64_read(&counter->hw.prev_count);
23a185ca 2441 now = get_cpu_migrations(counter);
6c594c21
IM
2442
2443 atomic64_set(&counter->hw.prev_count, now);
2444
2445 delta = now - prev;
6c594c21
IM
2446
2447 atomic64_add(delta, &counter->count);
2448}
2449
2450static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2451{
2452 cpu_migrations_perf_counter_update(counter);
2453}
2454
95cdd2e7 2455static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
6c594c21 2456{
c07c99b6
PM
2457 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2458 atomic64_set(&counter->hw.prev_count,
2459 get_cpu_migrations(counter));
95cdd2e7 2460 return 0;
6c594c21
IM
2461}
2462
2463static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2464{
2465 cpu_migrations_perf_counter_update(counter);
2466}
2467
2468static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
7671581f
IM
2469 .enable = cpu_migrations_perf_counter_enable,
2470 .disable = cpu_migrations_perf_counter_disable,
2471 .read = cpu_migrations_perf_counter_read,
6c594c21
IM
2472};
2473
e077df4f
PZ
2474#ifdef CONFIG_EVENT_PROFILE
2475void perf_tpcounter_event(int event_id)
2476{
b8e83514
PZ
2477 struct pt_regs *regs = get_irq_regs();
2478
2479 if (!regs)
2480 regs = task_pt_regs(current);
2481
2482 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
e077df4f
PZ
2483}
2484
2485extern int ftrace_profile_enable(int);
2486extern void ftrace_profile_disable(int);
2487
2488static void tp_perf_counter_destroy(struct perf_counter *counter)
2489{
f4a2deb4 2490 ftrace_profile_disable(perf_event_id(&counter->hw_event));
e077df4f
PZ
2491}
2492
2493static const struct hw_perf_counter_ops *
2494tp_perf_counter_init(struct perf_counter *counter)
2495{
f4a2deb4 2496 int event_id = perf_event_id(&counter->hw_event);
e077df4f
PZ
2497 int ret;
2498
2499 ret = ftrace_profile_enable(event_id);
2500 if (ret)
2501 return NULL;
2502
2503 counter->destroy = tp_perf_counter_destroy;
b8e83514 2504 counter->hw.irq_period = counter->hw_event.irq_period;
e077df4f
PZ
2505
2506 return &perf_ops_generic;
2507}
2508#else
2509static const struct hw_perf_counter_ops *
2510tp_perf_counter_init(struct perf_counter *counter)
2511{
2512 return NULL;
2513}
2514#endif
2515
5c92d124
IM
2516static const struct hw_perf_counter_ops *
2517sw_perf_counter_init(struct perf_counter *counter)
2518{
15dbf27c 2519 struct perf_counter_hw_event *hw_event = &counter->hw_event;
5c92d124 2520 const struct hw_perf_counter_ops *hw_ops = NULL;
15dbf27c 2521 struct hw_perf_counter *hwc = &counter->hw;
5c92d124 2522
0475f9ea
PM
2523 /*
2524 * Software counters (currently) can't in general distinguish
2525 * between user, kernel and hypervisor events.
2526 * However, context switches and cpu migrations are considered
2527 * to be kernel events, and page faults are never hypervisor
2528 * events.
2529 */
f4a2deb4 2530 switch (perf_event_id(&counter->hw_event)) {
5c92d124 2531 case PERF_COUNT_CPU_CLOCK:
d6d020e9
PZ
2532 hw_ops = &perf_ops_cpu_clock;
2533
2534 if (hw_event->irq_period && hw_event->irq_period < 10000)
2535 hw_event->irq_period = 10000;
5c92d124 2536 break;
bae43c99 2537 case PERF_COUNT_TASK_CLOCK:
23a185ca
PM
2538 /*
2539 * If the user instantiates this as a per-cpu counter,
2540 * use the cpu_clock counter instead.
2541 */
2542 if (counter->ctx->task)
2543 hw_ops = &perf_ops_task_clock;
2544 else
2545 hw_ops = &perf_ops_cpu_clock;
d6d020e9
PZ
2546
2547 if (hw_event->irq_period && hw_event->irq_period < 10000)
2548 hw_event->irq_period = 10000;
bae43c99 2549 break;
e06c61a8 2550 case PERF_COUNT_PAGE_FAULTS:
ac17dc8e
PZ
2551 case PERF_COUNT_PAGE_FAULTS_MIN:
2552 case PERF_COUNT_PAGE_FAULTS_MAJ:
5d6a27d8 2553 case PERF_COUNT_CONTEXT_SWITCHES:
4a0deca6 2554 hw_ops = &perf_ops_generic;
5d6a27d8 2555 break;
6c594c21 2556 case PERF_COUNT_CPU_MIGRATIONS:
0475f9ea
PM
2557 if (!counter->hw_event.exclude_kernel)
2558 hw_ops = &perf_ops_cpu_migrations;
6c594c21 2559 break;
5c92d124 2560 }
15dbf27c
PZ
2561
2562 if (hw_ops)
2563 hwc->irq_period = hw_event->irq_period;
2564
5c92d124
IM
2565 return hw_ops;
2566}
2567
0793a61d
TG
2568/*
2569 * Allocate and initialize a counter structure
2570 */
2571static struct perf_counter *
04289bb9
IM
2572perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2573 int cpu,
23a185ca 2574 struct perf_counter_context *ctx,
9b51f66d
IM
2575 struct perf_counter *group_leader,
2576 gfp_t gfpflags)
0793a61d 2577{
5c92d124 2578 const struct hw_perf_counter_ops *hw_ops;
621a01ea 2579 struct perf_counter *counter;
d5d2bc0d 2580 long err;
0793a61d 2581
9b51f66d 2582 counter = kzalloc(sizeof(*counter), gfpflags);
0793a61d 2583 if (!counter)
d5d2bc0d 2584 return ERR_PTR(-ENOMEM);
0793a61d 2585
04289bb9
IM
2586 /*
2587 * Single counters are their own group leaders, with an
2588 * empty sibling list:
2589 */
2590 if (!group_leader)
2591 group_leader = counter;
2592
0793a61d 2593 mutex_init(&counter->mutex);
04289bb9 2594 INIT_LIST_HEAD(&counter->list_entry);
592903cd 2595 INIT_LIST_HEAD(&counter->event_entry);
04289bb9 2596 INIT_LIST_HEAD(&counter->sibling_list);
0793a61d
TG
2597 init_waitqueue_head(&counter->waitq);
2598
7b732a75
PZ
2599 mutex_init(&counter->mmap_mutex);
2600
d859e29f
PM
2601 INIT_LIST_HEAD(&counter->child_list);
2602
9f66a381
IM
2603 counter->cpu = cpu;
2604 counter->hw_event = *hw_event;
04289bb9 2605 counter->group_leader = group_leader;
621a01ea 2606 counter->hw_ops = NULL;
23a185ca 2607 counter->ctx = ctx;
621a01ea 2608
235c7fc7 2609 counter->state = PERF_COUNTER_STATE_INACTIVE;
a86ed508
IM
2610 if (hw_event->disabled)
2611 counter->state = PERF_COUNTER_STATE_OFF;
2612
5c92d124 2613 hw_ops = NULL;
b8e83514 2614
f4a2deb4 2615 if (perf_event_raw(hw_event)) {
b8e83514 2616 hw_ops = hw_perf_counter_init(counter);
f4a2deb4
PZ
2617 goto done;
2618 }
2619
2620 switch (perf_event_type(hw_event)) {
b8e83514 2621 case PERF_TYPE_HARDWARE:
5c92d124 2622 hw_ops = hw_perf_counter_init(counter);
b8e83514
PZ
2623 break;
2624
2625 case PERF_TYPE_SOFTWARE:
2626 hw_ops = sw_perf_counter_init(counter);
2627 break;
2628
2629 case PERF_TYPE_TRACEPOINT:
2630 hw_ops = tp_perf_counter_init(counter);
2631 break;
2632 }
d5d2bc0d
PM
2633done:
2634 err = 0;
2635 if (!hw_ops)
2636 err = -EINVAL;
2637 else if (IS_ERR(hw_ops))
2638 err = PTR_ERR(hw_ops);
5c92d124 2639
d5d2bc0d 2640 if (err) {
621a01ea 2641 kfree(counter);
d5d2bc0d 2642 return ERR_PTR(err);
621a01ea 2643 }
d5d2bc0d 2644
621a01ea 2645 counter->hw_ops = hw_ops;
0793a61d
TG
2646
2647 return counter;
2648}
2649
2650/**
2743a5b0 2651 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
9f66a381
IM
2652 *
2653 * @hw_event_uptr: event type attributes for monitoring/sampling
0793a61d 2654 * @pid: target pid
9f66a381
IM
2655 * @cpu: target cpu
2656 * @group_fd: group leader counter fd
0793a61d 2657 */
2743a5b0 2658SYSCALL_DEFINE5(perf_counter_open,
f3dfd265 2659 const struct perf_counter_hw_event __user *, hw_event_uptr,
2743a5b0 2660 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
0793a61d 2661{
04289bb9 2662 struct perf_counter *counter, *group_leader;
9f66a381 2663 struct perf_counter_hw_event hw_event;
04289bb9 2664 struct perf_counter_context *ctx;
9b51f66d 2665 struct file *counter_file = NULL;
04289bb9
IM
2666 struct file *group_file = NULL;
2667 int fput_needed = 0;
9b51f66d 2668 int fput_needed2 = 0;
0793a61d
TG
2669 int ret;
2670
2743a5b0
PM
2671 /* for future expandability... */
2672 if (flags)
2673 return -EINVAL;
2674
9f66a381 2675 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
eab656ae
TG
2676 return -EFAULT;
2677
04289bb9 2678 /*
ccff286d
IM
2679 * Get the target context (task or percpu):
2680 */
2681 ctx = find_get_context(pid, cpu);
2682 if (IS_ERR(ctx))
2683 return PTR_ERR(ctx);
2684
2685 /*
2686 * Look up the group leader (we will attach this counter to it):
04289bb9
IM
2687 */
2688 group_leader = NULL;
2689 if (group_fd != -1) {
2690 ret = -EINVAL;
2691 group_file = fget_light(group_fd, &fput_needed);
2692 if (!group_file)
ccff286d 2693 goto err_put_context;
04289bb9 2694 if (group_file->f_op != &perf_fops)
ccff286d 2695 goto err_put_context;
04289bb9
IM
2696
2697 group_leader = group_file->private_data;
2698 /*
ccff286d
IM
2699 * Do not allow a recursive hierarchy (this new sibling
2700 * becoming part of another group-sibling):
2701 */
2702 if (group_leader->group_leader != group_leader)
2703 goto err_put_context;
2704 /*
2705 * Do not allow to attach to a group in a different
2706 * task or CPU context:
04289bb9 2707 */
ccff286d
IM
2708 if (group_leader->ctx != ctx)
2709 goto err_put_context;
3b6f9e5c
PM
2710 /*
2711 * Only a group leader can be exclusive or pinned
2712 */
2713 if (hw_event.exclusive || hw_event.pinned)
2714 goto err_put_context;
04289bb9
IM
2715 }
2716
23a185ca
PM
2717 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2718 GFP_KERNEL);
d5d2bc0d
PM
2719 ret = PTR_ERR(counter);
2720 if (IS_ERR(counter))
0793a61d
TG
2721 goto err_put_context;
2722
0793a61d
TG
2723 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2724 if (ret < 0)
9b51f66d
IM
2725 goto err_free_put_context;
2726
2727 counter_file = fget_light(ret, &fput_needed2);
2728 if (!counter_file)
2729 goto err_free_put_context;
2730
2731 counter->filp = counter_file;
d859e29f 2732 mutex_lock(&ctx->mutex);
9b51f66d 2733 perf_install_in_context(ctx, counter, cpu);
d859e29f 2734 mutex_unlock(&ctx->mutex);
9b51f66d
IM
2735
2736 fput_light(counter_file, fput_needed2);
0793a61d 2737
04289bb9
IM
2738out_fput:
2739 fput_light(group_file, fput_needed);
2740
0793a61d
TG
2741 return ret;
2742
9b51f66d 2743err_free_put_context:
0793a61d
TG
2744 kfree(counter);
2745
2746err_put_context:
2747 put_context(ctx);
2748
04289bb9 2749 goto out_fput;
0793a61d
TG
2750}
2751
9b51f66d
IM
2752/*
2753 * Initialize the perf_counter context in a task_struct:
2754 */
2755static void
2756__perf_counter_init_context(struct perf_counter_context *ctx,
2757 struct task_struct *task)
2758{
2759 memset(ctx, 0, sizeof(*ctx));
2760 spin_lock_init(&ctx->lock);
d859e29f 2761 mutex_init(&ctx->mutex);
9b51f66d 2762 INIT_LIST_HEAD(&ctx->counter_list);
592903cd 2763 INIT_LIST_HEAD(&ctx->event_list);
9b51f66d
IM
2764 ctx->task = task;
2765}
2766
2767/*
2768 * inherit a counter from parent task to child task:
2769 */
d859e29f 2770static struct perf_counter *
9b51f66d
IM
2771inherit_counter(struct perf_counter *parent_counter,
2772 struct task_struct *parent,
2773 struct perf_counter_context *parent_ctx,
2774 struct task_struct *child,
d859e29f 2775 struct perf_counter *group_leader,
9b51f66d
IM
2776 struct perf_counter_context *child_ctx)
2777{
2778 struct perf_counter *child_counter;
2779
d859e29f
PM
2780 /*
2781 * Instead of creating recursive hierarchies of counters,
2782 * we link inherited counters back to the original parent,
2783 * which has a filp for sure, which we use as the reference
2784 * count:
2785 */
2786 if (parent_counter->parent)
2787 parent_counter = parent_counter->parent;
2788
9b51f66d 2789 child_counter = perf_counter_alloc(&parent_counter->hw_event,
23a185ca
PM
2790 parent_counter->cpu, child_ctx,
2791 group_leader, GFP_KERNEL);
d5d2bc0d
PM
2792 if (IS_ERR(child_counter))
2793 return child_counter;
9b51f66d
IM
2794
2795 /*
2796 * Link it up in the child's context:
2797 */
9b51f66d 2798 child_counter->task = child;
53cfbf59 2799 add_counter_to_ctx(child_counter, child_ctx);
9b51f66d
IM
2800
2801 child_counter->parent = parent_counter;
9b51f66d
IM
2802 /*
2803 * inherit into child's child as well:
2804 */
2805 child_counter->hw_event.inherit = 1;
2806
2807 /*
2808 * Get a reference to the parent filp - we will fput it
2809 * when the child counter exits. This is safe to do because
2810 * we are in the parent and we know that the filp still
2811 * exists and has a nonzero count:
2812 */
2813 atomic_long_inc(&parent_counter->filp->f_count);
2814
d859e29f
PM
2815 /*
2816 * Link this into the parent counter's child list
2817 */
2818 mutex_lock(&parent_counter->mutex);
2819 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2820
2821 /*
2822 * Make the child state follow the state of the parent counter,
2823 * not its hw_event.disabled bit. We hold the parent's mutex,
2824 * so we won't race with perf_counter_{en,dis}able_family.
2825 */
2826 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2827 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2828 else
2829 child_counter->state = PERF_COUNTER_STATE_OFF;
2830
2831 mutex_unlock(&parent_counter->mutex);
2832
2833 return child_counter;
2834}
2835
2836static int inherit_group(struct perf_counter *parent_counter,
2837 struct task_struct *parent,
2838 struct perf_counter_context *parent_ctx,
2839 struct task_struct *child,
2840 struct perf_counter_context *child_ctx)
2841{
2842 struct perf_counter *leader;
2843 struct perf_counter *sub;
d5d2bc0d 2844 struct perf_counter *child_ctr;
d859e29f
PM
2845
2846 leader = inherit_counter(parent_counter, parent, parent_ctx,
2847 child, NULL, child_ctx);
d5d2bc0d
PM
2848 if (IS_ERR(leader))
2849 return PTR_ERR(leader);
d859e29f 2850 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
d5d2bc0d
PM
2851 child_ctr = inherit_counter(sub, parent, parent_ctx,
2852 child, leader, child_ctx);
2853 if (IS_ERR(child_ctr))
2854 return PTR_ERR(child_ctr);
d859e29f 2855 }
9b51f66d
IM
2856 return 0;
2857}
2858
d859e29f
PM
2859static void sync_child_counter(struct perf_counter *child_counter,
2860 struct perf_counter *parent_counter)
2861{
2862 u64 parent_val, child_val;
2863
2864 parent_val = atomic64_read(&parent_counter->count);
2865 child_val = atomic64_read(&child_counter->count);
2866
2867 /*
2868 * Add back the child's count to the parent's count:
2869 */
2870 atomic64_add(child_val, &parent_counter->count);
53cfbf59
PM
2871 atomic64_add(child_counter->total_time_enabled,
2872 &parent_counter->child_total_time_enabled);
2873 atomic64_add(child_counter->total_time_running,
2874 &parent_counter->child_total_time_running);
d859e29f
PM
2875
2876 /*
2877 * Remove this counter from the parent's list
2878 */
2879 mutex_lock(&parent_counter->mutex);
2880 list_del_init(&child_counter->child_list);
2881 mutex_unlock(&parent_counter->mutex);
2882
2883 /*
2884 * Release the parent counter, if this was the last
2885 * reference to it.
2886 */
2887 fput(parent_counter->filp);
2888}
2889
9b51f66d
IM
2890static void
2891__perf_counter_exit_task(struct task_struct *child,
2892 struct perf_counter *child_counter,
2893 struct perf_counter_context *child_ctx)
2894{
2895 struct perf_counter *parent_counter;
d859e29f 2896 struct perf_counter *sub, *tmp;
9b51f66d
IM
2897
2898 /*
235c7fc7
IM
2899 * If we do not self-reap then we have to wait for the
2900 * child task to unschedule (it will happen for sure),
2901 * so that its counter is at its final count. (This
2902 * condition triggers rarely - child tasks usually get
2903 * off their CPU before the parent has a chance to
2904 * get this far into the reaping action)
9b51f66d 2905 */
235c7fc7
IM
2906 if (child != current) {
2907 wait_task_inactive(child, 0);
2908 list_del_init(&child_counter->list_entry);
53cfbf59 2909 update_counter_times(child_counter);
235c7fc7 2910 } else {
0cc0c027 2911 struct perf_cpu_context *cpuctx;
235c7fc7
IM
2912 unsigned long flags;
2913 u64 perf_flags;
2914
2915 /*
2916 * Disable and unlink this counter.
2917 *
2918 * Be careful about zapping the list - IRQ/NMI context
2919 * could still be processing it:
2920 */
2921 curr_rq_lock_irq_save(&flags);
2922 perf_flags = hw_perf_save_disable();
0cc0c027
IM
2923
2924 cpuctx = &__get_cpu_var(perf_cpu_context);
2925
d859e29f 2926 group_sched_out(child_counter, cpuctx, child_ctx);
53cfbf59 2927 update_counter_times(child_counter);
0cc0c027 2928
235c7fc7 2929 list_del_init(&child_counter->list_entry);
0cc0c027 2930
235c7fc7 2931 child_ctx->nr_counters--;
9b51f66d 2932
235c7fc7
IM
2933 hw_perf_restore(perf_flags);
2934 curr_rq_unlock_irq_restore(&flags);
2935 }
9b51f66d
IM
2936
2937 parent_counter = child_counter->parent;
2938 /*
2939 * It can happen that parent exits first, and has counters
2940 * that are still around due to the child reference. These
2941 * counters need to be zapped - but otherwise linger.
2942 */
d859e29f
PM
2943 if (parent_counter) {
2944 sync_child_counter(child_counter, parent_counter);
2945 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2946 list_entry) {
4bcf349a 2947 if (sub->parent) {
d859e29f 2948 sync_child_counter(sub, sub->parent);
f1600952 2949 free_counter(sub);
4bcf349a 2950 }
d859e29f 2951 }
f1600952 2952 free_counter(child_counter);
4bcf349a 2953 }
9b51f66d
IM
2954}
2955
2956/*
d859e29f 2957 * When a child task exits, feed back counter values to parent counters.
9b51f66d 2958 *
d859e29f 2959 * Note: we may be running in child context, but the PID is not hashed
9b51f66d
IM
2960 * anymore so new counters will not be added.
2961 */
2962void perf_counter_exit_task(struct task_struct *child)
2963{
2964 struct perf_counter *child_counter, *tmp;
2965 struct perf_counter_context *child_ctx;
2966
2967 child_ctx = &child->perf_counter_ctx;
2968
2969 if (likely(!child_ctx->nr_counters))
2970 return;
2971
2972 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2973 list_entry)
2974 __perf_counter_exit_task(child, child_counter, child_ctx);
2975}
2976
2977/*
2978 * Initialize the perf_counter context in task_struct
2979 */
2980void perf_counter_init_task(struct task_struct *child)
2981{
2982 struct perf_counter_context *child_ctx, *parent_ctx;
d859e29f 2983 struct perf_counter *counter;
9b51f66d 2984 struct task_struct *parent = current;
9b51f66d
IM
2985
2986 child_ctx = &child->perf_counter_ctx;
2987 parent_ctx = &parent->perf_counter_ctx;
2988
2989 __perf_counter_init_context(child_ctx, child);
2990
2991 /*
2992 * This is executed from the parent task context, so inherit
2993 * counters that have been marked for cloning:
2994 */
2995
2996 if (likely(!parent_ctx->nr_counters))
2997 return;
2998
2999 /*
3000 * Lock the parent list. No need to lock the child - not PID
3001 * hashed yet and not running, so nobody can access it.
3002 */
d859e29f 3003 mutex_lock(&parent_ctx->mutex);
9b51f66d
IM
3004
3005 /*
3006 * We dont have to disable NMIs - we are only looking at
3007 * the list, not manipulating it:
3008 */
3009 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
d859e29f 3010 if (!counter->hw_event.inherit)
9b51f66d
IM
3011 continue;
3012
d859e29f 3013 if (inherit_group(counter, parent,
9b51f66d
IM
3014 parent_ctx, child, child_ctx))
3015 break;
3016 }
3017
d859e29f 3018 mutex_unlock(&parent_ctx->mutex);
9b51f66d
IM
3019}
3020
04289bb9 3021static void __cpuinit perf_counter_init_cpu(int cpu)
0793a61d 3022{
04289bb9 3023 struct perf_cpu_context *cpuctx;
0793a61d 3024
04289bb9
IM
3025 cpuctx = &per_cpu(perf_cpu_context, cpu);
3026 __perf_counter_init_context(&cpuctx->ctx, NULL);
0793a61d
TG
3027
3028 mutex_lock(&perf_resource_mutex);
04289bb9 3029 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
0793a61d 3030 mutex_unlock(&perf_resource_mutex);
04289bb9 3031
01d0287f 3032 hw_perf_counter_setup(cpu);
0793a61d
TG
3033}
3034
3035#ifdef CONFIG_HOTPLUG_CPU
04289bb9 3036static void __perf_counter_exit_cpu(void *info)
0793a61d
TG
3037{
3038 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3039 struct perf_counter_context *ctx = &cpuctx->ctx;
3040 struct perf_counter *counter, *tmp;
3041
04289bb9
IM
3042 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3043 __perf_counter_remove_from_context(counter);
0793a61d 3044}
04289bb9 3045static void perf_counter_exit_cpu(int cpu)
0793a61d 3046{
d859e29f
PM
3047 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3048 struct perf_counter_context *ctx = &cpuctx->ctx;
3049
3050 mutex_lock(&ctx->mutex);
04289bb9 3051 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
d859e29f 3052 mutex_unlock(&ctx->mutex);
0793a61d
TG
3053}
3054#else
04289bb9 3055static inline void perf_counter_exit_cpu(int cpu) { }
0793a61d
TG
3056#endif
3057
3058static int __cpuinit
3059perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3060{
3061 unsigned int cpu = (long)hcpu;
3062
3063 switch (action) {
3064
3065 case CPU_UP_PREPARE:
3066 case CPU_UP_PREPARE_FROZEN:
04289bb9 3067 perf_counter_init_cpu(cpu);
0793a61d
TG
3068 break;
3069
3070 case CPU_DOWN_PREPARE:
3071 case CPU_DOWN_PREPARE_FROZEN:
04289bb9 3072 perf_counter_exit_cpu(cpu);
0793a61d
TG
3073 break;
3074
3075 default:
3076 break;
3077 }
3078
3079 return NOTIFY_OK;
3080}
3081
3082static struct notifier_block __cpuinitdata perf_cpu_nb = {
3083 .notifier_call = perf_cpu_notify,
3084};
3085
3086static int __init perf_counter_init(void)
3087{
3088 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3089 (void *)(long)smp_processor_id());
3090 register_cpu_notifier(&perf_cpu_nb);
3091
3092 return 0;
3093}
3094early_initcall(perf_counter_init);
3095
3096static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3097{
3098 return sprintf(buf, "%d\n", perf_reserved_percpu);
3099}
3100
3101static ssize_t
3102perf_set_reserve_percpu(struct sysdev_class *class,
3103 const char *buf,
3104 size_t count)
3105{
3106 struct perf_cpu_context *cpuctx;
3107 unsigned long val;
3108 int err, cpu, mpt;
3109
3110 err = strict_strtoul(buf, 10, &val);
3111 if (err)
3112 return err;
3113 if (val > perf_max_counters)
3114 return -EINVAL;
3115
3116 mutex_lock(&perf_resource_mutex);
3117 perf_reserved_percpu = val;
3118 for_each_online_cpu(cpu) {
3119 cpuctx = &per_cpu(perf_cpu_context, cpu);
3120 spin_lock_irq(&cpuctx->ctx.lock);
3121 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3122 perf_max_counters - perf_reserved_percpu);
3123 cpuctx->max_pertask = mpt;
3124 spin_unlock_irq(&cpuctx->ctx.lock);
3125 }
3126 mutex_unlock(&perf_resource_mutex);
3127
3128 return count;
3129}
3130
3131static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3132{
3133 return sprintf(buf, "%d\n", perf_overcommit);
3134}
3135
3136static ssize_t
3137perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3138{
3139 unsigned long val;
3140 int err;
3141
3142 err = strict_strtoul(buf, 10, &val);
3143 if (err)
3144 return err;
3145 if (val > 1)
3146 return -EINVAL;
3147
3148 mutex_lock(&perf_resource_mutex);
3149 perf_overcommit = val;
3150 mutex_unlock(&perf_resource_mutex);
3151
3152 return count;
3153}
3154
3155static SYSDEV_CLASS_ATTR(
3156 reserve_percpu,
3157 0644,
3158 perf_show_reserve_percpu,
3159 perf_set_reserve_percpu
3160 );
3161
3162static SYSDEV_CLASS_ATTR(
3163 overcommit,
3164 0644,
3165 perf_show_overcommit,
3166 perf_set_overcommit
3167 );
3168
3169static struct attribute *perfclass_attrs[] = {
3170 &attr_reserve_percpu.attr,
3171 &attr_overcommit.attr,
3172 NULL
3173};
3174
3175static struct attribute_group perfclass_attr_group = {
3176 .attrs = perfclass_attrs,
3177 .name = "perf_counters",
3178};
3179
3180static int __init perf_counter_sysfs_init(void)
3181{
3182 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3183 &perfclass_attr_group);
3184}
3185device_initcall(perf_counter_sysfs_init);