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 | * | |
7 | * For licencing details see kernel-base/COPYING | |
8 | */ | |
9 | ||
10 | #include <linux/fs.h> | |
11 | #include <linux/cpu.h> | |
12 | #include <linux/smp.h> | |
04289bb9 | 13 | #include <linux/file.h> |
0793a61d TG |
14 | #include <linux/poll.h> |
15 | #include <linux/sysfs.h> | |
16 | #include <linux/ptrace.h> | |
17 | #include <linux/percpu.h> | |
18 | #include <linux/uaccess.h> | |
19 | #include <linux/syscalls.h> | |
20 | #include <linux/anon_inodes.h> | |
aa9c4c0f | 21 | #include <linux/kernel_stat.h> |
0793a61d TG |
22 | #include <linux/perf_counter.h> |
23 | ||
24 | /* | |
25 | * Each CPU has a list of per CPU counters: | |
26 | */ | |
27 | DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context); | |
28 | ||
088e2852 | 29 | int perf_max_counters __read_mostly = 1; |
0793a61d TG |
30 | static int perf_reserved_percpu __read_mostly; |
31 | static int perf_overcommit __read_mostly = 1; | |
32 | ||
33 | /* | |
34 | * Mutex for (sysadmin-configurable) counter reservations: | |
35 | */ | |
36 | static DEFINE_MUTEX(perf_resource_mutex); | |
37 | ||
38 | /* | |
39 | * Architecture provided APIs - weak aliases: | |
40 | */ | |
5c92d124 | 41 | extern __weak const struct hw_perf_counter_ops * |
621a01ea | 42 | hw_perf_counter_init(struct perf_counter *counter) |
0793a61d | 43 | { |
621a01ea | 44 | return ERR_PTR(-EINVAL); |
0793a61d TG |
45 | } |
46 | ||
01b2838c | 47 | u64 __weak hw_perf_save_disable(void) { return 0; } |
ee06094f | 48 | void __weak hw_perf_restore(u64 ctrl) { } |
5c92d124 | 49 | void __weak hw_perf_counter_setup(void) { } |
0793a61d | 50 | |
04289bb9 IM |
51 | static void |
52 | list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx) | |
53 | { | |
54 | struct perf_counter *group_leader = counter->group_leader; | |
55 | ||
56 | /* | |
57 | * Depending on whether it is a standalone or sibling counter, | |
58 | * add it straight to the context's counter list, or to the group | |
59 | * leader's sibling list: | |
60 | */ | |
61 | if (counter->group_leader == counter) | |
62 | list_add_tail(&counter->list_entry, &ctx->counter_list); | |
63 | else | |
64 | list_add_tail(&counter->list_entry, &group_leader->sibling_list); | |
65 | } | |
66 | ||
67 | static void | |
68 | list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx) | |
69 | { | |
70 | struct perf_counter *sibling, *tmp; | |
71 | ||
72 | list_del_init(&counter->list_entry); | |
73 | ||
04289bb9 IM |
74 | /* |
75 | * If this was a group counter with sibling counters then | |
76 | * upgrade the siblings to singleton counters by adding them | |
77 | * to the context list directly: | |
78 | */ | |
79 | list_for_each_entry_safe(sibling, tmp, | |
80 | &counter->sibling_list, list_entry) { | |
81 | ||
82 | list_del_init(&sibling->list_entry); | |
83 | list_add_tail(&sibling->list_entry, &ctx->counter_list); | |
04289bb9 IM |
84 | sibling->group_leader = sibling; |
85 | } | |
86 | } | |
87 | ||
0793a61d TG |
88 | /* |
89 | * Cross CPU call to remove a performance counter | |
90 | * | |
91 | * We disable the counter on the hardware level first. After that we | |
92 | * remove it from the context list. | |
93 | */ | |
04289bb9 | 94 | static void __perf_counter_remove_from_context(void *info) |
0793a61d TG |
95 | { |
96 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); | |
97 | struct perf_counter *counter = info; | |
98 | struct perf_counter_context *ctx = counter->ctx; | |
9b51f66d | 99 | unsigned long flags; |
5c92d124 | 100 | u64 perf_flags; |
0793a61d TG |
101 | |
102 | /* | |
103 | * If this is a task context, we need to check whether it is | |
104 | * the current task context of this cpu. If not it has been | |
105 | * scheduled out before the smp call arrived. | |
106 | */ | |
107 | if (ctx->task && cpuctx->task_ctx != ctx) | |
108 | return; | |
109 | ||
aa9c4c0f IM |
110 | curr_rq_lock_irq_save(&flags); |
111 | spin_lock(&ctx->lock); | |
0793a61d | 112 | |
6a930700 | 113 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
7671581f | 114 | counter->hw_ops->disable(counter); |
6a930700 | 115 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
0793a61d TG |
116 | ctx->nr_active--; |
117 | cpuctx->active_oncpu--; | |
118 | counter->task = NULL; | |
119 | } | |
120 | ctx->nr_counters--; | |
121 | ||
122 | /* | |
123 | * Protect the list operation against NMI by disabling the | |
124 | * counters on a global level. NOP for non NMI based counters. | |
125 | */ | |
01b2838c | 126 | perf_flags = hw_perf_save_disable(); |
04289bb9 | 127 | list_del_counter(counter, ctx); |
01b2838c | 128 | hw_perf_restore(perf_flags); |
0793a61d TG |
129 | |
130 | if (!ctx->task) { | |
131 | /* | |
132 | * Allow more per task counters with respect to the | |
133 | * reservation: | |
134 | */ | |
135 | cpuctx->max_pertask = | |
136 | min(perf_max_counters - ctx->nr_counters, | |
137 | perf_max_counters - perf_reserved_percpu); | |
138 | } | |
139 | ||
aa9c4c0f IM |
140 | spin_unlock(&ctx->lock); |
141 | curr_rq_unlock_irq_restore(&flags); | |
0793a61d TG |
142 | } |
143 | ||
144 | ||
145 | /* | |
146 | * Remove the counter from a task's (or a CPU's) list of counters. | |
147 | * | |
148 | * Must be called with counter->mutex held. | |
149 | * | |
150 | * CPU counters are removed with a smp call. For task counters we only | |
151 | * call when the task is on a CPU. | |
152 | */ | |
04289bb9 | 153 | static void perf_counter_remove_from_context(struct perf_counter *counter) |
0793a61d TG |
154 | { |
155 | struct perf_counter_context *ctx = counter->ctx; | |
156 | struct task_struct *task = ctx->task; | |
157 | ||
158 | if (!task) { | |
159 | /* | |
160 | * Per cpu counters are removed via an smp call and | |
161 | * the removal is always sucessful. | |
162 | */ | |
163 | smp_call_function_single(counter->cpu, | |
04289bb9 | 164 | __perf_counter_remove_from_context, |
0793a61d TG |
165 | counter, 1); |
166 | return; | |
167 | } | |
168 | ||
169 | retry: | |
04289bb9 | 170 | task_oncpu_function_call(task, __perf_counter_remove_from_context, |
0793a61d TG |
171 | counter); |
172 | ||
173 | spin_lock_irq(&ctx->lock); | |
174 | /* | |
175 | * If the context is active we need to retry the smp call. | |
176 | */ | |
04289bb9 | 177 | if (ctx->nr_active && !list_empty(&counter->list_entry)) { |
0793a61d TG |
178 | spin_unlock_irq(&ctx->lock); |
179 | goto retry; | |
180 | } | |
181 | ||
182 | /* | |
183 | * The lock prevents that this context is scheduled in so we | |
04289bb9 | 184 | * can remove the counter safely, if the call above did not |
0793a61d TG |
185 | * succeed. |
186 | */ | |
04289bb9 | 187 | if (!list_empty(&counter->list_entry)) { |
0793a61d | 188 | ctx->nr_counters--; |
04289bb9 | 189 | list_del_counter(counter, ctx); |
0793a61d TG |
190 | counter->task = NULL; |
191 | } | |
192 | spin_unlock_irq(&ctx->lock); | |
193 | } | |
194 | ||
195 | /* | |
196 | * Cross CPU call to install and enable a preformance counter | |
197 | */ | |
198 | static void __perf_install_in_context(void *info) | |
199 | { | |
200 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); | |
201 | struct perf_counter *counter = info; | |
202 | struct perf_counter_context *ctx = counter->ctx; | |
203 | int cpu = smp_processor_id(); | |
9b51f66d | 204 | unsigned long flags; |
5c92d124 | 205 | u64 perf_flags; |
0793a61d TG |
206 | |
207 | /* | |
208 | * If this is a task context, we need to check whether it is | |
209 | * the current task context of this cpu. If not it has been | |
210 | * scheduled out before the smp call arrived. | |
211 | */ | |
212 | if (ctx->task && cpuctx->task_ctx != ctx) | |
213 | return; | |
214 | ||
aa9c4c0f IM |
215 | curr_rq_lock_irq_save(&flags); |
216 | spin_lock(&ctx->lock); | |
0793a61d TG |
217 | |
218 | /* | |
219 | * Protect the list operation against NMI by disabling the | |
220 | * counters on a global level. NOP for non NMI based counters. | |
221 | */ | |
01b2838c | 222 | perf_flags = hw_perf_save_disable(); |
04289bb9 | 223 | list_add_counter(counter, ctx); |
01b2838c | 224 | hw_perf_restore(perf_flags); |
0793a61d TG |
225 | |
226 | ctx->nr_counters++; | |
227 | ||
228 | if (cpuctx->active_oncpu < perf_max_counters) { | |
6a930700 | 229 | counter->state = PERF_COUNTER_STATE_ACTIVE; |
0793a61d TG |
230 | counter->oncpu = cpu; |
231 | ctx->nr_active++; | |
232 | cpuctx->active_oncpu++; | |
7671581f | 233 | counter->hw_ops->enable(counter); |
0793a61d TG |
234 | } |
235 | ||
236 | if (!ctx->task && cpuctx->max_pertask) | |
237 | cpuctx->max_pertask--; | |
238 | ||
aa9c4c0f IM |
239 | spin_unlock(&ctx->lock); |
240 | curr_rq_unlock_irq_restore(&flags); | |
0793a61d TG |
241 | } |
242 | ||
243 | /* | |
244 | * Attach a performance counter to a context | |
245 | * | |
246 | * First we add the counter to the list with the hardware enable bit | |
247 | * in counter->hw_config cleared. | |
248 | * | |
249 | * If the counter is attached to a task which is on a CPU we use a smp | |
250 | * call to enable it in the task context. The task might have been | |
251 | * scheduled away, but we check this in the smp call again. | |
252 | */ | |
253 | static void | |
254 | perf_install_in_context(struct perf_counter_context *ctx, | |
255 | struct perf_counter *counter, | |
256 | int cpu) | |
257 | { | |
258 | struct task_struct *task = ctx->task; | |
259 | ||
260 | counter->ctx = ctx; | |
261 | if (!task) { | |
262 | /* | |
263 | * Per cpu counters are installed via an smp call and | |
264 | * the install is always sucessful. | |
265 | */ | |
266 | smp_call_function_single(cpu, __perf_install_in_context, | |
267 | counter, 1); | |
268 | return; | |
269 | } | |
270 | ||
271 | counter->task = task; | |
272 | retry: | |
273 | task_oncpu_function_call(task, __perf_install_in_context, | |
274 | counter); | |
275 | ||
276 | spin_lock_irq(&ctx->lock); | |
277 | /* | |
0793a61d TG |
278 | * we need to retry the smp call. |
279 | */ | |
04289bb9 | 280 | if (ctx->nr_active && list_empty(&counter->list_entry)) { |
0793a61d TG |
281 | spin_unlock_irq(&ctx->lock); |
282 | goto retry; | |
283 | } | |
284 | ||
285 | /* | |
286 | * The lock prevents that this context is scheduled in so we | |
287 | * can add the counter safely, if it the call above did not | |
288 | * succeed. | |
289 | */ | |
04289bb9 IM |
290 | if (list_empty(&counter->list_entry)) { |
291 | list_add_counter(counter, ctx); | |
0793a61d TG |
292 | ctx->nr_counters++; |
293 | } | |
294 | spin_unlock_irq(&ctx->lock); | |
295 | } | |
296 | ||
04289bb9 IM |
297 | static void |
298 | counter_sched_out(struct perf_counter *counter, | |
299 | struct perf_cpu_context *cpuctx, | |
300 | struct perf_counter_context *ctx) | |
301 | { | |
6a930700 | 302 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
04289bb9 IM |
303 | return; |
304 | ||
7671581f | 305 | counter->hw_ops->disable(counter); |
6a930700 IM |
306 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
307 | counter->oncpu = -1; | |
04289bb9 IM |
308 | |
309 | cpuctx->active_oncpu--; | |
310 | ctx->nr_active--; | |
311 | } | |
312 | ||
313 | static void | |
314 | group_sched_out(struct perf_counter *group_counter, | |
315 | struct perf_cpu_context *cpuctx, | |
316 | struct perf_counter_context *ctx) | |
317 | { | |
318 | struct perf_counter *counter; | |
319 | ||
320 | counter_sched_out(group_counter, cpuctx, ctx); | |
321 | ||
322 | /* | |
323 | * Schedule out siblings (if any): | |
324 | */ | |
325 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) | |
326 | counter_sched_out(counter, cpuctx, ctx); | |
327 | } | |
328 | ||
0793a61d TG |
329 | /* |
330 | * Called from scheduler to remove the counters of the current task, | |
331 | * with interrupts disabled. | |
332 | * | |
333 | * We stop each counter and update the counter value in counter->count. | |
334 | * | |
7671581f | 335 | * This does not protect us against NMI, but disable() |
0793a61d TG |
336 | * sets the disabled bit in the control field of counter _before_ |
337 | * accessing the counter control register. If a NMI hits, then it will | |
338 | * not restart the counter. | |
339 | */ | |
340 | void perf_counter_task_sched_out(struct task_struct *task, int cpu) | |
341 | { | |
342 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); | |
343 | struct perf_counter_context *ctx = &task->perf_counter_ctx; | |
344 | struct perf_counter *counter; | |
345 | ||
346 | if (likely(!cpuctx->task_ctx)) | |
347 | return; | |
348 | ||
349 | spin_lock(&ctx->lock); | |
04289bb9 IM |
350 | if (ctx->nr_active) { |
351 | list_for_each_entry(counter, &ctx->counter_list, list_entry) | |
352 | group_sched_out(counter, cpuctx, ctx); | |
0793a61d TG |
353 | } |
354 | spin_unlock(&ctx->lock); | |
355 | cpuctx->task_ctx = NULL; | |
356 | } | |
357 | ||
95cdd2e7 | 358 | static int |
04289bb9 IM |
359 | counter_sched_in(struct perf_counter *counter, |
360 | struct perf_cpu_context *cpuctx, | |
361 | struct perf_counter_context *ctx, | |
362 | int cpu) | |
363 | { | |
6a930700 | 364 | if (counter->state == PERF_COUNTER_STATE_OFF) |
95cdd2e7 IM |
365 | return 0; |
366 | ||
367 | if (counter->hw_ops->enable(counter)) | |
368 | return -EAGAIN; | |
1d1c7ddb | 369 | |
6a930700 | 370 | counter->state = PERF_COUNTER_STATE_ACTIVE; |
04289bb9 IM |
371 | counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */ |
372 | ||
373 | cpuctx->active_oncpu++; | |
374 | ctx->nr_active++; | |
95cdd2e7 IM |
375 | |
376 | return 0; | |
04289bb9 IM |
377 | } |
378 | ||
7995888f | 379 | static int |
04289bb9 IM |
380 | group_sched_in(struct perf_counter *group_counter, |
381 | struct perf_cpu_context *cpuctx, | |
382 | struct perf_counter_context *ctx, | |
383 | int cpu) | |
384 | { | |
95cdd2e7 IM |
385 | struct perf_counter *counter, *partial_group; |
386 | int ret = 0; | |
04289bb9 | 387 | |
95cdd2e7 IM |
388 | if (counter_sched_in(group_counter, cpuctx, ctx, cpu)) |
389 | return -EAGAIN; | |
04289bb9 IM |
390 | |
391 | /* | |
392 | * Schedule in siblings as one group (if any): | |
393 | */ | |
7995888f | 394 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { |
95cdd2e7 IM |
395 | if (counter_sched_in(counter, cpuctx, ctx, cpu)) { |
396 | partial_group = counter; | |
397 | goto group_error; | |
398 | } | |
399 | ret = -EAGAIN; | |
400 | } | |
401 | ||
402 | return ret; | |
403 | ||
404 | group_error: | |
405 | /* | |
406 | * Groups can be scheduled in as one unit only, so undo any | |
407 | * partial group before returning: | |
408 | */ | |
409 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { | |
410 | if (counter == partial_group) | |
411 | break; | |
412 | counter_sched_out(counter, cpuctx, ctx); | |
7995888f | 413 | } |
95cdd2e7 | 414 | counter_sched_out(group_counter, cpuctx, ctx); |
7995888f | 415 | |
95cdd2e7 | 416 | return -EAGAIN; |
04289bb9 IM |
417 | } |
418 | ||
0793a61d TG |
419 | /* |
420 | * Called from scheduler to add the counters of the current task | |
421 | * with interrupts disabled. | |
422 | * | |
423 | * We restore the counter value and then enable it. | |
424 | * | |
7671581f | 425 | * This does not protect us against NMI, but enable() |
0793a61d TG |
426 | * sets the enabled bit in the control field of counter _before_ |
427 | * accessing the counter control register. If a NMI hits, then it will | |
428 | * keep the counter running. | |
429 | */ | |
430 | void perf_counter_task_sched_in(struct task_struct *task, int cpu) | |
431 | { | |
432 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); | |
433 | struct perf_counter_context *ctx = &task->perf_counter_ctx; | |
434 | struct perf_counter *counter; | |
435 | ||
436 | if (likely(!ctx->nr_counters)) | |
437 | return; | |
438 | ||
439 | spin_lock(&ctx->lock); | |
04289bb9 | 440 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
04289bb9 IM |
441 | /* |
442 | * Listen to the 'cpu' scheduling filter constraint | |
443 | * of counters: | |
444 | */ | |
0793a61d TG |
445 | if (counter->cpu != -1 && counter->cpu != cpu) |
446 | continue; | |
447 | ||
7995888f IM |
448 | /* |
449 | * If we scheduled in a group atomically and | |
450 | * exclusively, break out: | |
451 | */ | |
452 | if (group_sched_in(counter, cpuctx, ctx, cpu)) | |
453 | break; | |
0793a61d TG |
454 | } |
455 | spin_unlock(&ctx->lock); | |
04289bb9 | 456 | |
0793a61d TG |
457 | cpuctx->task_ctx = ctx; |
458 | } | |
459 | ||
1d1c7ddb IM |
460 | int perf_counter_task_disable(void) |
461 | { | |
462 | struct task_struct *curr = current; | |
463 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; | |
464 | struct perf_counter *counter; | |
aa9c4c0f | 465 | unsigned long flags; |
1d1c7ddb IM |
466 | u64 perf_flags; |
467 | int cpu; | |
468 | ||
469 | if (likely(!ctx->nr_counters)) | |
470 | return 0; | |
471 | ||
aa9c4c0f | 472 | curr_rq_lock_irq_save(&flags); |
1d1c7ddb IM |
473 | cpu = smp_processor_id(); |
474 | ||
aa9c4c0f IM |
475 | /* force the update of the task clock: */ |
476 | __task_delta_exec(curr, 1); | |
477 | ||
1d1c7ddb IM |
478 | perf_counter_task_sched_out(curr, cpu); |
479 | ||
480 | spin_lock(&ctx->lock); | |
481 | ||
482 | /* | |
483 | * Disable all the counters: | |
484 | */ | |
485 | perf_flags = hw_perf_save_disable(); | |
486 | ||
9b51f66d | 487 | list_for_each_entry(counter, &ctx->counter_list, list_entry) |
6a930700 | 488 | counter->state = PERF_COUNTER_STATE_OFF; |
9b51f66d | 489 | |
1d1c7ddb IM |
490 | hw_perf_restore(perf_flags); |
491 | ||
492 | spin_unlock(&ctx->lock); | |
493 | ||
aa9c4c0f | 494 | curr_rq_unlock_irq_restore(&flags); |
1d1c7ddb IM |
495 | |
496 | return 0; | |
497 | } | |
498 | ||
499 | int perf_counter_task_enable(void) | |
500 | { | |
501 | struct task_struct *curr = current; | |
502 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; | |
503 | struct perf_counter *counter; | |
aa9c4c0f | 504 | unsigned long flags; |
1d1c7ddb IM |
505 | u64 perf_flags; |
506 | int cpu; | |
507 | ||
508 | if (likely(!ctx->nr_counters)) | |
509 | return 0; | |
510 | ||
aa9c4c0f | 511 | curr_rq_lock_irq_save(&flags); |
1d1c7ddb IM |
512 | cpu = smp_processor_id(); |
513 | ||
aa9c4c0f IM |
514 | /* force the update of the task clock: */ |
515 | __task_delta_exec(curr, 1); | |
516 | ||
1d1c7ddb IM |
517 | spin_lock(&ctx->lock); |
518 | ||
519 | /* | |
520 | * Disable all the counters: | |
521 | */ | |
522 | perf_flags = hw_perf_save_disable(); | |
523 | ||
524 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { | |
6a930700 | 525 | if (counter->state != PERF_COUNTER_STATE_OFF) |
1d1c7ddb | 526 | continue; |
6a930700 | 527 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
aa9c4c0f | 528 | counter->hw_event.disabled = 0; |
1d1c7ddb IM |
529 | } |
530 | hw_perf_restore(perf_flags); | |
531 | ||
532 | spin_unlock(&ctx->lock); | |
533 | ||
534 | perf_counter_task_sched_in(curr, cpu); | |
535 | ||
aa9c4c0f | 536 | curr_rq_unlock_irq_restore(&flags); |
1d1c7ddb IM |
537 | |
538 | return 0; | |
539 | } | |
540 | ||
0793a61d TG |
541 | void perf_counter_task_tick(struct task_struct *curr, int cpu) |
542 | { | |
543 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; | |
544 | struct perf_counter *counter; | |
5c92d124 | 545 | u64 perf_flags; |
0793a61d TG |
546 | |
547 | if (likely(!ctx->nr_counters)) | |
548 | return; | |
549 | ||
550 | perf_counter_task_sched_out(curr, cpu); | |
551 | ||
552 | spin_lock(&ctx->lock); | |
553 | ||
554 | /* | |
04289bb9 | 555 | * Rotate the first entry last (works just fine for group counters too): |
0793a61d | 556 | */ |
01b2838c | 557 | perf_flags = hw_perf_save_disable(); |
04289bb9 IM |
558 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
559 | list_del(&counter->list_entry); | |
560 | list_add_tail(&counter->list_entry, &ctx->counter_list); | |
0793a61d TG |
561 | break; |
562 | } | |
01b2838c | 563 | hw_perf_restore(perf_flags); |
0793a61d TG |
564 | |
565 | spin_unlock(&ctx->lock); | |
566 | ||
567 | perf_counter_task_sched_in(curr, cpu); | |
568 | } | |
569 | ||
0793a61d TG |
570 | /* |
571 | * Cross CPU call to read the hardware counter | |
572 | */ | |
7671581f | 573 | static void __read(void *info) |
0793a61d | 574 | { |
621a01ea | 575 | struct perf_counter *counter = info; |
aa9c4c0f | 576 | unsigned long flags; |
621a01ea | 577 | |
aa9c4c0f | 578 | curr_rq_lock_irq_save(&flags); |
7671581f | 579 | counter->hw_ops->read(counter); |
aa9c4c0f | 580 | curr_rq_unlock_irq_restore(&flags); |
0793a61d TG |
581 | } |
582 | ||
04289bb9 | 583 | static u64 perf_counter_read(struct perf_counter *counter) |
0793a61d TG |
584 | { |
585 | /* | |
586 | * If counter is enabled and currently active on a CPU, update the | |
587 | * value in the counter structure: | |
588 | */ | |
6a930700 | 589 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
0793a61d | 590 | smp_call_function_single(counter->oncpu, |
7671581f | 591 | __read, counter, 1); |
0793a61d TG |
592 | } |
593 | ||
ee06094f | 594 | return atomic64_read(&counter->count); |
0793a61d TG |
595 | } |
596 | ||
597 | /* | |
598 | * Cross CPU call to switch performance data pointers | |
599 | */ | |
600 | static void __perf_switch_irq_data(void *info) | |
601 | { | |
602 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); | |
603 | struct perf_counter *counter = info; | |
604 | struct perf_counter_context *ctx = counter->ctx; | |
605 | struct perf_data *oldirqdata = counter->irqdata; | |
606 | ||
607 | /* | |
608 | * If this is a task context, we need to check whether it is | |
609 | * the current task context of this cpu. If not it has been | |
610 | * scheduled out before the smp call arrived. | |
611 | */ | |
612 | if (ctx->task) { | |
613 | if (cpuctx->task_ctx != ctx) | |
614 | return; | |
615 | spin_lock(&ctx->lock); | |
616 | } | |
617 | ||
618 | /* Change the pointer NMI safe */ | |
619 | atomic_long_set((atomic_long_t *)&counter->irqdata, | |
620 | (unsigned long) counter->usrdata); | |
621 | counter->usrdata = oldirqdata; | |
622 | ||
623 | if (ctx->task) | |
624 | spin_unlock(&ctx->lock); | |
625 | } | |
626 | ||
627 | static struct perf_data *perf_switch_irq_data(struct perf_counter *counter) | |
628 | { | |
629 | struct perf_counter_context *ctx = counter->ctx; | |
630 | struct perf_data *oldirqdata = counter->irqdata; | |
631 | struct task_struct *task = ctx->task; | |
632 | ||
633 | if (!task) { | |
634 | smp_call_function_single(counter->cpu, | |
635 | __perf_switch_irq_data, | |
636 | counter, 1); | |
637 | return counter->usrdata; | |
638 | } | |
639 | ||
640 | retry: | |
641 | spin_lock_irq(&ctx->lock); | |
6a930700 | 642 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) { |
0793a61d TG |
643 | counter->irqdata = counter->usrdata; |
644 | counter->usrdata = oldirqdata; | |
645 | spin_unlock_irq(&ctx->lock); | |
646 | return oldirqdata; | |
647 | } | |
648 | spin_unlock_irq(&ctx->lock); | |
649 | task_oncpu_function_call(task, __perf_switch_irq_data, counter); | |
650 | /* Might have failed, because task was scheduled out */ | |
651 | if (counter->irqdata == oldirqdata) | |
652 | goto retry; | |
653 | ||
654 | return counter->usrdata; | |
655 | } | |
656 | ||
657 | static void put_context(struct perf_counter_context *ctx) | |
658 | { | |
659 | if (ctx->task) | |
660 | put_task_struct(ctx->task); | |
661 | } | |
662 | ||
663 | static struct perf_counter_context *find_get_context(pid_t pid, int cpu) | |
664 | { | |
665 | struct perf_cpu_context *cpuctx; | |
666 | struct perf_counter_context *ctx; | |
667 | struct task_struct *task; | |
668 | ||
669 | /* | |
670 | * If cpu is not a wildcard then this is a percpu counter: | |
671 | */ | |
672 | if (cpu != -1) { | |
673 | /* Must be root to operate on a CPU counter: */ | |
674 | if (!capable(CAP_SYS_ADMIN)) | |
675 | return ERR_PTR(-EACCES); | |
676 | ||
677 | if (cpu < 0 || cpu > num_possible_cpus()) | |
678 | return ERR_PTR(-EINVAL); | |
679 | ||
680 | /* | |
681 | * We could be clever and allow to attach a counter to an | |
682 | * offline CPU and activate it when the CPU comes up, but | |
683 | * that's for later. | |
684 | */ | |
685 | if (!cpu_isset(cpu, cpu_online_map)) | |
686 | return ERR_PTR(-ENODEV); | |
687 | ||
688 | cpuctx = &per_cpu(perf_cpu_context, cpu); | |
689 | ctx = &cpuctx->ctx; | |
690 | ||
0793a61d TG |
691 | return ctx; |
692 | } | |
693 | ||
694 | rcu_read_lock(); | |
695 | if (!pid) | |
696 | task = current; | |
697 | else | |
698 | task = find_task_by_vpid(pid); | |
699 | if (task) | |
700 | get_task_struct(task); | |
701 | rcu_read_unlock(); | |
702 | ||
703 | if (!task) | |
704 | return ERR_PTR(-ESRCH); | |
705 | ||
706 | ctx = &task->perf_counter_ctx; | |
707 | ctx->task = task; | |
708 | ||
709 | /* Reuse ptrace permission checks for now. */ | |
710 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) { | |
711 | put_context(ctx); | |
712 | return ERR_PTR(-EACCES); | |
713 | } | |
714 | ||
715 | return ctx; | |
716 | } | |
717 | ||
718 | /* | |
719 | * Called when the last reference to the file is gone. | |
720 | */ | |
721 | static int perf_release(struct inode *inode, struct file *file) | |
722 | { | |
723 | struct perf_counter *counter = file->private_data; | |
724 | struct perf_counter_context *ctx = counter->ctx; | |
725 | ||
726 | file->private_data = NULL; | |
727 | ||
728 | mutex_lock(&counter->mutex); | |
729 | ||
04289bb9 | 730 | perf_counter_remove_from_context(counter); |
0793a61d TG |
731 | put_context(ctx); |
732 | ||
733 | mutex_unlock(&counter->mutex); | |
734 | ||
735 | kfree(counter); | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
740 | /* | |
741 | * Read the performance counter - simple non blocking version for now | |
742 | */ | |
743 | static ssize_t | |
744 | perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) | |
745 | { | |
746 | u64 cntval; | |
747 | ||
748 | if (count != sizeof(cntval)) | |
749 | return -EINVAL; | |
750 | ||
751 | mutex_lock(&counter->mutex); | |
04289bb9 | 752 | cntval = perf_counter_read(counter); |
0793a61d TG |
753 | mutex_unlock(&counter->mutex); |
754 | ||
755 | return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval); | |
756 | } | |
757 | ||
758 | static ssize_t | |
759 | perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count) | |
760 | { | |
761 | if (!usrdata->len) | |
762 | return 0; | |
763 | ||
764 | count = min(count, (size_t)usrdata->len); | |
765 | if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count)) | |
766 | return -EFAULT; | |
767 | ||
768 | /* Adjust the counters */ | |
769 | usrdata->len -= count; | |
770 | if (!usrdata->len) | |
771 | usrdata->rd_idx = 0; | |
772 | else | |
773 | usrdata->rd_idx += count; | |
774 | ||
775 | return count; | |
776 | } | |
777 | ||
778 | static ssize_t | |
779 | perf_read_irq_data(struct perf_counter *counter, | |
780 | char __user *buf, | |
781 | size_t count, | |
782 | int nonblocking) | |
783 | { | |
784 | struct perf_data *irqdata, *usrdata; | |
785 | DECLARE_WAITQUEUE(wait, current); | |
786 | ssize_t res; | |
787 | ||
788 | irqdata = counter->irqdata; | |
789 | usrdata = counter->usrdata; | |
790 | ||
791 | if (usrdata->len + irqdata->len >= count) | |
792 | goto read_pending; | |
793 | ||
794 | if (nonblocking) | |
795 | return -EAGAIN; | |
796 | ||
797 | spin_lock_irq(&counter->waitq.lock); | |
798 | __add_wait_queue(&counter->waitq, &wait); | |
799 | for (;;) { | |
800 | set_current_state(TASK_INTERRUPTIBLE); | |
801 | if (usrdata->len + irqdata->len >= count) | |
802 | break; | |
803 | ||
804 | if (signal_pending(current)) | |
805 | break; | |
806 | ||
807 | spin_unlock_irq(&counter->waitq.lock); | |
808 | schedule(); | |
809 | spin_lock_irq(&counter->waitq.lock); | |
810 | } | |
811 | __remove_wait_queue(&counter->waitq, &wait); | |
812 | __set_current_state(TASK_RUNNING); | |
813 | spin_unlock_irq(&counter->waitq.lock); | |
814 | ||
815 | if (usrdata->len + irqdata->len < count) | |
816 | return -ERESTARTSYS; | |
817 | read_pending: | |
818 | mutex_lock(&counter->mutex); | |
819 | ||
820 | /* Drain pending data first: */ | |
821 | res = perf_copy_usrdata(usrdata, buf, count); | |
822 | if (res < 0 || res == count) | |
823 | goto out; | |
824 | ||
825 | /* Switch irq buffer: */ | |
826 | usrdata = perf_switch_irq_data(counter); | |
827 | if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) { | |
828 | if (!res) | |
829 | res = -EFAULT; | |
830 | } else { | |
831 | res = count; | |
832 | } | |
833 | out: | |
834 | mutex_unlock(&counter->mutex); | |
835 | ||
836 | return res; | |
837 | } | |
838 | ||
839 | static ssize_t | |
840 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
841 | { | |
842 | struct perf_counter *counter = file->private_data; | |
843 | ||
9f66a381 | 844 | switch (counter->hw_event.record_type) { |
0793a61d TG |
845 | case PERF_RECORD_SIMPLE: |
846 | return perf_read_hw(counter, buf, count); | |
847 | ||
848 | case PERF_RECORD_IRQ: | |
849 | case PERF_RECORD_GROUP: | |
850 | return perf_read_irq_data(counter, buf, count, | |
851 | file->f_flags & O_NONBLOCK); | |
852 | } | |
853 | return -EINVAL; | |
854 | } | |
855 | ||
856 | static unsigned int perf_poll(struct file *file, poll_table *wait) | |
857 | { | |
858 | struct perf_counter *counter = file->private_data; | |
859 | unsigned int events = 0; | |
860 | unsigned long flags; | |
861 | ||
862 | poll_wait(file, &counter->waitq, wait); | |
863 | ||
864 | spin_lock_irqsave(&counter->waitq.lock, flags); | |
865 | if (counter->usrdata->len || counter->irqdata->len) | |
866 | events |= POLLIN; | |
867 | spin_unlock_irqrestore(&counter->waitq.lock, flags); | |
868 | ||
869 | return events; | |
870 | } | |
871 | ||
872 | static const struct file_operations perf_fops = { | |
873 | .release = perf_release, | |
874 | .read = perf_read, | |
875 | .poll = perf_poll, | |
876 | }; | |
877 | ||
95cdd2e7 | 878 | static int cpu_clock_perf_counter_enable(struct perf_counter *counter) |
5c92d124 | 879 | { |
95cdd2e7 | 880 | return 0; |
5c92d124 IM |
881 | } |
882 | ||
883 | static void cpu_clock_perf_counter_disable(struct perf_counter *counter) | |
884 | { | |
885 | } | |
886 | ||
887 | static void cpu_clock_perf_counter_read(struct perf_counter *counter) | |
888 | { | |
889 | int cpu = raw_smp_processor_id(); | |
890 | ||
ee06094f | 891 | atomic64_set(&counter->count, cpu_clock(cpu)); |
5c92d124 IM |
892 | } |
893 | ||
894 | static const struct hw_perf_counter_ops perf_ops_cpu_clock = { | |
7671581f IM |
895 | .enable = cpu_clock_perf_counter_enable, |
896 | .disable = cpu_clock_perf_counter_disable, | |
897 | .read = cpu_clock_perf_counter_read, | |
5c92d124 IM |
898 | }; |
899 | ||
aa9c4c0f IM |
900 | /* |
901 | * Called from within the scheduler: | |
902 | */ | |
903 | static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update) | |
bae43c99 | 904 | { |
aa9c4c0f IM |
905 | struct task_struct *curr = counter->task; |
906 | u64 delta; | |
907 | ||
908 | WARN_ON_ONCE(counter->task != current); | |
909 | ||
910 | delta = __task_delta_exec(curr, update); | |
911 | ||
912 | return curr->se.sum_exec_runtime + delta; | |
913 | } | |
914 | ||
915 | static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now) | |
916 | { | |
917 | u64 prev; | |
8cb391e8 IM |
918 | s64 delta; |
919 | ||
920 | prev = atomic64_read(&counter->hw.prev_count); | |
8cb391e8 IM |
921 | |
922 | atomic64_set(&counter->hw.prev_count, now); | |
923 | ||
924 | delta = now - prev; | |
8cb391e8 IM |
925 | |
926 | atomic64_add(delta, &counter->count); | |
bae43c99 IM |
927 | } |
928 | ||
8cb391e8 | 929 | static void task_clock_perf_counter_read(struct perf_counter *counter) |
bae43c99 | 930 | { |
aa9c4c0f IM |
931 | u64 now = task_clock_perf_counter_val(counter, 1); |
932 | ||
933 | task_clock_perf_counter_update(counter, now); | |
bae43c99 IM |
934 | } |
935 | ||
95cdd2e7 | 936 | static int task_clock_perf_counter_enable(struct perf_counter *counter) |
8cb391e8 | 937 | { |
aa9c4c0f IM |
938 | u64 now = task_clock_perf_counter_val(counter, 0); |
939 | ||
940 | atomic64_set(&counter->hw.prev_count, now); | |
95cdd2e7 IM |
941 | |
942 | return 0; | |
8cb391e8 IM |
943 | } |
944 | ||
945 | static void task_clock_perf_counter_disable(struct perf_counter *counter) | |
bae43c99 | 946 | { |
aa9c4c0f IM |
947 | u64 now = task_clock_perf_counter_val(counter, 0); |
948 | ||
949 | task_clock_perf_counter_update(counter, now); | |
bae43c99 IM |
950 | } |
951 | ||
952 | static const struct hw_perf_counter_ops perf_ops_task_clock = { | |
7671581f IM |
953 | .enable = task_clock_perf_counter_enable, |
954 | .disable = task_clock_perf_counter_disable, | |
955 | .read = task_clock_perf_counter_read, | |
bae43c99 IM |
956 | }; |
957 | ||
e06c61a8 IM |
958 | static u64 get_page_faults(void) |
959 | { | |
960 | struct task_struct *curr = current; | |
961 | ||
962 | return curr->maj_flt + curr->min_flt; | |
963 | } | |
964 | ||
965 | static void page_faults_perf_counter_update(struct perf_counter *counter) | |
966 | { | |
967 | u64 prev, now; | |
968 | s64 delta; | |
969 | ||
970 | prev = atomic64_read(&counter->hw.prev_count); | |
971 | now = get_page_faults(); | |
972 | ||
973 | atomic64_set(&counter->hw.prev_count, now); | |
974 | ||
975 | delta = now - prev; | |
e06c61a8 IM |
976 | |
977 | atomic64_add(delta, &counter->count); | |
978 | } | |
979 | ||
980 | static void page_faults_perf_counter_read(struct perf_counter *counter) | |
981 | { | |
982 | page_faults_perf_counter_update(counter); | |
983 | } | |
984 | ||
95cdd2e7 | 985 | static int page_faults_perf_counter_enable(struct perf_counter *counter) |
e06c61a8 IM |
986 | { |
987 | /* | |
988 | * page-faults is a per-task value already, | |
989 | * so we dont have to clear it on switch-in. | |
990 | */ | |
95cdd2e7 IM |
991 | |
992 | return 0; | |
e06c61a8 IM |
993 | } |
994 | ||
995 | static void page_faults_perf_counter_disable(struct perf_counter *counter) | |
996 | { | |
997 | page_faults_perf_counter_update(counter); | |
998 | } | |
999 | ||
1000 | static const struct hw_perf_counter_ops perf_ops_page_faults = { | |
7671581f IM |
1001 | .enable = page_faults_perf_counter_enable, |
1002 | .disable = page_faults_perf_counter_disable, | |
1003 | .read = page_faults_perf_counter_read, | |
e06c61a8 IM |
1004 | }; |
1005 | ||
5d6a27d8 IM |
1006 | static u64 get_context_switches(void) |
1007 | { | |
1008 | struct task_struct *curr = current; | |
1009 | ||
1010 | return curr->nvcsw + curr->nivcsw; | |
1011 | } | |
1012 | ||
1013 | static void context_switches_perf_counter_update(struct perf_counter *counter) | |
1014 | { | |
1015 | u64 prev, now; | |
1016 | s64 delta; | |
1017 | ||
1018 | prev = atomic64_read(&counter->hw.prev_count); | |
1019 | now = get_context_switches(); | |
1020 | ||
1021 | atomic64_set(&counter->hw.prev_count, now); | |
1022 | ||
1023 | delta = now - prev; | |
5d6a27d8 IM |
1024 | |
1025 | atomic64_add(delta, &counter->count); | |
1026 | } | |
1027 | ||
1028 | static void context_switches_perf_counter_read(struct perf_counter *counter) | |
1029 | { | |
1030 | context_switches_perf_counter_update(counter); | |
1031 | } | |
1032 | ||
95cdd2e7 | 1033 | static int context_switches_perf_counter_enable(struct perf_counter *counter) |
5d6a27d8 IM |
1034 | { |
1035 | /* | |
1036 | * ->nvcsw + curr->nivcsw is a per-task value already, | |
1037 | * so we dont have to clear it on switch-in. | |
1038 | */ | |
95cdd2e7 IM |
1039 | |
1040 | return 0; | |
5d6a27d8 IM |
1041 | } |
1042 | ||
1043 | static void context_switches_perf_counter_disable(struct perf_counter *counter) | |
1044 | { | |
1045 | context_switches_perf_counter_update(counter); | |
1046 | } | |
1047 | ||
1048 | static const struct hw_perf_counter_ops perf_ops_context_switches = { | |
7671581f IM |
1049 | .enable = context_switches_perf_counter_enable, |
1050 | .disable = context_switches_perf_counter_disable, | |
1051 | .read = context_switches_perf_counter_read, | |
5d6a27d8 IM |
1052 | }; |
1053 | ||
6c594c21 IM |
1054 | static inline u64 get_cpu_migrations(void) |
1055 | { | |
1056 | return current->se.nr_migrations; | |
1057 | } | |
1058 | ||
1059 | static void cpu_migrations_perf_counter_update(struct perf_counter *counter) | |
1060 | { | |
1061 | u64 prev, now; | |
1062 | s64 delta; | |
1063 | ||
1064 | prev = atomic64_read(&counter->hw.prev_count); | |
1065 | now = get_cpu_migrations(); | |
1066 | ||
1067 | atomic64_set(&counter->hw.prev_count, now); | |
1068 | ||
1069 | delta = now - prev; | |
6c594c21 IM |
1070 | |
1071 | atomic64_add(delta, &counter->count); | |
1072 | } | |
1073 | ||
1074 | static void cpu_migrations_perf_counter_read(struct perf_counter *counter) | |
1075 | { | |
1076 | cpu_migrations_perf_counter_update(counter); | |
1077 | } | |
1078 | ||
95cdd2e7 | 1079 | static int cpu_migrations_perf_counter_enable(struct perf_counter *counter) |
6c594c21 IM |
1080 | { |
1081 | /* | |
1082 | * se.nr_migrations is a per-task value already, | |
1083 | * so we dont have to clear it on switch-in. | |
1084 | */ | |
95cdd2e7 IM |
1085 | |
1086 | return 0; | |
6c594c21 IM |
1087 | } |
1088 | ||
1089 | static void cpu_migrations_perf_counter_disable(struct perf_counter *counter) | |
1090 | { | |
1091 | cpu_migrations_perf_counter_update(counter); | |
1092 | } | |
1093 | ||
1094 | static const struct hw_perf_counter_ops perf_ops_cpu_migrations = { | |
7671581f IM |
1095 | .enable = cpu_migrations_perf_counter_enable, |
1096 | .disable = cpu_migrations_perf_counter_disable, | |
1097 | .read = cpu_migrations_perf_counter_read, | |
6c594c21 IM |
1098 | }; |
1099 | ||
5c92d124 IM |
1100 | static const struct hw_perf_counter_ops * |
1101 | sw_perf_counter_init(struct perf_counter *counter) | |
1102 | { | |
1103 | const struct hw_perf_counter_ops *hw_ops = NULL; | |
1104 | ||
1105 | switch (counter->hw_event.type) { | |
1106 | case PERF_COUNT_CPU_CLOCK: | |
1107 | hw_ops = &perf_ops_cpu_clock; | |
1108 | break; | |
bae43c99 IM |
1109 | case PERF_COUNT_TASK_CLOCK: |
1110 | hw_ops = &perf_ops_task_clock; | |
1111 | break; | |
e06c61a8 IM |
1112 | case PERF_COUNT_PAGE_FAULTS: |
1113 | hw_ops = &perf_ops_page_faults; | |
1114 | break; | |
5d6a27d8 IM |
1115 | case PERF_COUNT_CONTEXT_SWITCHES: |
1116 | hw_ops = &perf_ops_context_switches; | |
1117 | break; | |
6c594c21 IM |
1118 | case PERF_COUNT_CPU_MIGRATIONS: |
1119 | hw_ops = &perf_ops_cpu_migrations; | |
1120 | break; | |
5c92d124 IM |
1121 | default: |
1122 | break; | |
1123 | } | |
1124 | return hw_ops; | |
1125 | } | |
1126 | ||
0793a61d TG |
1127 | /* |
1128 | * Allocate and initialize a counter structure | |
1129 | */ | |
1130 | static struct perf_counter * | |
04289bb9 IM |
1131 | perf_counter_alloc(struct perf_counter_hw_event *hw_event, |
1132 | int cpu, | |
9b51f66d IM |
1133 | struct perf_counter *group_leader, |
1134 | gfp_t gfpflags) | |
0793a61d | 1135 | { |
5c92d124 | 1136 | const struct hw_perf_counter_ops *hw_ops; |
621a01ea | 1137 | struct perf_counter *counter; |
0793a61d | 1138 | |
9b51f66d | 1139 | counter = kzalloc(sizeof(*counter), gfpflags); |
0793a61d TG |
1140 | if (!counter) |
1141 | return NULL; | |
1142 | ||
04289bb9 IM |
1143 | /* |
1144 | * Single counters are their own group leaders, with an | |
1145 | * empty sibling list: | |
1146 | */ | |
1147 | if (!group_leader) | |
1148 | group_leader = counter; | |
1149 | ||
0793a61d | 1150 | mutex_init(&counter->mutex); |
04289bb9 IM |
1151 | INIT_LIST_HEAD(&counter->list_entry); |
1152 | INIT_LIST_HEAD(&counter->sibling_list); | |
0793a61d TG |
1153 | init_waitqueue_head(&counter->waitq); |
1154 | ||
9f66a381 IM |
1155 | counter->irqdata = &counter->data[0]; |
1156 | counter->usrdata = &counter->data[1]; | |
1157 | counter->cpu = cpu; | |
1158 | counter->hw_event = *hw_event; | |
1159 | counter->wakeup_pending = 0; | |
04289bb9 | 1160 | counter->group_leader = group_leader; |
621a01ea IM |
1161 | counter->hw_ops = NULL; |
1162 | ||
a86ed508 IM |
1163 | if (hw_event->disabled) |
1164 | counter->state = PERF_COUNTER_STATE_OFF; | |
1165 | ||
5c92d124 IM |
1166 | hw_ops = NULL; |
1167 | if (!hw_event->raw && hw_event->type < 0) | |
1168 | hw_ops = sw_perf_counter_init(counter); | |
9b51f66d | 1169 | if (!hw_ops) |
5c92d124 | 1170 | hw_ops = hw_perf_counter_init(counter); |
5c92d124 | 1171 | |
621a01ea IM |
1172 | if (!hw_ops) { |
1173 | kfree(counter); | |
1174 | return NULL; | |
1175 | } | |
1176 | counter->hw_ops = hw_ops; | |
0793a61d TG |
1177 | |
1178 | return counter; | |
1179 | } | |
1180 | ||
1181 | /** | |
9f66a381 IM |
1182 | * sys_perf_task_open - open a performance counter, associate it to a task/cpu |
1183 | * | |
1184 | * @hw_event_uptr: event type attributes for monitoring/sampling | |
0793a61d | 1185 | * @pid: target pid |
9f66a381 IM |
1186 | * @cpu: target cpu |
1187 | * @group_fd: group leader counter fd | |
0793a61d | 1188 | */ |
1d1c7ddb IM |
1189 | asmlinkage int |
1190 | sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user, | |
1191 | pid_t pid, int cpu, int group_fd) | |
0793a61d | 1192 | { |
04289bb9 | 1193 | struct perf_counter *counter, *group_leader; |
9f66a381 | 1194 | struct perf_counter_hw_event hw_event; |
04289bb9 | 1195 | struct perf_counter_context *ctx; |
9b51f66d | 1196 | struct file *counter_file = NULL; |
04289bb9 IM |
1197 | struct file *group_file = NULL; |
1198 | int fput_needed = 0; | |
9b51f66d | 1199 | int fput_needed2 = 0; |
0793a61d TG |
1200 | int ret; |
1201 | ||
9f66a381 | 1202 | if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0) |
eab656ae TG |
1203 | return -EFAULT; |
1204 | ||
04289bb9 | 1205 | /* |
ccff286d IM |
1206 | * Get the target context (task or percpu): |
1207 | */ | |
1208 | ctx = find_get_context(pid, cpu); | |
1209 | if (IS_ERR(ctx)) | |
1210 | return PTR_ERR(ctx); | |
1211 | ||
1212 | /* | |
1213 | * Look up the group leader (we will attach this counter to it): | |
04289bb9 IM |
1214 | */ |
1215 | group_leader = NULL; | |
1216 | if (group_fd != -1) { | |
1217 | ret = -EINVAL; | |
1218 | group_file = fget_light(group_fd, &fput_needed); | |
1219 | if (!group_file) | |
ccff286d | 1220 | goto err_put_context; |
04289bb9 | 1221 | if (group_file->f_op != &perf_fops) |
ccff286d | 1222 | goto err_put_context; |
04289bb9 IM |
1223 | |
1224 | group_leader = group_file->private_data; | |
1225 | /* | |
ccff286d IM |
1226 | * Do not allow a recursive hierarchy (this new sibling |
1227 | * becoming part of another group-sibling): | |
1228 | */ | |
1229 | if (group_leader->group_leader != group_leader) | |
1230 | goto err_put_context; | |
1231 | /* | |
1232 | * Do not allow to attach to a group in a different | |
1233 | * task or CPU context: | |
04289bb9 | 1234 | */ |
ccff286d IM |
1235 | if (group_leader->ctx != ctx) |
1236 | goto err_put_context; | |
04289bb9 IM |
1237 | } |
1238 | ||
5c92d124 | 1239 | ret = -EINVAL; |
9b51f66d | 1240 | counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL); |
0793a61d TG |
1241 | if (!counter) |
1242 | goto err_put_context; | |
1243 | ||
0793a61d TG |
1244 | ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0); |
1245 | if (ret < 0) | |
9b51f66d IM |
1246 | goto err_free_put_context; |
1247 | ||
1248 | counter_file = fget_light(ret, &fput_needed2); | |
1249 | if (!counter_file) | |
1250 | goto err_free_put_context; | |
1251 | ||
1252 | counter->filp = counter_file; | |
1253 | perf_install_in_context(ctx, counter, cpu); | |
1254 | ||
1255 | fput_light(counter_file, fput_needed2); | |
0793a61d | 1256 | |
04289bb9 IM |
1257 | out_fput: |
1258 | fput_light(group_file, fput_needed); | |
1259 | ||
0793a61d TG |
1260 | return ret; |
1261 | ||
9b51f66d | 1262 | err_free_put_context: |
0793a61d TG |
1263 | kfree(counter); |
1264 | ||
1265 | err_put_context: | |
1266 | put_context(ctx); | |
1267 | ||
04289bb9 | 1268 | goto out_fput; |
0793a61d TG |
1269 | } |
1270 | ||
9b51f66d IM |
1271 | /* |
1272 | * Initialize the perf_counter context in a task_struct: | |
1273 | */ | |
1274 | static void | |
1275 | __perf_counter_init_context(struct perf_counter_context *ctx, | |
1276 | struct task_struct *task) | |
1277 | { | |
1278 | memset(ctx, 0, sizeof(*ctx)); | |
1279 | spin_lock_init(&ctx->lock); | |
1280 | INIT_LIST_HEAD(&ctx->counter_list); | |
1281 | ctx->task = task; | |
1282 | } | |
1283 | ||
1284 | /* | |
1285 | * inherit a counter from parent task to child task: | |
1286 | */ | |
1287 | static int | |
1288 | inherit_counter(struct perf_counter *parent_counter, | |
1289 | struct task_struct *parent, | |
1290 | struct perf_counter_context *parent_ctx, | |
1291 | struct task_struct *child, | |
1292 | struct perf_counter_context *child_ctx) | |
1293 | { | |
1294 | struct perf_counter *child_counter; | |
1295 | ||
1296 | child_counter = perf_counter_alloc(&parent_counter->hw_event, | |
1297 | parent_counter->cpu, NULL, | |
1298 | GFP_ATOMIC); | |
1299 | if (!child_counter) | |
1300 | return -ENOMEM; | |
1301 | ||
1302 | /* | |
1303 | * Link it up in the child's context: | |
1304 | */ | |
1305 | child_counter->ctx = child_ctx; | |
1306 | child_counter->task = child; | |
1307 | list_add_counter(child_counter, child_ctx); | |
1308 | child_ctx->nr_counters++; | |
1309 | ||
1310 | child_counter->parent = parent_counter; | |
9b51f66d IM |
1311 | /* |
1312 | * inherit into child's child as well: | |
1313 | */ | |
1314 | child_counter->hw_event.inherit = 1; | |
1315 | ||
1316 | /* | |
1317 | * Get a reference to the parent filp - we will fput it | |
1318 | * when the child counter exits. This is safe to do because | |
1319 | * we are in the parent and we know that the filp still | |
1320 | * exists and has a nonzero count: | |
1321 | */ | |
1322 | atomic_long_inc(&parent_counter->filp->f_count); | |
1323 | ||
1324 | return 0; | |
1325 | } | |
1326 | ||
1327 | static void | |
1328 | __perf_counter_exit_task(struct task_struct *child, | |
1329 | struct perf_counter *child_counter, | |
1330 | struct perf_counter_context *child_ctx) | |
1331 | { | |
1332 | struct perf_counter *parent_counter; | |
1333 | u64 parent_val, child_val; | |
aa9c4c0f | 1334 | unsigned long flags; |
9b51f66d IM |
1335 | u64 perf_flags; |
1336 | ||
1337 | /* | |
1338 | * Disable and unlink this counter. | |
1339 | * | |
1340 | * Be careful about zapping the list - IRQ/NMI context | |
1341 | * could still be processing it: | |
1342 | */ | |
aa9c4c0f | 1343 | curr_rq_lock_irq_save(&flags); |
9b51f66d IM |
1344 | perf_flags = hw_perf_save_disable(); |
1345 | ||
0cc0c027 IM |
1346 | if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) { |
1347 | struct perf_cpu_context *cpuctx; | |
1348 | ||
1349 | cpuctx = &__get_cpu_var(perf_cpu_context); | |
1350 | ||
7671581f | 1351 | child_counter->hw_ops->disable(child_counter); |
0cc0c027 IM |
1352 | child_counter->state = PERF_COUNTER_STATE_INACTIVE; |
1353 | child_counter->oncpu = -1; | |
1354 | ||
1355 | cpuctx->active_oncpu--; | |
1356 | child_ctx->nr_active--; | |
1357 | } | |
1358 | ||
9b51f66d IM |
1359 | list_del_init(&child_counter->list_entry); |
1360 | ||
1361 | hw_perf_restore(perf_flags); | |
aa9c4c0f | 1362 | curr_rq_unlock_irq_restore(&flags); |
9b51f66d IM |
1363 | |
1364 | parent_counter = child_counter->parent; | |
1365 | /* | |
1366 | * It can happen that parent exits first, and has counters | |
1367 | * that are still around due to the child reference. These | |
1368 | * counters need to be zapped - but otherwise linger. | |
1369 | */ | |
1370 | if (!parent_counter) | |
1371 | return; | |
1372 | ||
1373 | parent_val = atomic64_read(&parent_counter->count); | |
1374 | child_val = atomic64_read(&child_counter->count); | |
1375 | ||
1376 | /* | |
1377 | * Add back the child's count to the parent's count: | |
1378 | */ | |
1379 | atomic64_add(child_val, &parent_counter->count); | |
1380 | ||
1381 | fput(parent_counter->filp); | |
1382 | ||
1383 | kfree(child_counter); | |
1384 | } | |
1385 | ||
1386 | /* | |
1387 | * When a child task exist, feed back counter values to parent counters. | |
1388 | * | |
1389 | * Note: we are running in child context, but the PID is not hashed | |
1390 | * anymore so new counters will not be added. | |
1391 | */ | |
1392 | void perf_counter_exit_task(struct task_struct *child) | |
1393 | { | |
1394 | struct perf_counter *child_counter, *tmp; | |
1395 | struct perf_counter_context *child_ctx; | |
1396 | ||
1397 | child_ctx = &child->perf_counter_ctx; | |
1398 | ||
1399 | if (likely(!child_ctx->nr_counters)) | |
1400 | return; | |
1401 | ||
1402 | list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list, | |
1403 | list_entry) | |
1404 | __perf_counter_exit_task(child, child_counter, child_ctx); | |
1405 | } | |
1406 | ||
1407 | /* | |
1408 | * Initialize the perf_counter context in task_struct | |
1409 | */ | |
1410 | void perf_counter_init_task(struct task_struct *child) | |
1411 | { | |
1412 | struct perf_counter_context *child_ctx, *parent_ctx; | |
1413 | struct perf_counter *counter, *parent_counter; | |
1414 | struct task_struct *parent = current; | |
1415 | unsigned long flags; | |
1416 | ||
1417 | child_ctx = &child->perf_counter_ctx; | |
1418 | parent_ctx = &parent->perf_counter_ctx; | |
1419 | ||
1420 | __perf_counter_init_context(child_ctx, child); | |
1421 | ||
1422 | /* | |
1423 | * This is executed from the parent task context, so inherit | |
1424 | * counters that have been marked for cloning: | |
1425 | */ | |
1426 | ||
1427 | if (likely(!parent_ctx->nr_counters)) | |
1428 | return; | |
1429 | ||
1430 | /* | |
1431 | * Lock the parent list. No need to lock the child - not PID | |
1432 | * hashed yet and not running, so nobody can access it. | |
1433 | */ | |
1434 | spin_lock_irqsave(&parent_ctx->lock, flags); | |
1435 | ||
1436 | /* | |
1437 | * We dont have to disable NMIs - we are only looking at | |
1438 | * the list, not manipulating it: | |
1439 | */ | |
1440 | list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) { | |
1441 | if (!counter->hw_event.inherit || counter->group_leader != counter) | |
1442 | continue; | |
1443 | ||
1444 | /* | |
1445 | * Instead of creating recursive hierarchies of counters, | |
1446 | * we link inheritd counters back to the original parent, | |
1447 | * which has a filp for sure, which we use as the reference | |
1448 | * count: | |
1449 | */ | |
1450 | parent_counter = counter; | |
1451 | if (counter->parent) | |
1452 | parent_counter = counter->parent; | |
1453 | ||
1454 | if (inherit_counter(parent_counter, parent, | |
1455 | parent_ctx, child, child_ctx)) | |
1456 | break; | |
1457 | } | |
1458 | ||
1459 | spin_unlock_irqrestore(&parent_ctx->lock, flags); | |
1460 | } | |
1461 | ||
04289bb9 | 1462 | static void __cpuinit perf_counter_init_cpu(int cpu) |
0793a61d | 1463 | { |
04289bb9 | 1464 | struct perf_cpu_context *cpuctx; |
0793a61d | 1465 | |
04289bb9 IM |
1466 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
1467 | __perf_counter_init_context(&cpuctx->ctx, NULL); | |
0793a61d TG |
1468 | |
1469 | mutex_lock(&perf_resource_mutex); | |
04289bb9 | 1470 | cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu; |
0793a61d | 1471 | mutex_unlock(&perf_resource_mutex); |
04289bb9 | 1472 | |
0793a61d TG |
1473 | hw_perf_counter_setup(); |
1474 | } | |
1475 | ||
1476 | #ifdef CONFIG_HOTPLUG_CPU | |
04289bb9 | 1477 | static void __perf_counter_exit_cpu(void *info) |
0793a61d TG |
1478 | { |
1479 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); | |
1480 | struct perf_counter_context *ctx = &cpuctx->ctx; | |
1481 | struct perf_counter *counter, *tmp; | |
1482 | ||
04289bb9 IM |
1483 | list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) |
1484 | __perf_counter_remove_from_context(counter); | |
0793a61d TG |
1485 | |
1486 | } | |
04289bb9 | 1487 | static void perf_counter_exit_cpu(int cpu) |
0793a61d | 1488 | { |
04289bb9 | 1489 | smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1); |
0793a61d TG |
1490 | } |
1491 | #else | |
04289bb9 | 1492 | static inline void perf_counter_exit_cpu(int cpu) { } |
0793a61d TG |
1493 | #endif |
1494 | ||
1495 | static int __cpuinit | |
1496 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) | |
1497 | { | |
1498 | unsigned int cpu = (long)hcpu; | |
1499 | ||
1500 | switch (action) { | |
1501 | ||
1502 | case CPU_UP_PREPARE: | |
1503 | case CPU_UP_PREPARE_FROZEN: | |
04289bb9 | 1504 | perf_counter_init_cpu(cpu); |
0793a61d TG |
1505 | break; |
1506 | ||
1507 | case CPU_DOWN_PREPARE: | |
1508 | case CPU_DOWN_PREPARE_FROZEN: | |
04289bb9 | 1509 | perf_counter_exit_cpu(cpu); |
0793a61d TG |
1510 | break; |
1511 | ||
1512 | default: | |
1513 | break; | |
1514 | } | |
1515 | ||
1516 | return NOTIFY_OK; | |
1517 | } | |
1518 | ||
1519 | static struct notifier_block __cpuinitdata perf_cpu_nb = { | |
1520 | .notifier_call = perf_cpu_notify, | |
1521 | }; | |
1522 | ||
1523 | static int __init perf_counter_init(void) | |
1524 | { | |
1525 | perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE, | |
1526 | (void *)(long)smp_processor_id()); | |
1527 | register_cpu_notifier(&perf_cpu_nb); | |
1528 | ||
1529 | return 0; | |
1530 | } | |
1531 | early_initcall(perf_counter_init); | |
1532 | ||
1533 | static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf) | |
1534 | { | |
1535 | return sprintf(buf, "%d\n", perf_reserved_percpu); | |
1536 | } | |
1537 | ||
1538 | static ssize_t | |
1539 | perf_set_reserve_percpu(struct sysdev_class *class, | |
1540 | const char *buf, | |
1541 | size_t count) | |
1542 | { | |
1543 | struct perf_cpu_context *cpuctx; | |
1544 | unsigned long val; | |
1545 | int err, cpu, mpt; | |
1546 | ||
1547 | err = strict_strtoul(buf, 10, &val); | |
1548 | if (err) | |
1549 | return err; | |
1550 | if (val > perf_max_counters) | |
1551 | return -EINVAL; | |
1552 | ||
1553 | mutex_lock(&perf_resource_mutex); | |
1554 | perf_reserved_percpu = val; | |
1555 | for_each_online_cpu(cpu) { | |
1556 | cpuctx = &per_cpu(perf_cpu_context, cpu); | |
1557 | spin_lock_irq(&cpuctx->ctx.lock); | |
1558 | mpt = min(perf_max_counters - cpuctx->ctx.nr_counters, | |
1559 | perf_max_counters - perf_reserved_percpu); | |
1560 | cpuctx->max_pertask = mpt; | |
1561 | spin_unlock_irq(&cpuctx->ctx.lock); | |
1562 | } | |
1563 | mutex_unlock(&perf_resource_mutex); | |
1564 | ||
1565 | return count; | |
1566 | } | |
1567 | ||
1568 | static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf) | |
1569 | { | |
1570 | return sprintf(buf, "%d\n", perf_overcommit); | |
1571 | } | |
1572 | ||
1573 | static ssize_t | |
1574 | perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count) | |
1575 | { | |
1576 | unsigned long val; | |
1577 | int err; | |
1578 | ||
1579 | err = strict_strtoul(buf, 10, &val); | |
1580 | if (err) | |
1581 | return err; | |
1582 | if (val > 1) | |
1583 | return -EINVAL; | |
1584 | ||
1585 | mutex_lock(&perf_resource_mutex); | |
1586 | perf_overcommit = val; | |
1587 | mutex_unlock(&perf_resource_mutex); | |
1588 | ||
1589 | return count; | |
1590 | } | |
1591 | ||
1592 | static SYSDEV_CLASS_ATTR( | |
1593 | reserve_percpu, | |
1594 | 0644, | |
1595 | perf_show_reserve_percpu, | |
1596 | perf_set_reserve_percpu | |
1597 | ); | |
1598 | ||
1599 | static SYSDEV_CLASS_ATTR( | |
1600 | overcommit, | |
1601 | 0644, | |
1602 | perf_show_overcommit, | |
1603 | perf_set_overcommit | |
1604 | ); | |
1605 | ||
1606 | static struct attribute *perfclass_attrs[] = { | |
1607 | &attr_reserve_percpu.attr, | |
1608 | &attr_overcommit.attr, | |
1609 | NULL | |
1610 | }; | |
1611 | ||
1612 | static struct attribute_group perfclass_attr_group = { | |
1613 | .attrs = perfclass_attrs, | |
1614 | .name = "perf_counters", | |
1615 | }; | |
1616 | ||
1617 | static int __init perf_counter_sysfs_init(void) | |
1618 | { | |
1619 | return sysfs_create_group(&cpu_sysdev_class.kset.kobj, | |
1620 | &perfclass_attr_group); | |
1621 | } | |
1622 | device_initcall(perf_counter_sysfs_init); |