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