blob: faf671b29566114f15da4a7cc0bb0aed17ef597d [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6 *
7 * For licencing details see kernel-base/COPYING
8 */
9
10#include <linux/fs.h>
11#include <linux/cpu.h>
12#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010013#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/poll.h>
15#include <linux/sysfs.h>
16#include <linux/ptrace.h>
17#include <linux/percpu.h>
18#include <linux/uaccess.h>
19#include <linux/syscalls.h>
20#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010021#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010022#include <linux/perf_counter.h>
23
24/*
25 * Each CPU has a list of per CPU counters:
26 */
27DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
28
Ingo Molnar088e2852008-12-14 20:21:00 +010029int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010030static int perf_reserved_percpu __read_mostly;
31static int perf_overcommit __read_mostly = 1;
32
33/*
34 * Mutex for (sysadmin-configurable) counter reservations:
35 */
36static DEFINE_MUTEX(perf_resource_mutex);
37
38/*
39 * Architecture provided APIs - weak aliases:
40 */
Ingo Molnar5c92d122008-12-11 13:21:10 +010041extern __weak const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +010042hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010043{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110044 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010045}
46
Ingo Molnar01b28382008-12-11 13:45:51 +010047u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080048void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110049void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110050int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
51 struct perf_cpu_context *cpuctx,
52 struct perf_counter_context *ctx, int cpu)
53{
54 return 0;
55}
Thomas Gleixner0793a612008-12-04 20:12:29 +010056
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110057void __weak perf_counter_print_debug(void) { }
58
Ingo Molnar04289bb2008-12-11 08:38:42 +010059static void
60list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
61{
62 struct perf_counter *group_leader = counter->group_leader;
63
64 /*
65 * Depending on whether it is a standalone or sibling counter,
66 * add it straight to the context's counter list, or to the group
67 * leader's sibling list:
68 */
69 if (counter->group_leader == counter)
70 list_add_tail(&counter->list_entry, &ctx->counter_list);
71 else
72 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
73}
74
75static void
76list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
77{
78 struct perf_counter *sibling, *tmp;
79
80 list_del_init(&counter->list_entry);
81
Ingo Molnar04289bb2008-12-11 08:38:42 +010082 /*
83 * If this was a group counter with sibling counters then
84 * upgrade the siblings to singleton counters by adding them
85 * to the context list directly:
86 */
87 list_for_each_entry_safe(sibling, tmp,
88 &counter->sibling_list, list_entry) {
89
90 list_del_init(&sibling->list_entry);
91 list_add_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010092 sibling->group_leader = sibling;
93 }
94}
95
Paul Mackerras3b6f9e52009-01-14 21:00:30 +110096static void
97counter_sched_out(struct perf_counter *counter,
98 struct perf_cpu_context *cpuctx,
99 struct perf_counter_context *ctx)
100{
101 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
102 return;
103
104 counter->state = PERF_COUNTER_STATE_INACTIVE;
105 counter->hw_ops->disable(counter);
106 counter->oncpu = -1;
107
108 if (!is_software_counter(counter))
109 cpuctx->active_oncpu--;
110 ctx->nr_active--;
111 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
112 cpuctx->exclusive = 0;
113}
114
Thomas Gleixner0793a612008-12-04 20:12:29 +0100115/*
116 * Cross CPU call to remove a performance counter
117 *
118 * We disable the counter on the hardware level first. After that we
119 * remove it from the context list.
120 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100121static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100122{
123 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
124 struct perf_counter *counter = info;
125 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100126 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100127 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100128
129 /*
130 * If this is a task context, we need to check whether it is
131 * the current task context of this cpu. If not it has been
132 * scheduled out before the smp call arrived.
133 */
134 if (ctx->task && cpuctx->task_ctx != ctx)
135 return;
136
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100137 curr_rq_lock_irq_save(&flags);
138 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100139
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100140 counter_sched_out(counter, cpuctx, ctx);
141
142 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100143 ctx->nr_counters--;
144
145 /*
146 * Protect the list operation against NMI by disabling the
147 * counters on a global level. NOP for non NMI based counters.
148 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100149 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100150 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100151 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100152
153 if (!ctx->task) {
154 /*
155 * Allow more per task counters with respect to the
156 * reservation:
157 */
158 cpuctx->max_pertask =
159 min(perf_max_counters - ctx->nr_counters,
160 perf_max_counters - perf_reserved_percpu);
161 }
162
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100163 spin_unlock(&ctx->lock);
164 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100165}
166
167
168/*
169 * Remove the counter from a task's (or a CPU's) list of counters.
170 *
171 * Must be called with counter->mutex held.
172 *
173 * CPU counters are removed with a smp call. For task counters we only
174 * call when the task is on a CPU.
175 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100176static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100177{
178 struct perf_counter_context *ctx = counter->ctx;
179 struct task_struct *task = ctx->task;
180
181 if (!task) {
182 /*
183 * Per cpu counters are removed via an smp call and
184 * the removal is always sucessful.
185 */
186 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100187 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100188 counter, 1);
189 return;
190 }
191
192retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100193 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100194 counter);
195
196 spin_lock_irq(&ctx->lock);
197 /*
198 * If the context is active we need to retry the smp call.
199 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100200 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100201 spin_unlock_irq(&ctx->lock);
202 goto retry;
203 }
204
205 /*
206 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100207 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100208 * succeed.
209 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100210 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100211 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100212 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100213 counter->task = NULL;
214 }
215 spin_unlock_irq(&ctx->lock);
216}
217
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100218static int
219counter_sched_in(struct perf_counter *counter,
220 struct perf_cpu_context *cpuctx,
221 struct perf_counter_context *ctx,
222 int cpu)
223{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100224 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100225 return 0;
226
227 counter->state = PERF_COUNTER_STATE_ACTIVE;
228 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
229 /*
230 * The new state must be visible before we turn it on in the hardware:
231 */
232 smp_wmb();
233
234 if (counter->hw_ops->enable(counter)) {
235 counter->state = PERF_COUNTER_STATE_INACTIVE;
236 counter->oncpu = -1;
237 return -EAGAIN;
238 }
239
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100240 if (!is_software_counter(counter))
241 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100242 ctx->nr_active++;
243
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100244 if (counter->hw_event.exclusive)
245 cpuctx->exclusive = 1;
246
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100247 return 0;
248}
249
Thomas Gleixner0793a612008-12-04 20:12:29 +0100250/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100251 * Return 1 for a group consisting entirely of software counters,
252 * 0 if the group contains any hardware counters.
253 */
254static int is_software_only_group(struct perf_counter *leader)
255{
256 struct perf_counter *counter;
257
258 if (!is_software_counter(leader))
259 return 0;
260 list_for_each_entry(counter, &leader->sibling_list, list_entry)
261 if (!is_software_counter(counter))
262 return 0;
263 return 1;
264}
265
266/*
267 * Work out whether we can put this counter group on the CPU now.
268 */
269static int group_can_go_on(struct perf_counter *counter,
270 struct perf_cpu_context *cpuctx,
271 int can_add_hw)
272{
273 /*
274 * Groups consisting entirely of software counters can always go on.
275 */
276 if (is_software_only_group(counter))
277 return 1;
278 /*
279 * If an exclusive group is already on, no other hardware
280 * counters can go on.
281 */
282 if (cpuctx->exclusive)
283 return 0;
284 /*
285 * If this group is exclusive and there are already
286 * counters on the CPU, it can't go on.
287 */
288 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
289 return 0;
290 /*
291 * Otherwise, try to add it if all previous groups were able
292 * to go on.
293 */
294 return can_add_hw;
295}
296
297/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100298 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100299 */
300static void __perf_install_in_context(void *info)
301{
302 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
303 struct perf_counter *counter = info;
304 struct perf_counter_context *ctx = counter->ctx;
305 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100306 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100307 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100308 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100309
310 /*
311 * If this is a task context, we need to check whether it is
312 * the current task context of this cpu. If not it has been
313 * scheduled out before the smp call arrived.
314 */
315 if (ctx->task && cpuctx->task_ctx != ctx)
316 return;
317
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100318 curr_rq_lock_irq_save(&flags);
319 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100320
321 /*
322 * Protect the list operation against NMI by disabling the
323 * counters on a global level. NOP for non NMI based counters.
324 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100325 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100326
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100327 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100328 ctx->nr_counters++;
329
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100330 /*
331 * An exclusive counter can't go on if there are already active
332 * hardware counters, and no hardware counter can go on if there
333 * is already an exclusive counter on.
334 */
335 if (counter->state == PERF_COUNTER_STATE_INACTIVE &&
336 !group_can_go_on(counter, cpuctx, 1))
337 err = -EEXIST;
338 else
339 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100340
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100341 if (err && counter->hw_event.pinned)
342 counter->state = PERF_COUNTER_STATE_ERROR;
343
344 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100345 cpuctx->max_pertask--;
346
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100347 hw_perf_restore(perf_flags);
348
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100349 spin_unlock(&ctx->lock);
350 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100351}
352
353/*
354 * Attach a performance counter to a context
355 *
356 * First we add the counter to the list with the hardware enable bit
357 * in counter->hw_config cleared.
358 *
359 * If the counter is attached to a task which is on a CPU we use a smp
360 * call to enable it in the task context. The task might have been
361 * scheduled away, but we check this in the smp call again.
362 */
363static void
364perf_install_in_context(struct perf_counter_context *ctx,
365 struct perf_counter *counter,
366 int cpu)
367{
368 struct task_struct *task = ctx->task;
369
370 counter->ctx = ctx;
371 if (!task) {
372 /*
373 * Per cpu counters are installed via an smp call and
374 * the install is always sucessful.
375 */
376 smp_call_function_single(cpu, __perf_install_in_context,
377 counter, 1);
378 return;
379 }
380
381 counter->task = task;
382retry:
383 task_oncpu_function_call(task, __perf_install_in_context,
384 counter);
385
386 spin_lock_irq(&ctx->lock);
387 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100388 * we need to retry the smp call.
389 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100390 if (ctx->nr_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100391 spin_unlock_irq(&ctx->lock);
392 goto retry;
393 }
394
395 /*
396 * The lock prevents that this context is scheduled in so we
397 * can add the counter safely, if it the call above did not
398 * succeed.
399 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100400 if (list_empty(&counter->list_entry)) {
401 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100402 ctx->nr_counters++;
403 }
404 spin_unlock_irq(&ctx->lock);
405}
406
Ingo Molnar04289bb2008-12-11 08:38:42 +0100407static void
Ingo Molnar04289bb2008-12-11 08:38:42 +0100408group_sched_out(struct perf_counter *group_counter,
409 struct perf_cpu_context *cpuctx,
410 struct perf_counter_context *ctx)
411{
412 struct perf_counter *counter;
413
Paul Mackerras3cbed422009-01-09 16:43:42 +1100414 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
415 return;
416
Ingo Molnar04289bb2008-12-11 08:38:42 +0100417 counter_sched_out(group_counter, cpuctx, ctx);
418
419 /*
420 * Schedule out siblings (if any):
421 */
422 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
423 counter_sched_out(counter, cpuctx, ctx);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100424
425 if (group_counter->hw_event.exclusive)
426 cpuctx->exclusive = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100427}
428
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100429void __perf_counter_sched_out(struct perf_counter_context *ctx,
430 struct perf_cpu_context *cpuctx)
431{
432 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100433 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100434
435 if (likely(!ctx->nr_counters))
436 return;
437
438 spin_lock(&ctx->lock);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100439 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100440 if (ctx->nr_active) {
441 list_for_each_entry(counter, &ctx->counter_list, list_entry)
442 group_sched_out(counter, cpuctx, ctx);
443 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100444 hw_perf_restore(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100445 spin_unlock(&ctx->lock);
446}
447
Thomas Gleixner0793a612008-12-04 20:12:29 +0100448/*
449 * Called from scheduler to remove the counters of the current task,
450 * with interrupts disabled.
451 *
452 * We stop each counter and update the counter value in counter->count.
453 *
Ingo Molnar76715812008-12-17 14:20:28 +0100454 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100455 * sets the disabled bit in the control field of counter _before_
456 * accessing the counter control register. If a NMI hits, then it will
457 * not restart the counter.
458 */
459void perf_counter_task_sched_out(struct task_struct *task, int cpu)
460{
461 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
462 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100463
464 if (likely(!cpuctx->task_ctx))
465 return;
466
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100467 __perf_counter_sched_out(ctx, cpuctx);
468
Thomas Gleixner0793a612008-12-04 20:12:29 +0100469 cpuctx->task_ctx = NULL;
470}
471
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100472static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100473{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100474 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100475}
476
Ingo Molnar79958882008-12-17 08:54:56 +0100477static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100478group_sched_in(struct perf_counter *group_counter,
479 struct perf_cpu_context *cpuctx,
480 struct perf_counter_context *ctx,
481 int cpu)
482{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100483 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100484 int ret;
485
486 if (group_counter->state == PERF_COUNTER_STATE_OFF)
487 return 0;
488
489 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
490 if (ret)
491 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100492
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100493 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
494 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100495
496 /*
497 * Schedule in siblings as one group (if any):
498 */
Ingo Molnar79958882008-12-17 08:54:56 +0100499 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100500 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
501 partial_group = counter;
502 goto group_error;
503 }
Ingo Molnar79958882008-12-17 08:54:56 +0100504 }
505
Paul Mackerras3cbed422009-01-09 16:43:42 +1100506 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100507
508group_error:
509 /*
510 * Groups can be scheduled in as one unit only, so undo any
511 * partial group before returning:
512 */
513 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
514 if (counter == partial_group)
515 break;
516 counter_sched_out(counter, cpuctx, ctx);
517 }
518 counter_sched_out(group_counter, cpuctx, ctx);
519
520 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100521}
522
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100523static void
524__perf_counter_sched_in(struct perf_counter_context *ctx,
525 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100526{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100527 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100528 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100529 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100530
531 if (likely(!ctx->nr_counters))
532 return;
533
534 spin_lock(&ctx->lock);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100535 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100536
537 /*
538 * First go through the list and put on any pinned groups
539 * in order to give them the best chance of going on.
540 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100541 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100542 if (counter->state <= PERF_COUNTER_STATE_OFF ||
543 !counter->hw_event.pinned)
544 continue;
545 if (counter->cpu != -1 && counter->cpu != cpu)
546 continue;
547
548 if (group_can_go_on(counter, cpuctx, 1))
549 group_sched_in(counter, cpuctx, ctx, cpu);
550
551 /*
552 * If this pinned group hasn't been scheduled,
553 * put it in error state.
554 */
555 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
556 counter->state = PERF_COUNTER_STATE_ERROR;
557 }
558
559 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
560 /*
561 * Ignore counters in OFF or ERROR state, and
562 * ignore pinned counters since we did them already.
563 */
564 if (counter->state <= PERF_COUNTER_STATE_OFF ||
565 counter->hw_event.pinned)
566 continue;
567
Ingo Molnar04289bb2008-12-11 08:38:42 +0100568 /*
569 * Listen to the 'cpu' scheduling filter constraint
570 * of counters:
571 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100572 if (counter->cpu != -1 && counter->cpu != cpu)
573 continue;
574
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100575 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100576 if (group_sched_in(counter, cpuctx, ctx, cpu))
577 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100578 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100579 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100580 hw_perf_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100581 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100582}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100583
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100584/*
585 * Called from scheduler to add the counters of the current task
586 * with interrupts disabled.
587 *
588 * We restore the counter value and then enable it.
589 *
590 * This does not protect us against NMI, but enable()
591 * sets the enabled bit in the control field of counter _before_
592 * accessing the counter control register. If a NMI hits, then it will
593 * keep the counter running.
594 */
595void perf_counter_task_sched_in(struct task_struct *task, int cpu)
596{
597 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
598 struct perf_counter_context *ctx = &task->perf_counter_ctx;
599
600 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100601 cpuctx->task_ctx = ctx;
602}
603
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100604static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
605{
606 struct perf_counter_context *ctx = &cpuctx->ctx;
607
608 __perf_counter_sched_in(ctx, cpuctx, cpu);
609}
610
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100611int perf_counter_task_disable(void)
612{
613 struct task_struct *curr = current;
614 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
615 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100616 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100617 u64 perf_flags;
618 int cpu;
619
620 if (likely(!ctx->nr_counters))
621 return 0;
622
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100623 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100624 cpu = smp_processor_id();
625
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100626 /* force the update of the task clock: */
627 __task_delta_exec(curr, 1);
628
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100629 perf_counter_task_sched_out(curr, cpu);
630
631 spin_lock(&ctx->lock);
632
633 /*
634 * Disable all the counters:
635 */
636 perf_flags = hw_perf_save_disable();
637
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100638 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
639 if (counter->state != PERF_COUNTER_STATE_ERROR)
640 counter->state = PERF_COUNTER_STATE_OFF;
641 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100642
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100643 hw_perf_restore(perf_flags);
644
645 spin_unlock(&ctx->lock);
646
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100647 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100648
649 return 0;
650}
651
652int perf_counter_task_enable(void)
653{
654 struct task_struct *curr = current;
655 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
656 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100657 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100658 u64 perf_flags;
659 int cpu;
660
661 if (likely(!ctx->nr_counters))
662 return 0;
663
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100664 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100665 cpu = smp_processor_id();
666
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100667 /* force the update of the task clock: */
668 __task_delta_exec(curr, 1);
669
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100670 perf_counter_task_sched_out(curr, cpu);
671
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100672 spin_lock(&ctx->lock);
673
674 /*
675 * Disable all the counters:
676 */
677 perf_flags = hw_perf_save_disable();
678
679 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100680 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100681 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +0100682 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100683 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100684 }
685 hw_perf_restore(perf_flags);
686
687 spin_unlock(&ctx->lock);
688
689 perf_counter_task_sched_in(curr, cpu);
690
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100691 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100692
693 return 0;
694}
695
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100696/*
697 * Round-robin a context's counters:
698 */
699static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100700{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100701 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100702 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100703
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100704 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100705 return;
706
Thomas Gleixner0793a612008-12-04 20:12:29 +0100707 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100708 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100709 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +0100710 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100711 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100712 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
713 list_del(&counter->list_entry);
714 list_add_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100715 break;
716 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100717 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100718
719 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100720}
Thomas Gleixner0793a612008-12-04 20:12:29 +0100721
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100722void perf_counter_task_tick(struct task_struct *curr, int cpu)
723{
724 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
725 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
726 const int rotate_percpu = 0;
727
728 if (rotate_percpu)
729 perf_counter_cpu_sched_out(cpuctx);
730 perf_counter_task_sched_out(curr, cpu);
731
732 if (rotate_percpu)
733 rotate_ctx(&cpuctx->ctx);
734 rotate_ctx(ctx);
735
736 if (rotate_percpu)
737 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100738 perf_counter_task_sched_in(curr, cpu);
739}
740
741/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100742 * Cross CPU call to read the hardware counter
743 */
Ingo Molnar76715812008-12-17 14:20:28 +0100744static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100745{
Ingo Molnar621a01e2008-12-11 12:46:46 +0100746 struct perf_counter *counter = info;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100747 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +0100748
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100749 curr_rq_lock_irq_save(&flags);
Ingo Molnar76715812008-12-17 14:20:28 +0100750 counter->hw_ops->read(counter);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100751 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100752}
753
Ingo Molnar04289bb2008-12-11 08:38:42 +0100754static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100755{
756 /*
757 * If counter is enabled and currently active on a CPU, update the
758 * value in the counter structure:
759 */
Ingo Molnar6a930702008-12-11 15:17:03 +0100760 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100761 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +0100762 __read, counter, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100763 }
764
Ingo Molnaree060942008-12-13 09:00:03 +0100765 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100766}
767
768/*
769 * Cross CPU call to switch performance data pointers
770 */
771static void __perf_switch_irq_data(void *info)
772{
773 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
774 struct perf_counter *counter = info;
775 struct perf_counter_context *ctx = counter->ctx;
776 struct perf_data *oldirqdata = counter->irqdata;
777
778 /*
779 * If this is a task context, we need to check whether it is
780 * the current task context of this cpu. If not it has been
781 * scheduled out before the smp call arrived.
782 */
783 if (ctx->task) {
784 if (cpuctx->task_ctx != ctx)
785 return;
786 spin_lock(&ctx->lock);
787 }
788
789 /* Change the pointer NMI safe */
790 atomic_long_set((atomic_long_t *)&counter->irqdata,
791 (unsigned long) counter->usrdata);
792 counter->usrdata = oldirqdata;
793
794 if (ctx->task)
795 spin_unlock(&ctx->lock);
796}
797
798static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
799{
800 struct perf_counter_context *ctx = counter->ctx;
801 struct perf_data *oldirqdata = counter->irqdata;
802 struct task_struct *task = ctx->task;
803
804 if (!task) {
805 smp_call_function_single(counter->cpu,
806 __perf_switch_irq_data,
807 counter, 1);
808 return counter->usrdata;
809 }
810
811retry:
812 spin_lock_irq(&ctx->lock);
Ingo Molnar6a930702008-12-11 15:17:03 +0100813 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100814 counter->irqdata = counter->usrdata;
815 counter->usrdata = oldirqdata;
816 spin_unlock_irq(&ctx->lock);
817 return oldirqdata;
818 }
819 spin_unlock_irq(&ctx->lock);
820 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
821 /* Might have failed, because task was scheduled out */
822 if (counter->irqdata == oldirqdata)
823 goto retry;
824
825 return counter->usrdata;
826}
827
828static void put_context(struct perf_counter_context *ctx)
829{
830 if (ctx->task)
831 put_task_struct(ctx->task);
832}
833
834static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
835{
836 struct perf_cpu_context *cpuctx;
837 struct perf_counter_context *ctx;
838 struct task_struct *task;
839
840 /*
841 * If cpu is not a wildcard then this is a percpu counter:
842 */
843 if (cpu != -1) {
844 /* Must be root to operate on a CPU counter: */
845 if (!capable(CAP_SYS_ADMIN))
846 return ERR_PTR(-EACCES);
847
848 if (cpu < 0 || cpu > num_possible_cpus())
849 return ERR_PTR(-EINVAL);
850
851 /*
852 * We could be clever and allow to attach a counter to an
853 * offline CPU and activate it when the CPU comes up, but
854 * that's for later.
855 */
856 if (!cpu_isset(cpu, cpu_online_map))
857 return ERR_PTR(-ENODEV);
858
859 cpuctx = &per_cpu(perf_cpu_context, cpu);
860 ctx = &cpuctx->ctx;
861
Thomas Gleixner0793a612008-12-04 20:12:29 +0100862 return ctx;
863 }
864
865 rcu_read_lock();
866 if (!pid)
867 task = current;
868 else
869 task = find_task_by_vpid(pid);
870 if (task)
871 get_task_struct(task);
872 rcu_read_unlock();
873
874 if (!task)
875 return ERR_PTR(-ESRCH);
876
877 ctx = &task->perf_counter_ctx;
878 ctx->task = task;
879
880 /* Reuse ptrace permission checks for now. */
881 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
882 put_context(ctx);
883 return ERR_PTR(-EACCES);
884 }
885
886 return ctx;
887}
888
889/*
890 * Called when the last reference to the file is gone.
891 */
892static int perf_release(struct inode *inode, struct file *file)
893{
894 struct perf_counter *counter = file->private_data;
895 struct perf_counter_context *ctx = counter->ctx;
896
897 file->private_data = NULL;
898
899 mutex_lock(&counter->mutex);
900
Ingo Molnar04289bb2008-12-11 08:38:42 +0100901 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100902 put_context(ctx);
903
904 mutex_unlock(&counter->mutex);
905
906 kfree(counter);
907
908 return 0;
909}
910
911/*
912 * Read the performance counter - simple non blocking version for now
913 */
914static ssize_t
915perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
916{
917 u64 cntval;
918
919 if (count != sizeof(cntval))
920 return -EINVAL;
921
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100922 /*
923 * Return end-of-file for a read on a counter that is in
924 * error state (i.e. because it was pinned but it couldn't be
925 * scheduled on to the CPU at some point).
926 */
927 if (counter->state == PERF_COUNTER_STATE_ERROR)
928 return 0;
929
Thomas Gleixner0793a612008-12-04 20:12:29 +0100930 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100931 cntval = perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100932 mutex_unlock(&counter->mutex);
933
934 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
935}
936
937static ssize_t
938perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
939{
940 if (!usrdata->len)
941 return 0;
942
943 count = min(count, (size_t)usrdata->len);
944 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
945 return -EFAULT;
946
947 /* Adjust the counters */
948 usrdata->len -= count;
949 if (!usrdata->len)
950 usrdata->rd_idx = 0;
951 else
952 usrdata->rd_idx += count;
953
954 return count;
955}
956
957static ssize_t
958perf_read_irq_data(struct perf_counter *counter,
959 char __user *buf,
960 size_t count,
961 int nonblocking)
962{
963 struct perf_data *irqdata, *usrdata;
964 DECLARE_WAITQUEUE(wait, current);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100965 ssize_t res, res2;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100966
967 irqdata = counter->irqdata;
968 usrdata = counter->usrdata;
969
970 if (usrdata->len + irqdata->len >= count)
971 goto read_pending;
972
973 if (nonblocking)
974 return -EAGAIN;
975
976 spin_lock_irq(&counter->waitq.lock);
977 __add_wait_queue(&counter->waitq, &wait);
978 for (;;) {
979 set_current_state(TASK_INTERRUPTIBLE);
980 if (usrdata->len + irqdata->len >= count)
981 break;
982
983 if (signal_pending(current))
984 break;
985
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100986 if (counter->state == PERF_COUNTER_STATE_ERROR)
987 break;
988
Thomas Gleixner0793a612008-12-04 20:12:29 +0100989 spin_unlock_irq(&counter->waitq.lock);
990 schedule();
991 spin_lock_irq(&counter->waitq.lock);
992 }
993 __remove_wait_queue(&counter->waitq, &wait);
994 __set_current_state(TASK_RUNNING);
995 spin_unlock_irq(&counter->waitq.lock);
996
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100997 if (usrdata->len + irqdata->len < count &&
998 counter->state != PERF_COUNTER_STATE_ERROR)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100999 return -ERESTARTSYS;
1000read_pending:
1001 mutex_lock(&counter->mutex);
1002
1003 /* Drain pending data first: */
1004 res = perf_copy_usrdata(usrdata, buf, count);
1005 if (res < 0 || res == count)
1006 goto out;
1007
1008 /* Switch irq buffer: */
1009 usrdata = perf_switch_irq_data(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001010 res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
1011 if (res2 < 0) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001012 if (!res)
1013 res = -EFAULT;
1014 } else {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001015 res += res2;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001016 }
1017out:
1018 mutex_unlock(&counter->mutex);
1019
1020 return res;
1021}
1022
1023static ssize_t
1024perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1025{
1026 struct perf_counter *counter = file->private_data;
1027
Ingo Molnar9f66a382008-12-10 12:33:23 +01001028 switch (counter->hw_event.record_type) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001029 case PERF_RECORD_SIMPLE:
1030 return perf_read_hw(counter, buf, count);
1031
1032 case PERF_RECORD_IRQ:
1033 case PERF_RECORD_GROUP:
1034 return perf_read_irq_data(counter, buf, count,
1035 file->f_flags & O_NONBLOCK);
1036 }
1037 return -EINVAL;
1038}
1039
1040static unsigned int perf_poll(struct file *file, poll_table *wait)
1041{
1042 struct perf_counter *counter = file->private_data;
1043 unsigned int events = 0;
1044 unsigned long flags;
1045
1046 poll_wait(file, &counter->waitq, wait);
1047
1048 spin_lock_irqsave(&counter->waitq.lock, flags);
1049 if (counter->usrdata->len || counter->irqdata->len)
1050 events |= POLLIN;
1051 spin_unlock_irqrestore(&counter->waitq.lock, flags);
1052
1053 return events;
1054}
1055
1056static const struct file_operations perf_fops = {
1057 .release = perf_release,
1058 .read = perf_read,
1059 .poll = perf_poll,
1060};
1061
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001062static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01001063{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001064 int cpu = raw_smp_processor_id();
1065
1066 atomic64_set(&counter->hw.prev_count, cpu_clock(cpu));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001067 return 0;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001068}
1069
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001070static void cpu_clock_perf_counter_update(struct perf_counter *counter)
1071{
1072 int cpu = raw_smp_processor_id();
1073 s64 prev;
1074 u64 now;
1075
1076 now = cpu_clock(cpu);
1077 prev = atomic64_read(&counter->hw.prev_count);
1078 atomic64_set(&counter->hw.prev_count, now);
1079 atomic64_add(now - prev, &counter->count);
1080}
1081
Ingo Molnar5c92d122008-12-11 13:21:10 +01001082static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
1083{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001084 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001085}
1086
1087static void cpu_clock_perf_counter_read(struct perf_counter *counter)
1088{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001089 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001090}
1091
1092static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01001093 .enable = cpu_clock_perf_counter_enable,
1094 .disable = cpu_clock_perf_counter_disable,
1095 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01001096};
1097
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001098/*
1099 * Called from within the scheduler:
1100 */
1101static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
Ingo Molnarbae43c92008-12-11 14:03:20 +01001102{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001103 struct task_struct *curr = counter->task;
1104 u64 delta;
1105
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001106 delta = __task_delta_exec(curr, update);
1107
1108 return curr->se.sum_exec_runtime + delta;
1109}
1110
1111static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
1112{
1113 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001114 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001115
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001116 prev = atomic64_read(&counter->hw.prev_count);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001117
1118 atomic64_set(&counter->hw.prev_count, now);
1119
1120 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001121
1122 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01001123}
1124
1125static void task_clock_perf_counter_read(struct perf_counter *counter)
1126{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001127 u64 now = task_clock_perf_counter_val(counter, 1);
1128
1129 task_clock_perf_counter_update(counter, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001130}
1131
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001132static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001133{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001134 u64 now = task_clock_perf_counter_val(counter, 0);
1135
1136 atomic64_set(&counter->hw.prev_count, now);
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001137
1138 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001139}
1140
1141static void task_clock_perf_counter_disable(struct perf_counter *counter)
1142{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001143 u64 now = task_clock_perf_counter_val(counter, 0);
1144
1145 task_clock_perf_counter_update(counter, now);
Ingo Molnarbae43c92008-12-11 14:03:20 +01001146}
1147
1148static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01001149 .enable = task_clock_perf_counter_enable,
1150 .disable = task_clock_perf_counter_disable,
1151 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01001152};
1153
Ingo Molnare06c61a2008-12-14 14:44:31 +01001154static u64 get_page_faults(void)
1155{
1156 struct task_struct *curr = current;
1157
1158 return curr->maj_flt + curr->min_flt;
1159}
1160
1161static void page_faults_perf_counter_update(struct perf_counter *counter)
1162{
1163 u64 prev, now;
1164 s64 delta;
1165
1166 prev = atomic64_read(&counter->hw.prev_count);
1167 now = get_page_faults();
1168
1169 atomic64_set(&counter->hw.prev_count, now);
1170
1171 delta = now - prev;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001172
1173 atomic64_add(delta, &counter->count);
1174}
1175
1176static void page_faults_perf_counter_read(struct perf_counter *counter)
1177{
1178 page_faults_perf_counter_update(counter);
1179}
1180
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001181static int page_faults_perf_counter_enable(struct perf_counter *counter)
Ingo Molnare06c61a2008-12-14 14:44:31 +01001182{
1183 /*
1184 * page-faults is a per-task value already,
1185 * so we dont have to clear it on switch-in.
1186 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001187
1188 return 0;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001189}
1190
1191static void page_faults_perf_counter_disable(struct perf_counter *counter)
1192{
1193 page_faults_perf_counter_update(counter);
1194}
1195
1196static const struct hw_perf_counter_ops perf_ops_page_faults = {
Ingo Molnar76715812008-12-17 14:20:28 +01001197 .enable = page_faults_perf_counter_enable,
1198 .disable = page_faults_perf_counter_disable,
1199 .read = page_faults_perf_counter_read,
Ingo Molnare06c61a2008-12-14 14:44:31 +01001200};
1201
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001202static u64 get_context_switches(void)
1203{
1204 struct task_struct *curr = current;
1205
1206 return curr->nvcsw + curr->nivcsw;
1207}
1208
1209static void context_switches_perf_counter_update(struct perf_counter *counter)
1210{
1211 u64 prev, now;
1212 s64 delta;
1213
1214 prev = atomic64_read(&counter->hw.prev_count);
1215 now = get_context_switches();
1216
1217 atomic64_set(&counter->hw.prev_count, now);
1218
1219 delta = now - prev;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001220
1221 atomic64_add(delta, &counter->count);
1222}
1223
1224static void context_switches_perf_counter_read(struct perf_counter *counter)
1225{
1226 context_switches_perf_counter_update(counter);
1227}
1228
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001229static int context_switches_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001230{
1231 /*
1232 * ->nvcsw + curr->nivcsw is a per-task value already,
1233 * so we dont have to clear it on switch-in.
1234 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001235
1236 return 0;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001237}
1238
1239static void context_switches_perf_counter_disable(struct perf_counter *counter)
1240{
1241 context_switches_perf_counter_update(counter);
1242}
1243
1244static const struct hw_perf_counter_ops perf_ops_context_switches = {
Ingo Molnar76715812008-12-17 14:20:28 +01001245 .enable = context_switches_perf_counter_enable,
1246 .disable = context_switches_perf_counter_disable,
1247 .read = context_switches_perf_counter_read,
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001248};
1249
Ingo Molnar6c594c22008-12-14 12:34:15 +01001250static inline u64 get_cpu_migrations(void)
1251{
1252 return current->se.nr_migrations;
1253}
1254
1255static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1256{
1257 u64 prev, now;
1258 s64 delta;
1259
1260 prev = atomic64_read(&counter->hw.prev_count);
1261 now = get_cpu_migrations();
1262
1263 atomic64_set(&counter->hw.prev_count, now);
1264
1265 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001266
1267 atomic64_add(delta, &counter->count);
1268}
1269
1270static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1271{
1272 cpu_migrations_perf_counter_update(counter);
1273}
1274
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001275static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01001276{
1277 /*
1278 * se.nr_migrations is a per-task value already,
1279 * so we dont have to clear it on switch-in.
1280 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001281
1282 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001283}
1284
1285static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1286{
1287 cpu_migrations_perf_counter_update(counter);
1288}
1289
1290static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01001291 .enable = cpu_migrations_perf_counter_enable,
1292 .disable = cpu_migrations_perf_counter_disable,
1293 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01001294};
1295
Ingo Molnar5c92d122008-12-11 13:21:10 +01001296static const struct hw_perf_counter_ops *
1297sw_perf_counter_init(struct perf_counter *counter)
1298{
1299 const struct hw_perf_counter_ops *hw_ops = NULL;
1300
1301 switch (counter->hw_event.type) {
1302 case PERF_COUNT_CPU_CLOCK:
1303 hw_ops = &perf_ops_cpu_clock;
1304 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001305 case PERF_COUNT_TASK_CLOCK:
1306 hw_ops = &perf_ops_task_clock;
1307 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001308 case PERF_COUNT_PAGE_FAULTS:
1309 hw_ops = &perf_ops_page_faults;
1310 break;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001311 case PERF_COUNT_CONTEXT_SWITCHES:
1312 hw_ops = &perf_ops_context_switches;
1313 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001314 case PERF_COUNT_CPU_MIGRATIONS:
1315 hw_ops = &perf_ops_cpu_migrations;
1316 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001317 default:
1318 break;
1319 }
1320 return hw_ops;
1321}
1322
Thomas Gleixner0793a612008-12-04 20:12:29 +01001323/*
1324 * Allocate and initialize a counter structure
1325 */
1326static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01001327perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1328 int cpu,
Ingo Molnar9b51f662008-12-12 13:49:45 +01001329 struct perf_counter *group_leader,
1330 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001331{
Ingo Molnar5c92d122008-12-11 13:21:10 +01001332 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001333 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001334
Ingo Molnar9b51f662008-12-12 13:49:45 +01001335 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001336 if (!counter)
1337 return NULL;
1338
Ingo Molnar04289bb2008-12-11 08:38:42 +01001339 /*
1340 * Single counters are their own group leaders, with an
1341 * empty sibling list:
1342 */
1343 if (!group_leader)
1344 group_leader = counter;
1345
Thomas Gleixner0793a612008-12-04 20:12:29 +01001346 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001347 INIT_LIST_HEAD(&counter->list_entry);
1348 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001349 init_waitqueue_head(&counter->waitq);
1350
Ingo Molnar9f66a382008-12-10 12:33:23 +01001351 counter->irqdata = &counter->data[0];
1352 counter->usrdata = &counter->data[1];
1353 counter->cpu = cpu;
1354 counter->hw_event = *hw_event;
1355 counter->wakeup_pending = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001356 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001357 counter->hw_ops = NULL;
1358
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001359 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01001360 if (hw_event->disabled)
1361 counter->state = PERF_COUNTER_STATE_OFF;
1362
Ingo Molnar5c92d122008-12-11 13:21:10 +01001363 hw_ops = NULL;
1364 if (!hw_event->raw && hw_event->type < 0)
1365 hw_ops = sw_perf_counter_init(counter);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001366 if (!hw_ops)
Ingo Molnar5c92d122008-12-11 13:21:10 +01001367 hw_ops = hw_perf_counter_init(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001368
Ingo Molnar621a01e2008-12-11 12:46:46 +01001369 if (!hw_ops) {
1370 kfree(counter);
1371 return NULL;
1372 }
1373 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001374
1375 return counter;
1376}
1377
1378/**
Ingo Molnar9f66a382008-12-10 12:33:23 +01001379 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1380 *
1381 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01001382 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01001383 * @cpu: target cpu
1384 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01001385 */
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001386asmlinkage int
1387sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
1388 pid_t pid, int cpu, int group_fd)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001389{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001390 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01001391 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001392 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001393 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001394 struct file *group_file = NULL;
1395 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001396 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001397 int ret;
1398
Ingo Molnar9f66a382008-12-10 12:33:23 +01001399 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01001400 return -EFAULT;
1401
Ingo Molnar04289bb2008-12-11 08:38:42 +01001402 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001403 * Get the target context (task or percpu):
1404 */
1405 ctx = find_get_context(pid, cpu);
1406 if (IS_ERR(ctx))
1407 return PTR_ERR(ctx);
1408
1409 /*
1410 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001411 */
1412 group_leader = NULL;
1413 if (group_fd != -1) {
1414 ret = -EINVAL;
1415 group_file = fget_light(group_fd, &fput_needed);
1416 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01001417 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001418 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01001419 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001420
1421 group_leader = group_file->private_data;
1422 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001423 * Do not allow a recursive hierarchy (this new sibling
1424 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001425 */
Ingo Molnarccff2862008-12-11 11:26:29 +01001426 if (group_leader->group_leader != group_leader)
1427 goto err_put_context;
1428 /*
1429 * Do not allow to attach to a group in a different
1430 * task or CPU context:
1431 */
1432 if (group_leader->ctx != ctx)
1433 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001434 /*
1435 * Only a group leader can be exclusive or pinned
1436 */
1437 if (hw_event.exclusive || hw_event.pinned)
1438 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001439 }
1440
Ingo Molnar5c92d122008-12-11 13:21:10 +01001441 ret = -EINVAL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001442 counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001443 if (!counter)
1444 goto err_put_context;
1445
Thomas Gleixner0793a612008-12-04 20:12:29 +01001446 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1447 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01001448 goto err_free_put_context;
1449
1450 counter_file = fget_light(ret, &fput_needed2);
1451 if (!counter_file)
1452 goto err_free_put_context;
1453
1454 counter->filp = counter_file;
1455 perf_install_in_context(ctx, counter, cpu);
1456
1457 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001458
Ingo Molnar04289bb2008-12-11 08:38:42 +01001459out_fput:
1460 fput_light(group_file, fput_needed);
1461
Thomas Gleixner0793a612008-12-04 20:12:29 +01001462 return ret;
1463
Ingo Molnar9b51f662008-12-12 13:49:45 +01001464err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001465 kfree(counter);
1466
1467err_put_context:
1468 put_context(ctx);
1469
Ingo Molnar04289bb2008-12-11 08:38:42 +01001470 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001471}
1472
Ingo Molnar9b51f662008-12-12 13:49:45 +01001473/*
1474 * Initialize the perf_counter context in a task_struct:
1475 */
1476static void
1477__perf_counter_init_context(struct perf_counter_context *ctx,
1478 struct task_struct *task)
1479{
1480 memset(ctx, 0, sizeof(*ctx));
1481 spin_lock_init(&ctx->lock);
1482 INIT_LIST_HEAD(&ctx->counter_list);
1483 ctx->task = task;
1484}
1485
1486/*
1487 * inherit a counter from parent task to child task:
1488 */
1489static int
1490inherit_counter(struct perf_counter *parent_counter,
1491 struct task_struct *parent,
1492 struct perf_counter_context *parent_ctx,
1493 struct task_struct *child,
1494 struct perf_counter_context *child_ctx)
1495{
1496 struct perf_counter *child_counter;
1497
1498 child_counter = perf_counter_alloc(&parent_counter->hw_event,
1499 parent_counter->cpu, NULL,
1500 GFP_ATOMIC);
1501 if (!child_counter)
1502 return -ENOMEM;
1503
1504 /*
1505 * Link it up in the child's context:
1506 */
1507 child_counter->ctx = child_ctx;
1508 child_counter->task = child;
1509 list_add_counter(child_counter, child_ctx);
1510 child_ctx->nr_counters++;
1511
1512 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001513 /*
1514 * inherit into child's child as well:
1515 */
1516 child_counter->hw_event.inherit = 1;
1517
1518 /*
1519 * Get a reference to the parent filp - we will fput it
1520 * when the child counter exits. This is safe to do because
1521 * we are in the parent and we know that the filp still
1522 * exists and has a nonzero count:
1523 */
1524 atomic_long_inc(&parent_counter->filp->f_count);
1525
1526 return 0;
1527}
1528
1529static void
1530__perf_counter_exit_task(struct task_struct *child,
1531 struct perf_counter *child_counter,
1532 struct perf_counter_context *child_ctx)
1533{
1534 struct perf_counter *parent_counter;
1535 u64 parent_val, child_val;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001536
1537 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001538 * If we do not self-reap then we have to wait for the
1539 * child task to unschedule (it will happen for sure),
1540 * so that its counter is at its final count. (This
1541 * condition triggers rarely - child tasks usually get
1542 * off their CPU before the parent has a chance to
1543 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01001544 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001545 if (child != current) {
1546 wait_task_inactive(child, 0);
1547 list_del_init(&child_counter->list_entry);
1548 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001549 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001550 unsigned long flags;
1551 u64 perf_flags;
1552
1553 /*
1554 * Disable and unlink this counter.
1555 *
1556 * Be careful about zapping the list - IRQ/NMI context
1557 * could still be processing it:
1558 */
1559 curr_rq_lock_irq_save(&flags);
1560 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001561
1562 cpuctx = &__get_cpu_var(perf_cpu_context);
1563
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001564 counter_sched_out(child_counter, cpuctx, child_ctx);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001565
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001566 list_del_init(&child_counter->list_entry);
1567
1568 child_ctx->nr_counters--;
1569
1570 hw_perf_restore(perf_flags);
1571 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001572 }
1573
Ingo Molnar9b51f662008-12-12 13:49:45 +01001574 parent_counter = child_counter->parent;
1575 /*
1576 * It can happen that parent exits first, and has counters
1577 * that are still around due to the child reference. These
1578 * counters need to be zapped - but otherwise linger.
1579 */
1580 if (!parent_counter)
1581 return;
1582
1583 parent_val = atomic64_read(&parent_counter->count);
1584 child_val = atomic64_read(&child_counter->count);
1585
1586 /*
1587 * Add back the child's count to the parent's count:
1588 */
1589 atomic64_add(child_val, &parent_counter->count);
1590
1591 fput(parent_counter->filp);
1592
1593 kfree(child_counter);
1594}
1595
1596/*
1597 * When a child task exist, feed back counter values to parent counters.
1598 *
1599 * Note: we are running in child context, but the PID is not hashed
1600 * anymore so new counters will not be added.
1601 */
1602void perf_counter_exit_task(struct task_struct *child)
1603{
1604 struct perf_counter *child_counter, *tmp;
1605 struct perf_counter_context *child_ctx;
1606
1607 child_ctx = &child->perf_counter_ctx;
1608
1609 if (likely(!child_ctx->nr_counters))
1610 return;
1611
1612 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
1613 list_entry)
1614 __perf_counter_exit_task(child, child_counter, child_ctx);
1615}
1616
1617/*
1618 * Initialize the perf_counter context in task_struct
1619 */
1620void perf_counter_init_task(struct task_struct *child)
1621{
1622 struct perf_counter_context *child_ctx, *parent_ctx;
1623 struct perf_counter *counter, *parent_counter;
1624 struct task_struct *parent = current;
1625 unsigned long flags;
1626
1627 child_ctx = &child->perf_counter_ctx;
1628 parent_ctx = &parent->perf_counter_ctx;
1629
1630 __perf_counter_init_context(child_ctx, child);
1631
1632 /*
1633 * This is executed from the parent task context, so inherit
1634 * counters that have been marked for cloning:
1635 */
1636
1637 if (likely(!parent_ctx->nr_counters))
1638 return;
1639
1640 /*
1641 * Lock the parent list. No need to lock the child - not PID
1642 * hashed yet and not running, so nobody can access it.
1643 */
1644 spin_lock_irqsave(&parent_ctx->lock, flags);
1645
1646 /*
1647 * We dont have to disable NMIs - we are only looking at
1648 * the list, not manipulating it:
1649 */
1650 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
1651 if (!counter->hw_event.inherit || counter->group_leader != counter)
1652 continue;
1653
1654 /*
1655 * Instead of creating recursive hierarchies of counters,
1656 * we link inheritd counters back to the original parent,
1657 * which has a filp for sure, which we use as the reference
1658 * count:
1659 */
1660 parent_counter = counter;
1661 if (counter->parent)
1662 parent_counter = counter->parent;
1663
1664 if (inherit_counter(parent_counter, parent,
1665 parent_ctx, child, child_ctx))
1666 break;
1667 }
1668
1669 spin_unlock_irqrestore(&parent_ctx->lock, flags);
1670}
1671
Ingo Molnar04289bb2008-12-11 08:38:42 +01001672static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001673{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001674 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001675
Ingo Molnar04289bb2008-12-11 08:38:42 +01001676 cpuctx = &per_cpu(perf_cpu_context, cpu);
1677 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001678
1679 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001680 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001681 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001682
Paul Mackerras01d02872009-01-14 13:44:19 +11001683 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001684}
1685
1686#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01001687static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001688{
1689 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1690 struct perf_counter_context *ctx = &cpuctx->ctx;
1691 struct perf_counter *counter, *tmp;
1692
Ingo Molnar04289bb2008-12-11 08:38:42 +01001693 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
1694 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001695
1696}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001697static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001698{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001699 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001700}
1701#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01001702static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001703#endif
1704
1705static int __cpuinit
1706perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1707{
1708 unsigned int cpu = (long)hcpu;
1709
1710 switch (action) {
1711
1712 case CPU_UP_PREPARE:
1713 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001714 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001715 break;
1716
1717 case CPU_DOWN_PREPARE:
1718 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001719 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001720 break;
1721
1722 default:
1723 break;
1724 }
1725
1726 return NOTIFY_OK;
1727}
1728
1729static struct notifier_block __cpuinitdata perf_cpu_nb = {
1730 .notifier_call = perf_cpu_notify,
1731};
1732
1733static int __init perf_counter_init(void)
1734{
1735 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
1736 (void *)(long)smp_processor_id());
1737 register_cpu_notifier(&perf_cpu_nb);
1738
1739 return 0;
1740}
1741early_initcall(perf_counter_init);
1742
1743static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
1744{
1745 return sprintf(buf, "%d\n", perf_reserved_percpu);
1746}
1747
1748static ssize_t
1749perf_set_reserve_percpu(struct sysdev_class *class,
1750 const char *buf,
1751 size_t count)
1752{
1753 struct perf_cpu_context *cpuctx;
1754 unsigned long val;
1755 int err, cpu, mpt;
1756
1757 err = strict_strtoul(buf, 10, &val);
1758 if (err)
1759 return err;
1760 if (val > perf_max_counters)
1761 return -EINVAL;
1762
1763 mutex_lock(&perf_resource_mutex);
1764 perf_reserved_percpu = val;
1765 for_each_online_cpu(cpu) {
1766 cpuctx = &per_cpu(perf_cpu_context, cpu);
1767 spin_lock_irq(&cpuctx->ctx.lock);
1768 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
1769 perf_max_counters - perf_reserved_percpu);
1770 cpuctx->max_pertask = mpt;
1771 spin_unlock_irq(&cpuctx->ctx.lock);
1772 }
1773 mutex_unlock(&perf_resource_mutex);
1774
1775 return count;
1776}
1777
1778static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
1779{
1780 return sprintf(buf, "%d\n", perf_overcommit);
1781}
1782
1783static ssize_t
1784perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
1785{
1786 unsigned long val;
1787 int err;
1788
1789 err = strict_strtoul(buf, 10, &val);
1790 if (err)
1791 return err;
1792 if (val > 1)
1793 return -EINVAL;
1794
1795 mutex_lock(&perf_resource_mutex);
1796 perf_overcommit = val;
1797 mutex_unlock(&perf_resource_mutex);
1798
1799 return count;
1800}
1801
1802static SYSDEV_CLASS_ATTR(
1803 reserve_percpu,
1804 0644,
1805 perf_show_reserve_percpu,
1806 perf_set_reserve_percpu
1807 );
1808
1809static SYSDEV_CLASS_ATTR(
1810 overcommit,
1811 0644,
1812 perf_show_overcommit,
1813 perf_set_overcommit
1814 );
1815
1816static struct attribute *perfclass_attrs[] = {
1817 &attr_reserve_percpu.attr,
1818 &attr_overcommit.attr,
1819 NULL
1820};
1821
1822static struct attribute_group perfclass_attr_group = {
1823 .attrs = perfclass_attrs,
1824 .name = "perf_counters",
1825};
1826
1827static int __init perf_counter_sysfs_init(void)
1828{
1829 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
1830 &perfclass_attr_group);
1831}
1832device_initcall(perf_counter_sysfs_init);