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