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