Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 2 | * Performance events core code: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 5 | * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar |
| 6 | * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
| 8 | * |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 9 | * For licensing details see kernel-base/COPYING |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/cpu.h> |
| 15 | #include <linux/smp.h> |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 16 | #include <linux/idr.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 17 | #include <linux/file.h> |
| 18 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 20 | #include <linux/hash.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 21 | #include <linux/sysfs.h> |
| 22 | #include <linux/dcache.h> |
| 23 | #include <linux/percpu.h> |
| 24 | #include <linux/ptrace.h> |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 25 | #include <linux/reboot.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 26 | #include <linux/vmstat.h> |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 27 | #include <linux/device.h> |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 28 | #include <linux/vmalloc.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 29 | #include <linux/hardirq.h> |
| 30 | #include <linux/rculist.h> |
| 31 | #include <linux/uaccess.h> |
| 32 | #include <linux/syscalls.h> |
| 33 | #include <linux/anon_inodes.h> |
| 34 | #include <linux/kernel_stat.h> |
| 35 | #include <linux/perf_event.h> |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 36 | #include <linux/ftrace_event.h> |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 37 | #include <linux/hw_breakpoint.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 38 | |
| 39 | #include <asm/irq_regs.h> |
| 40 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 41 | struct remote_function_call { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 42 | struct task_struct *p; |
| 43 | int (*func)(void *info); |
| 44 | void *info; |
| 45 | int ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static void remote_function(void *data) |
| 49 | { |
| 50 | struct remote_function_call *tfc = data; |
| 51 | struct task_struct *p = tfc->p; |
| 52 | |
| 53 | if (p) { |
| 54 | tfc->ret = -EAGAIN; |
| 55 | if (task_cpu(p) != smp_processor_id() || !task_curr(p)) |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | tfc->ret = tfc->func(tfc->info); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * task_function_call - call a function on the cpu on which a task runs |
| 64 | * @p: the task to evaluate |
| 65 | * @func: the function to be called |
| 66 | * @info: the function call argument |
| 67 | * |
| 68 | * Calls the function @func when the task is currently running. This might |
| 69 | * be on the current CPU, which just calls the function directly |
| 70 | * |
| 71 | * returns: @func return value, or |
| 72 | * -ESRCH - when the process isn't running |
| 73 | * -EAGAIN - when the process moved away |
| 74 | */ |
| 75 | static int |
| 76 | task_function_call(struct task_struct *p, int (*func) (void *info), void *info) |
| 77 | { |
| 78 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 79 | .p = p, |
| 80 | .func = func, |
| 81 | .info = info, |
| 82 | .ret = -ESRCH, /* No such (running) process */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | if (task_curr(p)) |
| 86 | smp_call_function_single(task_cpu(p), remote_function, &data, 1); |
| 87 | |
| 88 | return data.ret; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * cpu_function_call - call a function on the cpu |
| 93 | * @func: the function to be called |
| 94 | * @info: the function call argument |
| 95 | * |
| 96 | * Calls the function @func on the remote cpu. |
| 97 | * |
| 98 | * returns: @func return value or -ENXIO when the cpu is offline |
| 99 | */ |
| 100 | static int cpu_function_call(int cpu, int (*func) (void *info), void *info) |
| 101 | { |
| 102 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 103 | .p = NULL, |
| 104 | .func = func, |
| 105 | .info = info, |
| 106 | .ret = -ENXIO, /* No such CPU */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | smp_call_function_single(cpu, remote_function, &data, 1); |
| 110 | |
| 111 | return data.ret; |
| 112 | } |
| 113 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 114 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
| 115 | PERF_FLAG_FD_OUTPUT |\ |
| 116 | PERF_FLAG_PID_CGROUP) |
| 117 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 118 | enum event_type_t { |
| 119 | EVENT_FLEXIBLE = 0x1, |
| 120 | EVENT_PINNED = 0x2, |
| 121 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
| 122 | }; |
| 123 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 124 | /* |
| 125 | * perf_sched_events : >0 events exist |
| 126 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu |
| 127 | */ |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 128 | struct jump_label_key perf_sched_events __read_mostly; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 129 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
| 130 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 131 | static atomic_t nr_mmap_events __read_mostly; |
| 132 | static atomic_t nr_comm_events __read_mostly; |
| 133 | static atomic_t nr_task_events __read_mostly; |
| 134 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 135 | static LIST_HEAD(pmus); |
| 136 | static DEFINE_MUTEX(pmus_lock); |
| 137 | static struct srcu_struct pmus_srcu; |
| 138 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 139 | /* |
| 140 | * perf event paranoia level: |
| 141 | * -1 - not paranoid at all |
| 142 | * 0 - disallow raw tracepoint access for unpriv |
| 143 | * 1 - disallow cpu events for unpriv |
| 144 | * 2 - disallow kernel profiling for unpriv |
| 145 | */ |
| 146 | int sysctl_perf_event_paranoid __read_mostly = 1; |
| 147 | |
Frederic Weisbecker | 2044338 | 2011-03-31 03:33:29 +0200 | [diff] [blame] | 148 | /* Minimum for 512 kiB + 1 user control page */ |
| 149 | int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * max perf event sample rate |
| 153 | */ |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 154 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
| 155 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; |
| 156 | static int max_samples_per_tick __read_mostly = |
| 157 | DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); |
| 158 | |
| 159 | int perf_proc_update_handler(struct ctl_table *table, int write, |
| 160 | void __user *buffer, size_t *lenp, |
| 161 | loff_t *ppos) |
| 162 | { |
| 163 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 164 | |
| 165 | if (ret || !write) |
| 166 | return ret; |
| 167 | |
| 168 | max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); |
| 169 | |
| 170 | return 0; |
| 171 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 172 | |
| 173 | static atomic64_t perf_event_id; |
| 174 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 175 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 176 | enum event_type_t event_type); |
| 177 | |
| 178 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 179 | enum event_type_t event_type, |
| 180 | struct task_struct *task); |
| 181 | |
| 182 | static void update_context_time(struct perf_event_context *ctx); |
| 183 | static u64 perf_event_time(struct perf_event *event); |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 184 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 185 | void __weak perf_event_print_debug(void) { } |
| 186 | |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 187 | extern __weak const char *perf_pmu_name(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 188 | { |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 189 | return "pmu"; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 192 | static inline u64 perf_clock(void) |
| 193 | { |
| 194 | return local_clock(); |
| 195 | } |
| 196 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 197 | static inline struct perf_cpu_context * |
| 198 | __get_cpu_context(struct perf_event_context *ctx) |
| 199 | { |
| 200 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); |
| 201 | } |
| 202 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 203 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, |
| 204 | struct perf_event_context *ctx) |
| 205 | { |
| 206 | raw_spin_lock(&cpuctx->ctx.lock); |
| 207 | if (ctx) |
| 208 | raw_spin_lock(&ctx->lock); |
| 209 | } |
| 210 | |
| 211 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, |
| 212 | struct perf_event_context *ctx) |
| 213 | { |
| 214 | if (ctx) |
| 215 | raw_spin_unlock(&ctx->lock); |
| 216 | raw_spin_unlock(&cpuctx->ctx.lock); |
| 217 | } |
| 218 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 219 | #ifdef CONFIG_CGROUP_PERF |
| 220 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 221 | /* |
| 222 | * Must ensure cgroup is pinned (css_get) before calling |
| 223 | * this function. In other words, we cannot call this function |
| 224 | * if there is no cgroup event for the current CPU context. |
| 225 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 226 | static inline struct perf_cgroup * |
| 227 | perf_cgroup_from_task(struct task_struct *task) |
| 228 | { |
| 229 | return container_of(task_subsys_state(task, perf_subsys_id), |
| 230 | struct perf_cgroup, css); |
| 231 | } |
| 232 | |
| 233 | static inline bool |
| 234 | perf_cgroup_match(struct perf_event *event) |
| 235 | { |
| 236 | struct perf_event_context *ctx = event->ctx; |
| 237 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 238 | |
| 239 | return !event->cgrp || event->cgrp == cpuctx->cgrp; |
| 240 | } |
| 241 | |
| 242 | static inline void perf_get_cgroup(struct perf_event *event) |
| 243 | { |
| 244 | css_get(&event->cgrp->css); |
| 245 | } |
| 246 | |
| 247 | static inline void perf_put_cgroup(struct perf_event *event) |
| 248 | { |
| 249 | css_put(&event->cgrp->css); |
| 250 | } |
| 251 | |
| 252 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 253 | { |
| 254 | perf_put_cgroup(event); |
| 255 | event->cgrp = NULL; |
| 256 | } |
| 257 | |
| 258 | static inline int is_cgroup_event(struct perf_event *event) |
| 259 | { |
| 260 | return event->cgrp != NULL; |
| 261 | } |
| 262 | |
| 263 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 264 | { |
| 265 | struct perf_cgroup_info *t; |
| 266 | |
| 267 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 268 | return t->time; |
| 269 | } |
| 270 | |
| 271 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) |
| 272 | { |
| 273 | struct perf_cgroup_info *info; |
| 274 | u64 now; |
| 275 | |
| 276 | now = perf_clock(); |
| 277 | |
| 278 | info = this_cpu_ptr(cgrp->info); |
| 279 | |
| 280 | info->time += now - info->timestamp; |
| 281 | info->timestamp = now; |
| 282 | } |
| 283 | |
| 284 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 285 | { |
| 286 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; |
| 287 | if (cgrp_out) |
| 288 | __update_cgrp_time(cgrp_out); |
| 289 | } |
| 290 | |
| 291 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 292 | { |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 293 | struct perf_cgroup *cgrp; |
| 294 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 295 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 296 | * ensure we access cgroup data only when needed and |
| 297 | * when we know the cgroup is pinned (css_get) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 298 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 299 | if (!is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 300 | return; |
| 301 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 302 | cgrp = perf_cgroup_from_task(current); |
| 303 | /* |
| 304 | * Do not update time when cgroup is not active |
| 305 | */ |
| 306 | if (cgrp == event->cgrp) |
| 307 | __update_cgrp_time(event->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 311 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 312 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 313 | { |
| 314 | struct perf_cgroup *cgrp; |
| 315 | struct perf_cgroup_info *info; |
| 316 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 317 | /* |
| 318 | * ctx->lock held by caller |
| 319 | * ensure we do not access cgroup data |
| 320 | * unless we have the cgroup pinned (css_get) |
| 321 | */ |
| 322 | if (!task || !ctx->nr_cgroups) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 323 | return; |
| 324 | |
| 325 | cgrp = perf_cgroup_from_task(task); |
| 326 | info = this_cpu_ptr(cgrp->info); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 327 | info->timestamp = ctx->timestamp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
| 331 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ |
| 332 | |
| 333 | /* |
| 334 | * reschedule events based on the cgroup constraint of task. |
| 335 | * |
| 336 | * mode SWOUT : schedule out everything |
| 337 | * mode SWIN : schedule in based on cgroup for next |
| 338 | */ |
| 339 | void perf_cgroup_switch(struct task_struct *task, int mode) |
| 340 | { |
| 341 | struct perf_cpu_context *cpuctx; |
| 342 | struct pmu *pmu; |
| 343 | unsigned long flags; |
| 344 | |
| 345 | /* |
| 346 | * disable interrupts to avoid geting nr_cgroup |
| 347 | * changes via __perf_event_disable(). Also |
| 348 | * avoids preemption. |
| 349 | */ |
| 350 | local_irq_save(flags); |
| 351 | |
| 352 | /* |
| 353 | * we reschedule only in the presence of cgroup |
| 354 | * constrained events. |
| 355 | */ |
| 356 | rcu_read_lock(); |
| 357 | |
| 358 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 359 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 360 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 361 | /* |
| 362 | * perf_cgroup_events says at least one |
| 363 | * context on this CPU has cgroup events. |
| 364 | * |
| 365 | * ctx->nr_cgroups reports the number of cgroup |
| 366 | * events for a context. |
| 367 | */ |
| 368 | if (cpuctx->ctx.nr_cgroups > 0) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 369 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 370 | perf_pmu_disable(cpuctx->ctx.pmu); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 371 | |
| 372 | if (mode & PERF_CGROUP_SWOUT) { |
| 373 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 374 | /* |
| 375 | * must not be done before ctxswout due |
| 376 | * to event_filter_match() in event_sched_out() |
| 377 | */ |
| 378 | cpuctx->cgrp = NULL; |
| 379 | } |
| 380 | |
| 381 | if (mode & PERF_CGROUP_SWIN) { |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 382 | WARN_ON_ONCE(cpuctx->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 383 | /* set cgrp before ctxsw in to |
| 384 | * allow event_filter_match() to not |
| 385 | * have to pass task around |
| 386 | */ |
| 387 | cpuctx->cgrp = perf_cgroup_from_task(task); |
| 388 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
| 389 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 390 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 391 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 392 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | rcu_read_unlock(); |
| 396 | |
| 397 | local_irq_restore(flags); |
| 398 | } |
| 399 | |
| 400 | static inline void perf_cgroup_sched_out(struct task_struct *task) |
| 401 | { |
| 402 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); |
| 403 | } |
| 404 | |
| 405 | static inline void perf_cgroup_sched_in(struct task_struct *task) |
| 406 | { |
| 407 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); |
| 408 | } |
| 409 | |
| 410 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
| 411 | struct perf_event_attr *attr, |
| 412 | struct perf_event *group_leader) |
| 413 | { |
| 414 | struct perf_cgroup *cgrp; |
| 415 | struct cgroup_subsys_state *css; |
| 416 | struct file *file; |
| 417 | int ret = 0, fput_needed; |
| 418 | |
| 419 | file = fget_light(fd, &fput_needed); |
| 420 | if (!file) |
| 421 | return -EBADF; |
| 422 | |
| 423 | css = cgroup_css_from_dir(file, perf_subsys_id); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 424 | if (IS_ERR(css)) { |
| 425 | ret = PTR_ERR(css); |
| 426 | goto out; |
| 427 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 428 | |
| 429 | cgrp = container_of(css, struct perf_cgroup, css); |
| 430 | event->cgrp = cgrp; |
| 431 | |
Li Zefan | f75e18c | 2011-03-03 14:25:50 +0800 | [diff] [blame] | 432 | /* must be done before we fput() the file */ |
| 433 | perf_get_cgroup(event); |
| 434 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 435 | /* |
| 436 | * all events in a group must monitor |
| 437 | * the same cgroup because a task belongs |
| 438 | * to only one perf cgroup at a time |
| 439 | */ |
| 440 | if (group_leader && group_leader->cgrp != cgrp) { |
| 441 | perf_detach_cgroup(event); |
| 442 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 443 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 444 | out: |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 445 | fput_light(file, fput_needed); |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | static inline void |
| 450 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 451 | { |
| 452 | struct perf_cgroup_info *t; |
| 453 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 454 | event->shadow_ctx_time = now - t->timestamp; |
| 455 | } |
| 456 | |
| 457 | static inline void |
| 458 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 459 | { |
| 460 | /* |
| 461 | * when the current task's perf cgroup does not match |
| 462 | * the event's, we need to remember to call the |
| 463 | * perf_mark_enable() function the first time a task with |
| 464 | * a matching perf cgroup is scheduled in. |
| 465 | */ |
| 466 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 467 | event->cgrp_defer_enabled = 1; |
| 468 | } |
| 469 | |
| 470 | static inline void |
| 471 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 472 | struct perf_event_context *ctx) |
| 473 | { |
| 474 | struct perf_event *sub; |
| 475 | u64 tstamp = perf_event_time(event); |
| 476 | |
| 477 | if (!event->cgrp_defer_enabled) |
| 478 | return; |
| 479 | |
| 480 | event->cgrp_defer_enabled = 0; |
| 481 | |
| 482 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 483 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 484 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 485 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 486 | sub->cgrp_defer_enabled = 0; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | #else /* !CONFIG_CGROUP_PERF */ |
| 491 | |
| 492 | static inline bool |
| 493 | perf_cgroup_match(struct perf_event *event) |
| 494 | { |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 499 | {} |
| 500 | |
| 501 | static inline int is_cgroup_event(struct perf_event *event) |
| 502 | { |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 507 | { |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 512 | { |
| 513 | } |
| 514 | |
| 515 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 516 | { |
| 517 | } |
| 518 | |
| 519 | static inline void perf_cgroup_sched_out(struct task_struct *task) |
| 520 | { |
| 521 | } |
| 522 | |
| 523 | static inline void perf_cgroup_sched_in(struct task_struct *task) |
| 524 | { |
| 525 | } |
| 526 | |
| 527 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 528 | struct perf_event_attr *attr, |
| 529 | struct perf_event *group_leader) |
| 530 | { |
| 531 | return -EINVAL; |
| 532 | } |
| 533 | |
| 534 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 535 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 536 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 537 | { |
| 538 | } |
| 539 | |
| 540 | void |
| 541 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 542 | { |
| 543 | } |
| 544 | |
| 545 | static inline void |
| 546 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 547 | { |
| 548 | } |
| 549 | |
| 550 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 551 | { |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static inline void |
| 556 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 557 | { |
| 558 | } |
| 559 | |
| 560 | static inline void |
| 561 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 562 | struct perf_event_context *ctx) |
| 563 | { |
| 564 | } |
| 565 | #endif |
| 566 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 567 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 568 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 569 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 570 | if (!(*count)++) |
| 571 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 572 | } |
| 573 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 574 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 575 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 576 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 577 | if (!--(*count)) |
| 578 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 581 | static DEFINE_PER_CPU(struct list_head, rotation_list); |
| 582 | |
| 583 | /* |
| 584 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 585 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 586 | * disabled, while rotate_context is called from IRQ context. |
| 587 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 588 | static void perf_pmu_rotate_start(struct pmu *pmu) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 589 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 590 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 591 | struct list_head *head = &__get_cpu_var(rotation_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 592 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 593 | WARN_ON(!irqs_disabled()); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 594 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 595 | if (list_empty(&cpuctx->rotation_list)) |
| 596 | list_add(&cpuctx->rotation_list, head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static void get_ctx(struct perf_event_context *ctx) |
| 600 | { |
| 601 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 602 | } |
| 603 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 604 | static void put_ctx(struct perf_event_context *ctx) |
| 605 | { |
| 606 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 607 | if (ctx->parent_ctx) |
| 608 | put_ctx(ctx->parent_ctx); |
| 609 | if (ctx->task) |
| 610 | put_task_struct(ctx->task); |
Lai Jiangshan | cb796ff | 2011-03-18 12:07:41 +0800 | [diff] [blame] | 611 | kfree_rcu(ctx, rcu_head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | static void unclone_ctx(struct perf_event_context *ctx) |
| 616 | { |
| 617 | if (ctx->parent_ctx) { |
| 618 | put_ctx(ctx->parent_ctx); |
| 619 | ctx->parent_ctx = NULL; |
| 620 | } |
| 621 | } |
| 622 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 623 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 624 | { |
| 625 | /* |
| 626 | * only top level events have the pid namespace they were created in |
| 627 | */ |
| 628 | if (event->parent) |
| 629 | event = event->parent; |
| 630 | |
| 631 | return task_tgid_nr_ns(p, event->ns); |
| 632 | } |
| 633 | |
| 634 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 635 | { |
| 636 | /* |
| 637 | * only top level events have the pid namespace they were created in |
| 638 | */ |
| 639 | if (event->parent) |
| 640 | event = event->parent; |
| 641 | |
| 642 | return task_pid_nr_ns(p, event->ns); |
| 643 | } |
| 644 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 645 | /* |
| 646 | * If we inherit events we want to return the parent event id |
| 647 | * to userspace. |
| 648 | */ |
| 649 | static u64 primary_event_id(struct perf_event *event) |
| 650 | { |
| 651 | u64 id = event->id; |
| 652 | |
| 653 | if (event->parent) |
| 654 | id = event->parent->id; |
| 655 | |
| 656 | return id; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Get the perf_event_context for a task and lock it. |
| 661 | * This has to cope with with the fact that until it is locked, |
| 662 | * the context could get moved to another task. |
| 663 | */ |
| 664 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 665 | perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 666 | { |
| 667 | struct perf_event_context *ctx; |
| 668 | |
| 669 | rcu_read_lock(); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 670 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 671 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 672 | if (ctx) { |
| 673 | /* |
| 674 | * If this context is a clone of another, it might |
| 675 | * get swapped for another underneath us by |
| 676 | * perf_event_task_sched_out, though the |
| 677 | * rcu_read_lock() protects us from any context |
| 678 | * getting freed. Lock the context and check if it |
| 679 | * got swapped before we could get the lock, and retry |
| 680 | * if so. If we locked the right context, then it |
| 681 | * can't get swapped on us any more. |
| 682 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 683 | raw_spin_lock_irqsave(&ctx->lock, *flags); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 684 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 685 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 686 | goto retry; |
| 687 | } |
| 688 | |
| 689 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 690 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 691 | ctx = NULL; |
| 692 | } |
| 693 | } |
| 694 | rcu_read_unlock(); |
| 695 | return ctx; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Get the context for a task and increment its pin_count so it |
| 700 | * can't get swapped to another task. This also increments its |
| 701 | * reference count so that the context can't get freed. |
| 702 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 703 | static struct perf_event_context * |
| 704 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 705 | { |
| 706 | struct perf_event_context *ctx; |
| 707 | unsigned long flags; |
| 708 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 709 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 710 | if (ctx) { |
| 711 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 712 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 713 | } |
| 714 | return ctx; |
| 715 | } |
| 716 | |
| 717 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 718 | { |
| 719 | unsigned long flags; |
| 720 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 721 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 722 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 723 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 726 | /* |
| 727 | * Update the record of the current time in a context. |
| 728 | */ |
| 729 | static void update_context_time(struct perf_event_context *ctx) |
| 730 | { |
| 731 | u64 now = perf_clock(); |
| 732 | |
| 733 | ctx->time += now - ctx->timestamp; |
| 734 | ctx->timestamp = now; |
| 735 | } |
| 736 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 737 | static u64 perf_event_time(struct perf_event *event) |
| 738 | { |
| 739 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 740 | |
| 741 | if (is_cgroup_event(event)) |
| 742 | return perf_cgroup_event_time(event); |
| 743 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 744 | return ctx ? ctx->time : 0; |
| 745 | } |
| 746 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 747 | /* |
| 748 | * Update the total_time_enabled and total_time_running fields for a event. |
| 749 | */ |
| 750 | static void update_event_times(struct perf_event *event) |
| 751 | { |
| 752 | struct perf_event_context *ctx = event->ctx; |
| 753 | u64 run_end; |
| 754 | |
| 755 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 756 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 757 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 758 | /* |
| 759 | * in cgroup mode, time_enabled represents |
| 760 | * the time the event was enabled AND active |
| 761 | * tasks were in the monitored cgroup. This is |
| 762 | * independent of the activity of the context as |
| 763 | * there may be a mix of cgroup and non-cgroup events. |
| 764 | * |
| 765 | * That is why we treat cgroup events differently |
| 766 | * here. |
| 767 | */ |
| 768 | if (is_cgroup_event(event)) |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 769 | run_end = perf_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 770 | else if (ctx->is_active) |
| 771 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 772 | else |
| 773 | run_end = event->tstamp_stopped; |
| 774 | |
| 775 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 776 | |
| 777 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 778 | run_end = event->tstamp_stopped; |
| 779 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 780 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 781 | |
| 782 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 783 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 786 | /* |
| 787 | * Update total_time_enabled and total_time_running for all events in a group. |
| 788 | */ |
| 789 | static void update_group_times(struct perf_event *leader) |
| 790 | { |
| 791 | struct perf_event *event; |
| 792 | |
| 793 | update_event_times(leader); |
| 794 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 795 | update_event_times(event); |
| 796 | } |
| 797 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 798 | static struct list_head * |
| 799 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 800 | { |
| 801 | if (event->attr.pinned) |
| 802 | return &ctx->pinned_groups; |
| 803 | else |
| 804 | return &ctx->flexible_groups; |
| 805 | } |
| 806 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 807 | /* |
| 808 | * Add a event from the lists for its context. |
| 809 | * Must be called with ctx->mutex and ctx->lock held. |
| 810 | */ |
| 811 | static void |
| 812 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 813 | { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 814 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 815 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 816 | |
| 817 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 818 | * If we're a stand alone event or group leader, we go to the context |
| 819 | * list, group events are kept attached to the group so that |
| 820 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 821 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 822 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 823 | struct list_head *list; |
| 824 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 825 | if (is_software_event(event)) |
| 826 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 827 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 828 | list = ctx_group_list(event, ctx); |
| 829 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 832 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 833 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 834 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 835 | list_add_rcu(&event->event_entry, &ctx->event_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 836 | if (!ctx->nr_events) |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 837 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 838 | ctx->nr_events++; |
| 839 | if (event->attr.inherit_stat) |
| 840 | ctx->nr_stat++; |
| 841 | } |
| 842 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 843 | /* |
| 844 | * Called at perf_event creation and when events are attached/detached from a |
| 845 | * group. |
| 846 | */ |
| 847 | static void perf_event__read_size(struct perf_event *event) |
| 848 | { |
| 849 | int entry = sizeof(u64); /* value */ |
| 850 | int size = 0; |
| 851 | int nr = 1; |
| 852 | |
| 853 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 854 | size += sizeof(u64); |
| 855 | |
| 856 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 857 | size += sizeof(u64); |
| 858 | |
| 859 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 860 | entry += sizeof(u64); |
| 861 | |
| 862 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
| 863 | nr += event->group_leader->nr_siblings; |
| 864 | size += sizeof(u64); |
| 865 | } |
| 866 | |
| 867 | size += entry * nr; |
| 868 | event->read_size = size; |
| 869 | } |
| 870 | |
| 871 | static void perf_event__header_size(struct perf_event *event) |
| 872 | { |
| 873 | struct perf_sample_data *data; |
| 874 | u64 sample_type = event->attr.sample_type; |
| 875 | u16 size = 0; |
| 876 | |
| 877 | perf_event__read_size(event); |
| 878 | |
| 879 | if (sample_type & PERF_SAMPLE_IP) |
| 880 | size += sizeof(data->ip); |
| 881 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 882 | if (sample_type & PERF_SAMPLE_ADDR) |
| 883 | size += sizeof(data->addr); |
| 884 | |
| 885 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 886 | size += sizeof(data->period); |
| 887 | |
| 888 | if (sample_type & PERF_SAMPLE_READ) |
| 889 | size += event->read_size; |
| 890 | |
| 891 | event->header_size = size; |
| 892 | } |
| 893 | |
| 894 | static void perf_event__id_header_size(struct perf_event *event) |
| 895 | { |
| 896 | struct perf_sample_data *data; |
| 897 | u64 sample_type = event->attr.sample_type; |
| 898 | u16 size = 0; |
| 899 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 900 | if (sample_type & PERF_SAMPLE_TID) |
| 901 | size += sizeof(data->tid_entry); |
| 902 | |
| 903 | if (sample_type & PERF_SAMPLE_TIME) |
| 904 | size += sizeof(data->time); |
| 905 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 906 | if (sample_type & PERF_SAMPLE_ID) |
| 907 | size += sizeof(data->id); |
| 908 | |
| 909 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 910 | size += sizeof(data->stream_id); |
| 911 | |
| 912 | if (sample_type & PERF_SAMPLE_CPU) |
| 913 | size += sizeof(data->cpu_entry); |
| 914 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 915 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 916 | } |
| 917 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 918 | static void perf_group_attach(struct perf_event *event) |
| 919 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 920 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 921 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 922 | /* |
| 923 | * We can have double attach due to group movement in perf_event_open. |
| 924 | */ |
| 925 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 926 | return; |
| 927 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 928 | event->attach_state |= PERF_ATTACH_GROUP; |
| 929 | |
| 930 | if (group_leader == event) |
| 931 | return; |
| 932 | |
| 933 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 934 | !is_software_event(event)) |
| 935 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 936 | |
| 937 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 938 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 939 | |
| 940 | perf_event__header_size(group_leader); |
| 941 | |
| 942 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 943 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 946 | /* |
| 947 | * Remove a event from the lists for its context. |
| 948 | * Must be called with ctx->mutex and ctx->lock held. |
| 949 | */ |
| 950 | static void |
| 951 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 952 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 953 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 954 | /* |
| 955 | * We can have double detach due to exit/hot-unplug + close. |
| 956 | */ |
| 957 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 958 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 959 | |
| 960 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 961 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 962 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 963 | ctx->nr_cgroups--; |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 964 | cpuctx = __get_cpu_context(ctx); |
| 965 | /* |
| 966 | * if there are no more cgroup events |
| 967 | * then cler cgrp to avoid stale pointer |
| 968 | * in update_cgrp_time_from_cpuctx() |
| 969 | */ |
| 970 | if (!ctx->nr_cgroups) |
| 971 | cpuctx->cgrp = NULL; |
| 972 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 973 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 974 | ctx->nr_events--; |
| 975 | if (event->attr.inherit_stat) |
| 976 | ctx->nr_stat--; |
| 977 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 978 | list_del_rcu(&event->event_entry); |
| 979 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 980 | if (event->group_leader == event) |
| 981 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 982 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 983 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 984 | |
| 985 | /* |
| 986 | * If event was in error state, then keep it |
| 987 | * that way, otherwise bogus counts will be |
| 988 | * returned on read(). The only way to get out |
| 989 | * of error state is by explicit re-enabling |
| 990 | * of the event |
| 991 | */ |
| 992 | if (event->state > PERF_EVENT_STATE_OFF) |
| 993 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 994 | } |
| 995 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 996 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 997 | { |
| 998 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 999 | struct list_head *list = NULL; |
| 1000 | |
| 1001 | /* |
| 1002 | * We can have double detach due to exit/hot-unplug + close. |
| 1003 | */ |
| 1004 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1005 | return; |
| 1006 | |
| 1007 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1008 | |
| 1009 | /* |
| 1010 | * If this is a sibling, remove it from its group. |
| 1011 | */ |
| 1012 | if (event->group_leader != event) { |
| 1013 | list_del_init(&event->group_entry); |
| 1014 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1015 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | if (!list_empty(&event->group_entry)) |
| 1019 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1020 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1021 | /* |
| 1022 | * If this was a group event with sibling events then |
| 1023 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1024 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1025 | */ |
| 1026 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1027 | if (list) |
| 1028 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1029 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1030 | |
| 1031 | /* Inherit group flags from the previous leader */ |
| 1032 | sibling->group_flags = event->group_flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1033 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1034 | |
| 1035 | out: |
| 1036 | perf_event__header_size(event->group_leader); |
| 1037 | |
| 1038 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1039 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1042 | static inline int |
| 1043 | event_filter_match(struct perf_event *event) |
| 1044 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1045 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
| 1046 | && perf_cgroup_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1049 | static void |
| 1050 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1051 | struct perf_cpu_context *cpuctx, |
| 1052 | struct perf_event_context *ctx) |
| 1053 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1054 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1055 | u64 delta; |
| 1056 | /* |
| 1057 | * An event which could not be activated because of |
| 1058 | * filter mismatch still needs to have its timings |
| 1059 | * maintained, otherwise bogus information is return |
| 1060 | * via read() for time_enabled, time_running: |
| 1061 | */ |
| 1062 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1063 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1064 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1065 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1066 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1069 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1070 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1071 | |
| 1072 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1073 | if (event->pending_disable) { |
| 1074 | event->pending_disable = 0; |
| 1075 | event->state = PERF_EVENT_STATE_OFF; |
| 1076 | } |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1077 | event->tstamp_stopped = tstamp; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1078 | event->pmu->del(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1079 | event->oncpu = -1; |
| 1080 | |
| 1081 | if (!is_software_event(event)) |
| 1082 | cpuctx->active_oncpu--; |
| 1083 | ctx->nr_active--; |
| 1084 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1085 | cpuctx->exclusive = 0; |
| 1086 | } |
| 1087 | |
| 1088 | static void |
| 1089 | group_sched_out(struct perf_event *group_event, |
| 1090 | struct perf_cpu_context *cpuctx, |
| 1091 | struct perf_event_context *ctx) |
| 1092 | { |
| 1093 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1094 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1095 | |
| 1096 | event_sched_out(group_event, cpuctx, ctx); |
| 1097 | |
| 1098 | /* |
| 1099 | * Schedule out siblings (if any): |
| 1100 | */ |
| 1101 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1102 | event_sched_out(event, cpuctx, ctx); |
| 1103 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1104 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1105 | cpuctx->exclusive = 0; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * Cross CPU call to remove a performance event |
| 1110 | * |
| 1111 | * We disable the event on the hardware level first. After that we |
| 1112 | * remove it from the context list. |
| 1113 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1114 | static int __perf_remove_from_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1115 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1116 | struct perf_event *event = info; |
| 1117 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1118 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1119 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1120 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1121 | event_sched_out(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1122 | list_del_event(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1123 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1124 | |
| 1125 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | /* |
| 1130 | * Remove the event from a task's (or a CPU's) list of events. |
| 1131 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1132 | * CPU events are removed with a smp call. For task events we only |
| 1133 | * call when the task is on a CPU. |
| 1134 | * |
| 1135 | * If event->ctx is a cloned context, callers must make sure that |
| 1136 | * every task struct that event->ctx->task could possibly point to |
| 1137 | * remains valid. This is OK when called from perf_release since |
| 1138 | * that only calls us on the top-level context, which can't be a clone. |
| 1139 | * When called from perf_event_exit_task, it's OK because the |
| 1140 | * context has been detached from its task. |
| 1141 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1142 | static void perf_remove_from_context(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1143 | { |
| 1144 | struct perf_event_context *ctx = event->ctx; |
| 1145 | struct task_struct *task = ctx->task; |
| 1146 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1147 | lockdep_assert_held(&ctx->mutex); |
| 1148 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1149 | if (!task) { |
| 1150 | /* |
| 1151 | * Per cpu events are removed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1152 | * the removal is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1153 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1154 | cpu_function_call(event->cpu, __perf_remove_from_context, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1155 | return; |
| 1156 | } |
| 1157 | |
| 1158 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1159 | if (!task_function_call(task, __perf_remove_from_context, event)) |
| 1160 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1161 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1162 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1163 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1164 | * If we failed to find a running task, but find the context active now |
| 1165 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1166 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1167 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1168 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1169 | goto retry; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1173 | * Since the task isn't running, its safe to remove the event, us |
| 1174 | * holding the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1175 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1176 | list_del_event(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1177 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1178 | } |
| 1179 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1180 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1181 | * Cross CPU call to disable a performance event |
| 1182 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1183 | static int __perf_event_disable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1184 | { |
| 1185 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1186 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1187 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1188 | |
| 1189 | /* |
| 1190 | * If this is a per-task event, need to check whether this |
| 1191 | * event's task is the current task on this cpu. |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1192 | * |
| 1193 | * Can trigger due to concurrent perf_event_context_sched_out() |
| 1194 | * flipping contexts around. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1195 | */ |
| 1196 | if (ctx->task && cpuctx->task_ctx != ctx) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1197 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1198 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1199 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1200 | |
| 1201 | /* |
| 1202 | * If the event is on, turn it off. |
| 1203 | * If it is in error state, leave it in error state. |
| 1204 | */ |
| 1205 | if (event->state >= PERF_EVENT_STATE_INACTIVE) { |
| 1206 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1207 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1208 | update_group_times(event); |
| 1209 | if (event == event->group_leader) |
| 1210 | group_sched_out(event, cpuctx, ctx); |
| 1211 | else |
| 1212 | event_sched_out(event, cpuctx, ctx); |
| 1213 | event->state = PERF_EVENT_STATE_OFF; |
| 1214 | } |
| 1215 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1216 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1217 | |
| 1218 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Disable a event. |
| 1223 | * |
| 1224 | * If event->ctx is a cloned context, callers must make sure that |
| 1225 | * every task struct that event->ctx->task could possibly point to |
| 1226 | * remains valid. This condition is satisifed when called through |
| 1227 | * perf_event_for_each_child or perf_event_for_each because they |
| 1228 | * hold the top-level event's child_mutex, so any descendant that |
| 1229 | * goes to exit will block in sync_child_event. |
| 1230 | * When called from perf_pending_event it's OK because event->ctx |
| 1231 | * is the current context on this CPU and preemption is disabled, |
| 1232 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1233 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 1234 | void perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1235 | { |
| 1236 | struct perf_event_context *ctx = event->ctx; |
| 1237 | struct task_struct *task = ctx->task; |
| 1238 | |
| 1239 | if (!task) { |
| 1240 | /* |
| 1241 | * Disable the event on the cpu that it's on |
| 1242 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1243 | cpu_function_call(event->cpu, __perf_event_disable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1244 | return; |
| 1245 | } |
| 1246 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1247 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1248 | if (!task_function_call(task, __perf_event_disable, event)) |
| 1249 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1250 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1251 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1252 | /* |
| 1253 | * If the event is still active, we need to retry the cross-call. |
| 1254 | */ |
| 1255 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1256 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1257 | /* |
| 1258 | * Reload the task pointer, it might have been changed by |
| 1259 | * a concurrent perf_event_context_sched_out(). |
| 1260 | */ |
| 1261 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1262 | goto retry; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * Since we have the lock this context can't be scheduled |
| 1267 | * in, so we can change the state safely. |
| 1268 | */ |
| 1269 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 1270 | update_group_times(event); |
| 1271 | event->state = PERF_EVENT_STATE_OFF; |
| 1272 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1273 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1276 | static void perf_set_shadow_time(struct perf_event *event, |
| 1277 | struct perf_event_context *ctx, |
| 1278 | u64 tstamp) |
| 1279 | { |
| 1280 | /* |
| 1281 | * use the correct time source for the time snapshot |
| 1282 | * |
| 1283 | * We could get by without this by leveraging the |
| 1284 | * fact that to get to this function, the caller |
| 1285 | * has most likely already called update_context_time() |
| 1286 | * and update_cgrp_time_xx() and thus both timestamp |
| 1287 | * are identical (or very close). Given that tstamp is, |
| 1288 | * already adjusted for cgroup, we could say that: |
| 1289 | * tstamp - ctx->timestamp |
| 1290 | * is equivalent to |
| 1291 | * tstamp - cgrp->timestamp. |
| 1292 | * |
| 1293 | * Then, in perf_output_read(), the calculation would |
| 1294 | * work with no changes because: |
| 1295 | * - event is guaranteed scheduled in |
| 1296 | * - no scheduled out in between |
| 1297 | * - thus the timestamp would be the same |
| 1298 | * |
| 1299 | * But this is a bit hairy. |
| 1300 | * |
| 1301 | * So instead, we have an explicit cgroup call to remain |
| 1302 | * within the time time source all along. We believe it |
| 1303 | * is cleaner and simpler to understand. |
| 1304 | */ |
| 1305 | if (is_cgroup_event(event)) |
| 1306 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1307 | else |
| 1308 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1309 | } |
| 1310 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1311 | #define MAX_INTERRUPTS (~0ULL) |
| 1312 | |
| 1313 | static void perf_log_throttle(struct perf_event *event, int enable); |
| 1314 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1315 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1316 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1317 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1318 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1319 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1320 | u64 tstamp = perf_event_time(event); |
| 1321 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1322 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1323 | return 0; |
| 1324 | |
| 1325 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1326 | event->oncpu = smp_processor_id(); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1327 | |
| 1328 | /* |
| 1329 | * Unthrottle events, since we scheduled we might have missed several |
| 1330 | * ticks already, also for a heavily scheduling task there is little |
| 1331 | * guarantee it'll get a tick in a timely manner. |
| 1332 | */ |
| 1333 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1334 | perf_log_throttle(event, 1); |
| 1335 | event->hw.interrupts = 0; |
| 1336 | } |
| 1337 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1338 | /* |
| 1339 | * The new state must be visible before we turn it on in the hardware: |
| 1340 | */ |
| 1341 | smp_wmb(); |
| 1342 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1343 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1344 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1345 | event->oncpu = -1; |
| 1346 | return -EAGAIN; |
| 1347 | } |
| 1348 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1349 | event->tstamp_running += tstamp - event->tstamp_stopped; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1350 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1351 | perf_set_shadow_time(event, ctx, tstamp); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 1352 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1353 | if (!is_software_event(event)) |
| 1354 | cpuctx->active_oncpu++; |
| 1355 | ctx->nr_active++; |
| 1356 | |
| 1357 | if (event->attr.exclusive) |
| 1358 | cpuctx->exclusive = 1; |
| 1359 | |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | static int |
| 1364 | group_sched_in(struct perf_event *group_event, |
| 1365 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1366 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1367 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1368 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1369 | struct pmu *pmu = group_event->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1370 | u64 now = ctx->time; |
| 1371 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1372 | |
| 1373 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1374 | return 0; |
| 1375 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1376 | pmu->start_txn(pmu); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1377 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1378 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1379 | pmu->cancel_txn(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1380 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1381 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1382 | |
| 1383 | /* |
| 1384 | * Schedule in siblings as one group (if any): |
| 1385 | */ |
| 1386 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1387 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1388 | partial_group = event; |
| 1389 | goto group_error; |
| 1390 | } |
| 1391 | } |
| 1392 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1393 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 1394 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1395 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1396 | group_error: |
| 1397 | /* |
| 1398 | * Groups can be scheduled in as one unit only, so undo any |
| 1399 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1400 | * The events up to the failed event are scheduled out normally, |
| 1401 | * tstamp_stopped will be updated. |
| 1402 | * |
| 1403 | * The failed events and the remaining siblings need to have |
| 1404 | * their timings updated as if they had gone thru event_sched_in() |
| 1405 | * and event_sched_out(). This is required to get consistent timings |
| 1406 | * across the group. This also takes care of the case where the group |
| 1407 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 1408 | * the time the event was actually stopped, such that time delta |
| 1409 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1410 | */ |
| 1411 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 1412 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1413 | simulate = true; |
| 1414 | |
| 1415 | if (simulate) { |
| 1416 | event->tstamp_running += now - event->tstamp_stopped; |
| 1417 | event->tstamp_stopped = now; |
| 1418 | } else { |
| 1419 | event_sched_out(event, cpuctx, ctx); |
| 1420 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1421 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1422 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1423 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1424 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1425 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1426 | return -EAGAIN; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1430 | * Work out whether we can put this event group on the CPU now. |
| 1431 | */ |
| 1432 | static int group_can_go_on(struct perf_event *event, |
| 1433 | struct perf_cpu_context *cpuctx, |
| 1434 | int can_add_hw) |
| 1435 | { |
| 1436 | /* |
| 1437 | * Groups consisting entirely of software events can always go on. |
| 1438 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1439 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1440 | return 1; |
| 1441 | /* |
| 1442 | * If an exclusive group is already on, no other hardware |
| 1443 | * events can go on. |
| 1444 | */ |
| 1445 | if (cpuctx->exclusive) |
| 1446 | return 0; |
| 1447 | /* |
| 1448 | * If this group is exclusive and there are already |
| 1449 | * events on the CPU, it can't go on. |
| 1450 | */ |
| 1451 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 1452 | return 0; |
| 1453 | /* |
| 1454 | * Otherwise, try to add it if all previous groups were able |
| 1455 | * to go on. |
| 1456 | */ |
| 1457 | return can_add_hw; |
| 1458 | } |
| 1459 | |
| 1460 | static void add_event_to_ctx(struct perf_event *event, |
| 1461 | struct perf_event_context *ctx) |
| 1462 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1463 | u64 tstamp = perf_event_time(event); |
| 1464 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1465 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1466 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1467 | event->tstamp_enabled = tstamp; |
| 1468 | event->tstamp_running = tstamp; |
| 1469 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1470 | } |
| 1471 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1472 | static void task_ctx_sched_out(struct perf_event_context *ctx); |
| 1473 | static void |
| 1474 | ctx_sched_in(struct perf_event_context *ctx, |
| 1475 | struct perf_cpu_context *cpuctx, |
| 1476 | enum event_type_t event_type, |
| 1477 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1478 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1479 | /* |
| 1480 | * Cross CPU call to install and enable a performance event |
| 1481 | * |
| 1482 | * Must be called with ctx->mutex held |
| 1483 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1484 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1485 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1486 | struct perf_event *event = info; |
| 1487 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1488 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1489 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
| 1490 | struct task_struct *task = current; |
| 1491 | |
| 1492 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 1493 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1494 | |
| 1495 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1496 | * If there was an active task_ctx schedule it out. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1497 | */ |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1498 | if (task_ctx) { |
| 1499 | task_ctx_sched_out(task_ctx); |
| 1500 | /* |
| 1501 | * If the context we're installing events in is not the |
| 1502 | * active task_ctx, flip them. |
| 1503 | */ |
| 1504 | if (ctx->task && task_ctx != ctx) { |
| 1505 | raw_spin_unlock(&cpuctx->ctx.lock); |
| 1506 | raw_spin_lock(&ctx->lock); |
| 1507 | cpuctx->task_ctx = task_ctx = ctx; |
| 1508 | } |
| 1509 | task = task_ctx->task; |
| 1510 | } |
| 1511 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1512 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1513 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1514 | /* |
| 1515 | * update cgrp time only if current cgrp |
| 1516 | * matches event->cgrp. Must be done before |
| 1517 | * calling add_event_to_ctx() |
| 1518 | */ |
| 1519 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1520 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1521 | add_event_to_ctx(event, ctx); |
| 1522 | |
| 1523 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1524 | * Schedule everything back in |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1525 | */ |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1526 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 1527 | if (task_ctx) |
| 1528 | ctx_sched_in(task_ctx, cpuctx, EVENT_PINNED, task); |
| 1529 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 1530 | if (task_ctx) |
| 1531 | ctx_sched_in(task_ctx, cpuctx, EVENT_FLEXIBLE, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1532 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1533 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 1534 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1535 | |
| 1536 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | * Attach a performance event to a context |
| 1541 | * |
| 1542 | * First we add the event to the list with the hardware enable bit |
| 1543 | * in event->hw_config cleared. |
| 1544 | * |
| 1545 | * If the event is attached to a task which is on a CPU we use a smp |
| 1546 | * call to enable it in the task context. The task might have been |
| 1547 | * scheduled away, but we check this in the smp call again. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1548 | */ |
| 1549 | static void |
| 1550 | perf_install_in_context(struct perf_event_context *ctx, |
| 1551 | struct perf_event *event, |
| 1552 | int cpu) |
| 1553 | { |
| 1554 | struct task_struct *task = ctx->task; |
| 1555 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1556 | lockdep_assert_held(&ctx->mutex); |
| 1557 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 1558 | event->ctx = ctx; |
| 1559 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1560 | if (!task) { |
| 1561 | /* |
| 1562 | * Per cpu events are installed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1563 | * the install is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1564 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1565 | cpu_function_call(cpu, __perf_install_in_context, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1566 | return; |
| 1567 | } |
| 1568 | |
| 1569 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1570 | if (!task_function_call(task, __perf_install_in_context, event)) |
| 1571 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1572 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1573 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1574 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1575 | * If we failed to find a running task, but find the context active now |
| 1576 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1577 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1578 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1579 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1580 | goto retry; |
| 1581 | } |
| 1582 | |
| 1583 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1584 | * Since the task isn't running, its safe to add the event, us holding |
| 1585 | * the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1586 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1587 | add_event_to_ctx(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1588 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /* |
| 1592 | * Put a event into inactive state and update time fields. |
| 1593 | * Enabling the leader of a group effectively enables all |
| 1594 | * the group members that aren't explicitly disabled, so we |
| 1595 | * have to update their ->tstamp_enabled also. |
| 1596 | * Note: this works for group members as well as group leaders |
| 1597 | * since the non-leader members' sibling_lists will be empty. |
| 1598 | */ |
| 1599 | static void __perf_event_mark_enabled(struct perf_event *event, |
| 1600 | struct perf_event_context *ctx) |
| 1601 | { |
| 1602 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1603 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1604 | |
| 1605 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1606 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1607 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1608 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 1609 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1610 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /* |
| 1614 | * Cross CPU call to enable a performance event |
| 1615 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1616 | static int __perf_event_enable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1617 | { |
| 1618 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1619 | struct perf_event_context *ctx = event->ctx; |
| 1620 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1621 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1622 | int err; |
| 1623 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1624 | if (WARN_ON_ONCE(!ctx->is_active)) |
| 1625 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1626 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1627 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1628 | update_context_time(ctx); |
| 1629 | |
| 1630 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 1631 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1632 | |
| 1633 | /* |
| 1634 | * set current task's cgroup time reference point |
| 1635 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 1636 | perf_cgroup_set_timestamp(current, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1637 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1638 | __perf_event_mark_enabled(event, ctx); |
| 1639 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1640 | if (!event_filter_match(event)) { |
| 1641 | if (is_cgroup_event(event)) |
| 1642 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 1643 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1644 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 1645 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1646 | /* |
| 1647 | * If the event is in a group and isn't the group leader, |
| 1648 | * then don't put it on unless the group is on. |
| 1649 | */ |
| 1650 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) |
| 1651 | goto unlock; |
| 1652 | |
| 1653 | if (!group_can_go_on(event, cpuctx, 1)) { |
| 1654 | err = -EEXIST; |
| 1655 | } else { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1656 | if (event == leader) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1657 | err = group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1658 | else |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1659 | err = event_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | if (err) { |
| 1663 | /* |
| 1664 | * If this event can't go on and it's part of a |
| 1665 | * group, then the whole group has to come off. |
| 1666 | */ |
| 1667 | if (leader != event) |
| 1668 | group_sched_out(leader, cpuctx, ctx); |
| 1669 | if (leader->attr.pinned) { |
| 1670 | update_group_times(leader); |
| 1671 | leader->state = PERF_EVENT_STATE_ERROR; |
| 1672 | } |
| 1673 | } |
| 1674 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1675 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1676 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1677 | |
| 1678 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | /* |
| 1682 | * Enable a event. |
| 1683 | * |
| 1684 | * If event->ctx is a cloned context, callers must make sure that |
| 1685 | * every task struct that event->ctx->task could possibly point to |
| 1686 | * remains valid. This condition is satisfied when called through |
| 1687 | * perf_event_for_each_child or perf_event_for_each as described |
| 1688 | * for perf_event_disable. |
| 1689 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 1690 | void perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1691 | { |
| 1692 | struct perf_event_context *ctx = event->ctx; |
| 1693 | struct task_struct *task = ctx->task; |
| 1694 | |
| 1695 | if (!task) { |
| 1696 | /* |
| 1697 | * Enable the event on the cpu that it's on |
| 1698 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1699 | cpu_function_call(event->cpu, __perf_event_enable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1700 | return; |
| 1701 | } |
| 1702 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1703 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1704 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 1705 | goto out; |
| 1706 | |
| 1707 | /* |
| 1708 | * If the event is in error state, clear that first. |
| 1709 | * That way, if we see the event in error state below, we |
| 1710 | * know that it has gone back into error state, as distinct |
| 1711 | * from the task having been scheduled away before the |
| 1712 | * cross-call arrived. |
| 1713 | */ |
| 1714 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 1715 | event->state = PERF_EVENT_STATE_OFF; |
| 1716 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1717 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1718 | if (!ctx->is_active) { |
| 1719 | __perf_event_mark_enabled(event, ctx); |
| 1720 | goto out; |
| 1721 | } |
| 1722 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1723 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1724 | |
| 1725 | if (!task_function_call(task, __perf_event_enable, event)) |
| 1726 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1727 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1728 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1729 | |
| 1730 | /* |
| 1731 | * If the context is active and the event is still off, |
| 1732 | * we need to retry the cross-call. |
| 1733 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1734 | if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) { |
| 1735 | /* |
| 1736 | * task could have been flipped by a concurrent |
| 1737 | * perf_event_context_sched_out() |
| 1738 | */ |
| 1739 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1740 | goto retry; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1741 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1742 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1743 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1744 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | static int perf_event_refresh(struct perf_event *event, int refresh) |
| 1748 | { |
| 1749 | /* |
| 1750 | * not supported on inherited events |
| 1751 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 1752 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1753 | return -EINVAL; |
| 1754 | |
| 1755 | atomic_add(refresh, &event->event_limit); |
| 1756 | perf_event_enable(event); |
| 1757 | |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1761 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 1762 | struct perf_cpu_context *cpuctx, |
| 1763 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1764 | { |
| 1765 | struct perf_event *event; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame^] | 1766 | int is_active = ctx->is_active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1767 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame^] | 1768 | ctx->is_active &= ~event_type; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1769 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 1770 | return; |
| 1771 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1772 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1773 | update_cgrp_time_from_cpuctx(cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1774 | if (!ctx->nr_active) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 1775 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1776 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 1777 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame^] | 1778 | if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1779 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 1780 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1781 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1782 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame^] | 1783 | if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1784 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 1785 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1786 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 1787 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | /* |
| 1791 | * Test whether two contexts are equivalent, i.e. whether they |
| 1792 | * have both been cloned from the same version of the same context |
| 1793 | * and they both have the same number of enabled events. |
| 1794 | * If the number of enabled events is the same, then the set |
| 1795 | * of enabled events should be the same, because these are both |
| 1796 | * inherited contexts, therefore we can't access individual events |
| 1797 | * in them directly with an fd; we can only enable/disable all |
| 1798 | * events via prctl, or enable/disable all events in a family |
| 1799 | * via ioctl, which will have the same effect on both contexts. |
| 1800 | */ |
| 1801 | static int context_equiv(struct perf_event_context *ctx1, |
| 1802 | struct perf_event_context *ctx2) |
| 1803 | { |
| 1804 | return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx |
| 1805 | && ctx1->parent_gen == ctx2->parent_gen |
| 1806 | && !ctx1->pin_count && !ctx2->pin_count; |
| 1807 | } |
| 1808 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1809 | static void __perf_event_sync_stat(struct perf_event *event, |
| 1810 | struct perf_event *next_event) |
| 1811 | { |
| 1812 | u64 value; |
| 1813 | |
| 1814 | if (!event->attr.inherit_stat) |
| 1815 | return; |
| 1816 | |
| 1817 | /* |
| 1818 | * Update the event value, we cannot use perf_event_read() |
| 1819 | * because we're in the middle of a context switch and have IRQs |
| 1820 | * disabled, which upsets smp_call_function_single(), however |
| 1821 | * we know the event must be on the current CPU, therefore we |
| 1822 | * don't need to use it. |
| 1823 | */ |
| 1824 | switch (event->state) { |
| 1825 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 1826 | event->pmu->read(event); |
| 1827 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1828 | |
| 1829 | case PERF_EVENT_STATE_INACTIVE: |
| 1830 | update_event_times(event); |
| 1831 | break; |
| 1832 | |
| 1833 | default: |
| 1834 | break; |
| 1835 | } |
| 1836 | |
| 1837 | /* |
| 1838 | * In order to keep per-task stats reliable we need to flip the event |
| 1839 | * values when we flip the contexts. |
| 1840 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1841 | value = local64_read(&next_event->count); |
| 1842 | value = local64_xchg(&event->count, value); |
| 1843 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1844 | |
| 1845 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 1846 | swap(event->total_time_running, next_event->total_time_running); |
| 1847 | |
| 1848 | /* |
| 1849 | * Since we swizzled the values, update the user visible data too. |
| 1850 | */ |
| 1851 | perf_event_update_userpage(event); |
| 1852 | perf_event_update_userpage(next_event); |
| 1853 | } |
| 1854 | |
| 1855 | #define list_next_entry(pos, member) \ |
| 1856 | list_entry(pos->member.next, typeof(*pos), member) |
| 1857 | |
| 1858 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 1859 | struct perf_event_context *next_ctx) |
| 1860 | { |
| 1861 | struct perf_event *event, *next_event; |
| 1862 | |
| 1863 | if (!ctx->nr_stat) |
| 1864 | return; |
| 1865 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 1866 | update_context_time(ctx); |
| 1867 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1868 | event = list_first_entry(&ctx->event_list, |
| 1869 | struct perf_event, event_entry); |
| 1870 | |
| 1871 | next_event = list_first_entry(&next_ctx->event_list, |
| 1872 | struct perf_event, event_entry); |
| 1873 | |
| 1874 | while (&event->event_entry != &ctx->event_list && |
| 1875 | &next_event->event_entry != &next_ctx->event_list) { |
| 1876 | |
| 1877 | __perf_event_sync_stat(event, next_event); |
| 1878 | |
| 1879 | event = list_next_entry(event, event_entry); |
| 1880 | next_event = list_next_entry(next_event, event_entry); |
| 1881 | } |
| 1882 | } |
| 1883 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1884 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 1885 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1886 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1887 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1888 | struct perf_event_context *next_ctx; |
| 1889 | struct perf_event_context *parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1890 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1891 | int do_switch = 1; |
| 1892 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1893 | if (likely(!ctx)) |
| 1894 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1895 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1896 | cpuctx = __get_cpu_context(ctx); |
| 1897 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1898 | return; |
| 1899 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1900 | rcu_read_lock(); |
| 1901 | parent = rcu_dereference(ctx->parent_ctx); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1902 | next_ctx = next->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1903 | if (parent && next_ctx && |
| 1904 | rcu_dereference(next_ctx->parent_ctx) == parent) { |
| 1905 | /* |
| 1906 | * Looks like the two contexts are clones, so we might be |
| 1907 | * able to optimize the context switch. We lock both |
| 1908 | * contexts and check that they are clones under the |
| 1909 | * lock (including re-checking that neither has been |
| 1910 | * uncloned in the meantime). It doesn't matter which |
| 1911 | * order we take the locks because no other cpu could |
| 1912 | * be trying to lock both of these tasks. |
| 1913 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1914 | raw_spin_lock(&ctx->lock); |
| 1915 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1916 | if (context_equiv(ctx, next_ctx)) { |
| 1917 | /* |
| 1918 | * XXX do we need a memory barrier of sorts |
| 1919 | * wrt to rcu_dereference() of perf_event_ctxp |
| 1920 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1921 | task->perf_event_ctxp[ctxn] = next_ctx; |
| 1922 | next->perf_event_ctxp[ctxn] = ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1923 | ctx->task = next; |
| 1924 | next_ctx->task = task; |
| 1925 | do_switch = 0; |
| 1926 | |
| 1927 | perf_event_sync_stat(ctx, next_ctx); |
| 1928 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1929 | raw_spin_unlock(&next_ctx->lock); |
| 1930 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1931 | } |
| 1932 | rcu_read_unlock(); |
| 1933 | |
| 1934 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 1935 | raw_spin_lock(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1936 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1937 | cpuctx->task_ctx = NULL; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 1938 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1942 | #define for_each_task_context_nr(ctxn) \ |
| 1943 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 1944 | |
| 1945 | /* |
| 1946 | * Called from scheduler to remove the events of the current task, |
| 1947 | * with interrupts disabled. |
| 1948 | * |
| 1949 | * We stop each event and update the event value in event->count. |
| 1950 | * |
| 1951 | * This does not protect us against NMI, but disable() |
| 1952 | * sets the disabled bit in the control field of event _before_ |
| 1953 | * accessing the event control register. If a NMI hits, then it will |
| 1954 | * not restart the event. |
| 1955 | */ |
Peter Zijlstra | 82cd6de | 2010-10-14 17:57:23 +0200 | [diff] [blame] | 1956 | void __perf_event_task_sched_out(struct task_struct *task, |
| 1957 | struct task_struct *next) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1958 | { |
| 1959 | int ctxn; |
| 1960 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1961 | for_each_task_context_nr(ctxn) |
| 1962 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1963 | |
| 1964 | /* |
| 1965 | * if cgroup events exist on this CPU, then we need |
| 1966 | * to check if we have to switch out PMU state. |
| 1967 | * cgroup event are system-wide mode only |
| 1968 | */ |
| 1969 | if (atomic_read(&__get_cpu_var(perf_cgroup_events))) |
| 1970 | perf_cgroup_sched_out(task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 1973 | static void task_ctx_sched_out(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1974 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1975 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1976 | |
| 1977 | if (!cpuctx->task_ctx) |
| 1978 | return; |
| 1979 | |
| 1980 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 1981 | return; |
| 1982 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 1983 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1984 | cpuctx->task_ctx = NULL; |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * Called with IRQs disabled |
| 1989 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1990 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 1991 | enum event_type_t event_type) |
| 1992 | { |
| 1993 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1997 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1998 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1999 | { |
| 2000 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2001 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2002 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2003 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2004 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2005 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2006 | continue; |
| 2007 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2008 | /* may need to reset tstamp_enabled */ |
| 2009 | if (is_cgroup_event(event)) |
| 2010 | perf_cgroup_mark_enabled(event, ctx); |
| 2011 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2012 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2013 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2014 | |
| 2015 | /* |
| 2016 | * If this pinned group hasn't been scheduled, |
| 2017 | * put it in error state. |
| 2018 | */ |
| 2019 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2020 | update_group_times(event); |
| 2021 | event->state = PERF_EVENT_STATE_ERROR; |
| 2022 | } |
| 2023 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | static void |
| 2027 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2028 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2029 | { |
| 2030 | struct perf_event *event; |
| 2031 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2032 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2033 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2034 | /* Ignore events in OFF or ERROR state */ |
| 2035 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2036 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2037 | /* |
| 2038 | * Listen to the 'cpu' scheduling filter constraint |
| 2039 | * of events: |
| 2040 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2041 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2042 | continue; |
| 2043 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2044 | /* may need to reset tstamp_enabled */ |
| 2045 | if (is_cgroup_event(event)) |
| 2046 | perf_cgroup_mark_enabled(event, ctx); |
| 2047 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2048 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2049 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2050 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2051 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2052 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | static void |
| 2056 | ctx_sched_in(struct perf_event_context *ctx, |
| 2057 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2058 | enum event_type_t event_type, |
| 2059 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2060 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2061 | u64 now; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame^] | 2062 | int is_active = ctx->is_active; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2063 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame^] | 2064 | ctx->is_active |= event_type; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2065 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2066 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2067 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2068 | now = perf_clock(); |
| 2069 | ctx->timestamp = now; |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2070 | perf_cgroup_set_timestamp(task, ctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2071 | /* |
| 2072 | * First go through the list and put on any pinned groups |
| 2073 | * in order to give them the best chance of going on. |
| 2074 | */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame^] | 2075 | if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2076 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2077 | |
| 2078 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame^] | 2079 | if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2080 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2081 | } |
| 2082 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2083 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2084 | enum event_type_t event_type, |
| 2085 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2086 | { |
| 2087 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2088 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2089 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2090 | } |
| 2091 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2092 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2093 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2094 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2095 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2096 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2097 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2098 | if (cpuctx->task_ctx == ctx) |
| 2099 | return; |
| 2100 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2101 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2102 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2103 | /* |
| 2104 | * We want to keep the following priority order: |
| 2105 | * cpu pinned (that don't need to move), task pinned, |
| 2106 | * cpu flexible, task flexible. |
| 2107 | */ |
| 2108 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2109 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2110 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 2111 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 2112 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2113 | |
| 2114 | cpuctx->task_ctx = ctx; |
eranian@google.com | 9b33fa6 | 2010-03-10 22:26:05 -0800 | [diff] [blame] | 2115 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2116 | perf_pmu_enable(ctx->pmu); |
| 2117 | perf_ctx_unlock(cpuctx, ctx); |
| 2118 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2119 | /* |
| 2120 | * Since these rotations are per-cpu, we need to ensure the |
| 2121 | * cpu-context we got scheduled on is actually rotating. |
| 2122 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2123 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2124 | } |
| 2125 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2126 | /* |
| 2127 | * Called from scheduler to add the events of the current task |
| 2128 | * with interrupts disabled. |
| 2129 | * |
| 2130 | * We restore the event value and then enable it. |
| 2131 | * |
| 2132 | * This does not protect us against NMI, but enable() |
| 2133 | * sets the enabled bit in the control field of event _before_ |
| 2134 | * accessing the event control register. If a NMI hits, then it will |
| 2135 | * keep the event running. |
| 2136 | */ |
Peter Zijlstra | 82cd6de | 2010-10-14 17:57:23 +0200 | [diff] [blame] | 2137 | void __perf_event_task_sched_in(struct task_struct *task) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2138 | { |
| 2139 | struct perf_event_context *ctx; |
| 2140 | int ctxn; |
| 2141 | |
| 2142 | for_each_task_context_nr(ctxn) { |
| 2143 | ctx = task->perf_event_ctxp[ctxn]; |
| 2144 | if (likely(!ctx)) |
| 2145 | continue; |
| 2146 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2147 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2148 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2149 | /* |
| 2150 | * if cgroup events exist on this CPU, then we need |
| 2151 | * to check if we have to switch in PMU state. |
| 2152 | * cgroup event are system-wide mode only |
| 2153 | */ |
| 2154 | if (atomic_read(&__get_cpu_var(perf_cgroup_events))) |
| 2155 | perf_cgroup_sched_in(task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2156 | } |
| 2157 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2158 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 2159 | { |
| 2160 | u64 frequency = event->attr.sample_freq; |
| 2161 | u64 sec = NSEC_PER_SEC; |
| 2162 | u64 divisor, dividend; |
| 2163 | |
| 2164 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 2165 | |
| 2166 | count_fls = fls64(count); |
| 2167 | nsec_fls = fls64(nsec); |
| 2168 | frequency_fls = fls64(frequency); |
| 2169 | sec_fls = 30; |
| 2170 | |
| 2171 | /* |
| 2172 | * We got @count in @nsec, with a target of sample_freq HZ |
| 2173 | * the target period becomes: |
| 2174 | * |
| 2175 | * @count * 10^9 |
| 2176 | * period = ------------------- |
| 2177 | * @nsec * sample_freq |
| 2178 | * |
| 2179 | */ |
| 2180 | |
| 2181 | /* |
| 2182 | * Reduce accuracy by one bit such that @a and @b converge |
| 2183 | * to a similar magnitude. |
| 2184 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2185 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2186 | do { \ |
| 2187 | if (a##_fls > b##_fls) { \ |
| 2188 | a >>= 1; \ |
| 2189 | a##_fls--; \ |
| 2190 | } else { \ |
| 2191 | b >>= 1; \ |
| 2192 | b##_fls--; \ |
| 2193 | } \ |
| 2194 | } while (0) |
| 2195 | |
| 2196 | /* |
| 2197 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 2198 | * the other, so that finally we can do a u64/u64 division. |
| 2199 | */ |
| 2200 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 2201 | REDUCE_FLS(nsec, frequency); |
| 2202 | REDUCE_FLS(sec, count); |
| 2203 | } |
| 2204 | |
| 2205 | if (count_fls + sec_fls > 64) { |
| 2206 | divisor = nsec * frequency; |
| 2207 | |
| 2208 | while (count_fls + sec_fls > 64) { |
| 2209 | REDUCE_FLS(count, sec); |
| 2210 | divisor >>= 1; |
| 2211 | } |
| 2212 | |
| 2213 | dividend = count * sec; |
| 2214 | } else { |
| 2215 | dividend = count * sec; |
| 2216 | |
| 2217 | while (nsec_fls + frequency_fls > 64) { |
| 2218 | REDUCE_FLS(nsec, frequency); |
| 2219 | dividend >>= 1; |
| 2220 | } |
| 2221 | |
| 2222 | divisor = nsec * frequency; |
| 2223 | } |
| 2224 | |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2225 | if (!divisor) |
| 2226 | return dividend; |
| 2227 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2228 | return div64_u64(dividend, divisor); |
| 2229 | } |
| 2230 | |
| 2231 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2232 | { |
| 2233 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2234 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2235 | s64 delta; |
| 2236 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2237 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2238 | |
| 2239 | delta = (s64)(period - hwc->sample_period); |
| 2240 | delta = (delta + 7) / 8; /* low pass filter */ |
| 2241 | |
| 2242 | sample_period = hwc->sample_period + delta; |
| 2243 | |
| 2244 | if (!sample_period) |
| 2245 | sample_period = 1; |
| 2246 | |
| 2247 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2248 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2249 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2250 | event->pmu->stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2251 | local64_set(&hwc->period_left, 0); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2252 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2253 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2254 | } |
| 2255 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2256 | static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2257 | { |
| 2258 | struct perf_event *event; |
| 2259 | struct hw_perf_event *hwc; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2260 | u64 interrupts, now; |
| 2261 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2262 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 2263 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2264 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 2265 | continue; |
| 2266 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2267 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 2268 | continue; |
| 2269 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2270 | hwc = &event->hw; |
| 2271 | |
| 2272 | interrupts = hwc->interrupts; |
| 2273 | hwc->interrupts = 0; |
| 2274 | |
| 2275 | /* |
| 2276 | * unthrottle events on the tick |
| 2277 | */ |
| 2278 | if (interrupts == MAX_INTERRUPTS) { |
| 2279 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2280 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | if (!event->attr.freq || !event->attr.sample_freq) |
| 2284 | continue; |
| 2285 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2286 | event->pmu->read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2287 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2288 | delta = now - hwc->freq_count_stamp; |
| 2289 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2290 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2291 | if (delta > 0) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2292 | perf_adjust_period(event, period, delta); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2293 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | /* |
| 2297 | * Round-robin a context's events: |
| 2298 | */ |
| 2299 | static void rotate_ctx(struct perf_event_context *ctx) |
| 2300 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 2301 | /* |
| 2302 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 2303 | * disabled by the inheritance code. |
| 2304 | */ |
| 2305 | if (!ctx->rotate_disable) |
| 2306 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2307 | } |
| 2308 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2309 | /* |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2310 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 2311 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 2312 | * disabled, while rotate_context is called from IRQ context. |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2313 | */ |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2314 | static void perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2315 | { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2316 | u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2317 | struct perf_event_context *ctx = NULL; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2318 | int rotate = 0, remove = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2319 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2320 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2321 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2322 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 2323 | rotate = 1; |
| 2324 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2325 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2326 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2327 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2328 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2329 | if (ctx->nr_events != ctx->nr_active) |
| 2330 | rotate = 1; |
| 2331 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2332 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2333 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2334 | perf_pmu_disable(cpuctx->ctx.pmu); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2335 | perf_ctx_adjust_freq(&cpuctx->ctx, interval); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2336 | if (ctx) |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2337 | perf_ctx_adjust_freq(ctx, interval); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2338 | |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 2339 | if (!rotate) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2340 | goto done; |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 2341 | |
Frederic Weisbecker | 7defb0f | 2010-01-17 12:15:31 +0100 | [diff] [blame] | 2342 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2343 | if (ctx) |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2344 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2345 | |
| 2346 | rotate_ctx(&cpuctx->ctx); |
| 2347 | if (ctx) |
| 2348 | rotate_ctx(ctx); |
| 2349 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2350 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, current); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2351 | if (ctx) |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2352 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, current); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2353 | |
| 2354 | done: |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2355 | if (remove) |
| 2356 | list_del_init(&cpuctx->rotation_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2357 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2358 | perf_pmu_enable(cpuctx->ctx.pmu); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2359 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | void perf_event_task_tick(void) |
| 2363 | { |
| 2364 | struct list_head *head = &__get_cpu_var(rotation_list); |
| 2365 | struct perf_cpu_context *cpuctx, *tmp; |
| 2366 | |
| 2367 | WARN_ON(!irqs_disabled()); |
| 2368 | |
| 2369 | list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) { |
| 2370 | if (cpuctx->jiffies_interval == 1 || |
| 2371 | !(jiffies % cpuctx->jiffies_interval)) |
| 2372 | perf_rotate_context(cpuctx); |
| 2373 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2374 | } |
| 2375 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2376 | static int event_enable_on_exec(struct perf_event *event, |
| 2377 | struct perf_event_context *ctx) |
| 2378 | { |
| 2379 | if (!event->attr.enable_on_exec) |
| 2380 | return 0; |
| 2381 | |
| 2382 | event->attr.enable_on_exec = 0; |
| 2383 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2384 | return 0; |
| 2385 | |
| 2386 | __perf_event_mark_enabled(event, ctx); |
| 2387 | |
| 2388 | return 1; |
| 2389 | } |
| 2390 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2391 | /* |
| 2392 | * Enable all of a task's events that have been marked enable-on-exec. |
| 2393 | * This expects task == current. |
| 2394 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2395 | static void perf_event_enable_on_exec(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2396 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2397 | struct perf_event *event; |
| 2398 | unsigned long flags; |
| 2399 | int enabled = 0; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2400 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2401 | |
| 2402 | local_irq_save(flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2403 | if (!ctx || !ctx->nr_events) |
| 2404 | goto out; |
| 2405 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 2406 | /* |
| 2407 | * We must ctxsw out cgroup events to avoid conflict |
| 2408 | * when invoking perf_task_event_sched_in() later on |
| 2409 | * in this function. Otherwise we end up trying to |
| 2410 | * ctxswin cgroup events which are already scheduled |
| 2411 | * in. |
| 2412 | */ |
| 2413 | perf_cgroup_sched_out(current); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2414 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2415 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2416 | task_ctx_sched_out(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2417 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2418 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2419 | ret = event_enable_on_exec(event, ctx); |
| 2420 | if (ret) |
| 2421 | enabled = 1; |
| 2422 | } |
| 2423 | |
| 2424 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2425 | ret = event_enable_on_exec(event, ctx); |
| 2426 | if (ret) |
| 2427 | enabled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | /* |
| 2431 | * Unclone this context if we enabled any event. |
| 2432 | */ |
| 2433 | if (enabled) |
| 2434 | unclone_ctx(ctx); |
| 2435 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2436 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2437 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 2438 | /* |
| 2439 | * Also calls ctxswin for cgroup events, if any: |
| 2440 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2441 | perf_event_context_sched_in(ctx, ctx->task); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2442 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2443 | local_irq_restore(flags); |
| 2444 | } |
| 2445 | |
| 2446 | /* |
| 2447 | * Cross CPU call to read the hardware event |
| 2448 | */ |
| 2449 | static void __perf_event_read(void *info) |
| 2450 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2451 | struct perf_event *event = info; |
| 2452 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2453 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2454 | |
| 2455 | /* |
| 2456 | * If this is a task context, we need to check whether it is |
| 2457 | * the current task context of this cpu. If not it has been |
| 2458 | * scheduled out before the smp call arrived. In that case |
| 2459 | * event->count would have been updated to a recent sample |
| 2460 | * when the event was scheduled out. |
| 2461 | */ |
| 2462 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 2463 | return; |
| 2464 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2465 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2466 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 2467 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2468 | update_cgrp_time_from_event(event); |
| 2469 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2470 | update_event_times(event); |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 2471 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
| 2472 | event->pmu->read(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2473 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 2476 | static inline u64 perf_event_count(struct perf_event *event) |
| 2477 | { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2478 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 2479 | } |
| 2480 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2481 | static u64 perf_event_read(struct perf_event *event) |
| 2482 | { |
| 2483 | /* |
| 2484 | * If event is enabled and currently active on a CPU, update the |
| 2485 | * value in the event structure: |
| 2486 | */ |
| 2487 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
| 2488 | smp_call_function_single(event->oncpu, |
| 2489 | __perf_event_read, event, 1); |
| 2490 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 2491 | struct perf_event_context *ctx = event->ctx; |
| 2492 | unsigned long flags; |
| 2493 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2494 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 2495 | /* |
| 2496 | * may read while context is not active |
| 2497 | * (e.g., thread is blocked), in that case |
| 2498 | * we cannot update context time |
| 2499 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2500 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 2501 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2502 | update_cgrp_time_from_event(event); |
| 2503 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2504 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2505 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2506 | } |
| 2507 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 2508 | return perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | /* |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 2512 | * Callchain support |
| 2513 | */ |
| 2514 | |
| 2515 | struct callchain_cpus_entries { |
| 2516 | struct rcu_head rcu_head; |
| 2517 | struct perf_callchain_entry *cpu_entries[0]; |
| 2518 | }; |
| 2519 | |
Frederic Weisbecker | 7ae07ea | 2010-08-14 20:45:13 +0200 | [diff] [blame] | 2520 | static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 2521 | static atomic_t nr_callchain_events; |
| 2522 | static DEFINE_MUTEX(callchain_mutex); |
| 2523 | struct callchain_cpus_entries *callchain_cpus_entries; |
| 2524 | |
| 2525 | |
| 2526 | __weak void perf_callchain_kernel(struct perf_callchain_entry *entry, |
| 2527 | struct pt_regs *regs) |
| 2528 | { |
| 2529 | } |
| 2530 | |
| 2531 | __weak void perf_callchain_user(struct perf_callchain_entry *entry, |
| 2532 | struct pt_regs *regs) |
| 2533 | { |
| 2534 | } |
| 2535 | |
| 2536 | static void release_callchain_buffers_rcu(struct rcu_head *head) |
| 2537 | { |
| 2538 | struct callchain_cpus_entries *entries; |
| 2539 | int cpu; |
| 2540 | |
| 2541 | entries = container_of(head, struct callchain_cpus_entries, rcu_head); |
| 2542 | |
| 2543 | for_each_possible_cpu(cpu) |
| 2544 | kfree(entries->cpu_entries[cpu]); |
| 2545 | |
| 2546 | kfree(entries); |
| 2547 | } |
| 2548 | |
| 2549 | static void release_callchain_buffers(void) |
| 2550 | { |
| 2551 | struct callchain_cpus_entries *entries; |
| 2552 | |
| 2553 | entries = callchain_cpus_entries; |
| 2554 | rcu_assign_pointer(callchain_cpus_entries, NULL); |
| 2555 | call_rcu(&entries->rcu_head, release_callchain_buffers_rcu); |
| 2556 | } |
| 2557 | |
| 2558 | static int alloc_callchain_buffers(void) |
| 2559 | { |
| 2560 | int cpu; |
| 2561 | int size; |
| 2562 | struct callchain_cpus_entries *entries; |
| 2563 | |
| 2564 | /* |
| 2565 | * We can't use the percpu allocation API for data that can be |
| 2566 | * accessed from NMI. Use a temporary manual per cpu allocation |
| 2567 | * until that gets sorted out. |
| 2568 | */ |
Eric Dumazet | 88d4f0d | 2011-01-25 19:40:51 +0100 | [diff] [blame] | 2569 | size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 2570 | |
| 2571 | entries = kzalloc(size, GFP_KERNEL); |
| 2572 | if (!entries) |
| 2573 | return -ENOMEM; |
| 2574 | |
Frederic Weisbecker | 7ae07ea | 2010-08-14 20:45:13 +0200 | [diff] [blame] | 2575 | size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS; |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 2576 | |
| 2577 | for_each_possible_cpu(cpu) { |
| 2578 | entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL, |
| 2579 | cpu_to_node(cpu)); |
| 2580 | if (!entries->cpu_entries[cpu]) |
| 2581 | goto fail; |
| 2582 | } |
| 2583 | |
| 2584 | rcu_assign_pointer(callchain_cpus_entries, entries); |
| 2585 | |
| 2586 | return 0; |
| 2587 | |
| 2588 | fail: |
| 2589 | for_each_possible_cpu(cpu) |
| 2590 | kfree(entries->cpu_entries[cpu]); |
| 2591 | kfree(entries); |
| 2592 | |
| 2593 | return -ENOMEM; |
| 2594 | } |
| 2595 | |
| 2596 | static int get_callchain_buffers(void) |
| 2597 | { |
| 2598 | int err = 0; |
| 2599 | int count; |
| 2600 | |
| 2601 | mutex_lock(&callchain_mutex); |
| 2602 | |
| 2603 | count = atomic_inc_return(&nr_callchain_events); |
| 2604 | if (WARN_ON_ONCE(count < 1)) { |
| 2605 | err = -EINVAL; |
| 2606 | goto exit; |
| 2607 | } |
| 2608 | |
| 2609 | if (count > 1) { |
| 2610 | /* If the allocation failed, give up */ |
| 2611 | if (!callchain_cpus_entries) |
| 2612 | err = -ENOMEM; |
| 2613 | goto exit; |
| 2614 | } |
| 2615 | |
| 2616 | err = alloc_callchain_buffers(); |
| 2617 | if (err) |
| 2618 | release_callchain_buffers(); |
| 2619 | exit: |
| 2620 | mutex_unlock(&callchain_mutex); |
| 2621 | |
| 2622 | return err; |
| 2623 | } |
| 2624 | |
| 2625 | static void put_callchain_buffers(void) |
| 2626 | { |
| 2627 | if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) { |
| 2628 | release_callchain_buffers(); |
| 2629 | mutex_unlock(&callchain_mutex); |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | static int get_recursion_context(int *recursion) |
| 2634 | { |
| 2635 | int rctx; |
| 2636 | |
| 2637 | if (in_nmi()) |
| 2638 | rctx = 3; |
| 2639 | else if (in_irq()) |
| 2640 | rctx = 2; |
| 2641 | else if (in_softirq()) |
| 2642 | rctx = 1; |
| 2643 | else |
| 2644 | rctx = 0; |
| 2645 | |
| 2646 | if (recursion[rctx]) |
| 2647 | return -1; |
| 2648 | |
| 2649 | recursion[rctx]++; |
| 2650 | barrier(); |
| 2651 | |
| 2652 | return rctx; |
| 2653 | } |
| 2654 | |
| 2655 | static inline void put_recursion_context(int *recursion, int rctx) |
| 2656 | { |
| 2657 | barrier(); |
| 2658 | recursion[rctx]--; |
| 2659 | } |
| 2660 | |
| 2661 | static struct perf_callchain_entry *get_callchain_entry(int *rctx) |
| 2662 | { |
| 2663 | int cpu; |
| 2664 | struct callchain_cpus_entries *entries; |
| 2665 | |
| 2666 | *rctx = get_recursion_context(__get_cpu_var(callchain_recursion)); |
| 2667 | if (*rctx == -1) |
| 2668 | return NULL; |
| 2669 | |
| 2670 | entries = rcu_dereference(callchain_cpus_entries); |
| 2671 | if (!entries) |
| 2672 | return NULL; |
| 2673 | |
| 2674 | cpu = smp_processor_id(); |
| 2675 | |
| 2676 | return &entries->cpu_entries[cpu][*rctx]; |
| 2677 | } |
| 2678 | |
| 2679 | static void |
| 2680 | put_callchain_entry(int rctx) |
| 2681 | { |
| 2682 | put_recursion_context(__get_cpu_var(callchain_recursion), rctx); |
| 2683 | } |
| 2684 | |
| 2685 | static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) |
| 2686 | { |
| 2687 | int rctx; |
| 2688 | struct perf_callchain_entry *entry; |
| 2689 | |
| 2690 | |
| 2691 | entry = get_callchain_entry(&rctx); |
| 2692 | if (rctx == -1) |
| 2693 | return NULL; |
| 2694 | |
| 2695 | if (!entry) |
| 2696 | goto exit_put; |
| 2697 | |
| 2698 | entry->nr = 0; |
| 2699 | |
| 2700 | if (!user_mode(regs)) { |
| 2701 | perf_callchain_store(entry, PERF_CONTEXT_KERNEL); |
| 2702 | perf_callchain_kernel(entry, regs); |
| 2703 | if (current->mm) |
| 2704 | regs = task_pt_regs(current); |
| 2705 | else |
| 2706 | regs = NULL; |
| 2707 | } |
| 2708 | |
| 2709 | if (regs) { |
| 2710 | perf_callchain_store(entry, PERF_CONTEXT_USER); |
| 2711 | perf_callchain_user(entry, regs); |
| 2712 | } |
| 2713 | |
| 2714 | exit_put: |
| 2715 | put_callchain_entry(rctx); |
| 2716 | |
| 2717 | return entry; |
| 2718 | } |
| 2719 | |
| 2720 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2721 | * Initialize the perf_event context in a task_struct: |
| 2722 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 2723 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2724 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2725 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2726 | mutex_init(&ctx->mutex); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2727 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 2728 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2729 | INIT_LIST_HEAD(&ctx->event_list); |
| 2730 | atomic_set(&ctx->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2731 | } |
| 2732 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 2733 | static struct perf_event_context * |
| 2734 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2735 | { |
| 2736 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 2737 | |
| 2738 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 2739 | if (!ctx) |
| 2740 | return NULL; |
| 2741 | |
| 2742 | __perf_event_init_context(ctx); |
| 2743 | if (task) { |
| 2744 | ctx->task = task; |
| 2745 | get_task_struct(task); |
| 2746 | } |
| 2747 | ctx->pmu = pmu; |
| 2748 | |
| 2749 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2750 | } |
| 2751 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 2752 | static struct task_struct * |
| 2753 | find_lively_task_by_vpid(pid_t vpid) |
| 2754 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2755 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2756 | int err; |
| 2757 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2758 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 2759 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2760 | task = current; |
| 2761 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 2762 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2763 | if (task) |
| 2764 | get_task_struct(task); |
| 2765 | rcu_read_unlock(); |
| 2766 | |
| 2767 | if (!task) |
| 2768 | return ERR_PTR(-ESRCH); |
| 2769 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2770 | /* Reuse ptrace permission checks for now. */ |
| 2771 | err = -EACCES; |
| 2772 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 2773 | goto errout; |
| 2774 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 2775 | return task; |
| 2776 | errout: |
| 2777 | put_task_struct(task); |
| 2778 | return ERR_PTR(err); |
| 2779 | |
| 2780 | } |
| 2781 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2782 | /* |
| 2783 | * Returns a matching context with refcount and pincount. |
| 2784 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2785 | static struct perf_event_context * |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 2786 | find_get_context(struct pmu *pmu, struct task_struct *task, int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2787 | { |
| 2788 | struct perf_event_context *ctx; |
| 2789 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2790 | unsigned long flags; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2791 | int ctxn, err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2792 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 2793 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2794 | /* Must be root to operate on a CPU event: */ |
| 2795 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 2796 | return ERR_PTR(-EACCES); |
| 2797 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2798 | /* |
| 2799 | * We could be clever and allow to attach a event to an |
| 2800 | * offline CPU and activate it when the CPU comes up, but |
| 2801 | * that's for later. |
| 2802 | */ |
| 2803 | if (!cpu_online(cpu)) |
| 2804 | return ERR_PTR(-ENODEV); |
| 2805 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2806 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2807 | ctx = &cpuctx->ctx; |
| 2808 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2809 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2810 | |
| 2811 | return ctx; |
| 2812 | } |
| 2813 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2814 | err = -EINVAL; |
| 2815 | ctxn = pmu->task_ctx_nr; |
| 2816 | if (ctxn < 0) |
| 2817 | goto errout; |
| 2818 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2819 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2820 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2821 | if (ctx) { |
| 2822 | unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2823 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2824 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 2825 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 2826 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2827 | err = -ENOMEM; |
| 2828 | if (!ctx) |
| 2829 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 2830 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 2831 | err = 0; |
| 2832 | mutex_lock(&task->perf_event_mutex); |
| 2833 | /* |
| 2834 | * If it has already passed perf_event_exit_task(). |
| 2835 | * we must see PF_EXITING, it takes this mutex too. |
| 2836 | */ |
| 2837 | if (task->flags & PF_EXITING) |
| 2838 | err = -ESRCH; |
| 2839 | else if (task->perf_event_ctxp[ctxn]) |
| 2840 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2841 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 2842 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2843 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 2844 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2845 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 2846 | mutex_unlock(&task->perf_event_mutex); |
| 2847 | |
| 2848 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 2849 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 2850 | |
| 2851 | if (err == -EAGAIN) |
| 2852 | goto retry; |
| 2853 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2854 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2855 | } |
| 2856 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2857 | return ctx; |
| 2858 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2859 | errout: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2860 | return ERR_PTR(err); |
| 2861 | } |
| 2862 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2863 | static void perf_event_free_filter(struct perf_event *event); |
| 2864 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2865 | static void free_event_rcu(struct rcu_head *head) |
| 2866 | { |
| 2867 | struct perf_event *event; |
| 2868 | |
| 2869 | event = container_of(head, struct perf_event, rcu_head); |
| 2870 | if (event->ns) |
| 2871 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2872 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2873 | kfree(event); |
| 2874 | } |
| 2875 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2876 | static void perf_buffer_put(struct perf_buffer *buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2877 | |
| 2878 | static void free_event(struct perf_event *event) |
| 2879 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 2880 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2881 | |
| 2882 | if (!event->parent) { |
Peter Zijlstra | 82cd6de | 2010-10-14 17:57:23 +0200 | [diff] [blame] | 2883 | if (event->attach_state & PERF_ATTACH_TASK) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2884 | jump_label_dec(&perf_sched_events); |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 2885 | if (event->attr.mmap || event->attr.mmap_data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2886 | atomic_dec(&nr_mmap_events); |
| 2887 | if (event->attr.comm) |
| 2888 | atomic_dec(&nr_comm_events); |
| 2889 | if (event->attr.task) |
| 2890 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 2891 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 2892 | put_callchain_buffers(); |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 2893 | if (is_cgroup_event(event)) { |
| 2894 | atomic_dec(&per_cpu(perf_cgroup_events, event->cpu)); |
| 2895 | jump_label_dec(&perf_sched_events); |
| 2896 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2897 | } |
| 2898 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2899 | if (event->buffer) { |
| 2900 | perf_buffer_put(event->buffer); |
| 2901 | event->buffer = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2902 | } |
| 2903 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2904 | if (is_cgroup_event(event)) |
| 2905 | perf_detach_cgroup(event); |
| 2906 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2907 | if (event->destroy) |
| 2908 | event->destroy(event); |
| 2909 | |
Peter Zijlstra | 0c67b40 | 2010-09-13 11:15:58 +0200 | [diff] [blame] | 2910 | if (event->ctx) |
| 2911 | put_ctx(event->ctx); |
| 2912 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2913 | call_rcu(&event->rcu_head, free_event_rcu); |
| 2914 | } |
| 2915 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 2916 | int perf_event_release_kernel(struct perf_event *event) |
| 2917 | { |
| 2918 | struct perf_event_context *ctx = event->ctx; |
| 2919 | |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 2920 | /* |
| 2921 | * Remove from the PMU, can't get re-enabled since we got |
| 2922 | * here because the last ref went. |
| 2923 | */ |
| 2924 | perf_event_disable(event); |
| 2925 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 2926 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 2927 | /* |
| 2928 | * There are two ways this annotation is useful: |
| 2929 | * |
| 2930 | * 1) there is a lock recursion from perf_event_exit_task |
| 2931 | * see the comment there. |
| 2932 | * |
| 2933 | * 2) there is a lock-inversion with mmap_sem through |
| 2934 | * perf_event_read_group(), which takes faults while |
| 2935 | * holding ctx->mutex, however this is called after |
| 2936 | * the last filedesc died, so there is no possibility |
| 2937 | * to trigger the AB-BA case. |
| 2938 | */ |
| 2939 | mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING); |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 2940 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 2941 | perf_group_detach(event); |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 2942 | list_del_event(event, ctx); |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 2943 | raw_spin_unlock_irq(&ctx->lock); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 2944 | mutex_unlock(&ctx->mutex); |
| 2945 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 2946 | free_event(event); |
| 2947 | |
| 2948 | return 0; |
| 2949 | } |
| 2950 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 2951 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 2952 | /* |
| 2953 | * Called when the last reference to the file is gone. |
| 2954 | */ |
| 2955 | static int perf_release(struct inode *inode, struct file *file) |
| 2956 | { |
| 2957 | struct perf_event *event = file->private_data; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 2958 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 2959 | |
| 2960 | file->private_data = NULL; |
| 2961 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 2962 | rcu_read_lock(); |
| 2963 | owner = ACCESS_ONCE(event->owner); |
| 2964 | /* |
| 2965 | * Matches the smp_wmb() in perf_event_exit_task(). If we observe |
| 2966 | * !owner it means the list deletion is complete and we can indeed |
| 2967 | * free this event, otherwise we need to serialize on |
| 2968 | * owner->perf_event_mutex. |
| 2969 | */ |
| 2970 | smp_read_barrier_depends(); |
| 2971 | if (owner) { |
| 2972 | /* |
| 2973 | * Since delayed_put_task_struct() also drops the last |
| 2974 | * task reference we can safely take a new reference |
| 2975 | * while holding the rcu_read_lock(). |
| 2976 | */ |
| 2977 | get_task_struct(owner); |
| 2978 | } |
| 2979 | rcu_read_unlock(); |
| 2980 | |
| 2981 | if (owner) { |
| 2982 | mutex_lock(&owner->perf_event_mutex); |
| 2983 | /* |
| 2984 | * We have to re-check the event->owner field, if it is cleared |
| 2985 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 2986 | * ensured they're done, and we can proceed with freeing the |
| 2987 | * event. |
| 2988 | */ |
| 2989 | if (event->owner) |
| 2990 | list_del_init(&event->owner_entry); |
| 2991 | mutex_unlock(&owner->perf_event_mutex); |
| 2992 | put_task_struct(owner); |
| 2993 | } |
| 2994 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 2995 | return perf_event_release_kernel(event); |
| 2996 | } |
| 2997 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2998 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2999 | { |
| 3000 | struct perf_event *child; |
| 3001 | u64 total = 0; |
| 3002 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3003 | *enabled = 0; |
| 3004 | *running = 0; |
| 3005 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3006 | mutex_lock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3007 | total += perf_event_read(event); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3008 | *enabled += event->total_time_enabled + |
| 3009 | atomic64_read(&event->child_total_time_enabled); |
| 3010 | *running += event->total_time_running + |
| 3011 | atomic64_read(&event->child_total_time_running); |
| 3012 | |
| 3013 | list_for_each_entry(child, &event->child_list, child_list) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3014 | total += perf_event_read(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3015 | *enabled += child->total_time_enabled; |
| 3016 | *running += child->total_time_running; |
| 3017 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3018 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3019 | |
| 3020 | return total; |
| 3021 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3022 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3023 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3024 | static int perf_event_read_group(struct perf_event *event, |
| 3025 | u64 read_format, char __user *buf) |
| 3026 | { |
| 3027 | struct perf_event *leader = event->group_leader, *sub; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3028 | int n = 0, size = 0, ret = -EFAULT; |
| 3029 | struct perf_event_context *ctx = leader->ctx; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3030 | u64 values[5]; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3031 | u64 count, enabled, running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3032 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3033 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3034 | count = perf_event_read_value(leader, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3035 | |
| 3036 | values[n++] = 1 + leader->nr_siblings; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3037 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3038 | values[n++] = enabled; |
| 3039 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3040 | values[n++] = running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3041 | values[n++] = count; |
| 3042 | if (read_format & PERF_FORMAT_ID) |
| 3043 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3044 | |
| 3045 | size = n * sizeof(u64); |
| 3046 | |
| 3047 | if (copy_to_user(buf, values, size)) |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3048 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3049 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3050 | ret = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3051 | |
| 3052 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3053 | n = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3054 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3055 | values[n++] = perf_event_read_value(sub, &enabled, &running); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3056 | if (read_format & PERF_FORMAT_ID) |
| 3057 | values[n++] = primary_event_id(sub); |
| 3058 | |
| 3059 | size = n * sizeof(u64); |
| 3060 | |
Stephane Eranian | 184d3da | 2009-11-23 21:40:49 -0800 | [diff] [blame] | 3061 | if (copy_to_user(buf + ret, values, size)) { |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3062 | ret = -EFAULT; |
| 3063 | goto unlock; |
| 3064 | } |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3065 | |
| 3066 | ret += size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3067 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3068 | unlock: |
| 3069 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3070 | |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3071 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | static int perf_event_read_one(struct perf_event *event, |
| 3075 | u64 read_format, char __user *buf) |
| 3076 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3077 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3078 | u64 values[4]; |
| 3079 | int n = 0; |
| 3080 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3081 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 3082 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3083 | values[n++] = enabled; |
| 3084 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3085 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3086 | if (read_format & PERF_FORMAT_ID) |
| 3087 | values[n++] = primary_event_id(event); |
| 3088 | |
| 3089 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 3090 | return -EFAULT; |
| 3091 | |
| 3092 | return n * sizeof(u64); |
| 3093 | } |
| 3094 | |
| 3095 | /* |
| 3096 | * Read the performance event - simple non blocking version for now |
| 3097 | */ |
| 3098 | static ssize_t |
| 3099 | perf_read_hw(struct perf_event *event, char __user *buf, size_t count) |
| 3100 | { |
| 3101 | u64 read_format = event->attr.read_format; |
| 3102 | int ret; |
| 3103 | |
| 3104 | /* |
| 3105 | * Return end-of-file for a read on a event that is in |
| 3106 | * error state (i.e. because it was pinned but it couldn't be |
| 3107 | * scheduled on to the CPU at some point). |
| 3108 | */ |
| 3109 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 3110 | return 0; |
| 3111 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 3112 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3113 | return -ENOSPC; |
| 3114 | |
| 3115 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3116 | if (read_format & PERF_FORMAT_GROUP) |
| 3117 | ret = perf_event_read_group(event, read_format, buf); |
| 3118 | else |
| 3119 | ret = perf_event_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3120 | |
| 3121 | return ret; |
| 3122 | } |
| 3123 | |
| 3124 | static ssize_t |
| 3125 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 3126 | { |
| 3127 | struct perf_event *event = file->private_data; |
| 3128 | |
| 3129 | return perf_read_hw(event, buf, count); |
| 3130 | } |
| 3131 | |
| 3132 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 3133 | { |
| 3134 | struct perf_event *event = file->private_data; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3135 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3136 | unsigned int events = POLL_HUP; |
| 3137 | |
| 3138 | rcu_read_lock(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3139 | buffer = rcu_dereference(event->buffer); |
| 3140 | if (buffer) |
| 3141 | events = atomic_xchg(&buffer->poll, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3142 | rcu_read_unlock(); |
| 3143 | |
| 3144 | poll_wait(file, &event->waitq, wait); |
| 3145 | |
| 3146 | return events; |
| 3147 | } |
| 3148 | |
| 3149 | static void perf_event_reset(struct perf_event *event) |
| 3150 | { |
| 3151 | (void)perf_event_read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3152 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3153 | perf_event_update_userpage(event); |
| 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | * Holding the top-level event's child_mutex means that any |
| 3158 | * descendant process that has inherited this event will block |
| 3159 | * in sync_child_event if it goes to exit, thus satisfying the |
| 3160 | * task existence requirements of perf_event_enable/disable. |
| 3161 | */ |
| 3162 | static void perf_event_for_each_child(struct perf_event *event, |
| 3163 | void (*func)(struct perf_event *)) |
| 3164 | { |
| 3165 | struct perf_event *child; |
| 3166 | |
| 3167 | WARN_ON_ONCE(event->ctx->parent_ctx); |
| 3168 | mutex_lock(&event->child_mutex); |
| 3169 | func(event); |
| 3170 | list_for_each_entry(child, &event->child_list, child_list) |
| 3171 | func(child); |
| 3172 | mutex_unlock(&event->child_mutex); |
| 3173 | } |
| 3174 | |
| 3175 | static void perf_event_for_each(struct perf_event *event, |
| 3176 | void (*func)(struct perf_event *)) |
| 3177 | { |
| 3178 | struct perf_event_context *ctx = event->ctx; |
| 3179 | struct perf_event *sibling; |
| 3180 | |
| 3181 | WARN_ON_ONCE(ctx->parent_ctx); |
| 3182 | mutex_lock(&ctx->mutex); |
| 3183 | event = event->group_leader; |
| 3184 | |
| 3185 | perf_event_for_each_child(event, func); |
| 3186 | func(event); |
| 3187 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
| 3188 | perf_event_for_each_child(event, func); |
| 3189 | mutex_unlock(&ctx->mutex); |
| 3190 | } |
| 3191 | |
| 3192 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 3193 | { |
| 3194 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3195 | int ret = 0; |
| 3196 | u64 value; |
| 3197 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 3198 | if (!is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3199 | return -EINVAL; |
| 3200 | |
John Blackwood | ad0cf34 | 2010-09-28 18:03:11 -0400 | [diff] [blame] | 3201 | if (copy_from_user(&value, arg, sizeof(value))) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3202 | return -EFAULT; |
| 3203 | |
| 3204 | if (!value) |
| 3205 | return -EINVAL; |
| 3206 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3207 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3208 | if (event->attr.freq) { |
| 3209 | if (value > sysctl_perf_event_sample_rate) { |
| 3210 | ret = -EINVAL; |
| 3211 | goto unlock; |
| 3212 | } |
| 3213 | |
| 3214 | event->attr.sample_freq = value; |
| 3215 | } else { |
| 3216 | event->attr.sample_period = value; |
| 3217 | event->hw.sample_period = value; |
| 3218 | } |
| 3219 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3220 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3221 | |
| 3222 | return ret; |
| 3223 | } |
| 3224 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3225 | static const struct file_operations perf_fops; |
| 3226 | |
| 3227 | static struct perf_event *perf_fget_light(int fd, int *fput_needed) |
| 3228 | { |
| 3229 | struct file *file; |
| 3230 | |
| 3231 | file = fget_light(fd, fput_needed); |
| 3232 | if (!file) |
| 3233 | return ERR_PTR(-EBADF); |
| 3234 | |
| 3235 | if (file->f_op != &perf_fops) { |
| 3236 | fput_light(file, *fput_needed); |
| 3237 | *fput_needed = 0; |
| 3238 | return ERR_PTR(-EBADF); |
| 3239 | } |
| 3240 | |
| 3241 | return file->private_data; |
| 3242 | } |
| 3243 | |
| 3244 | static int perf_event_set_output(struct perf_event *event, |
| 3245 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3246 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3247 | |
| 3248 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 3249 | { |
| 3250 | struct perf_event *event = file->private_data; |
| 3251 | void (*func)(struct perf_event *); |
| 3252 | u32 flags = arg; |
| 3253 | |
| 3254 | switch (cmd) { |
| 3255 | case PERF_EVENT_IOC_ENABLE: |
| 3256 | func = perf_event_enable; |
| 3257 | break; |
| 3258 | case PERF_EVENT_IOC_DISABLE: |
| 3259 | func = perf_event_disable; |
| 3260 | break; |
| 3261 | case PERF_EVENT_IOC_RESET: |
| 3262 | func = perf_event_reset; |
| 3263 | break; |
| 3264 | |
| 3265 | case PERF_EVENT_IOC_REFRESH: |
| 3266 | return perf_event_refresh(event, arg); |
| 3267 | |
| 3268 | case PERF_EVENT_IOC_PERIOD: |
| 3269 | return perf_event_period(event, (u64 __user *)arg); |
| 3270 | |
| 3271 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3272 | { |
| 3273 | struct perf_event *output_event = NULL; |
| 3274 | int fput_needed = 0; |
| 3275 | int ret; |
| 3276 | |
| 3277 | if (arg != -1) { |
| 3278 | output_event = perf_fget_light(arg, &fput_needed); |
| 3279 | if (IS_ERR(output_event)) |
| 3280 | return PTR_ERR(output_event); |
| 3281 | } |
| 3282 | |
| 3283 | ret = perf_event_set_output(event, output_event); |
| 3284 | if (output_event) |
| 3285 | fput_light(output_event->filp, fput_needed); |
| 3286 | |
| 3287 | return ret; |
| 3288 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3289 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3290 | case PERF_EVENT_IOC_SET_FILTER: |
| 3291 | return perf_event_set_filter(event, (void __user *)arg); |
| 3292 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3293 | default: |
| 3294 | return -ENOTTY; |
| 3295 | } |
| 3296 | |
| 3297 | if (flags & PERF_IOC_FLAG_GROUP) |
| 3298 | perf_event_for_each(event, func); |
| 3299 | else |
| 3300 | perf_event_for_each_child(event, func); |
| 3301 | |
| 3302 | return 0; |
| 3303 | } |
| 3304 | |
| 3305 | int perf_event_task_enable(void) |
| 3306 | { |
| 3307 | struct perf_event *event; |
| 3308 | |
| 3309 | mutex_lock(¤t->perf_event_mutex); |
| 3310 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3311 | perf_event_for_each_child(event, perf_event_enable); |
| 3312 | mutex_unlock(¤t->perf_event_mutex); |
| 3313 | |
| 3314 | return 0; |
| 3315 | } |
| 3316 | |
| 3317 | int perf_event_task_disable(void) |
| 3318 | { |
| 3319 | struct perf_event *event; |
| 3320 | |
| 3321 | mutex_lock(¤t->perf_event_mutex); |
| 3322 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3323 | perf_event_for_each_child(event, perf_event_disable); |
| 3324 | mutex_unlock(¤t->perf_event_mutex); |
| 3325 | |
| 3326 | return 0; |
| 3327 | } |
| 3328 | |
| 3329 | #ifndef PERF_EVENT_INDEX_OFFSET |
| 3330 | # define PERF_EVENT_INDEX_OFFSET 0 |
| 3331 | #endif |
| 3332 | |
| 3333 | static int perf_event_index(struct perf_event *event) |
| 3334 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 3335 | if (event->hw.state & PERF_HES_STOPPED) |
| 3336 | return 0; |
| 3337 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3338 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 3339 | return 0; |
| 3340 | |
| 3341 | return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET; |
| 3342 | } |
| 3343 | |
| 3344 | /* |
| 3345 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 3346 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 3347 | * code calls this from NMI context. |
| 3348 | */ |
| 3349 | void perf_event_update_userpage(struct perf_event *event) |
| 3350 | { |
| 3351 | struct perf_event_mmap_page *userpg; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3352 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3353 | |
| 3354 | rcu_read_lock(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3355 | buffer = rcu_dereference(event->buffer); |
| 3356 | if (!buffer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3357 | goto unlock; |
| 3358 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3359 | userpg = buffer->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3360 | |
| 3361 | /* |
| 3362 | * Disable preemption so as to not let the corresponding user-space |
| 3363 | * spin too long if we get preempted. |
| 3364 | */ |
| 3365 | preempt_disable(); |
| 3366 | ++userpg->lock; |
| 3367 | barrier(); |
| 3368 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3369 | userpg->offset = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3370 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3371 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3372 | |
| 3373 | userpg->time_enabled = event->total_time_enabled + |
| 3374 | atomic64_read(&event->child_total_time_enabled); |
| 3375 | |
| 3376 | userpg->time_running = event->total_time_running + |
| 3377 | atomic64_read(&event->child_total_time_running); |
| 3378 | |
| 3379 | barrier(); |
| 3380 | ++userpg->lock; |
| 3381 | preempt_enable(); |
| 3382 | unlock: |
| 3383 | rcu_read_unlock(); |
| 3384 | } |
| 3385 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3386 | static unsigned long perf_data_size(struct perf_buffer *buffer); |
| 3387 | |
| 3388 | static void |
| 3389 | perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags) |
| 3390 | { |
| 3391 | long max_size = perf_data_size(buffer); |
| 3392 | |
| 3393 | if (watermark) |
| 3394 | buffer->watermark = min(max_size, watermark); |
| 3395 | |
| 3396 | if (!buffer->watermark) |
| 3397 | buffer->watermark = max_size / 2; |
| 3398 | |
| 3399 | if (flags & PERF_BUFFER_WRITABLE) |
| 3400 | buffer->writable = 1; |
| 3401 | |
| 3402 | atomic_set(&buffer->refcount, 1); |
| 3403 | } |
| 3404 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3405 | #ifndef CONFIG_PERF_USE_VMALLOC |
| 3406 | |
| 3407 | /* |
| 3408 | * Back perf_mmap() with regular GFP_KERNEL-0 pages. |
| 3409 | */ |
| 3410 | |
| 3411 | static struct page * |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3412 | perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3413 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3414 | if (pgoff > buffer->nr_pages) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3415 | return NULL; |
| 3416 | |
| 3417 | if (pgoff == 0) |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3418 | return virt_to_page(buffer->user_page); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3419 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3420 | return virt_to_page(buffer->data_pages[pgoff - 1]); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3421 | } |
| 3422 | |
Peter Zijlstra | a19d35c | 2010-05-17 18:48:00 +0200 | [diff] [blame] | 3423 | static void *perf_mmap_alloc_page(int cpu) |
| 3424 | { |
| 3425 | struct page *page; |
| 3426 | int node; |
| 3427 | |
| 3428 | node = (cpu == -1) ? cpu : cpu_to_node(cpu); |
| 3429 | page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0); |
| 3430 | if (!page) |
| 3431 | return NULL; |
| 3432 | |
| 3433 | return page_address(page); |
| 3434 | } |
| 3435 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3436 | static struct perf_buffer * |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3437 | perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3438 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3439 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3440 | unsigned long size; |
| 3441 | int i; |
| 3442 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3443 | size = sizeof(struct perf_buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3444 | size += nr_pages * sizeof(void *); |
| 3445 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3446 | buffer = kzalloc(size, GFP_KERNEL); |
| 3447 | if (!buffer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3448 | goto fail; |
| 3449 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3450 | buffer->user_page = perf_mmap_alloc_page(cpu); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3451 | if (!buffer->user_page) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3452 | goto fail_user_page; |
| 3453 | |
| 3454 | for (i = 0; i < nr_pages; i++) { |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3455 | buffer->data_pages[i] = perf_mmap_alloc_page(cpu); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3456 | if (!buffer->data_pages[i]) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3457 | goto fail_data_pages; |
| 3458 | } |
| 3459 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3460 | buffer->nr_pages = nr_pages; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3461 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3462 | perf_buffer_init(buffer, watermark, flags); |
| 3463 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3464 | return buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3465 | |
| 3466 | fail_data_pages: |
| 3467 | for (i--; i >= 0; i--) |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3468 | free_page((unsigned long)buffer->data_pages[i]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3469 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3470 | free_page((unsigned long)buffer->user_page); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3471 | |
| 3472 | fail_user_page: |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3473 | kfree(buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3474 | |
| 3475 | fail: |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3476 | return NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | static void perf_mmap_free_page(unsigned long addr) |
| 3480 | { |
| 3481 | struct page *page = virt_to_page((void *)addr); |
| 3482 | |
| 3483 | page->mapping = NULL; |
| 3484 | __free_page(page); |
| 3485 | } |
| 3486 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3487 | static void perf_buffer_free(struct perf_buffer *buffer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3488 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3489 | int i; |
| 3490 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3491 | perf_mmap_free_page((unsigned long)buffer->user_page); |
| 3492 | for (i = 0; i < buffer->nr_pages; i++) |
| 3493 | perf_mmap_free_page((unsigned long)buffer->data_pages[i]); |
| 3494 | kfree(buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3495 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3496 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3497 | static inline int page_order(struct perf_buffer *buffer) |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 3498 | { |
| 3499 | return 0; |
| 3500 | } |
| 3501 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3502 | #else |
| 3503 | |
| 3504 | /* |
| 3505 | * Back perf_mmap() with vmalloc memory. |
| 3506 | * |
| 3507 | * Required for architectures that have d-cache aliasing issues. |
| 3508 | */ |
| 3509 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3510 | static inline int page_order(struct perf_buffer *buffer) |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 3511 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3512 | return buffer->page_order; |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 3513 | } |
| 3514 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3515 | static struct page * |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3516 | perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3517 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3518 | if (pgoff > (1UL << page_order(buffer))) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3519 | return NULL; |
| 3520 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3521 | return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3522 | } |
| 3523 | |
| 3524 | static void perf_mmap_unmark_page(void *addr) |
| 3525 | { |
| 3526 | struct page *page = vmalloc_to_page(addr); |
| 3527 | |
| 3528 | page->mapping = NULL; |
| 3529 | } |
| 3530 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3531 | static void perf_buffer_free_work(struct work_struct *work) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3532 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3533 | struct perf_buffer *buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3534 | void *base; |
| 3535 | int i, nr; |
| 3536 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3537 | buffer = container_of(work, struct perf_buffer, work); |
| 3538 | nr = 1 << page_order(buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3539 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3540 | base = buffer->user_page; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3541 | for (i = 0; i < nr + 1; i++) |
| 3542 | perf_mmap_unmark_page(base + (i * PAGE_SIZE)); |
| 3543 | |
| 3544 | vfree(base); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3545 | kfree(buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3546 | } |
| 3547 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3548 | static void perf_buffer_free(struct perf_buffer *buffer) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3549 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3550 | schedule_work(&buffer->work); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3551 | } |
| 3552 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3553 | static struct perf_buffer * |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3554 | perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3555 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3556 | struct perf_buffer *buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3557 | unsigned long size; |
| 3558 | void *all_buf; |
| 3559 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3560 | size = sizeof(struct perf_buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3561 | size += sizeof(void *); |
| 3562 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3563 | buffer = kzalloc(size, GFP_KERNEL); |
| 3564 | if (!buffer) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3565 | goto fail; |
| 3566 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3567 | INIT_WORK(&buffer->work, perf_buffer_free_work); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3568 | |
| 3569 | all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE); |
| 3570 | if (!all_buf) |
| 3571 | goto fail_all_buf; |
| 3572 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3573 | buffer->user_page = all_buf; |
| 3574 | buffer->data_pages[0] = all_buf + PAGE_SIZE; |
| 3575 | buffer->page_order = ilog2(nr_pages); |
| 3576 | buffer->nr_pages = 1; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3577 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3578 | perf_buffer_init(buffer, watermark, flags); |
| 3579 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3580 | return buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3581 | |
| 3582 | fail_all_buf: |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3583 | kfree(buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3584 | |
| 3585 | fail: |
| 3586 | return NULL; |
| 3587 | } |
| 3588 | |
| 3589 | #endif |
| 3590 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3591 | static unsigned long perf_data_size(struct perf_buffer *buffer) |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 3592 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3593 | return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer)); |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 3594 | } |
| 3595 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3596 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 3597 | { |
| 3598 | struct perf_event *event = vma->vm_file->private_data; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3599 | struct perf_buffer *buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3600 | int ret = VM_FAULT_SIGBUS; |
| 3601 | |
| 3602 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 3603 | if (vmf->pgoff == 0) |
| 3604 | ret = 0; |
| 3605 | return ret; |
| 3606 | } |
| 3607 | |
| 3608 | rcu_read_lock(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3609 | buffer = rcu_dereference(event->buffer); |
| 3610 | if (!buffer) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3611 | goto unlock; |
| 3612 | |
| 3613 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 3614 | goto unlock; |
| 3615 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3616 | vmf->page = perf_mmap_to_page(buffer, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3617 | if (!vmf->page) |
| 3618 | goto unlock; |
| 3619 | |
| 3620 | get_page(vmf->page); |
| 3621 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 3622 | vmf->page->index = vmf->pgoff; |
| 3623 | |
| 3624 | ret = 0; |
| 3625 | unlock: |
| 3626 | rcu_read_unlock(); |
| 3627 | |
| 3628 | return ret; |
| 3629 | } |
| 3630 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3631 | static void perf_buffer_free_rcu(struct rcu_head *rcu_head) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3632 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3633 | struct perf_buffer *buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3634 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3635 | buffer = container_of(rcu_head, struct perf_buffer, rcu_head); |
| 3636 | perf_buffer_free(buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3637 | } |
| 3638 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3639 | static struct perf_buffer *perf_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3640 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3641 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3642 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3643 | rcu_read_lock(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3644 | buffer = rcu_dereference(event->buffer); |
| 3645 | if (buffer) { |
| 3646 | if (!atomic_inc_not_zero(&buffer->refcount)) |
| 3647 | buffer = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3648 | } |
| 3649 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3650 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3651 | return buffer; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3652 | } |
| 3653 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3654 | static void perf_buffer_put(struct perf_buffer *buffer) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3655 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3656 | if (!atomic_dec_and_test(&buffer->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3657 | return; |
| 3658 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3659 | call_rcu(&buffer->rcu_head, perf_buffer_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 3663 | { |
| 3664 | struct perf_event *event = vma->vm_file->private_data; |
| 3665 | |
| 3666 | atomic_inc(&event->mmap_count); |
| 3667 | } |
| 3668 | |
| 3669 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 3670 | { |
| 3671 | struct perf_event *event = vma->vm_file->private_data; |
| 3672 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3673 | if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3674 | unsigned long size = perf_data_size(event->buffer); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3675 | struct user_struct *user = event->mmap_user; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3676 | struct perf_buffer *buffer = event->buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3677 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3678 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3679 | vma->vm_mm->locked_vm -= event->mmap_locked; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3680 | rcu_assign_pointer(event->buffer, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3681 | mutex_unlock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3682 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3683 | perf_buffer_put(buffer); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3684 | free_uid(user); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3685 | } |
| 3686 | } |
| 3687 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 3688 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3689 | .open = perf_mmap_open, |
| 3690 | .close = perf_mmap_close, |
| 3691 | .fault = perf_mmap_fault, |
| 3692 | .page_mkwrite = perf_mmap_fault, |
| 3693 | }; |
| 3694 | |
| 3695 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 3696 | { |
| 3697 | struct perf_event *event = file->private_data; |
| 3698 | unsigned long user_locked, user_lock_limit; |
| 3699 | struct user_struct *user = current_user(); |
| 3700 | unsigned long locked, lock_limit; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3701 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3702 | unsigned long vma_size; |
| 3703 | unsigned long nr_pages; |
| 3704 | long user_extra, extra; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3705 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3706 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 3707 | /* |
| 3708 | * Don't allow mmap() of inherited per-task counters. This would |
| 3709 | * create a performance issue due to all children writing to the |
| 3710 | * same buffer. |
| 3711 | */ |
| 3712 | if (event->cpu == -1 && event->attr.inherit) |
| 3713 | return -EINVAL; |
| 3714 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3715 | if (!(vma->vm_flags & VM_SHARED)) |
| 3716 | return -EINVAL; |
| 3717 | |
| 3718 | vma_size = vma->vm_end - vma->vm_start; |
| 3719 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 3720 | |
| 3721 | /* |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3722 | * If we have buffer pages ensure they're a power-of-two number, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3723 | * can do bitmasks instead of modulo. |
| 3724 | */ |
| 3725 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
| 3726 | return -EINVAL; |
| 3727 | |
| 3728 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 3729 | return -EINVAL; |
| 3730 | |
| 3731 | if (vma->vm_pgoff != 0) |
| 3732 | return -EINVAL; |
| 3733 | |
| 3734 | WARN_ON_ONCE(event->ctx->parent_ctx); |
| 3735 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3736 | if (event->buffer) { |
| 3737 | if (event->buffer->nr_pages == nr_pages) |
| 3738 | atomic_inc(&event->buffer->refcount); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3739 | else |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3740 | ret = -EINVAL; |
| 3741 | goto unlock; |
| 3742 | } |
| 3743 | |
| 3744 | user_extra = nr_pages + 1; |
| 3745 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 3746 | |
| 3747 | /* |
| 3748 | * Increase the limit linearly with more CPUs: |
| 3749 | */ |
| 3750 | user_lock_limit *= num_online_cpus(); |
| 3751 | |
| 3752 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 3753 | |
| 3754 | extra = 0; |
| 3755 | if (user_locked > user_lock_limit) |
| 3756 | extra = user_locked - user_lock_limit; |
| 3757 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 3758 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3759 | lock_limit >>= PAGE_SHIFT; |
| 3760 | locked = vma->vm_mm->locked_vm + extra; |
| 3761 | |
| 3762 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 3763 | !capable(CAP_IPC_LOCK)) { |
| 3764 | ret = -EPERM; |
| 3765 | goto unlock; |
| 3766 | } |
| 3767 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3768 | WARN_ON(event->buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3769 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3770 | if (vma->vm_flags & VM_WRITE) |
| 3771 | flags |= PERF_BUFFER_WRITABLE; |
| 3772 | |
| 3773 | buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark, |
| 3774 | event->cpu, flags); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3775 | if (!buffer) { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3776 | ret = -ENOMEM; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3777 | goto unlock; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3778 | } |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3779 | rcu_assign_pointer(event->buffer, buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3780 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3781 | atomic_long_add(user_extra, &user->locked_vm); |
| 3782 | event->mmap_locked = extra; |
| 3783 | event->mmap_user = get_current_user(); |
| 3784 | vma->vm_mm->locked_vm += event->mmap_locked; |
| 3785 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3786 | unlock: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3787 | if (!ret) |
| 3788 | atomic_inc(&event->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3789 | mutex_unlock(&event->mmap_mutex); |
| 3790 | |
| 3791 | vma->vm_flags |= VM_RESERVED; |
| 3792 | vma->vm_ops = &perf_mmap_vmops; |
| 3793 | |
| 3794 | return ret; |
| 3795 | } |
| 3796 | |
| 3797 | static int perf_fasync(int fd, struct file *filp, int on) |
| 3798 | { |
| 3799 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 3800 | struct perf_event *event = filp->private_data; |
| 3801 | int retval; |
| 3802 | |
| 3803 | mutex_lock(&inode->i_mutex); |
| 3804 | retval = fasync_helper(fd, filp, on, &event->fasync); |
| 3805 | mutex_unlock(&inode->i_mutex); |
| 3806 | |
| 3807 | if (retval < 0) |
| 3808 | return retval; |
| 3809 | |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
| 3813 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 3814 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3815 | .release = perf_release, |
| 3816 | .read = perf_read, |
| 3817 | .poll = perf_poll, |
| 3818 | .unlocked_ioctl = perf_ioctl, |
| 3819 | .compat_ioctl = perf_ioctl, |
| 3820 | .mmap = perf_mmap, |
| 3821 | .fasync = perf_fasync, |
| 3822 | }; |
| 3823 | |
| 3824 | /* |
| 3825 | * Perf event wakeup |
| 3826 | * |
| 3827 | * If there's data, ensure we set the poll() state and publish everything |
| 3828 | * to user-space before waking everybody up. |
| 3829 | */ |
| 3830 | |
| 3831 | void perf_event_wakeup(struct perf_event *event) |
| 3832 | { |
| 3833 | wake_up_all(&event->waitq); |
| 3834 | |
| 3835 | if (event->pending_kill) { |
| 3836 | kill_fasync(&event->fasync, SIGIO, event->pending_kill); |
| 3837 | event->pending_kill = 0; |
| 3838 | } |
| 3839 | } |
| 3840 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3841 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3842 | { |
| 3843 | struct perf_event *event = container_of(entry, |
| 3844 | struct perf_event, pending); |
| 3845 | |
| 3846 | if (event->pending_disable) { |
| 3847 | event->pending_disable = 0; |
| 3848 | __perf_event_disable(event); |
| 3849 | } |
| 3850 | |
| 3851 | if (event->pending_wakeup) { |
| 3852 | event->pending_wakeup = 0; |
| 3853 | perf_event_wakeup(event); |
| 3854 | } |
| 3855 | } |
| 3856 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3857 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 3858 | * We assume there is only KVM supporting the callbacks. |
| 3859 | * Later on, we might change it to a list if there is |
| 3860 | * another virtualization implementation supporting the callbacks. |
| 3861 | */ |
| 3862 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 3863 | |
| 3864 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 3865 | { |
| 3866 | perf_guest_cbs = cbs; |
| 3867 | return 0; |
| 3868 | } |
| 3869 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 3870 | |
| 3871 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 3872 | { |
| 3873 | perf_guest_cbs = NULL; |
| 3874 | return 0; |
| 3875 | } |
| 3876 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 3877 | |
| 3878 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3879 | * Output |
| 3880 | */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3881 | static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3882 | unsigned long offset, unsigned long head) |
| 3883 | { |
| 3884 | unsigned long mask; |
| 3885 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3886 | if (!buffer->writable) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3887 | return true; |
| 3888 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3889 | mask = perf_data_size(buffer) - 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3890 | |
| 3891 | offset = (offset - tail) & mask; |
| 3892 | head = (head - tail) & mask; |
| 3893 | |
| 3894 | if ((int)(head - offset) < 0) |
| 3895 | return false; |
| 3896 | |
| 3897 | return true; |
| 3898 | } |
| 3899 | |
| 3900 | static void perf_output_wakeup(struct perf_output_handle *handle) |
| 3901 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3902 | atomic_set(&handle->buffer->poll, POLL_IN); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3903 | |
| 3904 | if (handle->nmi) { |
| 3905 | handle->event->pending_wakeup = 1; |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3906 | irq_work_queue(&handle->event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3907 | } else |
| 3908 | perf_event_wakeup(handle->event); |
| 3909 | } |
| 3910 | |
| 3911 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3912 | * We need to ensure a later event_id doesn't publish a head when a former |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3913 | * event isn't done writing. However since we need to deal with NMIs we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3914 | * cannot fully serialize things. |
| 3915 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3916 | * We only publish the head (and generate a wakeup) when the outer-most |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3917 | * event completes. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3918 | */ |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3919 | static void perf_output_get_handle(struct perf_output_handle *handle) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3920 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3921 | struct perf_buffer *buffer = handle->buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3922 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3923 | preempt_disable(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3924 | local_inc(&buffer->nest); |
| 3925 | handle->wakeup = local_read(&buffer->wakeup); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3926 | } |
| 3927 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3928 | static void perf_output_put_handle(struct perf_output_handle *handle) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3929 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3930 | struct perf_buffer *buffer = handle->buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3931 | unsigned long head; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3932 | |
| 3933 | again: |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3934 | head = local_read(&buffer->head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3935 | |
| 3936 | /* |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3937 | * IRQ/NMI can happen here, which means we can miss a head update. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3938 | */ |
| 3939 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3940 | if (!local_dec_and_test(&buffer->nest)) |
Frederic Weisbecker | acd35a4 | 2010-05-20 21:28:34 +0200 | [diff] [blame] | 3941 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3942 | |
| 3943 | /* |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3944 | * Publish the known good head. Rely on the full barrier implied |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3945 | * by atomic_dec_and_test() order the buffer->head read and this |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3946 | * write. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3947 | */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3948 | buffer->user_page->data_head = head; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3949 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3950 | /* |
| 3951 | * Now check if we missed an update, rely on the (compiler) |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3952 | * barrier in atomic_dec_and_test() to re-read buffer->head. |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3953 | */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3954 | if (unlikely(head != local_read(&buffer->head))) { |
| 3955 | local_inc(&buffer->nest); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3956 | goto again; |
| 3957 | } |
| 3958 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3959 | if (handle->wakeup != local_read(&buffer->wakeup)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3960 | perf_output_wakeup(handle); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3961 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3962 | out: |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3963 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3964 | } |
| 3965 | |
Peter Zijlstra | a94ffaa | 2010-05-20 19:50:07 +0200 | [diff] [blame] | 3966 | __always_inline void perf_output_copy(struct perf_output_handle *handle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3967 | const void *buf, unsigned int len) |
| 3968 | { |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3969 | do { |
Peter Zijlstra | a94ffaa | 2010-05-20 19:50:07 +0200 | [diff] [blame] | 3970 | unsigned long size = min_t(unsigned long, handle->size, len); |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3971 | |
| 3972 | memcpy(handle->addr, buf, size); |
| 3973 | |
| 3974 | len -= size; |
| 3975 | handle->addr += size; |
Frederic Weisbecker | 74048f8 | 2010-05-27 21:34:58 +0200 | [diff] [blame] | 3976 | buf += size; |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3977 | handle->size -= size; |
| 3978 | if (!handle->size) { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3979 | struct perf_buffer *buffer = handle->buffer; |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 3980 | |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3981 | handle->page++; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3982 | handle->page &= buffer->nr_pages - 1; |
| 3983 | handle->addr = buffer->data_pages[handle->page]; |
| 3984 | handle->size = PAGE_SIZE << page_order(buffer); |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3985 | } |
| 3986 | } while (len); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3987 | } |
| 3988 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 3989 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 3990 | struct perf_sample_data *data, |
| 3991 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 3992 | { |
| 3993 | u64 sample_type = event->attr.sample_type; |
| 3994 | |
| 3995 | data->type = sample_type; |
| 3996 | header->size += event->id_header_size; |
| 3997 | |
| 3998 | if (sample_type & PERF_SAMPLE_TID) { |
| 3999 | /* namespace issues */ |
| 4000 | data->tid_entry.pid = perf_event_pid(event, current); |
| 4001 | data->tid_entry.tid = perf_event_tid(event, current); |
| 4002 | } |
| 4003 | |
| 4004 | if (sample_type & PERF_SAMPLE_TIME) |
| 4005 | data->time = perf_clock(); |
| 4006 | |
| 4007 | if (sample_type & PERF_SAMPLE_ID) |
| 4008 | data->id = primary_event_id(event); |
| 4009 | |
| 4010 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4011 | data->stream_id = event->id; |
| 4012 | |
| 4013 | if (sample_type & PERF_SAMPLE_CPU) { |
| 4014 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 4015 | data->cpu_entry.reserved = 0; |
| 4016 | } |
| 4017 | } |
| 4018 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4019 | static void perf_event_header__init_id(struct perf_event_header *header, |
| 4020 | struct perf_sample_data *data, |
| 4021 | struct perf_event *event) |
| 4022 | { |
| 4023 | if (event->attr.sample_id_all) |
| 4024 | __perf_event_header__init_id(header, data, event); |
| 4025 | } |
| 4026 | |
| 4027 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 4028 | struct perf_sample_data *data) |
| 4029 | { |
| 4030 | u64 sample_type = data->type; |
| 4031 | |
| 4032 | if (sample_type & PERF_SAMPLE_TID) |
| 4033 | perf_output_put(handle, data->tid_entry); |
| 4034 | |
| 4035 | if (sample_type & PERF_SAMPLE_TIME) |
| 4036 | perf_output_put(handle, data->time); |
| 4037 | |
| 4038 | if (sample_type & PERF_SAMPLE_ID) |
| 4039 | perf_output_put(handle, data->id); |
| 4040 | |
| 4041 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4042 | perf_output_put(handle, data->stream_id); |
| 4043 | |
| 4044 | if (sample_type & PERF_SAMPLE_CPU) |
| 4045 | perf_output_put(handle, data->cpu_entry); |
| 4046 | } |
| 4047 | |
| 4048 | static void perf_event__output_id_sample(struct perf_event *event, |
| 4049 | struct perf_output_handle *handle, |
| 4050 | struct perf_sample_data *sample) |
| 4051 | { |
| 4052 | if (event->attr.sample_id_all) |
| 4053 | __perf_event__output_id_sample(handle, sample); |
| 4054 | } |
| 4055 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4056 | int perf_output_begin(struct perf_output_handle *handle, |
| 4057 | struct perf_event *event, unsigned int size, |
| 4058 | int nmi, int sample) |
| 4059 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4060 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4061 | unsigned long tail, offset, head; |
| 4062 | int have_lost; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4063 | struct perf_sample_data sample_data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4064 | struct { |
| 4065 | struct perf_event_header header; |
| 4066 | u64 id; |
| 4067 | u64 lost; |
| 4068 | } lost_event; |
| 4069 | |
| 4070 | rcu_read_lock(); |
| 4071 | /* |
| 4072 | * For inherited events we send all the output towards the parent. |
| 4073 | */ |
| 4074 | if (event->parent) |
| 4075 | event = event->parent; |
| 4076 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4077 | buffer = rcu_dereference(event->buffer); |
| 4078 | if (!buffer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4079 | goto out; |
| 4080 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4081 | handle->buffer = buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4082 | handle->event = event; |
| 4083 | handle->nmi = nmi; |
| 4084 | handle->sample = sample; |
| 4085 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4086 | if (!buffer->nr_pages) |
Stephane Eranian | 00d1d0b | 2010-05-17 12:46:01 +0200 | [diff] [blame] | 4087 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4088 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4089 | have_lost = local_read(&buffer->lost); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4090 | if (have_lost) { |
| 4091 | lost_event.header.size = sizeof(lost_event); |
| 4092 | perf_event_header__init_id(&lost_event.header, &sample_data, |
| 4093 | event); |
| 4094 | size += lost_event.header.size; |
| 4095 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4096 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 4097 | perf_output_get_handle(handle); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4098 | |
| 4099 | do { |
| 4100 | /* |
| 4101 | * Userspace could choose to issue a mb() before updating the |
| 4102 | * tail pointer. So that all reads will be completed before the |
| 4103 | * write is issued. |
| 4104 | */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4105 | tail = ACCESS_ONCE(buffer->user_page->data_tail); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4106 | smp_rmb(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4107 | offset = head = local_read(&buffer->head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4108 | head += size; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4109 | if (unlikely(!perf_output_space(buffer, tail, offset, head))) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4110 | goto fail; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4111 | } while (local_cmpxchg(&buffer->head, offset, head) != offset); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4112 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4113 | if (head - local_read(&buffer->wakeup) > buffer->watermark) |
| 4114 | local_add(buffer->watermark, &buffer->wakeup); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4115 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4116 | handle->page = offset >> (PAGE_SHIFT + page_order(buffer)); |
| 4117 | handle->page &= buffer->nr_pages - 1; |
| 4118 | handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1); |
| 4119 | handle->addr = buffer->data_pages[handle->page]; |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 4120 | handle->addr += handle->size; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4121 | handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size; |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 4122 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4123 | if (have_lost) { |
| 4124 | lost_event.header.type = PERF_RECORD_LOST; |
| 4125 | lost_event.header.misc = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4126 | lost_event.id = event->id; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4127 | lost_event.lost = local_xchg(&buffer->lost, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4128 | |
| 4129 | perf_output_put(handle, lost_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4130 | perf_event__output_id_sample(event, handle, &sample_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | return 0; |
| 4134 | |
| 4135 | fail: |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4136 | local_inc(&buffer->lost); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 4137 | perf_output_put_handle(handle); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4138 | out: |
| 4139 | rcu_read_unlock(); |
| 4140 | |
| 4141 | return -ENOSPC; |
| 4142 | } |
| 4143 | |
| 4144 | void perf_output_end(struct perf_output_handle *handle) |
| 4145 | { |
| 4146 | struct perf_event *event = handle->event; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4147 | struct perf_buffer *buffer = handle->buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4148 | |
| 4149 | int wakeup_events = event->attr.wakeup_events; |
| 4150 | |
| 4151 | if (handle->sample && wakeup_events) { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4152 | int events = local_inc_return(&buffer->events); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4153 | if (events >= wakeup_events) { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 4154 | local_sub(wakeup_events, &buffer->events); |
| 4155 | local_inc(&buffer->wakeup); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4156 | } |
| 4157 | } |
| 4158 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 4159 | perf_output_put_handle(handle); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4160 | rcu_read_unlock(); |
| 4161 | } |
| 4162 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4163 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4164 | struct perf_event *event, |
| 4165 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4166 | { |
| 4167 | u64 read_format = event->attr.read_format; |
| 4168 | u64 values[4]; |
| 4169 | int n = 0; |
| 4170 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4171 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4172 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4173 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4174 | atomic64_read(&event->child_total_time_enabled); |
| 4175 | } |
| 4176 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4177 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4178 | atomic64_read(&event->child_total_time_running); |
| 4179 | } |
| 4180 | if (read_format & PERF_FORMAT_ID) |
| 4181 | values[n++] = primary_event_id(event); |
| 4182 | |
| 4183 | perf_output_copy(handle, values, n * sizeof(u64)); |
| 4184 | } |
| 4185 | |
| 4186 | /* |
| 4187 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 4188 | */ |
| 4189 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4190 | struct perf_event *event, |
| 4191 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4192 | { |
| 4193 | struct perf_event *leader = event->group_leader, *sub; |
| 4194 | u64 read_format = event->attr.read_format; |
| 4195 | u64 values[5]; |
| 4196 | int n = 0; |
| 4197 | |
| 4198 | values[n++] = 1 + leader->nr_siblings; |
| 4199 | |
| 4200 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4201 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4202 | |
| 4203 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4204 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4205 | |
| 4206 | if (leader != event) |
| 4207 | leader->pmu->read(leader); |
| 4208 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4209 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4210 | if (read_format & PERF_FORMAT_ID) |
| 4211 | values[n++] = primary_event_id(leader); |
| 4212 | |
| 4213 | perf_output_copy(handle, values, n * sizeof(u64)); |
| 4214 | |
| 4215 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 4216 | n = 0; |
| 4217 | |
| 4218 | if (sub != event) |
| 4219 | sub->pmu->read(sub); |
| 4220 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4221 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4222 | if (read_format & PERF_FORMAT_ID) |
| 4223 | values[n++] = primary_event_id(sub); |
| 4224 | |
| 4225 | perf_output_copy(handle, values, n * sizeof(u64)); |
| 4226 | } |
| 4227 | } |
| 4228 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4229 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 4230 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4231 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4232 | static void perf_output_read(struct perf_output_handle *handle, |
| 4233 | struct perf_event *event) |
| 4234 | { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4235 | u64 enabled = 0, running = 0, now, ctx_time; |
| 4236 | u64 read_format = event->attr.read_format; |
| 4237 | |
| 4238 | /* |
| 4239 | * compute total_time_enabled, total_time_running |
| 4240 | * based on snapshot values taken when the event |
| 4241 | * was last scheduled in. |
| 4242 | * |
| 4243 | * we cannot simply called update_context_time() |
| 4244 | * because of locking issue as we are called in |
| 4245 | * NMI context |
| 4246 | */ |
| 4247 | if (read_format & PERF_FORMAT_TOTAL_TIMES) { |
| 4248 | now = perf_clock(); |
| 4249 | ctx_time = event->shadow_ctx_time + now; |
| 4250 | enabled = ctx_time - event->tstamp_enabled; |
| 4251 | running = ctx_time - event->tstamp_running; |
| 4252 | } |
| 4253 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4254 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4255 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4256 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4257 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | void perf_output_sample(struct perf_output_handle *handle, |
| 4261 | struct perf_event_header *header, |
| 4262 | struct perf_sample_data *data, |
| 4263 | struct perf_event *event) |
| 4264 | { |
| 4265 | u64 sample_type = data->type; |
| 4266 | |
| 4267 | perf_output_put(handle, *header); |
| 4268 | |
| 4269 | if (sample_type & PERF_SAMPLE_IP) |
| 4270 | perf_output_put(handle, data->ip); |
| 4271 | |
| 4272 | if (sample_type & PERF_SAMPLE_TID) |
| 4273 | perf_output_put(handle, data->tid_entry); |
| 4274 | |
| 4275 | if (sample_type & PERF_SAMPLE_TIME) |
| 4276 | perf_output_put(handle, data->time); |
| 4277 | |
| 4278 | if (sample_type & PERF_SAMPLE_ADDR) |
| 4279 | perf_output_put(handle, data->addr); |
| 4280 | |
| 4281 | if (sample_type & PERF_SAMPLE_ID) |
| 4282 | perf_output_put(handle, data->id); |
| 4283 | |
| 4284 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4285 | perf_output_put(handle, data->stream_id); |
| 4286 | |
| 4287 | if (sample_type & PERF_SAMPLE_CPU) |
| 4288 | perf_output_put(handle, data->cpu_entry); |
| 4289 | |
| 4290 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 4291 | perf_output_put(handle, data->period); |
| 4292 | |
| 4293 | if (sample_type & PERF_SAMPLE_READ) |
| 4294 | perf_output_read(handle, event); |
| 4295 | |
| 4296 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4297 | if (data->callchain) { |
| 4298 | int size = 1; |
| 4299 | |
| 4300 | if (data->callchain) |
| 4301 | size += data->callchain->nr; |
| 4302 | |
| 4303 | size *= sizeof(u64); |
| 4304 | |
| 4305 | perf_output_copy(handle, data->callchain, size); |
| 4306 | } else { |
| 4307 | u64 nr = 0; |
| 4308 | perf_output_put(handle, nr); |
| 4309 | } |
| 4310 | } |
| 4311 | |
| 4312 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4313 | if (data->raw) { |
| 4314 | perf_output_put(handle, data->raw->size); |
| 4315 | perf_output_copy(handle, data->raw->data, |
| 4316 | data->raw->size); |
| 4317 | } else { |
| 4318 | struct { |
| 4319 | u32 size; |
| 4320 | u32 data; |
| 4321 | } raw = { |
| 4322 | .size = sizeof(u32), |
| 4323 | .data = 0, |
| 4324 | }; |
| 4325 | perf_output_put(handle, raw); |
| 4326 | } |
| 4327 | } |
| 4328 | } |
| 4329 | |
| 4330 | void perf_prepare_sample(struct perf_event_header *header, |
| 4331 | struct perf_sample_data *data, |
| 4332 | struct perf_event *event, |
| 4333 | struct pt_regs *regs) |
| 4334 | { |
| 4335 | u64 sample_type = event->attr.sample_type; |
| 4336 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4337 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4338 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4339 | |
| 4340 | header->misc = 0; |
| 4341 | header->misc |= perf_misc_flags(regs); |
| 4342 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4343 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4344 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4345 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4346 | data->ip = perf_instruction_pointer(regs); |
| 4347 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4348 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4349 | int size = 1; |
| 4350 | |
| 4351 | data->callchain = perf_callchain(regs); |
| 4352 | |
| 4353 | if (data->callchain) |
| 4354 | size += data->callchain->nr; |
| 4355 | |
| 4356 | header->size += size * sizeof(u64); |
| 4357 | } |
| 4358 | |
| 4359 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4360 | int size = sizeof(u32); |
| 4361 | |
| 4362 | if (data->raw) |
| 4363 | size += data->raw->size; |
| 4364 | else |
| 4365 | size += sizeof(u32); |
| 4366 | |
| 4367 | WARN_ON_ONCE(size & (sizeof(u64)-1)); |
| 4368 | header->size += size; |
| 4369 | } |
| 4370 | } |
| 4371 | |
| 4372 | static void perf_event_output(struct perf_event *event, int nmi, |
| 4373 | struct perf_sample_data *data, |
| 4374 | struct pt_regs *regs) |
| 4375 | { |
| 4376 | struct perf_output_handle handle; |
| 4377 | struct perf_event_header header; |
| 4378 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4379 | /* protect the callchain buffers */ |
| 4380 | rcu_read_lock(); |
| 4381 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4382 | perf_prepare_sample(&header, data, event, regs); |
| 4383 | |
| 4384 | if (perf_output_begin(&handle, event, header.size, nmi, 1)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4385 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4386 | |
| 4387 | perf_output_sample(&handle, &header, data, event); |
| 4388 | |
| 4389 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4390 | |
| 4391 | exit: |
| 4392 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | /* |
| 4396 | * read event_id |
| 4397 | */ |
| 4398 | |
| 4399 | struct perf_read_event { |
| 4400 | struct perf_event_header header; |
| 4401 | |
| 4402 | u32 pid; |
| 4403 | u32 tid; |
| 4404 | }; |
| 4405 | |
| 4406 | static void |
| 4407 | perf_event_read_event(struct perf_event *event, |
| 4408 | struct task_struct *task) |
| 4409 | { |
| 4410 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4411 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4412 | struct perf_read_event read_event = { |
| 4413 | .header = { |
| 4414 | .type = PERF_RECORD_READ, |
| 4415 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4416 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4417 | }, |
| 4418 | .pid = perf_event_pid(event, task), |
| 4419 | .tid = perf_event_tid(event, task), |
| 4420 | }; |
| 4421 | int ret; |
| 4422 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4423 | perf_event_header__init_id(&read_event.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4424 | ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0); |
| 4425 | if (ret) |
| 4426 | return; |
| 4427 | |
| 4428 | perf_output_put(&handle, read_event); |
| 4429 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4430 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4431 | |
| 4432 | perf_output_end(&handle); |
| 4433 | } |
| 4434 | |
| 4435 | /* |
| 4436 | * task tracking -- fork/exit |
| 4437 | * |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4438 | * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4439 | */ |
| 4440 | |
| 4441 | struct perf_task_event { |
| 4442 | struct task_struct *task; |
| 4443 | struct perf_event_context *task_ctx; |
| 4444 | |
| 4445 | struct { |
| 4446 | struct perf_event_header header; |
| 4447 | |
| 4448 | u32 pid; |
| 4449 | u32 ppid; |
| 4450 | u32 tid; |
| 4451 | u32 ptid; |
| 4452 | u64 time; |
| 4453 | } event_id; |
| 4454 | }; |
| 4455 | |
| 4456 | static void perf_event_task_output(struct perf_event *event, |
| 4457 | struct perf_task_event *task_event) |
| 4458 | { |
| 4459 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4460 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4461 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4462 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 4463 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4464 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4465 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4466 | ret = perf_output_begin(&handle, event, |
| 4467 | task_event->event_id.header.size, 0, 0); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 4468 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4469 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4470 | |
| 4471 | task_event->event_id.pid = perf_event_pid(event, task); |
| 4472 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 4473 | |
| 4474 | task_event->event_id.tid = perf_event_tid(event, task); |
| 4475 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 4476 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4477 | perf_output_put(&handle, task_event->event_id); |
| 4478 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4479 | perf_event__output_id_sample(event, &handle, &sample); |
| 4480 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4481 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4482 | out: |
| 4483 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | static int perf_event_task_match(struct perf_event *event) |
| 4487 | { |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 4488 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 22e1908 | 2010-01-18 09:12:32 +0100 | [diff] [blame] | 4489 | return 0; |
| 4490 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 4491 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 4492 | return 0; |
| 4493 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4494 | if (event->attr.comm || event->attr.mmap || |
| 4495 | event->attr.mmap_data || event->attr.task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4496 | return 1; |
| 4497 | |
| 4498 | return 0; |
| 4499 | } |
| 4500 | |
| 4501 | static void perf_event_task_ctx(struct perf_event_context *ctx, |
| 4502 | struct perf_task_event *task_event) |
| 4503 | { |
| 4504 | struct perf_event *event; |
| 4505 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4506 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 4507 | if (perf_event_task_match(event)) |
| 4508 | perf_event_task_output(event, task_event); |
| 4509 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4510 | } |
| 4511 | |
| 4512 | static void perf_event_task_event(struct perf_task_event *task_event) |
| 4513 | { |
| 4514 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4515 | struct perf_event_context *ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4516 | struct pmu *pmu; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4517 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4518 | |
Peter Zijlstra | d6ff86c | 2009-11-20 22:19:46 +0100 | [diff] [blame] | 4519 | rcu_read_lock(); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4520 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 41945f6 | 2010-09-16 19:17:24 +0200 | [diff] [blame] | 4521 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 4522 | if (cpuctx->active_pmu != pmu) |
| 4523 | goto next; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4524 | perf_event_task_ctx(&cpuctx->ctx, task_event); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4525 | |
| 4526 | ctx = task_event->task_ctx; |
| 4527 | if (!ctx) { |
| 4528 | ctxn = pmu->task_ctx_nr; |
| 4529 | if (ctxn < 0) |
Peter Zijlstra | 41945f6 | 2010-09-16 19:17:24 +0200 | [diff] [blame] | 4530 | goto next; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4531 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 4532 | } |
| 4533 | if (ctx) |
| 4534 | perf_event_task_ctx(ctx, task_event); |
Peter Zijlstra | 41945f6 | 2010-09-16 19:17:24 +0200 | [diff] [blame] | 4535 | next: |
| 4536 | put_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4537 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4538 | rcu_read_unlock(); |
| 4539 | } |
| 4540 | |
| 4541 | static void perf_event_task(struct task_struct *task, |
| 4542 | struct perf_event_context *task_ctx, |
| 4543 | int new) |
| 4544 | { |
| 4545 | struct perf_task_event task_event; |
| 4546 | |
| 4547 | if (!atomic_read(&nr_comm_events) && |
| 4548 | !atomic_read(&nr_mmap_events) && |
| 4549 | !atomic_read(&nr_task_events)) |
| 4550 | return; |
| 4551 | |
| 4552 | task_event = (struct perf_task_event){ |
| 4553 | .task = task, |
| 4554 | .task_ctx = task_ctx, |
| 4555 | .event_id = { |
| 4556 | .header = { |
| 4557 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 4558 | .misc = 0, |
| 4559 | .size = sizeof(task_event.event_id), |
| 4560 | }, |
| 4561 | /* .pid */ |
| 4562 | /* .ppid */ |
| 4563 | /* .tid */ |
| 4564 | /* .ptid */ |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 4565 | .time = perf_clock(), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4566 | }, |
| 4567 | }; |
| 4568 | |
| 4569 | perf_event_task_event(&task_event); |
| 4570 | } |
| 4571 | |
| 4572 | void perf_event_fork(struct task_struct *task) |
| 4573 | { |
| 4574 | perf_event_task(task, NULL, 1); |
| 4575 | } |
| 4576 | |
| 4577 | /* |
| 4578 | * comm tracking |
| 4579 | */ |
| 4580 | |
| 4581 | struct perf_comm_event { |
| 4582 | struct task_struct *task; |
| 4583 | char *comm; |
| 4584 | int comm_size; |
| 4585 | |
| 4586 | struct { |
| 4587 | struct perf_event_header header; |
| 4588 | |
| 4589 | u32 pid; |
| 4590 | u32 tid; |
| 4591 | } event_id; |
| 4592 | }; |
| 4593 | |
| 4594 | static void perf_event_comm_output(struct perf_event *event, |
| 4595 | struct perf_comm_event *comm_event) |
| 4596 | { |
| 4597 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4598 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4599 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4600 | int ret; |
| 4601 | |
| 4602 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 4603 | ret = perf_output_begin(&handle, event, |
| 4604 | comm_event->event_id.header.size, 0, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4605 | |
| 4606 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4607 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4608 | |
| 4609 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 4610 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 4611 | |
| 4612 | perf_output_put(&handle, comm_event->event_id); |
| 4613 | perf_output_copy(&handle, comm_event->comm, |
| 4614 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4615 | |
| 4616 | perf_event__output_id_sample(event, &handle, &sample); |
| 4617 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4618 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4619 | out: |
| 4620 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | static int perf_event_comm_match(struct perf_event *event) |
| 4624 | { |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 4625 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 22e1908 | 2010-01-18 09:12:32 +0100 | [diff] [blame] | 4626 | return 0; |
| 4627 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 4628 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 4629 | return 0; |
| 4630 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4631 | if (event->attr.comm) |
| 4632 | return 1; |
| 4633 | |
| 4634 | return 0; |
| 4635 | } |
| 4636 | |
| 4637 | static void perf_event_comm_ctx(struct perf_event_context *ctx, |
| 4638 | struct perf_comm_event *comm_event) |
| 4639 | { |
| 4640 | struct perf_event *event; |
| 4641 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4642 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 4643 | if (perf_event_comm_match(event)) |
| 4644 | perf_event_comm_output(event, comm_event); |
| 4645 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 4649 | { |
| 4650 | struct perf_cpu_context *cpuctx; |
| 4651 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4652 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4653 | unsigned int size; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4654 | struct pmu *pmu; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4655 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4656 | |
| 4657 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 4658 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4659 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 4660 | |
| 4661 | comm_event->comm = comm; |
| 4662 | comm_event->comm_size = size; |
| 4663 | |
| 4664 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | f6595f3 | 2009-11-20 22:19:47 +0100 | [diff] [blame] | 4665 | rcu_read_lock(); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4666 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 41945f6 | 2010-09-16 19:17:24 +0200 | [diff] [blame] | 4667 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 4668 | if (cpuctx->active_pmu != pmu) |
| 4669 | goto next; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4670 | perf_event_comm_ctx(&cpuctx->ctx, comm_event); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4671 | |
| 4672 | ctxn = pmu->task_ctx_nr; |
| 4673 | if (ctxn < 0) |
Peter Zijlstra | 41945f6 | 2010-09-16 19:17:24 +0200 | [diff] [blame] | 4674 | goto next; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4675 | |
| 4676 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 4677 | if (ctx) |
| 4678 | perf_event_comm_ctx(ctx, comm_event); |
Peter Zijlstra | 41945f6 | 2010-09-16 19:17:24 +0200 | [diff] [blame] | 4679 | next: |
| 4680 | put_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4681 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4682 | rcu_read_unlock(); |
| 4683 | } |
| 4684 | |
| 4685 | void perf_event_comm(struct task_struct *task) |
| 4686 | { |
| 4687 | struct perf_comm_event comm_event; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4688 | struct perf_event_context *ctx; |
| 4689 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4690 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4691 | for_each_task_context_nr(ctxn) { |
| 4692 | ctx = task->perf_event_ctxp[ctxn]; |
| 4693 | if (!ctx) |
| 4694 | continue; |
| 4695 | |
| 4696 | perf_event_enable_on_exec(ctx); |
| 4697 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4698 | |
| 4699 | if (!atomic_read(&nr_comm_events)) |
| 4700 | return; |
| 4701 | |
| 4702 | comm_event = (struct perf_comm_event){ |
| 4703 | .task = task, |
| 4704 | /* .comm */ |
| 4705 | /* .comm_size */ |
| 4706 | .event_id = { |
| 4707 | .header = { |
| 4708 | .type = PERF_RECORD_COMM, |
| 4709 | .misc = 0, |
| 4710 | /* .size */ |
| 4711 | }, |
| 4712 | /* .pid */ |
| 4713 | /* .tid */ |
| 4714 | }, |
| 4715 | }; |
| 4716 | |
| 4717 | perf_event_comm_event(&comm_event); |
| 4718 | } |
| 4719 | |
| 4720 | /* |
| 4721 | * mmap tracking |
| 4722 | */ |
| 4723 | |
| 4724 | struct perf_mmap_event { |
| 4725 | struct vm_area_struct *vma; |
| 4726 | |
| 4727 | const char *file_name; |
| 4728 | int file_size; |
| 4729 | |
| 4730 | struct { |
| 4731 | struct perf_event_header header; |
| 4732 | |
| 4733 | u32 pid; |
| 4734 | u32 tid; |
| 4735 | u64 start; |
| 4736 | u64 len; |
| 4737 | u64 pgoff; |
| 4738 | } event_id; |
| 4739 | }; |
| 4740 | |
| 4741 | static void perf_event_mmap_output(struct perf_event *event, |
| 4742 | struct perf_mmap_event *mmap_event) |
| 4743 | { |
| 4744 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4745 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4746 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4747 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4748 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4749 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 4750 | ret = perf_output_begin(&handle, event, |
| 4751 | mmap_event->event_id.header.size, 0, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4752 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4753 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4754 | |
| 4755 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 4756 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 4757 | |
| 4758 | perf_output_put(&handle, mmap_event->event_id); |
| 4759 | perf_output_copy(&handle, mmap_event->file_name, |
| 4760 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4761 | |
| 4762 | perf_event__output_id_sample(event, &handle, &sample); |
| 4763 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4764 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4765 | out: |
| 4766 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | static int perf_event_mmap_match(struct perf_event *event, |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4770 | struct perf_mmap_event *mmap_event, |
| 4771 | int executable) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4772 | { |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 4773 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 22e1908 | 2010-01-18 09:12:32 +0100 | [diff] [blame] | 4774 | return 0; |
| 4775 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 4776 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 4777 | return 0; |
| 4778 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4779 | if ((!executable && event->attr.mmap_data) || |
| 4780 | (executable && event->attr.mmap)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4781 | return 1; |
| 4782 | |
| 4783 | return 0; |
| 4784 | } |
| 4785 | |
| 4786 | static void perf_event_mmap_ctx(struct perf_event_context *ctx, |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4787 | struct perf_mmap_event *mmap_event, |
| 4788 | int executable) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4789 | { |
| 4790 | struct perf_event *event; |
| 4791 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4792 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4793 | if (perf_event_mmap_match(event, mmap_event, executable)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4794 | perf_event_mmap_output(event, mmap_event); |
| 4795 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 4799 | { |
| 4800 | struct perf_cpu_context *cpuctx; |
| 4801 | struct perf_event_context *ctx; |
| 4802 | struct vm_area_struct *vma = mmap_event->vma; |
| 4803 | struct file *file = vma->vm_file; |
| 4804 | unsigned int size; |
| 4805 | char tmp[16]; |
| 4806 | char *buf = NULL; |
| 4807 | const char *name; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4808 | struct pmu *pmu; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4809 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4810 | |
| 4811 | memset(tmp, 0, sizeof(tmp)); |
| 4812 | |
| 4813 | if (file) { |
| 4814 | /* |
| 4815 | * d_path works from the end of the buffer backwards, so we |
| 4816 | * need to add enough zero bytes after the string to handle |
| 4817 | * the 64bit alignment we do later. |
| 4818 | */ |
| 4819 | buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL); |
| 4820 | if (!buf) { |
| 4821 | name = strncpy(tmp, "//enomem", sizeof(tmp)); |
| 4822 | goto got_name; |
| 4823 | } |
| 4824 | name = d_path(&file->f_path, buf, PATH_MAX); |
| 4825 | if (IS_ERR(name)) { |
| 4826 | name = strncpy(tmp, "//toolong", sizeof(tmp)); |
| 4827 | goto got_name; |
| 4828 | } |
| 4829 | } else { |
| 4830 | if (arch_vma_name(mmap_event->vma)) { |
| 4831 | name = strncpy(tmp, arch_vma_name(mmap_event->vma), |
| 4832 | sizeof(tmp)); |
| 4833 | goto got_name; |
| 4834 | } |
| 4835 | |
| 4836 | if (!vma->vm_mm) { |
| 4837 | name = strncpy(tmp, "[vdso]", sizeof(tmp)); |
| 4838 | goto got_name; |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4839 | } else if (vma->vm_start <= vma->vm_mm->start_brk && |
| 4840 | vma->vm_end >= vma->vm_mm->brk) { |
| 4841 | name = strncpy(tmp, "[heap]", sizeof(tmp)); |
| 4842 | goto got_name; |
| 4843 | } else if (vma->vm_start <= vma->vm_mm->start_stack && |
| 4844 | vma->vm_end >= vma->vm_mm->start_stack) { |
| 4845 | name = strncpy(tmp, "[stack]", sizeof(tmp)); |
| 4846 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | name = strncpy(tmp, "//anon", sizeof(tmp)); |
| 4850 | goto got_name; |
| 4851 | } |
| 4852 | |
| 4853 | got_name: |
| 4854 | size = ALIGN(strlen(name)+1, sizeof(u64)); |
| 4855 | |
| 4856 | mmap_event->file_name = name; |
| 4857 | mmap_event->file_size = size; |
| 4858 | |
| 4859 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 4860 | |
Peter Zijlstra | f6d9dd2 | 2009-11-20 22:19:48 +0100 | [diff] [blame] | 4861 | rcu_read_lock(); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4862 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 41945f6 | 2010-09-16 19:17:24 +0200 | [diff] [blame] | 4863 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 4864 | if (cpuctx->active_pmu != pmu) |
| 4865 | goto next; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4866 | perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, |
| 4867 | vma->vm_flags & VM_EXEC); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4868 | |
| 4869 | ctxn = pmu->task_ctx_nr; |
| 4870 | if (ctxn < 0) |
Peter Zijlstra | 41945f6 | 2010-09-16 19:17:24 +0200 | [diff] [blame] | 4871 | goto next; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4872 | |
| 4873 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 4874 | if (ctx) { |
| 4875 | perf_event_mmap_ctx(ctx, mmap_event, |
| 4876 | vma->vm_flags & VM_EXEC); |
| 4877 | } |
Peter Zijlstra | 41945f6 | 2010-09-16 19:17:24 +0200 | [diff] [blame] | 4878 | next: |
| 4879 | put_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 4880 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4881 | rcu_read_unlock(); |
| 4882 | |
| 4883 | kfree(buf); |
| 4884 | } |
| 4885 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4886 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4887 | { |
| 4888 | struct perf_mmap_event mmap_event; |
| 4889 | |
| 4890 | if (!atomic_read(&nr_mmap_events)) |
| 4891 | return; |
| 4892 | |
| 4893 | mmap_event = (struct perf_mmap_event){ |
| 4894 | .vma = vma, |
| 4895 | /* .file_name */ |
| 4896 | /* .file_size */ |
| 4897 | .event_id = { |
| 4898 | .header = { |
| 4899 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 4900 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4901 | /* .size */ |
| 4902 | }, |
| 4903 | /* .pid */ |
| 4904 | /* .tid */ |
| 4905 | .start = vma->vm_start, |
| 4906 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 4907 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4908 | }, |
| 4909 | }; |
| 4910 | |
| 4911 | perf_event_mmap_event(&mmap_event); |
| 4912 | } |
| 4913 | |
| 4914 | /* |
| 4915 | * IRQ throttle logging |
| 4916 | */ |
| 4917 | |
| 4918 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 4919 | { |
| 4920 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4921 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4922 | int ret; |
| 4923 | |
| 4924 | struct { |
| 4925 | struct perf_event_header header; |
| 4926 | u64 time; |
| 4927 | u64 id; |
| 4928 | u64 stream_id; |
| 4929 | } throttle_event = { |
| 4930 | .header = { |
| 4931 | .type = PERF_RECORD_THROTTLE, |
| 4932 | .misc = 0, |
| 4933 | .size = sizeof(throttle_event), |
| 4934 | }, |
| 4935 | .time = perf_clock(), |
| 4936 | .id = primary_event_id(event), |
| 4937 | .stream_id = event->id, |
| 4938 | }; |
| 4939 | |
| 4940 | if (enable) |
| 4941 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 4942 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4943 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 4944 | |
| 4945 | ret = perf_output_begin(&handle, event, |
| 4946 | throttle_event.header.size, 1, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4947 | if (ret) |
| 4948 | return; |
| 4949 | |
| 4950 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4951 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4952 | perf_output_end(&handle); |
| 4953 | } |
| 4954 | |
| 4955 | /* |
| 4956 | * Generic event overflow handling, sampling. |
| 4957 | */ |
| 4958 | |
| 4959 | static int __perf_event_overflow(struct perf_event *event, int nmi, |
| 4960 | int throttle, struct perf_sample_data *data, |
| 4961 | struct pt_regs *regs) |
| 4962 | { |
| 4963 | int events = atomic_read(&event->event_limit); |
| 4964 | struct hw_perf_event *hwc = &event->hw; |
| 4965 | int ret = 0; |
| 4966 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 4967 | /* |
| 4968 | * Non-sampling counters might still use the PMI to fold short |
| 4969 | * hardware counters, ignore those. |
| 4970 | */ |
| 4971 | if (unlikely(!is_sampling_event(event))) |
| 4972 | return 0; |
| 4973 | |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 4974 | if (unlikely(hwc->interrupts >= max_samples_per_tick)) { |
| 4975 | if (throttle) { |
| 4976 | hwc->interrupts = MAX_INTERRUPTS; |
| 4977 | perf_log_throttle(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4978 | ret = 1; |
| 4979 | } |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 4980 | } else |
| 4981 | hwc->interrupts++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4982 | |
| 4983 | if (event->attr.freq) { |
| 4984 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 4985 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4986 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 4987 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4988 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 4989 | if (delta > 0 && delta < 2*TICK_NSEC) |
| 4990 | perf_adjust_period(event, delta, hwc->last_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | /* |
| 4994 | * XXX event_limit might not quite work as expected on inherited |
| 4995 | * events |
| 4996 | */ |
| 4997 | |
| 4998 | event->pending_kill = POLL_IN; |
| 4999 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 5000 | ret = 1; |
| 5001 | event->pending_kill = POLL_HUP; |
| 5002 | if (nmi) { |
| 5003 | event->pending_disable = 1; |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 5004 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5005 | } else |
| 5006 | perf_event_disable(event); |
| 5007 | } |
| 5008 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5009 | if (event->overflow_handler) |
| 5010 | event->overflow_handler(event, nmi, data, regs); |
| 5011 | else |
| 5012 | perf_event_output(event, nmi, data, regs); |
| 5013 | |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5014 | if (event->fasync && event->pending_kill) { |
| 5015 | if (nmi) { |
| 5016 | event->pending_wakeup = 1; |
| 5017 | irq_work_queue(&event->pending); |
| 5018 | } else |
| 5019 | perf_event_wakeup(event); |
| 5020 | } |
| 5021 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5022 | return ret; |
| 5023 | } |
| 5024 | |
| 5025 | int perf_event_overflow(struct perf_event *event, int nmi, |
| 5026 | struct perf_sample_data *data, |
| 5027 | struct pt_regs *regs) |
| 5028 | { |
| 5029 | return __perf_event_overflow(event, nmi, 1, data, regs); |
| 5030 | } |
| 5031 | |
| 5032 | /* |
| 5033 | * Generic software event infrastructure |
| 5034 | */ |
| 5035 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5036 | struct swevent_htable { |
| 5037 | struct swevent_hlist *swevent_hlist; |
| 5038 | struct mutex hlist_mutex; |
| 5039 | int hlist_refcount; |
| 5040 | |
| 5041 | /* Recursion avoidance in each contexts */ |
| 5042 | int recursion[PERF_NR_CONTEXTS]; |
| 5043 | }; |
| 5044 | |
| 5045 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 5046 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5047 | /* |
| 5048 | * We directly increment event->count and keep a second value in |
| 5049 | * event->hw.period_left to count intervals. This period event |
| 5050 | * is kept in the range [-sample_period, 0] so that we can use the |
| 5051 | * sign as trigger. |
| 5052 | */ |
| 5053 | |
| 5054 | static u64 perf_swevent_set_period(struct perf_event *event) |
| 5055 | { |
| 5056 | struct hw_perf_event *hwc = &event->hw; |
| 5057 | u64 period = hwc->last_period; |
| 5058 | u64 nr, offset; |
| 5059 | s64 old, val; |
| 5060 | |
| 5061 | hwc->last_period = hwc->sample_period; |
| 5062 | |
| 5063 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5064 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5065 | if (val < 0) |
| 5066 | return 0; |
| 5067 | |
| 5068 | nr = div64_u64(period + val, period); |
| 5069 | offset = nr * period; |
| 5070 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5071 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5072 | goto again; |
| 5073 | |
| 5074 | return nr; |
| 5075 | } |
| 5076 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5077 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5078 | int nmi, struct perf_sample_data *data, |
| 5079 | struct pt_regs *regs) |
| 5080 | { |
| 5081 | struct hw_perf_event *hwc = &event->hw; |
| 5082 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5083 | |
| 5084 | data->period = event->hw.last_period; |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5085 | if (!overflow) |
| 5086 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5087 | |
| 5088 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 5089 | return; |
| 5090 | |
| 5091 | for (; overflow; overflow--) { |
| 5092 | if (__perf_event_overflow(event, nmi, throttle, |
| 5093 | data, regs)) { |
| 5094 | /* |
| 5095 | * We inhibit the overflow from happening when |
| 5096 | * hwc->interrupts == MAX_INTERRUPTS. |
| 5097 | */ |
| 5098 | break; |
| 5099 | } |
| 5100 | throttle = 1; |
| 5101 | } |
| 5102 | } |
| 5103 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5104 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5105 | int nmi, struct perf_sample_data *data, |
| 5106 | struct pt_regs *regs) |
| 5107 | { |
| 5108 | struct hw_perf_event *hwc = &event->hw; |
| 5109 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5110 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5111 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5112 | if (!regs) |
| 5113 | return; |
| 5114 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5115 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5116 | return; |
| 5117 | |
| 5118 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
| 5119 | return perf_swevent_overflow(event, 1, nmi, data, regs); |
| 5120 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5121 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5122 | return; |
| 5123 | |
| 5124 | perf_swevent_overflow(event, 0, nmi, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5125 | } |
| 5126 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5127 | static int perf_exclude_event(struct perf_event *event, |
| 5128 | struct pt_regs *regs) |
| 5129 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5130 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 5131 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5132 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5133 | if (regs) { |
| 5134 | if (event->attr.exclude_user && user_mode(regs)) |
| 5135 | return 1; |
| 5136 | |
| 5137 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 5138 | return 1; |
| 5139 | } |
| 5140 | |
| 5141 | return 0; |
| 5142 | } |
| 5143 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5144 | static int perf_swevent_match(struct perf_event *event, |
| 5145 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5146 | u32 event_id, |
| 5147 | struct perf_sample_data *data, |
| 5148 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5149 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5150 | if (event->attr.type != type) |
| 5151 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5152 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5153 | if (event->attr.config != event_id) |
| 5154 | return 0; |
| 5155 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5156 | if (perf_exclude_event(event, regs)) |
| 5157 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5158 | |
| 5159 | return 1; |
| 5160 | } |
| 5161 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5162 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5163 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5164 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5165 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5166 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 5167 | } |
| 5168 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5169 | static inline struct hlist_head * |
| 5170 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5171 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5172 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5173 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5174 | return &hlist->heads[hash]; |
| 5175 | } |
| 5176 | |
| 5177 | /* For the read side: events when they trigger */ |
| 5178 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5179 | find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5180 | { |
| 5181 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5182 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5183 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5184 | if (!hlist) |
| 5185 | return NULL; |
| 5186 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5187 | return __find_swevent_head(hlist, type, event_id); |
| 5188 | } |
| 5189 | |
| 5190 | /* For the event head insertion and removal in the hlist */ |
| 5191 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5192 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5193 | { |
| 5194 | struct swevent_hlist *hlist; |
| 5195 | u32 event_id = event->attr.config; |
| 5196 | u64 type = event->attr.type; |
| 5197 | |
| 5198 | /* |
| 5199 | * Event scheduling is always serialized against hlist allocation |
| 5200 | * and release. Which makes the protected version suitable here. |
| 5201 | * The context lock guarantees that. |
| 5202 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5203 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5204 | lockdep_is_held(&event->ctx->lock)); |
| 5205 | if (!hlist) |
| 5206 | return NULL; |
| 5207 | |
| 5208 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, |
| 5212 | u64 nr, int nmi, |
| 5213 | struct perf_sample_data *data, |
| 5214 | struct pt_regs *regs) |
| 5215 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5216 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5217 | struct perf_event *event; |
| 5218 | struct hlist_node *node; |
| 5219 | struct hlist_head *head; |
| 5220 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5221 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5222 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5223 | if (!head) |
| 5224 | goto end; |
| 5225 | |
| 5226 | hlist_for_each_entry_rcu(event, node, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5227 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5228 | perf_swevent_event(event, nr, nmi, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5229 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5230 | end: |
| 5231 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5232 | } |
| 5233 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5234 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5235 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5236 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5237 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5238 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5239 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 5240 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5241 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 5242 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5243 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5244 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5245 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5246 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5247 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5248 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5249 | void __perf_sw_event(u32 event_id, u64 nr, int nmi, |
| 5250 | struct pt_regs *regs, u64 addr) |
| 5251 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5252 | struct perf_sample_data data; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5253 | int rctx; |
| 5254 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5255 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5256 | rctx = perf_swevent_get_recursion_context(); |
| 5257 | if (rctx < 0) |
| 5258 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5259 | |
Peter Zijlstra | dc1d628 | 2010-03-03 15:55:04 +0100 | [diff] [blame] | 5260 | perf_sample_data_init(&data, addr); |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5261 | |
| 5262 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5263 | |
| 5264 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5265 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5266 | } |
| 5267 | |
| 5268 | static void perf_swevent_read(struct perf_event *event) |
| 5269 | { |
| 5270 | } |
| 5271 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5272 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5273 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5274 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5275 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5276 | struct hlist_head *head; |
| 5277 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5278 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5279 | hwc->last_period = hwc->sample_period; |
| 5280 | perf_swevent_set_period(event); |
| 5281 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5282 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5283 | hwc->state = !(flags & PERF_EF_START); |
| 5284 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5285 | head = find_swevent_head(swhash, event); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5286 | if (WARN_ON_ONCE(!head)) |
| 5287 | return -EINVAL; |
| 5288 | |
| 5289 | hlist_add_head_rcu(&event->hlist_entry, head); |
| 5290 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5291 | return 0; |
| 5292 | } |
| 5293 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5294 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5295 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5296 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5297 | } |
| 5298 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5299 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5300 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5301 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5302 | } |
| 5303 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5304 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5305 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5306 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5307 | } |
| 5308 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5309 | /* Deref the hlist from the update side */ |
| 5310 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5311 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5312 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5313 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 5314 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5315 | } |
| 5316 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5317 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5318 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5319 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5320 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5321 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5322 | return; |
| 5323 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5324 | rcu_assign_pointer(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 5325 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5326 | } |
| 5327 | |
| 5328 | static void swevent_hlist_put_cpu(struct perf_event *event, int cpu) |
| 5329 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5330 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5331 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5332 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5333 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5334 | if (!--swhash->hlist_refcount) |
| 5335 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5336 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5337 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5338 | } |
| 5339 | |
| 5340 | static void swevent_hlist_put(struct perf_event *event) |
| 5341 | { |
| 5342 | int cpu; |
| 5343 | |
| 5344 | if (event->cpu != -1) { |
| 5345 | swevent_hlist_put_cpu(event, event->cpu); |
| 5346 | return; |
| 5347 | } |
| 5348 | |
| 5349 | for_each_possible_cpu(cpu) |
| 5350 | swevent_hlist_put_cpu(event, cpu); |
| 5351 | } |
| 5352 | |
| 5353 | static int swevent_hlist_get_cpu(struct perf_event *event, int cpu) |
| 5354 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5355 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5356 | int err = 0; |
| 5357 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5358 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5359 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5360 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5361 | struct swevent_hlist *hlist; |
| 5362 | |
| 5363 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 5364 | if (!hlist) { |
| 5365 | err = -ENOMEM; |
| 5366 | goto exit; |
| 5367 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5368 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5369 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5370 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 5371 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5372 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5373 | |
| 5374 | return err; |
| 5375 | } |
| 5376 | |
| 5377 | static int swevent_hlist_get(struct perf_event *event) |
| 5378 | { |
| 5379 | int err; |
| 5380 | int cpu, failed_cpu; |
| 5381 | |
| 5382 | if (event->cpu != -1) |
| 5383 | return swevent_hlist_get_cpu(event, event->cpu); |
| 5384 | |
| 5385 | get_online_cpus(); |
| 5386 | for_each_possible_cpu(cpu) { |
| 5387 | err = swevent_hlist_get_cpu(event, cpu); |
| 5388 | if (err) { |
| 5389 | failed_cpu = cpu; |
| 5390 | goto fail; |
| 5391 | } |
| 5392 | } |
| 5393 | put_online_cpus(); |
| 5394 | |
| 5395 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 5396 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5397 | for_each_possible_cpu(cpu) { |
| 5398 | if (cpu == failed_cpu) |
| 5399 | break; |
| 5400 | swevent_hlist_put_cpu(event, cpu); |
| 5401 | } |
| 5402 | |
| 5403 | put_online_cpus(); |
| 5404 | return err; |
| 5405 | } |
| 5406 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 5407 | struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5408 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5409 | static void sw_perf_event_destroy(struct perf_event *event) |
| 5410 | { |
| 5411 | u64 event_id = event->attr.config; |
| 5412 | |
| 5413 | WARN_ON(event->parent); |
| 5414 | |
Peter Zijlstra | 7e54a5a | 2010-10-14 22:32:45 +0200 | [diff] [blame] | 5415 | jump_label_dec(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5416 | swevent_hlist_put(event); |
| 5417 | } |
| 5418 | |
| 5419 | static int perf_swevent_init(struct perf_event *event) |
| 5420 | { |
| 5421 | int event_id = event->attr.config; |
| 5422 | |
| 5423 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 5424 | return -ENOENT; |
| 5425 | |
| 5426 | switch (event_id) { |
| 5427 | case PERF_COUNT_SW_CPU_CLOCK: |
| 5428 | case PERF_COUNT_SW_TASK_CLOCK: |
| 5429 | return -ENOENT; |
| 5430 | |
| 5431 | default: |
| 5432 | break; |
| 5433 | } |
| 5434 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 5435 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5436 | return -ENOENT; |
| 5437 | |
| 5438 | if (!event->parent) { |
| 5439 | int err; |
| 5440 | |
| 5441 | err = swevent_hlist_get(event); |
| 5442 | if (err) |
| 5443 | return err; |
| 5444 | |
Peter Zijlstra | 7e54a5a | 2010-10-14 22:32:45 +0200 | [diff] [blame] | 5445 | jump_label_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5446 | event->destroy = sw_perf_event_destroy; |
| 5447 | } |
| 5448 | |
| 5449 | return 0; |
| 5450 | } |
| 5451 | |
| 5452 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 5453 | .task_ctx_nr = perf_sw_context, |
| 5454 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5455 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5456 | .add = perf_swevent_add, |
| 5457 | .del = perf_swevent_del, |
| 5458 | .start = perf_swevent_start, |
| 5459 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5460 | .read = perf_swevent_read, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5461 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5462 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5463 | #ifdef CONFIG_EVENT_TRACING |
| 5464 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5465 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5466 | struct perf_sample_data *data) |
| 5467 | { |
| 5468 | void *record = data->raw->data; |
| 5469 | |
| 5470 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 5471 | return 1; |
| 5472 | return 0; |
| 5473 | } |
| 5474 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5475 | static int perf_tp_event_match(struct perf_event *event, |
| 5476 | struct perf_sample_data *data, |
| 5477 | struct pt_regs *regs) |
| 5478 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 5479 | if (event->hw.state & PERF_HES_STOPPED) |
| 5480 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 5481 | /* |
| 5482 | * All tracepoints are from kernel-space. |
| 5483 | */ |
| 5484 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5485 | return 0; |
| 5486 | |
| 5487 | if (!perf_tp_filter_match(event, data)) |
| 5488 | return 0; |
| 5489 | |
| 5490 | return 1; |
| 5491 | } |
| 5492 | |
| 5493 | void perf_tp_event(u64 addr, u64 count, void *record, int entry_size, |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 5494 | struct pt_regs *regs, struct hlist_head *head, int rctx) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5495 | { |
| 5496 | struct perf_sample_data data; |
| 5497 | struct perf_event *event; |
| 5498 | struct hlist_node *node; |
| 5499 | |
| 5500 | struct perf_raw_record raw = { |
| 5501 | .size = entry_size, |
| 5502 | .data = record, |
| 5503 | }; |
| 5504 | |
| 5505 | perf_sample_data_init(&data, addr); |
| 5506 | data.raw = &raw; |
| 5507 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5508 | hlist_for_each_entry_rcu(event, node, head, hlist_entry) { |
| 5509 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5510 | perf_swevent_event(event, count, 1, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5511 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 5512 | |
| 5513 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5514 | } |
| 5515 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 5516 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5517 | static void tp_perf_event_destroy(struct perf_event *event) |
| 5518 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5519 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5520 | } |
| 5521 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5522 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5523 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5524 | int err; |
| 5525 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5526 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 5527 | return -ENOENT; |
| 5528 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5529 | err = perf_trace_init(event); |
| 5530 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5531 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5532 | |
| 5533 | event->destroy = tp_perf_event_destroy; |
| 5534 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5535 | return 0; |
| 5536 | } |
| 5537 | |
| 5538 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 5539 | .task_ctx_nr = perf_sw_context, |
| 5540 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5541 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5542 | .add = perf_trace_add, |
| 5543 | .del = perf_trace_del, |
| 5544 | .start = perf_swevent_start, |
| 5545 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5546 | .read = perf_swevent_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5547 | }; |
| 5548 | |
| 5549 | static inline void perf_tp_register(void) |
| 5550 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 5551 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5552 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5553 | |
| 5554 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 5555 | { |
| 5556 | char *filter_str; |
| 5557 | int ret; |
| 5558 | |
| 5559 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 5560 | return -EINVAL; |
| 5561 | |
| 5562 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 5563 | if (IS_ERR(filter_str)) |
| 5564 | return PTR_ERR(filter_str); |
| 5565 | |
| 5566 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); |
| 5567 | |
| 5568 | kfree(filter_str); |
| 5569 | return ret; |
| 5570 | } |
| 5571 | |
| 5572 | static void perf_event_free_filter(struct perf_event *event) |
| 5573 | { |
| 5574 | ftrace_profile_free_filter(event); |
| 5575 | } |
| 5576 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5577 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5578 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5579 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5580 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5581 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5582 | |
| 5583 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 5584 | { |
| 5585 | return -ENOENT; |
| 5586 | } |
| 5587 | |
| 5588 | static void perf_event_free_filter(struct perf_event *event) |
| 5589 | { |
| 5590 | } |
| 5591 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 5592 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5593 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 5594 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5595 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 5596 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5597 | struct perf_sample_data sample; |
| 5598 | struct pt_regs *regs = data; |
| 5599 | |
Peter Zijlstra | dc1d628 | 2010-03-03 15:55:04 +0100 | [diff] [blame] | 5600 | perf_sample_data_init(&sample, bp->attr.bp_addr); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5601 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5602 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
| 5603 | perf_swevent_event(bp, 1, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 5604 | } |
| 5605 | #endif |
| 5606 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5607 | /* |
| 5608 | * hrtimer based swevent callback |
| 5609 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5610 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5611 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5612 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5613 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 5614 | struct perf_sample_data data; |
| 5615 | struct pt_regs *regs; |
| 5616 | struct perf_event *event; |
| 5617 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5618 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5619 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 5620 | |
| 5621 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 5622 | return HRTIMER_NORESTART; |
| 5623 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5624 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5625 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5626 | perf_sample_data_init(&data, 0); |
| 5627 | data.period = event->hw.last_period; |
| 5628 | regs = get_irq_regs(); |
| 5629 | |
| 5630 | if (regs && !perf_exclude_event(event, regs)) { |
| 5631 | if (!(event->attr.exclude_idle && current->pid == 0)) |
| 5632 | if (perf_event_overflow(event, 0, &data, regs)) |
| 5633 | ret = HRTIMER_NORESTART; |
| 5634 | } |
| 5635 | |
| 5636 | period = max_t(u64, 10000, event->hw.sample_period); |
| 5637 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 5638 | |
| 5639 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5640 | } |
| 5641 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5642 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5643 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5644 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 5645 | s64 period; |
| 5646 | |
| 5647 | if (!is_sampling_event(event)) |
| 5648 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5649 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 5650 | period = local64_read(&hwc->period_left); |
| 5651 | if (period) { |
| 5652 | if (period < 0) |
| 5653 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 5654 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 5655 | local64_set(&hwc->period_left, 0); |
| 5656 | } else { |
| 5657 | period = max_t(u64, 10000, hwc->sample_period); |
| 5658 | } |
| 5659 | __hrtimer_start_range_ns(&hwc->hrtimer, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5660 | ns_to_ktime(period), 0, |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 5661 | HRTIMER_MODE_REL_PINNED, 0); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 5665 | { |
| 5666 | struct hw_perf_event *hwc = &event->hw; |
| 5667 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5668 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5669 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 5670 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5671 | |
| 5672 | hrtimer_cancel(&hwc->hrtimer); |
| 5673 | } |
| 5674 | } |
| 5675 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 5676 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 5677 | { |
| 5678 | struct hw_perf_event *hwc = &event->hw; |
| 5679 | |
| 5680 | if (!is_sampling_event(event)) |
| 5681 | return; |
| 5682 | |
| 5683 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 5684 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 5685 | |
| 5686 | /* |
| 5687 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 5688 | * mapping and avoid the whole period adjust feedback stuff. |
| 5689 | */ |
| 5690 | if (event->attr.freq) { |
| 5691 | long freq = event->attr.sample_freq; |
| 5692 | |
| 5693 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 5694 | hwc->sample_period = event->attr.sample_period; |
| 5695 | local64_set(&hwc->period_left, hwc->sample_period); |
| 5696 | event->attr.freq = 0; |
| 5697 | } |
| 5698 | } |
| 5699 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5700 | /* |
| 5701 | * Software event: cpu wall time clock |
| 5702 | */ |
| 5703 | |
| 5704 | static void cpu_clock_event_update(struct perf_event *event) |
| 5705 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5706 | s64 prev; |
| 5707 | u64 now; |
| 5708 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5709 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5710 | prev = local64_xchg(&event->hw.prev_count, now); |
| 5711 | local64_add(now - prev, &event->count); |
| 5712 | } |
| 5713 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5714 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5715 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5716 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5717 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 5721 | { |
| 5722 | perf_swevent_cancel_hrtimer(event); |
| 5723 | cpu_clock_event_update(event); |
| 5724 | } |
| 5725 | |
| 5726 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 5727 | { |
| 5728 | if (flags & PERF_EF_START) |
| 5729 | cpu_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5730 | |
| 5731 | return 0; |
| 5732 | } |
| 5733 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5734 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5735 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5736 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5737 | } |
| 5738 | |
| 5739 | static void cpu_clock_event_read(struct perf_event *event) |
| 5740 | { |
| 5741 | cpu_clock_event_update(event); |
| 5742 | } |
| 5743 | |
| 5744 | static int cpu_clock_event_init(struct perf_event *event) |
| 5745 | { |
| 5746 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 5747 | return -ENOENT; |
| 5748 | |
| 5749 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 5750 | return -ENOENT; |
| 5751 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 5752 | perf_swevent_init_hrtimer(event); |
| 5753 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5754 | return 0; |
| 5755 | } |
| 5756 | |
| 5757 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 5758 | .task_ctx_nr = perf_sw_context, |
| 5759 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5760 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5761 | .add = cpu_clock_event_add, |
| 5762 | .del = cpu_clock_event_del, |
| 5763 | .start = cpu_clock_event_start, |
| 5764 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5765 | .read = cpu_clock_event_read, |
| 5766 | }; |
| 5767 | |
| 5768 | /* |
| 5769 | * Software event: task time clock |
| 5770 | */ |
| 5771 | |
| 5772 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 5773 | { |
| 5774 | u64 prev; |
| 5775 | s64 delta; |
| 5776 | |
| 5777 | prev = local64_xchg(&event->hw.prev_count, now); |
| 5778 | delta = now - prev; |
| 5779 | local64_add(delta, &event->count); |
| 5780 | } |
| 5781 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5782 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5783 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5784 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5785 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5786 | } |
| 5787 | |
| 5788 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 5789 | { |
| 5790 | perf_swevent_cancel_hrtimer(event); |
| 5791 | task_clock_event_update(event, event->ctx->time); |
| 5792 | } |
| 5793 | |
| 5794 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 5795 | { |
| 5796 | if (flags & PERF_EF_START) |
| 5797 | task_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5798 | |
| 5799 | return 0; |
| 5800 | } |
| 5801 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5802 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5803 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5804 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | static void task_clock_event_read(struct perf_event *event) |
| 5808 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 5809 | u64 now = perf_clock(); |
| 5810 | u64 delta = now - event->ctx->timestamp; |
| 5811 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5812 | |
| 5813 | task_clock_event_update(event, time); |
| 5814 | } |
| 5815 | |
| 5816 | static int task_clock_event_init(struct perf_event *event) |
| 5817 | { |
| 5818 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 5819 | return -ENOENT; |
| 5820 | |
| 5821 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 5822 | return -ENOENT; |
| 5823 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 5824 | perf_swevent_init_hrtimer(event); |
| 5825 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5826 | return 0; |
| 5827 | } |
| 5828 | |
| 5829 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 5830 | .task_ctx_nr = perf_sw_context, |
| 5831 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5832 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5833 | .add = task_clock_event_add, |
| 5834 | .del = task_clock_event_del, |
| 5835 | .start = task_clock_event_start, |
| 5836 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5837 | .read = task_clock_event_read, |
| 5838 | }; |
| 5839 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 5840 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 5841 | { |
| 5842 | } |
| 5843 | |
| 5844 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 5845 | { |
| 5846 | return 0; |
| 5847 | } |
| 5848 | |
| 5849 | static void perf_pmu_start_txn(struct pmu *pmu) |
| 5850 | { |
| 5851 | perf_pmu_disable(pmu); |
| 5852 | } |
| 5853 | |
| 5854 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 5855 | { |
| 5856 | perf_pmu_enable(pmu); |
| 5857 | return 0; |
| 5858 | } |
| 5859 | |
| 5860 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 5861 | { |
| 5862 | perf_pmu_enable(pmu); |
| 5863 | } |
| 5864 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5865 | /* |
| 5866 | * Ensures all contexts with the same task_ctx_nr have the same |
| 5867 | * pmu_cpu_context too. |
| 5868 | */ |
| 5869 | static void *find_pmu_context(int ctxn) |
| 5870 | { |
| 5871 | struct pmu *pmu; |
| 5872 | |
| 5873 | if (ctxn < 0) |
| 5874 | return NULL; |
| 5875 | |
| 5876 | list_for_each_entry(pmu, &pmus, entry) { |
| 5877 | if (pmu->task_ctx_nr == ctxn) |
| 5878 | return pmu->pmu_cpu_context; |
| 5879 | } |
| 5880 | |
| 5881 | return NULL; |
| 5882 | } |
| 5883 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 5884 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5885 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 5886 | int cpu; |
| 5887 | |
| 5888 | for_each_possible_cpu(cpu) { |
| 5889 | struct perf_cpu_context *cpuctx; |
| 5890 | |
| 5891 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 5892 | |
| 5893 | if (cpuctx->active_pmu == old_pmu) |
| 5894 | cpuctx->active_pmu = pmu; |
| 5895 | } |
| 5896 | } |
| 5897 | |
| 5898 | static void free_pmu_context(struct pmu *pmu) |
| 5899 | { |
| 5900 | struct pmu *i; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5901 | |
| 5902 | mutex_lock(&pmus_lock); |
| 5903 | /* |
| 5904 | * Like a real lame refcount. |
| 5905 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 5906 | list_for_each_entry(i, &pmus, entry) { |
| 5907 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 5908 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5909 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 5910 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5911 | } |
| 5912 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 5913 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5914 | out: |
| 5915 | mutex_unlock(&pmus_lock); |
| 5916 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 5917 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5918 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 5919 | static ssize_t |
| 5920 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 5921 | { |
| 5922 | struct pmu *pmu = dev_get_drvdata(dev); |
| 5923 | |
| 5924 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 5925 | } |
| 5926 | |
| 5927 | static struct device_attribute pmu_dev_attrs[] = { |
| 5928 | __ATTR_RO(type), |
| 5929 | __ATTR_NULL, |
| 5930 | }; |
| 5931 | |
| 5932 | static int pmu_bus_running; |
| 5933 | static struct bus_type pmu_bus = { |
| 5934 | .name = "event_source", |
| 5935 | .dev_attrs = pmu_dev_attrs, |
| 5936 | }; |
| 5937 | |
| 5938 | static void pmu_dev_release(struct device *dev) |
| 5939 | { |
| 5940 | kfree(dev); |
| 5941 | } |
| 5942 | |
| 5943 | static int pmu_dev_alloc(struct pmu *pmu) |
| 5944 | { |
| 5945 | int ret = -ENOMEM; |
| 5946 | |
| 5947 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 5948 | if (!pmu->dev) |
| 5949 | goto out; |
| 5950 | |
| 5951 | device_initialize(pmu->dev); |
| 5952 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 5953 | if (ret) |
| 5954 | goto free_dev; |
| 5955 | |
| 5956 | dev_set_drvdata(pmu->dev, pmu); |
| 5957 | pmu->dev->bus = &pmu_bus; |
| 5958 | pmu->dev->release = pmu_dev_release; |
| 5959 | ret = device_add(pmu->dev); |
| 5960 | if (ret) |
| 5961 | goto free_dev; |
| 5962 | |
| 5963 | out: |
| 5964 | return ret; |
| 5965 | |
| 5966 | free_dev: |
| 5967 | put_device(pmu->dev); |
| 5968 | goto out; |
| 5969 | } |
| 5970 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 5971 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 5972 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 5973 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 5974 | int perf_pmu_register(struct pmu *pmu, char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5975 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 5976 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 5977 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5978 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 5979 | ret = -ENOMEM; |
| 5980 | pmu->pmu_disable_count = alloc_percpu(int); |
| 5981 | if (!pmu->pmu_disable_count) |
| 5982 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 5983 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 5984 | pmu->type = -1; |
| 5985 | if (!name) |
| 5986 | goto skip_type; |
| 5987 | pmu->name = name; |
| 5988 | |
| 5989 | if (type < 0) { |
| 5990 | int err = idr_pre_get(&pmu_idr, GFP_KERNEL); |
| 5991 | if (!err) |
| 5992 | goto free_pdc; |
| 5993 | |
| 5994 | err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type); |
| 5995 | if (err) { |
| 5996 | ret = err; |
| 5997 | goto free_pdc; |
| 5998 | } |
| 5999 | } |
| 6000 | pmu->type = type; |
| 6001 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6002 | if (pmu_bus_running) { |
| 6003 | ret = pmu_dev_alloc(pmu); |
| 6004 | if (ret) |
| 6005 | goto free_idr; |
| 6006 | } |
| 6007 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6008 | skip_type: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6009 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 6010 | if (pmu->pmu_cpu_context) |
| 6011 | goto got_cpu_context; |
| 6012 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6013 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 6014 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6015 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6016 | |
| 6017 | for_each_possible_cpu(cpu) { |
| 6018 | struct perf_cpu_context *cpuctx; |
| 6019 | |
| 6020 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 6021 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6022 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6023 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6024 | cpuctx->ctx.type = cpu_context; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6025 | cpuctx->ctx.pmu = pmu; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 6026 | cpuctx->jiffies_interval = 1; |
| 6027 | INIT_LIST_HEAD(&cpuctx->rotation_list); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6028 | cpuctx->active_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6029 | } |
| 6030 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6031 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6032 | if (!pmu->start_txn) { |
| 6033 | if (pmu->pmu_enable) { |
| 6034 | /* |
| 6035 | * If we have pmu_enable/pmu_disable calls, install |
| 6036 | * transaction stubs that use that to try and batch |
| 6037 | * hardware accesses. |
| 6038 | */ |
| 6039 | pmu->start_txn = perf_pmu_start_txn; |
| 6040 | pmu->commit_txn = perf_pmu_commit_txn; |
| 6041 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 6042 | } else { |
| 6043 | pmu->start_txn = perf_pmu_nop_void; |
| 6044 | pmu->commit_txn = perf_pmu_nop_int; |
| 6045 | pmu->cancel_txn = perf_pmu_nop_void; |
| 6046 | } |
| 6047 | } |
| 6048 | |
| 6049 | if (!pmu->pmu_enable) { |
| 6050 | pmu->pmu_enable = perf_pmu_nop_void; |
| 6051 | pmu->pmu_disable = perf_pmu_nop_void; |
| 6052 | } |
| 6053 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6054 | list_add_rcu(&pmu->entry, &pmus); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6055 | ret = 0; |
| 6056 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6057 | mutex_unlock(&pmus_lock); |
| 6058 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6059 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6060 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6061 | free_dev: |
| 6062 | device_del(pmu->dev); |
| 6063 | put_device(pmu->dev); |
| 6064 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6065 | free_idr: |
| 6066 | if (pmu->type >= PERF_TYPE_MAX) |
| 6067 | idr_remove(&pmu_idr, pmu->type); |
| 6068 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6069 | free_pdc: |
| 6070 | free_percpu(pmu->pmu_disable_count); |
| 6071 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6072 | } |
| 6073 | |
| 6074 | void perf_pmu_unregister(struct pmu *pmu) |
| 6075 | { |
| 6076 | mutex_lock(&pmus_lock); |
| 6077 | list_del_rcu(&pmu->entry); |
| 6078 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6079 | |
| 6080 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6081 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 6082 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6083 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6084 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6085 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6086 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6087 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6088 | if (pmu->type >= PERF_TYPE_MAX) |
| 6089 | idr_remove(&pmu_idr, pmu->type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6090 | device_del(pmu->dev); |
| 6091 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6092 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6093 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6094 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6095 | struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6096 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6097 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6098 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6099 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6100 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6101 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6102 | |
| 6103 | rcu_read_lock(); |
| 6104 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 6105 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6106 | if (pmu) { |
| 6107 | ret = pmu->event_init(event); |
| 6108 | if (ret) |
| 6109 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6110 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6111 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6112 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6113 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6114 | ret = pmu->event_init(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6115 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6116 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6117 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6118 | if (ret != -ENOENT) { |
| 6119 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6120 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6121 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6122 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6123 | pmu = ERR_PTR(-ENOENT); |
| 6124 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6125 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6126 | |
| 6127 | return pmu; |
| 6128 | } |
| 6129 | |
| 6130 | /* |
| 6131 | * Allocate and initialize a event structure |
| 6132 | */ |
| 6133 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6134 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6135 | struct task_struct *task, |
| 6136 | struct perf_event *group_leader, |
| 6137 | struct perf_event *parent_event, |
| 6138 | perf_overflow_handler_t overflow_handler) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6139 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6140 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6141 | struct perf_event *event; |
| 6142 | struct hw_perf_event *hwc; |
| 6143 | long err; |
| 6144 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 6145 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 6146 | if (!task || cpu != -1) |
| 6147 | return ERR_PTR(-EINVAL); |
| 6148 | } |
| 6149 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6150 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6151 | if (!event) |
| 6152 | return ERR_PTR(-ENOMEM); |
| 6153 | |
| 6154 | /* |
| 6155 | * Single events are their own group leaders, with an |
| 6156 | * empty sibling list: |
| 6157 | */ |
| 6158 | if (!group_leader) |
| 6159 | group_leader = event; |
| 6160 | |
| 6161 | mutex_init(&event->child_mutex); |
| 6162 | INIT_LIST_HEAD(&event->child_list); |
| 6163 | |
| 6164 | INIT_LIST_HEAD(&event->group_entry); |
| 6165 | INIT_LIST_HEAD(&event->event_entry); |
| 6166 | INIT_LIST_HEAD(&event->sibling_list); |
| 6167 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 6168 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6169 | |
| 6170 | mutex_init(&event->mmap_mutex); |
| 6171 | |
| 6172 | event->cpu = cpu; |
| 6173 | event->attr = *attr; |
| 6174 | event->group_leader = group_leader; |
| 6175 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6176 | event->oncpu = -1; |
| 6177 | |
| 6178 | event->parent = parent_event; |
| 6179 | |
| 6180 | event->ns = get_pid_ns(current->nsproxy->pid_ns); |
| 6181 | event->id = atomic64_inc_return(&perf_event_id); |
| 6182 | |
| 6183 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 6184 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6185 | if (task) { |
| 6186 | event->attach_state = PERF_ATTACH_TASK; |
| 6187 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
| 6188 | /* |
| 6189 | * hw_breakpoint is a bit difficult here.. |
| 6190 | */ |
| 6191 | if (attr->type == PERF_TYPE_BREAKPOINT) |
| 6192 | event->hw.bp_target = task; |
| 6193 | #endif |
| 6194 | } |
| 6195 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 6196 | if (!overflow_handler && parent_event) |
| 6197 | overflow_handler = parent_event->overflow_handler; |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 6198 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 6199 | event->overflow_handler = overflow_handler; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 6200 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6201 | if (attr->disabled) |
| 6202 | event->state = PERF_EVENT_STATE_OFF; |
| 6203 | |
| 6204 | pmu = NULL; |
| 6205 | |
| 6206 | hwc = &event->hw; |
| 6207 | hwc->sample_period = attr->sample_period; |
| 6208 | if (attr->freq && attr->sample_freq) |
| 6209 | hwc->sample_period = 1; |
| 6210 | hwc->last_period = hwc->sample_period; |
| 6211 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6212 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6213 | |
| 6214 | /* |
| 6215 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 6216 | */ |
| 6217 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
| 6218 | goto done; |
| 6219 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6220 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6221 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6222 | done: |
| 6223 | err = 0; |
| 6224 | if (!pmu) |
| 6225 | err = -EINVAL; |
| 6226 | else if (IS_ERR(pmu)) |
| 6227 | err = PTR_ERR(pmu); |
| 6228 | |
| 6229 | if (err) { |
| 6230 | if (event->ns) |
| 6231 | put_pid_ns(event->ns); |
| 6232 | kfree(event); |
| 6233 | return ERR_PTR(err); |
| 6234 | } |
| 6235 | |
| 6236 | event->pmu = pmu; |
| 6237 | |
| 6238 | if (!event->parent) { |
Peter Zijlstra | 82cd6de | 2010-10-14 17:57:23 +0200 | [diff] [blame] | 6239 | if (event->attach_state & PERF_ATTACH_TASK) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6240 | jump_label_inc(&perf_sched_events); |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 6241 | if (event->attr.mmap || event->attr.mmap_data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6242 | atomic_inc(&nr_mmap_events); |
| 6243 | if (event->attr.comm) |
| 6244 | atomic_inc(&nr_comm_events); |
| 6245 | if (event->attr.task) |
| 6246 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6247 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 6248 | err = get_callchain_buffers(); |
| 6249 | if (err) { |
| 6250 | free_event(event); |
| 6251 | return ERR_PTR(err); |
| 6252 | } |
| 6253 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6254 | } |
| 6255 | |
| 6256 | return event; |
| 6257 | } |
| 6258 | |
| 6259 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 6260 | struct perf_event_attr *attr) |
| 6261 | { |
| 6262 | u32 size; |
| 6263 | int ret; |
| 6264 | |
| 6265 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 6266 | return -EFAULT; |
| 6267 | |
| 6268 | /* |
| 6269 | * zero the full structure, so that a short copy will be nice. |
| 6270 | */ |
| 6271 | memset(attr, 0, sizeof(*attr)); |
| 6272 | |
| 6273 | ret = get_user(size, &uattr->size); |
| 6274 | if (ret) |
| 6275 | return ret; |
| 6276 | |
| 6277 | if (size > PAGE_SIZE) /* silly large */ |
| 6278 | goto err_size; |
| 6279 | |
| 6280 | if (!size) /* abi compat */ |
| 6281 | size = PERF_ATTR_SIZE_VER0; |
| 6282 | |
| 6283 | if (size < PERF_ATTR_SIZE_VER0) |
| 6284 | goto err_size; |
| 6285 | |
| 6286 | /* |
| 6287 | * If we're handed a bigger struct than we know of, |
| 6288 | * ensure all the unknown bits are 0 - i.e. new |
| 6289 | * user-space does not rely on any kernel feature |
| 6290 | * extensions we dont know about yet. |
| 6291 | */ |
| 6292 | if (size > sizeof(*attr)) { |
| 6293 | unsigned char __user *addr; |
| 6294 | unsigned char __user *end; |
| 6295 | unsigned char val; |
| 6296 | |
| 6297 | addr = (void __user *)uattr + sizeof(*attr); |
| 6298 | end = (void __user *)uattr + size; |
| 6299 | |
| 6300 | for (; addr < end; addr++) { |
| 6301 | ret = get_user(val, addr); |
| 6302 | if (ret) |
| 6303 | return ret; |
| 6304 | if (val) |
| 6305 | goto err_size; |
| 6306 | } |
| 6307 | size = sizeof(*attr); |
| 6308 | } |
| 6309 | |
| 6310 | ret = copy_from_user(attr, uattr, size); |
| 6311 | if (ret) |
| 6312 | return -EFAULT; |
| 6313 | |
| 6314 | /* |
| 6315 | * If the type exists, the corresponding creation will verify |
| 6316 | * the attr->config. |
| 6317 | */ |
| 6318 | if (attr->type >= PERF_TYPE_MAX) |
| 6319 | return -EINVAL; |
| 6320 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 6321 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6322 | return -EINVAL; |
| 6323 | |
| 6324 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 6325 | return -EINVAL; |
| 6326 | |
| 6327 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 6328 | return -EINVAL; |
| 6329 | |
| 6330 | out: |
| 6331 | return ret; |
| 6332 | |
| 6333 | err_size: |
| 6334 | put_user(sizeof(*attr), &uattr->size); |
| 6335 | ret = -E2BIG; |
| 6336 | goto out; |
| 6337 | } |
| 6338 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6339 | static int |
| 6340 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6341 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 6342 | struct perf_buffer *buffer = NULL, *old_buffer = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6343 | int ret = -EINVAL; |
| 6344 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6345 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6346 | goto set; |
| 6347 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6348 | /* don't allow circular references */ |
| 6349 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6350 | goto out; |
| 6351 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 6352 | /* |
| 6353 | * Don't allow cross-cpu buffers |
| 6354 | */ |
| 6355 | if (output_event->cpu != event->cpu) |
| 6356 | goto out; |
| 6357 | |
| 6358 | /* |
| 6359 | * If its not a per-cpu buffer, it must be the same task. |
| 6360 | */ |
| 6361 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 6362 | goto out; |
| 6363 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6364 | set: |
| 6365 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6366 | /* Can't redirect output if we've got an active mmap() */ |
| 6367 | if (atomic_read(&event->mmap_count)) |
| 6368 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6369 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6370 | if (output_event) { |
| 6371 | /* get the buffer we want to redirect to */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 6372 | buffer = perf_buffer_get(output_event); |
| 6373 | if (!buffer) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6374 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6375 | } |
| 6376 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 6377 | old_buffer = event->buffer; |
| 6378 | rcu_assign_pointer(event->buffer, buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6379 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6380 | unlock: |
| 6381 | mutex_unlock(&event->mmap_mutex); |
| 6382 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 6383 | if (old_buffer) |
| 6384 | perf_buffer_put(old_buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6385 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6386 | return ret; |
| 6387 | } |
| 6388 | |
| 6389 | /** |
| 6390 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 6391 | * |
| 6392 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 6393 | * @pid: target pid |
| 6394 | * @cpu: target cpu |
| 6395 | * @group_fd: group leader event fd |
| 6396 | */ |
| 6397 | SYSCALL_DEFINE5(perf_event_open, |
| 6398 | struct perf_event_attr __user *, attr_uptr, |
| 6399 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 6400 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6401 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 6402 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6403 | struct perf_event_attr attr; |
| 6404 | struct perf_event_context *ctx; |
| 6405 | struct file *event_file = NULL; |
| 6406 | struct file *group_file = NULL; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 6407 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6408 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6409 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6410 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6411 | int fput_needed = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6412 | int err; |
| 6413 | |
| 6414 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6415 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6416 | return -EINVAL; |
| 6417 | |
| 6418 | err = perf_copy_attr(attr_uptr, &attr); |
| 6419 | if (err) |
| 6420 | return err; |
| 6421 | |
| 6422 | if (!attr.exclude_kernel) { |
| 6423 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 6424 | return -EACCES; |
| 6425 | } |
| 6426 | |
| 6427 | if (attr.freq) { |
| 6428 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 6429 | return -EINVAL; |
| 6430 | } |
| 6431 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6432 | /* |
| 6433 | * In cgroup mode, the pid argument is used to pass the fd |
| 6434 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 6435 | * designates the cpu on which to monitor threads from that |
| 6436 | * cgroup. |
| 6437 | */ |
| 6438 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 6439 | return -EINVAL; |
| 6440 | |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6441 | event_fd = get_unused_fd_flags(O_RDWR); |
| 6442 | if (event_fd < 0) |
| 6443 | return event_fd; |
| 6444 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6445 | if (group_fd != -1) { |
| 6446 | group_leader = perf_fget_light(group_fd, &fput_needed); |
| 6447 | if (IS_ERR(group_leader)) { |
| 6448 | err = PTR_ERR(group_leader); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 6449 | goto err_fd; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6450 | } |
| 6451 | group_file = group_leader->filp; |
| 6452 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 6453 | output_event = group_leader; |
| 6454 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 6455 | group_leader = NULL; |
| 6456 | } |
| 6457 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6458 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 6459 | task = find_lively_task_by_vpid(pid); |
| 6460 | if (IS_ERR(task)) { |
| 6461 | err = PTR_ERR(task); |
| 6462 | goto err_group_fd; |
| 6463 | } |
| 6464 | } |
| 6465 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6466 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 6467 | if (IS_ERR(event)) { |
| 6468 | err = PTR_ERR(event); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 6469 | goto err_task; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 6470 | } |
| 6471 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6472 | if (flags & PERF_FLAG_PID_CGROUP) { |
| 6473 | err = perf_cgroup_connect(pid, event, &attr, group_leader); |
| 6474 | if (err) |
| 6475 | goto err_alloc; |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 6476 | /* |
| 6477 | * one more event: |
| 6478 | * - that has cgroup constraint on event->cpu |
| 6479 | * - that may need work on context switch |
| 6480 | */ |
| 6481 | atomic_inc(&per_cpu(perf_cgroup_events, event->cpu)); |
| 6482 | jump_label_inc(&perf_sched_events); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6483 | } |
| 6484 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6485 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6486 | * Special case software events and allow them to be part of |
| 6487 | * any hardware group. |
| 6488 | */ |
| 6489 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6490 | |
| 6491 | if (group_leader && |
| 6492 | (is_software_event(event) != is_software_event(group_leader))) { |
| 6493 | if (is_software_event(event)) { |
| 6494 | /* |
| 6495 | * If event and group_leader are not both a software |
| 6496 | * event, and event is, then group leader is not. |
| 6497 | * |
| 6498 | * Allow the addition of software events to !software |
| 6499 | * groups, this is safe because software events never |
| 6500 | * fail to schedule. |
| 6501 | */ |
| 6502 | pmu = group_leader->pmu; |
| 6503 | } else if (is_software_event(group_leader) && |
| 6504 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 6505 | /* |
| 6506 | * In case the group is a pure software group, and we |
| 6507 | * try to add a hardware event, move the whole group to |
| 6508 | * the hardware context. |
| 6509 | */ |
| 6510 | move_group = 1; |
| 6511 | } |
| 6512 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6513 | |
| 6514 | /* |
| 6515 | * Get the target context (task or percpu): |
| 6516 | */ |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 6517 | ctx = find_get_context(pmu, task, cpu); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6518 | if (IS_ERR(ctx)) { |
| 6519 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 6520 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6521 | } |
| 6522 | |
Peter Zijlstra | fd1edb3 | 2011-03-28 13:13:56 +0200 | [diff] [blame] | 6523 | if (task) { |
| 6524 | put_task_struct(task); |
| 6525 | task = NULL; |
| 6526 | } |
| 6527 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6528 | /* |
| 6529 | * Look up the group leader (we will attach this event to it): |
| 6530 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6531 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6532 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6533 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6534 | /* |
| 6535 | * Do not allow a recursive hierarchy (this new sibling |
| 6536 | * becoming part of another group-sibling): |
| 6537 | */ |
| 6538 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6539 | goto err_context; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6540 | /* |
| 6541 | * Do not allow to attach to a group in a different |
| 6542 | * task or CPU context: |
| 6543 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6544 | if (move_group) { |
| 6545 | if (group_leader->ctx->type != ctx->type) |
| 6546 | goto err_context; |
| 6547 | } else { |
| 6548 | if (group_leader->ctx != ctx) |
| 6549 | goto err_context; |
| 6550 | } |
| 6551 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6552 | /* |
| 6553 | * Only a group leader can be exclusive or pinned |
| 6554 | */ |
| 6555 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6556 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6557 | } |
| 6558 | |
| 6559 | if (output_event) { |
| 6560 | err = perf_event_set_output(event, output_event); |
| 6561 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6562 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6563 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6564 | |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6565 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR); |
| 6566 | if (IS_ERR(event_file)) { |
| 6567 | err = PTR_ERR(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6568 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6569 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6570 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6571 | if (move_group) { |
| 6572 | struct perf_event_context *gctx = group_leader->ctx; |
| 6573 | |
| 6574 | mutex_lock(&gctx->mutex); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 6575 | perf_remove_from_context(group_leader); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6576 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 6577 | group_entry) { |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 6578 | perf_remove_from_context(sibling); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6579 | put_ctx(gctx); |
| 6580 | } |
| 6581 | mutex_unlock(&gctx->mutex); |
| 6582 | put_ctx(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6583 | } |
| 6584 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6585 | event->filp = event_file; |
| 6586 | WARN_ON_ONCE(ctx->parent_ctx); |
| 6587 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6588 | |
| 6589 | if (move_group) { |
| 6590 | perf_install_in_context(ctx, group_leader, cpu); |
| 6591 | get_ctx(ctx); |
| 6592 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 6593 | group_entry) { |
| 6594 | perf_install_in_context(ctx, sibling, cpu); |
| 6595 | get_ctx(ctx); |
| 6596 | } |
| 6597 | } |
| 6598 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6599 | perf_install_in_context(ctx, event, cpu); |
| 6600 | ++ctx->generation; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 6601 | perf_unpin_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6602 | mutex_unlock(&ctx->mutex); |
| 6603 | |
| 6604 | event->owner = current; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 6605 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6606 | mutex_lock(¤t->perf_event_mutex); |
| 6607 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 6608 | mutex_unlock(¤t->perf_event_mutex); |
| 6609 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 6610 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 6611 | * Precalculate sample_data sizes |
| 6612 | */ |
| 6613 | perf_event__header_size(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 6614 | perf_event__id_header_size(event); |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 6615 | |
| 6616 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 6617 | * Drop the reference on the group_event after placing the |
| 6618 | * new event on the sibling_list. This ensures destruction |
| 6619 | * of the group leader will find the pointer to itself in |
| 6620 | * perf_group_detach(). |
| 6621 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6622 | fput_light(group_file, fput_needed); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6623 | fd_install(event_fd, event_file); |
| 6624 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6625 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6626 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 6627 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6628 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 6629 | err_alloc: |
| 6630 | free_event(event); |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 6631 | err_task: |
| 6632 | if (task) |
| 6633 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6634 | err_group_fd: |
| 6635 | fput_light(group_file, fput_needed); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6636 | err_fd: |
| 6637 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6638 | return err; |
| 6639 | } |
| 6640 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6641 | /** |
| 6642 | * perf_event_create_kernel_counter |
| 6643 | * |
| 6644 | * @attr: attributes of the counter to create |
| 6645 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 6646 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6647 | */ |
| 6648 | struct perf_event * |
| 6649 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 6650 | struct task_struct *task, |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 6651 | perf_overflow_handler_t overflow_handler) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6652 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6653 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6654 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6655 | int err; |
| 6656 | |
| 6657 | /* |
| 6658 | * Get the target context (task or percpu): |
| 6659 | */ |
| 6660 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6661 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 6662 | if (IS_ERR(event)) { |
| 6663 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6664 | goto err; |
| 6665 | } |
| 6666 | |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 6667 | ctx = find_get_context(event->pmu, task, cpu); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6668 | if (IS_ERR(ctx)) { |
| 6669 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6670 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 6671 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6672 | |
| 6673 | event->filp = NULL; |
| 6674 | WARN_ON_ONCE(ctx->parent_ctx); |
| 6675 | mutex_lock(&ctx->mutex); |
| 6676 | perf_install_in_context(ctx, event, cpu); |
| 6677 | ++ctx->generation; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 6678 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6679 | mutex_unlock(&ctx->mutex); |
| 6680 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6681 | return event; |
| 6682 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6683 | err_free: |
| 6684 | free_event(event); |
| 6685 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 6686 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 6687 | } |
| 6688 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 6689 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6690 | static void sync_child_event(struct perf_event *child_event, |
| 6691 | struct task_struct *child) |
| 6692 | { |
| 6693 | struct perf_event *parent_event = child_event->parent; |
| 6694 | u64 child_val; |
| 6695 | |
| 6696 | if (child_event->attr.inherit_stat) |
| 6697 | perf_event_read_event(child_event, child); |
| 6698 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 6699 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6700 | |
| 6701 | /* |
| 6702 | * Add back the child's count to the parent's count: |
| 6703 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 6704 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6705 | atomic64_add(child_event->total_time_enabled, |
| 6706 | &parent_event->child_total_time_enabled); |
| 6707 | atomic64_add(child_event->total_time_running, |
| 6708 | &parent_event->child_total_time_running); |
| 6709 | |
| 6710 | /* |
| 6711 | * Remove this event from the parent's list |
| 6712 | */ |
| 6713 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 6714 | mutex_lock(&parent_event->child_mutex); |
| 6715 | list_del_init(&child_event->child_list); |
| 6716 | mutex_unlock(&parent_event->child_mutex); |
| 6717 | |
| 6718 | /* |
| 6719 | * Release the parent event, if this was the last |
| 6720 | * reference to it. |
| 6721 | */ |
| 6722 | fput(parent_event->filp); |
| 6723 | } |
| 6724 | |
| 6725 | static void |
| 6726 | __perf_event_exit_task(struct perf_event *child_event, |
| 6727 | struct perf_event_context *child_ctx, |
| 6728 | struct task_struct *child) |
| 6729 | { |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 6730 | if (child_event->parent) { |
| 6731 | raw_spin_lock_irq(&child_ctx->lock); |
| 6732 | perf_group_detach(child_event); |
| 6733 | raw_spin_unlock_irq(&child_ctx->lock); |
| 6734 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6735 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 6736 | perf_remove_from_context(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6737 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6738 | /* |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 6739 | * It can happen that the parent exits first, and has events |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6740 | * that are still around due to the child reference. These |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 6741 | * events need to be zapped. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6742 | */ |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 6743 | if (child_event->parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6744 | sync_child_event(child_event, child); |
| 6745 | free_event(child_event); |
| 6746 | } |
| 6747 | } |
| 6748 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6749 | static void perf_event_exit_task_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6750 | { |
| 6751 | struct perf_event *child_event, *tmp; |
| 6752 | struct perf_event_context *child_ctx; |
| 6753 | unsigned long flags; |
| 6754 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6755 | if (likely(!child->perf_event_ctxp[ctxn])) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6756 | perf_event_task(child, NULL, 0); |
| 6757 | return; |
| 6758 | } |
| 6759 | |
| 6760 | local_irq_save(flags); |
| 6761 | /* |
| 6762 | * We can't reschedule here because interrupts are disabled, |
| 6763 | * and either child is current or it is a task that can't be |
| 6764 | * scheduled, so we are now safe from rescheduling changing |
| 6765 | * our context. |
| 6766 | */ |
Oleg Nesterov | 806839b | 2011-01-21 18:45:47 +0100 | [diff] [blame] | 6767 | child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6768 | |
| 6769 | /* |
| 6770 | * Take the context lock here so that if find_get_context is |
| 6771 | * reading child->perf_event_ctxp, we wait until it has |
| 6772 | * incremented the context's refcount before we do put_ctx below. |
| 6773 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 6774 | raw_spin_lock(&child_ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 6775 | task_ctx_sched_out(child_ctx); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6776 | child->perf_event_ctxp[ctxn] = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6777 | /* |
| 6778 | * If this context is a clone; unclone it so it can't get |
| 6779 | * swapped to another process while we're removing all |
| 6780 | * the events from it. |
| 6781 | */ |
| 6782 | unclone_ctx(child_ctx); |
Peter Zijlstra | 5e942bb | 2009-11-23 11:37:26 +0100 | [diff] [blame] | 6783 | update_context_time(child_ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 6784 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6785 | |
| 6786 | /* |
| 6787 | * Report the task dead after unscheduling the events so that we |
| 6788 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 6789 | * get a few PERF_RECORD_READ events. |
| 6790 | */ |
| 6791 | perf_event_task(child, child_ctx, 0); |
| 6792 | |
| 6793 | /* |
| 6794 | * We can recurse on the same lock type through: |
| 6795 | * |
| 6796 | * __perf_event_exit_task() |
| 6797 | * sync_child_event() |
| 6798 | * fput(parent_event->filp) |
| 6799 | * perf_release() |
| 6800 | * mutex_lock(&ctx->mutex) |
| 6801 | * |
| 6802 | * But since its the parent context it won't be the same instance. |
| 6803 | */ |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 6804 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6805 | |
| 6806 | again: |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 6807 | list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups, |
| 6808 | group_entry) |
| 6809 | __perf_event_exit_task(child_event, child_ctx, child); |
| 6810 | |
| 6811 | list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6812 | group_entry) |
| 6813 | __perf_event_exit_task(child_event, child_ctx, child); |
| 6814 | |
| 6815 | /* |
| 6816 | * If the last event was a group event, it will have appended all |
| 6817 | * its siblings to the list, but we obtained 'tmp' before that which |
| 6818 | * will still point to the list head terminating the iteration. |
| 6819 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 6820 | if (!list_empty(&child_ctx->pinned_groups) || |
| 6821 | !list_empty(&child_ctx->flexible_groups)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6822 | goto again; |
| 6823 | |
| 6824 | mutex_unlock(&child_ctx->mutex); |
| 6825 | |
| 6826 | put_ctx(child_ctx); |
| 6827 | } |
| 6828 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6829 | /* |
| 6830 | * When a child task exits, feed back event values to parent events. |
| 6831 | */ |
| 6832 | void perf_event_exit_task(struct task_struct *child) |
| 6833 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 6834 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6835 | int ctxn; |
| 6836 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 6837 | mutex_lock(&child->perf_event_mutex); |
| 6838 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 6839 | owner_entry) { |
| 6840 | list_del_init(&event->owner_entry); |
| 6841 | |
| 6842 | /* |
| 6843 | * Ensure the list deletion is visible before we clear |
| 6844 | * the owner, closes a race against perf_release() where |
| 6845 | * we need to serialize on the owner->perf_event_mutex. |
| 6846 | */ |
| 6847 | smp_wmb(); |
| 6848 | event->owner = NULL; |
| 6849 | } |
| 6850 | mutex_unlock(&child->perf_event_mutex); |
| 6851 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6852 | for_each_task_context_nr(ctxn) |
| 6853 | perf_event_exit_task_context(child, ctxn); |
| 6854 | } |
| 6855 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 6856 | static void perf_free_event(struct perf_event *event, |
| 6857 | struct perf_event_context *ctx) |
| 6858 | { |
| 6859 | struct perf_event *parent = event->parent; |
| 6860 | |
| 6861 | if (WARN_ON_ONCE(!parent)) |
| 6862 | return; |
| 6863 | |
| 6864 | mutex_lock(&parent->child_mutex); |
| 6865 | list_del_init(&event->child_list); |
| 6866 | mutex_unlock(&parent->child_mutex); |
| 6867 | |
| 6868 | fput(parent->filp); |
| 6869 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 6870 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 6871 | list_del_event(event, ctx); |
| 6872 | free_event(event); |
| 6873 | } |
| 6874 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6875 | /* |
| 6876 | * free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6877 | * perf_event_init_task below, used by fork() in case of fail. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6878 | */ |
| 6879 | void perf_event_free_task(struct task_struct *task) |
| 6880 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6881 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6882 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6883 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6884 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6885 | for_each_task_context_nr(ctxn) { |
| 6886 | ctx = task->perf_event_ctxp[ctxn]; |
| 6887 | if (!ctx) |
| 6888 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6889 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6890 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6891 | again: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6892 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 6893 | group_entry) |
| 6894 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6895 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6896 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 6897 | group_entry) |
| 6898 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6899 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6900 | if (!list_empty(&ctx->pinned_groups) || |
| 6901 | !list_empty(&ctx->flexible_groups)) |
| 6902 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6903 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6904 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6905 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6906 | put_ctx(ctx); |
| 6907 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6908 | } |
| 6909 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 6910 | void perf_event_delayed_put(struct task_struct *task) |
| 6911 | { |
| 6912 | int ctxn; |
| 6913 | |
| 6914 | for_each_task_context_nr(ctxn) |
| 6915 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 6916 | } |
| 6917 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 6918 | /* |
| 6919 | * inherit a event from parent task to child task: |
| 6920 | */ |
| 6921 | static struct perf_event * |
| 6922 | inherit_event(struct perf_event *parent_event, |
| 6923 | struct task_struct *parent, |
| 6924 | struct perf_event_context *parent_ctx, |
| 6925 | struct task_struct *child, |
| 6926 | struct perf_event *group_leader, |
| 6927 | struct perf_event_context *child_ctx) |
| 6928 | { |
| 6929 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 6930 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 6931 | |
| 6932 | /* |
| 6933 | * Instead of creating recursive hierarchies of events, |
| 6934 | * we link inherited events back to the original parent, |
| 6935 | * which has a filp for sure, which we use as the reference |
| 6936 | * count: |
| 6937 | */ |
| 6938 | if (parent_event->parent) |
| 6939 | parent_event = parent_event->parent; |
| 6940 | |
| 6941 | child_event = perf_event_alloc(&parent_event->attr, |
| 6942 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6943 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 6944 | group_leader, parent_event, |
| 6945 | NULL); |
| 6946 | if (IS_ERR(child_event)) |
| 6947 | return child_event; |
| 6948 | get_ctx(child_ctx); |
| 6949 | |
| 6950 | /* |
| 6951 | * Make the child state follow the state of the parent event, |
| 6952 | * not its attr.disabled bit. We hold the parent's mutex, |
| 6953 | * so we won't race with perf_event_{en, dis}able_family. |
| 6954 | */ |
| 6955 | if (parent_event->state >= PERF_EVENT_STATE_INACTIVE) |
| 6956 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 6957 | else |
| 6958 | child_event->state = PERF_EVENT_STATE_OFF; |
| 6959 | |
| 6960 | if (parent_event->attr.freq) { |
| 6961 | u64 sample_period = parent_event->hw.sample_period; |
| 6962 | struct hw_perf_event *hwc = &child_event->hw; |
| 6963 | |
| 6964 | hwc->sample_period = sample_period; |
| 6965 | hwc->last_period = sample_period; |
| 6966 | |
| 6967 | local64_set(&hwc->period_left, sample_period); |
| 6968 | } |
| 6969 | |
| 6970 | child_event->ctx = child_ctx; |
| 6971 | child_event->overflow_handler = parent_event->overflow_handler; |
| 6972 | |
| 6973 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 6974 | * Precalculate sample_data sizes |
| 6975 | */ |
| 6976 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 6977 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 6978 | |
| 6979 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 6980 | * Link it up in the child's context: |
| 6981 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 6982 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 6983 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 6984 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 6985 | |
| 6986 | /* |
| 6987 | * Get a reference to the parent filp - we will fput it |
| 6988 | * when the child event exits. This is safe to do because |
| 6989 | * we are in the parent and we know that the filp still |
| 6990 | * exists and has a nonzero count: |
| 6991 | */ |
| 6992 | atomic_long_inc(&parent_event->filp->f_count); |
| 6993 | |
| 6994 | /* |
| 6995 | * Link this into the parent event's child list |
| 6996 | */ |
| 6997 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 6998 | mutex_lock(&parent_event->child_mutex); |
| 6999 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 7000 | mutex_unlock(&parent_event->child_mutex); |
| 7001 | |
| 7002 | return child_event; |
| 7003 | } |
| 7004 | |
| 7005 | static int inherit_group(struct perf_event *parent_event, |
| 7006 | struct task_struct *parent, |
| 7007 | struct perf_event_context *parent_ctx, |
| 7008 | struct task_struct *child, |
| 7009 | struct perf_event_context *child_ctx) |
| 7010 | { |
| 7011 | struct perf_event *leader; |
| 7012 | struct perf_event *sub; |
| 7013 | struct perf_event *child_ctr; |
| 7014 | |
| 7015 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 7016 | child, NULL, child_ctx); |
| 7017 | if (IS_ERR(leader)) |
| 7018 | return PTR_ERR(leader); |
| 7019 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 7020 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 7021 | child, leader, child_ctx); |
| 7022 | if (IS_ERR(child_ctr)) |
| 7023 | return PTR_ERR(child_ctr); |
| 7024 | } |
| 7025 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7026 | } |
| 7027 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7028 | static int |
| 7029 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 7030 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7031 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7032 | int *inherited_all) |
| 7033 | { |
| 7034 | int ret; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7035 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7036 | |
| 7037 | if (!event->attr.inherit) { |
| 7038 | *inherited_all = 0; |
| 7039 | return 0; |
| 7040 | } |
| 7041 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7042 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7043 | if (!child_ctx) { |
| 7044 | /* |
| 7045 | * This is executed from the parent task context, so |
| 7046 | * inherit events that have been marked for cloning. |
| 7047 | * First allocate and initialize a context for the |
| 7048 | * child. |
| 7049 | */ |
| 7050 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 7051 | child_ctx = alloc_perf_context(event->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7052 | if (!child_ctx) |
| 7053 | return -ENOMEM; |
| 7054 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7055 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | ret = inherit_group(event, parent, parent_ctx, |
| 7059 | child, child_ctx); |
| 7060 | |
| 7061 | if (ret) |
| 7062 | *inherited_all = 0; |
| 7063 | |
| 7064 | return ret; |
| 7065 | } |
| 7066 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7067 | /* |
| 7068 | * Initialize the perf_event context in task_struct |
| 7069 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7070 | int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7071 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7072 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7073 | struct perf_event_context *cloned_ctx; |
| 7074 | struct perf_event *event; |
| 7075 | struct task_struct *parent = current; |
| 7076 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7077 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7078 | int ret = 0; |
| 7079 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7080 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7081 | return 0; |
| 7082 | |
| 7083 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7084 | * If the parent's context is a clone, pin it so it won't get |
| 7085 | * swapped under us. |
| 7086 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7087 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7088 | |
| 7089 | /* |
| 7090 | * No need to check if parent_ctx != NULL here; since we saw |
| 7091 | * it non-NULL earlier, the only reason for it to become NULL |
| 7092 | * is if we exit, and since we're currently in the middle of |
| 7093 | * a fork we can't be exiting at the same time. |
| 7094 | */ |
| 7095 | |
| 7096 | /* |
| 7097 | * Lock the parent list. No need to lock the child - not PID |
| 7098 | * hashed yet and not running, so nobody can access it. |
| 7099 | */ |
| 7100 | mutex_lock(&parent_ctx->mutex); |
| 7101 | |
| 7102 | /* |
| 7103 | * We dont have to disable NMIs - we are only looking at |
| 7104 | * the list, not manipulating it: |
| 7105 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7106 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7107 | ret = inherit_task_group(event, parent, parent_ctx, |
| 7108 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7109 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7110 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7111 | } |
| 7112 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7113 | /* |
| 7114 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 7115 | * to allocations, but we need to prevent rotation because |
| 7116 | * rotate_ctx() will change the list from interrupt context. |
| 7117 | */ |
| 7118 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 7119 | parent_ctx->rotate_disable = 1; |
| 7120 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 7121 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7122 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7123 | ret = inherit_task_group(event, parent, parent_ctx, |
| 7124 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7125 | if (ret) |
| 7126 | break; |
| 7127 | } |
| 7128 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7129 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 7130 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7131 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7132 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7133 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 7134 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7135 | /* |
| 7136 | * Mark the child context as a clone of the parent |
| 7137 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7138 | * |
| 7139 | * Note that if the parent is a clone, the holding of |
| 7140 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7141 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7142 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7143 | if (cloned_ctx) { |
| 7144 | child_ctx->parent_ctx = cloned_ctx; |
| 7145 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 7146 | } else { |
| 7147 | child_ctx->parent_ctx = parent_ctx; |
| 7148 | child_ctx->parent_gen = parent_ctx->generation; |
| 7149 | } |
| 7150 | get_ctx(child_ctx->parent_ctx); |
| 7151 | } |
| 7152 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7153 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7154 | mutex_unlock(&parent_ctx->mutex); |
| 7155 | |
| 7156 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7157 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7158 | |
| 7159 | return ret; |
| 7160 | } |
| 7161 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7162 | /* |
| 7163 | * Initialize the perf_event context in task_struct |
| 7164 | */ |
| 7165 | int perf_event_init_task(struct task_struct *child) |
| 7166 | { |
| 7167 | int ctxn, ret; |
| 7168 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 7169 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 7170 | mutex_init(&child->perf_event_mutex); |
| 7171 | INIT_LIST_HEAD(&child->perf_event_list); |
| 7172 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7173 | for_each_task_context_nr(ctxn) { |
| 7174 | ret = perf_event_init_context(child, ctxn); |
| 7175 | if (ret) |
| 7176 | return ret; |
| 7177 | } |
| 7178 | |
| 7179 | return 0; |
| 7180 | } |
| 7181 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7182 | static void __init perf_event_init_all_cpus(void) |
| 7183 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7184 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7185 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7186 | |
| 7187 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7188 | swhash = &per_cpu(swevent_htable, cpu); |
| 7189 | mutex_init(&swhash->hlist_mutex); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7190 | INIT_LIST_HEAD(&per_cpu(rotation_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7191 | } |
| 7192 | } |
| 7193 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7194 | static void __cpuinit perf_event_init_cpu(int cpu) |
| 7195 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7196 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7197 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7198 | mutex_lock(&swhash->hlist_mutex); |
| 7199 | if (swhash->hlist_refcount > 0) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7200 | struct swevent_hlist *hlist; |
| 7201 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7202 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 7203 | WARN_ON(!hlist); |
| 7204 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7205 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7206 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7207 | } |
| 7208 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 7209 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7210 | static void perf_pmu_rotate_stop(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7211 | { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7212 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 7213 | |
| 7214 | WARN_ON(!irqs_disabled()); |
| 7215 | |
| 7216 | list_del_init(&cpuctx->rotation_list); |
| 7217 | } |
| 7218 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7219 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7220 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7221 | struct perf_event_context *ctx = __info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7222 | struct perf_event *event, *tmp; |
| 7223 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7224 | perf_pmu_rotate_stop(ctx->pmu); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 7225 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7226 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7227 | __perf_remove_from_context(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7228 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7229 | __perf_remove_from_context(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7230 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7231 | |
| 7232 | static void perf_event_exit_cpu_context(int cpu) |
| 7233 | { |
| 7234 | struct perf_event_context *ctx; |
| 7235 | struct pmu *pmu; |
| 7236 | int idx; |
| 7237 | |
| 7238 | idx = srcu_read_lock(&pmus_srcu); |
| 7239 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 7240 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7241 | |
| 7242 | mutex_lock(&ctx->mutex); |
| 7243 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 7244 | mutex_unlock(&ctx->mutex); |
| 7245 | } |
| 7246 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7247 | } |
| 7248 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7249 | static void perf_event_exit_cpu(int cpu) |
| 7250 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7251 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7252 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7253 | mutex_lock(&swhash->hlist_mutex); |
| 7254 | swevent_hlist_release(swhash); |
| 7255 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7256 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7257 | perf_event_exit_cpu_context(cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7258 | } |
| 7259 | #else |
| 7260 | static inline void perf_event_exit_cpu(int cpu) { } |
| 7261 | #endif |
| 7262 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 7263 | static int |
| 7264 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 7265 | { |
| 7266 | int cpu; |
| 7267 | |
| 7268 | for_each_online_cpu(cpu) |
| 7269 | perf_event_exit_cpu(cpu); |
| 7270 | |
| 7271 | return NOTIFY_OK; |
| 7272 | } |
| 7273 | |
| 7274 | /* |
| 7275 | * Run the perf reboot notifier at the very last possible moment so that |
| 7276 | * the generic watchdog code runs as long as possible. |
| 7277 | */ |
| 7278 | static struct notifier_block perf_reboot_notifier = { |
| 7279 | .notifier_call = perf_reboot, |
| 7280 | .priority = INT_MIN, |
| 7281 | }; |
| 7282 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7283 | static int __cpuinit |
| 7284 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 7285 | { |
| 7286 | unsigned int cpu = (long)hcpu; |
| 7287 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7288 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7289 | |
| 7290 | case CPU_UP_PREPARE: |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7291 | case CPU_DOWN_FAILED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7292 | perf_event_init_cpu(cpu); |
| 7293 | break; |
| 7294 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7295 | case CPU_UP_CANCELED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7296 | case CPU_DOWN_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7297 | perf_event_exit_cpu(cpu); |
| 7298 | break; |
| 7299 | |
| 7300 | default: |
| 7301 | break; |
| 7302 | } |
| 7303 | |
| 7304 | return NOTIFY_OK; |
| 7305 | } |
| 7306 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7307 | void __init perf_event_init(void) |
| 7308 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 7309 | int ret; |
| 7310 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7311 | idr_init(&pmu_idr); |
| 7312 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7313 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7314 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7315 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 7316 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 7317 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7318 | perf_tp_register(); |
| 7319 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 7320 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 7321 | |
| 7322 | ret = init_hw_breakpoint(); |
| 7323 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7324 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7325 | |
| 7326 | static int __init perf_event_sysfs_init(void) |
| 7327 | { |
| 7328 | struct pmu *pmu; |
| 7329 | int ret; |
| 7330 | |
| 7331 | mutex_lock(&pmus_lock); |
| 7332 | |
| 7333 | ret = bus_register(&pmu_bus); |
| 7334 | if (ret) |
| 7335 | goto unlock; |
| 7336 | |
| 7337 | list_for_each_entry(pmu, &pmus, entry) { |
| 7338 | if (!pmu->name || pmu->type < 0) |
| 7339 | continue; |
| 7340 | |
| 7341 | ret = pmu_dev_alloc(pmu); |
| 7342 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 7343 | } |
| 7344 | pmu_bus_running = 1; |
| 7345 | ret = 0; |
| 7346 | |
| 7347 | unlock: |
| 7348 | mutex_unlock(&pmus_lock); |
| 7349 | |
| 7350 | return ret; |
| 7351 | } |
| 7352 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7353 | |
| 7354 | #ifdef CONFIG_CGROUP_PERF |
| 7355 | static struct cgroup_subsys_state *perf_cgroup_create( |
| 7356 | struct cgroup_subsys *ss, struct cgroup *cont) |
| 7357 | { |
| 7358 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7359 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 7360 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7361 | if (!jc) |
| 7362 | return ERR_PTR(-ENOMEM); |
| 7363 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7364 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 7365 | if (!jc->info) { |
| 7366 | kfree(jc); |
| 7367 | return ERR_PTR(-ENOMEM); |
| 7368 | } |
| 7369 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7370 | return &jc->css; |
| 7371 | } |
| 7372 | |
| 7373 | static void perf_cgroup_destroy(struct cgroup_subsys *ss, |
| 7374 | struct cgroup *cont) |
| 7375 | { |
| 7376 | struct perf_cgroup *jc; |
| 7377 | jc = container_of(cgroup_subsys_state(cont, perf_subsys_id), |
| 7378 | struct perf_cgroup, css); |
| 7379 | free_percpu(jc->info); |
| 7380 | kfree(jc); |
| 7381 | } |
| 7382 | |
| 7383 | static int __perf_cgroup_move(void *info) |
| 7384 | { |
| 7385 | struct task_struct *task = info; |
| 7386 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
| 7387 | return 0; |
| 7388 | } |
| 7389 | |
| 7390 | static void perf_cgroup_move(struct task_struct *task) |
| 7391 | { |
| 7392 | task_function_call(task, __perf_cgroup_move, task); |
| 7393 | } |
| 7394 | |
| 7395 | static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
| 7396 | struct cgroup *old_cgrp, struct task_struct *task, |
| 7397 | bool threadgroup) |
| 7398 | { |
| 7399 | perf_cgroup_move(task); |
| 7400 | if (threadgroup) { |
| 7401 | struct task_struct *c; |
| 7402 | rcu_read_lock(); |
| 7403 | list_for_each_entry_rcu(c, &task->thread_group, thread_group) { |
| 7404 | perf_cgroup_move(c); |
| 7405 | } |
| 7406 | rcu_read_unlock(); |
| 7407 | } |
| 7408 | } |
| 7409 | |
| 7410 | static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp, |
| 7411 | struct cgroup *old_cgrp, struct task_struct *task) |
| 7412 | { |
| 7413 | /* |
| 7414 | * cgroup_exit() is called in the copy_process() failure path. |
| 7415 | * Ignore this case since the task hasn't ran yet, this avoids |
| 7416 | * trying to poke a half freed task state from generic code. |
| 7417 | */ |
| 7418 | if (!(task->flags & PF_EXITING)) |
| 7419 | return; |
| 7420 | |
| 7421 | perf_cgroup_move(task); |
| 7422 | } |
| 7423 | |
| 7424 | struct cgroup_subsys perf_subsys = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 7425 | .name = "perf_event", |
| 7426 | .subsys_id = perf_subsys_id, |
| 7427 | .create = perf_cgroup_create, |
| 7428 | .destroy = perf_cgroup_destroy, |
| 7429 | .exit = perf_cgroup_exit, |
| 7430 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7431 | }; |
| 7432 | #endif /* CONFIG_CGROUP_PERF */ |