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> |
Al Viro | d36b691 | 2011-12-29 17:09:01 -0500 | [diff] [blame] | 7 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 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> |
Frederic Weisbecker | 12351ef | 2013-04-20 15:48:22 +0200 | [diff] [blame] | 21 | #include <linux/tick.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 22 | #include <linux/sysfs.h> |
| 23 | #include <linux/dcache.h> |
| 24 | #include <linux/percpu.h> |
| 25 | #include <linux/ptrace.h> |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 26 | #include <linux/reboot.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 27 | #include <linux/vmstat.h> |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 28 | #include <linux/device.h> |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 31 | #include <linux/hardirq.h> |
| 32 | #include <linux/rculist.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/syscalls.h> |
| 35 | #include <linux/anon_inodes.h> |
| 36 | #include <linux/kernel_stat.h> |
| 37 | #include <linux/perf_event.h> |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 38 | #include <linux/ftrace_event.h> |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 39 | #include <linux/hw_breakpoint.h> |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 40 | #include <linux/mm_types.h> |
Li Zefan | 877c685 | 2013-03-05 11:38:08 +0800 | [diff] [blame] | 41 | #include <linux/cgroup.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 42 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 43 | #include "internal.h" |
| 44 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 45 | #include <asm/irq_regs.h> |
| 46 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 47 | struct remote_function_call { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 48 | struct task_struct *p; |
| 49 | int (*func)(void *info); |
| 50 | void *info; |
| 51 | int ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static void remote_function(void *data) |
| 55 | { |
| 56 | struct remote_function_call *tfc = data; |
| 57 | struct task_struct *p = tfc->p; |
| 58 | |
| 59 | if (p) { |
| 60 | tfc->ret = -EAGAIN; |
| 61 | if (task_cpu(p) != smp_processor_id() || !task_curr(p)) |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | tfc->ret = tfc->func(tfc->info); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * task_function_call - call a function on the cpu on which a task runs |
| 70 | * @p: the task to evaluate |
| 71 | * @func: the function to be called |
| 72 | * @info: the function call argument |
| 73 | * |
| 74 | * Calls the function @func when the task is currently running. This might |
| 75 | * be on the current CPU, which just calls the function directly |
| 76 | * |
| 77 | * returns: @func return value, or |
| 78 | * -ESRCH - when the process isn't running |
| 79 | * -EAGAIN - when the process moved away |
| 80 | */ |
| 81 | static int |
| 82 | task_function_call(struct task_struct *p, int (*func) (void *info), void *info) |
| 83 | { |
| 84 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 85 | .p = p, |
| 86 | .func = func, |
| 87 | .info = info, |
| 88 | .ret = -ESRCH, /* No such (running) process */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | if (task_curr(p)) |
| 92 | smp_call_function_single(task_cpu(p), remote_function, &data, 1); |
| 93 | |
| 94 | return data.ret; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * cpu_function_call - call a function on the cpu |
| 99 | * @func: the function to be called |
| 100 | * @info: the function call argument |
| 101 | * |
| 102 | * Calls the function @func on the remote cpu. |
| 103 | * |
| 104 | * returns: @func return value or -ENXIO when the cpu is offline |
| 105 | */ |
| 106 | static int cpu_function_call(int cpu, int (*func) (void *info), void *info) |
| 107 | { |
| 108 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 109 | .p = NULL, |
| 110 | .func = func, |
| 111 | .info = info, |
| 112 | .ret = -ENXIO, /* No such CPU */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | smp_call_function_single(cpu, remote_function, &data, 1); |
| 116 | |
| 117 | return data.ret; |
| 118 | } |
| 119 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 120 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
| 121 | PERF_FLAG_FD_OUTPUT |\ |
| 122 | PERF_FLAG_PID_CGROUP) |
| 123 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 124 | /* |
| 125 | * branch priv levels that need permission checks |
| 126 | */ |
| 127 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ |
| 128 | (PERF_SAMPLE_BRANCH_KERNEL |\ |
| 129 | PERF_SAMPLE_BRANCH_HV) |
| 130 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 131 | enum event_type_t { |
| 132 | EVENT_FLEXIBLE = 0x1, |
| 133 | EVENT_PINNED = 0x2, |
| 134 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
| 135 | }; |
| 136 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 137 | /* |
| 138 | * perf_sched_events : >0 events exist |
| 139 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu |
| 140 | */ |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 141 | struct static_key_deferred perf_sched_events __read_mostly; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 142 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 143 | static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 144 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 145 | static atomic_t nr_mmap_events __read_mostly; |
| 146 | static atomic_t nr_comm_events __read_mostly; |
| 147 | static atomic_t nr_task_events __read_mostly; |
| 148 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 149 | static LIST_HEAD(pmus); |
| 150 | static DEFINE_MUTEX(pmus_lock); |
| 151 | static struct srcu_struct pmus_srcu; |
| 152 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 153 | /* |
| 154 | * perf event paranoia level: |
| 155 | * -1 - not paranoid at all |
| 156 | * 0 - disallow raw tracepoint access for unpriv |
| 157 | * 1 - disallow cpu events for unpriv |
| 158 | * 2 - disallow kernel profiling for unpriv |
| 159 | */ |
| 160 | int sysctl_perf_event_paranoid __read_mostly = 1; |
| 161 | |
Frederic Weisbecker | 2044338 | 2011-03-31 03:33:29 +0200 | [diff] [blame] | 162 | /* Minimum for 512 kiB + 1 user control page */ |
| 163 | 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] | 164 | |
| 165 | /* |
| 166 | * max perf event sample rate |
| 167 | */ |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 168 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
| 169 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) |
| 170 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 |
| 171 | |
| 172 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; |
| 173 | |
| 174 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); |
| 175 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; |
| 176 | |
| 177 | static atomic_t perf_sample_allowed_ns __read_mostly = |
| 178 | ATOMIC_INIT( DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100); |
| 179 | |
| 180 | void update_perf_cpu_limits(void) |
| 181 | { |
| 182 | u64 tmp = perf_sample_period_ns; |
| 183 | |
| 184 | tmp *= sysctl_perf_cpu_time_max_percent; |
Stephane Eranian | e530292 | 2013-07-05 00:30:11 +0200 | [diff] [blame] | 185 | do_div(tmp, 100); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 186 | atomic_set(&perf_sample_allowed_ns, tmp); |
| 187 | } |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 188 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 189 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
| 190 | |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 191 | int perf_proc_update_handler(struct ctl_table *table, int write, |
| 192 | void __user *buffer, size_t *lenp, |
| 193 | loff_t *ppos) |
| 194 | { |
| 195 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 196 | |
| 197 | if (ret || !write) |
| 198 | return ret; |
| 199 | |
| 200 | max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 201 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 202 | update_perf_cpu_limits(); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 203 | |
| 204 | return 0; |
| 205 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 206 | |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 207 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; |
| 208 | |
| 209 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, |
| 210 | void __user *buffer, size_t *lenp, |
| 211 | loff_t *ppos) |
| 212 | { |
| 213 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 214 | |
| 215 | if (ret || !write) |
| 216 | return ret; |
| 217 | |
| 218 | update_perf_cpu_limits(); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * perf samples are done in some very critical code paths (NMIs). |
| 225 | * If they take too much CPU time, the system can lock up and not |
| 226 | * get any real work done. This will drop the sample rate when |
| 227 | * we detect that events are taking too long. |
| 228 | */ |
| 229 | #define NR_ACCUMULATED_SAMPLES 128 |
| 230 | DEFINE_PER_CPU(u64, running_sample_length); |
| 231 | |
| 232 | void perf_sample_event_took(u64 sample_len_ns) |
| 233 | { |
| 234 | u64 avg_local_sample_len; |
Stephane Eranian | e530292 | 2013-07-05 00:30:11 +0200 | [diff] [blame] | 235 | u64 local_samples_len; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 236 | |
| 237 | if (atomic_read(&perf_sample_allowed_ns) == 0) |
| 238 | return; |
| 239 | |
| 240 | /* decay the counter by 1 average sample */ |
| 241 | local_samples_len = __get_cpu_var(running_sample_length); |
| 242 | local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES; |
| 243 | local_samples_len += sample_len_ns; |
| 244 | __get_cpu_var(running_sample_length) = local_samples_len; |
| 245 | |
| 246 | /* |
| 247 | * note: this will be biased artifically low until we have |
| 248 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us |
| 249 | * from having to maintain a count. |
| 250 | */ |
| 251 | avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES; |
| 252 | |
| 253 | if (avg_local_sample_len <= atomic_read(&perf_sample_allowed_ns)) |
| 254 | return; |
| 255 | |
| 256 | if (max_samples_per_tick <= 1) |
| 257 | return; |
| 258 | |
| 259 | max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2); |
| 260 | sysctl_perf_event_sample_rate = max_samples_per_tick * HZ; |
| 261 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 262 | |
| 263 | printk_ratelimited(KERN_WARNING |
| 264 | "perf samples too long (%lld > %d), lowering " |
| 265 | "kernel.perf_event_max_sample_rate to %d\n", |
| 266 | avg_local_sample_len, |
| 267 | atomic_read(&perf_sample_allowed_ns), |
| 268 | sysctl_perf_event_sample_rate); |
| 269 | |
| 270 | update_perf_cpu_limits(); |
| 271 | } |
| 272 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 273 | static atomic64_t perf_event_id; |
| 274 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 275 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 276 | enum event_type_t event_type); |
| 277 | |
| 278 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 279 | enum event_type_t event_type, |
| 280 | struct task_struct *task); |
| 281 | |
| 282 | static void update_context_time(struct perf_event_context *ctx); |
| 283 | static u64 perf_event_time(struct perf_event *event); |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 284 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 285 | void __weak perf_event_print_debug(void) { } |
| 286 | |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 287 | extern __weak const char *perf_pmu_name(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 288 | { |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 289 | return "pmu"; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 292 | static inline u64 perf_clock(void) |
| 293 | { |
| 294 | return local_clock(); |
| 295 | } |
| 296 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 297 | static inline struct perf_cpu_context * |
| 298 | __get_cpu_context(struct perf_event_context *ctx) |
| 299 | { |
| 300 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); |
| 301 | } |
| 302 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 303 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, |
| 304 | struct perf_event_context *ctx) |
| 305 | { |
| 306 | raw_spin_lock(&cpuctx->ctx.lock); |
| 307 | if (ctx) |
| 308 | raw_spin_lock(&ctx->lock); |
| 309 | } |
| 310 | |
| 311 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, |
| 312 | struct perf_event_context *ctx) |
| 313 | { |
| 314 | if (ctx) |
| 315 | raw_spin_unlock(&ctx->lock); |
| 316 | raw_spin_unlock(&cpuctx->ctx.lock); |
| 317 | } |
| 318 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 319 | #ifdef CONFIG_CGROUP_PERF |
| 320 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 321 | /* |
Li Zefan | 877c685 | 2013-03-05 11:38:08 +0800 | [diff] [blame] | 322 | * perf_cgroup_info keeps track of time_enabled for a cgroup. |
| 323 | * This is a per-cpu dynamically allocated data structure. |
| 324 | */ |
| 325 | struct perf_cgroup_info { |
| 326 | u64 time; |
| 327 | u64 timestamp; |
| 328 | }; |
| 329 | |
| 330 | struct perf_cgroup { |
| 331 | struct cgroup_subsys_state css; |
Namhyung Kim | 86e213e | 2013-03-18 18:56:34 +0900 | [diff] [blame] | 332 | struct perf_cgroup_info __percpu *info; |
Li Zefan | 877c685 | 2013-03-05 11:38:08 +0800 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 336 | * Must ensure cgroup is pinned (css_get) before calling |
| 337 | * this function. In other words, we cannot call this function |
| 338 | * if there is no cgroup event for the current CPU context. |
| 339 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 340 | static inline struct perf_cgroup * |
| 341 | perf_cgroup_from_task(struct task_struct *task) |
| 342 | { |
| 343 | return container_of(task_subsys_state(task, perf_subsys_id), |
| 344 | struct perf_cgroup, css); |
| 345 | } |
| 346 | |
| 347 | static inline bool |
| 348 | perf_cgroup_match(struct perf_event *event) |
| 349 | { |
| 350 | struct perf_event_context *ctx = event->ctx; |
| 351 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 352 | |
Tejun Heo | ef824fa | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 353 | /* @event doesn't care about cgroup */ |
| 354 | if (!event->cgrp) |
| 355 | return true; |
| 356 | |
| 357 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ |
| 358 | if (!cpuctx->cgrp) |
| 359 | return false; |
| 360 | |
| 361 | /* |
| 362 | * Cgroup scoping is recursive. An event enabled for a cgroup is |
| 363 | * also enabled for all its descendant cgroups. If @cpuctx's |
| 364 | * cgroup is a descendant of @event's (the test covers identity |
| 365 | * case), it's a match. |
| 366 | */ |
| 367 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, |
| 368 | event->cgrp->css.cgroup); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Salman Qazi | 9c5da09 | 2012-06-14 15:31:09 -0700 | [diff] [blame] | 371 | static inline bool perf_tryget_cgroup(struct perf_event *event) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 372 | { |
Salman Qazi | 9c5da09 | 2012-06-14 15:31:09 -0700 | [diff] [blame] | 373 | return css_tryget(&event->cgrp->css); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static inline void perf_put_cgroup(struct perf_event *event) |
| 377 | { |
| 378 | css_put(&event->cgrp->css); |
| 379 | } |
| 380 | |
| 381 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 382 | { |
| 383 | perf_put_cgroup(event); |
| 384 | event->cgrp = NULL; |
| 385 | } |
| 386 | |
| 387 | static inline int is_cgroup_event(struct perf_event *event) |
| 388 | { |
| 389 | return event->cgrp != NULL; |
| 390 | } |
| 391 | |
| 392 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 393 | { |
| 394 | struct perf_cgroup_info *t; |
| 395 | |
| 396 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 397 | return t->time; |
| 398 | } |
| 399 | |
| 400 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) |
| 401 | { |
| 402 | struct perf_cgroup_info *info; |
| 403 | u64 now; |
| 404 | |
| 405 | now = perf_clock(); |
| 406 | |
| 407 | info = this_cpu_ptr(cgrp->info); |
| 408 | |
| 409 | info->time += now - info->timestamp; |
| 410 | info->timestamp = now; |
| 411 | } |
| 412 | |
| 413 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 414 | { |
| 415 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; |
| 416 | if (cgrp_out) |
| 417 | __update_cgrp_time(cgrp_out); |
| 418 | } |
| 419 | |
| 420 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 421 | { |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 422 | struct perf_cgroup *cgrp; |
| 423 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 424 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 425 | * ensure we access cgroup data only when needed and |
| 426 | * when we know the cgroup is pinned (css_get) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 427 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 428 | if (!is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 429 | return; |
| 430 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 431 | cgrp = perf_cgroup_from_task(current); |
| 432 | /* |
| 433 | * Do not update time when cgroup is not active |
| 434 | */ |
| 435 | if (cgrp == event->cgrp) |
| 436 | __update_cgrp_time(event->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 440 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 441 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 442 | { |
| 443 | struct perf_cgroup *cgrp; |
| 444 | struct perf_cgroup_info *info; |
| 445 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 446 | /* |
| 447 | * ctx->lock held by caller |
| 448 | * ensure we do not access cgroup data |
| 449 | * unless we have the cgroup pinned (css_get) |
| 450 | */ |
| 451 | if (!task || !ctx->nr_cgroups) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 452 | return; |
| 453 | |
| 454 | cgrp = perf_cgroup_from_task(task); |
| 455 | info = this_cpu_ptr(cgrp->info); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 456 | info->timestamp = ctx->timestamp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
| 460 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ |
| 461 | |
| 462 | /* |
| 463 | * reschedule events based on the cgroup constraint of task. |
| 464 | * |
| 465 | * mode SWOUT : schedule out everything |
| 466 | * mode SWIN : schedule in based on cgroup for next |
| 467 | */ |
| 468 | void perf_cgroup_switch(struct task_struct *task, int mode) |
| 469 | { |
| 470 | struct perf_cpu_context *cpuctx; |
| 471 | struct pmu *pmu; |
| 472 | unsigned long flags; |
| 473 | |
| 474 | /* |
| 475 | * disable interrupts to avoid geting nr_cgroup |
| 476 | * changes via __perf_event_disable(). Also |
| 477 | * avoids preemption. |
| 478 | */ |
| 479 | local_irq_save(flags); |
| 480 | |
| 481 | /* |
| 482 | * we reschedule only in the presence of cgroup |
| 483 | * constrained events. |
| 484 | */ |
| 485 | rcu_read_lock(); |
| 486 | |
| 487 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 488 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 489 | if (cpuctx->unique_pmu != pmu) |
| 490 | continue; /* ensure we process each cpuctx once */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 491 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 492 | /* |
| 493 | * perf_cgroup_events says at least one |
| 494 | * context on this CPU has cgroup events. |
| 495 | * |
| 496 | * ctx->nr_cgroups reports the number of cgroup |
| 497 | * events for a context. |
| 498 | */ |
| 499 | if (cpuctx->ctx.nr_cgroups > 0) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 500 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 501 | perf_pmu_disable(cpuctx->ctx.pmu); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 502 | |
| 503 | if (mode & PERF_CGROUP_SWOUT) { |
| 504 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 505 | /* |
| 506 | * must not be done before ctxswout due |
| 507 | * to event_filter_match() in event_sched_out() |
| 508 | */ |
| 509 | cpuctx->cgrp = NULL; |
| 510 | } |
| 511 | |
| 512 | if (mode & PERF_CGROUP_SWIN) { |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 513 | WARN_ON_ONCE(cpuctx->cgrp); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 514 | /* |
| 515 | * set cgrp before ctxsw in to allow |
| 516 | * event_filter_match() to not have to pass |
| 517 | * task around |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 518 | */ |
| 519 | cpuctx->cgrp = perf_cgroup_from_task(task); |
| 520 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
| 521 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 522 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 523 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 524 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | rcu_read_unlock(); |
| 528 | |
| 529 | local_irq_restore(flags); |
| 530 | } |
| 531 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 532 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 533 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 534 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 535 | struct perf_cgroup *cgrp1; |
| 536 | struct perf_cgroup *cgrp2 = NULL; |
| 537 | |
| 538 | /* |
| 539 | * we come here when we know perf_cgroup_events > 0 |
| 540 | */ |
| 541 | cgrp1 = perf_cgroup_from_task(task); |
| 542 | |
| 543 | /* |
| 544 | * next is NULL when called from perf_event_enable_on_exec() |
| 545 | * that will systematically cause a cgroup_switch() |
| 546 | */ |
| 547 | if (next) |
| 548 | cgrp2 = perf_cgroup_from_task(next); |
| 549 | |
| 550 | /* |
| 551 | * only schedule out current cgroup events if we know |
| 552 | * that we are switching to a different cgroup. Otherwise, |
| 553 | * do no touch the cgroup events. |
| 554 | */ |
| 555 | if (cgrp1 != cgrp2) |
| 556 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 559 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 560 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 561 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 562 | struct perf_cgroup *cgrp1; |
| 563 | struct perf_cgroup *cgrp2 = NULL; |
| 564 | |
| 565 | /* |
| 566 | * we come here when we know perf_cgroup_events > 0 |
| 567 | */ |
| 568 | cgrp1 = perf_cgroup_from_task(task); |
| 569 | |
| 570 | /* prev can never be NULL */ |
| 571 | cgrp2 = perf_cgroup_from_task(prev); |
| 572 | |
| 573 | /* |
| 574 | * only need to schedule in cgroup events if we are changing |
| 575 | * cgroup during ctxsw. Cgroup events were not scheduled |
| 576 | * out of ctxsw out if that was not the case. |
| 577 | */ |
| 578 | if (cgrp1 != cgrp2) |
| 579 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
| 583 | struct perf_event_attr *attr, |
| 584 | struct perf_event *group_leader) |
| 585 | { |
| 586 | struct perf_cgroup *cgrp; |
| 587 | struct cgroup_subsys_state *css; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 588 | struct fd f = fdget(fd); |
| 589 | int ret = 0; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 590 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 591 | if (!f.file) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 592 | return -EBADF; |
| 593 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 594 | css = cgroup_css_from_dir(f.file, perf_subsys_id); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 595 | if (IS_ERR(css)) { |
| 596 | ret = PTR_ERR(css); |
| 597 | goto out; |
| 598 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 599 | |
| 600 | cgrp = container_of(css, struct perf_cgroup, css); |
| 601 | event->cgrp = cgrp; |
| 602 | |
Li Zefan | f75e18c | 2011-03-03 14:25:50 +0800 | [diff] [blame] | 603 | /* must be done before we fput() the file */ |
Salman Qazi | 9c5da09 | 2012-06-14 15:31:09 -0700 | [diff] [blame] | 604 | if (!perf_tryget_cgroup(event)) { |
| 605 | event->cgrp = NULL; |
| 606 | ret = -ENOENT; |
| 607 | goto out; |
| 608 | } |
Li Zefan | f75e18c | 2011-03-03 14:25:50 +0800 | [diff] [blame] | 609 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 610 | /* |
| 611 | * all events in a group must monitor |
| 612 | * the same cgroup because a task belongs |
| 613 | * to only one perf cgroup at a time |
| 614 | */ |
| 615 | if (group_leader && group_leader->cgrp != cgrp) { |
| 616 | perf_detach_cgroup(event); |
| 617 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 618 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 619 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 620 | fdput(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | static inline void |
| 625 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 626 | { |
| 627 | struct perf_cgroup_info *t; |
| 628 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 629 | event->shadow_ctx_time = now - t->timestamp; |
| 630 | } |
| 631 | |
| 632 | static inline void |
| 633 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 634 | { |
| 635 | /* |
| 636 | * when the current task's perf cgroup does not match |
| 637 | * the event's, we need to remember to call the |
| 638 | * perf_mark_enable() function the first time a task with |
| 639 | * a matching perf cgroup is scheduled in. |
| 640 | */ |
| 641 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 642 | event->cgrp_defer_enabled = 1; |
| 643 | } |
| 644 | |
| 645 | static inline void |
| 646 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 647 | struct perf_event_context *ctx) |
| 648 | { |
| 649 | struct perf_event *sub; |
| 650 | u64 tstamp = perf_event_time(event); |
| 651 | |
| 652 | if (!event->cgrp_defer_enabled) |
| 653 | return; |
| 654 | |
| 655 | event->cgrp_defer_enabled = 0; |
| 656 | |
| 657 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 658 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 659 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 660 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 661 | sub->cgrp_defer_enabled = 0; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | #else /* !CONFIG_CGROUP_PERF */ |
| 666 | |
| 667 | static inline bool |
| 668 | perf_cgroup_match(struct perf_event *event) |
| 669 | { |
| 670 | return true; |
| 671 | } |
| 672 | |
| 673 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 674 | {} |
| 675 | |
| 676 | static inline int is_cgroup_event(struct perf_event *event) |
| 677 | { |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 682 | { |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 687 | { |
| 688 | } |
| 689 | |
| 690 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 691 | { |
| 692 | } |
| 693 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 694 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 695 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 696 | { |
| 697 | } |
| 698 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 699 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 700 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 701 | { |
| 702 | } |
| 703 | |
| 704 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 705 | struct perf_event_attr *attr, |
| 706 | struct perf_event *group_leader) |
| 707 | { |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | |
| 711 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 712 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 713 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 714 | { |
| 715 | } |
| 716 | |
| 717 | void |
| 718 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 719 | { |
| 720 | } |
| 721 | |
| 722 | static inline void |
| 723 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 724 | { |
| 725 | } |
| 726 | |
| 727 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 728 | { |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static inline void |
| 733 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 734 | { |
| 735 | } |
| 736 | |
| 737 | static inline void |
| 738 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 739 | struct perf_event_context *ctx) |
| 740 | { |
| 741 | } |
| 742 | #endif |
| 743 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 744 | /* |
| 745 | * set default to be dependent on timer tick just |
| 746 | * like original code |
| 747 | */ |
| 748 | #define PERF_CPU_HRTIMER (1000 / HZ) |
| 749 | /* |
| 750 | * function must be called with interrupts disbled |
| 751 | */ |
| 752 | static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr) |
| 753 | { |
| 754 | struct perf_cpu_context *cpuctx; |
| 755 | enum hrtimer_restart ret = HRTIMER_NORESTART; |
| 756 | int rotations = 0; |
| 757 | |
| 758 | WARN_ON(!irqs_disabled()); |
| 759 | |
| 760 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); |
| 761 | |
| 762 | rotations = perf_rotate_context(cpuctx); |
| 763 | |
| 764 | /* |
| 765 | * arm timer if needed |
| 766 | */ |
| 767 | if (rotations) { |
| 768 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
| 769 | ret = HRTIMER_RESTART; |
| 770 | } |
| 771 | |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | /* CPU is going down */ |
| 776 | void perf_cpu_hrtimer_cancel(int cpu) |
| 777 | { |
| 778 | struct perf_cpu_context *cpuctx; |
| 779 | struct pmu *pmu; |
| 780 | unsigned long flags; |
| 781 | |
| 782 | if (WARN_ON(cpu != smp_processor_id())) |
| 783 | return; |
| 784 | |
| 785 | local_irq_save(flags); |
| 786 | |
| 787 | rcu_read_lock(); |
| 788 | |
| 789 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 790 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 791 | |
| 792 | if (pmu->task_ctx_nr == perf_sw_context) |
| 793 | continue; |
| 794 | |
| 795 | hrtimer_cancel(&cpuctx->hrtimer); |
| 796 | } |
| 797 | |
| 798 | rcu_read_unlock(); |
| 799 | |
| 800 | local_irq_restore(flags); |
| 801 | } |
| 802 | |
| 803 | static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
| 804 | { |
| 805 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 806 | struct pmu *pmu = cpuctx->ctx.pmu; |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 807 | int timer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 808 | |
| 809 | /* no multiplexing needed for SW PMU */ |
| 810 | if (pmu->task_ctx_nr == perf_sw_context) |
| 811 | return; |
| 812 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 813 | /* |
| 814 | * check default is sane, if not set then force to |
| 815 | * default interval (1/tick) |
| 816 | */ |
| 817 | timer = pmu->hrtimer_interval_ms; |
| 818 | if (timer < 1) |
| 819 | timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; |
| 820 | |
| 821 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 822 | |
| 823 | hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
| 824 | hr->function = perf_cpu_hrtimer_handler; |
| 825 | } |
| 826 | |
| 827 | static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx) |
| 828 | { |
| 829 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 830 | struct pmu *pmu = cpuctx->ctx.pmu; |
| 831 | |
| 832 | /* not for SW PMU */ |
| 833 | if (pmu->task_ctx_nr == perf_sw_context) |
| 834 | return; |
| 835 | |
| 836 | if (hrtimer_active(hr)) |
| 837 | return; |
| 838 | |
| 839 | if (!hrtimer_callback_running(hr)) |
| 840 | __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval, |
| 841 | 0, HRTIMER_MODE_REL_PINNED, 0); |
| 842 | } |
| 843 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 844 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 845 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 846 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 847 | if (!(*count)++) |
| 848 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 849 | } |
| 850 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 851 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 852 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 853 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 854 | if (!--(*count)) |
| 855 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 856 | } |
| 857 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 858 | static DEFINE_PER_CPU(struct list_head, rotation_list); |
| 859 | |
| 860 | /* |
| 861 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 862 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 863 | * disabled, while rotate_context is called from IRQ context. |
| 864 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 865 | static void perf_pmu_rotate_start(struct pmu *pmu) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 866 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 867 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 868 | struct list_head *head = &__get_cpu_var(rotation_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 869 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 870 | WARN_ON(!irqs_disabled()); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 871 | |
Frederic Weisbecker | 12351ef | 2013-04-20 15:48:22 +0200 | [diff] [blame] | 872 | if (list_empty(&cpuctx->rotation_list)) { |
| 873 | int was_empty = list_empty(head); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 874 | list_add(&cpuctx->rotation_list, head); |
Frederic Weisbecker | 12351ef | 2013-04-20 15:48:22 +0200 | [diff] [blame] | 875 | if (was_empty) |
| 876 | tick_nohz_full_kick(); |
| 877 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static void get_ctx(struct perf_event_context *ctx) |
| 881 | { |
| 882 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 883 | } |
| 884 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 885 | static void put_ctx(struct perf_event_context *ctx) |
| 886 | { |
| 887 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 888 | if (ctx->parent_ctx) |
| 889 | put_ctx(ctx->parent_ctx); |
| 890 | if (ctx->task) |
| 891 | put_task_struct(ctx->task); |
Lai Jiangshan | cb796ff | 2011-03-18 12:07:41 +0800 | [diff] [blame] | 892 | kfree_rcu(ctx, rcu_head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | |
| 896 | static void unclone_ctx(struct perf_event_context *ctx) |
| 897 | { |
| 898 | if (ctx->parent_ctx) { |
| 899 | put_ctx(ctx->parent_ctx); |
| 900 | ctx->parent_ctx = NULL; |
| 901 | } |
| 902 | } |
| 903 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 904 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 905 | { |
| 906 | /* |
| 907 | * only top level events have the pid namespace they were created in |
| 908 | */ |
| 909 | if (event->parent) |
| 910 | event = event->parent; |
| 911 | |
| 912 | return task_tgid_nr_ns(p, event->ns); |
| 913 | } |
| 914 | |
| 915 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 916 | { |
| 917 | /* |
| 918 | * only top level events have the pid namespace they were created in |
| 919 | */ |
| 920 | if (event->parent) |
| 921 | event = event->parent; |
| 922 | |
| 923 | return task_pid_nr_ns(p, event->ns); |
| 924 | } |
| 925 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 926 | /* |
| 927 | * If we inherit events we want to return the parent event id |
| 928 | * to userspace. |
| 929 | */ |
| 930 | static u64 primary_event_id(struct perf_event *event) |
| 931 | { |
| 932 | u64 id = event->id; |
| 933 | |
| 934 | if (event->parent) |
| 935 | id = event->parent->id; |
| 936 | |
| 937 | return id; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Get the perf_event_context for a task and lock it. |
| 942 | * This has to cope with with the fact that until it is locked, |
| 943 | * the context could get moved to another task. |
| 944 | */ |
| 945 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 946 | 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] | 947 | { |
| 948 | struct perf_event_context *ctx; |
| 949 | |
| 950 | rcu_read_lock(); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 951 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 952 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 953 | if (ctx) { |
| 954 | /* |
| 955 | * If this context is a clone of another, it might |
| 956 | * get swapped for another underneath us by |
| 957 | * perf_event_task_sched_out, though the |
| 958 | * rcu_read_lock() protects us from any context |
| 959 | * getting freed. Lock the context and check if it |
| 960 | * got swapped before we could get the lock, and retry |
| 961 | * if so. If we locked the right context, then it |
| 962 | * can't get swapped on us any more. |
| 963 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 964 | raw_spin_lock_irqsave(&ctx->lock, *flags); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 965 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 966 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 967 | goto retry; |
| 968 | } |
| 969 | |
| 970 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 971 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 972 | ctx = NULL; |
| 973 | } |
| 974 | } |
| 975 | rcu_read_unlock(); |
| 976 | return ctx; |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * Get the context for a task and increment its pin_count so it |
| 981 | * can't get swapped to another task. This also increments its |
| 982 | * reference count so that the context can't get freed. |
| 983 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 984 | static struct perf_event_context * |
| 985 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 986 | { |
| 987 | struct perf_event_context *ctx; |
| 988 | unsigned long flags; |
| 989 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 990 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 991 | if (ctx) { |
| 992 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 993 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 994 | } |
| 995 | return ctx; |
| 996 | } |
| 997 | |
| 998 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 999 | { |
| 1000 | unsigned long flags; |
| 1001 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1002 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1003 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1004 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1007 | /* |
| 1008 | * Update the record of the current time in a context. |
| 1009 | */ |
| 1010 | static void update_context_time(struct perf_event_context *ctx) |
| 1011 | { |
| 1012 | u64 now = perf_clock(); |
| 1013 | |
| 1014 | ctx->time += now - ctx->timestamp; |
| 1015 | ctx->timestamp = now; |
| 1016 | } |
| 1017 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1018 | static u64 perf_event_time(struct perf_event *event) |
| 1019 | { |
| 1020 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1021 | |
| 1022 | if (is_cgroup_event(event)) |
| 1023 | return perf_cgroup_event_time(event); |
| 1024 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1025 | return ctx ? ctx->time : 0; |
| 1026 | } |
| 1027 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1028 | /* |
| 1029 | * Update the total_time_enabled and total_time_running fields for a event. |
Eric B Munson | b7526f0 | 2011-06-23 16:34:37 -0400 | [diff] [blame] | 1030 | * The caller of this function needs to hold the ctx->lock. |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1031 | */ |
| 1032 | static void update_event_times(struct perf_event *event) |
| 1033 | { |
| 1034 | struct perf_event_context *ctx = event->ctx; |
| 1035 | u64 run_end; |
| 1036 | |
| 1037 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1038 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1039 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1040 | /* |
| 1041 | * in cgroup mode, time_enabled represents |
| 1042 | * the time the event was enabled AND active |
| 1043 | * tasks were in the monitored cgroup. This is |
| 1044 | * independent of the activity of the context as |
| 1045 | * there may be a mix of cgroup and non-cgroup events. |
| 1046 | * |
| 1047 | * That is why we treat cgroup events differently |
| 1048 | * here. |
| 1049 | */ |
| 1050 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1051 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1052 | else if (ctx->is_active) |
| 1053 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1054 | else |
| 1055 | run_end = event->tstamp_stopped; |
| 1056 | |
| 1057 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1058 | |
| 1059 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1060 | run_end = event->tstamp_stopped; |
| 1061 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1062 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1063 | |
| 1064 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1065 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1068 | /* |
| 1069 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1070 | */ |
| 1071 | static void update_group_times(struct perf_event *leader) |
| 1072 | { |
| 1073 | struct perf_event *event; |
| 1074 | |
| 1075 | update_event_times(leader); |
| 1076 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1077 | update_event_times(event); |
| 1078 | } |
| 1079 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1080 | static struct list_head * |
| 1081 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1082 | { |
| 1083 | if (event->attr.pinned) |
| 1084 | return &ctx->pinned_groups; |
| 1085 | else |
| 1086 | return &ctx->flexible_groups; |
| 1087 | } |
| 1088 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1089 | /* |
| 1090 | * Add a event from the lists for its context. |
| 1091 | * Must be called with ctx->mutex and ctx->lock held. |
| 1092 | */ |
| 1093 | static void |
| 1094 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1095 | { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1096 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1097 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1098 | |
| 1099 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1100 | * If we're a stand alone event or group leader, we go to the context |
| 1101 | * list, group events are kept attached to the group so that |
| 1102 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1103 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1104 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1105 | struct list_head *list; |
| 1106 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1107 | if (is_software_event(event)) |
| 1108 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 1109 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1110 | list = ctx_group_list(event, ctx); |
| 1111 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 1114 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1115 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1116 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1117 | if (has_branch_stack(event)) |
| 1118 | ctx->nr_branch_stack++; |
| 1119 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1120 | list_add_rcu(&event->event_entry, &ctx->event_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1121 | if (!ctx->nr_events) |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1122 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1123 | ctx->nr_events++; |
| 1124 | if (event->attr.inherit_stat) |
| 1125 | ctx->nr_stat++; |
| 1126 | } |
| 1127 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1128 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1129 | * Initialize event state based on the perf_event_attr::disabled. |
| 1130 | */ |
| 1131 | static inline void perf_event__state_init(struct perf_event *event) |
| 1132 | { |
| 1133 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1134 | PERF_EVENT_STATE_INACTIVE; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1138 | * Called at perf_event creation and when events are attached/detached from a |
| 1139 | * group. |
| 1140 | */ |
| 1141 | static void perf_event__read_size(struct perf_event *event) |
| 1142 | { |
| 1143 | int entry = sizeof(u64); /* value */ |
| 1144 | int size = 0; |
| 1145 | int nr = 1; |
| 1146 | |
| 1147 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1148 | size += sizeof(u64); |
| 1149 | |
| 1150 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1151 | size += sizeof(u64); |
| 1152 | |
| 1153 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1154 | entry += sizeof(u64); |
| 1155 | |
| 1156 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
| 1157 | nr += event->group_leader->nr_siblings; |
| 1158 | size += sizeof(u64); |
| 1159 | } |
| 1160 | |
| 1161 | size += entry * nr; |
| 1162 | event->read_size = size; |
| 1163 | } |
| 1164 | |
| 1165 | static void perf_event__header_size(struct perf_event *event) |
| 1166 | { |
| 1167 | struct perf_sample_data *data; |
| 1168 | u64 sample_type = event->attr.sample_type; |
| 1169 | u16 size = 0; |
| 1170 | |
| 1171 | perf_event__read_size(event); |
| 1172 | |
| 1173 | if (sample_type & PERF_SAMPLE_IP) |
| 1174 | size += sizeof(data->ip); |
| 1175 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1176 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1177 | size += sizeof(data->addr); |
| 1178 | |
| 1179 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1180 | size += sizeof(data->period); |
| 1181 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1182 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1183 | size += sizeof(data->weight); |
| 1184 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1185 | if (sample_type & PERF_SAMPLE_READ) |
| 1186 | size += event->read_size; |
| 1187 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1188 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1189 | size += sizeof(data->data_src.val); |
| 1190 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1191 | event->header_size = size; |
| 1192 | } |
| 1193 | |
| 1194 | static void perf_event__id_header_size(struct perf_event *event) |
| 1195 | { |
| 1196 | struct perf_sample_data *data; |
| 1197 | u64 sample_type = event->attr.sample_type; |
| 1198 | u16 size = 0; |
| 1199 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1200 | if (sample_type & PERF_SAMPLE_TID) |
| 1201 | size += sizeof(data->tid_entry); |
| 1202 | |
| 1203 | if (sample_type & PERF_SAMPLE_TIME) |
| 1204 | size += sizeof(data->time); |
| 1205 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1206 | if (sample_type & PERF_SAMPLE_ID) |
| 1207 | size += sizeof(data->id); |
| 1208 | |
| 1209 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1210 | size += sizeof(data->stream_id); |
| 1211 | |
| 1212 | if (sample_type & PERF_SAMPLE_CPU) |
| 1213 | size += sizeof(data->cpu_entry); |
| 1214 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1215 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1216 | } |
| 1217 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1218 | static void perf_group_attach(struct perf_event *event) |
| 1219 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1220 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1221 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1222 | /* |
| 1223 | * We can have double attach due to group movement in perf_event_open. |
| 1224 | */ |
| 1225 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1226 | return; |
| 1227 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1228 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1229 | |
| 1230 | if (group_leader == event) |
| 1231 | return; |
| 1232 | |
| 1233 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 1234 | !is_software_event(event)) |
| 1235 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 1236 | |
| 1237 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1238 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1239 | |
| 1240 | perf_event__header_size(group_leader); |
| 1241 | |
| 1242 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1243 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1246 | /* |
| 1247 | * Remove a event from the lists for its context. |
| 1248 | * Must be called with ctx->mutex and ctx->lock held. |
| 1249 | */ |
| 1250 | static void |
| 1251 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1252 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1253 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1254 | /* |
| 1255 | * We can have double detach due to exit/hot-unplug + close. |
| 1256 | */ |
| 1257 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1258 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1259 | |
| 1260 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1261 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1262 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1263 | ctx->nr_cgroups--; |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1264 | cpuctx = __get_cpu_context(ctx); |
| 1265 | /* |
| 1266 | * if there are no more cgroup events |
| 1267 | * then cler cgrp to avoid stale pointer |
| 1268 | * in update_cgrp_time_from_cpuctx() |
| 1269 | */ |
| 1270 | if (!ctx->nr_cgroups) |
| 1271 | cpuctx->cgrp = NULL; |
| 1272 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1273 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1274 | if (has_branch_stack(event)) |
| 1275 | ctx->nr_branch_stack--; |
| 1276 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1277 | ctx->nr_events--; |
| 1278 | if (event->attr.inherit_stat) |
| 1279 | ctx->nr_stat--; |
| 1280 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1281 | list_del_rcu(&event->event_entry); |
| 1282 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1283 | if (event->group_leader == event) |
| 1284 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1285 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1286 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1287 | |
| 1288 | /* |
| 1289 | * If event was in error state, then keep it |
| 1290 | * that way, otherwise bogus counts will be |
| 1291 | * returned on read(). The only way to get out |
| 1292 | * of error state is by explicit re-enabling |
| 1293 | * of the event |
| 1294 | */ |
| 1295 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1296 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1299 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1300 | { |
| 1301 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1302 | struct list_head *list = NULL; |
| 1303 | |
| 1304 | /* |
| 1305 | * We can have double detach due to exit/hot-unplug + close. |
| 1306 | */ |
| 1307 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1308 | return; |
| 1309 | |
| 1310 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1311 | |
| 1312 | /* |
| 1313 | * If this is a sibling, remove it from its group. |
| 1314 | */ |
| 1315 | if (event->group_leader != event) { |
| 1316 | list_del_init(&event->group_entry); |
| 1317 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1318 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | if (!list_empty(&event->group_entry)) |
| 1322 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1323 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1324 | /* |
| 1325 | * If this was a group event with sibling events then |
| 1326 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1327 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1328 | */ |
| 1329 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1330 | if (list) |
| 1331 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1332 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1333 | |
| 1334 | /* Inherit group flags from the previous leader */ |
| 1335 | sibling->group_flags = event->group_flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1336 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1337 | |
| 1338 | out: |
| 1339 | perf_event__header_size(event->group_leader); |
| 1340 | |
| 1341 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1342 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1345 | static inline int |
| 1346 | event_filter_match(struct perf_event *event) |
| 1347 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1348 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
| 1349 | && perf_cgroup_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1350 | } |
| 1351 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1352 | static void |
| 1353 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1354 | struct perf_cpu_context *cpuctx, |
| 1355 | struct perf_event_context *ctx) |
| 1356 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1357 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1358 | u64 delta; |
| 1359 | /* |
| 1360 | * An event which could not be activated because of |
| 1361 | * filter mismatch still needs to have its timings |
| 1362 | * maintained, otherwise bogus information is return |
| 1363 | * via read() for time_enabled, time_running: |
| 1364 | */ |
| 1365 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1366 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1367 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1368 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1369 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1372 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1373 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1374 | |
| 1375 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1376 | if (event->pending_disable) { |
| 1377 | event->pending_disable = 0; |
| 1378 | event->state = PERF_EVENT_STATE_OFF; |
| 1379 | } |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1380 | event->tstamp_stopped = tstamp; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1381 | event->pmu->del(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1382 | event->oncpu = -1; |
| 1383 | |
| 1384 | if (!is_software_event(event)) |
| 1385 | cpuctx->active_oncpu--; |
| 1386 | ctx->nr_active--; |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1387 | if (event->attr.freq && event->attr.sample_freq) |
| 1388 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1389 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1390 | cpuctx->exclusive = 0; |
| 1391 | } |
| 1392 | |
| 1393 | static void |
| 1394 | group_sched_out(struct perf_event *group_event, |
| 1395 | struct perf_cpu_context *cpuctx, |
| 1396 | struct perf_event_context *ctx) |
| 1397 | { |
| 1398 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1399 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1400 | |
| 1401 | event_sched_out(group_event, cpuctx, ctx); |
| 1402 | |
| 1403 | /* |
| 1404 | * Schedule out siblings (if any): |
| 1405 | */ |
| 1406 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1407 | event_sched_out(event, cpuctx, ctx); |
| 1408 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1409 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1410 | cpuctx->exclusive = 0; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
| 1414 | * Cross CPU call to remove a performance event |
| 1415 | * |
| 1416 | * We disable the event on the hardware level first. After that we |
| 1417 | * remove it from the context list. |
| 1418 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1419 | static int __perf_remove_from_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1420 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1421 | struct perf_event *event = info; |
| 1422 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1423 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1424 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1425 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1426 | event_sched_out(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1427 | list_del_event(event, ctx); |
Peter Zijlstra | 64ce312 | 2011-04-09 21:17:48 +0200 | [diff] [blame] | 1428 | if (!ctx->nr_events && cpuctx->task_ctx == ctx) { |
| 1429 | ctx->is_active = 0; |
| 1430 | cpuctx->task_ctx = NULL; |
| 1431 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1432 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1433 | |
| 1434 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | |
| 1438 | /* |
| 1439 | * Remove the event from a task's (or a CPU's) list of events. |
| 1440 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1441 | * CPU events are removed with a smp call. For task events we only |
| 1442 | * call when the task is on a CPU. |
| 1443 | * |
| 1444 | * If event->ctx is a cloned context, callers must make sure that |
| 1445 | * every task struct that event->ctx->task could possibly point to |
| 1446 | * remains valid. This is OK when called from perf_release since |
| 1447 | * that only calls us on the top-level context, which can't be a clone. |
| 1448 | * When called from perf_event_exit_task, it's OK because the |
| 1449 | * context has been detached from its task. |
| 1450 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1451 | static void perf_remove_from_context(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1452 | { |
| 1453 | struct perf_event_context *ctx = event->ctx; |
| 1454 | struct task_struct *task = ctx->task; |
| 1455 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1456 | lockdep_assert_held(&ctx->mutex); |
| 1457 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1458 | if (!task) { |
| 1459 | /* |
| 1460 | * Per cpu events are removed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1461 | * the removal is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1462 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1463 | cpu_function_call(event->cpu, __perf_remove_from_context, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1464 | return; |
| 1465 | } |
| 1466 | |
| 1467 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1468 | if (!task_function_call(task, __perf_remove_from_context, event)) |
| 1469 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1470 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1471 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1472 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1473 | * If we failed to find a running task, but find the context active now |
| 1474 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1475 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1476 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1477 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1478 | goto retry; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1482 | * Since the task isn't running, its safe to remove the event, us |
| 1483 | * holding the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1484 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1485 | list_del_event(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1486 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1487 | } |
| 1488 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1489 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1490 | * Cross CPU call to disable a performance event |
| 1491 | */ |
K.Prasad | 500ad2d | 2012-08-02 13:46:35 +0530 | [diff] [blame] | 1492 | int __perf_event_disable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1493 | { |
| 1494 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1495 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1496 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1497 | |
| 1498 | /* |
| 1499 | * If this is a per-task event, need to check whether this |
| 1500 | * event's task is the current task on this cpu. |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1501 | * |
| 1502 | * Can trigger due to concurrent perf_event_context_sched_out() |
| 1503 | * flipping contexts around. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1504 | */ |
| 1505 | if (ctx->task && cpuctx->task_ctx != ctx) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1506 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1507 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1508 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1509 | |
| 1510 | /* |
| 1511 | * If the event is on, turn it off. |
| 1512 | * If it is in error state, leave it in error state. |
| 1513 | */ |
| 1514 | if (event->state >= PERF_EVENT_STATE_INACTIVE) { |
| 1515 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1516 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1517 | update_group_times(event); |
| 1518 | if (event == event->group_leader) |
| 1519 | group_sched_out(event, cpuctx, ctx); |
| 1520 | else |
| 1521 | event_sched_out(event, cpuctx, ctx); |
| 1522 | event->state = PERF_EVENT_STATE_OFF; |
| 1523 | } |
| 1524 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1525 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1526 | |
| 1527 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * Disable a event. |
| 1532 | * |
| 1533 | * If event->ctx is a cloned context, callers must make sure that |
| 1534 | * every task struct that event->ctx->task could possibly point to |
| 1535 | * remains valid. This condition is satisifed when called through |
| 1536 | * perf_event_for_each_child or perf_event_for_each because they |
| 1537 | * hold the top-level event's child_mutex, so any descendant that |
| 1538 | * goes to exit will block in sync_child_event. |
| 1539 | * When called from perf_pending_event it's OK because event->ctx |
| 1540 | * is the current context on this CPU and preemption is disabled, |
| 1541 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1542 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 1543 | void perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1544 | { |
| 1545 | struct perf_event_context *ctx = event->ctx; |
| 1546 | struct task_struct *task = ctx->task; |
| 1547 | |
| 1548 | if (!task) { |
| 1549 | /* |
| 1550 | * Disable the event on the cpu that it's on |
| 1551 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1552 | cpu_function_call(event->cpu, __perf_event_disable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1553 | return; |
| 1554 | } |
| 1555 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1556 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1557 | if (!task_function_call(task, __perf_event_disable, event)) |
| 1558 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1559 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1560 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1561 | /* |
| 1562 | * If the event is still active, we need to retry the cross-call. |
| 1563 | */ |
| 1564 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1565 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1566 | /* |
| 1567 | * Reload the task pointer, it might have been changed by |
| 1568 | * a concurrent perf_event_context_sched_out(). |
| 1569 | */ |
| 1570 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1571 | goto retry; |
| 1572 | } |
| 1573 | |
| 1574 | /* |
| 1575 | * Since we have the lock this context can't be scheduled |
| 1576 | * in, so we can change the state safely. |
| 1577 | */ |
| 1578 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 1579 | update_group_times(event); |
| 1580 | event->state = PERF_EVENT_STATE_OFF; |
| 1581 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1582 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1583 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 1584 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1585 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1586 | static void perf_set_shadow_time(struct perf_event *event, |
| 1587 | struct perf_event_context *ctx, |
| 1588 | u64 tstamp) |
| 1589 | { |
| 1590 | /* |
| 1591 | * use the correct time source for the time snapshot |
| 1592 | * |
| 1593 | * We could get by without this by leveraging the |
| 1594 | * fact that to get to this function, the caller |
| 1595 | * has most likely already called update_context_time() |
| 1596 | * and update_cgrp_time_xx() and thus both timestamp |
| 1597 | * are identical (or very close). Given that tstamp is, |
| 1598 | * already adjusted for cgroup, we could say that: |
| 1599 | * tstamp - ctx->timestamp |
| 1600 | * is equivalent to |
| 1601 | * tstamp - cgrp->timestamp. |
| 1602 | * |
| 1603 | * Then, in perf_output_read(), the calculation would |
| 1604 | * work with no changes because: |
| 1605 | * - event is guaranteed scheduled in |
| 1606 | * - no scheduled out in between |
| 1607 | * - thus the timestamp would be the same |
| 1608 | * |
| 1609 | * But this is a bit hairy. |
| 1610 | * |
| 1611 | * So instead, we have an explicit cgroup call to remain |
| 1612 | * within the time time source all along. We believe it |
| 1613 | * is cleaner and simpler to understand. |
| 1614 | */ |
| 1615 | if (is_cgroup_event(event)) |
| 1616 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1617 | else |
| 1618 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1619 | } |
| 1620 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1621 | #define MAX_INTERRUPTS (~0ULL) |
| 1622 | |
| 1623 | static void perf_log_throttle(struct perf_event *event, int enable); |
| 1624 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1625 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1626 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1627 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1628 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1629 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1630 | u64 tstamp = perf_event_time(event); |
| 1631 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1632 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1633 | return 0; |
| 1634 | |
| 1635 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1636 | event->oncpu = smp_processor_id(); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1637 | |
| 1638 | /* |
| 1639 | * Unthrottle events, since we scheduled we might have missed several |
| 1640 | * ticks already, also for a heavily scheduling task there is little |
| 1641 | * guarantee it'll get a tick in a timely manner. |
| 1642 | */ |
| 1643 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1644 | perf_log_throttle(event, 1); |
| 1645 | event->hw.interrupts = 0; |
| 1646 | } |
| 1647 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1648 | /* |
| 1649 | * The new state must be visible before we turn it on in the hardware: |
| 1650 | */ |
| 1651 | smp_wmb(); |
| 1652 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1653 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1654 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1655 | event->oncpu = -1; |
| 1656 | return -EAGAIN; |
| 1657 | } |
| 1658 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1659 | event->tstamp_running += tstamp - event->tstamp_stopped; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1660 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1661 | perf_set_shadow_time(event, ctx, tstamp); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 1662 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1663 | if (!is_software_event(event)) |
| 1664 | cpuctx->active_oncpu++; |
| 1665 | ctx->nr_active++; |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1666 | if (event->attr.freq && event->attr.sample_freq) |
| 1667 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1668 | |
| 1669 | if (event->attr.exclusive) |
| 1670 | cpuctx->exclusive = 1; |
| 1671 | |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
| 1675 | static int |
| 1676 | group_sched_in(struct perf_event *group_event, |
| 1677 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1678 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1679 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1680 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1681 | struct pmu *pmu = group_event->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1682 | u64 now = ctx->time; |
| 1683 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1684 | |
| 1685 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1686 | return 0; |
| 1687 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1688 | pmu->start_txn(pmu); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1689 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1690 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1691 | pmu->cancel_txn(pmu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1692 | perf_cpu_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1693 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1694 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1695 | |
| 1696 | /* |
| 1697 | * Schedule in siblings as one group (if any): |
| 1698 | */ |
| 1699 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1700 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1701 | partial_group = event; |
| 1702 | goto group_error; |
| 1703 | } |
| 1704 | } |
| 1705 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1706 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 1707 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1708 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1709 | group_error: |
| 1710 | /* |
| 1711 | * Groups can be scheduled in as one unit only, so undo any |
| 1712 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1713 | * The events up to the failed event are scheduled out normally, |
| 1714 | * tstamp_stopped will be updated. |
| 1715 | * |
| 1716 | * The failed events and the remaining siblings need to have |
| 1717 | * their timings updated as if they had gone thru event_sched_in() |
| 1718 | * and event_sched_out(). This is required to get consistent timings |
| 1719 | * across the group. This also takes care of the case where the group |
| 1720 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 1721 | * the time the event was actually stopped, such that time delta |
| 1722 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1723 | */ |
| 1724 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 1725 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1726 | simulate = true; |
| 1727 | |
| 1728 | if (simulate) { |
| 1729 | event->tstamp_running += now - event->tstamp_stopped; |
| 1730 | event->tstamp_stopped = now; |
| 1731 | } else { |
| 1732 | event_sched_out(event, cpuctx, ctx); |
| 1733 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1734 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1735 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1736 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1737 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1738 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1739 | perf_cpu_hrtimer_restart(cpuctx); |
| 1740 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1741 | return -EAGAIN; |
| 1742 | } |
| 1743 | |
| 1744 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1745 | * Work out whether we can put this event group on the CPU now. |
| 1746 | */ |
| 1747 | static int group_can_go_on(struct perf_event *event, |
| 1748 | struct perf_cpu_context *cpuctx, |
| 1749 | int can_add_hw) |
| 1750 | { |
| 1751 | /* |
| 1752 | * Groups consisting entirely of software events can always go on. |
| 1753 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1754 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1755 | return 1; |
| 1756 | /* |
| 1757 | * If an exclusive group is already on, no other hardware |
| 1758 | * events can go on. |
| 1759 | */ |
| 1760 | if (cpuctx->exclusive) |
| 1761 | return 0; |
| 1762 | /* |
| 1763 | * If this group is exclusive and there are already |
| 1764 | * events on the CPU, it can't go on. |
| 1765 | */ |
| 1766 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 1767 | return 0; |
| 1768 | /* |
| 1769 | * Otherwise, try to add it if all previous groups were able |
| 1770 | * to go on. |
| 1771 | */ |
| 1772 | return can_add_hw; |
| 1773 | } |
| 1774 | |
| 1775 | static void add_event_to_ctx(struct perf_event *event, |
| 1776 | struct perf_event_context *ctx) |
| 1777 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1778 | u64 tstamp = perf_event_time(event); |
| 1779 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1780 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1781 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1782 | event->tstamp_enabled = tstamp; |
| 1783 | event->tstamp_running = tstamp; |
| 1784 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1785 | } |
| 1786 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1787 | static void task_ctx_sched_out(struct perf_event_context *ctx); |
| 1788 | static void |
| 1789 | ctx_sched_in(struct perf_event_context *ctx, |
| 1790 | struct perf_cpu_context *cpuctx, |
| 1791 | enum event_type_t event_type, |
| 1792 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1793 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 1794 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 1795 | struct perf_event_context *ctx, |
| 1796 | struct task_struct *task) |
| 1797 | { |
| 1798 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 1799 | if (ctx) |
| 1800 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 1801 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 1802 | if (ctx) |
| 1803 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 1804 | } |
| 1805 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1806 | /* |
| 1807 | * Cross CPU call to install and enable a performance event |
| 1808 | * |
| 1809 | * Must be called with ctx->mutex held |
| 1810 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1811 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1812 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1813 | struct perf_event *event = info; |
| 1814 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1815 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1816 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
| 1817 | struct task_struct *task = current; |
| 1818 | |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1819 | perf_ctx_lock(cpuctx, task_ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1820 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1821 | |
| 1822 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1823 | * If there was an active task_ctx schedule it out. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1824 | */ |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1825 | if (task_ctx) |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1826 | task_ctx_sched_out(task_ctx); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1827 | |
| 1828 | /* |
| 1829 | * If the context we're installing events in is not the |
| 1830 | * active task_ctx, flip them. |
| 1831 | */ |
| 1832 | if (ctx->task && task_ctx != ctx) { |
| 1833 | if (task_ctx) |
| 1834 | raw_spin_unlock(&task_ctx->lock); |
| 1835 | raw_spin_lock(&ctx->lock); |
| 1836 | task_ctx = ctx; |
| 1837 | } |
| 1838 | |
| 1839 | if (task_ctx) { |
| 1840 | cpuctx->task_ctx = task_ctx; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1841 | task = task_ctx->task; |
| 1842 | } |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1843 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1844 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1845 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1846 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1847 | /* |
| 1848 | * update cgrp time only if current cgrp |
| 1849 | * matches event->cgrp. Must be done before |
| 1850 | * calling add_event_to_ctx() |
| 1851 | */ |
| 1852 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1853 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1854 | add_event_to_ctx(event, ctx); |
| 1855 | |
| 1856 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1857 | * Schedule everything back in |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1858 | */ |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 1859 | perf_event_sched_in(cpuctx, task_ctx, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1860 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1861 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 1862 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1863 | |
| 1864 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * Attach a performance event to a context |
| 1869 | * |
| 1870 | * First we add the event to the list with the hardware enable bit |
| 1871 | * in event->hw_config cleared. |
| 1872 | * |
| 1873 | * If the event is attached to a task which is on a CPU we use a smp |
| 1874 | * call to enable it in the task context. The task might have been |
| 1875 | * scheduled away, but we check this in the smp call again. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1876 | */ |
| 1877 | static void |
| 1878 | perf_install_in_context(struct perf_event_context *ctx, |
| 1879 | struct perf_event *event, |
| 1880 | int cpu) |
| 1881 | { |
| 1882 | struct task_struct *task = ctx->task; |
| 1883 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1884 | lockdep_assert_held(&ctx->mutex); |
| 1885 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 1886 | event->ctx = ctx; |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 1887 | if (event->cpu != -1) |
| 1888 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 1889 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1890 | if (!task) { |
| 1891 | /* |
| 1892 | * Per cpu events are installed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1893 | * the install is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1894 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1895 | cpu_function_call(cpu, __perf_install_in_context, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1896 | return; |
| 1897 | } |
| 1898 | |
| 1899 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1900 | if (!task_function_call(task, __perf_install_in_context, event)) |
| 1901 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1902 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1903 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1904 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1905 | * If we failed to find a running task, but find the context active now |
| 1906 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1907 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1908 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1909 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1910 | goto retry; |
| 1911 | } |
| 1912 | |
| 1913 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1914 | * Since the task isn't running, its safe to add the event, us holding |
| 1915 | * the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1916 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1917 | add_event_to_ctx(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1918 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | /* |
| 1922 | * Put a event into inactive state and update time fields. |
| 1923 | * Enabling the leader of a group effectively enables all |
| 1924 | * the group members that aren't explicitly disabled, so we |
| 1925 | * have to update their ->tstamp_enabled also. |
| 1926 | * Note: this works for group members as well as group leaders |
| 1927 | * since the non-leader members' sibling_lists will be empty. |
| 1928 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 1929 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1930 | { |
| 1931 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1932 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1933 | |
| 1934 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1935 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1936 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1937 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 1938 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1939 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | /* |
| 1943 | * Cross CPU call to enable a performance event |
| 1944 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1945 | static int __perf_event_enable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1946 | { |
| 1947 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1948 | struct perf_event_context *ctx = event->ctx; |
| 1949 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1950 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1951 | int err; |
| 1952 | |
Jiri Olsa | 06f4179 | 2013-07-09 17:44:11 +0200 | [diff] [blame^] | 1953 | /* |
| 1954 | * There's a time window between 'ctx->is_active' check |
| 1955 | * in perf_event_enable function and this place having: |
| 1956 | * - IRQs on |
| 1957 | * - ctx->lock unlocked |
| 1958 | * |
| 1959 | * where the task could be killed and 'ctx' deactivated |
| 1960 | * by perf_event_exit_task. |
| 1961 | */ |
| 1962 | if (!ctx->is_active) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1963 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1964 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1965 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1966 | update_context_time(ctx); |
| 1967 | |
| 1968 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 1969 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1970 | |
| 1971 | /* |
| 1972 | * set current task's cgroup time reference point |
| 1973 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 1974 | perf_cgroup_set_timestamp(current, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1975 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 1976 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1977 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1978 | if (!event_filter_match(event)) { |
| 1979 | if (is_cgroup_event(event)) |
| 1980 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 1981 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1982 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 1983 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1984 | /* |
| 1985 | * If the event is in a group and isn't the group leader, |
| 1986 | * then don't put it on unless the group is on. |
| 1987 | */ |
| 1988 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) |
| 1989 | goto unlock; |
| 1990 | |
| 1991 | if (!group_can_go_on(event, cpuctx, 1)) { |
| 1992 | err = -EEXIST; |
| 1993 | } else { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1994 | if (event == leader) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1995 | err = group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1996 | else |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1997 | err = event_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | if (err) { |
| 2001 | /* |
| 2002 | * If this event can't go on and it's part of a |
| 2003 | * group, then the whole group has to come off. |
| 2004 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2005 | if (leader != event) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2006 | group_sched_out(leader, cpuctx, ctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2007 | perf_cpu_hrtimer_restart(cpuctx); |
| 2008 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2009 | if (leader->attr.pinned) { |
| 2010 | update_group_times(leader); |
| 2011 | leader->state = PERF_EVENT_STATE_ERROR; |
| 2012 | } |
| 2013 | } |
| 2014 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2015 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2016 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2017 | |
| 2018 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | /* |
| 2022 | * Enable a event. |
| 2023 | * |
| 2024 | * If event->ctx is a cloned context, callers must make sure that |
| 2025 | * every task struct that event->ctx->task could possibly point to |
| 2026 | * remains valid. This condition is satisfied when called through |
| 2027 | * perf_event_for_each_child or perf_event_for_each as described |
| 2028 | * for perf_event_disable. |
| 2029 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 2030 | void perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2031 | { |
| 2032 | struct perf_event_context *ctx = event->ctx; |
| 2033 | struct task_struct *task = ctx->task; |
| 2034 | |
| 2035 | if (!task) { |
| 2036 | /* |
| 2037 | * Enable the event on the cpu that it's on |
| 2038 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2039 | cpu_function_call(event->cpu, __perf_event_enable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2040 | return; |
| 2041 | } |
| 2042 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2043 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2044 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2045 | goto out; |
| 2046 | |
| 2047 | /* |
| 2048 | * If the event is in error state, clear that first. |
| 2049 | * That way, if we see the event in error state below, we |
| 2050 | * know that it has gone back into error state, as distinct |
| 2051 | * from the task having been scheduled away before the |
| 2052 | * cross-call arrived. |
| 2053 | */ |
| 2054 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2055 | event->state = PERF_EVENT_STATE_OFF; |
| 2056 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2057 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2058 | if (!ctx->is_active) { |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2059 | __perf_event_mark_enabled(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2060 | goto out; |
| 2061 | } |
| 2062 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2063 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2064 | |
| 2065 | if (!task_function_call(task, __perf_event_enable, event)) |
| 2066 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2067 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2068 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2069 | |
| 2070 | /* |
| 2071 | * If the context is active and the event is still off, |
| 2072 | * we need to retry the cross-call. |
| 2073 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2074 | if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) { |
| 2075 | /* |
| 2076 | * task could have been flipped by a concurrent |
| 2077 | * perf_event_context_sched_out() |
| 2078 | */ |
| 2079 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2080 | goto retry; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2081 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2082 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2083 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2084 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2085 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2086 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2087 | |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2088 | int perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2089 | { |
| 2090 | /* |
| 2091 | * not supported on inherited events |
| 2092 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2093 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2094 | return -EINVAL; |
| 2095 | |
| 2096 | atomic_add(refresh, &event->event_limit); |
| 2097 | perf_event_enable(event); |
| 2098 | |
| 2099 | return 0; |
| 2100 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2101 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2102 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2103 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2104 | struct perf_cpu_context *cpuctx, |
| 2105 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2106 | { |
| 2107 | struct perf_event *event; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2108 | int is_active = ctx->is_active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2109 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2110 | ctx->is_active &= ~event_type; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2111 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2112 | return; |
| 2113 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2114 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2115 | update_cgrp_time_from_cpuctx(cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2116 | if (!ctx->nr_active) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2117 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2118 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2119 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2120 | if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2121 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2122 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2123 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2124 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2125 | if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2126 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2127 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2128 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2129 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | /* |
| 2133 | * Test whether two contexts are equivalent, i.e. whether they |
| 2134 | * have both been cloned from the same version of the same context |
| 2135 | * and they both have the same number of enabled events. |
| 2136 | * If the number of enabled events is the same, then the set |
| 2137 | * of enabled events should be the same, because these are both |
| 2138 | * inherited contexts, therefore we can't access individual events |
| 2139 | * in them directly with an fd; we can only enable/disable all |
| 2140 | * events via prctl, or enable/disable all events in a family |
| 2141 | * via ioctl, which will have the same effect on both contexts. |
| 2142 | */ |
| 2143 | static int context_equiv(struct perf_event_context *ctx1, |
| 2144 | struct perf_event_context *ctx2) |
| 2145 | { |
| 2146 | return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx |
| 2147 | && ctx1->parent_gen == ctx2->parent_gen |
| 2148 | && !ctx1->pin_count && !ctx2->pin_count; |
| 2149 | } |
| 2150 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2151 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2152 | struct perf_event *next_event) |
| 2153 | { |
| 2154 | u64 value; |
| 2155 | |
| 2156 | if (!event->attr.inherit_stat) |
| 2157 | return; |
| 2158 | |
| 2159 | /* |
| 2160 | * Update the event value, we cannot use perf_event_read() |
| 2161 | * because we're in the middle of a context switch and have IRQs |
| 2162 | * disabled, which upsets smp_call_function_single(), however |
| 2163 | * we know the event must be on the current CPU, therefore we |
| 2164 | * don't need to use it. |
| 2165 | */ |
| 2166 | switch (event->state) { |
| 2167 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2168 | event->pmu->read(event); |
| 2169 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2170 | |
| 2171 | case PERF_EVENT_STATE_INACTIVE: |
| 2172 | update_event_times(event); |
| 2173 | break; |
| 2174 | |
| 2175 | default: |
| 2176 | break; |
| 2177 | } |
| 2178 | |
| 2179 | /* |
| 2180 | * In order to keep per-task stats reliable we need to flip the event |
| 2181 | * values when we flip the contexts. |
| 2182 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2183 | value = local64_read(&next_event->count); |
| 2184 | value = local64_xchg(&event->count, value); |
| 2185 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2186 | |
| 2187 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2188 | swap(event->total_time_running, next_event->total_time_running); |
| 2189 | |
| 2190 | /* |
| 2191 | * Since we swizzled the values, update the user visible data too. |
| 2192 | */ |
| 2193 | perf_event_update_userpage(event); |
| 2194 | perf_event_update_userpage(next_event); |
| 2195 | } |
| 2196 | |
| 2197 | #define list_next_entry(pos, member) \ |
| 2198 | list_entry(pos->member.next, typeof(*pos), member) |
| 2199 | |
| 2200 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2201 | struct perf_event_context *next_ctx) |
| 2202 | { |
| 2203 | struct perf_event *event, *next_event; |
| 2204 | |
| 2205 | if (!ctx->nr_stat) |
| 2206 | return; |
| 2207 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2208 | update_context_time(ctx); |
| 2209 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2210 | event = list_first_entry(&ctx->event_list, |
| 2211 | struct perf_event, event_entry); |
| 2212 | |
| 2213 | next_event = list_first_entry(&next_ctx->event_list, |
| 2214 | struct perf_event, event_entry); |
| 2215 | |
| 2216 | while (&event->event_entry != &ctx->event_list && |
| 2217 | &next_event->event_entry != &next_ctx->event_list) { |
| 2218 | |
| 2219 | __perf_event_sync_stat(event, next_event); |
| 2220 | |
| 2221 | event = list_next_entry(event, event_entry); |
| 2222 | next_event = list_next_entry(next_event, event_entry); |
| 2223 | } |
| 2224 | } |
| 2225 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2226 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2227 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2228 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2229 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2230 | struct perf_event_context *next_ctx; |
| 2231 | struct perf_event_context *parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2232 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2233 | int do_switch = 1; |
| 2234 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2235 | if (likely(!ctx)) |
| 2236 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2237 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2238 | cpuctx = __get_cpu_context(ctx); |
| 2239 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2240 | return; |
| 2241 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2242 | rcu_read_lock(); |
| 2243 | parent = rcu_dereference(ctx->parent_ctx); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2244 | next_ctx = next->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2245 | if (parent && next_ctx && |
| 2246 | rcu_dereference(next_ctx->parent_ctx) == parent) { |
| 2247 | /* |
| 2248 | * Looks like the two contexts are clones, so we might be |
| 2249 | * able to optimize the context switch. We lock both |
| 2250 | * contexts and check that they are clones under the |
| 2251 | * lock (including re-checking that neither has been |
| 2252 | * uncloned in the meantime). It doesn't matter which |
| 2253 | * order we take the locks because no other cpu could |
| 2254 | * be trying to lock both of these tasks. |
| 2255 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2256 | raw_spin_lock(&ctx->lock); |
| 2257 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2258 | if (context_equiv(ctx, next_ctx)) { |
| 2259 | /* |
| 2260 | * XXX do we need a memory barrier of sorts |
| 2261 | * wrt to rcu_dereference() of perf_event_ctxp |
| 2262 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2263 | task->perf_event_ctxp[ctxn] = next_ctx; |
| 2264 | next->perf_event_ctxp[ctxn] = ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2265 | ctx->task = next; |
| 2266 | next_ctx->task = task; |
| 2267 | do_switch = 0; |
| 2268 | |
| 2269 | perf_event_sync_stat(ctx, next_ctx); |
| 2270 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2271 | raw_spin_unlock(&next_ctx->lock); |
| 2272 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2273 | } |
| 2274 | rcu_read_unlock(); |
| 2275 | |
| 2276 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2277 | raw_spin_lock(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2278 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2279 | cpuctx->task_ctx = NULL; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2280 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2281 | } |
| 2282 | } |
| 2283 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2284 | #define for_each_task_context_nr(ctxn) \ |
| 2285 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2286 | |
| 2287 | /* |
| 2288 | * Called from scheduler to remove the events of the current task, |
| 2289 | * with interrupts disabled. |
| 2290 | * |
| 2291 | * We stop each event and update the event value in event->count. |
| 2292 | * |
| 2293 | * This does not protect us against NMI, but disable() |
| 2294 | * sets the disabled bit in the control field of event _before_ |
| 2295 | * accessing the event control register. If a NMI hits, then it will |
| 2296 | * not restart the event. |
| 2297 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2298 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2299 | struct task_struct *next) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2300 | { |
| 2301 | int ctxn; |
| 2302 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2303 | for_each_task_context_nr(ctxn) |
| 2304 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2305 | |
| 2306 | /* |
| 2307 | * if cgroup events exist on this CPU, then we need |
| 2308 | * to check if we have to switch out PMU state. |
| 2309 | * cgroup event are system-wide mode only |
| 2310 | */ |
| 2311 | if (atomic_read(&__get_cpu_var(perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2312 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2313 | } |
| 2314 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2315 | static void task_ctx_sched_out(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2316 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2317 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2318 | |
| 2319 | if (!cpuctx->task_ctx) |
| 2320 | return; |
| 2321 | |
| 2322 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2323 | return; |
| 2324 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2325 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2326 | cpuctx->task_ctx = NULL; |
| 2327 | } |
| 2328 | |
| 2329 | /* |
| 2330 | * Called with IRQs disabled |
| 2331 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2332 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2333 | enum event_type_t event_type) |
| 2334 | { |
| 2335 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2339 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2340 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2341 | { |
| 2342 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2343 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2344 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2345 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2346 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2347 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2348 | continue; |
| 2349 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2350 | /* may need to reset tstamp_enabled */ |
| 2351 | if (is_cgroup_event(event)) |
| 2352 | perf_cgroup_mark_enabled(event, ctx); |
| 2353 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2354 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2355 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2356 | |
| 2357 | /* |
| 2358 | * If this pinned group hasn't been scheduled, |
| 2359 | * put it in error state. |
| 2360 | */ |
| 2361 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2362 | update_group_times(event); |
| 2363 | event->state = PERF_EVENT_STATE_ERROR; |
| 2364 | } |
| 2365 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | static void |
| 2369 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2370 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2371 | { |
| 2372 | struct perf_event *event; |
| 2373 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2374 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2375 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2376 | /* Ignore events in OFF or ERROR state */ |
| 2377 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2378 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2379 | /* |
| 2380 | * Listen to the 'cpu' scheduling filter constraint |
| 2381 | * of events: |
| 2382 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2383 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2384 | continue; |
| 2385 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2386 | /* may need to reset tstamp_enabled */ |
| 2387 | if (is_cgroup_event(event)) |
| 2388 | perf_cgroup_mark_enabled(event, ctx); |
| 2389 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2390 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2391 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2392 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2393 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2394 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | static void |
| 2398 | ctx_sched_in(struct perf_event_context *ctx, |
| 2399 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2400 | enum event_type_t event_type, |
| 2401 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2402 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2403 | u64 now; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2404 | int is_active = ctx->is_active; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2405 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2406 | ctx->is_active |= event_type; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2407 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2408 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2409 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2410 | now = perf_clock(); |
| 2411 | ctx->timestamp = now; |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2412 | perf_cgroup_set_timestamp(task, ctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2413 | /* |
| 2414 | * First go through the list and put on any pinned groups |
| 2415 | * in order to give them the best chance of going on. |
| 2416 | */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2417 | if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2418 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2419 | |
| 2420 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2421 | if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2422 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2425 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2426 | enum event_type_t event_type, |
| 2427 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2428 | { |
| 2429 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2430 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2431 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2432 | } |
| 2433 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2434 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2435 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2436 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2437 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2438 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2439 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2440 | if (cpuctx->task_ctx == ctx) |
| 2441 | return; |
| 2442 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2443 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2444 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2445 | /* |
| 2446 | * We want to keep the following priority order: |
| 2447 | * cpu pinned (that don't need to move), task pinned, |
| 2448 | * cpu flexible, task flexible. |
| 2449 | */ |
| 2450 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2451 | |
Gleb Natapov | 1d5f003 | 2011-10-23 19:10:33 +0200 | [diff] [blame] | 2452 | if (ctx->nr_events) |
| 2453 | cpuctx->task_ctx = ctx; |
eranian@google.com | 9b33fa6 | 2010-03-10 22:26:05 -0800 | [diff] [blame] | 2454 | |
Gleb Natapov | 86b47c2 | 2011-11-22 16:08:21 +0200 | [diff] [blame] | 2455 | perf_event_sched_in(cpuctx, cpuctx->task_ctx, task); |
| 2456 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2457 | perf_pmu_enable(ctx->pmu); |
| 2458 | perf_ctx_unlock(cpuctx, ctx); |
| 2459 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2460 | /* |
| 2461 | * Since these rotations are per-cpu, we need to ensure the |
| 2462 | * cpu-context we got scheduled on is actually rotating. |
| 2463 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2464 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2465 | } |
| 2466 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2467 | /* |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2468 | * When sampling the branck stack in system-wide, it may be necessary |
| 2469 | * to flush the stack on context switch. This happens when the branch |
| 2470 | * stack does not tag its entries with the pid of the current task. |
| 2471 | * Otherwise it becomes impossible to associate a branch entry with a |
| 2472 | * task. This ambiguity is more likely to appear when the branch stack |
| 2473 | * supports priv level filtering and the user sets it to monitor only |
| 2474 | * at the user level (which could be a useful measurement in system-wide |
| 2475 | * mode). In that case, the risk is high of having a branch stack with |
| 2476 | * branch from multiple tasks. Flushing may mean dropping the existing |
| 2477 | * entries or stashing them somewhere in the PMU specific code layer. |
| 2478 | * |
| 2479 | * This function provides the context switch callback to the lower code |
| 2480 | * layer. It is invoked ONLY when there is at least one system-wide context |
| 2481 | * with at least one active event using taken branch sampling. |
| 2482 | */ |
| 2483 | static void perf_branch_stack_sched_in(struct task_struct *prev, |
| 2484 | struct task_struct *task) |
| 2485 | { |
| 2486 | struct perf_cpu_context *cpuctx; |
| 2487 | struct pmu *pmu; |
| 2488 | unsigned long flags; |
| 2489 | |
| 2490 | /* no need to flush branch stack if not changing task */ |
| 2491 | if (prev == task) |
| 2492 | return; |
| 2493 | |
| 2494 | local_irq_save(flags); |
| 2495 | |
| 2496 | rcu_read_lock(); |
| 2497 | |
| 2498 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 2499 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2500 | |
| 2501 | /* |
| 2502 | * check if the context has at least one |
| 2503 | * event using PERF_SAMPLE_BRANCH_STACK |
| 2504 | */ |
| 2505 | if (cpuctx->ctx.nr_branch_stack > 0 |
| 2506 | && pmu->flush_branch_stack) { |
| 2507 | |
| 2508 | pmu = cpuctx->ctx.pmu; |
| 2509 | |
| 2510 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2511 | |
| 2512 | perf_pmu_disable(pmu); |
| 2513 | |
| 2514 | pmu->flush_branch_stack(); |
| 2515 | |
| 2516 | perf_pmu_enable(pmu); |
| 2517 | |
| 2518 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | rcu_read_unlock(); |
| 2523 | |
| 2524 | local_irq_restore(flags); |
| 2525 | } |
| 2526 | |
| 2527 | /* |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2528 | * Called from scheduler to add the events of the current task |
| 2529 | * with interrupts disabled. |
| 2530 | * |
| 2531 | * We restore the event value and then enable it. |
| 2532 | * |
| 2533 | * This does not protect us against NMI, but enable() |
| 2534 | * sets the enabled bit in the control field of event _before_ |
| 2535 | * accessing the event control register. If a NMI hits, then it will |
| 2536 | * keep the event running. |
| 2537 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2538 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 2539 | struct task_struct *task) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2540 | { |
| 2541 | struct perf_event_context *ctx; |
| 2542 | int ctxn; |
| 2543 | |
| 2544 | for_each_task_context_nr(ctxn) { |
| 2545 | ctx = task->perf_event_ctxp[ctxn]; |
| 2546 | if (likely(!ctx)) |
| 2547 | continue; |
| 2548 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2549 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2550 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2551 | /* |
| 2552 | * if cgroup events exist on this CPU, then we need |
| 2553 | * to check if we have to switch in PMU state. |
| 2554 | * cgroup event are system-wide mode only |
| 2555 | */ |
| 2556 | if (atomic_read(&__get_cpu_var(perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2557 | perf_cgroup_sched_in(prev, task); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2558 | |
| 2559 | /* check for system-wide branch_stack events */ |
| 2560 | if (atomic_read(&__get_cpu_var(perf_branch_stack_events))) |
| 2561 | perf_branch_stack_sched_in(prev, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2562 | } |
| 2563 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2564 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 2565 | { |
| 2566 | u64 frequency = event->attr.sample_freq; |
| 2567 | u64 sec = NSEC_PER_SEC; |
| 2568 | u64 divisor, dividend; |
| 2569 | |
| 2570 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 2571 | |
| 2572 | count_fls = fls64(count); |
| 2573 | nsec_fls = fls64(nsec); |
| 2574 | frequency_fls = fls64(frequency); |
| 2575 | sec_fls = 30; |
| 2576 | |
| 2577 | /* |
| 2578 | * We got @count in @nsec, with a target of sample_freq HZ |
| 2579 | * the target period becomes: |
| 2580 | * |
| 2581 | * @count * 10^9 |
| 2582 | * period = ------------------- |
| 2583 | * @nsec * sample_freq |
| 2584 | * |
| 2585 | */ |
| 2586 | |
| 2587 | /* |
| 2588 | * Reduce accuracy by one bit such that @a and @b converge |
| 2589 | * to a similar magnitude. |
| 2590 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2591 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2592 | do { \ |
| 2593 | if (a##_fls > b##_fls) { \ |
| 2594 | a >>= 1; \ |
| 2595 | a##_fls--; \ |
| 2596 | } else { \ |
| 2597 | b >>= 1; \ |
| 2598 | b##_fls--; \ |
| 2599 | } \ |
| 2600 | } while (0) |
| 2601 | |
| 2602 | /* |
| 2603 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 2604 | * the other, so that finally we can do a u64/u64 division. |
| 2605 | */ |
| 2606 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 2607 | REDUCE_FLS(nsec, frequency); |
| 2608 | REDUCE_FLS(sec, count); |
| 2609 | } |
| 2610 | |
| 2611 | if (count_fls + sec_fls > 64) { |
| 2612 | divisor = nsec * frequency; |
| 2613 | |
| 2614 | while (count_fls + sec_fls > 64) { |
| 2615 | REDUCE_FLS(count, sec); |
| 2616 | divisor >>= 1; |
| 2617 | } |
| 2618 | |
| 2619 | dividend = count * sec; |
| 2620 | } else { |
| 2621 | dividend = count * sec; |
| 2622 | |
| 2623 | while (nsec_fls + frequency_fls > 64) { |
| 2624 | REDUCE_FLS(nsec, frequency); |
| 2625 | dividend >>= 1; |
| 2626 | } |
| 2627 | |
| 2628 | divisor = nsec * frequency; |
| 2629 | } |
| 2630 | |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2631 | if (!divisor) |
| 2632 | return dividend; |
| 2633 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2634 | return div64_u64(dividend, divisor); |
| 2635 | } |
| 2636 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2637 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 2638 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 2639 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2640 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2641 | { |
| 2642 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2643 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2644 | s64 delta; |
| 2645 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2646 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2647 | |
| 2648 | delta = (s64)(period - hwc->sample_period); |
| 2649 | delta = (delta + 7) / 8; /* low pass filter */ |
| 2650 | |
| 2651 | sample_period = hwc->sample_period + delta; |
| 2652 | |
| 2653 | if (!sample_period) |
| 2654 | sample_period = 1; |
| 2655 | |
| 2656 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2657 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2658 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2659 | if (disable) |
| 2660 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2661 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2662 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2663 | |
| 2664 | if (disable) |
| 2665 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2666 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2667 | } |
| 2668 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2669 | /* |
| 2670 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 2671 | * events. At the same time, make sure, having freq events does not change |
| 2672 | * the rate of unthrottling as that would introduce bias. |
| 2673 | */ |
| 2674 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 2675 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2676 | { |
| 2677 | struct perf_event *event; |
| 2678 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2679 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2680 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2681 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2682 | /* |
| 2683 | * only need to iterate over all events iff: |
| 2684 | * - context have events in frequency mode (needs freq adjust) |
| 2685 | * - there are events to unthrottle on this cpu |
| 2686 | */ |
| 2687 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2688 | return; |
| 2689 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2690 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2691 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2692 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 2693 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2694 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 2695 | continue; |
| 2696 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2697 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 2698 | continue; |
| 2699 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2700 | hwc = &event->hw; |
| 2701 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2702 | if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) { |
| 2703 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2704 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2705 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | if (!event->attr.freq || !event->attr.sample_freq) |
| 2709 | continue; |
| 2710 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2711 | /* |
| 2712 | * stop the event and update event->count |
| 2713 | */ |
| 2714 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2715 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2716 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2717 | delta = now - hwc->freq_count_stamp; |
| 2718 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2719 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2720 | /* |
| 2721 | * restart the event |
| 2722 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2723 | * we have stopped the event so tell that |
| 2724 | * to perf_adjust_period() to avoid stopping it |
| 2725 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2726 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2727 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2728 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2729 | |
| 2730 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2731 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2732 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2733 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2734 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | /* |
| 2738 | * Round-robin a context's events: |
| 2739 | */ |
| 2740 | static void rotate_ctx(struct perf_event_context *ctx) |
| 2741 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 2742 | /* |
| 2743 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 2744 | * disabled by the inheritance code. |
| 2745 | */ |
| 2746 | if (!ctx->rotate_disable) |
| 2747 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2748 | } |
| 2749 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2750 | /* |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2751 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 2752 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 2753 | * disabled, while rotate_context is called from IRQ context. |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2754 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2755 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2756 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2757 | struct perf_event_context *ctx = NULL; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2758 | int rotate = 0, remove = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2759 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2760 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2761 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2762 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 2763 | rotate = 1; |
| 2764 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2765 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2766 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2767 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2768 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2769 | if (ctx->nr_events != ctx->nr_active) |
| 2770 | rotate = 1; |
| 2771 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2772 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2773 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2774 | goto done; |
| 2775 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2776 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2777 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2778 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2779 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2780 | if (ctx) |
| 2781 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 2782 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2783 | rotate_ctx(&cpuctx->ctx); |
| 2784 | if (ctx) |
| 2785 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2786 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2787 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2788 | |
| 2789 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 2790 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2791 | done: |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2792 | if (remove) |
| 2793 | list_del_init(&cpuctx->rotation_list); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2794 | |
| 2795 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2796 | } |
| 2797 | |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2798 | #ifdef CONFIG_NO_HZ_FULL |
| 2799 | bool perf_event_can_stop_tick(void) |
| 2800 | { |
| 2801 | if (list_empty(&__get_cpu_var(rotation_list))) |
| 2802 | return true; |
| 2803 | else |
| 2804 | return false; |
| 2805 | } |
| 2806 | #endif |
| 2807 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2808 | void perf_event_task_tick(void) |
| 2809 | { |
| 2810 | struct list_head *head = &__get_cpu_var(rotation_list); |
| 2811 | struct perf_cpu_context *cpuctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2812 | struct perf_event_context *ctx; |
| 2813 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2814 | |
| 2815 | WARN_ON(!irqs_disabled()); |
| 2816 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2817 | __this_cpu_inc(perf_throttled_seq); |
| 2818 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
| 2819 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2820 | list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2821 | ctx = &cpuctx->ctx; |
| 2822 | perf_adjust_freq_unthr_context(ctx, throttled); |
| 2823 | |
| 2824 | ctx = cpuctx->task_ctx; |
| 2825 | if (ctx) |
| 2826 | perf_adjust_freq_unthr_context(ctx, throttled); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2827 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2828 | } |
| 2829 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2830 | static int event_enable_on_exec(struct perf_event *event, |
| 2831 | struct perf_event_context *ctx) |
| 2832 | { |
| 2833 | if (!event->attr.enable_on_exec) |
| 2834 | return 0; |
| 2835 | |
| 2836 | event->attr.enable_on_exec = 0; |
| 2837 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2838 | return 0; |
| 2839 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2840 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2841 | |
| 2842 | return 1; |
| 2843 | } |
| 2844 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2845 | /* |
| 2846 | * Enable all of a task's events that have been marked enable-on-exec. |
| 2847 | * This expects task == current. |
| 2848 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2849 | static void perf_event_enable_on_exec(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2850 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2851 | struct perf_event *event; |
| 2852 | unsigned long flags; |
| 2853 | int enabled = 0; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2854 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2855 | |
| 2856 | local_irq_save(flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2857 | if (!ctx || !ctx->nr_events) |
| 2858 | goto out; |
| 2859 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 2860 | /* |
| 2861 | * We must ctxsw out cgroup events to avoid conflict |
| 2862 | * when invoking perf_task_event_sched_in() later on |
| 2863 | * in this function. Otherwise we end up trying to |
| 2864 | * ctxswin cgroup events which are already scheduled |
| 2865 | * in. |
| 2866 | */ |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2867 | perf_cgroup_sched_out(current, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2868 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2869 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2870 | task_ctx_sched_out(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2871 | |
Peter Zijlstra | b79387e | 2011-11-22 11:25:43 +0100 | [diff] [blame] | 2872 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2873 | ret = event_enable_on_exec(event, ctx); |
| 2874 | if (ret) |
| 2875 | enabled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | /* |
| 2879 | * Unclone this context if we enabled any event. |
| 2880 | */ |
| 2881 | if (enabled) |
| 2882 | unclone_ctx(ctx); |
| 2883 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2884 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2885 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 2886 | /* |
| 2887 | * Also calls ctxswin for cgroup events, if any: |
| 2888 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2889 | perf_event_context_sched_in(ctx, ctx->task); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2890 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2891 | local_irq_restore(flags); |
| 2892 | } |
| 2893 | |
| 2894 | /* |
| 2895 | * Cross CPU call to read the hardware event |
| 2896 | */ |
| 2897 | static void __perf_event_read(void *info) |
| 2898 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2899 | struct perf_event *event = info; |
| 2900 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2901 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2902 | |
| 2903 | /* |
| 2904 | * If this is a task context, we need to check whether it is |
| 2905 | * the current task context of this cpu. If not it has been |
| 2906 | * scheduled out before the smp call arrived. In that case |
| 2907 | * event->count would have been updated to a recent sample |
| 2908 | * when the event was scheduled out. |
| 2909 | */ |
| 2910 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 2911 | return; |
| 2912 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2913 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2914 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 2915 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2916 | update_cgrp_time_from_event(event); |
| 2917 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2918 | update_event_times(event); |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 2919 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
| 2920 | event->pmu->read(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2921 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 2924 | static inline u64 perf_event_count(struct perf_event *event) |
| 2925 | { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2926 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 2927 | } |
| 2928 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2929 | static u64 perf_event_read(struct perf_event *event) |
| 2930 | { |
| 2931 | /* |
| 2932 | * If event is enabled and currently active on a CPU, update the |
| 2933 | * value in the event structure: |
| 2934 | */ |
| 2935 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
| 2936 | smp_call_function_single(event->oncpu, |
| 2937 | __perf_event_read, event, 1); |
| 2938 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 2939 | struct perf_event_context *ctx = event->ctx; |
| 2940 | unsigned long flags; |
| 2941 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2942 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 2943 | /* |
| 2944 | * may read while context is not active |
| 2945 | * (e.g., thread is blocked), in that case |
| 2946 | * we cannot update context time |
| 2947 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2948 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 2949 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2950 | update_cgrp_time_from_event(event); |
| 2951 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2952 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2953 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2954 | } |
| 2955 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 2956 | return perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | /* |
| 2960 | * Initialize the perf_event context in a task_struct: |
| 2961 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 2962 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2963 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2964 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2965 | mutex_init(&ctx->mutex); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2966 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 2967 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2968 | INIT_LIST_HEAD(&ctx->event_list); |
| 2969 | atomic_set(&ctx->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2970 | } |
| 2971 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 2972 | static struct perf_event_context * |
| 2973 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2974 | { |
| 2975 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 2976 | |
| 2977 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 2978 | if (!ctx) |
| 2979 | return NULL; |
| 2980 | |
| 2981 | __perf_event_init_context(ctx); |
| 2982 | if (task) { |
| 2983 | ctx->task = task; |
| 2984 | get_task_struct(task); |
| 2985 | } |
| 2986 | ctx->pmu = pmu; |
| 2987 | |
| 2988 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2989 | } |
| 2990 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 2991 | static struct task_struct * |
| 2992 | find_lively_task_by_vpid(pid_t vpid) |
| 2993 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2994 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2995 | int err; |
| 2996 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2997 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 2998 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2999 | task = current; |
| 3000 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3001 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3002 | if (task) |
| 3003 | get_task_struct(task); |
| 3004 | rcu_read_unlock(); |
| 3005 | |
| 3006 | if (!task) |
| 3007 | return ERR_PTR(-ESRCH); |
| 3008 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3009 | /* Reuse ptrace permission checks for now. */ |
| 3010 | err = -EACCES; |
| 3011 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 3012 | goto errout; |
| 3013 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3014 | return task; |
| 3015 | errout: |
| 3016 | put_task_struct(task); |
| 3017 | return ERR_PTR(err); |
| 3018 | |
| 3019 | } |
| 3020 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3021 | /* |
| 3022 | * Returns a matching context with refcount and pincount. |
| 3023 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3024 | static struct perf_event_context * |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 3025 | find_get_context(struct pmu *pmu, struct task_struct *task, int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3026 | { |
| 3027 | struct perf_event_context *ctx; |
| 3028 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3029 | unsigned long flags; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3030 | int ctxn, err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3031 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3032 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3033 | /* Must be root to operate on a CPU event: */ |
| 3034 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3035 | return ERR_PTR(-EACCES); |
| 3036 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3037 | /* |
| 3038 | * We could be clever and allow to attach a event to an |
| 3039 | * offline CPU and activate it when the CPU comes up, but |
| 3040 | * that's for later. |
| 3041 | */ |
| 3042 | if (!cpu_online(cpu)) |
| 3043 | return ERR_PTR(-ENODEV); |
| 3044 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3045 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3046 | ctx = &cpuctx->ctx; |
| 3047 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3048 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3049 | |
| 3050 | return ctx; |
| 3051 | } |
| 3052 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3053 | err = -EINVAL; |
| 3054 | ctxn = pmu->task_ctx_nr; |
| 3055 | if (ctxn < 0) |
| 3056 | goto errout; |
| 3057 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3058 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3059 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3060 | if (ctx) { |
| 3061 | unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3062 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3063 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3064 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3065 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3066 | err = -ENOMEM; |
| 3067 | if (!ctx) |
| 3068 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3069 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3070 | err = 0; |
| 3071 | mutex_lock(&task->perf_event_mutex); |
| 3072 | /* |
| 3073 | * If it has already passed perf_event_exit_task(). |
| 3074 | * we must see PF_EXITING, it takes this mutex too. |
| 3075 | */ |
| 3076 | if (task->flags & PF_EXITING) |
| 3077 | err = -ESRCH; |
| 3078 | else if (task->perf_event_ctxp[ctxn]) |
| 3079 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3080 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3081 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3082 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3083 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3084 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3085 | mutex_unlock(&task->perf_event_mutex); |
| 3086 | |
| 3087 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3088 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3089 | |
| 3090 | if (err == -EAGAIN) |
| 3091 | goto retry; |
| 3092 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3093 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3094 | } |
| 3095 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3096 | return ctx; |
| 3097 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3098 | errout: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3099 | return ERR_PTR(err); |
| 3100 | } |
| 3101 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3102 | static void perf_event_free_filter(struct perf_event *event); |
| 3103 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3104 | static void free_event_rcu(struct rcu_head *head) |
| 3105 | { |
| 3106 | struct perf_event *event; |
| 3107 | |
| 3108 | event = container_of(head, struct perf_event, rcu_head); |
| 3109 | if (event->ns) |
| 3110 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3111 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3112 | kfree(event); |
| 3113 | } |
| 3114 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3115 | static void ring_buffer_put(struct ring_buffer *rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3116 | static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3117 | |
| 3118 | static void free_event(struct perf_event *event) |
| 3119 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3120 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3121 | |
| 3122 | if (!event->parent) { |
Peter Zijlstra | 82cd6de | 2010-10-14 17:57:23 +0200 | [diff] [blame] | 3123 | if (event->attach_state & PERF_ATTACH_TASK) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 3124 | static_key_slow_dec_deferred(&perf_sched_events); |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 3125 | if (event->attr.mmap || event->attr.mmap_data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3126 | atomic_dec(&nr_mmap_events); |
| 3127 | if (event->attr.comm) |
| 3128 | atomic_dec(&nr_comm_events); |
| 3129 | if (event->attr.task) |
| 3130 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 3131 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 3132 | put_callchain_buffers(); |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 3133 | if (is_cgroup_event(event)) { |
| 3134 | atomic_dec(&per_cpu(perf_cgroup_events, event->cpu)); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 3135 | static_key_slow_dec_deferred(&perf_sched_events); |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 3136 | } |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 3137 | |
| 3138 | if (has_branch_stack(event)) { |
| 3139 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3140 | /* is system-wide event */ |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3141 | if (!(event->attach_state & PERF_ATTACH_TASK)) { |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 3142 | atomic_dec(&per_cpu(perf_branch_stack_events, |
| 3143 | event->cpu)); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3144 | } |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 3145 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3146 | } |
| 3147 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3148 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3149 | struct ring_buffer *rb; |
| 3150 | |
| 3151 | /* |
| 3152 | * Can happen when we close an event with re-directed output. |
| 3153 | * |
| 3154 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 3155 | * over us; possibly making our ring_buffer_put() the last. |
| 3156 | */ |
| 3157 | mutex_lock(&event->mmap_mutex); |
| 3158 | rb = event->rb; |
| 3159 | if (rb) { |
| 3160 | rcu_assign_pointer(event->rb, NULL); |
| 3161 | ring_buffer_detach(event, rb); |
| 3162 | ring_buffer_put(rb); /* could be last */ |
| 3163 | } |
| 3164 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3165 | } |
| 3166 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3167 | if (is_cgroup_event(event)) |
| 3168 | perf_detach_cgroup(event); |
| 3169 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3170 | if (event->destroy) |
| 3171 | event->destroy(event); |
| 3172 | |
Peter Zijlstra | 0c67b40 | 2010-09-13 11:15:58 +0200 | [diff] [blame] | 3173 | if (event->ctx) |
| 3174 | put_ctx(event->ctx); |
| 3175 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3176 | call_rcu(&event->rcu_head, free_event_rcu); |
| 3177 | } |
| 3178 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3179 | int perf_event_release_kernel(struct perf_event *event) |
| 3180 | { |
| 3181 | struct perf_event_context *ctx = event->ctx; |
| 3182 | |
| 3183 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 3184 | /* |
| 3185 | * There are two ways this annotation is useful: |
| 3186 | * |
| 3187 | * 1) there is a lock recursion from perf_event_exit_task |
| 3188 | * see the comment there. |
| 3189 | * |
| 3190 | * 2) there is a lock-inversion with mmap_sem through |
| 3191 | * perf_event_read_group(), which takes faults while |
| 3192 | * holding ctx->mutex, however this is called after |
| 3193 | * the last filedesc died, so there is no possibility |
| 3194 | * to trigger the AB-BA case. |
| 3195 | */ |
| 3196 | mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING); |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 3197 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 3198 | perf_group_detach(event); |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 3199 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | e03a9a5 | 2011-04-09 21:17:47 +0200 | [diff] [blame] | 3200 | perf_remove_from_context(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3201 | mutex_unlock(&ctx->mutex); |
| 3202 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3203 | free_event(event); |
| 3204 | |
| 3205 | return 0; |
| 3206 | } |
| 3207 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 3208 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3209 | /* |
| 3210 | * Called when the last reference to the file is gone. |
| 3211 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3212 | static void put_event(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3213 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3214 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3215 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3216 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 3217 | return; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3218 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3219 | rcu_read_lock(); |
| 3220 | owner = ACCESS_ONCE(event->owner); |
| 3221 | /* |
| 3222 | * Matches the smp_wmb() in perf_event_exit_task(). If we observe |
| 3223 | * !owner it means the list deletion is complete and we can indeed |
| 3224 | * free this event, otherwise we need to serialize on |
| 3225 | * owner->perf_event_mutex. |
| 3226 | */ |
| 3227 | smp_read_barrier_depends(); |
| 3228 | if (owner) { |
| 3229 | /* |
| 3230 | * Since delayed_put_task_struct() also drops the last |
| 3231 | * task reference we can safely take a new reference |
| 3232 | * while holding the rcu_read_lock(). |
| 3233 | */ |
| 3234 | get_task_struct(owner); |
| 3235 | } |
| 3236 | rcu_read_unlock(); |
| 3237 | |
| 3238 | if (owner) { |
| 3239 | mutex_lock(&owner->perf_event_mutex); |
| 3240 | /* |
| 3241 | * We have to re-check the event->owner field, if it is cleared |
| 3242 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 3243 | * ensured they're done, and we can proceed with freeing the |
| 3244 | * event. |
| 3245 | */ |
| 3246 | if (event->owner) |
| 3247 | list_del_init(&event->owner_entry); |
| 3248 | mutex_unlock(&owner->perf_event_mutex); |
| 3249 | put_task_struct(owner); |
| 3250 | } |
| 3251 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3252 | perf_event_release_kernel(event); |
| 3253 | } |
| 3254 | |
| 3255 | static int perf_release(struct inode *inode, struct file *file) |
| 3256 | { |
| 3257 | put_event(file->private_data); |
| 3258 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3259 | } |
| 3260 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3261 | 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] | 3262 | { |
| 3263 | struct perf_event *child; |
| 3264 | u64 total = 0; |
| 3265 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3266 | *enabled = 0; |
| 3267 | *running = 0; |
| 3268 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3269 | mutex_lock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3270 | total += perf_event_read(event); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3271 | *enabled += event->total_time_enabled + |
| 3272 | atomic64_read(&event->child_total_time_enabled); |
| 3273 | *running += event->total_time_running + |
| 3274 | atomic64_read(&event->child_total_time_running); |
| 3275 | |
| 3276 | list_for_each_entry(child, &event->child_list, child_list) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3277 | total += perf_event_read(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3278 | *enabled += child->total_time_enabled; |
| 3279 | *running += child->total_time_running; |
| 3280 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3281 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3282 | |
| 3283 | return total; |
| 3284 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3285 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3286 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3287 | static int perf_event_read_group(struct perf_event *event, |
| 3288 | u64 read_format, char __user *buf) |
| 3289 | { |
| 3290 | struct perf_event *leader = event->group_leader, *sub; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3291 | int n = 0, size = 0, ret = -EFAULT; |
| 3292 | struct perf_event_context *ctx = leader->ctx; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3293 | u64 values[5]; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3294 | u64 count, enabled, running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3295 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3296 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3297 | count = perf_event_read_value(leader, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3298 | |
| 3299 | values[n++] = 1 + leader->nr_siblings; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3300 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3301 | values[n++] = enabled; |
| 3302 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3303 | values[n++] = running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3304 | values[n++] = count; |
| 3305 | if (read_format & PERF_FORMAT_ID) |
| 3306 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3307 | |
| 3308 | size = n * sizeof(u64); |
| 3309 | |
| 3310 | if (copy_to_user(buf, values, size)) |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3311 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3312 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3313 | ret = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3314 | |
| 3315 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3316 | n = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3317 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3318 | values[n++] = perf_event_read_value(sub, &enabled, &running); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3319 | if (read_format & PERF_FORMAT_ID) |
| 3320 | values[n++] = primary_event_id(sub); |
| 3321 | |
| 3322 | size = n * sizeof(u64); |
| 3323 | |
Stephane Eranian | 184d3da | 2009-11-23 21:40:49 -0800 | [diff] [blame] | 3324 | if (copy_to_user(buf + ret, values, size)) { |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3325 | ret = -EFAULT; |
| 3326 | goto unlock; |
| 3327 | } |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3328 | |
| 3329 | ret += size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3330 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3331 | unlock: |
| 3332 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3333 | |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3334 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | static int perf_event_read_one(struct perf_event *event, |
| 3338 | u64 read_format, char __user *buf) |
| 3339 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3340 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3341 | u64 values[4]; |
| 3342 | int n = 0; |
| 3343 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3344 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 3345 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3346 | values[n++] = enabled; |
| 3347 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3348 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3349 | if (read_format & PERF_FORMAT_ID) |
| 3350 | values[n++] = primary_event_id(event); |
| 3351 | |
| 3352 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 3353 | return -EFAULT; |
| 3354 | |
| 3355 | return n * sizeof(u64); |
| 3356 | } |
| 3357 | |
| 3358 | /* |
| 3359 | * Read the performance event - simple non blocking version for now |
| 3360 | */ |
| 3361 | static ssize_t |
| 3362 | perf_read_hw(struct perf_event *event, char __user *buf, size_t count) |
| 3363 | { |
| 3364 | u64 read_format = event->attr.read_format; |
| 3365 | int ret; |
| 3366 | |
| 3367 | /* |
| 3368 | * Return end-of-file for a read on a event that is in |
| 3369 | * error state (i.e. because it was pinned but it couldn't be |
| 3370 | * scheduled on to the CPU at some point). |
| 3371 | */ |
| 3372 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 3373 | return 0; |
| 3374 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 3375 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3376 | return -ENOSPC; |
| 3377 | |
| 3378 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3379 | if (read_format & PERF_FORMAT_GROUP) |
| 3380 | ret = perf_event_read_group(event, read_format, buf); |
| 3381 | else |
| 3382 | ret = perf_event_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3383 | |
| 3384 | return ret; |
| 3385 | } |
| 3386 | |
| 3387 | static ssize_t |
| 3388 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 3389 | { |
| 3390 | struct perf_event *event = file->private_data; |
| 3391 | |
| 3392 | return perf_read_hw(event, buf, count); |
| 3393 | } |
| 3394 | |
| 3395 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 3396 | { |
| 3397 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3398 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3399 | unsigned int events = POLL_HUP; |
| 3400 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3401 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3402 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 3403 | * perf_event_set_output() can swizzle our rb and make us miss wakeups. |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3404 | */ |
| 3405 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3406 | rb = event->rb; |
| 3407 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3408 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3409 | mutex_unlock(&event->mmap_mutex); |
| 3410 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3411 | poll_wait(file, &event->waitq, wait); |
| 3412 | |
| 3413 | return events; |
| 3414 | } |
| 3415 | |
| 3416 | static void perf_event_reset(struct perf_event *event) |
| 3417 | { |
| 3418 | (void)perf_event_read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3419 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3420 | perf_event_update_userpage(event); |
| 3421 | } |
| 3422 | |
| 3423 | /* |
| 3424 | * Holding the top-level event's child_mutex means that any |
| 3425 | * descendant process that has inherited this event will block |
| 3426 | * in sync_child_event if it goes to exit, thus satisfying the |
| 3427 | * task existence requirements of perf_event_enable/disable. |
| 3428 | */ |
| 3429 | static void perf_event_for_each_child(struct perf_event *event, |
| 3430 | void (*func)(struct perf_event *)) |
| 3431 | { |
| 3432 | struct perf_event *child; |
| 3433 | |
| 3434 | WARN_ON_ONCE(event->ctx->parent_ctx); |
| 3435 | mutex_lock(&event->child_mutex); |
| 3436 | func(event); |
| 3437 | list_for_each_entry(child, &event->child_list, child_list) |
| 3438 | func(child); |
| 3439 | mutex_unlock(&event->child_mutex); |
| 3440 | } |
| 3441 | |
| 3442 | static void perf_event_for_each(struct perf_event *event, |
| 3443 | void (*func)(struct perf_event *)) |
| 3444 | { |
| 3445 | struct perf_event_context *ctx = event->ctx; |
| 3446 | struct perf_event *sibling; |
| 3447 | |
| 3448 | WARN_ON_ONCE(ctx->parent_ctx); |
| 3449 | mutex_lock(&ctx->mutex); |
| 3450 | event = event->group_leader; |
| 3451 | |
| 3452 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3453 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 3454 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3455 | mutex_unlock(&ctx->mutex); |
| 3456 | } |
| 3457 | |
| 3458 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 3459 | { |
| 3460 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3461 | int ret = 0; |
| 3462 | u64 value; |
| 3463 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 3464 | if (!is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3465 | return -EINVAL; |
| 3466 | |
John Blackwood | ad0cf34 | 2010-09-28 18:03:11 -0400 | [diff] [blame] | 3467 | if (copy_from_user(&value, arg, sizeof(value))) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3468 | return -EFAULT; |
| 3469 | |
| 3470 | if (!value) |
| 3471 | return -EINVAL; |
| 3472 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3473 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3474 | if (event->attr.freq) { |
| 3475 | if (value > sysctl_perf_event_sample_rate) { |
| 3476 | ret = -EINVAL; |
| 3477 | goto unlock; |
| 3478 | } |
| 3479 | |
| 3480 | event->attr.sample_freq = value; |
| 3481 | } else { |
| 3482 | event->attr.sample_period = value; |
| 3483 | event->hw.sample_period = value; |
| 3484 | } |
| 3485 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3486 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3487 | |
| 3488 | return ret; |
| 3489 | } |
| 3490 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3491 | static const struct file_operations perf_fops; |
| 3492 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3493 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3494 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3495 | struct fd f = fdget(fd); |
| 3496 | if (!f.file) |
| 3497 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3498 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3499 | if (f.file->f_op != &perf_fops) { |
| 3500 | fdput(f); |
| 3501 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3502 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3503 | *p = f; |
| 3504 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | static int perf_event_set_output(struct perf_event *event, |
| 3508 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3509 | 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] | 3510 | |
| 3511 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 3512 | { |
| 3513 | struct perf_event *event = file->private_data; |
| 3514 | void (*func)(struct perf_event *); |
| 3515 | u32 flags = arg; |
| 3516 | |
| 3517 | switch (cmd) { |
| 3518 | case PERF_EVENT_IOC_ENABLE: |
| 3519 | func = perf_event_enable; |
| 3520 | break; |
| 3521 | case PERF_EVENT_IOC_DISABLE: |
| 3522 | func = perf_event_disable; |
| 3523 | break; |
| 3524 | case PERF_EVENT_IOC_RESET: |
| 3525 | func = perf_event_reset; |
| 3526 | break; |
| 3527 | |
| 3528 | case PERF_EVENT_IOC_REFRESH: |
| 3529 | return perf_event_refresh(event, arg); |
| 3530 | |
| 3531 | case PERF_EVENT_IOC_PERIOD: |
| 3532 | return perf_event_period(event, (u64 __user *)arg); |
| 3533 | |
| 3534 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3535 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3536 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3537 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3538 | struct perf_event *output_event; |
| 3539 | struct fd output; |
| 3540 | ret = perf_fget_light(arg, &output); |
| 3541 | if (ret) |
| 3542 | return ret; |
| 3543 | output_event = output.file->private_data; |
| 3544 | ret = perf_event_set_output(event, output_event); |
| 3545 | fdput(output); |
| 3546 | } else { |
| 3547 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3548 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3549 | return ret; |
| 3550 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3551 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3552 | case PERF_EVENT_IOC_SET_FILTER: |
| 3553 | return perf_event_set_filter(event, (void __user *)arg); |
| 3554 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3555 | default: |
| 3556 | return -ENOTTY; |
| 3557 | } |
| 3558 | |
| 3559 | if (flags & PERF_IOC_FLAG_GROUP) |
| 3560 | perf_event_for_each(event, func); |
| 3561 | else |
| 3562 | perf_event_for_each_child(event, func); |
| 3563 | |
| 3564 | return 0; |
| 3565 | } |
| 3566 | |
| 3567 | int perf_event_task_enable(void) |
| 3568 | { |
| 3569 | struct perf_event *event; |
| 3570 | |
| 3571 | mutex_lock(¤t->perf_event_mutex); |
| 3572 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3573 | perf_event_for_each_child(event, perf_event_enable); |
| 3574 | mutex_unlock(¤t->perf_event_mutex); |
| 3575 | |
| 3576 | return 0; |
| 3577 | } |
| 3578 | |
| 3579 | int perf_event_task_disable(void) |
| 3580 | { |
| 3581 | struct perf_event *event; |
| 3582 | |
| 3583 | mutex_lock(¤t->perf_event_mutex); |
| 3584 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3585 | perf_event_for_each_child(event, perf_event_disable); |
| 3586 | mutex_unlock(¤t->perf_event_mutex); |
| 3587 | |
| 3588 | return 0; |
| 3589 | } |
| 3590 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3591 | static int perf_event_index(struct perf_event *event) |
| 3592 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 3593 | if (event->hw.state & PERF_HES_STOPPED) |
| 3594 | return 0; |
| 3595 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3596 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 3597 | return 0; |
| 3598 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 3599 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3600 | } |
| 3601 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3602 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3603 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3604 | u64 *enabled, |
| 3605 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3606 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3607 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3608 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3609 | *now = perf_clock(); |
| 3610 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3611 | *enabled = ctx_time - event->tstamp_enabled; |
| 3612 | *running = ctx_time - event->tstamp_running; |
| 3613 | } |
| 3614 | |
Peter Zijlstra | c720620 | 2012-03-22 17:26:36 +0100 | [diff] [blame] | 3615 | void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3616 | { |
| 3617 | } |
| 3618 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3619 | /* |
| 3620 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 3621 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 3622 | * code calls this from NMI context. |
| 3623 | */ |
| 3624 | void perf_event_update_userpage(struct perf_event *event) |
| 3625 | { |
| 3626 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3627 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3628 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3629 | |
| 3630 | rcu_read_lock(); |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3631 | /* |
| 3632 | * compute total_time_enabled, total_time_running |
| 3633 | * based on snapshot values taken when the event |
| 3634 | * was last scheduled in. |
| 3635 | * |
| 3636 | * we cannot simply called update_context_time() |
| 3637 | * because of locking issue as we can be called in |
| 3638 | * NMI context |
| 3639 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3640 | calc_timer_values(event, &now, &enabled, &running); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3641 | rb = rcu_dereference(event->rb); |
| 3642 | if (!rb) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3643 | goto unlock; |
| 3644 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3645 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3646 | |
| 3647 | /* |
| 3648 | * Disable preemption so as to not let the corresponding user-space |
| 3649 | * spin too long if we get preempted. |
| 3650 | */ |
| 3651 | preempt_disable(); |
| 3652 | ++userpg->lock; |
| 3653 | barrier(); |
| 3654 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3655 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 3656 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3657 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3658 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3659 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3660 | atomic64_read(&event->child_total_time_enabled); |
| 3661 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3662 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3663 | atomic64_read(&event->child_total_time_running); |
| 3664 | |
Peter Zijlstra | c720620 | 2012-03-22 17:26:36 +0100 | [diff] [blame] | 3665 | arch_perf_update_userpage(userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3666 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3667 | barrier(); |
| 3668 | ++userpg->lock; |
| 3669 | preempt_enable(); |
| 3670 | unlock: |
| 3671 | rcu_read_unlock(); |
| 3672 | } |
| 3673 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3674 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 3675 | { |
| 3676 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3677 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3678 | int ret = VM_FAULT_SIGBUS; |
| 3679 | |
| 3680 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 3681 | if (vmf->pgoff == 0) |
| 3682 | ret = 0; |
| 3683 | return ret; |
| 3684 | } |
| 3685 | |
| 3686 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3687 | rb = rcu_dereference(event->rb); |
| 3688 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3689 | goto unlock; |
| 3690 | |
| 3691 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 3692 | goto unlock; |
| 3693 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3694 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3695 | if (!vmf->page) |
| 3696 | goto unlock; |
| 3697 | |
| 3698 | get_page(vmf->page); |
| 3699 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 3700 | vmf->page->index = vmf->pgoff; |
| 3701 | |
| 3702 | ret = 0; |
| 3703 | unlock: |
| 3704 | rcu_read_unlock(); |
| 3705 | |
| 3706 | return ret; |
| 3707 | } |
| 3708 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3709 | static void ring_buffer_attach(struct perf_event *event, |
| 3710 | struct ring_buffer *rb) |
| 3711 | { |
| 3712 | unsigned long flags; |
| 3713 | |
| 3714 | if (!list_empty(&event->rb_entry)) |
| 3715 | return; |
| 3716 | |
| 3717 | spin_lock_irqsave(&rb->event_lock, flags); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3718 | if (list_empty(&event->rb_entry)) |
| 3719 | list_add(&event->rb_entry, &rb->event_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3720 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 3721 | } |
| 3722 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3723 | static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb) |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3724 | { |
| 3725 | unsigned long flags; |
| 3726 | |
| 3727 | if (list_empty(&event->rb_entry)) |
| 3728 | return; |
| 3729 | |
| 3730 | spin_lock_irqsave(&rb->event_lock, flags); |
| 3731 | list_del_init(&event->rb_entry); |
| 3732 | wake_up_all(&event->waitq); |
| 3733 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 3734 | } |
| 3735 | |
| 3736 | static void ring_buffer_wakeup(struct perf_event *event) |
| 3737 | { |
| 3738 | struct ring_buffer *rb; |
| 3739 | |
| 3740 | rcu_read_lock(); |
| 3741 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3742 | if (rb) { |
| 3743 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 3744 | wake_up_all(&event->waitq); |
| 3745 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3746 | rcu_read_unlock(); |
| 3747 | } |
| 3748 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3749 | static void rb_free_rcu(struct rcu_head *rcu_head) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3750 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3751 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3752 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3753 | rb = container_of(rcu_head, struct ring_buffer, rcu_head); |
| 3754 | rb_free(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3755 | } |
| 3756 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3757 | static struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3758 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3759 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3760 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3761 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3762 | rb = rcu_dereference(event->rb); |
| 3763 | if (rb) { |
| 3764 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 3765 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3766 | } |
| 3767 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3768 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3769 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3770 | } |
| 3771 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3772 | static void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3773 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3774 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3775 | return; |
| 3776 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3777 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3778 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3779 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 3783 | { |
| 3784 | struct perf_event *event = vma->vm_file->private_data; |
| 3785 | |
| 3786 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3787 | atomic_inc(&event->rb->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3788 | } |
| 3789 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3790 | /* |
| 3791 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 3792 | * event, or through other events by use of perf_event_set_output(). |
| 3793 | * |
| 3794 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 3795 | * the buffer here, where we still have a VM context. This means we need |
| 3796 | * to detach all events redirecting to us. |
| 3797 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3798 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 3799 | { |
| 3800 | struct perf_event *event = vma->vm_file->private_data; |
| 3801 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3802 | struct ring_buffer *rb = event->rb; |
| 3803 | struct user_struct *mmap_user = rb->mmap_user; |
| 3804 | int mmap_locked = rb->mmap_locked; |
| 3805 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3806 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3807 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3808 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3809 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
| 3810 | return; |
| 3811 | |
| 3812 | /* Detach current event from the buffer. */ |
| 3813 | rcu_assign_pointer(event->rb, NULL); |
| 3814 | ring_buffer_detach(event, rb); |
| 3815 | mutex_unlock(&event->mmap_mutex); |
| 3816 | |
| 3817 | /* If there's still other mmap()s of this buffer, we're done. */ |
| 3818 | if (atomic_read(&rb->mmap_count)) { |
| 3819 | ring_buffer_put(rb); /* can't be last */ |
| 3820 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3821 | } |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3822 | |
| 3823 | /* |
| 3824 | * No other mmap()s, detach from all other events that might redirect |
| 3825 | * into the now unreachable buffer. Somewhat complicated by the |
| 3826 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 3827 | */ |
| 3828 | again: |
| 3829 | rcu_read_lock(); |
| 3830 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 3831 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 3832 | /* |
| 3833 | * This event is en-route to free_event() which will |
| 3834 | * detach it and remove it from the list. |
| 3835 | */ |
| 3836 | continue; |
| 3837 | } |
| 3838 | rcu_read_unlock(); |
| 3839 | |
| 3840 | mutex_lock(&event->mmap_mutex); |
| 3841 | /* |
| 3842 | * Check we didn't race with perf_event_set_output() which can |
| 3843 | * swizzle the rb from under us while we were waiting to |
| 3844 | * acquire mmap_mutex. |
| 3845 | * |
| 3846 | * If we find a different rb; ignore this event, a next |
| 3847 | * iteration will no longer find it on the list. We have to |
| 3848 | * still restart the iteration to make sure we're not now |
| 3849 | * iterating the wrong list. |
| 3850 | */ |
| 3851 | if (event->rb == rb) { |
| 3852 | rcu_assign_pointer(event->rb, NULL); |
| 3853 | ring_buffer_detach(event, rb); |
| 3854 | ring_buffer_put(rb); /* can't be last, we still have one */ |
| 3855 | } |
| 3856 | mutex_unlock(&event->mmap_mutex); |
| 3857 | put_event(event); |
| 3858 | |
| 3859 | /* |
| 3860 | * Restart the iteration; either we're on the wrong list or |
| 3861 | * destroyed its integrity by doing a deletion. |
| 3862 | */ |
| 3863 | goto again; |
| 3864 | } |
| 3865 | rcu_read_unlock(); |
| 3866 | |
| 3867 | /* |
| 3868 | * It could be there's still a few 0-ref events on the list; they'll |
| 3869 | * get cleaned up by free_event() -- they'll also still have their |
| 3870 | * ref on the rb and will free it whenever they are done with it. |
| 3871 | * |
| 3872 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 3873 | * undo the VM accounting. |
| 3874 | */ |
| 3875 | |
| 3876 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 3877 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 3878 | free_uid(mmap_user); |
| 3879 | |
| 3880 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 3883 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3884 | .open = perf_mmap_open, |
| 3885 | .close = perf_mmap_close, |
| 3886 | .fault = perf_mmap_fault, |
| 3887 | .page_mkwrite = perf_mmap_fault, |
| 3888 | }; |
| 3889 | |
| 3890 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 3891 | { |
| 3892 | struct perf_event *event = file->private_data; |
| 3893 | unsigned long user_locked, user_lock_limit; |
| 3894 | struct user_struct *user = current_user(); |
| 3895 | unsigned long locked, lock_limit; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3896 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3897 | unsigned long vma_size; |
| 3898 | unsigned long nr_pages; |
| 3899 | long user_extra, extra; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3900 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3901 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 3902 | /* |
| 3903 | * Don't allow mmap() of inherited per-task counters. This would |
| 3904 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3905 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 3906 | */ |
| 3907 | if (event->cpu == -1 && event->attr.inherit) |
| 3908 | return -EINVAL; |
| 3909 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3910 | if (!(vma->vm_flags & VM_SHARED)) |
| 3911 | return -EINVAL; |
| 3912 | |
| 3913 | vma_size = vma->vm_end - vma->vm_start; |
| 3914 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 3915 | |
| 3916 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3917 | * If we have rb pages ensure they're a power-of-two number, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3918 | * can do bitmasks instead of modulo. |
| 3919 | */ |
| 3920 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
| 3921 | return -EINVAL; |
| 3922 | |
| 3923 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 3924 | return -EINVAL; |
| 3925 | |
| 3926 | if (vma->vm_pgoff != 0) |
| 3927 | return -EINVAL; |
| 3928 | |
| 3929 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3930 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3931 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3932 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3933 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3934 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3935 | goto unlock; |
| 3936 | } |
| 3937 | |
| 3938 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 3939 | /* |
| 3940 | * Raced against perf_mmap_close() through |
| 3941 | * perf_event_set_output(). Try again, hope for better |
| 3942 | * luck. |
| 3943 | */ |
| 3944 | mutex_unlock(&event->mmap_mutex); |
| 3945 | goto again; |
| 3946 | } |
| 3947 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3948 | goto unlock; |
| 3949 | } |
| 3950 | |
| 3951 | user_extra = nr_pages + 1; |
| 3952 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 3953 | |
| 3954 | /* |
| 3955 | * Increase the limit linearly with more CPUs: |
| 3956 | */ |
| 3957 | user_lock_limit *= num_online_cpus(); |
| 3958 | |
| 3959 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 3960 | |
| 3961 | extra = 0; |
| 3962 | if (user_locked > user_lock_limit) |
| 3963 | extra = user_locked - user_lock_limit; |
| 3964 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 3965 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3966 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 3967 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3968 | |
| 3969 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 3970 | !capable(CAP_IPC_LOCK)) { |
| 3971 | ret = -EPERM; |
| 3972 | goto unlock; |
| 3973 | } |
| 3974 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3975 | WARN_ON(event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3976 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3977 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3978 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 3979 | |
Vince Weaver | 4ec8363 | 2011-06-01 15:15:36 -0400 | [diff] [blame] | 3980 | rb = rb_alloc(nr_pages, |
| 3981 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 3982 | event->cpu, flags); |
| 3983 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3984 | if (!rb) { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3985 | ret = -ENOMEM; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3986 | goto unlock; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3987 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 3988 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3989 | atomic_set(&rb->mmap_count, 1); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 3990 | rb->mmap_locked = extra; |
| 3991 | rb->mmap_user = get_current_user(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3992 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3993 | atomic_long_add(user_extra, &user->locked_vm); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 3994 | vma->vm_mm->pinned_vm += extra; |
| 3995 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3996 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 3997 | rcu_assign_pointer(event->rb, rb); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3998 | |
Peter Zijlstra | 9a0f05c | 2011-11-21 15:13:29 +0100 | [diff] [blame] | 3999 | perf_event_update_userpage(event); |
| 4000 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4001 | unlock: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4002 | if (!ret) |
| 4003 | atomic_inc(&event->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4004 | mutex_unlock(&event->mmap_mutex); |
| 4005 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4006 | /* |
| 4007 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 4008 | * vma. |
| 4009 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4010 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4011 | vma->vm_ops = &perf_mmap_vmops; |
| 4012 | |
| 4013 | return ret; |
| 4014 | } |
| 4015 | |
| 4016 | static int perf_fasync(int fd, struct file *filp, int on) |
| 4017 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4018 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4019 | struct perf_event *event = filp->private_data; |
| 4020 | int retval; |
| 4021 | |
| 4022 | mutex_lock(&inode->i_mutex); |
| 4023 | retval = fasync_helper(fd, filp, on, &event->fasync); |
| 4024 | mutex_unlock(&inode->i_mutex); |
| 4025 | |
| 4026 | if (retval < 0) |
| 4027 | return retval; |
| 4028 | |
| 4029 | return 0; |
| 4030 | } |
| 4031 | |
| 4032 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 4033 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4034 | .release = perf_release, |
| 4035 | .read = perf_read, |
| 4036 | .poll = perf_poll, |
| 4037 | .unlocked_ioctl = perf_ioctl, |
| 4038 | .compat_ioctl = perf_ioctl, |
| 4039 | .mmap = perf_mmap, |
| 4040 | .fasync = perf_fasync, |
| 4041 | }; |
| 4042 | |
| 4043 | /* |
| 4044 | * Perf event wakeup |
| 4045 | * |
| 4046 | * If there's data, ensure we set the poll() state and publish everything |
| 4047 | * to user-space before waking everybody up. |
| 4048 | */ |
| 4049 | |
| 4050 | void perf_event_wakeup(struct perf_event *event) |
| 4051 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4052 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4053 | |
| 4054 | if (event->pending_kill) { |
| 4055 | kill_fasync(&event->fasync, SIGIO, event->pending_kill); |
| 4056 | event->pending_kill = 0; |
| 4057 | } |
| 4058 | } |
| 4059 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 4060 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4061 | { |
| 4062 | struct perf_event *event = container_of(entry, |
| 4063 | struct perf_event, pending); |
| 4064 | |
| 4065 | if (event->pending_disable) { |
| 4066 | event->pending_disable = 0; |
| 4067 | __perf_event_disable(event); |
| 4068 | } |
| 4069 | |
| 4070 | if (event->pending_wakeup) { |
| 4071 | event->pending_wakeup = 0; |
| 4072 | perf_event_wakeup(event); |
| 4073 | } |
| 4074 | } |
| 4075 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4076 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 4077 | * We assume there is only KVM supporting the callbacks. |
| 4078 | * Later on, we might change it to a list if there is |
| 4079 | * another virtualization implementation supporting the callbacks. |
| 4080 | */ |
| 4081 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 4082 | |
| 4083 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4084 | { |
| 4085 | perf_guest_cbs = cbs; |
| 4086 | return 0; |
| 4087 | } |
| 4088 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 4089 | |
| 4090 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4091 | { |
| 4092 | perf_guest_cbs = NULL; |
| 4093 | return 0; |
| 4094 | } |
| 4095 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 4096 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4097 | static void |
| 4098 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 4099 | struct pt_regs *regs, u64 mask) |
| 4100 | { |
| 4101 | int bit; |
| 4102 | |
| 4103 | for_each_set_bit(bit, (const unsigned long *) &mask, |
| 4104 | sizeof(mask) * BITS_PER_BYTE) { |
| 4105 | u64 val; |
| 4106 | |
| 4107 | val = perf_reg_value(regs, bit); |
| 4108 | perf_output_put(handle, val); |
| 4109 | } |
| 4110 | } |
| 4111 | |
| 4112 | static void perf_sample_regs_user(struct perf_regs_user *regs_user, |
| 4113 | struct pt_regs *regs) |
| 4114 | { |
| 4115 | if (!user_mode(regs)) { |
| 4116 | if (current->mm) |
| 4117 | regs = task_pt_regs(current); |
| 4118 | else |
| 4119 | regs = NULL; |
| 4120 | } |
| 4121 | |
| 4122 | if (regs) { |
| 4123 | regs_user->regs = regs; |
| 4124 | regs_user->abi = perf_reg_abi(current); |
| 4125 | } |
| 4126 | } |
| 4127 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4128 | /* |
| 4129 | * Get remaining task size from user stack pointer. |
| 4130 | * |
| 4131 | * It'd be better to take stack vma map and limit this more |
| 4132 | * precisly, but there's no way to get it safely under interrupt, |
| 4133 | * so using TASK_SIZE as limit. |
| 4134 | */ |
| 4135 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 4136 | { |
| 4137 | unsigned long addr = perf_user_stack_pointer(regs); |
| 4138 | |
| 4139 | if (!addr || addr >= TASK_SIZE) |
| 4140 | return 0; |
| 4141 | |
| 4142 | return TASK_SIZE - addr; |
| 4143 | } |
| 4144 | |
| 4145 | static u16 |
| 4146 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 4147 | struct pt_regs *regs) |
| 4148 | { |
| 4149 | u64 task_size; |
| 4150 | |
| 4151 | /* No regs, no stack pointer, no dump. */ |
| 4152 | if (!regs) |
| 4153 | return 0; |
| 4154 | |
| 4155 | /* |
| 4156 | * Check if we fit in with the requested stack size into the: |
| 4157 | * - TASK_SIZE |
| 4158 | * If we don't, we limit the size to the TASK_SIZE. |
| 4159 | * |
| 4160 | * - remaining sample size |
| 4161 | * If we don't, we customize the stack size to |
| 4162 | * fit in to the remaining sample size. |
| 4163 | */ |
| 4164 | |
| 4165 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 4166 | stack_size = min(stack_size, (u16) task_size); |
| 4167 | |
| 4168 | /* Current header size plus static size and dynamic size. */ |
| 4169 | header_size += 2 * sizeof(u64); |
| 4170 | |
| 4171 | /* Do we fit in with the current stack dump size? */ |
| 4172 | if ((u16) (header_size + stack_size) < header_size) { |
| 4173 | /* |
| 4174 | * If we overflow the maximum size for the sample, |
| 4175 | * we customize the stack dump size to fit in. |
| 4176 | */ |
| 4177 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 4178 | stack_size = round_up(stack_size, sizeof(u64)); |
| 4179 | } |
| 4180 | |
| 4181 | return stack_size; |
| 4182 | } |
| 4183 | |
| 4184 | static void |
| 4185 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 4186 | struct pt_regs *regs) |
| 4187 | { |
| 4188 | /* Case of a kernel thread, nothing to dump */ |
| 4189 | if (!regs) { |
| 4190 | u64 size = 0; |
| 4191 | perf_output_put(handle, size); |
| 4192 | } else { |
| 4193 | unsigned long sp; |
| 4194 | unsigned int rem; |
| 4195 | u64 dyn_size; |
| 4196 | |
| 4197 | /* |
| 4198 | * We dump: |
| 4199 | * static size |
| 4200 | * - the size requested by user or the best one we can fit |
| 4201 | * in to the sample max size |
| 4202 | * data |
| 4203 | * - user stack dump data |
| 4204 | * dynamic size |
| 4205 | * - the actual dumped size |
| 4206 | */ |
| 4207 | |
| 4208 | /* Static size. */ |
| 4209 | perf_output_put(handle, dump_size); |
| 4210 | |
| 4211 | /* Data. */ |
| 4212 | sp = perf_user_stack_pointer(regs); |
| 4213 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 4214 | dyn_size = dump_size - rem; |
| 4215 | |
| 4216 | perf_output_skip(handle, rem); |
| 4217 | |
| 4218 | /* Dynamic size. */ |
| 4219 | perf_output_put(handle, dyn_size); |
| 4220 | } |
| 4221 | } |
| 4222 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4223 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 4224 | struct perf_sample_data *data, |
| 4225 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4226 | { |
| 4227 | u64 sample_type = event->attr.sample_type; |
| 4228 | |
| 4229 | data->type = sample_type; |
| 4230 | header->size += event->id_header_size; |
| 4231 | |
| 4232 | if (sample_type & PERF_SAMPLE_TID) { |
| 4233 | /* namespace issues */ |
| 4234 | data->tid_entry.pid = perf_event_pid(event, current); |
| 4235 | data->tid_entry.tid = perf_event_tid(event, current); |
| 4236 | } |
| 4237 | |
| 4238 | if (sample_type & PERF_SAMPLE_TIME) |
| 4239 | data->time = perf_clock(); |
| 4240 | |
| 4241 | if (sample_type & PERF_SAMPLE_ID) |
| 4242 | data->id = primary_event_id(event); |
| 4243 | |
| 4244 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4245 | data->stream_id = event->id; |
| 4246 | |
| 4247 | if (sample_type & PERF_SAMPLE_CPU) { |
| 4248 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 4249 | data->cpu_entry.reserved = 0; |
| 4250 | } |
| 4251 | } |
| 4252 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4253 | void perf_event_header__init_id(struct perf_event_header *header, |
| 4254 | struct perf_sample_data *data, |
| 4255 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4256 | { |
| 4257 | if (event->attr.sample_id_all) |
| 4258 | __perf_event_header__init_id(header, data, event); |
| 4259 | } |
| 4260 | |
| 4261 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 4262 | struct perf_sample_data *data) |
| 4263 | { |
| 4264 | u64 sample_type = data->type; |
| 4265 | |
| 4266 | if (sample_type & PERF_SAMPLE_TID) |
| 4267 | perf_output_put(handle, data->tid_entry); |
| 4268 | |
| 4269 | if (sample_type & PERF_SAMPLE_TIME) |
| 4270 | perf_output_put(handle, data->time); |
| 4271 | |
| 4272 | if (sample_type & PERF_SAMPLE_ID) |
| 4273 | perf_output_put(handle, data->id); |
| 4274 | |
| 4275 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4276 | perf_output_put(handle, data->stream_id); |
| 4277 | |
| 4278 | if (sample_type & PERF_SAMPLE_CPU) |
| 4279 | perf_output_put(handle, data->cpu_entry); |
| 4280 | } |
| 4281 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4282 | void perf_event__output_id_sample(struct perf_event *event, |
| 4283 | struct perf_output_handle *handle, |
| 4284 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4285 | { |
| 4286 | if (event->attr.sample_id_all) |
| 4287 | __perf_event__output_id_sample(handle, sample); |
| 4288 | } |
| 4289 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4290 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4291 | struct perf_event *event, |
| 4292 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4293 | { |
| 4294 | u64 read_format = event->attr.read_format; |
| 4295 | u64 values[4]; |
| 4296 | int n = 0; |
| 4297 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4298 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4299 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4300 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4301 | atomic64_read(&event->child_total_time_enabled); |
| 4302 | } |
| 4303 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4304 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4305 | atomic64_read(&event->child_total_time_running); |
| 4306 | } |
| 4307 | if (read_format & PERF_FORMAT_ID) |
| 4308 | values[n++] = primary_event_id(event); |
| 4309 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4310 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | /* |
| 4314 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 4315 | */ |
| 4316 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4317 | struct perf_event *event, |
| 4318 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4319 | { |
| 4320 | struct perf_event *leader = event->group_leader, *sub; |
| 4321 | u64 read_format = event->attr.read_format; |
| 4322 | u64 values[5]; |
| 4323 | int n = 0; |
| 4324 | |
| 4325 | values[n++] = 1 + leader->nr_siblings; |
| 4326 | |
| 4327 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4328 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4329 | |
| 4330 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4331 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4332 | |
| 4333 | if (leader != event) |
| 4334 | leader->pmu->read(leader); |
| 4335 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4336 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4337 | if (read_format & PERF_FORMAT_ID) |
| 4338 | values[n++] = primary_event_id(leader); |
| 4339 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4340 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4341 | |
| 4342 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 4343 | n = 0; |
| 4344 | |
| 4345 | if (sub != event) |
| 4346 | sub->pmu->read(sub); |
| 4347 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4348 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4349 | if (read_format & PERF_FORMAT_ID) |
| 4350 | values[n++] = primary_event_id(sub); |
| 4351 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4352 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4353 | } |
| 4354 | } |
| 4355 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4356 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 4357 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4358 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4359 | static void perf_output_read(struct perf_output_handle *handle, |
| 4360 | struct perf_event *event) |
| 4361 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4362 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4363 | u64 read_format = event->attr.read_format; |
| 4364 | |
| 4365 | /* |
| 4366 | * compute total_time_enabled, total_time_running |
| 4367 | * based on snapshot values taken when the event |
| 4368 | * was last scheduled in. |
| 4369 | * |
| 4370 | * we cannot simply called update_context_time() |
| 4371 | * because of locking issue as we are called in |
| 4372 | * NMI context |
| 4373 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4374 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4375 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4376 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4377 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4378 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4379 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4380 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | void perf_output_sample(struct perf_output_handle *handle, |
| 4384 | struct perf_event_header *header, |
| 4385 | struct perf_sample_data *data, |
| 4386 | struct perf_event *event) |
| 4387 | { |
| 4388 | u64 sample_type = data->type; |
| 4389 | |
| 4390 | perf_output_put(handle, *header); |
| 4391 | |
| 4392 | if (sample_type & PERF_SAMPLE_IP) |
| 4393 | perf_output_put(handle, data->ip); |
| 4394 | |
| 4395 | if (sample_type & PERF_SAMPLE_TID) |
| 4396 | perf_output_put(handle, data->tid_entry); |
| 4397 | |
| 4398 | if (sample_type & PERF_SAMPLE_TIME) |
| 4399 | perf_output_put(handle, data->time); |
| 4400 | |
| 4401 | if (sample_type & PERF_SAMPLE_ADDR) |
| 4402 | perf_output_put(handle, data->addr); |
| 4403 | |
| 4404 | if (sample_type & PERF_SAMPLE_ID) |
| 4405 | perf_output_put(handle, data->id); |
| 4406 | |
| 4407 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4408 | perf_output_put(handle, data->stream_id); |
| 4409 | |
| 4410 | if (sample_type & PERF_SAMPLE_CPU) |
| 4411 | perf_output_put(handle, data->cpu_entry); |
| 4412 | |
| 4413 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 4414 | perf_output_put(handle, data->period); |
| 4415 | |
| 4416 | if (sample_type & PERF_SAMPLE_READ) |
| 4417 | perf_output_read(handle, event); |
| 4418 | |
| 4419 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4420 | if (data->callchain) { |
| 4421 | int size = 1; |
| 4422 | |
| 4423 | if (data->callchain) |
| 4424 | size += data->callchain->nr; |
| 4425 | |
| 4426 | size *= sizeof(u64); |
| 4427 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4428 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4429 | } else { |
| 4430 | u64 nr = 0; |
| 4431 | perf_output_put(handle, nr); |
| 4432 | } |
| 4433 | } |
| 4434 | |
| 4435 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4436 | if (data->raw) { |
| 4437 | perf_output_put(handle, data->raw->size); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4438 | __output_copy(handle, data->raw->data, |
| 4439 | data->raw->size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4440 | } else { |
| 4441 | struct { |
| 4442 | u32 size; |
| 4443 | u32 data; |
| 4444 | } raw = { |
| 4445 | .size = sizeof(u32), |
| 4446 | .data = 0, |
| 4447 | }; |
| 4448 | perf_output_put(handle, raw); |
| 4449 | } |
| 4450 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4451 | |
| 4452 | if (!event->attr.watermark) { |
| 4453 | int wakeup_events = event->attr.wakeup_events; |
| 4454 | |
| 4455 | if (wakeup_events) { |
| 4456 | struct ring_buffer *rb = handle->rb; |
| 4457 | int events = local_inc_return(&rb->events); |
| 4458 | |
| 4459 | if (events >= wakeup_events) { |
| 4460 | local_sub(wakeup_events, &rb->events); |
| 4461 | local_inc(&rb->wakeup); |
| 4462 | } |
| 4463 | } |
| 4464 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4465 | |
| 4466 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4467 | if (data->br_stack) { |
| 4468 | size_t size; |
| 4469 | |
| 4470 | size = data->br_stack->nr |
| 4471 | * sizeof(struct perf_branch_entry); |
| 4472 | |
| 4473 | perf_output_put(handle, data->br_stack->nr); |
| 4474 | perf_output_copy(handle, data->br_stack->entries, size); |
| 4475 | } else { |
| 4476 | /* |
| 4477 | * we always store at least the value of nr |
| 4478 | */ |
| 4479 | u64 nr = 0; |
| 4480 | perf_output_put(handle, nr); |
| 4481 | } |
| 4482 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4483 | |
| 4484 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 4485 | u64 abi = data->regs_user.abi; |
| 4486 | |
| 4487 | /* |
| 4488 | * If there are no regs to dump, notice it through |
| 4489 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 4490 | */ |
| 4491 | perf_output_put(handle, abi); |
| 4492 | |
| 4493 | if (abi) { |
| 4494 | u64 mask = event->attr.sample_regs_user; |
| 4495 | perf_output_sample_regs(handle, |
| 4496 | data->regs_user.regs, |
| 4497 | mask); |
| 4498 | } |
| 4499 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4500 | |
| 4501 | if (sample_type & PERF_SAMPLE_STACK_USER) |
| 4502 | perf_output_sample_ustack(handle, |
| 4503 | data->stack_user_size, |
| 4504 | data->regs_user.regs); |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 4505 | |
| 4506 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 4507 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 4508 | |
| 4509 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 4510 | perf_output_put(handle, data->data_src.val); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | void perf_prepare_sample(struct perf_event_header *header, |
| 4514 | struct perf_sample_data *data, |
| 4515 | struct perf_event *event, |
| 4516 | struct pt_regs *regs) |
| 4517 | { |
| 4518 | u64 sample_type = event->attr.sample_type; |
| 4519 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4520 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4521 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4522 | |
| 4523 | header->misc = 0; |
| 4524 | header->misc |= perf_misc_flags(regs); |
| 4525 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4526 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4527 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4528 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4529 | data->ip = perf_instruction_pointer(regs); |
| 4530 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4531 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4532 | int size = 1; |
| 4533 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 4534 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4535 | |
| 4536 | if (data->callchain) |
| 4537 | size += data->callchain->nr; |
| 4538 | |
| 4539 | header->size += size * sizeof(u64); |
| 4540 | } |
| 4541 | |
| 4542 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4543 | int size = sizeof(u32); |
| 4544 | |
| 4545 | if (data->raw) |
| 4546 | size += data->raw->size; |
| 4547 | else |
| 4548 | size += sizeof(u32); |
| 4549 | |
| 4550 | WARN_ON_ONCE(size & (sizeof(u64)-1)); |
| 4551 | header->size += size; |
| 4552 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4553 | |
| 4554 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4555 | int size = sizeof(u64); /* nr */ |
| 4556 | if (data->br_stack) { |
| 4557 | size += data->br_stack->nr |
| 4558 | * sizeof(struct perf_branch_entry); |
| 4559 | } |
| 4560 | header->size += size; |
| 4561 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4562 | |
| 4563 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 4564 | /* regs dump ABI info */ |
| 4565 | int size = sizeof(u64); |
| 4566 | |
| 4567 | perf_sample_regs_user(&data->regs_user, regs); |
| 4568 | |
| 4569 | if (data->regs_user.regs) { |
| 4570 | u64 mask = event->attr.sample_regs_user; |
| 4571 | size += hweight64(mask) * sizeof(u64); |
| 4572 | } |
| 4573 | |
| 4574 | header->size += size; |
| 4575 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4576 | |
| 4577 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 4578 | /* |
| 4579 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 4580 | * processed as the last one or have additional check added |
| 4581 | * in case new sample type is added, because we could eat |
| 4582 | * up the rest of the sample size. |
| 4583 | */ |
| 4584 | struct perf_regs_user *uregs = &data->regs_user; |
| 4585 | u16 stack_size = event->attr.sample_stack_user; |
| 4586 | u16 size = sizeof(u64); |
| 4587 | |
| 4588 | if (!uregs->abi) |
| 4589 | perf_sample_regs_user(uregs, regs); |
| 4590 | |
| 4591 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
| 4592 | uregs->regs); |
| 4593 | |
| 4594 | /* |
| 4595 | * If there is something to dump, add space for the dump |
| 4596 | * itself and for the field that tells the dynamic size, |
| 4597 | * which is how many have been actually dumped. |
| 4598 | */ |
| 4599 | if (stack_size) |
| 4600 | size += sizeof(u64) + stack_size; |
| 4601 | |
| 4602 | data->stack_user_size = stack_size; |
| 4603 | header->size += size; |
| 4604 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4605 | } |
| 4606 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 4607 | static void perf_event_output(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4608 | struct perf_sample_data *data, |
| 4609 | struct pt_regs *regs) |
| 4610 | { |
| 4611 | struct perf_output_handle handle; |
| 4612 | struct perf_event_header header; |
| 4613 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4614 | /* protect the callchain buffers */ |
| 4615 | rcu_read_lock(); |
| 4616 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4617 | perf_prepare_sample(&header, data, event, regs); |
| 4618 | |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4619 | if (perf_output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4620 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4621 | |
| 4622 | perf_output_sample(&handle, &header, data, event); |
| 4623 | |
| 4624 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4625 | |
| 4626 | exit: |
| 4627 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | /* |
| 4631 | * read event_id |
| 4632 | */ |
| 4633 | |
| 4634 | struct perf_read_event { |
| 4635 | struct perf_event_header header; |
| 4636 | |
| 4637 | u32 pid; |
| 4638 | u32 tid; |
| 4639 | }; |
| 4640 | |
| 4641 | static void |
| 4642 | perf_event_read_event(struct perf_event *event, |
| 4643 | struct task_struct *task) |
| 4644 | { |
| 4645 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4646 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4647 | struct perf_read_event read_event = { |
| 4648 | .header = { |
| 4649 | .type = PERF_RECORD_READ, |
| 4650 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4651 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4652 | }, |
| 4653 | .pid = perf_event_pid(event, task), |
| 4654 | .tid = perf_event_tid(event, task), |
| 4655 | }; |
| 4656 | int ret; |
| 4657 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4658 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4659 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4660 | if (ret) |
| 4661 | return; |
| 4662 | |
| 4663 | perf_output_put(&handle, read_event); |
| 4664 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4665 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4666 | |
| 4667 | perf_output_end(&handle); |
| 4668 | } |
| 4669 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4670 | typedef int (perf_event_aux_match_cb)(struct perf_event *event, void *data); |
| 4671 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
| 4672 | |
| 4673 | static void |
| 4674 | perf_event_aux_ctx(struct perf_event_context *ctx, |
| 4675 | perf_event_aux_match_cb match, |
| 4676 | perf_event_aux_output_cb output, |
| 4677 | void *data) |
| 4678 | { |
| 4679 | struct perf_event *event; |
| 4680 | |
| 4681 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 4682 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 4683 | continue; |
| 4684 | if (!event_filter_match(event)) |
| 4685 | continue; |
| 4686 | if (match(event, data)) |
| 4687 | output(event, data); |
| 4688 | } |
| 4689 | } |
| 4690 | |
| 4691 | static void |
| 4692 | perf_event_aux(perf_event_aux_match_cb match, |
| 4693 | perf_event_aux_output_cb output, |
| 4694 | void *data, |
| 4695 | struct perf_event_context *task_ctx) |
| 4696 | { |
| 4697 | struct perf_cpu_context *cpuctx; |
| 4698 | struct perf_event_context *ctx; |
| 4699 | struct pmu *pmu; |
| 4700 | int ctxn; |
| 4701 | |
| 4702 | rcu_read_lock(); |
| 4703 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 4704 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 4705 | if (cpuctx->unique_pmu != pmu) |
| 4706 | goto next; |
| 4707 | perf_event_aux_ctx(&cpuctx->ctx, match, output, data); |
| 4708 | if (task_ctx) |
| 4709 | goto next; |
| 4710 | ctxn = pmu->task_ctx_nr; |
| 4711 | if (ctxn < 0) |
| 4712 | goto next; |
| 4713 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 4714 | if (ctx) |
| 4715 | perf_event_aux_ctx(ctx, match, output, data); |
| 4716 | next: |
| 4717 | put_cpu_ptr(pmu->pmu_cpu_context); |
| 4718 | } |
| 4719 | |
| 4720 | if (task_ctx) { |
| 4721 | preempt_disable(); |
| 4722 | perf_event_aux_ctx(task_ctx, match, output, data); |
| 4723 | preempt_enable(); |
| 4724 | } |
| 4725 | rcu_read_unlock(); |
| 4726 | } |
| 4727 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4728 | /* |
| 4729 | * task tracking -- fork/exit |
| 4730 | * |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4731 | * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4732 | */ |
| 4733 | |
| 4734 | struct perf_task_event { |
| 4735 | struct task_struct *task; |
| 4736 | struct perf_event_context *task_ctx; |
| 4737 | |
| 4738 | struct { |
| 4739 | struct perf_event_header header; |
| 4740 | |
| 4741 | u32 pid; |
| 4742 | u32 ppid; |
| 4743 | u32 tid; |
| 4744 | u32 ptid; |
| 4745 | u64 time; |
| 4746 | } event_id; |
| 4747 | }; |
| 4748 | |
| 4749 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4750 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4751 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4752 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4753 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4754 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4755 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4756 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 4757 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4758 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4759 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4760 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4761 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 4762 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4763 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4764 | |
| 4765 | task_event->event_id.pid = perf_event_pid(event, task); |
| 4766 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 4767 | |
| 4768 | task_event->event_id.tid = perf_event_tid(event, task); |
| 4769 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 4770 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4771 | perf_output_put(&handle, task_event->event_id); |
| 4772 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4773 | perf_event__output_id_sample(event, &handle, &sample); |
| 4774 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4775 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4776 | out: |
| 4777 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4778 | } |
| 4779 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4780 | static int perf_event_task_match(struct perf_event *event, |
| 4781 | void *data __maybe_unused) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4782 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4783 | return event->attr.comm || event->attr.mmap || |
| 4784 | event->attr.mmap_data || event->attr.task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4785 | } |
| 4786 | |
| 4787 | static void perf_event_task(struct task_struct *task, |
| 4788 | struct perf_event_context *task_ctx, |
| 4789 | int new) |
| 4790 | { |
| 4791 | struct perf_task_event task_event; |
| 4792 | |
| 4793 | if (!atomic_read(&nr_comm_events) && |
| 4794 | !atomic_read(&nr_mmap_events) && |
| 4795 | !atomic_read(&nr_task_events)) |
| 4796 | return; |
| 4797 | |
| 4798 | task_event = (struct perf_task_event){ |
| 4799 | .task = task, |
| 4800 | .task_ctx = task_ctx, |
| 4801 | .event_id = { |
| 4802 | .header = { |
| 4803 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 4804 | .misc = 0, |
| 4805 | .size = sizeof(task_event.event_id), |
| 4806 | }, |
| 4807 | /* .pid */ |
| 4808 | /* .ppid */ |
| 4809 | /* .tid */ |
| 4810 | /* .ptid */ |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 4811 | .time = perf_clock(), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4812 | }, |
| 4813 | }; |
| 4814 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4815 | perf_event_aux(perf_event_task_match, |
| 4816 | perf_event_task_output, |
| 4817 | &task_event, |
| 4818 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | void perf_event_fork(struct task_struct *task) |
| 4822 | { |
| 4823 | perf_event_task(task, NULL, 1); |
| 4824 | } |
| 4825 | |
| 4826 | /* |
| 4827 | * comm tracking |
| 4828 | */ |
| 4829 | |
| 4830 | struct perf_comm_event { |
| 4831 | struct task_struct *task; |
| 4832 | char *comm; |
| 4833 | int comm_size; |
| 4834 | |
| 4835 | struct { |
| 4836 | struct perf_event_header header; |
| 4837 | |
| 4838 | u32 pid; |
| 4839 | u32 tid; |
| 4840 | } event_id; |
| 4841 | }; |
| 4842 | |
| 4843 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4844 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4845 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4846 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4847 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4848 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4849 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4850 | int ret; |
| 4851 | |
| 4852 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 4853 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4854 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4855 | |
| 4856 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4857 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4858 | |
| 4859 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 4860 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 4861 | |
| 4862 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4863 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4864 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4865 | |
| 4866 | perf_event__output_id_sample(event, &handle, &sample); |
| 4867 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4868 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4869 | out: |
| 4870 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4871 | } |
| 4872 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4873 | static int perf_event_comm_match(struct perf_event *event, |
| 4874 | void *data __maybe_unused) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4875 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4876 | return event->attr.comm; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 4880 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4881 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4882 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4883 | |
| 4884 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 4885 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4886 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 4887 | |
| 4888 | comm_event->comm = comm; |
| 4889 | comm_event->comm_size = size; |
| 4890 | |
| 4891 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4892 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4893 | perf_event_aux(perf_event_comm_match, |
| 4894 | perf_event_comm_output, |
| 4895 | comm_event, |
| 4896 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | void perf_event_comm(struct task_struct *task) |
| 4900 | { |
| 4901 | struct perf_comm_event comm_event; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4902 | struct perf_event_context *ctx; |
| 4903 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4904 | |
Paul E. McKenney | c79aa0d9 | 2013-04-19 12:01:24 -0700 | [diff] [blame] | 4905 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 4906 | for_each_task_context_nr(ctxn) { |
| 4907 | ctx = task->perf_event_ctxp[ctxn]; |
| 4908 | if (!ctx) |
| 4909 | continue; |
| 4910 | |
| 4911 | perf_event_enable_on_exec(ctx); |
| 4912 | } |
Paul E. McKenney | c79aa0d9 | 2013-04-19 12:01:24 -0700 | [diff] [blame] | 4913 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4914 | |
| 4915 | if (!atomic_read(&nr_comm_events)) |
| 4916 | return; |
| 4917 | |
| 4918 | comm_event = (struct perf_comm_event){ |
| 4919 | .task = task, |
| 4920 | /* .comm */ |
| 4921 | /* .comm_size */ |
| 4922 | .event_id = { |
| 4923 | .header = { |
| 4924 | .type = PERF_RECORD_COMM, |
| 4925 | .misc = 0, |
| 4926 | /* .size */ |
| 4927 | }, |
| 4928 | /* .pid */ |
| 4929 | /* .tid */ |
| 4930 | }, |
| 4931 | }; |
| 4932 | |
| 4933 | perf_event_comm_event(&comm_event); |
| 4934 | } |
| 4935 | |
| 4936 | /* |
| 4937 | * mmap tracking |
| 4938 | */ |
| 4939 | |
| 4940 | struct perf_mmap_event { |
| 4941 | struct vm_area_struct *vma; |
| 4942 | |
| 4943 | const char *file_name; |
| 4944 | int file_size; |
| 4945 | |
| 4946 | struct { |
| 4947 | struct perf_event_header header; |
| 4948 | |
| 4949 | u32 pid; |
| 4950 | u32 tid; |
| 4951 | u64 start; |
| 4952 | u64 len; |
| 4953 | u64 pgoff; |
| 4954 | } event_id; |
| 4955 | }; |
| 4956 | |
| 4957 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4958 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4959 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4960 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4961 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4962 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4963 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4964 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4965 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4966 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 4967 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4968 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4969 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4970 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4971 | |
| 4972 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 4973 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 4974 | |
| 4975 | perf_output_put(&handle, mmap_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4976 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4977 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4978 | |
| 4979 | perf_event__output_id_sample(event, &handle, &sample); |
| 4980 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4981 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4982 | out: |
| 4983 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | static int perf_event_mmap_match(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4987 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4988 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4989 | struct perf_mmap_event *mmap_event = data; |
| 4990 | struct vm_area_struct *vma = mmap_event->vma; |
| 4991 | int executable = vma->vm_flags & VM_EXEC; |
Peter Zijlstra | 22e1908 | 2010-01-18 09:12:32 +0100 | [diff] [blame] | 4992 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4993 | return (!executable && event->attr.mmap_data) || |
| 4994 | (executable && event->attr.mmap); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 4998 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4999 | struct vm_area_struct *vma = mmap_event->vma; |
| 5000 | struct file *file = vma->vm_file; |
| 5001 | unsigned int size; |
| 5002 | char tmp[16]; |
| 5003 | char *buf = NULL; |
| 5004 | const char *name; |
| 5005 | |
| 5006 | memset(tmp, 0, sizeof(tmp)); |
| 5007 | |
| 5008 | if (file) { |
| 5009 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5010 | * d_path works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5011 | * need to add enough zero bytes after the string to handle |
| 5012 | * the 64bit alignment we do later. |
| 5013 | */ |
| 5014 | buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL); |
| 5015 | if (!buf) { |
| 5016 | name = strncpy(tmp, "//enomem", sizeof(tmp)); |
| 5017 | goto got_name; |
| 5018 | } |
| 5019 | name = d_path(&file->f_path, buf, PATH_MAX); |
| 5020 | if (IS_ERR(name)) { |
| 5021 | name = strncpy(tmp, "//toolong", sizeof(tmp)); |
| 5022 | goto got_name; |
| 5023 | } |
| 5024 | } else { |
| 5025 | if (arch_vma_name(mmap_event->vma)) { |
| 5026 | name = strncpy(tmp, arch_vma_name(mmap_event->vma), |
Chen Gang | c97847d | 2013-04-08 11:48:27 +0800 | [diff] [blame] | 5027 | sizeof(tmp) - 1); |
| 5028 | tmp[sizeof(tmp) - 1] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5029 | goto got_name; |
| 5030 | } |
| 5031 | |
| 5032 | if (!vma->vm_mm) { |
| 5033 | name = strncpy(tmp, "[vdso]", sizeof(tmp)); |
| 5034 | goto got_name; |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 5035 | } else if (vma->vm_start <= vma->vm_mm->start_brk && |
| 5036 | vma->vm_end >= vma->vm_mm->brk) { |
| 5037 | name = strncpy(tmp, "[heap]", sizeof(tmp)); |
| 5038 | goto got_name; |
| 5039 | } else if (vma->vm_start <= vma->vm_mm->start_stack && |
| 5040 | vma->vm_end >= vma->vm_mm->start_stack) { |
| 5041 | name = strncpy(tmp, "[stack]", sizeof(tmp)); |
| 5042 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | name = strncpy(tmp, "//anon", sizeof(tmp)); |
| 5046 | goto got_name; |
| 5047 | } |
| 5048 | |
| 5049 | got_name: |
| 5050 | size = ALIGN(strlen(name)+1, sizeof(u64)); |
| 5051 | |
| 5052 | mmap_event->file_name = name; |
| 5053 | mmap_event->file_size = size; |
| 5054 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 5055 | if (!(vma->vm_flags & VM_EXEC)) |
| 5056 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 5057 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5058 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 5059 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5060 | perf_event_aux(perf_event_mmap_match, |
| 5061 | perf_event_mmap_output, |
| 5062 | mmap_event, |
| 5063 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5064 | |
| 5065 | kfree(buf); |
| 5066 | } |
| 5067 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 5068 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5069 | { |
| 5070 | struct perf_mmap_event mmap_event; |
| 5071 | |
| 5072 | if (!atomic_read(&nr_mmap_events)) |
| 5073 | return; |
| 5074 | |
| 5075 | mmap_event = (struct perf_mmap_event){ |
| 5076 | .vma = vma, |
| 5077 | /* .file_name */ |
| 5078 | /* .file_size */ |
| 5079 | .event_id = { |
| 5080 | .header = { |
| 5081 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5082 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5083 | /* .size */ |
| 5084 | }, |
| 5085 | /* .pid */ |
| 5086 | /* .tid */ |
| 5087 | .start = vma->vm_start, |
| 5088 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 5089 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5090 | }, |
| 5091 | }; |
| 5092 | |
| 5093 | perf_event_mmap_event(&mmap_event); |
| 5094 | } |
| 5095 | |
| 5096 | /* |
| 5097 | * IRQ throttle logging |
| 5098 | */ |
| 5099 | |
| 5100 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 5101 | { |
| 5102 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5103 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5104 | int ret; |
| 5105 | |
| 5106 | struct { |
| 5107 | struct perf_event_header header; |
| 5108 | u64 time; |
| 5109 | u64 id; |
| 5110 | u64 stream_id; |
| 5111 | } throttle_event = { |
| 5112 | .header = { |
| 5113 | .type = PERF_RECORD_THROTTLE, |
| 5114 | .misc = 0, |
| 5115 | .size = sizeof(throttle_event), |
| 5116 | }, |
| 5117 | .time = perf_clock(), |
| 5118 | .id = primary_event_id(event), |
| 5119 | .stream_id = event->id, |
| 5120 | }; |
| 5121 | |
| 5122 | if (enable) |
| 5123 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 5124 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5125 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 5126 | |
| 5127 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5128 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5129 | if (ret) |
| 5130 | return; |
| 5131 | |
| 5132 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5133 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5134 | perf_output_end(&handle); |
| 5135 | } |
| 5136 | |
| 5137 | /* |
| 5138 | * Generic event overflow handling, sampling. |
| 5139 | */ |
| 5140 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5141 | static int __perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5142 | int throttle, struct perf_sample_data *data, |
| 5143 | struct pt_regs *regs) |
| 5144 | { |
| 5145 | int events = atomic_read(&event->event_limit); |
| 5146 | struct hw_perf_event *hwc = &event->hw; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5147 | u64 seq; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5148 | int ret = 0; |
| 5149 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 5150 | /* |
| 5151 | * Non-sampling counters might still use the PMI to fold short |
| 5152 | * hardware counters, ignore those. |
| 5153 | */ |
| 5154 | if (unlikely(!is_sampling_event(event))) |
| 5155 | return 0; |
| 5156 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5157 | seq = __this_cpu_read(perf_throttled_seq); |
| 5158 | if (seq != hwc->interrupts_seq) { |
| 5159 | hwc->interrupts_seq = seq; |
| 5160 | hwc->interrupts = 1; |
| 5161 | } else { |
| 5162 | hwc->interrupts++; |
| 5163 | if (unlikely(throttle |
| 5164 | && hwc->interrupts >= max_samples_per_tick)) { |
| 5165 | __this_cpu_inc(perf_throttled_count); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 5166 | hwc->interrupts = MAX_INTERRUPTS; |
| 5167 | perf_log_throttle(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5168 | ret = 1; |
| 5169 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5170 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5171 | |
| 5172 | if (event->attr.freq) { |
| 5173 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5174 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5175 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5176 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5177 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5178 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 5179 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | /* |
| 5183 | * XXX event_limit might not quite work as expected on inherited |
| 5184 | * events |
| 5185 | */ |
| 5186 | |
| 5187 | event->pending_kill = POLL_IN; |
| 5188 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 5189 | ret = 1; |
| 5190 | event->pending_kill = POLL_HUP; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5191 | event->pending_disable = 1; |
| 5192 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5193 | } |
| 5194 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5195 | if (event->overflow_handler) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5196 | event->overflow_handler(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5197 | else |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5198 | perf_event_output(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5199 | |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5200 | if (event->fasync && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5201 | event->pending_wakeup = 1; |
| 5202 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5203 | } |
| 5204 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5205 | return ret; |
| 5206 | } |
| 5207 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5208 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5209 | struct perf_sample_data *data, |
| 5210 | struct pt_regs *regs) |
| 5211 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5212 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5213 | } |
| 5214 | |
| 5215 | /* |
| 5216 | * Generic software event infrastructure |
| 5217 | */ |
| 5218 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5219 | struct swevent_htable { |
| 5220 | struct swevent_hlist *swevent_hlist; |
| 5221 | struct mutex hlist_mutex; |
| 5222 | int hlist_refcount; |
| 5223 | |
| 5224 | /* Recursion avoidance in each contexts */ |
| 5225 | int recursion[PERF_NR_CONTEXTS]; |
| 5226 | }; |
| 5227 | |
| 5228 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 5229 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5230 | /* |
| 5231 | * We directly increment event->count and keep a second value in |
| 5232 | * event->hw.period_left to count intervals. This period event |
| 5233 | * is kept in the range [-sample_period, 0] so that we can use the |
| 5234 | * sign as trigger. |
| 5235 | */ |
| 5236 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 5237 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5238 | { |
| 5239 | struct hw_perf_event *hwc = &event->hw; |
| 5240 | u64 period = hwc->last_period; |
| 5241 | u64 nr, offset; |
| 5242 | s64 old, val; |
| 5243 | |
| 5244 | hwc->last_period = hwc->sample_period; |
| 5245 | |
| 5246 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5247 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5248 | if (val < 0) |
| 5249 | return 0; |
| 5250 | |
| 5251 | nr = div64_u64(period + val, period); |
| 5252 | offset = nr * period; |
| 5253 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5254 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5255 | goto again; |
| 5256 | |
| 5257 | return nr; |
| 5258 | } |
| 5259 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5260 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5261 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5262 | struct pt_regs *regs) |
| 5263 | { |
| 5264 | struct hw_perf_event *hwc = &event->hw; |
| 5265 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5266 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5267 | if (!overflow) |
| 5268 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5269 | |
| 5270 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 5271 | return; |
| 5272 | |
| 5273 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5274 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5275 | data, regs)) { |
| 5276 | /* |
| 5277 | * We inhibit the overflow from happening when |
| 5278 | * hwc->interrupts == MAX_INTERRUPTS. |
| 5279 | */ |
| 5280 | break; |
| 5281 | } |
| 5282 | throttle = 1; |
| 5283 | } |
| 5284 | } |
| 5285 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5286 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5287 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5288 | struct pt_regs *regs) |
| 5289 | { |
| 5290 | struct hw_perf_event *hwc = &event->hw; |
| 5291 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5292 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5293 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5294 | if (!regs) |
| 5295 | return; |
| 5296 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5297 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5298 | return; |
| 5299 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 5300 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 5301 | data->period = nr; |
| 5302 | return perf_swevent_overflow(event, 1, data, regs); |
| 5303 | } else |
| 5304 | data->period = event->hw.last_period; |
| 5305 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5306 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5307 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5308 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5309 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5310 | return; |
| 5311 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5312 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5313 | } |
| 5314 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5315 | static int perf_exclude_event(struct perf_event *event, |
| 5316 | struct pt_regs *regs) |
| 5317 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5318 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 5319 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5320 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5321 | if (regs) { |
| 5322 | if (event->attr.exclude_user && user_mode(regs)) |
| 5323 | return 1; |
| 5324 | |
| 5325 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 5326 | return 1; |
| 5327 | } |
| 5328 | |
| 5329 | return 0; |
| 5330 | } |
| 5331 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5332 | static int perf_swevent_match(struct perf_event *event, |
| 5333 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5334 | u32 event_id, |
| 5335 | struct perf_sample_data *data, |
| 5336 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5337 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5338 | if (event->attr.type != type) |
| 5339 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5340 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5341 | if (event->attr.config != event_id) |
| 5342 | return 0; |
| 5343 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5344 | if (perf_exclude_event(event, regs)) |
| 5345 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5346 | |
| 5347 | return 1; |
| 5348 | } |
| 5349 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5350 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5351 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5352 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5353 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5354 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 5355 | } |
| 5356 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5357 | static inline struct hlist_head * |
| 5358 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5359 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5360 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5361 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5362 | return &hlist->heads[hash]; |
| 5363 | } |
| 5364 | |
| 5365 | /* For the read side: events when they trigger */ |
| 5366 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5367 | 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] | 5368 | { |
| 5369 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5370 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5371 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5372 | if (!hlist) |
| 5373 | return NULL; |
| 5374 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5375 | return __find_swevent_head(hlist, type, event_id); |
| 5376 | } |
| 5377 | |
| 5378 | /* For the event head insertion and removal in the hlist */ |
| 5379 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5380 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5381 | { |
| 5382 | struct swevent_hlist *hlist; |
| 5383 | u32 event_id = event->attr.config; |
| 5384 | u64 type = event->attr.type; |
| 5385 | |
| 5386 | /* |
| 5387 | * Event scheduling is always serialized against hlist allocation |
| 5388 | * and release. Which makes the protected version suitable here. |
| 5389 | * The context lock guarantees that. |
| 5390 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5391 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5392 | lockdep_is_held(&event->ctx->lock)); |
| 5393 | if (!hlist) |
| 5394 | return NULL; |
| 5395 | |
| 5396 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5400 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5401 | struct perf_sample_data *data, |
| 5402 | struct pt_regs *regs) |
| 5403 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5404 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5405 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5406 | struct hlist_head *head; |
| 5407 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5408 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5409 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5410 | if (!head) |
| 5411 | goto end; |
| 5412 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5413 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5414 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5415 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5416 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5417 | end: |
| 5418 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5419 | } |
| 5420 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5421 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5422 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5423 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5424 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5425 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5426 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 5427 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5428 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 5429 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5430 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5431 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5432 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5433 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5434 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5435 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5436 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5437 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5438 | struct perf_sample_data data; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5439 | int rctx; |
| 5440 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5441 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5442 | rctx = perf_swevent_get_recursion_context(); |
| 5443 | if (rctx < 0) |
| 5444 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5445 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 5446 | perf_sample_data_init(&data, addr, 0); |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5447 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5448 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5449 | |
| 5450 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5451 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | static void perf_swevent_read(struct perf_event *event) |
| 5455 | { |
| 5456 | } |
| 5457 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5458 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5459 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5460 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5461 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5462 | struct hlist_head *head; |
| 5463 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5464 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5465 | hwc->last_period = hwc->sample_period; |
| 5466 | perf_swevent_set_period(event); |
| 5467 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5468 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5469 | hwc->state = !(flags & PERF_EF_START); |
| 5470 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5471 | head = find_swevent_head(swhash, event); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5472 | if (WARN_ON_ONCE(!head)) |
| 5473 | return -EINVAL; |
| 5474 | |
| 5475 | hlist_add_head_rcu(&event->hlist_entry, head); |
| 5476 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5477 | return 0; |
| 5478 | } |
| 5479 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5480 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5481 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5482 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5483 | } |
| 5484 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5485 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5486 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5487 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5488 | } |
| 5489 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5490 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5491 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5492 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5493 | } |
| 5494 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5495 | /* Deref the hlist from the update side */ |
| 5496 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5497 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5498 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5499 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 5500 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5501 | } |
| 5502 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5503 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5504 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5505 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5506 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5507 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5508 | return; |
| 5509 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5510 | rcu_assign_pointer(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 5511 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5512 | } |
| 5513 | |
| 5514 | static void swevent_hlist_put_cpu(struct perf_event *event, int cpu) |
| 5515 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5516 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5517 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5518 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5519 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5520 | if (!--swhash->hlist_refcount) |
| 5521 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5522 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5523 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | static void swevent_hlist_put(struct perf_event *event) |
| 5527 | { |
| 5528 | int cpu; |
| 5529 | |
| 5530 | if (event->cpu != -1) { |
| 5531 | swevent_hlist_put_cpu(event, event->cpu); |
| 5532 | return; |
| 5533 | } |
| 5534 | |
| 5535 | for_each_possible_cpu(cpu) |
| 5536 | swevent_hlist_put_cpu(event, cpu); |
| 5537 | } |
| 5538 | |
| 5539 | static int swevent_hlist_get_cpu(struct perf_event *event, int cpu) |
| 5540 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5541 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5542 | int err = 0; |
| 5543 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5544 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5545 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5546 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5547 | struct swevent_hlist *hlist; |
| 5548 | |
| 5549 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 5550 | if (!hlist) { |
| 5551 | err = -ENOMEM; |
| 5552 | goto exit; |
| 5553 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5554 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5555 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5556 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 5557 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5558 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5559 | |
| 5560 | return err; |
| 5561 | } |
| 5562 | |
| 5563 | static int swevent_hlist_get(struct perf_event *event) |
| 5564 | { |
| 5565 | int err; |
| 5566 | int cpu, failed_cpu; |
| 5567 | |
| 5568 | if (event->cpu != -1) |
| 5569 | return swevent_hlist_get_cpu(event, event->cpu); |
| 5570 | |
| 5571 | get_online_cpus(); |
| 5572 | for_each_possible_cpu(cpu) { |
| 5573 | err = swevent_hlist_get_cpu(event, cpu); |
| 5574 | if (err) { |
| 5575 | failed_cpu = cpu; |
| 5576 | goto fail; |
| 5577 | } |
| 5578 | } |
| 5579 | put_online_cpus(); |
| 5580 | |
| 5581 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 5582 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5583 | for_each_possible_cpu(cpu) { |
| 5584 | if (cpu == failed_cpu) |
| 5585 | break; |
| 5586 | swevent_hlist_put_cpu(event, cpu); |
| 5587 | } |
| 5588 | |
| 5589 | put_online_cpus(); |
| 5590 | return err; |
| 5591 | } |
| 5592 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 5593 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5594 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5595 | static void sw_perf_event_destroy(struct perf_event *event) |
| 5596 | { |
| 5597 | u64 event_id = event->attr.config; |
| 5598 | |
| 5599 | WARN_ON(event->parent); |
| 5600 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 5601 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5602 | swevent_hlist_put(event); |
| 5603 | } |
| 5604 | |
| 5605 | static int perf_swevent_init(struct perf_event *event) |
| 5606 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 5607 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5608 | |
| 5609 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 5610 | return -ENOENT; |
| 5611 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 5612 | /* |
| 5613 | * no branch sampling for software events |
| 5614 | */ |
| 5615 | if (has_branch_stack(event)) |
| 5616 | return -EOPNOTSUPP; |
| 5617 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5618 | switch (event_id) { |
| 5619 | case PERF_COUNT_SW_CPU_CLOCK: |
| 5620 | case PERF_COUNT_SW_TASK_CLOCK: |
| 5621 | return -ENOENT; |
| 5622 | |
| 5623 | default: |
| 5624 | break; |
| 5625 | } |
| 5626 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 5627 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5628 | return -ENOENT; |
| 5629 | |
| 5630 | if (!event->parent) { |
| 5631 | int err; |
| 5632 | |
| 5633 | err = swevent_hlist_get(event); |
| 5634 | if (err) |
| 5635 | return err; |
| 5636 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 5637 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5638 | event->destroy = sw_perf_event_destroy; |
| 5639 | } |
| 5640 | |
| 5641 | return 0; |
| 5642 | } |
| 5643 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 5644 | static int perf_swevent_event_idx(struct perf_event *event) |
| 5645 | { |
| 5646 | return 0; |
| 5647 | } |
| 5648 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5649 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 5650 | .task_ctx_nr = perf_sw_context, |
| 5651 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5652 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5653 | .add = perf_swevent_add, |
| 5654 | .del = perf_swevent_del, |
| 5655 | .start = perf_swevent_start, |
| 5656 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5657 | .read = perf_swevent_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 5658 | |
| 5659 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5660 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5661 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5662 | #ifdef CONFIG_EVENT_TRACING |
| 5663 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5664 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5665 | struct perf_sample_data *data) |
| 5666 | { |
| 5667 | void *record = data->raw->data; |
| 5668 | |
| 5669 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 5670 | return 1; |
| 5671 | return 0; |
| 5672 | } |
| 5673 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5674 | static int perf_tp_event_match(struct perf_event *event, |
| 5675 | struct perf_sample_data *data, |
| 5676 | struct pt_regs *regs) |
| 5677 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 5678 | if (event->hw.state & PERF_HES_STOPPED) |
| 5679 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 5680 | /* |
| 5681 | * All tracepoints are from kernel-space. |
| 5682 | */ |
| 5683 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5684 | return 0; |
| 5685 | |
| 5686 | if (!perf_tp_filter_match(event, data)) |
| 5687 | return 0; |
| 5688 | |
| 5689 | return 1; |
| 5690 | } |
| 5691 | |
| 5692 | void perf_tp_event(u64 addr, u64 count, void *record, int entry_size, |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5693 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 5694 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5695 | { |
| 5696 | struct perf_sample_data data; |
| 5697 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5698 | |
| 5699 | struct perf_raw_record raw = { |
| 5700 | .size = entry_size, |
| 5701 | .data = record, |
| 5702 | }; |
| 5703 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 5704 | perf_sample_data_init(&data, addr, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5705 | data.raw = &raw; |
| 5706 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5707 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5708 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5709 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5710 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 5711 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5712 | /* |
| 5713 | * If we got specified a target task, also iterate its context and |
| 5714 | * deliver this event there too. |
| 5715 | */ |
| 5716 | if (task && task != current) { |
| 5717 | struct perf_event_context *ctx; |
| 5718 | struct trace_entry *entry = record; |
| 5719 | |
| 5720 | rcu_read_lock(); |
| 5721 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 5722 | if (!ctx) |
| 5723 | goto unlock; |
| 5724 | |
| 5725 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 5726 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 5727 | continue; |
| 5728 | if (event->attr.config != entry->type) |
| 5729 | continue; |
| 5730 | if (perf_tp_event_match(event, &data, regs)) |
| 5731 | perf_swevent_event(event, count, &data, regs); |
| 5732 | } |
| 5733 | unlock: |
| 5734 | rcu_read_unlock(); |
| 5735 | } |
| 5736 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 5737 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5738 | } |
| 5739 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 5740 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5741 | static void tp_perf_event_destroy(struct perf_event *event) |
| 5742 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5743 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5744 | } |
| 5745 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5746 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5747 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5748 | int err; |
| 5749 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5750 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 5751 | return -ENOENT; |
| 5752 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 5753 | /* |
| 5754 | * no branch sampling for tracepoint events |
| 5755 | */ |
| 5756 | if (has_branch_stack(event)) |
| 5757 | return -EOPNOTSUPP; |
| 5758 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5759 | err = perf_trace_init(event); |
| 5760 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5761 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5762 | |
| 5763 | event->destroy = tp_perf_event_destroy; |
| 5764 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5765 | return 0; |
| 5766 | } |
| 5767 | |
| 5768 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 5769 | .task_ctx_nr = perf_sw_context, |
| 5770 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5771 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5772 | .add = perf_trace_add, |
| 5773 | .del = perf_trace_del, |
| 5774 | .start = perf_swevent_start, |
| 5775 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5776 | .read = perf_swevent_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 5777 | |
| 5778 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5779 | }; |
| 5780 | |
| 5781 | static inline void perf_tp_register(void) |
| 5782 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 5783 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5784 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5785 | |
| 5786 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 5787 | { |
| 5788 | char *filter_str; |
| 5789 | int ret; |
| 5790 | |
| 5791 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 5792 | return -EINVAL; |
| 5793 | |
| 5794 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 5795 | if (IS_ERR(filter_str)) |
| 5796 | return PTR_ERR(filter_str); |
| 5797 | |
| 5798 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); |
| 5799 | |
| 5800 | kfree(filter_str); |
| 5801 | return ret; |
| 5802 | } |
| 5803 | |
| 5804 | static void perf_event_free_filter(struct perf_event *event) |
| 5805 | { |
| 5806 | ftrace_profile_free_filter(event); |
| 5807 | } |
| 5808 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5809 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5810 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5811 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5812 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5813 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5814 | |
| 5815 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 5816 | { |
| 5817 | return -ENOENT; |
| 5818 | } |
| 5819 | |
| 5820 | static void perf_event_free_filter(struct perf_event *event) |
| 5821 | { |
| 5822 | } |
| 5823 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 5824 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5825 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 5826 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5827 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 5828 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5829 | struct perf_sample_data sample; |
| 5830 | struct pt_regs *regs = data; |
| 5831 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 5832 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5833 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5834 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5835 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 5836 | } |
| 5837 | #endif |
| 5838 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5839 | /* |
| 5840 | * hrtimer based swevent callback |
| 5841 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5842 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5843 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5844 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5845 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 5846 | struct perf_sample_data data; |
| 5847 | struct pt_regs *regs; |
| 5848 | struct perf_event *event; |
| 5849 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5850 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5851 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 5852 | |
| 5853 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 5854 | return HRTIMER_NORESTART; |
| 5855 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5856 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5857 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 5858 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5859 | regs = get_irq_regs(); |
| 5860 | |
| 5861 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 5862 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 5863 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5864 | ret = HRTIMER_NORESTART; |
| 5865 | } |
| 5866 | |
| 5867 | period = max_t(u64, 10000, event->hw.sample_period); |
| 5868 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 5869 | |
| 5870 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5871 | } |
| 5872 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5873 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5874 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5875 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 5876 | s64 period; |
| 5877 | |
| 5878 | if (!is_sampling_event(event)) |
| 5879 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5880 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 5881 | period = local64_read(&hwc->period_left); |
| 5882 | if (period) { |
| 5883 | if (period < 0) |
| 5884 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 5885 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 5886 | local64_set(&hwc->period_left, 0); |
| 5887 | } else { |
| 5888 | period = max_t(u64, 10000, hwc->sample_period); |
| 5889 | } |
| 5890 | __hrtimer_start_range_ns(&hwc->hrtimer, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5891 | ns_to_ktime(period), 0, |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 5892 | HRTIMER_MODE_REL_PINNED, 0); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 5896 | { |
| 5897 | struct hw_perf_event *hwc = &event->hw; |
| 5898 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5899 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5900 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 5901 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5902 | |
| 5903 | hrtimer_cancel(&hwc->hrtimer); |
| 5904 | } |
| 5905 | } |
| 5906 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 5907 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 5908 | { |
| 5909 | struct hw_perf_event *hwc = &event->hw; |
| 5910 | |
| 5911 | if (!is_sampling_event(event)) |
| 5912 | return; |
| 5913 | |
| 5914 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 5915 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 5916 | |
| 5917 | /* |
| 5918 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 5919 | * mapping and avoid the whole period adjust feedback stuff. |
| 5920 | */ |
| 5921 | if (event->attr.freq) { |
| 5922 | long freq = event->attr.sample_freq; |
| 5923 | |
| 5924 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 5925 | hwc->sample_period = event->attr.sample_period; |
| 5926 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 5927 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 5928 | event->attr.freq = 0; |
| 5929 | } |
| 5930 | } |
| 5931 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5932 | /* |
| 5933 | * Software event: cpu wall time clock |
| 5934 | */ |
| 5935 | |
| 5936 | static void cpu_clock_event_update(struct perf_event *event) |
| 5937 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5938 | s64 prev; |
| 5939 | u64 now; |
| 5940 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5941 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5942 | prev = local64_xchg(&event->hw.prev_count, now); |
| 5943 | local64_add(now - prev, &event->count); |
| 5944 | } |
| 5945 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5946 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5947 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5948 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5949 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5950 | } |
| 5951 | |
| 5952 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 5953 | { |
| 5954 | perf_swevent_cancel_hrtimer(event); |
| 5955 | cpu_clock_event_update(event); |
| 5956 | } |
| 5957 | |
| 5958 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 5959 | { |
| 5960 | if (flags & PERF_EF_START) |
| 5961 | cpu_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5962 | |
| 5963 | return 0; |
| 5964 | } |
| 5965 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5966 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5967 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5968 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5969 | } |
| 5970 | |
| 5971 | static void cpu_clock_event_read(struct perf_event *event) |
| 5972 | { |
| 5973 | cpu_clock_event_update(event); |
| 5974 | } |
| 5975 | |
| 5976 | static int cpu_clock_event_init(struct perf_event *event) |
| 5977 | { |
| 5978 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 5979 | return -ENOENT; |
| 5980 | |
| 5981 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 5982 | return -ENOENT; |
| 5983 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 5984 | /* |
| 5985 | * no branch sampling for software events |
| 5986 | */ |
| 5987 | if (has_branch_stack(event)) |
| 5988 | return -EOPNOTSUPP; |
| 5989 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 5990 | perf_swevent_init_hrtimer(event); |
| 5991 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5992 | return 0; |
| 5993 | } |
| 5994 | |
| 5995 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 5996 | .task_ctx_nr = perf_sw_context, |
| 5997 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5998 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5999 | .add = cpu_clock_event_add, |
| 6000 | .del = cpu_clock_event_del, |
| 6001 | .start = cpu_clock_event_start, |
| 6002 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6003 | .read = cpu_clock_event_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6004 | |
| 6005 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6006 | }; |
| 6007 | |
| 6008 | /* |
| 6009 | * Software event: task time clock |
| 6010 | */ |
| 6011 | |
| 6012 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 6013 | { |
| 6014 | u64 prev; |
| 6015 | s64 delta; |
| 6016 | |
| 6017 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6018 | delta = now - prev; |
| 6019 | local64_add(delta, &event->count); |
| 6020 | } |
| 6021 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6022 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6023 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6024 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6025 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6026 | } |
| 6027 | |
| 6028 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 6029 | { |
| 6030 | perf_swevent_cancel_hrtimer(event); |
| 6031 | task_clock_event_update(event, event->ctx->time); |
| 6032 | } |
| 6033 | |
| 6034 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 6035 | { |
| 6036 | if (flags & PERF_EF_START) |
| 6037 | task_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6038 | |
| 6039 | return 0; |
| 6040 | } |
| 6041 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6042 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6043 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6044 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | static void task_clock_event_read(struct perf_event *event) |
| 6048 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 6049 | u64 now = perf_clock(); |
| 6050 | u64 delta = now - event->ctx->timestamp; |
| 6051 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6052 | |
| 6053 | task_clock_event_update(event, time); |
| 6054 | } |
| 6055 | |
| 6056 | static int task_clock_event_init(struct perf_event *event) |
| 6057 | { |
| 6058 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6059 | return -ENOENT; |
| 6060 | |
| 6061 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 6062 | return -ENOENT; |
| 6063 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6064 | /* |
| 6065 | * no branch sampling for software events |
| 6066 | */ |
| 6067 | if (has_branch_stack(event)) |
| 6068 | return -EOPNOTSUPP; |
| 6069 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6070 | perf_swevent_init_hrtimer(event); |
| 6071 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6072 | return 0; |
| 6073 | } |
| 6074 | |
| 6075 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6076 | .task_ctx_nr = perf_sw_context, |
| 6077 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6078 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6079 | .add = task_clock_event_add, |
| 6080 | .del = task_clock_event_del, |
| 6081 | .start = task_clock_event_start, |
| 6082 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6083 | .read = task_clock_event_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6084 | |
| 6085 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6086 | }; |
| 6087 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6088 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 6089 | { |
| 6090 | } |
| 6091 | |
| 6092 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 6093 | { |
| 6094 | return 0; |
| 6095 | } |
| 6096 | |
| 6097 | static void perf_pmu_start_txn(struct pmu *pmu) |
| 6098 | { |
| 6099 | perf_pmu_disable(pmu); |
| 6100 | } |
| 6101 | |
| 6102 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 6103 | { |
| 6104 | perf_pmu_enable(pmu); |
| 6105 | return 0; |
| 6106 | } |
| 6107 | |
| 6108 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 6109 | { |
| 6110 | perf_pmu_enable(pmu); |
| 6111 | } |
| 6112 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6113 | static int perf_event_idx_default(struct perf_event *event) |
| 6114 | { |
| 6115 | return event->hw.idx + 1; |
| 6116 | } |
| 6117 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6118 | /* |
| 6119 | * Ensures all contexts with the same task_ctx_nr have the same |
| 6120 | * pmu_cpu_context too. |
| 6121 | */ |
| 6122 | static void *find_pmu_context(int ctxn) |
| 6123 | { |
| 6124 | struct pmu *pmu; |
| 6125 | |
| 6126 | if (ctxn < 0) |
| 6127 | return NULL; |
| 6128 | |
| 6129 | list_for_each_entry(pmu, &pmus, entry) { |
| 6130 | if (pmu->task_ctx_nr == ctxn) |
| 6131 | return pmu->pmu_cpu_context; |
| 6132 | } |
| 6133 | |
| 6134 | return NULL; |
| 6135 | } |
| 6136 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6137 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6138 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6139 | int cpu; |
| 6140 | |
| 6141 | for_each_possible_cpu(cpu) { |
| 6142 | struct perf_cpu_context *cpuctx; |
| 6143 | |
| 6144 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6145 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6146 | if (cpuctx->unique_pmu == old_pmu) |
| 6147 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6148 | } |
| 6149 | } |
| 6150 | |
| 6151 | static void free_pmu_context(struct pmu *pmu) |
| 6152 | { |
| 6153 | struct pmu *i; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6154 | |
| 6155 | mutex_lock(&pmus_lock); |
| 6156 | /* |
| 6157 | * Like a real lame refcount. |
| 6158 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6159 | list_for_each_entry(i, &pmus, entry) { |
| 6160 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 6161 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6162 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6163 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6164 | } |
| 6165 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6166 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6167 | out: |
| 6168 | mutex_unlock(&pmus_lock); |
| 6169 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6170 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6171 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6172 | static ssize_t |
| 6173 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 6174 | { |
| 6175 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6176 | |
| 6177 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 6178 | } |
| 6179 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6180 | static ssize_t |
| 6181 | perf_event_mux_interval_ms_show(struct device *dev, |
| 6182 | struct device_attribute *attr, |
| 6183 | char *page) |
| 6184 | { |
| 6185 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6186 | |
| 6187 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 6188 | } |
| 6189 | |
| 6190 | static ssize_t |
| 6191 | perf_event_mux_interval_ms_store(struct device *dev, |
| 6192 | struct device_attribute *attr, |
| 6193 | const char *buf, size_t count) |
| 6194 | { |
| 6195 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6196 | int timer, cpu, ret; |
| 6197 | |
| 6198 | ret = kstrtoint(buf, 0, &timer); |
| 6199 | if (ret) |
| 6200 | return ret; |
| 6201 | |
| 6202 | if (timer < 1) |
| 6203 | return -EINVAL; |
| 6204 | |
| 6205 | /* same value, noting to do */ |
| 6206 | if (timer == pmu->hrtimer_interval_ms) |
| 6207 | return count; |
| 6208 | |
| 6209 | pmu->hrtimer_interval_ms = timer; |
| 6210 | |
| 6211 | /* update all cpuctx for this PMU */ |
| 6212 | for_each_possible_cpu(cpu) { |
| 6213 | struct perf_cpu_context *cpuctx; |
| 6214 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6215 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 6216 | |
| 6217 | if (hrtimer_active(&cpuctx->hrtimer)) |
| 6218 | hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval); |
| 6219 | } |
| 6220 | |
| 6221 | return count; |
| 6222 | } |
| 6223 | |
| 6224 | #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store) |
| 6225 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6226 | static struct device_attribute pmu_dev_attrs[] = { |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6227 | __ATTR_RO(type), |
| 6228 | __ATTR_RW(perf_event_mux_interval_ms), |
| 6229 | __ATTR_NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6230 | }; |
| 6231 | |
| 6232 | static int pmu_bus_running; |
| 6233 | static struct bus_type pmu_bus = { |
| 6234 | .name = "event_source", |
| 6235 | .dev_attrs = pmu_dev_attrs, |
| 6236 | }; |
| 6237 | |
| 6238 | static void pmu_dev_release(struct device *dev) |
| 6239 | { |
| 6240 | kfree(dev); |
| 6241 | } |
| 6242 | |
| 6243 | static int pmu_dev_alloc(struct pmu *pmu) |
| 6244 | { |
| 6245 | int ret = -ENOMEM; |
| 6246 | |
| 6247 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 6248 | if (!pmu->dev) |
| 6249 | goto out; |
| 6250 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 6251 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6252 | device_initialize(pmu->dev); |
| 6253 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 6254 | if (ret) |
| 6255 | goto free_dev; |
| 6256 | |
| 6257 | dev_set_drvdata(pmu->dev, pmu); |
| 6258 | pmu->dev->bus = &pmu_bus; |
| 6259 | pmu->dev->release = pmu_dev_release; |
| 6260 | ret = device_add(pmu->dev); |
| 6261 | if (ret) |
| 6262 | goto free_dev; |
| 6263 | |
| 6264 | out: |
| 6265 | return ret; |
| 6266 | |
| 6267 | free_dev: |
| 6268 | put_device(pmu->dev); |
| 6269 | goto out; |
| 6270 | } |
| 6271 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6272 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6273 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6274 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 6275 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6276 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6277 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6278 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6279 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6280 | ret = -ENOMEM; |
| 6281 | pmu->pmu_disable_count = alloc_percpu(int); |
| 6282 | if (!pmu->pmu_disable_count) |
| 6283 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6284 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6285 | pmu->type = -1; |
| 6286 | if (!name) |
| 6287 | goto skip_type; |
| 6288 | pmu->name = name; |
| 6289 | |
| 6290 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 6291 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 6292 | if (type < 0) { |
| 6293 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6294 | goto free_pdc; |
| 6295 | } |
| 6296 | } |
| 6297 | pmu->type = type; |
| 6298 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6299 | if (pmu_bus_running) { |
| 6300 | ret = pmu_dev_alloc(pmu); |
| 6301 | if (ret) |
| 6302 | goto free_idr; |
| 6303 | } |
| 6304 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6305 | skip_type: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6306 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 6307 | if (pmu->pmu_cpu_context) |
| 6308 | goto got_cpu_context; |
| 6309 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 6310 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6311 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 6312 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6313 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6314 | |
| 6315 | for_each_possible_cpu(cpu) { |
| 6316 | struct perf_cpu_context *cpuctx; |
| 6317 | |
| 6318 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 6319 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6320 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6321 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6322 | cpuctx->ctx.type = cpu_context; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6323 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 6324 | |
| 6325 | __perf_cpu_hrtimer_init(cpuctx, cpu); |
| 6326 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 6327 | INIT_LIST_HEAD(&cpuctx->rotation_list); |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6328 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6329 | } |
| 6330 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6331 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6332 | if (!pmu->start_txn) { |
| 6333 | if (pmu->pmu_enable) { |
| 6334 | /* |
| 6335 | * If we have pmu_enable/pmu_disable calls, install |
| 6336 | * transaction stubs that use that to try and batch |
| 6337 | * hardware accesses. |
| 6338 | */ |
| 6339 | pmu->start_txn = perf_pmu_start_txn; |
| 6340 | pmu->commit_txn = perf_pmu_commit_txn; |
| 6341 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 6342 | } else { |
| 6343 | pmu->start_txn = perf_pmu_nop_void; |
| 6344 | pmu->commit_txn = perf_pmu_nop_int; |
| 6345 | pmu->cancel_txn = perf_pmu_nop_void; |
| 6346 | } |
| 6347 | } |
| 6348 | |
| 6349 | if (!pmu->pmu_enable) { |
| 6350 | pmu->pmu_enable = perf_pmu_nop_void; |
| 6351 | pmu->pmu_disable = perf_pmu_nop_void; |
| 6352 | } |
| 6353 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6354 | if (!pmu->event_idx) |
| 6355 | pmu->event_idx = perf_event_idx_default; |
| 6356 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6357 | list_add_rcu(&pmu->entry, &pmus); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6358 | ret = 0; |
| 6359 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6360 | mutex_unlock(&pmus_lock); |
| 6361 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6362 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6363 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6364 | free_dev: |
| 6365 | device_del(pmu->dev); |
| 6366 | put_device(pmu->dev); |
| 6367 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6368 | free_idr: |
| 6369 | if (pmu->type >= PERF_TYPE_MAX) |
| 6370 | idr_remove(&pmu_idr, pmu->type); |
| 6371 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6372 | free_pdc: |
| 6373 | free_percpu(pmu->pmu_disable_count); |
| 6374 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6375 | } |
| 6376 | |
| 6377 | void perf_pmu_unregister(struct pmu *pmu) |
| 6378 | { |
| 6379 | mutex_lock(&pmus_lock); |
| 6380 | list_del_rcu(&pmu->entry); |
| 6381 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6382 | |
| 6383 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6384 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 6385 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6386 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6387 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6388 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6389 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6390 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6391 | if (pmu->type >= PERF_TYPE_MAX) |
| 6392 | idr_remove(&pmu_idr, pmu->type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6393 | device_del(pmu->dev); |
| 6394 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6395 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6396 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6397 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6398 | struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6399 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6400 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6401 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6402 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6403 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6404 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6405 | |
| 6406 | rcu_read_lock(); |
| 6407 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 6408 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6409 | if (pmu) { |
Mark Rutland | 7e5b2a0 | 2011-08-11 12:31:20 +0100 | [diff] [blame] | 6410 | event->pmu = pmu; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6411 | ret = pmu->event_init(event); |
| 6412 | if (ret) |
| 6413 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6414 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6415 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6416 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6417 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Mark Rutland | 7e5b2a0 | 2011-08-11 12:31:20 +0100 | [diff] [blame] | 6418 | event->pmu = pmu; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6419 | ret = pmu->event_init(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6420 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6421 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6422 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6423 | if (ret != -ENOENT) { |
| 6424 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6425 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6426 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6427 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6428 | pmu = ERR_PTR(-ENOENT); |
| 6429 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6430 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6431 | |
| 6432 | return pmu; |
| 6433 | } |
| 6434 | |
| 6435 | /* |
| 6436 | * Allocate and initialize a event structure |
| 6437 | */ |
| 6438 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6439 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6440 | struct task_struct *task, |
| 6441 | struct perf_event *group_leader, |
| 6442 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6443 | perf_overflow_handler_t overflow_handler, |
| 6444 | void *context) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6445 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6446 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6447 | struct perf_event *event; |
| 6448 | struct hw_perf_event *hwc; |
| 6449 | long err; |
| 6450 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 6451 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 6452 | if (!task || cpu != -1) |
| 6453 | return ERR_PTR(-EINVAL); |
| 6454 | } |
| 6455 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6456 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6457 | if (!event) |
| 6458 | return ERR_PTR(-ENOMEM); |
| 6459 | |
| 6460 | /* |
| 6461 | * Single events are their own group leaders, with an |
| 6462 | * empty sibling list: |
| 6463 | */ |
| 6464 | if (!group_leader) |
| 6465 | group_leader = event; |
| 6466 | |
| 6467 | mutex_init(&event->child_mutex); |
| 6468 | INIT_LIST_HEAD(&event->child_list); |
| 6469 | |
| 6470 | INIT_LIST_HEAD(&event->group_entry); |
| 6471 | INIT_LIST_HEAD(&event->event_entry); |
| 6472 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 6473 | INIT_LIST_HEAD(&event->rb_entry); |
| 6474 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6475 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 6476 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6477 | |
| 6478 | mutex_init(&event->mmap_mutex); |
| 6479 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 6480 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6481 | event->cpu = cpu; |
| 6482 | event->attr = *attr; |
| 6483 | event->group_leader = group_leader; |
| 6484 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6485 | event->oncpu = -1; |
| 6486 | |
| 6487 | event->parent = parent_event; |
| 6488 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 6489 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6490 | event->id = atomic64_inc_return(&perf_event_id); |
| 6491 | |
| 6492 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 6493 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6494 | if (task) { |
| 6495 | event->attach_state = PERF_ATTACH_TASK; |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 6496 | |
| 6497 | if (attr->type == PERF_TYPE_TRACEPOINT) |
| 6498 | event->hw.tp_target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6499 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
| 6500 | /* |
| 6501 | * hw_breakpoint is a bit difficult here.. |
| 6502 | */ |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 6503 | else if (attr->type == PERF_TYPE_BREAKPOINT) |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6504 | event->hw.bp_target = task; |
| 6505 | #endif |
| 6506 | } |
| 6507 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6508 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 6509 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6510 | context = parent_event->overflow_handler_context; |
| 6511 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 6512 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 6513 | event->overflow_handler = overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6514 | event->overflow_handler_context = context; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 6515 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 6516 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6517 | |
| 6518 | pmu = NULL; |
| 6519 | |
| 6520 | hwc = &event->hw; |
| 6521 | hwc->sample_period = attr->sample_period; |
| 6522 | if (attr->freq && attr->sample_freq) |
| 6523 | hwc->sample_period = 1; |
| 6524 | hwc->last_period = hwc->sample_period; |
| 6525 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6526 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6527 | |
| 6528 | /* |
| 6529 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 6530 | */ |
| 6531 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
| 6532 | goto done; |
| 6533 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6534 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6535 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6536 | done: |
| 6537 | err = 0; |
| 6538 | if (!pmu) |
| 6539 | err = -EINVAL; |
| 6540 | else if (IS_ERR(pmu)) |
| 6541 | err = PTR_ERR(pmu); |
| 6542 | |
| 6543 | if (err) { |
| 6544 | if (event->ns) |
| 6545 | put_pid_ns(event->ns); |
| 6546 | kfree(event); |
| 6547 | return ERR_PTR(err); |
| 6548 | } |
| 6549 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6550 | if (!event->parent) { |
Peter Zijlstra | 82cd6de | 2010-10-14 17:57:23 +0200 | [diff] [blame] | 6551 | if (event->attach_state & PERF_ATTACH_TASK) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6552 | static_key_slow_inc(&perf_sched_events.key); |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 6553 | if (event->attr.mmap || event->attr.mmap_data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6554 | atomic_inc(&nr_mmap_events); |
| 6555 | if (event->attr.comm) |
| 6556 | atomic_inc(&nr_comm_events); |
| 6557 | if (event->attr.task) |
| 6558 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6559 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 6560 | err = get_callchain_buffers(); |
| 6561 | if (err) { |
| 6562 | free_event(event); |
| 6563 | return ERR_PTR(err); |
| 6564 | } |
| 6565 | } |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 6566 | if (has_branch_stack(event)) { |
| 6567 | static_key_slow_inc(&perf_sched_events.key); |
| 6568 | if (!(event->attach_state & PERF_ATTACH_TASK)) |
| 6569 | atomic_inc(&per_cpu(perf_branch_stack_events, |
| 6570 | event->cpu)); |
| 6571 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6572 | } |
| 6573 | |
| 6574 | return event; |
| 6575 | } |
| 6576 | |
| 6577 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 6578 | struct perf_event_attr *attr) |
| 6579 | { |
| 6580 | u32 size; |
| 6581 | int ret; |
| 6582 | |
| 6583 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 6584 | return -EFAULT; |
| 6585 | |
| 6586 | /* |
| 6587 | * zero the full structure, so that a short copy will be nice. |
| 6588 | */ |
| 6589 | memset(attr, 0, sizeof(*attr)); |
| 6590 | |
| 6591 | ret = get_user(size, &uattr->size); |
| 6592 | if (ret) |
| 6593 | return ret; |
| 6594 | |
| 6595 | if (size > PAGE_SIZE) /* silly large */ |
| 6596 | goto err_size; |
| 6597 | |
| 6598 | if (!size) /* abi compat */ |
| 6599 | size = PERF_ATTR_SIZE_VER0; |
| 6600 | |
| 6601 | if (size < PERF_ATTR_SIZE_VER0) |
| 6602 | goto err_size; |
| 6603 | |
| 6604 | /* |
| 6605 | * If we're handed a bigger struct than we know of, |
| 6606 | * ensure all the unknown bits are 0 - i.e. new |
| 6607 | * user-space does not rely on any kernel feature |
| 6608 | * extensions we dont know about yet. |
| 6609 | */ |
| 6610 | if (size > sizeof(*attr)) { |
| 6611 | unsigned char __user *addr; |
| 6612 | unsigned char __user *end; |
| 6613 | unsigned char val; |
| 6614 | |
| 6615 | addr = (void __user *)uattr + sizeof(*attr); |
| 6616 | end = (void __user *)uattr + size; |
| 6617 | |
| 6618 | for (; addr < end; addr++) { |
| 6619 | ret = get_user(val, addr); |
| 6620 | if (ret) |
| 6621 | return ret; |
| 6622 | if (val) |
| 6623 | goto err_size; |
| 6624 | } |
| 6625 | size = sizeof(*attr); |
| 6626 | } |
| 6627 | |
| 6628 | ret = copy_from_user(attr, uattr, size); |
| 6629 | if (ret) |
| 6630 | return -EFAULT; |
| 6631 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 6632 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6633 | return -EINVAL; |
| 6634 | |
| 6635 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 6636 | return -EINVAL; |
| 6637 | |
| 6638 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 6639 | return -EINVAL; |
| 6640 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 6641 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 6642 | u64 mask = attr->branch_sample_type; |
| 6643 | |
| 6644 | /* only using defined bits */ |
| 6645 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 6646 | return -EINVAL; |
| 6647 | |
| 6648 | /* at least one branch bit must be set */ |
| 6649 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 6650 | return -EINVAL; |
| 6651 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 6652 | /* propagate priv level, when not set for branch */ |
| 6653 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 6654 | |
| 6655 | /* exclude_kernel checked on syscall entry */ |
| 6656 | if (!attr->exclude_kernel) |
| 6657 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 6658 | |
| 6659 | if (!attr->exclude_user) |
| 6660 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 6661 | |
| 6662 | if (!attr->exclude_hv) |
| 6663 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 6664 | /* |
| 6665 | * adjust user setting (for HW filter setup) |
| 6666 | */ |
| 6667 | attr->branch_sample_type = mask; |
| 6668 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 6669 | /* privileged levels capture (kernel, hv): check permissions */ |
| 6670 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 6671 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 6672 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 6673 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 6674 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6675 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 6676 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6677 | if (ret) |
| 6678 | return ret; |
| 6679 | } |
| 6680 | |
| 6681 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 6682 | if (!arch_perf_have_user_stack_dump()) |
| 6683 | return -ENOSYS; |
| 6684 | |
| 6685 | /* |
| 6686 | * We have __u32 type for the size, but so far |
| 6687 | * we can only use __u16 as maximum due to the |
| 6688 | * __u16 sample size limit. |
| 6689 | */ |
| 6690 | if (attr->sample_stack_user >= USHRT_MAX) |
| 6691 | ret = -EINVAL; |
| 6692 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
| 6693 | ret = -EINVAL; |
| 6694 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 6695 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6696 | out: |
| 6697 | return ret; |
| 6698 | |
| 6699 | err_size: |
| 6700 | put_user(sizeof(*attr), &uattr->size); |
| 6701 | ret = -E2BIG; |
| 6702 | goto out; |
| 6703 | } |
| 6704 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6705 | static int |
| 6706 | 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] | 6707 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6708 | struct ring_buffer *rb = NULL, *old_rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6709 | int ret = -EINVAL; |
| 6710 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6711 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6712 | goto set; |
| 6713 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6714 | /* don't allow circular references */ |
| 6715 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6716 | goto out; |
| 6717 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 6718 | /* |
| 6719 | * Don't allow cross-cpu buffers |
| 6720 | */ |
| 6721 | if (output_event->cpu != event->cpu) |
| 6722 | goto out; |
| 6723 | |
| 6724 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6725 | * If its not a per-cpu rb, it must be the same task. |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 6726 | */ |
| 6727 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 6728 | goto out; |
| 6729 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6730 | set: |
| 6731 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6732 | /* Can't redirect output if we've got an active mmap() */ |
| 6733 | if (atomic_read(&event->mmap_count)) |
| 6734 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6735 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 6736 | old_rb = event->rb; |
| 6737 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6738 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6739 | /* get the rb we want to redirect to */ |
| 6740 | rb = ring_buffer_get(output_event); |
| 6741 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6742 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6743 | } |
| 6744 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 6745 | if (old_rb) |
| 6746 | ring_buffer_detach(event, old_rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 6747 | |
| 6748 | if (rb) |
| 6749 | ring_buffer_attach(event, rb); |
| 6750 | |
| 6751 | rcu_assign_pointer(event->rb, rb); |
| 6752 | |
| 6753 | if (old_rb) { |
| 6754 | ring_buffer_put(old_rb); |
| 6755 | /* |
| 6756 | * Since we detached before setting the new rb, so that we |
| 6757 | * could attach the new rb, we could have missed a wakeup. |
| 6758 | * Provide it now. |
| 6759 | */ |
| 6760 | wake_up_all(&event->waitq); |
| 6761 | } |
| 6762 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6763 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6764 | unlock: |
| 6765 | mutex_unlock(&event->mmap_mutex); |
| 6766 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6767 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6768 | return ret; |
| 6769 | } |
| 6770 | |
| 6771 | /** |
| 6772 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 6773 | * |
| 6774 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 6775 | * @pid: target pid |
| 6776 | * @cpu: target cpu |
| 6777 | * @group_fd: group leader event fd |
| 6778 | */ |
| 6779 | SYSCALL_DEFINE5(perf_event_open, |
| 6780 | struct perf_event_attr __user *, attr_uptr, |
| 6781 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 6782 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6783 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 6784 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6785 | struct perf_event_attr attr; |
| 6786 | struct perf_event_context *ctx; |
| 6787 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 6788 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 6789 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6790 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6791 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6792 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6793 | int err; |
| 6794 | |
| 6795 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6796 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6797 | return -EINVAL; |
| 6798 | |
| 6799 | err = perf_copy_attr(attr_uptr, &attr); |
| 6800 | if (err) |
| 6801 | return err; |
| 6802 | |
| 6803 | if (!attr.exclude_kernel) { |
| 6804 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 6805 | return -EACCES; |
| 6806 | } |
| 6807 | |
| 6808 | if (attr.freq) { |
| 6809 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 6810 | return -EINVAL; |
| 6811 | } |
| 6812 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6813 | /* |
| 6814 | * In cgroup mode, the pid argument is used to pass the fd |
| 6815 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 6816 | * designates the cpu on which to monitor threads from that |
| 6817 | * cgroup. |
| 6818 | */ |
| 6819 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 6820 | return -EINVAL; |
| 6821 | |
Al Viro | ab72a70 | 2012-08-21 09:40:46 -0400 | [diff] [blame] | 6822 | event_fd = get_unused_fd(); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6823 | if (event_fd < 0) |
| 6824 | return event_fd; |
| 6825 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6826 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 6827 | err = perf_fget_light(group_fd, &group); |
| 6828 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 6829 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 6830 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6831 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 6832 | output_event = group_leader; |
| 6833 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 6834 | group_leader = NULL; |
| 6835 | } |
| 6836 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6837 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 6838 | task = find_lively_task_by_vpid(pid); |
| 6839 | if (IS_ERR(task)) { |
| 6840 | err = PTR_ERR(task); |
| 6841 | goto err_group_fd; |
| 6842 | } |
| 6843 | } |
| 6844 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 6845 | get_online_cpus(); |
| 6846 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6847 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
| 6848 | NULL, NULL); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 6849 | if (IS_ERR(event)) { |
| 6850 | err = PTR_ERR(event); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 6851 | goto err_task; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 6852 | } |
| 6853 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6854 | if (flags & PERF_FLAG_PID_CGROUP) { |
| 6855 | err = perf_cgroup_connect(pid, event, &attr, group_leader); |
| 6856 | if (err) |
| 6857 | goto err_alloc; |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 6858 | /* |
| 6859 | * one more event: |
| 6860 | * - that has cgroup constraint on event->cpu |
| 6861 | * - that may need work on context switch |
| 6862 | */ |
| 6863 | atomic_inc(&per_cpu(perf_cgroup_events, event->cpu)); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6864 | static_key_slow_inc(&perf_sched_events.key); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6865 | } |
| 6866 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6867 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6868 | * Special case software events and allow them to be part of |
| 6869 | * any hardware group. |
| 6870 | */ |
| 6871 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6872 | |
| 6873 | if (group_leader && |
| 6874 | (is_software_event(event) != is_software_event(group_leader))) { |
| 6875 | if (is_software_event(event)) { |
| 6876 | /* |
| 6877 | * If event and group_leader are not both a software |
| 6878 | * event, and event is, then group leader is not. |
| 6879 | * |
| 6880 | * Allow the addition of software events to !software |
| 6881 | * groups, this is safe because software events never |
| 6882 | * fail to schedule. |
| 6883 | */ |
| 6884 | pmu = group_leader->pmu; |
| 6885 | } else if (is_software_event(group_leader) && |
| 6886 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 6887 | /* |
| 6888 | * In case the group is a pure software group, and we |
| 6889 | * try to add a hardware event, move the whole group to |
| 6890 | * the hardware context. |
| 6891 | */ |
| 6892 | move_group = 1; |
| 6893 | } |
| 6894 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6895 | |
| 6896 | /* |
| 6897 | * Get the target context (task or percpu): |
| 6898 | */ |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 6899 | ctx = find_get_context(pmu, task, event->cpu); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6900 | if (IS_ERR(ctx)) { |
| 6901 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 6902 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6903 | } |
| 6904 | |
Peter Zijlstra | fd1edb3 | 2011-03-28 13:13:56 +0200 | [diff] [blame] | 6905 | if (task) { |
| 6906 | put_task_struct(task); |
| 6907 | task = NULL; |
| 6908 | } |
| 6909 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6910 | /* |
| 6911 | * Look up the group leader (we will attach this event to it): |
| 6912 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6913 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6914 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6915 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6916 | /* |
| 6917 | * Do not allow a recursive hierarchy (this new sibling |
| 6918 | * becoming part of another group-sibling): |
| 6919 | */ |
| 6920 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6921 | goto err_context; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6922 | /* |
| 6923 | * Do not allow to attach to a group in a different |
| 6924 | * task or CPU context: |
| 6925 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6926 | if (move_group) { |
| 6927 | if (group_leader->ctx->type != ctx->type) |
| 6928 | goto err_context; |
| 6929 | } else { |
| 6930 | if (group_leader->ctx != ctx) |
| 6931 | goto err_context; |
| 6932 | } |
| 6933 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6934 | /* |
| 6935 | * Only a group leader can be exclusive or pinned |
| 6936 | */ |
| 6937 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6938 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6939 | } |
| 6940 | |
| 6941 | if (output_event) { |
| 6942 | err = perf_event_set_output(event, output_event); |
| 6943 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6944 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 6945 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6946 | |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6947 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR); |
| 6948 | if (IS_ERR(event_file)) { |
| 6949 | err = PTR_ERR(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6950 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 6951 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6952 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6953 | if (move_group) { |
| 6954 | struct perf_event_context *gctx = group_leader->ctx; |
| 6955 | |
| 6956 | mutex_lock(&gctx->mutex); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 6957 | perf_remove_from_context(group_leader); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 6958 | |
| 6959 | /* |
| 6960 | * Removing from the context ends up with disabled |
| 6961 | * event. What we want here is event in the initial |
| 6962 | * startup state, ready to be add into new context. |
| 6963 | */ |
| 6964 | perf_event__state_init(group_leader); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6965 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 6966 | group_entry) { |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 6967 | perf_remove_from_context(sibling); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 6968 | perf_event__state_init(sibling); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6969 | put_ctx(gctx); |
| 6970 | } |
| 6971 | mutex_unlock(&gctx->mutex); |
| 6972 | put_ctx(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6973 | } |
| 6974 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6975 | WARN_ON_ONCE(ctx->parent_ctx); |
| 6976 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6977 | |
| 6978 | if (move_group) { |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 6979 | synchronize_rcu(); |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 6980 | perf_install_in_context(ctx, group_leader, event->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6981 | get_ctx(ctx); |
| 6982 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 6983 | group_entry) { |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 6984 | perf_install_in_context(ctx, sibling, event->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6985 | get_ctx(ctx); |
| 6986 | } |
| 6987 | } |
| 6988 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 6989 | perf_install_in_context(ctx, event, event->cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6990 | ++ctx->generation; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 6991 | perf_unpin_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6992 | mutex_unlock(&ctx->mutex); |
| 6993 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 6994 | put_online_cpus(); |
| 6995 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6996 | event->owner = current; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 6997 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6998 | mutex_lock(¤t->perf_event_mutex); |
| 6999 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 7000 | mutex_unlock(¤t->perf_event_mutex); |
| 7001 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7002 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7003 | * Precalculate sample_data sizes |
| 7004 | */ |
| 7005 | perf_event__header_size(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7006 | perf_event__id_header_size(event); |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7007 | |
| 7008 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7009 | * Drop the reference on the group_event after placing the |
| 7010 | * new event on the sibling_list. This ensures destruction |
| 7011 | * of the group leader will find the pointer to itself in |
| 7012 | * perf_group_detach(). |
| 7013 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7014 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7015 | fd_install(event_fd, event_file); |
| 7016 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7017 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7018 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7019 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7020 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7021 | err_alloc: |
| 7022 | free_event(event); |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 7023 | err_task: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7024 | put_online_cpus(); |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 7025 | if (task) |
| 7026 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7027 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7028 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7029 | err_fd: |
| 7030 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7031 | return err; |
| 7032 | } |
| 7033 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7034 | /** |
| 7035 | * perf_event_create_kernel_counter |
| 7036 | * |
| 7037 | * @attr: attributes of the counter to create |
| 7038 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7039 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7040 | */ |
| 7041 | struct perf_event * |
| 7042 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7043 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7044 | perf_overflow_handler_t overflow_handler, |
| 7045 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7046 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7047 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7048 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7049 | int err; |
| 7050 | |
| 7051 | /* |
| 7052 | * Get the target context (task or percpu): |
| 7053 | */ |
| 7054 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7055 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
| 7056 | overflow_handler, context); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7057 | if (IS_ERR(event)) { |
| 7058 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7059 | goto err; |
| 7060 | } |
| 7061 | |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7062 | ctx = find_get_context(event->pmu, task, cpu); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7063 | if (IS_ERR(ctx)) { |
| 7064 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7065 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7066 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7067 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7068 | WARN_ON_ONCE(ctx->parent_ctx); |
| 7069 | mutex_lock(&ctx->mutex); |
| 7070 | perf_install_in_context(ctx, event, cpu); |
| 7071 | ++ctx->generation; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7072 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7073 | mutex_unlock(&ctx->mutex); |
| 7074 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7075 | return event; |
| 7076 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7077 | err_free: |
| 7078 | free_event(event); |
| 7079 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7080 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7081 | } |
| 7082 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 7083 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7084 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 7085 | { |
| 7086 | struct perf_event_context *src_ctx; |
| 7087 | struct perf_event_context *dst_ctx; |
| 7088 | struct perf_event *event, *tmp; |
| 7089 | LIST_HEAD(events); |
| 7090 | |
| 7091 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 7092 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 7093 | |
| 7094 | mutex_lock(&src_ctx->mutex); |
| 7095 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 7096 | event_entry) { |
| 7097 | perf_remove_from_context(event); |
| 7098 | put_ctx(src_ctx); |
| 7099 | list_add(&event->event_entry, &events); |
| 7100 | } |
| 7101 | mutex_unlock(&src_ctx->mutex); |
| 7102 | |
| 7103 | synchronize_rcu(); |
| 7104 | |
| 7105 | mutex_lock(&dst_ctx->mutex); |
| 7106 | list_for_each_entry_safe(event, tmp, &events, event_entry) { |
| 7107 | list_del(&event->event_entry); |
| 7108 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 7109 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 7110 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 7111 | get_ctx(dst_ctx); |
| 7112 | } |
| 7113 | mutex_unlock(&dst_ctx->mutex); |
| 7114 | } |
| 7115 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 7116 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7117 | static void sync_child_event(struct perf_event *child_event, |
| 7118 | struct task_struct *child) |
| 7119 | { |
| 7120 | struct perf_event *parent_event = child_event->parent; |
| 7121 | u64 child_val; |
| 7122 | |
| 7123 | if (child_event->attr.inherit_stat) |
| 7124 | perf_event_read_event(child_event, child); |
| 7125 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 7126 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7127 | |
| 7128 | /* |
| 7129 | * Add back the child's count to the parent's count: |
| 7130 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 7131 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7132 | atomic64_add(child_event->total_time_enabled, |
| 7133 | &parent_event->child_total_time_enabled); |
| 7134 | atomic64_add(child_event->total_time_running, |
| 7135 | &parent_event->child_total_time_running); |
| 7136 | |
| 7137 | /* |
| 7138 | * Remove this event from the parent's list |
| 7139 | */ |
| 7140 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7141 | mutex_lock(&parent_event->child_mutex); |
| 7142 | list_del_init(&child_event->child_list); |
| 7143 | mutex_unlock(&parent_event->child_mutex); |
| 7144 | |
| 7145 | /* |
| 7146 | * Release the parent event, if this was the last |
| 7147 | * reference to it. |
| 7148 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7149 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7150 | } |
| 7151 | |
| 7152 | static void |
| 7153 | __perf_event_exit_task(struct perf_event *child_event, |
| 7154 | struct perf_event_context *child_ctx, |
| 7155 | struct task_struct *child) |
| 7156 | { |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7157 | if (child_event->parent) { |
| 7158 | raw_spin_lock_irq(&child_ctx->lock); |
| 7159 | perf_group_detach(child_event); |
| 7160 | raw_spin_unlock_irq(&child_ctx->lock); |
| 7161 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7162 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7163 | perf_remove_from_context(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7164 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7165 | /* |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7166 | * It can happen that the parent exits first, and has events |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7167 | * that are still around due to the child reference. These |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7168 | * events need to be zapped. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7169 | */ |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7170 | if (child_event->parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7171 | sync_child_event(child_event, child); |
| 7172 | free_event(child_event); |
| 7173 | } |
| 7174 | } |
| 7175 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7176 | 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] | 7177 | { |
| 7178 | struct perf_event *child_event, *tmp; |
| 7179 | struct perf_event_context *child_ctx; |
| 7180 | unsigned long flags; |
| 7181 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7182 | if (likely(!child->perf_event_ctxp[ctxn])) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7183 | perf_event_task(child, NULL, 0); |
| 7184 | return; |
| 7185 | } |
| 7186 | |
| 7187 | local_irq_save(flags); |
| 7188 | /* |
| 7189 | * We can't reschedule here because interrupts are disabled, |
| 7190 | * and either child is current or it is a task that can't be |
| 7191 | * scheduled, so we are now safe from rescheduling changing |
| 7192 | * our context. |
| 7193 | */ |
Oleg Nesterov | 806839b | 2011-01-21 18:45:47 +0100 | [diff] [blame] | 7194 | child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7195 | |
| 7196 | /* |
| 7197 | * Take the context lock here so that if find_get_context is |
| 7198 | * reading child->perf_event_ctxp, we wait until it has |
| 7199 | * incremented the context's refcount before we do put_ctx below. |
| 7200 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7201 | raw_spin_lock(&child_ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 7202 | task_ctx_sched_out(child_ctx); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7203 | child->perf_event_ctxp[ctxn] = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7204 | /* |
| 7205 | * If this context is a clone; unclone it so it can't get |
| 7206 | * swapped to another process while we're removing all |
| 7207 | * the events from it. |
| 7208 | */ |
| 7209 | unclone_ctx(child_ctx); |
Peter Zijlstra | 5e942bb | 2009-11-23 11:37:26 +0100 | [diff] [blame] | 7210 | update_context_time(child_ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7211 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7212 | |
| 7213 | /* |
| 7214 | * Report the task dead after unscheduling the events so that we |
| 7215 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 7216 | * get a few PERF_RECORD_READ events. |
| 7217 | */ |
| 7218 | perf_event_task(child, child_ctx, 0); |
| 7219 | |
| 7220 | /* |
| 7221 | * We can recurse on the same lock type through: |
| 7222 | * |
| 7223 | * __perf_event_exit_task() |
| 7224 | * sync_child_event() |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7225 | * put_event() |
| 7226 | * mutex_lock(&ctx->mutex) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7227 | * |
| 7228 | * But since its the parent context it won't be the same instance. |
| 7229 | */ |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 7230 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7231 | |
| 7232 | again: |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7233 | list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups, |
| 7234 | group_entry) |
| 7235 | __perf_event_exit_task(child_event, child_ctx, child); |
| 7236 | |
| 7237 | list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7238 | group_entry) |
| 7239 | __perf_event_exit_task(child_event, child_ctx, child); |
| 7240 | |
| 7241 | /* |
| 7242 | * If the last event was a group event, it will have appended all |
| 7243 | * its siblings to the list, but we obtained 'tmp' before that which |
| 7244 | * will still point to the list head terminating the iteration. |
| 7245 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7246 | if (!list_empty(&child_ctx->pinned_groups) || |
| 7247 | !list_empty(&child_ctx->flexible_groups)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7248 | goto again; |
| 7249 | |
| 7250 | mutex_unlock(&child_ctx->mutex); |
| 7251 | |
| 7252 | put_ctx(child_ctx); |
| 7253 | } |
| 7254 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7255 | /* |
| 7256 | * When a child task exits, feed back event values to parent events. |
| 7257 | */ |
| 7258 | void perf_event_exit_task(struct task_struct *child) |
| 7259 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7260 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7261 | int ctxn; |
| 7262 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7263 | mutex_lock(&child->perf_event_mutex); |
| 7264 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 7265 | owner_entry) { |
| 7266 | list_del_init(&event->owner_entry); |
| 7267 | |
| 7268 | /* |
| 7269 | * Ensure the list deletion is visible before we clear |
| 7270 | * the owner, closes a race against perf_release() where |
| 7271 | * we need to serialize on the owner->perf_event_mutex. |
| 7272 | */ |
| 7273 | smp_wmb(); |
| 7274 | event->owner = NULL; |
| 7275 | } |
| 7276 | mutex_unlock(&child->perf_event_mutex); |
| 7277 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7278 | for_each_task_context_nr(ctxn) |
| 7279 | perf_event_exit_task_context(child, ctxn); |
| 7280 | } |
| 7281 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7282 | static void perf_free_event(struct perf_event *event, |
| 7283 | struct perf_event_context *ctx) |
| 7284 | { |
| 7285 | struct perf_event *parent = event->parent; |
| 7286 | |
| 7287 | if (WARN_ON_ONCE(!parent)) |
| 7288 | return; |
| 7289 | |
| 7290 | mutex_lock(&parent->child_mutex); |
| 7291 | list_del_init(&event->child_list); |
| 7292 | mutex_unlock(&parent->child_mutex); |
| 7293 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7294 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7295 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7296 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7297 | list_del_event(event, ctx); |
| 7298 | free_event(event); |
| 7299 | } |
| 7300 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7301 | /* |
| 7302 | * free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7303 | * perf_event_init_task below, used by fork() in case of fail. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7304 | */ |
| 7305 | void perf_event_free_task(struct task_struct *task) |
| 7306 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7307 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7308 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7309 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7310 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7311 | for_each_task_context_nr(ctxn) { |
| 7312 | ctx = task->perf_event_ctxp[ctxn]; |
| 7313 | if (!ctx) |
| 7314 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7315 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7316 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7317 | again: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7318 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 7319 | group_entry) |
| 7320 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7321 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7322 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 7323 | group_entry) |
| 7324 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7325 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7326 | if (!list_empty(&ctx->pinned_groups) || |
| 7327 | !list_empty(&ctx->flexible_groups)) |
| 7328 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7329 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7330 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7331 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7332 | put_ctx(ctx); |
| 7333 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7334 | } |
| 7335 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 7336 | void perf_event_delayed_put(struct task_struct *task) |
| 7337 | { |
| 7338 | int ctxn; |
| 7339 | |
| 7340 | for_each_task_context_nr(ctxn) |
| 7341 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 7342 | } |
| 7343 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7344 | /* |
| 7345 | * inherit a event from parent task to child task: |
| 7346 | */ |
| 7347 | static struct perf_event * |
| 7348 | inherit_event(struct perf_event *parent_event, |
| 7349 | struct task_struct *parent, |
| 7350 | struct perf_event_context *parent_ctx, |
| 7351 | struct task_struct *child, |
| 7352 | struct perf_event *group_leader, |
| 7353 | struct perf_event_context *child_ctx) |
| 7354 | { |
| 7355 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7356 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7357 | |
| 7358 | /* |
| 7359 | * Instead of creating recursive hierarchies of events, |
| 7360 | * we link inherited events back to the original parent, |
| 7361 | * which has a filp for sure, which we use as the reference |
| 7362 | * count: |
| 7363 | */ |
| 7364 | if (parent_event->parent) |
| 7365 | parent_event = parent_event->parent; |
| 7366 | |
| 7367 | child_event = perf_event_alloc(&parent_event->attr, |
| 7368 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7369 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7370 | group_leader, parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7371 | NULL, NULL); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7372 | if (IS_ERR(child_event)) |
| 7373 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7374 | |
| 7375 | if (!atomic_long_inc_not_zero(&parent_event->refcount)) { |
| 7376 | free_event(child_event); |
| 7377 | return NULL; |
| 7378 | } |
| 7379 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7380 | get_ctx(child_ctx); |
| 7381 | |
| 7382 | /* |
| 7383 | * Make the child state follow the state of the parent event, |
| 7384 | * not its attr.disabled bit. We hold the parent's mutex, |
| 7385 | * so we won't race with perf_event_{en, dis}able_family. |
| 7386 | */ |
| 7387 | if (parent_event->state >= PERF_EVENT_STATE_INACTIVE) |
| 7388 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 7389 | else |
| 7390 | child_event->state = PERF_EVENT_STATE_OFF; |
| 7391 | |
| 7392 | if (parent_event->attr.freq) { |
| 7393 | u64 sample_period = parent_event->hw.sample_period; |
| 7394 | struct hw_perf_event *hwc = &child_event->hw; |
| 7395 | |
| 7396 | hwc->sample_period = sample_period; |
| 7397 | hwc->last_period = sample_period; |
| 7398 | |
| 7399 | local64_set(&hwc->period_left, sample_period); |
| 7400 | } |
| 7401 | |
| 7402 | child_event->ctx = child_ctx; |
| 7403 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7404 | child_event->overflow_handler_context |
| 7405 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7406 | |
| 7407 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 7408 | * Precalculate sample_data sizes |
| 7409 | */ |
| 7410 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7411 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 7412 | |
| 7413 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7414 | * Link it up in the child's context: |
| 7415 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7416 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7417 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7418 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7419 | |
| 7420 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7421 | * Link this into the parent event's child list |
| 7422 | */ |
| 7423 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7424 | mutex_lock(&parent_event->child_mutex); |
| 7425 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 7426 | mutex_unlock(&parent_event->child_mutex); |
| 7427 | |
| 7428 | return child_event; |
| 7429 | } |
| 7430 | |
| 7431 | static int inherit_group(struct perf_event *parent_event, |
| 7432 | struct task_struct *parent, |
| 7433 | struct perf_event_context *parent_ctx, |
| 7434 | struct task_struct *child, |
| 7435 | struct perf_event_context *child_ctx) |
| 7436 | { |
| 7437 | struct perf_event *leader; |
| 7438 | struct perf_event *sub; |
| 7439 | struct perf_event *child_ctr; |
| 7440 | |
| 7441 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 7442 | child, NULL, child_ctx); |
| 7443 | if (IS_ERR(leader)) |
| 7444 | return PTR_ERR(leader); |
| 7445 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 7446 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 7447 | child, leader, child_ctx); |
| 7448 | if (IS_ERR(child_ctr)) |
| 7449 | return PTR_ERR(child_ctr); |
| 7450 | } |
| 7451 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7452 | } |
| 7453 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7454 | static int |
| 7455 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 7456 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7457 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7458 | int *inherited_all) |
| 7459 | { |
| 7460 | int ret; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7461 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7462 | |
| 7463 | if (!event->attr.inherit) { |
| 7464 | *inherited_all = 0; |
| 7465 | return 0; |
| 7466 | } |
| 7467 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7468 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7469 | if (!child_ctx) { |
| 7470 | /* |
| 7471 | * This is executed from the parent task context, so |
| 7472 | * inherit events that have been marked for cloning. |
| 7473 | * First allocate and initialize a context for the |
| 7474 | * child. |
| 7475 | */ |
| 7476 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 7477 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7478 | if (!child_ctx) |
| 7479 | return -ENOMEM; |
| 7480 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7481 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7482 | } |
| 7483 | |
| 7484 | ret = inherit_group(event, parent, parent_ctx, |
| 7485 | child, child_ctx); |
| 7486 | |
| 7487 | if (ret) |
| 7488 | *inherited_all = 0; |
| 7489 | |
| 7490 | return ret; |
| 7491 | } |
| 7492 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7493 | /* |
| 7494 | * Initialize the perf_event context in task_struct |
| 7495 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7496 | int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7497 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7498 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7499 | struct perf_event_context *cloned_ctx; |
| 7500 | struct perf_event *event; |
| 7501 | struct task_struct *parent = current; |
| 7502 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7503 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7504 | int ret = 0; |
| 7505 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7506 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7507 | return 0; |
| 7508 | |
| 7509 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7510 | * If the parent's context is a clone, pin it so it won't get |
| 7511 | * swapped under us. |
| 7512 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7513 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7514 | |
| 7515 | /* |
| 7516 | * No need to check if parent_ctx != NULL here; since we saw |
| 7517 | * it non-NULL earlier, the only reason for it to become NULL |
| 7518 | * is if we exit, and since we're currently in the middle of |
| 7519 | * a fork we can't be exiting at the same time. |
| 7520 | */ |
| 7521 | |
| 7522 | /* |
| 7523 | * Lock the parent list. No need to lock the child - not PID |
| 7524 | * hashed yet and not running, so nobody can access it. |
| 7525 | */ |
| 7526 | mutex_lock(&parent_ctx->mutex); |
| 7527 | |
| 7528 | /* |
| 7529 | * We dont have to disable NMIs - we are only looking at |
| 7530 | * the list, not manipulating it: |
| 7531 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7532 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7533 | ret = inherit_task_group(event, parent, parent_ctx, |
| 7534 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7535 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7536 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7537 | } |
| 7538 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7539 | /* |
| 7540 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 7541 | * to allocations, but we need to prevent rotation because |
| 7542 | * rotate_ctx() will change the list from interrupt context. |
| 7543 | */ |
| 7544 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 7545 | parent_ctx->rotate_disable = 1; |
| 7546 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 7547 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7548 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7549 | ret = inherit_task_group(event, parent, parent_ctx, |
| 7550 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7551 | if (ret) |
| 7552 | break; |
| 7553 | } |
| 7554 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7555 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 7556 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7557 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7558 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7559 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 7560 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7561 | /* |
| 7562 | * Mark the child context as a clone of the parent |
| 7563 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7564 | * |
| 7565 | * Note that if the parent is a clone, the holding of |
| 7566 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7567 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7568 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7569 | if (cloned_ctx) { |
| 7570 | child_ctx->parent_ctx = cloned_ctx; |
| 7571 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 7572 | } else { |
| 7573 | child_ctx->parent_ctx = parent_ctx; |
| 7574 | child_ctx->parent_gen = parent_ctx->generation; |
| 7575 | } |
| 7576 | get_ctx(child_ctx->parent_ctx); |
| 7577 | } |
| 7578 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7579 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7580 | mutex_unlock(&parent_ctx->mutex); |
| 7581 | |
| 7582 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7583 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7584 | |
| 7585 | return ret; |
| 7586 | } |
| 7587 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7588 | /* |
| 7589 | * Initialize the perf_event context in task_struct |
| 7590 | */ |
| 7591 | int perf_event_init_task(struct task_struct *child) |
| 7592 | { |
| 7593 | int ctxn, ret; |
| 7594 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 7595 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 7596 | mutex_init(&child->perf_event_mutex); |
| 7597 | INIT_LIST_HEAD(&child->perf_event_list); |
| 7598 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7599 | for_each_task_context_nr(ctxn) { |
| 7600 | ret = perf_event_init_context(child, ctxn); |
| 7601 | if (ret) |
| 7602 | return ret; |
| 7603 | } |
| 7604 | |
| 7605 | return 0; |
| 7606 | } |
| 7607 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7608 | static void __init perf_event_init_all_cpus(void) |
| 7609 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7610 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7611 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7612 | |
| 7613 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7614 | swhash = &per_cpu(swevent_htable, cpu); |
| 7615 | mutex_init(&swhash->hlist_mutex); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7616 | INIT_LIST_HEAD(&per_cpu(rotation_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7617 | } |
| 7618 | } |
| 7619 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7620 | static void __cpuinit perf_event_init_cpu(int cpu) |
| 7621 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7622 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7623 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7624 | mutex_lock(&swhash->hlist_mutex); |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 7625 | if (swhash->hlist_refcount > 0) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7626 | struct swevent_hlist *hlist; |
| 7627 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7628 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 7629 | WARN_ON(!hlist); |
| 7630 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7631 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7632 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7633 | } |
| 7634 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 7635 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7636 | static void perf_pmu_rotate_stop(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7637 | { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7638 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 7639 | |
| 7640 | WARN_ON(!irqs_disabled()); |
| 7641 | |
| 7642 | list_del_init(&cpuctx->rotation_list); |
| 7643 | } |
| 7644 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7645 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7646 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7647 | struct perf_event_context *ctx = __info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7648 | struct perf_event *event, *tmp; |
| 7649 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7650 | perf_pmu_rotate_stop(ctx->pmu); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 7651 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7652 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7653 | __perf_remove_from_context(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7654 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7655 | __perf_remove_from_context(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7656 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7657 | |
| 7658 | static void perf_event_exit_cpu_context(int cpu) |
| 7659 | { |
| 7660 | struct perf_event_context *ctx; |
| 7661 | struct pmu *pmu; |
| 7662 | int idx; |
| 7663 | |
| 7664 | idx = srcu_read_lock(&pmus_srcu); |
| 7665 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 7666 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7667 | |
| 7668 | mutex_lock(&ctx->mutex); |
| 7669 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 7670 | mutex_unlock(&ctx->mutex); |
| 7671 | } |
| 7672 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7673 | } |
| 7674 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7675 | static void perf_event_exit_cpu(int cpu) |
| 7676 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7677 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7678 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7679 | mutex_lock(&swhash->hlist_mutex); |
| 7680 | swevent_hlist_release(swhash); |
| 7681 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7682 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7683 | perf_event_exit_cpu_context(cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7684 | } |
| 7685 | #else |
| 7686 | static inline void perf_event_exit_cpu(int cpu) { } |
| 7687 | #endif |
| 7688 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 7689 | static int |
| 7690 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 7691 | { |
| 7692 | int cpu; |
| 7693 | |
| 7694 | for_each_online_cpu(cpu) |
| 7695 | perf_event_exit_cpu(cpu); |
| 7696 | |
| 7697 | return NOTIFY_OK; |
| 7698 | } |
| 7699 | |
| 7700 | /* |
| 7701 | * Run the perf reboot notifier at the very last possible moment so that |
| 7702 | * the generic watchdog code runs as long as possible. |
| 7703 | */ |
| 7704 | static struct notifier_block perf_reboot_notifier = { |
| 7705 | .notifier_call = perf_reboot, |
| 7706 | .priority = INT_MIN, |
| 7707 | }; |
| 7708 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7709 | static int __cpuinit |
| 7710 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 7711 | { |
| 7712 | unsigned int cpu = (long)hcpu; |
| 7713 | |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 7714 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7715 | |
| 7716 | case CPU_UP_PREPARE: |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7717 | case CPU_DOWN_FAILED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7718 | perf_event_init_cpu(cpu); |
| 7719 | break; |
| 7720 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7721 | case CPU_UP_CANCELED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7722 | case CPU_DOWN_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7723 | perf_event_exit_cpu(cpu); |
| 7724 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7725 | default: |
| 7726 | break; |
| 7727 | } |
| 7728 | |
| 7729 | return NOTIFY_OK; |
| 7730 | } |
| 7731 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7732 | void __init perf_event_init(void) |
| 7733 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 7734 | int ret; |
| 7735 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7736 | idr_init(&pmu_idr); |
| 7737 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7738 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7739 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7740 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 7741 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 7742 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7743 | perf_tp_register(); |
| 7744 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 7745 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 7746 | |
| 7747 | ret = init_hw_breakpoint(); |
| 7748 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 7749 | |
| 7750 | /* do not patch jump label more than once per second */ |
| 7751 | jump_label_rate_limit(&perf_sched_events, HZ); |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 7752 | |
| 7753 | /* |
| 7754 | * Build time assertion that we keep the data_head at the intended |
| 7755 | * location. IOW, validation we got the __reserved[] size right. |
| 7756 | */ |
| 7757 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 7758 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7759 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7760 | |
| 7761 | static int __init perf_event_sysfs_init(void) |
| 7762 | { |
| 7763 | struct pmu *pmu; |
| 7764 | int ret; |
| 7765 | |
| 7766 | mutex_lock(&pmus_lock); |
| 7767 | |
| 7768 | ret = bus_register(&pmu_bus); |
| 7769 | if (ret) |
| 7770 | goto unlock; |
| 7771 | |
| 7772 | list_for_each_entry(pmu, &pmus, entry) { |
| 7773 | if (!pmu->name || pmu->type < 0) |
| 7774 | continue; |
| 7775 | |
| 7776 | ret = pmu_dev_alloc(pmu); |
| 7777 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 7778 | } |
| 7779 | pmu_bus_running = 1; |
| 7780 | ret = 0; |
| 7781 | |
| 7782 | unlock: |
| 7783 | mutex_unlock(&pmus_lock); |
| 7784 | |
| 7785 | return ret; |
| 7786 | } |
| 7787 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7788 | |
| 7789 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 7790 | static struct cgroup_subsys_state *perf_cgroup_css_alloc(struct cgroup *cont) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7791 | { |
| 7792 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7793 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 7794 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7795 | if (!jc) |
| 7796 | return ERR_PTR(-ENOMEM); |
| 7797 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7798 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 7799 | if (!jc->info) { |
| 7800 | kfree(jc); |
| 7801 | return ERR_PTR(-ENOMEM); |
| 7802 | } |
| 7803 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7804 | return &jc->css; |
| 7805 | } |
| 7806 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 7807 | static void perf_cgroup_css_free(struct cgroup *cont) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7808 | { |
| 7809 | struct perf_cgroup *jc; |
| 7810 | jc = container_of(cgroup_subsys_state(cont, perf_subsys_id), |
| 7811 | struct perf_cgroup, css); |
| 7812 | free_percpu(jc->info); |
| 7813 | kfree(jc); |
| 7814 | } |
| 7815 | |
| 7816 | static int __perf_cgroup_move(void *info) |
| 7817 | { |
| 7818 | struct task_struct *task = info; |
| 7819 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
| 7820 | return 0; |
| 7821 | } |
| 7822 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 7823 | static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7824 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 7825 | struct task_struct *task; |
| 7826 | |
| 7827 | cgroup_taskset_for_each(task, cgrp, tset) |
| 7828 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7829 | } |
| 7830 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 7831 | static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp, |
| 7832 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7833 | { |
| 7834 | /* |
| 7835 | * cgroup_exit() is called in the copy_process() failure path. |
| 7836 | * Ignore this case since the task hasn't ran yet, this avoids |
| 7837 | * trying to poke a half freed task state from generic code. |
| 7838 | */ |
| 7839 | if (!(task->flags & PF_EXITING)) |
| 7840 | return; |
| 7841 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 7842 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7843 | } |
| 7844 | |
| 7845 | struct cgroup_subsys perf_subsys = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 7846 | .name = "perf_event", |
| 7847 | .subsys_id = perf_subsys_id, |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 7848 | .css_alloc = perf_cgroup_css_alloc, |
| 7849 | .css_free = perf_cgroup_css_free, |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 7850 | .exit = perf_cgroup_exit, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 7851 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7852 | }; |
| 7853 | #endif /* CONFIG_CGROUP_PERF */ |