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