]>
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> | |
21 | #include <linux/perf_counter.h> | |
22 | ||
23 | /* | |
24 | * Each CPU has a list of per CPU counters: | |
25 | */ | |
26 | DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context); | |
27 | ||
28 | int perf_max_counters __read_mostly; | |
29 | static int perf_reserved_percpu __read_mostly; | |
30 | static int perf_overcommit __read_mostly = 1; | |
31 | ||
32 | /* | |
33 | * Mutex for (sysadmin-configurable) counter reservations: | |
34 | */ | |
35 | static DEFINE_MUTEX(perf_resource_mutex); | |
36 | ||
37 | /* | |
38 | * Architecture provided APIs - weak aliases: | |
39 | */ | |
621a01ea IM |
40 | extern __weak struct hw_perf_counter_ops * |
41 | hw_perf_counter_init(struct perf_counter *counter) | |
0793a61d | 42 | { |
621a01ea | 43 | return ERR_PTR(-EINVAL); |
0793a61d TG |
44 | } |
45 | ||
621a01ea IM |
46 | void __weak hw_perf_disable_all(void) { } |
47 | void __weak hw_perf_enable_all(void) { } | |
48 | void __weak hw_perf_counter_setup(void) { } | |
0793a61d TG |
49 | |
50 | #if BITS_PER_LONG == 64 | |
51 | ||
52 | /* | |
53 | * Read the cached counter in counter safe against cross CPU / NMI | |
54 | * modifications. 64 bit version - no complications. | |
55 | */ | |
04289bb9 | 56 | static inline u64 perf_counter_read_safe(struct perf_counter *counter) |
0793a61d TG |
57 | { |
58 | return (u64) atomic64_read(&counter->count); | |
59 | } | |
60 | ||
61 | #else | |
62 | ||
63 | /* | |
64 | * Read the cached counter in counter safe against cross CPU / NMI | |
65 | * modifications. 32 bit version. | |
66 | */ | |
04289bb9 | 67 | static u64 perf_counter_read_safe(struct perf_counter *counter) |
0793a61d TG |
68 | { |
69 | u32 cntl, cnth; | |
70 | ||
71 | local_irq_disable(); | |
72 | do { | |
73 | cnth = atomic_read(&counter->count32[1]); | |
74 | cntl = atomic_read(&counter->count32[0]); | |
75 | } while (cnth != atomic_read(&counter->count32[1])); | |
76 | ||
77 | local_irq_enable(); | |
78 | ||
79 | return cntl | ((u64) cnth) << 32; | |
80 | } | |
81 | ||
82 | #endif | |
83 | ||
04289bb9 IM |
84 | static void |
85 | list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx) | |
86 | { | |
87 | struct perf_counter *group_leader = counter->group_leader; | |
88 | ||
89 | /* | |
90 | * Depending on whether it is a standalone or sibling counter, | |
91 | * add it straight to the context's counter list, or to the group | |
92 | * leader's sibling list: | |
93 | */ | |
94 | if (counter->group_leader == counter) | |
95 | list_add_tail(&counter->list_entry, &ctx->counter_list); | |
96 | else | |
97 | list_add_tail(&counter->list_entry, &group_leader->sibling_list); | |
98 | } | |
99 | ||
100 | static void | |
101 | list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx) | |
102 | { | |
103 | struct perf_counter *sibling, *tmp; | |
104 | ||
105 | list_del_init(&counter->list_entry); | |
106 | ||
04289bb9 IM |
107 | /* |
108 | * If this was a group counter with sibling counters then | |
109 | * upgrade the siblings to singleton counters by adding them | |
110 | * to the context list directly: | |
111 | */ | |
112 | list_for_each_entry_safe(sibling, tmp, | |
113 | &counter->sibling_list, list_entry) { | |
114 | ||
115 | list_del_init(&sibling->list_entry); | |
116 | list_add_tail(&sibling->list_entry, &ctx->counter_list); | |
117 | WARN_ON_ONCE(!sibling->group_leader); | |
118 | WARN_ON_ONCE(sibling->group_leader == sibling); | |
119 | sibling->group_leader = sibling; | |
120 | } | |
121 | } | |
122 | ||
0793a61d TG |
123 | /* |
124 | * Cross CPU call to remove a performance counter | |
125 | * | |
126 | * We disable the counter on the hardware level first. After that we | |
127 | * remove it from the context list. | |
128 | */ | |
04289bb9 | 129 | static void __perf_counter_remove_from_context(void *info) |
0793a61d TG |
130 | { |
131 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); | |
132 | struct perf_counter *counter = info; | |
133 | struct perf_counter_context *ctx = counter->ctx; | |
134 | ||
135 | /* | |
136 | * If this is a task context, we need to check whether it is | |
137 | * the current task context of this cpu. If not it has been | |
138 | * scheduled out before the smp call arrived. | |
139 | */ | |
140 | if (ctx->task && cpuctx->task_ctx != ctx) | |
141 | return; | |
142 | ||
143 | spin_lock(&ctx->lock); | |
144 | ||
145 | if (counter->active) { | |
621a01ea | 146 | counter->hw_ops->hw_perf_counter_disable(counter); |
0793a61d TG |
147 | counter->active = 0; |
148 | ctx->nr_active--; | |
149 | cpuctx->active_oncpu--; | |
150 | counter->task = NULL; | |
151 | } | |
152 | ctx->nr_counters--; | |
153 | ||
154 | /* | |
155 | * Protect the list operation against NMI by disabling the | |
156 | * counters on a global level. NOP for non NMI based counters. | |
157 | */ | |
158 | hw_perf_disable_all(); | |
04289bb9 | 159 | list_del_counter(counter, ctx); |
0793a61d TG |
160 | hw_perf_enable_all(); |
161 | ||
162 | if (!ctx->task) { | |
163 | /* | |
164 | * Allow more per task counters with respect to the | |
165 | * reservation: | |
166 | */ | |
167 | cpuctx->max_pertask = | |
168 | min(perf_max_counters - ctx->nr_counters, | |
169 | perf_max_counters - perf_reserved_percpu); | |
170 | } | |
171 | ||
172 | spin_unlock(&ctx->lock); | |
173 | } | |
174 | ||
175 | ||
176 | /* | |
177 | * Remove the counter from a task's (or a CPU's) list of counters. | |
178 | * | |
179 | * Must be called with counter->mutex held. | |
180 | * | |
181 | * CPU counters are removed with a smp call. For task counters we only | |
182 | * call when the task is on a CPU. | |
183 | */ | |
04289bb9 | 184 | static void perf_counter_remove_from_context(struct perf_counter *counter) |
0793a61d TG |
185 | { |
186 | struct perf_counter_context *ctx = counter->ctx; | |
187 | struct task_struct *task = ctx->task; | |
188 | ||
189 | if (!task) { | |
190 | /* | |
191 | * Per cpu counters are removed via an smp call and | |
192 | * the removal is always sucessful. | |
193 | */ | |
194 | smp_call_function_single(counter->cpu, | |
04289bb9 | 195 | __perf_counter_remove_from_context, |
0793a61d TG |
196 | counter, 1); |
197 | return; | |
198 | } | |
199 | ||
200 | retry: | |
04289bb9 | 201 | task_oncpu_function_call(task, __perf_counter_remove_from_context, |
0793a61d TG |
202 | counter); |
203 | ||
204 | spin_lock_irq(&ctx->lock); | |
205 | /* | |
206 | * If the context is active we need to retry the smp call. | |
207 | */ | |
04289bb9 | 208 | if (ctx->nr_active && !list_empty(&counter->list_entry)) { |
0793a61d TG |
209 | spin_unlock_irq(&ctx->lock); |
210 | goto retry; | |
211 | } | |
212 | ||
213 | /* | |
214 | * The lock prevents that this context is scheduled in so we | |
04289bb9 | 215 | * can remove the counter safely, if the call above did not |
0793a61d TG |
216 | * succeed. |
217 | */ | |
04289bb9 | 218 | if (!list_empty(&counter->list_entry)) { |
0793a61d | 219 | ctx->nr_counters--; |
04289bb9 | 220 | list_del_counter(counter, ctx); |
0793a61d TG |
221 | counter->task = NULL; |
222 | } | |
223 | spin_unlock_irq(&ctx->lock); | |
224 | } | |
225 | ||
226 | /* | |
227 | * Cross CPU call to install and enable a preformance counter | |
228 | */ | |
229 | static void __perf_install_in_context(void *info) | |
230 | { | |
231 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); | |
232 | struct perf_counter *counter = info; | |
233 | struct perf_counter_context *ctx = counter->ctx; | |
234 | int cpu = smp_processor_id(); | |
235 | ||
236 | /* | |
237 | * If this is a task context, we need to check whether it is | |
238 | * the current task context of this cpu. If not it has been | |
239 | * scheduled out before the smp call arrived. | |
240 | */ | |
241 | if (ctx->task && cpuctx->task_ctx != ctx) | |
242 | return; | |
243 | ||
244 | spin_lock(&ctx->lock); | |
245 | ||
246 | /* | |
247 | * Protect the list operation against NMI by disabling the | |
248 | * counters on a global level. NOP for non NMI based counters. | |
249 | */ | |
250 | hw_perf_disable_all(); | |
04289bb9 | 251 | list_add_counter(counter, ctx); |
0793a61d TG |
252 | hw_perf_enable_all(); |
253 | ||
254 | ctx->nr_counters++; | |
255 | ||
256 | if (cpuctx->active_oncpu < perf_max_counters) { | |
621a01ea | 257 | counter->hw_ops->hw_perf_counter_enable(counter); |
0793a61d TG |
258 | counter->active = 1; |
259 | counter->oncpu = cpu; | |
260 | ctx->nr_active++; | |
261 | cpuctx->active_oncpu++; | |
262 | } | |
263 | ||
264 | if (!ctx->task && cpuctx->max_pertask) | |
265 | cpuctx->max_pertask--; | |
266 | ||
267 | spin_unlock(&ctx->lock); | |
268 | } | |
269 | ||
270 | /* | |
271 | * Attach a performance counter to a context | |
272 | * | |
273 | * First we add the counter to the list with the hardware enable bit | |
274 | * in counter->hw_config cleared. | |
275 | * | |
276 | * If the counter is attached to a task which is on a CPU we use a smp | |
277 | * call to enable it in the task context. The task might have been | |
278 | * scheduled away, but we check this in the smp call again. | |
279 | */ | |
280 | static void | |
281 | perf_install_in_context(struct perf_counter_context *ctx, | |
282 | struct perf_counter *counter, | |
283 | int cpu) | |
284 | { | |
285 | struct task_struct *task = ctx->task; | |
286 | ||
287 | counter->ctx = ctx; | |
288 | if (!task) { | |
289 | /* | |
290 | * Per cpu counters are installed via an smp call and | |
291 | * the install is always sucessful. | |
292 | */ | |
293 | smp_call_function_single(cpu, __perf_install_in_context, | |
294 | counter, 1); | |
295 | return; | |
296 | } | |
297 | ||
298 | counter->task = task; | |
299 | retry: | |
300 | task_oncpu_function_call(task, __perf_install_in_context, | |
301 | counter); | |
302 | ||
303 | spin_lock_irq(&ctx->lock); | |
304 | /* | |
305 | * If the context is active and the counter has not been added | |
306 | * we need to retry the smp call. | |
307 | */ | |
04289bb9 | 308 | if (ctx->nr_active && list_empty(&counter->list_entry)) { |
0793a61d TG |
309 | spin_unlock_irq(&ctx->lock); |
310 | goto retry; | |
311 | } | |
312 | ||
313 | /* | |
314 | * The lock prevents that this context is scheduled in so we | |
315 | * can add the counter safely, if it the call above did not | |
316 | * succeed. | |
317 | */ | |
04289bb9 IM |
318 | if (list_empty(&counter->list_entry)) { |
319 | list_add_counter(counter, ctx); | |
0793a61d TG |
320 | ctx->nr_counters++; |
321 | } | |
322 | spin_unlock_irq(&ctx->lock); | |
323 | } | |
324 | ||
04289bb9 IM |
325 | static void |
326 | counter_sched_out(struct perf_counter *counter, | |
327 | struct perf_cpu_context *cpuctx, | |
328 | struct perf_counter_context *ctx) | |
329 | { | |
330 | if (!counter->active) | |
331 | return; | |
332 | ||
621a01ea | 333 | counter->hw_ops->hw_perf_counter_disable(counter); |
04289bb9 IM |
334 | counter->active = 0; |
335 | counter->oncpu = -1; | |
336 | ||
337 | cpuctx->active_oncpu--; | |
338 | ctx->nr_active--; | |
339 | } | |
340 | ||
341 | static void | |
342 | group_sched_out(struct perf_counter *group_counter, | |
343 | struct perf_cpu_context *cpuctx, | |
344 | struct perf_counter_context *ctx) | |
345 | { | |
346 | struct perf_counter *counter; | |
347 | ||
348 | counter_sched_out(group_counter, cpuctx, ctx); | |
349 | ||
350 | /* | |
351 | * Schedule out siblings (if any): | |
352 | */ | |
353 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) | |
354 | counter_sched_out(counter, cpuctx, ctx); | |
355 | } | |
356 | ||
0793a61d TG |
357 | /* |
358 | * Called from scheduler to remove the counters of the current task, | |
359 | * with interrupts disabled. | |
360 | * | |
361 | * We stop each counter and update the counter value in counter->count. | |
362 | * | |
363 | * This does not protect us against NMI, but hw_perf_counter_disable() | |
364 | * sets the disabled bit in the control field of counter _before_ | |
365 | * accessing the counter control register. If a NMI hits, then it will | |
366 | * not restart the counter. | |
367 | */ | |
368 | void perf_counter_task_sched_out(struct task_struct *task, int cpu) | |
369 | { | |
370 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); | |
371 | struct perf_counter_context *ctx = &task->perf_counter_ctx; | |
372 | struct perf_counter *counter; | |
373 | ||
374 | if (likely(!cpuctx->task_ctx)) | |
375 | return; | |
376 | ||
377 | spin_lock(&ctx->lock); | |
04289bb9 IM |
378 | if (ctx->nr_active) { |
379 | list_for_each_entry(counter, &ctx->counter_list, list_entry) | |
380 | group_sched_out(counter, cpuctx, ctx); | |
0793a61d TG |
381 | } |
382 | spin_unlock(&ctx->lock); | |
383 | cpuctx->task_ctx = NULL; | |
384 | } | |
385 | ||
04289bb9 IM |
386 | static void |
387 | counter_sched_in(struct perf_counter *counter, | |
388 | struct perf_cpu_context *cpuctx, | |
389 | struct perf_counter_context *ctx, | |
390 | int cpu) | |
391 | { | |
621a01ea | 392 | counter->hw_ops->hw_perf_counter_enable(counter); |
04289bb9 IM |
393 | counter->active = 1; |
394 | counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */ | |
395 | ||
396 | cpuctx->active_oncpu++; | |
397 | ctx->nr_active++; | |
398 | } | |
399 | ||
400 | static void | |
401 | group_sched_in(struct perf_counter *group_counter, | |
402 | struct perf_cpu_context *cpuctx, | |
403 | struct perf_counter_context *ctx, | |
404 | int cpu) | |
405 | { | |
406 | struct perf_counter *counter; | |
407 | ||
408 | counter_sched_in(group_counter, cpuctx, ctx, cpu); | |
409 | ||
410 | /* | |
411 | * Schedule in siblings as one group (if any): | |
412 | */ | |
413 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) | |
414 | counter_sched_in(counter, cpuctx, ctx, cpu); | |
415 | } | |
416 | ||
0793a61d TG |
417 | /* |
418 | * Called from scheduler to add the counters of the current task | |
419 | * with interrupts disabled. | |
420 | * | |
421 | * We restore the counter value and then enable it. | |
422 | * | |
423 | * This does not protect us against NMI, but hw_perf_counter_enable() | |
424 | * sets the enabled bit in the control field of counter _before_ | |
425 | * accessing the counter control register. If a NMI hits, then it will | |
426 | * keep the counter running. | |
427 | */ | |
428 | void perf_counter_task_sched_in(struct task_struct *task, int cpu) | |
429 | { | |
430 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); | |
431 | struct perf_counter_context *ctx = &task->perf_counter_ctx; | |
432 | struct perf_counter *counter; | |
433 | ||
434 | if (likely(!ctx->nr_counters)) | |
435 | return; | |
436 | ||
437 | spin_lock(&ctx->lock); | |
04289bb9 | 438 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
0793a61d TG |
439 | if (ctx->nr_active == cpuctx->max_pertask) |
440 | break; | |
04289bb9 IM |
441 | |
442 | /* | |
443 | * Listen to the 'cpu' scheduling filter constraint | |
444 | * of counters: | |
445 | */ | |
0793a61d TG |
446 | if (counter->cpu != -1 && counter->cpu != cpu) |
447 | continue; | |
448 | ||
04289bb9 | 449 | group_sched_in(counter, cpuctx, ctx, cpu); |
0793a61d TG |
450 | } |
451 | spin_unlock(&ctx->lock); | |
04289bb9 | 452 | |
0793a61d TG |
453 | cpuctx->task_ctx = ctx; |
454 | } | |
455 | ||
456 | void perf_counter_task_tick(struct task_struct *curr, int cpu) | |
457 | { | |
458 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; | |
459 | struct perf_counter *counter; | |
460 | ||
461 | if (likely(!ctx->nr_counters)) | |
462 | return; | |
463 | ||
464 | perf_counter_task_sched_out(curr, cpu); | |
465 | ||
466 | spin_lock(&ctx->lock); | |
467 | ||
468 | /* | |
04289bb9 | 469 | * Rotate the first entry last (works just fine for group counters too): |
0793a61d TG |
470 | */ |
471 | hw_perf_disable_all(); | |
04289bb9 IM |
472 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
473 | list_del(&counter->list_entry); | |
474 | list_add_tail(&counter->list_entry, &ctx->counter_list); | |
0793a61d TG |
475 | break; |
476 | } | |
477 | hw_perf_enable_all(); | |
478 | ||
479 | spin_unlock(&ctx->lock); | |
480 | ||
481 | perf_counter_task_sched_in(curr, cpu); | |
482 | } | |
483 | ||
04289bb9 IM |
484 | /* |
485 | * Initialize the perf_counter context in a task_struct: | |
486 | */ | |
487 | static void | |
488 | __perf_counter_init_context(struct perf_counter_context *ctx, | |
489 | struct task_struct *task) | |
490 | { | |
491 | spin_lock_init(&ctx->lock); | |
492 | INIT_LIST_HEAD(&ctx->counter_list); | |
493 | ctx->nr_counters = 0; | |
494 | ctx->task = task; | |
495 | } | |
0793a61d TG |
496 | /* |
497 | * Initialize the perf_counter context in task_struct | |
498 | */ | |
499 | void perf_counter_init_task(struct task_struct *task) | |
500 | { | |
04289bb9 | 501 | __perf_counter_init_context(&task->perf_counter_ctx, task); |
0793a61d TG |
502 | } |
503 | ||
504 | /* | |
505 | * Cross CPU call to read the hardware counter | |
506 | */ | |
507 | static void __hw_perf_counter_read(void *info) | |
508 | { | |
621a01ea IM |
509 | struct perf_counter *counter = info; |
510 | ||
511 | counter->hw_ops->hw_perf_counter_read(counter); | |
0793a61d TG |
512 | } |
513 | ||
04289bb9 | 514 | static u64 perf_counter_read(struct perf_counter *counter) |
0793a61d TG |
515 | { |
516 | /* | |
517 | * If counter is enabled and currently active on a CPU, update the | |
518 | * value in the counter structure: | |
519 | */ | |
520 | if (counter->active) { | |
521 | smp_call_function_single(counter->oncpu, | |
522 | __hw_perf_counter_read, counter, 1); | |
523 | } | |
524 | ||
04289bb9 | 525 | return perf_counter_read_safe(counter); |
0793a61d TG |
526 | } |
527 | ||
528 | /* | |
529 | * Cross CPU call to switch performance data pointers | |
530 | */ | |
531 | static void __perf_switch_irq_data(void *info) | |
532 | { | |
533 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); | |
534 | struct perf_counter *counter = info; | |
535 | struct perf_counter_context *ctx = counter->ctx; | |
536 | struct perf_data *oldirqdata = counter->irqdata; | |
537 | ||
538 | /* | |
539 | * If this is a task context, we need to check whether it is | |
540 | * the current task context of this cpu. If not it has been | |
541 | * scheduled out before the smp call arrived. | |
542 | */ | |
543 | if (ctx->task) { | |
544 | if (cpuctx->task_ctx != ctx) | |
545 | return; | |
546 | spin_lock(&ctx->lock); | |
547 | } | |
548 | ||
549 | /* Change the pointer NMI safe */ | |
550 | atomic_long_set((atomic_long_t *)&counter->irqdata, | |
551 | (unsigned long) counter->usrdata); | |
552 | counter->usrdata = oldirqdata; | |
553 | ||
554 | if (ctx->task) | |
555 | spin_unlock(&ctx->lock); | |
556 | } | |
557 | ||
558 | static struct perf_data *perf_switch_irq_data(struct perf_counter *counter) | |
559 | { | |
560 | struct perf_counter_context *ctx = counter->ctx; | |
561 | struct perf_data *oldirqdata = counter->irqdata; | |
562 | struct task_struct *task = ctx->task; | |
563 | ||
564 | if (!task) { | |
565 | smp_call_function_single(counter->cpu, | |
566 | __perf_switch_irq_data, | |
567 | counter, 1); | |
568 | return counter->usrdata; | |
569 | } | |
570 | ||
571 | retry: | |
572 | spin_lock_irq(&ctx->lock); | |
573 | if (!counter->active) { | |
574 | counter->irqdata = counter->usrdata; | |
575 | counter->usrdata = oldirqdata; | |
576 | spin_unlock_irq(&ctx->lock); | |
577 | return oldirqdata; | |
578 | } | |
579 | spin_unlock_irq(&ctx->lock); | |
580 | task_oncpu_function_call(task, __perf_switch_irq_data, counter); | |
581 | /* Might have failed, because task was scheduled out */ | |
582 | if (counter->irqdata == oldirqdata) | |
583 | goto retry; | |
584 | ||
585 | return counter->usrdata; | |
586 | } | |
587 | ||
588 | static void put_context(struct perf_counter_context *ctx) | |
589 | { | |
590 | if (ctx->task) | |
591 | put_task_struct(ctx->task); | |
592 | } | |
593 | ||
594 | static struct perf_counter_context *find_get_context(pid_t pid, int cpu) | |
595 | { | |
596 | struct perf_cpu_context *cpuctx; | |
597 | struct perf_counter_context *ctx; | |
598 | struct task_struct *task; | |
599 | ||
600 | /* | |
601 | * If cpu is not a wildcard then this is a percpu counter: | |
602 | */ | |
603 | if (cpu != -1) { | |
604 | /* Must be root to operate on a CPU counter: */ | |
605 | if (!capable(CAP_SYS_ADMIN)) | |
606 | return ERR_PTR(-EACCES); | |
607 | ||
608 | if (cpu < 0 || cpu > num_possible_cpus()) | |
609 | return ERR_PTR(-EINVAL); | |
610 | ||
611 | /* | |
612 | * We could be clever and allow to attach a counter to an | |
613 | * offline CPU and activate it when the CPU comes up, but | |
614 | * that's for later. | |
615 | */ | |
616 | if (!cpu_isset(cpu, cpu_online_map)) | |
617 | return ERR_PTR(-ENODEV); | |
618 | ||
619 | cpuctx = &per_cpu(perf_cpu_context, cpu); | |
620 | ctx = &cpuctx->ctx; | |
621 | ||
622 | WARN_ON_ONCE(ctx->task); | |
623 | return ctx; | |
624 | } | |
625 | ||
626 | rcu_read_lock(); | |
627 | if (!pid) | |
628 | task = current; | |
629 | else | |
630 | task = find_task_by_vpid(pid); | |
631 | if (task) | |
632 | get_task_struct(task); | |
633 | rcu_read_unlock(); | |
634 | ||
635 | if (!task) | |
636 | return ERR_PTR(-ESRCH); | |
637 | ||
638 | ctx = &task->perf_counter_ctx; | |
639 | ctx->task = task; | |
640 | ||
641 | /* Reuse ptrace permission checks for now. */ | |
642 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) { | |
643 | put_context(ctx); | |
644 | return ERR_PTR(-EACCES); | |
645 | } | |
646 | ||
647 | return ctx; | |
648 | } | |
649 | ||
650 | /* | |
651 | * Called when the last reference to the file is gone. | |
652 | */ | |
653 | static int perf_release(struct inode *inode, struct file *file) | |
654 | { | |
655 | struct perf_counter *counter = file->private_data; | |
656 | struct perf_counter_context *ctx = counter->ctx; | |
657 | ||
658 | file->private_data = NULL; | |
659 | ||
660 | mutex_lock(&counter->mutex); | |
661 | ||
04289bb9 | 662 | perf_counter_remove_from_context(counter); |
0793a61d TG |
663 | put_context(ctx); |
664 | ||
665 | mutex_unlock(&counter->mutex); | |
666 | ||
667 | kfree(counter); | |
668 | ||
669 | return 0; | |
670 | } | |
671 | ||
672 | /* | |
673 | * Read the performance counter - simple non blocking version for now | |
674 | */ | |
675 | static ssize_t | |
676 | perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) | |
677 | { | |
678 | u64 cntval; | |
679 | ||
680 | if (count != sizeof(cntval)) | |
681 | return -EINVAL; | |
682 | ||
683 | mutex_lock(&counter->mutex); | |
04289bb9 | 684 | cntval = perf_counter_read(counter); |
0793a61d TG |
685 | mutex_unlock(&counter->mutex); |
686 | ||
687 | return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval); | |
688 | } | |
689 | ||
690 | static ssize_t | |
691 | perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count) | |
692 | { | |
693 | if (!usrdata->len) | |
694 | return 0; | |
695 | ||
696 | count = min(count, (size_t)usrdata->len); | |
697 | if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count)) | |
698 | return -EFAULT; | |
699 | ||
700 | /* Adjust the counters */ | |
701 | usrdata->len -= count; | |
702 | if (!usrdata->len) | |
703 | usrdata->rd_idx = 0; | |
704 | else | |
705 | usrdata->rd_idx += count; | |
706 | ||
707 | return count; | |
708 | } | |
709 | ||
710 | static ssize_t | |
711 | perf_read_irq_data(struct perf_counter *counter, | |
712 | char __user *buf, | |
713 | size_t count, | |
714 | int nonblocking) | |
715 | { | |
716 | struct perf_data *irqdata, *usrdata; | |
717 | DECLARE_WAITQUEUE(wait, current); | |
718 | ssize_t res; | |
719 | ||
720 | irqdata = counter->irqdata; | |
721 | usrdata = counter->usrdata; | |
722 | ||
723 | if (usrdata->len + irqdata->len >= count) | |
724 | goto read_pending; | |
725 | ||
726 | if (nonblocking) | |
727 | return -EAGAIN; | |
728 | ||
729 | spin_lock_irq(&counter->waitq.lock); | |
730 | __add_wait_queue(&counter->waitq, &wait); | |
731 | for (;;) { | |
732 | set_current_state(TASK_INTERRUPTIBLE); | |
733 | if (usrdata->len + irqdata->len >= count) | |
734 | break; | |
735 | ||
736 | if (signal_pending(current)) | |
737 | break; | |
738 | ||
739 | spin_unlock_irq(&counter->waitq.lock); | |
740 | schedule(); | |
741 | spin_lock_irq(&counter->waitq.lock); | |
742 | } | |
743 | __remove_wait_queue(&counter->waitq, &wait); | |
744 | __set_current_state(TASK_RUNNING); | |
745 | spin_unlock_irq(&counter->waitq.lock); | |
746 | ||
747 | if (usrdata->len + irqdata->len < count) | |
748 | return -ERESTARTSYS; | |
749 | read_pending: | |
750 | mutex_lock(&counter->mutex); | |
751 | ||
752 | /* Drain pending data first: */ | |
753 | res = perf_copy_usrdata(usrdata, buf, count); | |
754 | if (res < 0 || res == count) | |
755 | goto out; | |
756 | ||
757 | /* Switch irq buffer: */ | |
758 | usrdata = perf_switch_irq_data(counter); | |
759 | if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) { | |
760 | if (!res) | |
761 | res = -EFAULT; | |
762 | } else { | |
763 | res = count; | |
764 | } | |
765 | out: | |
766 | mutex_unlock(&counter->mutex); | |
767 | ||
768 | return res; | |
769 | } | |
770 | ||
771 | static ssize_t | |
772 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
773 | { | |
774 | struct perf_counter *counter = file->private_data; | |
775 | ||
9f66a381 | 776 | switch (counter->hw_event.record_type) { |
0793a61d TG |
777 | case PERF_RECORD_SIMPLE: |
778 | return perf_read_hw(counter, buf, count); | |
779 | ||
780 | case PERF_RECORD_IRQ: | |
781 | case PERF_RECORD_GROUP: | |
782 | return perf_read_irq_data(counter, buf, count, | |
783 | file->f_flags & O_NONBLOCK); | |
784 | } | |
785 | return -EINVAL; | |
786 | } | |
787 | ||
788 | static unsigned int perf_poll(struct file *file, poll_table *wait) | |
789 | { | |
790 | struct perf_counter *counter = file->private_data; | |
791 | unsigned int events = 0; | |
792 | unsigned long flags; | |
793 | ||
794 | poll_wait(file, &counter->waitq, wait); | |
795 | ||
796 | spin_lock_irqsave(&counter->waitq.lock, flags); | |
797 | if (counter->usrdata->len || counter->irqdata->len) | |
798 | events |= POLLIN; | |
799 | spin_unlock_irqrestore(&counter->waitq.lock, flags); | |
800 | ||
801 | return events; | |
802 | } | |
803 | ||
804 | static const struct file_operations perf_fops = { | |
805 | .release = perf_release, | |
806 | .read = perf_read, | |
807 | .poll = perf_poll, | |
808 | }; | |
809 | ||
810 | /* | |
811 | * Allocate and initialize a counter structure | |
812 | */ | |
813 | static struct perf_counter * | |
04289bb9 IM |
814 | perf_counter_alloc(struct perf_counter_hw_event *hw_event, |
815 | int cpu, | |
816 | struct perf_counter *group_leader) | |
0793a61d | 817 | { |
621a01ea IM |
818 | struct hw_perf_counter_ops *hw_ops; |
819 | struct perf_counter *counter; | |
0793a61d | 820 | |
621a01ea | 821 | counter = kzalloc(sizeof(*counter), GFP_KERNEL); |
0793a61d TG |
822 | if (!counter) |
823 | return NULL; | |
824 | ||
04289bb9 IM |
825 | /* |
826 | * Single counters are their own group leaders, with an | |
827 | * empty sibling list: | |
828 | */ | |
829 | if (!group_leader) | |
830 | group_leader = counter; | |
831 | ||
0793a61d | 832 | mutex_init(&counter->mutex); |
04289bb9 IM |
833 | INIT_LIST_HEAD(&counter->list_entry); |
834 | INIT_LIST_HEAD(&counter->sibling_list); | |
0793a61d TG |
835 | init_waitqueue_head(&counter->waitq); |
836 | ||
9f66a381 IM |
837 | counter->irqdata = &counter->data[0]; |
838 | counter->usrdata = &counter->data[1]; | |
839 | counter->cpu = cpu; | |
840 | counter->hw_event = *hw_event; | |
841 | counter->wakeup_pending = 0; | |
04289bb9 | 842 | counter->group_leader = group_leader; |
621a01ea IM |
843 | counter->hw_ops = NULL; |
844 | ||
845 | hw_ops = hw_perf_counter_init(counter); | |
846 | if (!hw_ops) { | |
847 | kfree(counter); | |
848 | return NULL; | |
849 | } | |
850 | counter->hw_ops = hw_ops; | |
0793a61d TG |
851 | |
852 | return counter; | |
853 | } | |
854 | ||
855 | /** | |
9f66a381 IM |
856 | * sys_perf_task_open - open a performance counter, associate it to a task/cpu |
857 | * | |
858 | * @hw_event_uptr: event type attributes for monitoring/sampling | |
0793a61d | 859 | * @pid: target pid |
9f66a381 IM |
860 | * @cpu: target cpu |
861 | * @group_fd: group leader counter fd | |
0793a61d | 862 | */ |
9f66a381 IM |
863 | asmlinkage int sys_perf_counter_open( |
864 | ||
865 | struct perf_counter_hw_event *hw_event_uptr __user, | |
866 | pid_t pid, | |
867 | int cpu, | |
868 | int group_fd) | |
869 | ||
0793a61d | 870 | { |
04289bb9 | 871 | struct perf_counter *counter, *group_leader; |
9f66a381 | 872 | struct perf_counter_hw_event hw_event; |
04289bb9 IM |
873 | struct perf_counter_context *ctx; |
874 | struct file *group_file = NULL; | |
875 | int fput_needed = 0; | |
0793a61d TG |
876 | int ret; |
877 | ||
9f66a381 | 878 | if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0) |
eab656ae TG |
879 | return -EFAULT; |
880 | ||
04289bb9 | 881 | /* |
ccff286d IM |
882 | * Get the target context (task or percpu): |
883 | */ | |
884 | ctx = find_get_context(pid, cpu); | |
885 | if (IS_ERR(ctx)) | |
886 | return PTR_ERR(ctx); | |
887 | ||
888 | /* | |
889 | * Look up the group leader (we will attach this counter to it): | |
04289bb9 IM |
890 | */ |
891 | group_leader = NULL; | |
892 | if (group_fd != -1) { | |
893 | ret = -EINVAL; | |
894 | group_file = fget_light(group_fd, &fput_needed); | |
895 | if (!group_file) | |
ccff286d | 896 | goto err_put_context; |
04289bb9 | 897 | if (group_file->f_op != &perf_fops) |
ccff286d | 898 | goto err_put_context; |
04289bb9 IM |
899 | |
900 | group_leader = group_file->private_data; | |
901 | /* | |
ccff286d IM |
902 | * Do not allow a recursive hierarchy (this new sibling |
903 | * becoming part of another group-sibling): | |
904 | */ | |
905 | if (group_leader->group_leader != group_leader) | |
906 | goto err_put_context; | |
907 | /* | |
908 | * Do not allow to attach to a group in a different | |
909 | * task or CPU context: | |
04289bb9 | 910 | */ |
ccff286d IM |
911 | if (group_leader->ctx != ctx) |
912 | goto err_put_context; | |
04289bb9 IM |
913 | } |
914 | ||
0793a61d | 915 | ret = -ENOMEM; |
04289bb9 | 916 | counter = perf_counter_alloc(&hw_event, cpu, group_leader); |
0793a61d TG |
917 | if (!counter) |
918 | goto err_put_context; | |
919 | ||
0793a61d TG |
920 | perf_install_in_context(ctx, counter, cpu); |
921 | ||
922 | ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0); | |
923 | if (ret < 0) | |
924 | goto err_remove_free_put_context; | |
925 | ||
04289bb9 IM |
926 | out_fput: |
927 | fput_light(group_file, fput_needed); | |
928 | ||
0793a61d TG |
929 | return ret; |
930 | ||
931 | err_remove_free_put_context: | |
932 | mutex_lock(&counter->mutex); | |
04289bb9 | 933 | perf_counter_remove_from_context(counter); |
0793a61d | 934 | mutex_unlock(&counter->mutex); |
0793a61d TG |
935 | kfree(counter); |
936 | ||
937 | err_put_context: | |
938 | put_context(ctx); | |
939 | ||
04289bb9 | 940 | goto out_fput; |
0793a61d TG |
941 | } |
942 | ||
04289bb9 | 943 | static void __cpuinit perf_counter_init_cpu(int cpu) |
0793a61d | 944 | { |
04289bb9 | 945 | struct perf_cpu_context *cpuctx; |
0793a61d | 946 | |
04289bb9 IM |
947 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
948 | __perf_counter_init_context(&cpuctx->ctx, NULL); | |
0793a61d TG |
949 | |
950 | mutex_lock(&perf_resource_mutex); | |
04289bb9 | 951 | cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu; |
0793a61d | 952 | mutex_unlock(&perf_resource_mutex); |
04289bb9 | 953 | |
0793a61d TG |
954 | hw_perf_counter_setup(); |
955 | } | |
956 | ||
957 | #ifdef CONFIG_HOTPLUG_CPU | |
04289bb9 | 958 | static void __perf_counter_exit_cpu(void *info) |
0793a61d TG |
959 | { |
960 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); | |
961 | struct perf_counter_context *ctx = &cpuctx->ctx; | |
962 | struct perf_counter *counter, *tmp; | |
963 | ||
04289bb9 IM |
964 | list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) |
965 | __perf_counter_remove_from_context(counter); | |
0793a61d TG |
966 | |
967 | } | |
04289bb9 | 968 | static void perf_counter_exit_cpu(int cpu) |
0793a61d | 969 | { |
04289bb9 | 970 | smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1); |
0793a61d TG |
971 | } |
972 | #else | |
04289bb9 | 973 | static inline void perf_counter_exit_cpu(int cpu) { } |
0793a61d TG |
974 | #endif |
975 | ||
976 | static int __cpuinit | |
977 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) | |
978 | { | |
979 | unsigned int cpu = (long)hcpu; | |
980 | ||
981 | switch (action) { | |
982 | ||
983 | case CPU_UP_PREPARE: | |
984 | case CPU_UP_PREPARE_FROZEN: | |
04289bb9 | 985 | perf_counter_init_cpu(cpu); |
0793a61d TG |
986 | break; |
987 | ||
988 | case CPU_DOWN_PREPARE: | |
989 | case CPU_DOWN_PREPARE_FROZEN: | |
04289bb9 | 990 | perf_counter_exit_cpu(cpu); |
0793a61d TG |
991 | break; |
992 | ||
993 | default: | |
994 | break; | |
995 | } | |
996 | ||
997 | return NOTIFY_OK; | |
998 | } | |
999 | ||
1000 | static struct notifier_block __cpuinitdata perf_cpu_nb = { | |
1001 | .notifier_call = perf_cpu_notify, | |
1002 | }; | |
1003 | ||
1004 | static int __init perf_counter_init(void) | |
1005 | { | |
1006 | perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE, | |
1007 | (void *)(long)smp_processor_id()); | |
1008 | register_cpu_notifier(&perf_cpu_nb); | |
1009 | ||
1010 | return 0; | |
1011 | } | |
1012 | early_initcall(perf_counter_init); | |
1013 | ||
1014 | static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf) | |
1015 | { | |
1016 | return sprintf(buf, "%d\n", perf_reserved_percpu); | |
1017 | } | |
1018 | ||
1019 | static ssize_t | |
1020 | perf_set_reserve_percpu(struct sysdev_class *class, | |
1021 | const char *buf, | |
1022 | size_t count) | |
1023 | { | |
1024 | struct perf_cpu_context *cpuctx; | |
1025 | unsigned long val; | |
1026 | int err, cpu, mpt; | |
1027 | ||
1028 | err = strict_strtoul(buf, 10, &val); | |
1029 | if (err) | |
1030 | return err; | |
1031 | if (val > perf_max_counters) | |
1032 | return -EINVAL; | |
1033 | ||
1034 | mutex_lock(&perf_resource_mutex); | |
1035 | perf_reserved_percpu = val; | |
1036 | for_each_online_cpu(cpu) { | |
1037 | cpuctx = &per_cpu(perf_cpu_context, cpu); | |
1038 | spin_lock_irq(&cpuctx->ctx.lock); | |
1039 | mpt = min(perf_max_counters - cpuctx->ctx.nr_counters, | |
1040 | perf_max_counters - perf_reserved_percpu); | |
1041 | cpuctx->max_pertask = mpt; | |
1042 | spin_unlock_irq(&cpuctx->ctx.lock); | |
1043 | } | |
1044 | mutex_unlock(&perf_resource_mutex); | |
1045 | ||
1046 | return count; | |
1047 | } | |
1048 | ||
1049 | static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf) | |
1050 | { | |
1051 | return sprintf(buf, "%d\n", perf_overcommit); | |
1052 | } | |
1053 | ||
1054 | static ssize_t | |
1055 | perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count) | |
1056 | { | |
1057 | unsigned long val; | |
1058 | int err; | |
1059 | ||
1060 | err = strict_strtoul(buf, 10, &val); | |
1061 | if (err) | |
1062 | return err; | |
1063 | if (val > 1) | |
1064 | return -EINVAL; | |
1065 | ||
1066 | mutex_lock(&perf_resource_mutex); | |
1067 | perf_overcommit = val; | |
1068 | mutex_unlock(&perf_resource_mutex); | |
1069 | ||
1070 | return count; | |
1071 | } | |
1072 | ||
1073 | static SYSDEV_CLASS_ATTR( | |
1074 | reserve_percpu, | |
1075 | 0644, | |
1076 | perf_show_reserve_percpu, | |
1077 | perf_set_reserve_percpu | |
1078 | ); | |
1079 | ||
1080 | static SYSDEV_CLASS_ATTR( | |
1081 | overcommit, | |
1082 | 0644, | |
1083 | perf_show_overcommit, | |
1084 | perf_set_overcommit | |
1085 | ); | |
1086 | ||
1087 | static struct attribute *perfclass_attrs[] = { | |
1088 | &attr_reserve_percpu.attr, | |
1089 | &attr_overcommit.attr, | |
1090 | NULL | |
1091 | }; | |
1092 | ||
1093 | static struct attribute_group perfclass_attr_group = { | |
1094 | .attrs = perfclass_attrs, | |
1095 | .name = "perf_counters", | |
1096 | }; | |
1097 | ||
1098 | static int __init perf_counter_sysfs_init(void) | |
1099 | { | |
1100 | return sysfs_create_group(&cpu_sysdev_class.kset.kobj, | |
1101 | &perfclass_attr_group); | |
1102 | } | |
1103 | device_initcall(perf_counter_sysfs_init); | |
1104 |