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 | |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 170 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 171 | counter->hw_ops->hw_perf_counter_disable(counter); |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 172 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 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); |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 284 | counter->state = PERF_COUNTER_STATE_ACTIVE; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 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 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 331 | * we need to retry the smp call. |
| 332 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 333 | if (ctx->nr_active && list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 334 | spin_unlock_irq(&ctx->lock); |
| 335 | goto retry; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * The lock prevents that this context is scheduled in so we |
| 340 | * can add the counter safely, if it the call above did not |
| 341 | * succeed. |
| 342 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 343 | if (list_empty(&counter->list_entry)) { |
| 344 | list_add_counter(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 345 | ctx->nr_counters++; |
| 346 | } |
| 347 | spin_unlock_irq(&ctx->lock); |
| 348 | } |
| 349 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 350 | static void |
| 351 | counter_sched_out(struct perf_counter *counter, |
| 352 | struct perf_cpu_context *cpuctx, |
| 353 | struct perf_counter_context *ctx) |
| 354 | { |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 355 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 356 | return; |
| 357 | |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 358 | counter->hw_ops->hw_perf_counter_disable(counter); |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 359 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 360 | counter->oncpu = -1; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 361 | |
| 362 | cpuctx->active_oncpu--; |
| 363 | ctx->nr_active--; |
| 364 | } |
| 365 | |
| 366 | static void |
| 367 | group_sched_out(struct perf_counter *group_counter, |
| 368 | struct perf_cpu_context *cpuctx, |
| 369 | struct perf_counter_context *ctx) |
| 370 | { |
| 371 | struct perf_counter *counter; |
| 372 | |
| 373 | counter_sched_out(group_counter, cpuctx, ctx); |
| 374 | |
| 375 | /* |
| 376 | * Schedule out siblings (if any): |
| 377 | */ |
| 378 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) |
| 379 | counter_sched_out(counter, cpuctx, ctx); |
| 380 | } |
| 381 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 382 | /* |
| 383 | * Called from scheduler to remove the counters of the current task, |
| 384 | * with interrupts disabled. |
| 385 | * |
| 386 | * We stop each counter and update the counter value in counter->count. |
| 387 | * |
| 388 | * This does not protect us against NMI, but hw_perf_counter_disable() |
| 389 | * sets the disabled bit in the control field of counter _before_ |
| 390 | * accessing the counter control register. If a NMI hits, then it will |
| 391 | * not restart the counter. |
| 392 | */ |
| 393 | void perf_counter_task_sched_out(struct task_struct *task, int cpu) |
| 394 | { |
| 395 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 396 | struct perf_counter_context *ctx = &task->perf_counter_ctx; |
| 397 | struct perf_counter *counter; |
| 398 | |
| 399 | if (likely(!cpuctx->task_ctx)) |
| 400 | return; |
| 401 | |
| 402 | spin_lock(&ctx->lock); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 403 | if (ctx->nr_active) { |
| 404 | list_for_each_entry(counter, &ctx->counter_list, list_entry) |
| 405 | group_sched_out(counter, cpuctx, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 406 | } |
| 407 | spin_unlock(&ctx->lock); |
| 408 | cpuctx->task_ctx = NULL; |
| 409 | } |
| 410 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 411 | static void |
| 412 | counter_sched_in(struct perf_counter *counter, |
| 413 | struct perf_cpu_context *cpuctx, |
| 414 | struct perf_counter_context *ctx, |
| 415 | int cpu) |
| 416 | { |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 417 | if (counter->state == PERF_COUNTER_STATE_OFF) |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 418 | return; |
| 419 | |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 420 | counter->hw_ops->hw_perf_counter_enable(counter); |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 421 | counter->state = PERF_COUNTER_STATE_ACTIVE; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 422 | counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */ |
| 423 | |
| 424 | cpuctx->active_oncpu++; |
| 425 | ctx->nr_active++; |
| 426 | } |
| 427 | |
| 428 | static void |
| 429 | group_sched_in(struct perf_counter *group_counter, |
| 430 | struct perf_cpu_context *cpuctx, |
| 431 | struct perf_counter_context *ctx, |
| 432 | int cpu) |
| 433 | { |
| 434 | struct perf_counter *counter; |
| 435 | |
| 436 | counter_sched_in(group_counter, cpuctx, ctx, cpu); |
| 437 | |
| 438 | /* |
| 439 | * Schedule in siblings as one group (if any): |
| 440 | */ |
| 441 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) |
| 442 | counter_sched_in(counter, cpuctx, ctx, cpu); |
| 443 | } |
| 444 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 445 | /* |
| 446 | * Called from scheduler to add the counters of the current task |
| 447 | * with interrupts disabled. |
| 448 | * |
| 449 | * We restore the counter value and then enable it. |
| 450 | * |
| 451 | * This does not protect us against NMI, but hw_perf_counter_enable() |
| 452 | * sets the enabled bit in the control field of counter _before_ |
| 453 | * accessing the counter control register. If a NMI hits, then it will |
| 454 | * keep the counter running. |
| 455 | */ |
| 456 | void perf_counter_task_sched_in(struct task_struct *task, int cpu) |
| 457 | { |
| 458 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 459 | struct perf_counter_context *ctx = &task->perf_counter_ctx; |
| 460 | struct perf_counter *counter; |
| 461 | |
| 462 | if (likely(!ctx->nr_counters)) |
| 463 | return; |
| 464 | |
| 465 | spin_lock(&ctx->lock); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 466 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 467 | if (ctx->nr_active == cpuctx->max_pertask) |
| 468 | break; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 469 | |
| 470 | /* |
| 471 | * Listen to the 'cpu' scheduling filter constraint |
| 472 | * of counters: |
| 473 | */ |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 474 | if (counter->cpu != -1 && counter->cpu != cpu) |
| 475 | continue; |
| 476 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 477 | group_sched_in(counter, cpuctx, ctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 478 | } |
| 479 | spin_unlock(&ctx->lock); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 480 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 481 | cpuctx->task_ctx = ctx; |
| 482 | } |
| 483 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 484 | int perf_counter_task_disable(void) |
| 485 | { |
| 486 | struct task_struct *curr = current; |
| 487 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 488 | struct perf_counter *counter; |
| 489 | u64 perf_flags; |
| 490 | int cpu; |
| 491 | |
| 492 | if (likely(!ctx->nr_counters)) |
| 493 | return 0; |
| 494 | |
| 495 | local_irq_disable(); |
| 496 | cpu = smp_processor_id(); |
| 497 | |
| 498 | perf_counter_task_sched_out(curr, cpu); |
| 499 | |
| 500 | spin_lock(&ctx->lock); |
| 501 | |
| 502 | /* |
| 503 | * Disable all the counters: |
| 504 | */ |
| 505 | perf_flags = hw_perf_save_disable(); |
| 506 | |
| 507 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 508 | WARN_ON_ONCE(counter->state == PERF_COUNTER_STATE_ACTIVE); |
| 509 | counter->state = PERF_COUNTER_STATE_OFF; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 510 | } |
| 511 | hw_perf_restore(perf_flags); |
| 512 | |
| 513 | spin_unlock(&ctx->lock); |
| 514 | |
| 515 | local_irq_enable(); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | int perf_counter_task_enable(void) |
| 521 | { |
| 522 | struct task_struct *curr = current; |
| 523 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 524 | struct perf_counter *counter; |
| 525 | u64 perf_flags; |
| 526 | int cpu; |
| 527 | |
| 528 | if (likely(!ctx->nr_counters)) |
| 529 | return 0; |
| 530 | |
| 531 | local_irq_disable(); |
| 532 | cpu = smp_processor_id(); |
| 533 | |
| 534 | spin_lock(&ctx->lock); |
| 535 | |
| 536 | /* |
| 537 | * Disable all the counters: |
| 538 | */ |
| 539 | perf_flags = hw_perf_save_disable(); |
| 540 | |
| 541 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 542 | if (counter->state != PERF_COUNTER_STATE_OFF) |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 543 | continue; |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 544 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 545 | } |
| 546 | hw_perf_restore(perf_flags); |
| 547 | |
| 548 | spin_unlock(&ctx->lock); |
| 549 | |
| 550 | perf_counter_task_sched_in(curr, cpu); |
| 551 | |
| 552 | local_irq_enable(); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 557 | void perf_counter_task_tick(struct task_struct *curr, int cpu) |
| 558 | { |
| 559 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 560 | struct perf_counter *counter; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 561 | u64 perf_flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 562 | |
| 563 | if (likely(!ctx->nr_counters)) |
| 564 | return; |
| 565 | |
| 566 | perf_counter_task_sched_out(curr, cpu); |
| 567 | |
| 568 | spin_lock(&ctx->lock); |
| 569 | |
| 570 | /* |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 571 | * Rotate the first entry last (works just fine for group counters too): |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 572 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 573 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 574 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
| 575 | list_del(&counter->list_entry); |
| 576 | list_add_tail(&counter->list_entry, &ctx->counter_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 577 | break; |
| 578 | } |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 579 | hw_perf_restore(perf_flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 580 | |
| 581 | spin_unlock(&ctx->lock); |
| 582 | |
| 583 | perf_counter_task_sched_in(curr, cpu); |
| 584 | } |
| 585 | |
| 586 | /* |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 587 | * Initialize the perf_counter context in a task_struct: |
| 588 | */ |
| 589 | static void |
| 590 | __perf_counter_init_context(struct perf_counter_context *ctx, |
| 591 | struct task_struct *task) |
| 592 | { |
| 593 | spin_lock_init(&ctx->lock); |
| 594 | INIT_LIST_HEAD(&ctx->counter_list); |
| 595 | ctx->nr_counters = 0; |
| 596 | ctx->task = task; |
| 597 | } |
| 598 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 599 | * Initialize the perf_counter context in task_struct |
| 600 | */ |
| 601 | void perf_counter_init_task(struct task_struct *task) |
| 602 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 603 | __perf_counter_init_context(&task->perf_counter_ctx, task); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Cross CPU call to read the hardware counter |
| 608 | */ |
| 609 | static void __hw_perf_counter_read(void *info) |
| 610 | { |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 611 | struct perf_counter *counter = info; |
| 612 | |
| 613 | counter->hw_ops->hw_perf_counter_read(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 616 | static u64 perf_counter_read(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 617 | { |
| 618 | /* |
| 619 | * If counter is enabled and currently active on a CPU, update the |
| 620 | * value in the counter structure: |
| 621 | */ |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 622 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 623 | smp_call_function_single(counter->oncpu, |
| 624 | __hw_perf_counter_read, counter, 1); |
| 625 | } |
| 626 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 627 | return perf_counter_read_safe(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Cross CPU call to switch performance data pointers |
| 632 | */ |
| 633 | static void __perf_switch_irq_data(void *info) |
| 634 | { |
| 635 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 636 | struct perf_counter *counter = info; |
| 637 | struct perf_counter_context *ctx = counter->ctx; |
| 638 | struct perf_data *oldirqdata = counter->irqdata; |
| 639 | |
| 640 | /* |
| 641 | * If this is a task context, we need to check whether it is |
| 642 | * the current task context of this cpu. If not it has been |
| 643 | * scheduled out before the smp call arrived. |
| 644 | */ |
| 645 | if (ctx->task) { |
| 646 | if (cpuctx->task_ctx != ctx) |
| 647 | return; |
| 648 | spin_lock(&ctx->lock); |
| 649 | } |
| 650 | |
| 651 | /* Change the pointer NMI safe */ |
| 652 | atomic_long_set((atomic_long_t *)&counter->irqdata, |
| 653 | (unsigned long) counter->usrdata); |
| 654 | counter->usrdata = oldirqdata; |
| 655 | |
| 656 | if (ctx->task) |
| 657 | spin_unlock(&ctx->lock); |
| 658 | } |
| 659 | |
| 660 | static struct perf_data *perf_switch_irq_data(struct perf_counter *counter) |
| 661 | { |
| 662 | struct perf_counter_context *ctx = counter->ctx; |
| 663 | struct perf_data *oldirqdata = counter->irqdata; |
| 664 | struct task_struct *task = ctx->task; |
| 665 | |
| 666 | if (!task) { |
| 667 | smp_call_function_single(counter->cpu, |
| 668 | __perf_switch_irq_data, |
| 669 | counter, 1); |
| 670 | return counter->usrdata; |
| 671 | } |
| 672 | |
| 673 | retry: |
| 674 | spin_lock_irq(&ctx->lock); |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame^] | 675 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 676 | counter->irqdata = counter->usrdata; |
| 677 | counter->usrdata = oldirqdata; |
| 678 | spin_unlock_irq(&ctx->lock); |
| 679 | return oldirqdata; |
| 680 | } |
| 681 | spin_unlock_irq(&ctx->lock); |
| 682 | task_oncpu_function_call(task, __perf_switch_irq_data, counter); |
| 683 | /* Might have failed, because task was scheduled out */ |
| 684 | if (counter->irqdata == oldirqdata) |
| 685 | goto retry; |
| 686 | |
| 687 | return counter->usrdata; |
| 688 | } |
| 689 | |
| 690 | static void put_context(struct perf_counter_context *ctx) |
| 691 | { |
| 692 | if (ctx->task) |
| 693 | put_task_struct(ctx->task); |
| 694 | } |
| 695 | |
| 696 | static struct perf_counter_context *find_get_context(pid_t pid, int cpu) |
| 697 | { |
| 698 | struct perf_cpu_context *cpuctx; |
| 699 | struct perf_counter_context *ctx; |
| 700 | struct task_struct *task; |
| 701 | |
| 702 | /* |
| 703 | * If cpu is not a wildcard then this is a percpu counter: |
| 704 | */ |
| 705 | if (cpu != -1) { |
| 706 | /* Must be root to operate on a CPU counter: */ |
| 707 | if (!capable(CAP_SYS_ADMIN)) |
| 708 | return ERR_PTR(-EACCES); |
| 709 | |
| 710 | if (cpu < 0 || cpu > num_possible_cpus()) |
| 711 | return ERR_PTR(-EINVAL); |
| 712 | |
| 713 | /* |
| 714 | * We could be clever and allow to attach a counter to an |
| 715 | * offline CPU and activate it when the CPU comes up, but |
| 716 | * that's for later. |
| 717 | */ |
| 718 | if (!cpu_isset(cpu, cpu_online_map)) |
| 719 | return ERR_PTR(-ENODEV); |
| 720 | |
| 721 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 722 | ctx = &cpuctx->ctx; |
| 723 | |
| 724 | WARN_ON_ONCE(ctx->task); |
| 725 | return ctx; |
| 726 | } |
| 727 | |
| 728 | rcu_read_lock(); |
| 729 | if (!pid) |
| 730 | task = current; |
| 731 | else |
| 732 | task = find_task_by_vpid(pid); |
| 733 | if (task) |
| 734 | get_task_struct(task); |
| 735 | rcu_read_unlock(); |
| 736 | |
| 737 | if (!task) |
| 738 | return ERR_PTR(-ESRCH); |
| 739 | |
| 740 | ctx = &task->perf_counter_ctx; |
| 741 | ctx->task = task; |
| 742 | |
| 743 | /* Reuse ptrace permission checks for now. */ |
| 744 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) { |
| 745 | put_context(ctx); |
| 746 | return ERR_PTR(-EACCES); |
| 747 | } |
| 748 | |
| 749 | return ctx; |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Called when the last reference to the file is gone. |
| 754 | */ |
| 755 | static int perf_release(struct inode *inode, struct file *file) |
| 756 | { |
| 757 | struct perf_counter *counter = file->private_data; |
| 758 | struct perf_counter_context *ctx = counter->ctx; |
| 759 | |
| 760 | file->private_data = NULL; |
| 761 | |
| 762 | mutex_lock(&counter->mutex); |
| 763 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 764 | perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 765 | put_context(ctx); |
| 766 | |
| 767 | mutex_unlock(&counter->mutex); |
| 768 | |
| 769 | kfree(counter); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Read the performance counter - simple non blocking version for now |
| 776 | */ |
| 777 | static ssize_t |
| 778 | perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) |
| 779 | { |
| 780 | u64 cntval; |
| 781 | |
| 782 | if (count != sizeof(cntval)) |
| 783 | return -EINVAL; |
| 784 | |
| 785 | mutex_lock(&counter->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 786 | cntval = perf_counter_read(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 787 | mutex_unlock(&counter->mutex); |
| 788 | |
| 789 | return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval); |
| 790 | } |
| 791 | |
| 792 | static ssize_t |
| 793 | perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count) |
| 794 | { |
| 795 | if (!usrdata->len) |
| 796 | return 0; |
| 797 | |
| 798 | count = min(count, (size_t)usrdata->len); |
| 799 | if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count)) |
| 800 | return -EFAULT; |
| 801 | |
| 802 | /* Adjust the counters */ |
| 803 | usrdata->len -= count; |
| 804 | if (!usrdata->len) |
| 805 | usrdata->rd_idx = 0; |
| 806 | else |
| 807 | usrdata->rd_idx += count; |
| 808 | |
| 809 | return count; |
| 810 | } |
| 811 | |
| 812 | static ssize_t |
| 813 | perf_read_irq_data(struct perf_counter *counter, |
| 814 | char __user *buf, |
| 815 | size_t count, |
| 816 | int nonblocking) |
| 817 | { |
| 818 | struct perf_data *irqdata, *usrdata; |
| 819 | DECLARE_WAITQUEUE(wait, current); |
| 820 | ssize_t res; |
| 821 | |
| 822 | irqdata = counter->irqdata; |
| 823 | usrdata = counter->usrdata; |
| 824 | |
| 825 | if (usrdata->len + irqdata->len >= count) |
| 826 | goto read_pending; |
| 827 | |
| 828 | if (nonblocking) |
| 829 | return -EAGAIN; |
| 830 | |
| 831 | spin_lock_irq(&counter->waitq.lock); |
| 832 | __add_wait_queue(&counter->waitq, &wait); |
| 833 | for (;;) { |
| 834 | set_current_state(TASK_INTERRUPTIBLE); |
| 835 | if (usrdata->len + irqdata->len >= count) |
| 836 | break; |
| 837 | |
| 838 | if (signal_pending(current)) |
| 839 | break; |
| 840 | |
| 841 | spin_unlock_irq(&counter->waitq.lock); |
| 842 | schedule(); |
| 843 | spin_lock_irq(&counter->waitq.lock); |
| 844 | } |
| 845 | __remove_wait_queue(&counter->waitq, &wait); |
| 846 | __set_current_state(TASK_RUNNING); |
| 847 | spin_unlock_irq(&counter->waitq.lock); |
| 848 | |
| 849 | if (usrdata->len + irqdata->len < count) |
| 850 | return -ERESTARTSYS; |
| 851 | read_pending: |
| 852 | mutex_lock(&counter->mutex); |
| 853 | |
| 854 | /* Drain pending data first: */ |
| 855 | res = perf_copy_usrdata(usrdata, buf, count); |
| 856 | if (res < 0 || res == count) |
| 857 | goto out; |
| 858 | |
| 859 | /* Switch irq buffer: */ |
| 860 | usrdata = perf_switch_irq_data(counter); |
| 861 | if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) { |
| 862 | if (!res) |
| 863 | res = -EFAULT; |
| 864 | } else { |
| 865 | res = count; |
| 866 | } |
| 867 | out: |
| 868 | mutex_unlock(&counter->mutex); |
| 869 | |
| 870 | return res; |
| 871 | } |
| 872 | |
| 873 | static ssize_t |
| 874 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 875 | { |
| 876 | struct perf_counter *counter = file->private_data; |
| 877 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 878 | switch (counter->hw_event.record_type) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 879 | case PERF_RECORD_SIMPLE: |
| 880 | return perf_read_hw(counter, buf, count); |
| 881 | |
| 882 | case PERF_RECORD_IRQ: |
| 883 | case PERF_RECORD_GROUP: |
| 884 | return perf_read_irq_data(counter, buf, count, |
| 885 | file->f_flags & O_NONBLOCK); |
| 886 | } |
| 887 | return -EINVAL; |
| 888 | } |
| 889 | |
| 890 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 891 | { |
| 892 | struct perf_counter *counter = file->private_data; |
| 893 | unsigned int events = 0; |
| 894 | unsigned long flags; |
| 895 | |
| 896 | poll_wait(file, &counter->waitq, wait); |
| 897 | |
| 898 | spin_lock_irqsave(&counter->waitq.lock, flags); |
| 899 | if (counter->usrdata->len || counter->irqdata->len) |
| 900 | events |= POLLIN; |
| 901 | spin_unlock_irqrestore(&counter->waitq.lock, flags); |
| 902 | |
| 903 | return events; |
| 904 | } |
| 905 | |
| 906 | static const struct file_operations perf_fops = { |
| 907 | .release = perf_release, |
| 908 | .read = perf_read, |
| 909 | .poll = perf_poll, |
| 910 | }; |
| 911 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 912 | static void cpu_clock_perf_counter_enable(struct perf_counter *counter) |
| 913 | { |
| 914 | } |
| 915 | |
| 916 | static void cpu_clock_perf_counter_disable(struct perf_counter *counter) |
| 917 | { |
| 918 | } |
| 919 | |
| 920 | static void cpu_clock_perf_counter_read(struct perf_counter *counter) |
| 921 | { |
| 922 | int cpu = raw_smp_processor_id(); |
| 923 | |
| 924 | atomic64_counter_set(counter, cpu_clock(cpu)); |
| 925 | } |
| 926 | |
| 927 | static const struct hw_perf_counter_ops perf_ops_cpu_clock = { |
| 928 | .hw_perf_counter_enable = cpu_clock_perf_counter_enable, |
| 929 | .hw_perf_counter_disable = cpu_clock_perf_counter_disable, |
| 930 | .hw_perf_counter_read = cpu_clock_perf_counter_read, |
| 931 | }; |
| 932 | |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 933 | static void task_clock_perf_counter_enable(struct perf_counter *counter) |
| 934 | { |
| 935 | } |
| 936 | |
| 937 | static void task_clock_perf_counter_disable(struct perf_counter *counter) |
| 938 | { |
| 939 | } |
| 940 | |
| 941 | static void task_clock_perf_counter_read(struct perf_counter *counter) |
| 942 | { |
| 943 | atomic64_counter_set(counter, current->se.sum_exec_runtime); |
| 944 | } |
| 945 | |
| 946 | static const struct hw_perf_counter_ops perf_ops_task_clock = { |
| 947 | .hw_perf_counter_enable = task_clock_perf_counter_enable, |
| 948 | .hw_perf_counter_disable = task_clock_perf_counter_disable, |
| 949 | .hw_perf_counter_read = task_clock_perf_counter_read, |
| 950 | }; |
| 951 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 952 | static const struct hw_perf_counter_ops * |
| 953 | sw_perf_counter_init(struct perf_counter *counter) |
| 954 | { |
| 955 | const struct hw_perf_counter_ops *hw_ops = NULL; |
| 956 | |
| 957 | switch (counter->hw_event.type) { |
| 958 | case PERF_COUNT_CPU_CLOCK: |
| 959 | hw_ops = &perf_ops_cpu_clock; |
| 960 | break; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 961 | case PERF_COUNT_TASK_CLOCK: |
| 962 | hw_ops = &perf_ops_task_clock; |
| 963 | break; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 964 | default: |
| 965 | break; |
| 966 | } |
| 967 | return hw_ops; |
| 968 | } |
| 969 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 970 | /* |
| 971 | * Allocate and initialize a counter structure |
| 972 | */ |
| 973 | static struct perf_counter * |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 974 | perf_counter_alloc(struct perf_counter_hw_event *hw_event, |
| 975 | int cpu, |
| 976 | struct perf_counter *group_leader) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 977 | { |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 978 | const struct hw_perf_counter_ops *hw_ops; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 979 | struct perf_counter *counter; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 980 | |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 981 | counter = kzalloc(sizeof(*counter), GFP_KERNEL); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 982 | if (!counter) |
| 983 | return NULL; |
| 984 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 985 | /* |
| 986 | * Single counters are their own group leaders, with an |
| 987 | * empty sibling list: |
| 988 | */ |
| 989 | if (!group_leader) |
| 990 | group_leader = counter; |
| 991 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 992 | mutex_init(&counter->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 993 | INIT_LIST_HEAD(&counter->list_entry); |
| 994 | INIT_LIST_HEAD(&counter->sibling_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 995 | init_waitqueue_head(&counter->waitq); |
| 996 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 997 | counter->irqdata = &counter->data[0]; |
| 998 | counter->usrdata = &counter->data[1]; |
| 999 | counter->cpu = cpu; |
| 1000 | counter->hw_event = *hw_event; |
| 1001 | counter->wakeup_pending = 0; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1002 | counter->group_leader = group_leader; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1003 | counter->hw_ops = NULL; |
| 1004 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1005 | hw_ops = NULL; |
| 1006 | if (!hw_event->raw && hw_event->type < 0) |
| 1007 | hw_ops = sw_perf_counter_init(counter); |
| 1008 | if (!hw_ops) { |
| 1009 | hw_ops = hw_perf_counter_init(counter); |
| 1010 | } |
| 1011 | |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1012 | if (!hw_ops) { |
| 1013 | kfree(counter); |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | counter->hw_ops = hw_ops; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1017 | |
| 1018 | return counter; |
| 1019 | } |
| 1020 | |
| 1021 | /** |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 1022 | * sys_perf_task_open - open a performance counter, associate it to a task/cpu |
| 1023 | * |
| 1024 | * @hw_event_uptr: event type attributes for monitoring/sampling |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1025 | * @pid: target pid |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 1026 | * @cpu: target cpu |
| 1027 | * @group_fd: group leader counter fd |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1028 | */ |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1029 | asmlinkage int |
| 1030 | sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user, |
| 1031 | pid_t pid, int cpu, int group_fd) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1032 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1033 | struct perf_counter *counter, *group_leader; |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 1034 | struct perf_counter_hw_event hw_event; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1035 | struct perf_counter_context *ctx; |
| 1036 | struct file *group_file = NULL; |
| 1037 | int fput_needed = 0; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1038 | int ret; |
| 1039 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 1040 | 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] | 1041 | return -EFAULT; |
| 1042 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1043 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1044 | * Get the target context (task or percpu): |
| 1045 | */ |
| 1046 | ctx = find_get_context(pid, cpu); |
| 1047 | if (IS_ERR(ctx)) |
| 1048 | return PTR_ERR(ctx); |
| 1049 | |
| 1050 | /* |
| 1051 | * Look up the group leader (we will attach this counter to it): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1052 | */ |
| 1053 | group_leader = NULL; |
| 1054 | if (group_fd != -1) { |
| 1055 | ret = -EINVAL; |
| 1056 | group_file = fget_light(group_fd, &fput_needed); |
| 1057 | if (!group_file) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1058 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1059 | if (group_file->f_op != &perf_fops) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1060 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1061 | |
| 1062 | group_leader = group_file->private_data; |
| 1063 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1064 | * Do not allow a recursive hierarchy (this new sibling |
| 1065 | * becoming part of another group-sibling): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1066 | */ |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1067 | if (group_leader->group_leader != group_leader) |
| 1068 | goto err_put_context; |
| 1069 | /* |
| 1070 | * Do not allow to attach to a group in a different |
| 1071 | * task or CPU context: |
| 1072 | */ |
| 1073 | if (group_leader->ctx != ctx) |
| 1074 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1077 | ret = -EINVAL; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1078 | counter = perf_counter_alloc(&hw_event, cpu, group_leader); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1079 | if (!counter) |
| 1080 | goto err_put_context; |
| 1081 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1082 | perf_install_in_context(ctx, counter, cpu); |
| 1083 | |
| 1084 | ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0); |
| 1085 | if (ret < 0) |
| 1086 | goto err_remove_free_put_context; |
| 1087 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1088 | out_fput: |
| 1089 | fput_light(group_file, fput_needed); |
| 1090 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1091 | return ret; |
| 1092 | |
| 1093 | err_remove_free_put_context: |
| 1094 | mutex_lock(&counter->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1095 | perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1096 | mutex_unlock(&counter->mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1097 | kfree(counter); |
| 1098 | |
| 1099 | err_put_context: |
| 1100 | put_context(ctx); |
| 1101 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1102 | goto out_fput; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1103 | } |
| 1104 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1105 | static void __cpuinit perf_counter_init_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1106 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1107 | struct perf_cpu_context *cpuctx; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1108 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1109 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1110 | __perf_counter_init_context(&cpuctx->ctx, NULL); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1111 | |
| 1112 | mutex_lock(&perf_resource_mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1113 | cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1114 | mutex_unlock(&perf_resource_mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1115 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1116 | hw_perf_counter_setup(); |
| 1117 | } |
| 1118 | |
| 1119 | #ifdef CONFIG_HOTPLUG_CPU |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1120 | static void __perf_counter_exit_cpu(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1121 | { |
| 1122 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 1123 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 1124 | struct perf_counter *counter, *tmp; |
| 1125 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1126 | list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) |
| 1127 | __perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1128 | |
| 1129 | } |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1130 | static void perf_counter_exit_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1131 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1132 | smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1133 | } |
| 1134 | #else |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1135 | static inline void perf_counter_exit_cpu(int cpu) { } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1136 | #endif |
| 1137 | |
| 1138 | static int __cpuinit |
| 1139 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 1140 | { |
| 1141 | unsigned int cpu = (long)hcpu; |
| 1142 | |
| 1143 | switch (action) { |
| 1144 | |
| 1145 | case CPU_UP_PREPARE: |
| 1146 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1147 | perf_counter_init_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1148 | break; |
| 1149 | |
| 1150 | case CPU_DOWN_PREPARE: |
| 1151 | case CPU_DOWN_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1152 | perf_counter_exit_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1153 | break; |
| 1154 | |
| 1155 | default: |
| 1156 | break; |
| 1157 | } |
| 1158 | |
| 1159 | return NOTIFY_OK; |
| 1160 | } |
| 1161 | |
| 1162 | static struct notifier_block __cpuinitdata perf_cpu_nb = { |
| 1163 | .notifier_call = perf_cpu_notify, |
| 1164 | }; |
| 1165 | |
| 1166 | static int __init perf_counter_init(void) |
| 1167 | { |
| 1168 | perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE, |
| 1169 | (void *)(long)smp_processor_id()); |
| 1170 | register_cpu_notifier(&perf_cpu_nb); |
| 1171 | |
| 1172 | return 0; |
| 1173 | } |
| 1174 | early_initcall(perf_counter_init); |
| 1175 | |
| 1176 | static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf) |
| 1177 | { |
| 1178 | return sprintf(buf, "%d\n", perf_reserved_percpu); |
| 1179 | } |
| 1180 | |
| 1181 | static ssize_t |
| 1182 | perf_set_reserve_percpu(struct sysdev_class *class, |
| 1183 | const char *buf, |
| 1184 | size_t count) |
| 1185 | { |
| 1186 | struct perf_cpu_context *cpuctx; |
| 1187 | unsigned long val; |
| 1188 | int err, cpu, mpt; |
| 1189 | |
| 1190 | err = strict_strtoul(buf, 10, &val); |
| 1191 | if (err) |
| 1192 | return err; |
| 1193 | if (val > perf_max_counters) |
| 1194 | return -EINVAL; |
| 1195 | |
| 1196 | mutex_lock(&perf_resource_mutex); |
| 1197 | perf_reserved_percpu = val; |
| 1198 | for_each_online_cpu(cpu) { |
| 1199 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1200 | spin_lock_irq(&cpuctx->ctx.lock); |
| 1201 | mpt = min(perf_max_counters - cpuctx->ctx.nr_counters, |
| 1202 | perf_max_counters - perf_reserved_percpu); |
| 1203 | cpuctx->max_pertask = mpt; |
| 1204 | spin_unlock_irq(&cpuctx->ctx.lock); |
| 1205 | } |
| 1206 | mutex_unlock(&perf_resource_mutex); |
| 1207 | |
| 1208 | return count; |
| 1209 | } |
| 1210 | |
| 1211 | static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf) |
| 1212 | { |
| 1213 | return sprintf(buf, "%d\n", perf_overcommit); |
| 1214 | } |
| 1215 | |
| 1216 | static ssize_t |
| 1217 | perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count) |
| 1218 | { |
| 1219 | unsigned long val; |
| 1220 | int err; |
| 1221 | |
| 1222 | err = strict_strtoul(buf, 10, &val); |
| 1223 | if (err) |
| 1224 | return err; |
| 1225 | if (val > 1) |
| 1226 | return -EINVAL; |
| 1227 | |
| 1228 | mutex_lock(&perf_resource_mutex); |
| 1229 | perf_overcommit = val; |
| 1230 | mutex_unlock(&perf_resource_mutex); |
| 1231 | |
| 1232 | return count; |
| 1233 | } |
| 1234 | |
| 1235 | static SYSDEV_CLASS_ATTR( |
| 1236 | reserve_percpu, |
| 1237 | 0644, |
| 1238 | perf_show_reserve_percpu, |
| 1239 | perf_set_reserve_percpu |
| 1240 | ); |
| 1241 | |
| 1242 | static SYSDEV_CLASS_ATTR( |
| 1243 | overcommit, |
| 1244 | 0644, |
| 1245 | perf_show_overcommit, |
| 1246 | perf_set_overcommit |
| 1247 | ); |
| 1248 | |
| 1249 | static struct attribute *perfclass_attrs[] = { |
| 1250 | &attr_reserve_percpu.attr, |
| 1251 | &attr_overcommit.attr, |
| 1252 | NULL |
| 1253 | }; |
| 1254 | |
| 1255 | static struct attribute_group perfclass_attr_group = { |
| 1256 | .attrs = perfclass_attrs, |
| 1257 | .name = "perf_counters", |
| 1258 | }; |
| 1259 | |
| 1260 | static int __init perf_counter_sysfs_init(void) |
| 1261 | { |
| 1262 | return sysfs_create_group(&cpu_sysdev_class.kset.kobj, |
| 1263 | &perfclass_attr_group); |
| 1264 | } |
| 1265 | device_initcall(perf_counter_sysfs_init); |
| 1266 | |