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> |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 42 | #include <linux/module.h> |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 43 | #include <linux/mman.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 44 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 45 | #include "internal.h" |
| 46 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 47 | #include <asm/irq_regs.h> |
| 48 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 49 | struct remote_function_call { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 50 | struct task_struct *p; |
| 51 | int (*func)(void *info); |
| 52 | void *info; |
| 53 | int ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static void remote_function(void *data) |
| 57 | { |
| 58 | struct remote_function_call *tfc = data; |
| 59 | struct task_struct *p = tfc->p; |
| 60 | |
| 61 | if (p) { |
| 62 | tfc->ret = -EAGAIN; |
| 63 | if (task_cpu(p) != smp_processor_id() || !task_curr(p)) |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | tfc->ret = tfc->func(tfc->info); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * task_function_call - call a function on the cpu on which a task runs |
| 72 | * @p: the task to evaluate |
| 73 | * @func: the function to be called |
| 74 | * @info: the function call argument |
| 75 | * |
| 76 | * Calls the function @func when the task is currently running. This might |
| 77 | * be on the current CPU, which just calls the function directly |
| 78 | * |
| 79 | * returns: @func return value, or |
| 80 | * -ESRCH - when the process isn't running |
| 81 | * -EAGAIN - when the process moved away |
| 82 | */ |
| 83 | static int |
| 84 | task_function_call(struct task_struct *p, int (*func) (void *info), void *info) |
| 85 | { |
| 86 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 87 | .p = p, |
| 88 | .func = func, |
| 89 | .info = info, |
| 90 | .ret = -ESRCH, /* No such (running) process */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | if (task_curr(p)) |
| 94 | smp_call_function_single(task_cpu(p), remote_function, &data, 1); |
| 95 | |
| 96 | return data.ret; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * cpu_function_call - call a function on the cpu |
| 101 | * @func: the function to be called |
| 102 | * @info: the function call argument |
| 103 | * |
| 104 | * Calls the function @func on the remote cpu. |
| 105 | * |
| 106 | * returns: @func return value or -ENXIO when the cpu is offline |
| 107 | */ |
| 108 | static int cpu_function_call(int cpu, int (*func) (void *info), void *info) |
| 109 | { |
| 110 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 111 | .p = NULL, |
| 112 | .func = func, |
| 113 | .info = info, |
| 114 | .ret = -ENXIO, /* No such CPU */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | smp_call_function_single(cpu, remote_function, &data, 1); |
| 118 | |
| 119 | return data.ret; |
| 120 | } |
| 121 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 122 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
| 123 | PERF_FLAG_FD_OUTPUT |\ |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 124 | PERF_FLAG_PID_CGROUP |\ |
| 125 | PERF_FLAG_FD_CLOEXEC) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 126 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 127 | /* |
| 128 | * branch priv levels that need permission checks |
| 129 | */ |
| 130 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ |
| 131 | (PERF_SAMPLE_BRANCH_KERNEL |\ |
| 132 | PERF_SAMPLE_BRANCH_HV) |
| 133 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 134 | enum event_type_t { |
| 135 | EVENT_FLEXIBLE = 0x1, |
| 136 | EVENT_PINNED = 0x2, |
| 137 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
| 138 | }; |
| 139 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 140 | /* |
| 141 | * perf_sched_events : >0 events exist |
| 142 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu |
| 143 | */ |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 144 | struct static_key_deferred perf_sched_events __read_mostly; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 145 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 146 | static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 147 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 148 | static atomic_t nr_mmap_events __read_mostly; |
| 149 | static atomic_t nr_comm_events __read_mostly; |
| 150 | static atomic_t nr_task_events __read_mostly; |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 151 | static atomic_t nr_freq_events __read_mostly; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 152 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 153 | static LIST_HEAD(pmus); |
| 154 | static DEFINE_MUTEX(pmus_lock); |
| 155 | static struct srcu_struct pmus_srcu; |
| 156 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 157 | /* |
| 158 | * perf event paranoia level: |
| 159 | * -1 - not paranoid at all |
| 160 | * 0 - disallow raw tracepoint access for unpriv |
| 161 | * 1 - disallow cpu events for unpriv |
| 162 | * 2 - disallow kernel profiling for unpriv |
| 163 | */ |
| 164 | int sysctl_perf_event_paranoid __read_mostly = 1; |
| 165 | |
Frederic Weisbecker | 2044338 | 2011-03-31 03:33:29 +0200 | [diff] [blame] | 166 | /* Minimum for 512 kiB + 1 user control page */ |
| 167 | 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] | 168 | |
| 169 | /* |
| 170 | * max perf event sample rate |
| 171 | */ |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 172 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
| 173 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) |
| 174 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 |
| 175 | |
| 176 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; |
| 177 | |
| 178 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); |
| 179 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; |
| 180 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 181 | static int perf_sample_allowed_ns __read_mostly = |
| 182 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 183 | |
| 184 | void update_perf_cpu_limits(void) |
| 185 | { |
| 186 | u64 tmp = perf_sample_period_ns; |
| 187 | |
| 188 | tmp *= sysctl_perf_cpu_time_max_percent; |
Stephane Eranian | e530292 | 2013-07-05 00:30:11 +0200 | [diff] [blame] | 189 | do_div(tmp, 100); |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 190 | ACCESS_ONCE(perf_sample_allowed_ns) = tmp; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 191 | } |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 192 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 193 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
| 194 | |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 195 | int perf_proc_update_handler(struct ctl_table *table, int write, |
| 196 | void __user *buffer, size_t *lenp, |
| 197 | loff_t *ppos) |
| 198 | { |
Knut Petersen | 723478c | 2013-09-25 14:29:37 +0200 | [diff] [blame] | 199 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 200 | |
| 201 | if (ret || !write) |
| 202 | return ret; |
| 203 | |
| 204 | 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] | 205 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 206 | update_perf_cpu_limits(); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 207 | |
| 208 | return 0; |
| 209 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 210 | |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 211 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; |
| 212 | |
| 213 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, |
| 214 | void __user *buffer, size_t *lenp, |
| 215 | loff_t *ppos) |
| 216 | { |
| 217 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 218 | |
| 219 | if (ret || !write) |
| 220 | return ret; |
| 221 | |
| 222 | update_perf_cpu_limits(); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * perf samples are done in some very critical code paths (NMIs). |
| 229 | * If they take too much CPU time, the system can lock up and not |
| 230 | * get any real work done. This will drop the sample rate when |
| 231 | * we detect that events are taking too long. |
| 232 | */ |
| 233 | #define NR_ACCUMULATED_SAMPLES 128 |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 234 | static DEFINE_PER_CPU(u64, running_sample_length); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 235 | |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 236 | static void perf_duration_warn(struct irq_work *w) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 237 | { |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 238 | u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 239 | u64 avg_local_sample_len; |
Stephane Eranian | e530292 | 2013-07-05 00:30:11 +0200 | [diff] [blame] | 240 | u64 local_samples_len; |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 241 | |
| 242 | local_samples_len = __get_cpu_var(running_sample_length); |
| 243 | avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES; |
| 244 | |
| 245 | printk_ratelimited(KERN_WARNING |
| 246 | "perf interrupt took too long (%lld > %lld), lowering " |
| 247 | "kernel.perf_event_max_sample_rate to %d\n", |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 248 | avg_local_sample_len, allowed_ns >> 1, |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 249 | sysctl_perf_event_sample_rate); |
| 250 | } |
| 251 | |
| 252 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); |
| 253 | |
| 254 | void perf_sample_event_took(u64 sample_len_ns) |
| 255 | { |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 256 | u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns); |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 257 | u64 avg_local_sample_len; |
| 258 | u64 local_samples_len; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 259 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 260 | if (allowed_ns == 0) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 261 | return; |
| 262 | |
| 263 | /* decay the counter by 1 average sample */ |
| 264 | local_samples_len = __get_cpu_var(running_sample_length); |
| 265 | local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES; |
| 266 | local_samples_len += sample_len_ns; |
| 267 | __get_cpu_var(running_sample_length) = local_samples_len; |
| 268 | |
| 269 | /* |
| 270 | * note: this will be biased artifically low until we have |
| 271 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us |
| 272 | * from having to maintain a count. |
| 273 | */ |
| 274 | avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES; |
| 275 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 276 | if (avg_local_sample_len <= allowed_ns) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 277 | return; |
| 278 | |
| 279 | if (max_samples_per_tick <= 1) |
| 280 | return; |
| 281 | |
| 282 | max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2); |
| 283 | sysctl_perf_event_sample_rate = max_samples_per_tick * HZ; |
| 284 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 285 | |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 286 | update_perf_cpu_limits(); |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 287 | |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 288 | if (!irq_work_queue(&perf_duration_work)) { |
| 289 | early_printk("perf interrupt took too long (%lld > %lld), lowering " |
| 290 | "kernel.perf_event_max_sample_rate to %d\n", |
| 291 | avg_local_sample_len, allowed_ns >> 1, |
| 292 | sysctl_perf_event_sample_rate); |
| 293 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 296 | static atomic64_t perf_event_id; |
| 297 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 298 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 299 | enum event_type_t event_type); |
| 300 | |
| 301 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 302 | enum event_type_t event_type, |
| 303 | struct task_struct *task); |
| 304 | |
| 305 | static void update_context_time(struct perf_event_context *ctx); |
| 306 | static u64 perf_event_time(struct perf_event *event); |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 307 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 308 | void __weak perf_event_print_debug(void) { } |
| 309 | |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 310 | extern __weak const char *perf_pmu_name(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 311 | { |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 312 | return "pmu"; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 315 | static inline u64 perf_clock(void) |
| 316 | { |
| 317 | return local_clock(); |
| 318 | } |
| 319 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 320 | static inline struct perf_cpu_context * |
| 321 | __get_cpu_context(struct perf_event_context *ctx) |
| 322 | { |
| 323 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); |
| 324 | } |
| 325 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 326 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, |
| 327 | struct perf_event_context *ctx) |
| 328 | { |
| 329 | raw_spin_lock(&cpuctx->ctx.lock); |
| 330 | if (ctx) |
| 331 | raw_spin_lock(&ctx->lock); |
| 332 | } |
| 333 | |
| 334 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, |
| 335 | struct perf_event_context *ctx) |
| 336 | { |
| 337 | if (ctx) |
| 338 | raw_spin_unlock(&ctx->lock); |
| 339 | raw_spin_unlock(&cpuctx->ctx.lock); |
| 340 | } |
| 341 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 342 | #ifdef CONFIG_CGROUP_PERF |
| 343 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 344 | /* |
Li Zefan | 877c685 | 2013-03-05 11:38:08 +0800 | [diff] [blame] | 345 | * perf_cgroup_info keeps track of time_enabled for a cgroup. |
| 346 | * This is a per-cpu dynamically allocated data structure. |
| 347 | */ |
| 348 | struct perf_cgroup_info { |
| 349 | u64 time; |
| 350 | u64 timestamp; |
| 351 | }; |
| 352 | |
| 353 | struct perf_cgroup { |
| 354 | struct cgroup_subsys_state css; |
Namhyung Kim | 86e213e | 2013-03-18 18:56:34 +0900 | [diff] [blame] | 355 | struct perf_cgroup_info __percpu *info; |
Li Zefan | 877c685 | 2013-03-05 11:38:08 +0800 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 359 | * Must ensure cgroup is pinned (css_get) before calling |
| 360 | * this function. In other words, we cannot call this function |
| 361 | * if there is no cgroup event for the current CPU context. |
| 362 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 363 | static inline struct perf_cgroup * |
| 364 | perf_cgroup_from_task(struct task_struct *task) |
| 365 | { |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 366 | return container_of(task_css(task, perf_event_cgrp_id), |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 367 | struct perf_cgroup, css); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static inline bool |
| 371 | perf_cgroup_match(struct perf_event *event) |
| 372 | { |
| 373 | struct perf_event_context *ctx = event->ctx; |
| 374 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 375 | |
Tejun Heo | ef824fa | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 376 | /* @event doesn't care about cgroup */ |
| 377 | if (!event->cgrp) |
| 378 | return true; |
| 379 | |
| 380 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ |
| 381 | if (!cpuctx->cgrp) |
| 382 | return false; |
| 383 | |
| 384 | /* |
| 385 | * Cgroup scoping is recursive. An event enabled for a cgroup is |
| 386 | * also enabled for all its descendant cgroups. If @cpuctx's |
| 387 | * cgroup is a descendant of @event's (the test covers identity |
| 388 | * case), it's a match. |
| 389 | */ |
| 390 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, |
| 391 | event->cgrp->css.cgroup); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 394 | static inline void perf_put_cgroup(struct perf_event *event) |
| 395 | { |
| 396 | css_put(&event->cgrp->css); |
| 397 | } |
| 398 | |
| 399 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 400 | { |
| 401 | perf_put_cgroup(event); |
| 402 | event->cgrp = NULL; |
| 403 | } |
| 404 | |
| 405 | static inline int is_cgroup_event(struct perf_event *event) |
| 406 | { |
| 407 | return event->cgrp != NULL; |
| 408 | } |
| 409 | |
| 410 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 411 | { |
| 412 | struct perf_cgroup_info *t; |
| 413 | |
| 414 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 415 | return t->time; |
| 416 | } |
| 417 | |
| 418 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) |
| 419 | { |
| 420 | struct perf_cgroup_info *info; |
| 421 | u64 now; |
| 422 | |
| 423 | now = perf_clock(); |
| 424 | |
| 425 | info = this_cpu_ptr(cgrp->info); |
| 426 | |
| 427 | info->time += now - info->timestamp; |
| 428 | info->timestamp = now; |
| 429 | } |
| 430 | |
| 431 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 432 | { |
| 433 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; |
| 434 | if (cgrp_out) |
| 435 | __update_cgrp_time(cgrp_out); |
| 436 | } |
| 437 | |
| 438 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 439 | { |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 440 | struct perf_cgroup *cgrp; |
| 441 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 442 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 443 | * ensure we access cgroup data only when needed and |
| 444 | * when we know the cgroup is pinned (css_get) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 445 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 446 | if (!is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 447 | return; |
| 448 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 449 | cgrp = perf_cgroup_from_task(current); |
| 450 | /* |
| 451 | * Do not update time when cgroup is not active |
| 452 | */ |
| 453 | if (cgrp == event->cgrp) |
| 454 | __update_cgrp_time(event->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 458 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 459 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 460 | { |
| 461 | struct perf_cgroup *cgrp; |
| 462 | struct perf_cgroup_info *info; |
| 463 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 464 | /* |
| 465 | * ctx->lock held by caller |
| 466 | * ensure we do not access cgroup data |
| 467 | * unless we have the cgroup pinned (css_get) |
| 468 | */ |
| 469 | if (!task || !ctx->nr_cgroups) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 470 | return; |
| 471 | |
| 472 | cgrp = perf_cgroup_from_task(task); |
| 473 | info = this_cpu_ptr(cgrp->info); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 474 | info->timestamp = ctx->timestamp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
| 478 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ |
| 479 | |
| 480 | /* |
| 481 | * reschedule events based on the cgroup constraint of task. |
| 482 | * |
| 483 | * mode SWOUT : schedule out everything |
| 484 | * mode SWIN : schedule in based on cgroup for next |
| 485 | */ |
| 486 | void perf_cgroup_switch(struct task_struct *task, int mode) |
| 487 | { |
| 488 | struct perf_cpu_context *cpuctx; |
| 489 | struct pmu *pmu; |
| 490 | unsigned long flags; |
| 491 | |
| 492 | /* |
| 493 | * disable interrupts to avoid geting nr_cgroup |
| 494 | * changes via __perf_event_disable(). Also |
| 495 | * avoids preemption. |
| 496 | */ |
| 497 | local_irq_save(flags); |
| 498 | |
| 499 | /* |
| 500 | * we reschedule only in the presence of cgroup |
| 501 | * constrained events. |
| 502 | */ |
| 503 | rcu_read_lock(); |
| 504 | |
| 505 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 506 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 507 | if (cpuctx->unique_pmu != pmu) |
| 508 | continue; /* ensure we process each cpuctx once */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 509 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 510 | /* |
| 511 | * perf_cgroup_events says at least one |
| 512 | * context on this CPU has cgroup events. |
| 513 | * |
| 514 | * ctx->nr_cgroups reports the number of cgroup |
| 515 | * events for a context. |
| 516 | */ |
| 517 | if (cpuctx->ctx.nr_cgroups > 0) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 518 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 519 | perf_pmu_disable(cpuctx->ctx.pmu); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 520 | |
| 521 | if (mode & PERF_CGROUP_SWOUT) { |
| 522 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 523 | /* |
| 524 | * must not be done before ctxswout due |
| 525 | * to event_filter_match() in event_sched_out() |
| 526 | */ |
| 527 | cpuctx->cgrp = NULL; |
| 528 | } |
| 529 | |
| 530 | if (mode & PERF_CGROUP_SWIN) { |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 531 | WARN_ON_ONCE(cpuctx->cgrp); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 532 | /* |
| 533 | * set cgrp before ctxsw in to allow |
| 534 | * event_filter_match() to not have to pass |
| 535 | * task around |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 536 | */ |
| 537 | cpuctx->cgrp = perf_cgroup_from_task(task); |
| 538 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
| 539 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 540 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 541 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 542 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | rcu_read_unlock(); |
| 546 | |
| 547 | local_irq_restore(flags); |
| 548 | } |
| 549 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 550 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 551 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 552 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 553 | struct perf_cgroup *cgrp1; |
| 554 | struct perf_cgroup *cgrp2 = NULL; |
| 555 | |
| 556 | /* |
| 557 | * we come here when we know perf_cgroup_events > 0 |
| 558 | */ |
| 559 | cgrp1 = perf_cgroup_from_task(task); |
| 560 | |
| 561 | /* |
| 562 | * next is NULL when called from perf_event_enable_on_exec() |
| 563 | * that will systematically cause a cgroup_switch() |
| 564 | */ |
| 565 | if (next) |
| 566 | cgrp2 = perf_cgroup_from_task(next); |
| 567 | |
| 568 | /* |
| 569 | * only schedule out current cgroup events if we know |
| 570 | * that we are switching to a different cgroup. Otherwise, |
| 571 | * do no touch the cgroup events. |
| 572 | */ |
| 573 | if (cgrp1 != cgrp2) |
| 574 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 577 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 578 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 579 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 580 | struct perf_cgroup *cgrp1; |
| 581 | struct perf_cgroup *cgrp2 = NULL; |
| 582 | |
| 583 | /* |
| 584 | * we come here when we know perf_cgroup_events > 0 |
| 585 | */ |
| 586 | cgrp1 = perf_cgroup_from_task(task); |
| 587 | |
| 588 | /* prev can never be NULL */ |
| 589 | cgrp2 = perf_cgroup_from_task(prev); |
| 590 | |
| 591 | /* |
| 592 | * only need to schedule in cgroup events if we are changing |
| 593 | * cgroup during ctxsw. Cgroup events were not scheduled |
| 594 | * out of ctxsw out if that was not the case. |
| 595 | */ |
| 596 | if (cgrp1 != cgrp2) |
| 597 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
| 601 | struct perf_event_attr *attr, |
| 602 | struct perf_event *group_leader) |
| 603 | { |
| 604 | struct perf_cgroup *cgrp; |
| 605 | struct cgroup_subsys_state *css; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 606 | struct fd f = fdget(fd); |
| 607 | int ret = 0; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 608 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 609 | if (!f.file) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 610 | return -EBADF; |
| 611 | |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 612 | css = css_tryget_online_from_dir(f.file->f_dentry, |
| 613 | &perf_event_cgrp_subsys); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 614 | if (IS_ERR(css)) { |
| 615 | ret = PTR_ERR(css); |
| 616 | goto out; |
| 617 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 618 | |
| 619 | cgrp = container_of(css, struct perf_cgroup, css); |
| 620 | event->cgrp = cgrp; |
| 621 | |
| 622 | /* |
| 623 | * all events in a group must monitor |
| 624 | * the same cgroup because a task belongs |
| 625 | * to only one perf cgroup at a time |
| 626 | */ |
| 627 | if (group_leader && group_leader->cgrp != cgrp) { |
| 628 | perf_detach_cgroup(event); |
| 629 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 630 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 631 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 632 | fdput(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 633 | return ret; |
| 634 | } |
| 635 | |
| 636 | static inline void |
| 637 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 638 | { |
| 639 | struct perf_cgroup_info *t; |
| 640 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 641 | event->shadow_ctx_time = now - t->timestamp; |
| 642 | } |
| 643 | |
| 644 | static inline void |
| 645 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 646 | { |
| 647 | /* |
| 648 | * when the current task's perf cgroup does not match |
| 649 | * the event's, we need to remember to call the |
| 650 | * perf_mark_enable() function the first time a task with |
| 651 | * a matching perf cgroup is scheduled in. |
| 652 | */ |
| 653 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 654 | event->cgrp_defer_enabled = 1; |
| 655 | } |
| 656 | |
| 657 | static inline void |
| 658 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 659 | struct perf_event_context *ctx) |
| 660 | { |
| 661 | struct perf_event *sub; |
| 662 | u64 tstamp = perf_event_time(event); |
| 663 | |
| 664 | if (!event->cgrp_defer_enabled) |
| 665 | return; |
| 666 | |
| 667 | event->cgrp_defer_enabled = 0; |
| 668 | |
| 669 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 670 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 671 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 672 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 673 | sub->cgrp_defer_enabled = 0; |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | #else /* !CONFIG_CGROUP_PERF */ |
| 678 | |
| 679 | static inline bool |
| 680 | perf_cgroup_match(struct perf_event *event) |
| 681 | { |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 686 | {} |
| 687 | |
| 688 | static inline int is_cgroup_event(struct perf_event *event) |
| 689 | { |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 694 | { |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 699 | { |
| 700 | } |
| 701 | |
| 702 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 703 | { |
| 704 | } |
| 705 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 706 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 707 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 708 | { |
| 709 | } |
| 710 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 711 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 712 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 713 | { |
| 714 | } |
| 715 | |
| 716 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 717 | struct perf_event_attr *attr, |
| 718 | struct perf_event *group_leader) |
| 719 | { |
| 720 | return -EINVAL; |
| 721 | } |
| 722 | |
| 723 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 724 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 725 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 726 | { |
| 727 | } |
| 728 | |
| 729 | void |
| 730 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 731 | { |
| 732 | } |
| 733 | |
| 734 | static inline void |
| 735 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 736 | { |
| 737 | } |
| 738 | |
| 739 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 740 | { |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static inline void |
| 745 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 746 | { |
| 747 | } |
| 748 | |
| 749 | static inline void |
| 750 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 751 | struct perf_event_context *ctx) |
| 752 | { |
| 753 | } |
| 754 | #endif |
| 755 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 756 | /* |
| 757 | * set default to be dependent on timer tick just |
| 758 | * like original code |
| 759 | */ |
| 760 | #define PERF_CPU_HRTIMER (1000 / HZ) |
| 761 | /* |
| 762 | * function must be called with interrupts disbled |
| 763 | */ |
| 764 | static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr) |
| 765 | { |
| 766 | struct perf_cpu_context *cpuctx; |
| 767 | enum hrtimer_restart ret = HRTIMER_NORESTART; |
| 768 | int rotations = 0; |
| 769 | |
| 770 | WARN_ON(!irqs_disabled()); |
| 771 | |
| 772 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); |
| 773 | |
| 774 | rotations = perf_rotate_context(cpuctx); |
| 775 | |
| 776 | /* |
| 777 | * arm timer if needed |
| 778 | */ |
| 779 | if (rotations) { |
| 780 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
| 781 | ret = HRTIMER_RESTART; |
| 782 | } |
| 783 | |
| 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | /* CPU is going down */ |
| 788 | void perf_cpu_hrtimer_cancel(int cpu) |
| 789 | { |
| 790 | struct perf_cpu_context *cpuctx; |
| 791 | struct pmu *pmu; |
| 792 | unsigned long flags; |
| 793 | |
| 794 | if (WARN_ON(cpu != smp_processor_id())) |
| 795 | return; |
| 796 | |
| 797 | local_irq_save(flags); |
| 798 | |
| 799 | rcu_read_lock(); |
| 800 | |
| 801 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 802 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 803 | |
| 804 | if (pmu->task_ctx_nr == perf_sw_context) |
| 805 | continue; |
| 806 | |
| 807 | hrtimer_cancel(&cpuctx->hrtimer); |
| 808 | } |
| 809 | |
| 810 | rcu_read_unlock(); |
| 811 | |
| 812 | local_irq_restore(flags); |
| 813 | } |
| 814 | |
| 815 | static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
| 816 | { |
| 817 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 818 | struct pmu *pmu = cpuctx->ctx.pmu; |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 819 | int timer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 820 | |
| 821 | /* no multiplexing needed for SW PMU */ |
| 822 | if (pmu->task_ctx_nr == perf_sw_context) |
| 823 | return; |
| 824 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 825 | /* |
| 826 | * check default is sane, if not set then force to |
| 827 | * default interval (1/tick) |
| 828 | */ |
| 829 | timer = pmu->hrtimer_interval_ms; |
| 830 | if (timer < 1) |
| 831 | timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; |
| 832 | |
| 833 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 834 | |
| 835 | hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
| 836 | hr->function = perf_cpu_hrtimer_handler; |
| 837 | } |
| 838 | |
| 839 | static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx) |
| 840 | { |
| 841 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 842 | struct pmu *pmu = cpuctx->ctx.pmu; |
| 843 | |
| 844 | /* not for SW PMU */ |
| 845 | if (pmu->task_ctx_nr == perf_sw_context) |
| 846 | return; |
| 847 | |
| 848 | if (hrtimer_active(hr)) |
| 849 | return; |
| 850 | |
| 851 | if (!hrtimer_callback_running(hr)) |
| 852 | __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval, |
| 853 | 0, HRTIMER_MODE_REL_PINNED, 0); |
| 854 | } |
| 855 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 856 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 857 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 858 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 859 | if (!(*count)++) |
| 860 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 861 | } |
| 862 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 863 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 864 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 865 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 866 | if (!--(*count)) |
| 867 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 870 | static DEFINE_PER_CPU(struct list_head, rotation_list); |
| 871 | |
| 872 | /* |
| 873 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 874 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 875 | * disabled, while rotate_context is called from IRQ context. |
| 876 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 877 | static void perf_pmu_rotate_start(struct pmu *pmu) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 878 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 879 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 880 | struct list_head *head = &__get_cpu_var(rotation_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 881 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 882 | WARN_ON(!irqs_disabled()); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 883 | |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 884 | if (list_empty(&cpuctx->rotation_list)) |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 885 | list_add(&cpuctx->rotation_list, head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | static void get_ctx(struct perf_event_context *ctx) |
| 889 | { |
| 890 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 891 | } |
| 892 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 893 | static void put_ctx(struct perf_event_context *ctx) |
| 894 | { |
| 895 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 896 | if (ctx->parent_ctx) |
| 897 | put_ctx(ctx->parent_ctx); |
| 898 | if (ctx->task) |
| 899 | put_task_struct(ctx->task); |
Lai Jiangshan | cb796ff | 2011-03-18 12:07:41 +0800 | [diff] [blame] | 900 | kfree_rcu(ctx, rcu_head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
| 904 | static void unclone_ctx(struct perf_event_context *ctx) |
| 905 | { |
| 906 | if (ctx->parent_ctx) { |
| 907 | put_ctx(ctx->parent_ctx); |
| 908 | ctx->parent_ctx = NULL; |
| 909 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 910 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 911 | } |
| 912 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 913 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 914 | { |
| 915 | /* |
| 916 | * only top level events have the pid namespace they were created in |
| 917 | */ |
| 918 | if (event->parent) |
| 919 | event = event->parent; |
| 920 | |
| 921 | return task_tgid_nr_ns(p, event->ns); |
| 922 | } |
| 923 | |
| 924 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 925 | { |
| 926 | /* |
| 927 | * only top level events have the pid namespace they were created in |
| 928 | */ |
| 929 | if (event->parent) |
| 930 | event = event->parent; |
| 931 | |
| 932 | return task_pid_nr_ns(p, event->ns); |
| 933 | } |
| 934 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 935 | /* |
| 936 | * If we inherit events we want to return the parent event id |
| 937 | * to userspace. |
| 938 | */ |
| 939 | static u64 primary_event_id(struct perf_event *event) |
| 940 | { |
| 941 | u64 id = event->id; |
| 942 | |
| 943 | if (event->parent) |
| 944 | id = event->parent->id; |
| 945 | |
| 946 | return id; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Get the perf_event_context for a task and lock it. |
| 951 | * This has to cope with with the fact that until it is locked, |
| 952 | * the context could get moved to another task. |
| 953 | */ |
| 954 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 955 | 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] | 956 | { |
| 957 | struct perf_event_context *ctx; |
| 958 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 959 | retry: |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 960 | /* |
| 961 | * One of the few rules of preemptible RCU is that one cannot do |
| 962 | * rcu_read_unlock() while holding a scheduler (or nested) lock when |
| 963 | * part of the read side critical section was preemptible -- see |
| 964 | * rcu_read_unlock_special(). |
| 965 | * |
| 966 | * Since ctx->lock nests under rq->lock we must ensure the entire read |
| 967 | * side critical section is non-preemptible. |
| 968 | */ |
| 969 | preempt_disable(); |
| 970 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 971 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 972 | if (ctx) { |
| 973 | /* |
| 974 | * If this context is a clone of another, it might |
| 975 | * get swapped for another underneath us by |
| 976 | * perf_event_task_sched_out, though the |
| 977 | * rcu_read_lock() protects us from any context |
| 978 | * getting freed. Lock the context and check if it |
| 979 | * got swapped before we could get the lock, and retry |
| 980 | * if so. If we locked the right context, then it |
| 981 | * can't get swapped on us any more. |
| 982 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 983 | raw_spin_lock_irqsave(&ctx->lock, *flags); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 984 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 985 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 986 | rcu_read_unlock(); |
| 987 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 988 | goto retry; |
| 989 | } |
| 990 | |
| 991 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 992 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 993 | ctx = NULL; |
| 994 | } |
| 995 | } |
| 996 | rcu_read_unlock(); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 997 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 998 | return ctx; |
| 999 | } |
| 1000 | |
| 1001 | /* |
| 1002 | * Get the context for a task and increment its pin_count so it |
| 1003 | * can't get swapped to another task. This also increments its |
| 1004 | * reference count so that the context can't get freed. |
| 1005 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1006 | static struct perf_event_context * |
| 1007 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1008 | { |
| 1009 | struct perf_event_context *ctx; |
| 1010 | unsigned long flags; |
| 1011 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1012 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1013 | if (ctx) { |
| 1014 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1015 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1016 | } |
| 1017 | return ctx; |
| 1018 | } |
| 1019 | |
| 1020 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 1021 | { |
| 1022 | unsigned long flags; |
| 1023 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1024 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1025 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1026 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1029 | /* |
| 1030 | * Update the record of the current time in a context. |
| 1031 | */ |
| 1032 | static void update_context_time(struct perf_event_context *ctx) |
| 1033 | { |
| 1034 | u64 now = perf_clock(); |
| 1035 | |
| 1036 | ctx->time += now - ctx->timestamp; |
| 1037 | ctx->timestamp = now; |
| 1038 | } |
| 1039 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1040 | static u64 perf_event_time(struct perf_event *event) |
| 1041 | { |
| 1042 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1043 | |
| 1044 | if (is_cgroup_event(event)) |
| 1045 | return perf_cgroup_event_time(event); |
| 1046 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1047 | return ctx ? ctx->time : 0; |
| 1048 | } |
| 1049 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1050 | /* |
| 1051 | * 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] | 1052 | * The caller of this function needs to hold the ctx->lock. |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1053 | */ |
| 1054 | static void update_event_times(struct perf_event *event) |
| 1055 | { |
| 1056 | struct perf_event_context *ctx = event->ctx; |
| 1057 | u64 run_end; |
| 1058 | |
| 1059 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1060 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1061 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1062 | /* |
| 1063 | * in cgroup mode, time_enabled represents |
| 1064 | * the time the event was enabled AND active |
| 1065 | * tasks were in the monitored cgroup. This is |
| 1066 | * independent of the activity of the context as |
| 1067 | * there may be a mix of cgroup and non-cgroup events. |
| 1068 | * |
| 1069 | * That is why we treat cgroup events differently |
| 1070 | * here. |
| 1071 | */ |
| 1072 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1073 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1074 | else if (ctx->is_active) |
| 1075 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1076 | else |
| 1077 | run_end = event->tstamp_stopped; |
| 1078 | |
| 1079 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1080 | |
| 1081 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1082 | run_end = event->tstamp_stopped; |
| 1083 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1084 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1085 | |
| 1086 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1087 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1088 | } |
| 1089 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1090 | /* |
| 1091 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1092 | */ |
| 1093 | static void update_group_times(struct perf_event *leader) |
| 1094 | { |
| 1095 | struct perf_event *event; |
| 1096 | |
| 1097 | update_event_times(leader); |
| 1098 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1099 | update_event_times(event); |
| 1100 | } |
| 1101 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1102 | static struct list_head * |
| 1103 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1104 | { |
| 1105 | if (event->attr.pinned) |
| 1106 | return &ctx->pinned_groups; |
| 1107 | else |
| 1108 | return &ctx->flexible_groups; |
| 1109 | } |
| 1110 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1111 | /* |
| 1112 | * Add a event from the lists for its context. |
| 1113 | * Must be called with ctx->mutex and ctx->lock held. |
| 1114 | */ |
| 1115 | static void |
| 1116 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1117 | { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1118 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1119 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1120 | |
| 1121 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1122 | * If we're a stand alone event or group leader, we go to the context |
| 1123 | * list, group events are kept attached to the group so that |
| 1124 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1125 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1126 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1127 | struct list_head *list; |
| 1128 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1129 | if (is_software_event(event)) |
| 1130 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 1131 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1132 | list = ctx_group_list(event, ctx); |
| 1133 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1134 | } |
| 1135 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 1136 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1137 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1138 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1139 | if (has_branch_stack(event)) |
| 1140 | ctx->nr_branch_stack++; |
| 1141 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1142 | list_add_rcu(&event->event_entry, &ctx->event_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1143 | if (!ctx->nr_events) |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1144 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1145 | ctx->nr_events++; |
| 1146 | if (event->attr.inherit_stat) |
| 1147 | ctx->nr_stat++; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1148 | |
| 1149 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1152 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1153 | * Initialize event state based on the perf_event_attr::disabled. |
| 1154 | */ |
| 1155 | static inline void perf_event__state_init(struct perf_event *event) |
| 1156 | { |
| 1157 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1158 | PERF_EVENT_STATE_INACTIVE; |
| 1159 | } |
| 1160 | |
| 1161 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1162 | * Called at perf_event creation and when events are attached/detached from a |
| 1163 | * group. |
| 1164 | */ |
| 1165 | static void perf_event__read_size(struct perf_event *event) |
| 1166 | { |
| 1167 | int entry = sizeof(u64); /* value */ |
| 1168 | int size = 0; |
| 1169 | int nr = 1; |
| 1170 | |
| 1171 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1172 | size += sizeof(u64); |
| 1173 | |
| 1174 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1175 | size += sizeof(u64); |
| 1176 | |
| 1177 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1178 | entry += sizeof(u64); |
| 1179 | |
| 1180 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
| 1181 | nr += event->group_leader->nr_siblings; |
| 1182 | size += sizeof(u64); |
| 1183 | } |
| 1184 | |
| 1185 | size += entry * nr; |
| 1186 | event->read_size = size; |
| 1187 | } |
| 1188 | |
| 1189 | static void perf_event__header_size(struct perf_event *event) |
| 1190 | { |
| 1191 | struct perf_sample_data *data; |
| 1192 | u64 sample_type = event->attr.sample_type; |
| 1193 | u16 size = 0; |
| 1194 | |
| 1195 | perf_event__read_size(event); |
| 1196 | |
| 1197 | if (sample_type & PERF_SAMPLE_IP) |
| 1198 | size += sizeof(data->ip); |
| 1199 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1200 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1201 | size += sizeof(data->addr); |
| 1202 | |
| 1203 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1204 | size += sizeof(data->period); |
| 1205 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1206 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1207 | size += sizeof(data->weight); |
| 1208 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1209 | if (sample_type & PERF_SAMPLE_READ) |
| 1210 | size += event->read_size; |
| 1211 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1212 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1213 | size += sizeof(data->data_src.val); |
| 1214 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 1215 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1216 | size += sizeof(data->txn); |
| 1217 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1218 | event->header_size = size; |
| 1219 | } |
| 1220 | |
| 1221 | static void perf_event__id_header_size(struct perf_event *event) |
| 1222 | { |
| 1223 | struct perf_sample_data *data; |
| 1224 | u64 sample_type = event->attr.sample_type; |
| 1225 | u16 size = 0; |
| 1226 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1227 | if (sample_type & PERF_SAMPLE_TID) |
| 1228 | size += sizeof(data->tid_entry); |
| 1229 | |
| 1230 | if (sample_type & PERF_SAMPLE_TIME) |
| 1231 | size += sizeof(data->time); |
| 1232 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 1233 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1234 | size += sizeof(data->id); |
| 1235 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1236 | if (sample_type & PERF_SAMPLE_ID) |
| 1237 | size += sizeof(data->id); |
| 1238 | |
| 1239 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1240 | size += sizeof(data->stream_id); |
| 1241 | |
| 1242 | if (sample_type & PERF_SAMPLE_CPU) |
| 1243 | size += sizeof(data->cpu_entry); |
| 1244 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1245 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1246 | } |
| 1247 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1248 | static void perf_group_attach(struct perf_event *event) |
| 1249 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1250 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1251 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1252 | /* |
| 1253 | * We can have double attach due to group movement in perf_event_open. |
| 1254 | */ |
| 1255 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1256 | return; |
| 1257 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1258 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1259 | |
| 1260 | if (group_leader == event) |
| 1261 | return; |
| 1262 | |
| 1263 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 1264 | !is_software_event(event)) |
| 1265 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 1266 | |
| 1267 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1268 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1269 | |
| 1270 | perf_event__header_size(group_leader); |
| 1271 | |
| 1272 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1273 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1276 | /* |
| 1277 | * Remove a event from the lists for its context. |
| 1278 | * Must be called with ctx->mutex and ctx->lock held. |
| 1279 | */ |
| 1280 | static void |
| 1281 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1282 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1283 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1284 | /* |
| 1285 | * We can have double detach due to exit/hot-unplug + close. |
| 1286 | */ |
| 1287 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1288 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1289 | |
| 1290 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1291 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1292 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1293 | ctx->nr_cgroups--; |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1294 | cpuctx = __get_cpu_context(ctx); |
| 1295 | /* |
| 1296 | * if there are no more cgroup events |
| 1297 | * then cler cgrp to avoid stale pointer |
| 1298 | * in update_cgrp_time_from_cpuctx() |
| 1299 | */ |
| 1300 | if (!ctx->nr_cgroups) |
| 1301 | cpuctx->cgrp = NULL; |
| 1302 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1303 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1304 | if (has_branch_stack(event)) |
| 1305 | ctx->nr_branch_stack--; |
| 1306 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1307 | ctx->nr_events--; |
| 1308 | if (event->attr.inherit_stat) |
| 1309 | ctx->nr_stat--; |
| 1310 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1311 | list_del_rcu(&event->event_entry); |
| 1312 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1313 | if (event->group_leader == event) |
| 1314 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1315 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1316 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1317 | |
| 1318 | /* |
| 1319 | * If event was in error state, then keep it |
| 1320 | * that way, otherwise bogus counts will be |
| 1321 | * returned on read(). The only way to get out |
| 1322 | * of error state is by explicit re-enabling |
| 1323 | * of the event |
| 1324 | */ |
| 1325 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1326 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1327 | |
| 1328 | ctx->generation++; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1329 | } |
| 1330 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1331 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1332 | { |
| 1333 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1334 | struct list_head *list = NULL; |
| 1335 | |
| 1336 | /* |
| 1337 | * We can have double detach due to exit/hot-unplug + close. |
| 1338 | */ |
| 1339 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1340 | return; |
| 1341 | |
| 1342 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1343 | |
| 1344 | /* |
| 1345 | * If this is a sibling, remove it from its group. |
| 1346 | */ |
| 1347 | if (event->group_leader != event) { |
| 1348 | list_del_init(&event->group_entry); |
| 1349 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1350 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | if (!list_empty(&event->group_entry)) |
| 1354 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1355 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1356 | /* |
| 1357 | * If this was a group event with sibling events then |
| 1358 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1359 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1360 | */ |
| 1361 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1362 | if (list) |
| 1363 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1364 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1365 | |
| 1366 | /* Inherit group flags from the previous leader */ |
| 1367 | sibling->group_flags = event->group_flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1368 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1369 | |
| 1370 | out: |
| 1371 | perf_event__header_size(event->group_leader); |
| 1372 | |
| 1373 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1374 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1375 | } |
| 1376 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1377 | static inline int |
| 1378 | event_filter_match(struct perf_event *event) |
| 1379 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1380 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
| 1381 | && perf_cgroup_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1384 | static void |
| 1385 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1386 | struct perf_cpu_context *cpuctx, |
| 1387 | struct perf_event_context *ctx) |
| 1388 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1389 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1390 | u64 delta; |
| 1391 | /* |
| 1392 | * An event which could not be activated because of |
| 1393 | * filter mismatch still needs to have its timings |
| 1394 | * maintained, otherwise bogus information is return |
| 1395 | * via read() for time_enabled, time_running: |
| 1396 | */ |
| 1397 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1398 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1399 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1400 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1401 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1404 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1405 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1406 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1407 | perf_pmu_disable(event->pmu); |
| 1408 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1409 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1410 | if (event->pending_disable) { |
| 1411 | event->pending_disable = 0; |
| 1412 | event->state = PERF_EVENT_STATE_OFF; |
| 1413 | } |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1414 | event->tstamp_stopped = tstamp; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1415 | event->pmu->del(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1416 | event->oncpu = -1; |
| 1417 | |
| 1418 | if (!is_software_event(event)) |
| 1419 | cpuctx->active_oncpu--; |
| 1420 | ctx->nr_active--; |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1421 | if (event->attr.freq && event->attr.sample_freq) |
| 1422 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1423 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1424 | cpuctx->exclusive = 0; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1425 | |
| 1426 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | static void |
| 1430 | group_sched_out(struct perf_event *group_event, |
| 1431 | struct perf_cpu_context *cpuctx, |
| 1432 | struct perf_event_context *ctx) |
| 1433 | { |
| 1434 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1435 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1436 | |
| 1437 | event_sched_out(group_event, cpuctx, ctx); |
| 1438 | |
| 1439 | /* |
| 1440 | * Schedule out siblings (if any): |
| 1441 | */ |
| 1442 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1443 | event_sched_out(event, cpuctx, ctx); |
| 1444 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1445 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1446 | cpuctx->exclusive = 0; |
| 1447 | } |
| 1448 | |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1449 | struct remove_event { |
| 1450 | struct perf_event *event; |
| 1451 | bool detach_group; |
| 1452 | }; |
| 1453 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1454 | /* |
| 1455 | * Cross CPU call to remove a performance event |
| 1456 | * |
| 1457 | * We disable the event on the hardware level first. After that we |
| 1458 | * remove it from the context list. |
| 1459 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1460 | static int __perf_remove_from_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1461 | { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1462 | struct remove_event *re = info; |
| 1463 | struct perf_event *event = re->event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1464 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1465 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1466 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1467 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1468 | event_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1469 | if (re->detach_group) |
| 1470 | perf_group_detach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1471 | list_del_event(event, ctx); |
Peter Zijlstra | 64ce312 | 2011-04-09 21:17:48 +0200 | [diff] [blame] | 1472 | if (!ctx->nr_events && cpuctx->task_ctx == ctx) { |
| 1473 | ctx->is_active = 0; |
| 1474 | cpuctx->task_ctx = NULL; |
| 1475 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1476 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1477 | |
| 1478 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | |
| 1482 | /* |
| 1483 | * Remove the event from a task's (or a CPU's) list of events. |
| 1484 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1485 | * CPU events are removed with a smp call. For task events we only |
| 1486 | * call when the task is on a CPU. |
| 1487 | * |
| 1488 | * If event->ctx is a cloned context, callers must make sure that |
| 1489 | * every task struct that event->ctx->task could possibly point to |
| 1490 | * remains valid. This is OK when called from perf_release since |
| 1491 | * that only calls us on the top-level context, which can't be a clone. |
| 1492 | * When called from perf_event_exit_task, it's OK because the |
| 1493 | * context has been detached from its task. |
| 1494 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1495 | static void perf_remove_from_context(struct perf_event *event, bool detach_group) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1496 | { |
| 1497 | struct perf_event_context *ctx = event->ctx; |
| 1498 | struct task_struct *task = ctx->task; |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1499 | struct remove_event re = { |
| 1500 | .event = event, |
| 1501 | .detach_group = detach_group, |
| 1502 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1503 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1504 | lockdep_assert_held(&ctx->mutex); |
| 1505 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1506 | if (!task) { |
| 1507 | /* |
| 1508 | * Per cpu events are removed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1509 | * the removal is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1510 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1511 | cpu_function_call(event->cpu, __perf_remove_from_context, &re); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | retry: |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1516 | if (!task_function_call(task, __perf_remove_from_context, &re)) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1517 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1518 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1519 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1520 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1521 | * If we failed to find a running task, but find the context active now |
| 1522 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1523 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1524 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1525 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1526 | goto retry; |
| 1527 | } |
| 1528 | |
| 1529 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1530 | * Since the task isn't running, its safe to remove the event, us |
| 1531 | * holding the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1532 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1533 | if (detach_group) |
| 1534 | perf_group_detach(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1535 | list_del_event(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1536 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1537 | } |
| 1538 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1539 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1540 | * Cross CPU call to disable a performance event |
| 1541 | */ |
K.Prasad | 500ad2d | 2012-08-02 13:46:35 +0530 | [diff] [blame] | 1542 | int __perf_event_disable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1543 | { |
| 1544 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1545 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1546 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1547 | |
| 1548 | /* |
| 1549 | * If this is a per-task event, need to check whether this |
| 1550 | * event's task is the current task on this cpu. |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1551 | * |
| 1552 | * Can trigger due to concurrent perf_event_context_sched_out() |
| 1553 | * flipping contexts around. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1554 | */ |
| 1555 | if (ctx->task && cpuctx->task_ctx != ctx) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1556 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1557 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1558 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1559 | |
| 1560 | /* |
| 1561 | * If the event is on, turn it off. |
| 1562 | * If it is in error state, leave it in error state. |
| 1563 | */ |
| 1564 | if (event->state >= PERF_EVENT_STATE_INACTIVE) { |
| 1565 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1566 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1567 | update_group_times(event); |
| 1568 | if (event == event->group_leader) |
| 1569 | group_sched_out(event, cpuctx, ctx); |
| 1570 | else |
| 1571 | event_sched_out(event, cpuctx, ctx); |
| 1572 | event->state = PERF_EVENT_STATE_OFF; |
| 1573 | } |
| 1574 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1575 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1576 | |
| 1577 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | * Disable a event. |
| 1582 | * |
| 1583 | * If event->ctx is a cloned context, callers must make sure that |
| 1584 | * every task struct that event->ctx->task could possibly point to |
| 1585 | * remains valid. This condition is satisifed when called through |
| 1586 | * perf_event_for_each_child or perf_event_for_each because they |
| 1587 | * hold the top-level event's child_mutex, so any descendant that |
| 1588 | * goes to exit will block in sync_child_event. |
| 1589 | * When called from perf_pending_event it's OK because event->ctx |
| 1590 | * is the current context on this CPU and preemption is disabled, |
| 1591 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1592 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 1593 | void perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1594 | { |
| 1595 | struct perf_event_context *ctx = event->ctx; |
| 1596 | struct task_struct *task = ctx->task; |
| 1597 | |
| 1598 | if (!task) { |
| 1599 | /* |
| 1600 | * Disable the event on the cpu that it's on |
| 1601 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1602 | cpu_function_call(event->cpu, __perf_event_disable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1603 | return; |
| 1604 | } |
| 1605 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1606 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1607 | if (!task_function_call(task, __perf_event_disable, event)) |
| 1608 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1609 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1610 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1611 | /* |
| 1612 | * If the event is still active, we need to retry the cross-call. |
| 1613 | */ |
| 1614 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1615 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1616 | /* |
| 1617 | * Reload the task pointer, it might have been changed by |
| 1618 | * a concurrent perf_event_context_sched_out(). |
| 1619 | */ |
| 1620 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1621 | goto retry; |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * Since we have the lock this context can't be scheduled |
| 1626 | * in, so we can change the state safely. |
| 1627 | */ |
| 1628 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 1629 | update_group_times(event); |
| 1630 | event->state = PERF_EVENT_STATE_OFF; |
| 1631 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1632 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1633 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 1634 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1635 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1636 | static void perf_set_shadow_time(struct perf_event *event, |
| 1637 | struct perf_event_context *ctx, |
| 1638 | u64 tstamp) |
| 1639 | { |
| 1640 | /* |
| 1641 | * use the correct time source for the time snapshot |
| 1642 | * |
| 1643 | * We could get by without this by leveraging the |
| 1644 | * fact that to get to this function, the caller |
| 1645 | * has most likely already called update_context_time() |
| 1646 | * and update_cgrp_time_xx() and thus both timestamp |
| 1647 | * are identical (or very close). Given that tstamp is, |
| 1648 | * already adjusted for cgroup, we could say that: |
| 1649 | * tstamp - ctx->timestamp |
| 1650 | * is equivalent to |
| 1651 | * tstamp - cgrp->timestamp. |
| 1652 | * |
| 1653 | * Then, in perf_output_read(), the calculation would |
| 1654 | * work with no changes because: |
| 1655 | * - event is guaranteed scheduled in |
| 1656 | * - no scheduled out in between |
| 1657 | * - thus the timestamp would be the same |
| 1658 | * |
| 1659 | * But this is a bit hairy. |
| 1660 | * |
| 1661 | * So instead, we have an explicit cgroup call to remain |
| 1662 | * within the time time source all along. We believe it |
| 1663 | * is cleaner and simpler to understand. |
| 1664 | */ |
| 1665 | if (is_cgroup_event(event)) |
| 1666 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1667 | else |
| 1668 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1669 | } |
| 1670 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1671 | #define MAX_INTERRUPTS (~0ULL) |
| 1672 | |
| 1673 | static void perf_log_throttle(struct perf_event *event, int enable); |
| 1674 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1675 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1676 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 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 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1680 | u64 tstamp = perf_event_time(event); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1681 | int ret = 0; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1682 | |
Peter Zijlstra | 6334241 | 2014-05-05 11:49:16 +0200 | [diff] [blame] | 1683 | lockdep_assert_held(&ctx->lock); |
| 1684 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1685 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1686 | return 0; |
| 1687 | |
| 1688 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1689 | event->oncpu = smp_processor_id(); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1690 | |
| 1691 | /* |
| 1692 | * Unthrottle events, since we scheduled we might have missed several |
| 1693 | * ticks already, also for a heavily scheduling task there is little |
| 1694 | * guarantee it'll get a tick in a timely manner. |
| 1695 | */ |
| 1696 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1697 | perf_log_throttle(event, 1); |
| 1698 | event->hw.interrupts = 0; |
| 1699 | } |
| 1700 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1701 | /* |
| 1702 | * The new state must be visible before we turn it on in the hardware: |
| 1703 | */ |
| 1704 | smp_wmb(); |
| 1705 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1706 | perf_pmu_disable(event->pmu); |
| 1707 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1708 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1709 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1710 | event->oncpu = -1; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1711 | ret = -EAGAIN; |
| 1712 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1713 | } |
| 1714 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1715 | event->tstamp_running += tstamp - event->tstamp_stopped; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1716 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1717 | perf_set_shadow_time(event, ctx, tstamp); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 1718 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1719 | if (!is_software_event(event)) |
| 1720 | cpuctx->active_oncpu++; |
| 1721 | ctx->nr_active++; |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1722 | if (event->attr.freq && event->attr.sample_freq) |
| 1723 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1724 | |
| 1725 | if (event->attr.exclusive) |
| 1726 | cpuctx->exclusive = 1; |
| 1727 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1728 | out: |
| 1729 | perf_pmu_enable(event->pmu); |
| 1730 | |
| 1731 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | static int |
| 1735 | group_sched_in(struct perf_event *group_event, |
| 1736 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1737 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1738 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1739 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 4a23459 | 2014-02-24 12:43:31 +0100 | [diff] [blame] | 1740 | struct pmu *pmu = ctx->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1741 | u64 now = ctx->time; |
| 1742 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1743 | |
| 1744 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1745 | return 0; |
| 1746 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1747 | pmu->start_txn(pmu); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1748 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1749 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1750 | pmu->cancel_txn(pmu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1751 | perf_cpu_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1752 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1753 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1754 | |
| 1755 | /* |
| 1756 | * Schedule in siblings as one group (if any): |
| 1757 | */ |
| 1758 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1759 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1760 | partial_group = event; |
| 1761 | goto group_error; |
| 1762 | } |
| 1763 | } |
| 1764 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1765 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 1766 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1767 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1768 | group_error: |
| 1769 | /* |
| 1770 | * Groups can be scheduled in as one unit only, so undo any |
| 1771 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1772 | * The events up to the failed event are scheduled out normally, |
| 1773 | * tstamp_stopped will be updated. |
| 1774 | * |
| 1775 | * The failed events and the remaining siblings need to have |
| 1776 | * their timings updated as if they had gone thru event_sched_in() |
| 1777 | * and event_sched_out(). This is required to get consistent timings |
| 1778 | * across the group. This also takes care of the case where the group |
| 1779 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 1780 | * the time the event was actually stopped, such that time delta |
| 1781 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1782 | */ |
| 1783 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 1784 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1785 | simulate = true; |
| 1786 | |
| 1787 | if (simulate) { |
| 1788 | event->tstamp_running += now - event->tstamp_stopped; |
| 1789 | event->tstamp_stopped = now; |
| 1790 | } else { |
| 1791 | event_sched_out(event, cpuctx, ctx); |
| 1792 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1793 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1794 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1795 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1796 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1797 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1798 | perf_cpu_hrtimer_restart(cpuctx); |
| 1799 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1800 | return -EAGAIN; |
| 1801 | } |
| 1802 | |
| 1803 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1804 | * Work out whether we can put this event group on the CPU now. |
| 1805 | */ |
| 1806 | static int group_can_go_on(struct perf_event *event, |
| 1807 | struct perf_cpu_context *cpuctx, |
| 1808 | int can_add_hw) |
| 1809 | { |
| 1810 | /* |
| 1811 | * Groups consisting entirely of software events can always go on. |
| 1812 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1813 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1814 | return 1; |
| 1815 | /* |
| 1816 | * If an exclusive group is already on, no other hardware |
| 1817 | * events can go on. |
| 1818 | */ |
| 1819 | if (cpuctx->exclusive) |
| 1820 | return 0; |
| 1821 | /* |
| 1822 | * If this group is exclusive and there are already |
| 1823 | * events on the CPU, it can't go on. |
| 1824 | */ |
| 1825 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 1826 | return 0; |
| 1827 | /* |
| 1828 | * Otherwise, try to add it if all previous groups were able |
| 1829 | * to go on. |
| 1830 | */ |
| 1831 | return can_add_hw; |
| 1832 | } |
| 1833 | |
| 1834 | static void add_event_to_ctx(struct perf_event *event, |
| 1835 | struct perf_event_context *ctx) |
| 1836 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1837 | u64 tstamp = perf_event_time(event); |
| 1838 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1839 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1840 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1841 | event->tstamp_enabled = tstamp; |
| 1842 | event->tstamp_running = tstamp; |
| 1843 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1844 | } |
| 1845 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1846 | static void task_ctx_sched_out(struct perf_event_context *ctx); |
| 1847 | static void |
| 1848 | ctx_sched_in(struct perf_event_context *ctx, |
| 1849 | struct perf_cpu_context *cpuctx, |
| 1850 | enum event_type_t event_type, |
| 1851 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1852 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 1853 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 1854 | struct perf_event_context *ctx, |
| 1855 | struct task_struct *task) |
| 1856 | { |
| 1857 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 1858 | if (ctx) |
| 1859 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 1860 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 1861 | if (ctx) |
| 1862 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 1863 | } |
| 1864 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1865 | /* |
| 1866 | * Cross CPU call to install and enable a performance event |
| 1867 | * |
| 1868 | * Must be called with ctx->mutex held |
| 1869 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1870 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1871 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1872 | struct perf_event *event = info; |
| 1873 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1874 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1875 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
| 1876 | struct task_struct *task = current; |
| 1877 | |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1878 | perf_ctx_lock(cpuctx, task_ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1879 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1880 | |
| 1881 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1882 | * If there was an active task_ctx schedule it out. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1883 | */ |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1884 | if (task_ctx) |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1885 | task_ctx_sched_out(task_ctx); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1886 | |
| 1887 | /* |
| 1888 | * If the context we're installing events in is not the |
| 1889 | * active task_ctx, flip them. |
| 1890 | */ |
| 1891 | if (ctx->task && task_ctx != ctx) { |
| 1892 | if (task_ctx) |
| 1893 | raw_spin_unlock(&task_ctx->lock); |
| 1894 | raw_spin_lock(&ctx->lock); |
| 1895 | task_ctx = ctx; |
| 1896 | } |
| 1897 | |
| 1898 | if (task_ctx) { |
| 1899 | cpuctx->task_ctx = task_ctx; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1900 | task = task_ctx->task; |
| 1901 | } |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1902 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1903 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1904 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1905 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1906 | /* |
| 1907 | * update cgrp time only if current cgrp |
| 1908 | * matches event->cgrp. Must be done before |
| 1909 | * calling add_event_to_ctx() |
| 1910 | */ |
| 1911 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1912 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1913 | add_event_to_ctx(event, ctx); |
| 1914 | |
| 1915 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1916 | * Schedule everything back in |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1917 | */ |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 1918 | perf_event_sched_in(cpuctx, task_ctx, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1919 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1920 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 1921 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1922 | |
| 1923 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | /* |
| 1927 | * Attach a performance event to a context |
| 1928 | * |
| 1929 | * First we add the event to the list with the hardware enable bit |
| 1930 | * in event->hw_config cleared. |
| 1931 | * |
| 1932 | * If the event is attached to a task which is on a CPU we use a smp |
| 1933 | * call to enable it in the task context. The task might have been |
| 1934 | * scheduled away, but we check this in the smp call again. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1935 | */ |
| 1936 | static void |
| 1937 | perf_install_in_context(struct perf_event_context *ctx, |
| 1938 | struct perf_event *event, |
| 1939 | int cpu) |
| 1940 | { |
| 1941 | struct task_struct *task = ctx->task; |
| 1942 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1943 | lockdep_assert_held(&ctx->mutex); |
| 1944 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 1945 | event->ctx = ctx; |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 1946 | if (event->cpu != -1) |
| 1947 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 1948 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1949 | if (!task) { |
| 1950 | /* |
| 1951 | * Per cpu events are installed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1952 | * the install is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1953 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1954 | cpu_function_call(cpu, __perf_install_in_context, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1955 | return; |
| 1956 | } |
| 1957 | |
| 1958 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1959 | if (!task_function_call(task, __perf_install_in_context, event)) |
| 1960 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1961 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1962 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1963 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1964 | * If we failed to find a running task, but find the context active now |
| 1965 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1966 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1967 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1968 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1969 | goto retry; |
| 1970 | } |
| 1971 | |
| 1972 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1973 | * Since the task isn't running, its safe to add the event, us holding |
| 1974 | * the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1975 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1976 | add_event_to_ctx(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1977 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | * Put a event into inactive state and update time fields. |
| 1982 | * Enabling the leader of a group effectively enables all |
| 1983 | * the group members that aren't explicitly disabled, so we |
| 1984 | * have to update their ->tstamp_enabled also. |
| 1985 | * Note: this works for group members as well as group leaders |
| 1986 | * since the non-leader members' sibling_lists will be empty. |
| 1987 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 1988 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1989 | { |
| 1990 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1991 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1992 | |
| 1993 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1994 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1995 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1996 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 1997 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1998 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | /* |
| 2002 | * Cross CPU call to enable a performance event |
| 2003 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2004 | static int __perf_event_enable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2005 | { |
| 2006 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2007 | struct perf_event_context *ctx = event->ctx; |
| 2008 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2009 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2010 | int err; |
| 2011 | |
Jiri Olsa | 06f4179 | 2013-07-09 17:44:11 +0200 | [diff] [blame] | 2012 | /* |
| 2013 | * There's a time window between 'ctx->is_active' check |
| 2014 | * in perf_event_enable function and this place having: |
| 2015 | * - IRQs on |
| 2016 | * - ctx->lock unlocked |
| 2017 | * |
| 2018 | * where the task could be killed and 'ctx' deactivated |
| 2019 | * by perf_event_exit_task. |
| 2020 | */ |
| 2021 | if (!ctx->is_active) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2022 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2023 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2024 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2025 | update_context_time(ctx); |
| 2026 | |
| 2027 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2028 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2029 | |
| 2030 | /* |
| 2031 | * set current task's cgroup time reference point |
| 2032 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2033 | perf_cgroup_set_timestamp(current, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2034 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2035 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2036 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2037 | if (!event_filter_match(event)) { |
| 2038 | if (is_cgroup_event(event)) |
| 2039 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2040 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2041 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2042 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2043 | /* |
| 2044 | * If the event is in a group and isn't the group leader, |
| 2045 | * then don't put it on unless the group is on. |
| 2046 | */ |
| 2047 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) |
| 2048 | goto unlock; |
| 2049 | |
| 2050 | if (!group_can_go_on(event, cpuctx, 1)) { |
| 2051 | err = -EEXIST; |
| 2052 | } else { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2053 | if (event == leader) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2054 | err = group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2055 | else |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2056 | err = event_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | if (err) { |
| 2060 | /* |
| 2061 | * If this event can't go on and it's part of a |
| 2062 | * group, then the whole group has to come off. |
| 2063 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2064 | if (leader != event) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2065 | group_sched_out(leader, cpuctx, ctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2066 | perf_cpu_hrtimer_restart(cpuctx); |
| 2067 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2068 | if (leader->attr.pinned) { |
| 2069 | update_group_times(leader); |
| 2070 | leader->state = PERF_EVENT_STATE_ERROR; |
| 2071 | } |
| 2072 | } |
| 2073 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2074 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2075 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2076 | |
| 2077 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * Enable a event. |
| 2082 | * |
| 2083 | * If event->ctx is a cloned context, callers must make sure that |
| 2084 | * every task struct that event->ctx->task could possibly point to |
| 2085 | * remains valid. This condition is satisfied when called through |
| 2086 | * perf_event_for_each_child or perf_event_for_each as described |
| 2087 | * for perf_event_disable. |
| 2088 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 2089 | void perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2090 | { |
| 2091 | struct perf_event_context *ctx = event->ctx; |
| 2092 | struct task_struct *task = ctx->task; |
| 2093 | |
| 2094 | if (!task) { |
| 2095 | /* |
| 2096 | * Enable the event on the cpu that it's on |
| 2097 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2098 | cpu_function_call(event->cpu, __perf_event_enable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2099 | return; |
| 2100 | } |
| 2101 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2102 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2103 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2104 | goto out; |
| 2105 | |
| 2106 | /* |
| 2107 | * If the event is in error state, clear that first. |
| 2108 | * That way, if we see the event in error state below, we |
| 2109 | * know that it has gone back into error state, as distinct |
| 2110 | * from the task having been scheduled away before the |
| 2111 | * cross-call arrived. |
| 2112 | */ |
| 2113 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2114 | event->state = PERF_EVENT_STATE_OFF; |
| 2115 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2116 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2117 | if (!ctx->is_active) { |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2118 | __perf_event_mark_enabled(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2119 | goto out; |
| 2120 | } |
| 2121 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2122 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2123 | |
| 2124 | if (!task_function_call(task, __perf_event_enable, event)) |
| 2125 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2126 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2127 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2128 | |
| 2129 | /* |
| 2130 | * If the context is active and the event is still off, |
| 2131 | * we need to retry the cross-call. |
| 2132 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2133 | if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) { |
| 2134 | /* |
| 2135 | * task could have been flipped by a concurrent |
| 2136 | * perf_event_context_sched_out() |
| 2137 | */ |
| 2138 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2139 | goto retry; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2140 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2141 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2142 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2143 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2144 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2145 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2146 | |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2147 | int perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2148 | { |
| 2149 | /* |
| 2150 | * not supported on inherited events |
| 2151 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2152 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2153 | return -EINVAL; |
| 2154 | |
| 2155 | atomic_add(refresh, &event->event_limit); |
| 2156 | perf_event_enable(event); |
| 2157 | |
| 2158 | return 0; |
| 2159 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2160 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2161 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2162 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2163 | struct perf_cpu_context *cpuctx, |
| 2164 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2165 | { |
| 2166 | struct perf_event *event; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2167 | int is_active = ctx->is_active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2168 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2169 | ctx->is_active &= ~event_type; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2170 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2171 | return; |
| 2172 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2173 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2174 | update_cgrp_time_from_cpuctx(cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2175 | if (!ctx->nr_active) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2176 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2177 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2178 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2179 | if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2180 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2181 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2182 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2183 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2184 | if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2185 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2186 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2187 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2188 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | /* |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2192 | * Test whether two contexts are equivalent, i.e. whether they have both been |
| 2193 | * cloned from the same version of the same context. |
| 2194 | * |
| 2195 | * Equivalence is measured using a generation number in the context that is |
| 2196 | * incremented on each modification to it; see unclone_ctx(), list_add_event() |
| 2197 | * and list_del_event(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2198 | */ |
| 2199 | static int context_equiv(struct perf_event_context *ctx1, |
| 2200 | struct perf_event_context *ctx2) |
| 2201 | { |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2202 | /* Pinning disables the swap optimization */ |
| 2203 | if (ctx1->pin_count || ctx2->pin_count) |
| 2204 | return 0; |
| 2205 | |
| 2206 | /* If ctx1 is the parent of ctx2 */ |
| 2207 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) |
| 2208 | return 1; |
| 2209 | |
| 2210 | /* If ctx2 is the parent of ctx1 */ |
| 2211 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) |
| 2212 | return 1; |
| 2213 | |
| 2214 | /* |
| 2215 | * If ctx1 and ctx2 have the same parent; we flatten the parent |
| 2216 | * hierarchy, see perf_event_init_context(). |
| 2217 | */ |
| 2218 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && |
| 2219 | ctx1->parent_gen == ctx2->parent_gen) |
| 2220 | return 1; |
| 2221 | |
| 2222 | /* Unmatched */ |
| 2223 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2224 | } |
| 2225 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2226 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2227 | struct perf_event *next_event) |
| 2228 | { |
| 2229 | u64 value; |
| 2230 | |
| 2231 | if (!event->attr.inherit_stat) |
| 2232 | return; |
| 2233 | |
| 2234 | /* |
| 2235 | * Update the event value, we cannot use perf_event_read() |
| 2236 | * because we're in the middle of a context switch and have IRQs |
| 2237 | * disabled, which upsets smp_call_function_single(), however |
| 2238 | * we know the event must be on the current CPU, therefore we |
| 2239 | * don't need to use it. |
| 2240 | */ |
| 2241 | switch (event->state) { |
| 2242 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2243 | event->pmu->read(event); |
| 2244 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2245 | |
| 2246 | case PERF_EVENT_STATE_INACTIVE: |
| 2247 | update_event_times(event); |
| 2248 | break; |
| 2249 | |
| 2250 | default: |
| 2251 | break; |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * In order to keep per-task stats reliable we need to flip the event |
| 2256 | * values when we flip the contexts. |
| 2257 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2258 | value = local64_read(&next_event->count); |
| 2259 | value = local64_xchg(&event->count, value); |
| 2260 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2261 | |
| 2262 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2263 | swap(event->total_time_running, next_event->total_time_running); |
| 2264 | |
| 2265 | /* |
| 2266 | * Since we swizzled the values, update the user visible data too. |
| 2267 | */ |
| 2268 | perf_event_update_userpage(event); |
| 2269 | perf_event_update_userpage(next_event); |
| 2270 | } |
| 2271 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2272 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2273 | struct perf_event_context *next_ctx) |
| 2274 | { |
| 2275 | struct perf_event *event, *next_event; |
| 2276 | |
| 2277 | if (!ctx->nr_stat) |
| 2278 | return; |
| 2279 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2280 | update_context_time(ctx); |
| 2281 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2282 | event = list_first_entry(&ctx->event_list, |
| 2283 | struct perf_event, event_entry); |
| 2284 | |
| 2285 | next_event = list_first_entry(&next_ctx->event_list, |
| 2286 | struct perf_event, event_entry); |
| 2287 | |
| 2288 | while (&event->event_entry != &ctx->event_list && |
| 2289 | &next_event->event_entry != &next_ctx->event_list) { |
| 2290 | |
| 2291 | __perf_event_sync_stat(event, next_event); |
| 2292 | |
| 2293 | event = list_next_entry(event, event_entry); |
| 2294 | next_event = list_next_entry(next_event, event_entry); |
| 2295 | } |
| 2296 | } |
| 2297 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2298 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2299 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2300 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2301 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2302 | struct perf_event_context *next_ctx; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2303 | struct perf_event_context *parent, *next_parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2304 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2305 | int do_switch = 1; |
| 2306 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2307 | if (likely(!ctx)) |
| 2308 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2309 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2310 | cpuctx = __get_cpu_context(ctx); |
| 2311 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2312 | return; |
| 2313 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2314 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2315 | next_ctx = next->perf_event_ctxp[ctxn]; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2316 | if (!next_ctx) |
| 2317 | goto unlock; |
| 2318 | |
| 2319 | parent = rcu_dereference(ctx->parent_ctx); |
| 2320 | next_parent = rcu_dereference(next_ctx->parent_ctx); |
| 2321 | |
| 2322 | /* If neither context have a parent context; they cannot be clones. */ |
Jiri Olsa | 1f9a726 | 2014-06-24 10:20:25 +0200 | [diff] [blame] | 2323 | if (!parent || !next_parent) |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2324 | goto unlock; |
| 2325 | |
| 2326 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2327 | /* |
| 2328 | * Looks like the two contexts are clones, so we might be |
| 2329 | * able to optimize the context switch. We lock both |
| 2330 | * contexts and check that they are clones under the |
| 2331 | * lock (including re-checking that neither has been |
| 2332 | * uncloned in the meantime). It doesn't matter which |
| 2333 | * order we take the locks because no other cpu could |
| 2334 | * be trying to lock both of these tasks. |
| 2335 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2336 | raw_spin_lock(&ctx->lock); |
| 2337 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2338 | if (context_equiv(ctx, next_ctx)) { |
| 2339 | /* |
| 2340 | * XXX do we need a memory barrier of sorts |
| 2341 | * wrt to rcu_dereference() of perf_event_ctxp |
| 2342 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2343 | task->perf_event_ctxp[ctxn] = next_ctx; |
| 2344 | next->perf_event_ctxp[ctxn] = ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2345 | ctx->task = next; |
| 2346 | next_ctx->task = task; |
| 2347 | do_switch = 0; |
| 2348 | |
| 2349 | perf_event_sync_stat(ctx, next_ctx); |
| 2350 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2351 | raw_spin_unlock(&next_ctx->lock); |
| 2352 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2353 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2354 | unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2355 | rcu_read_unlock(); |
| 2356 | |
| 2357 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2358 | raw_spin_lock(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2359 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2360 | cpuctx->task_ctx = NULL; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2361 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2362 | } |
| 2363 | } |
| 2364 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2365 | #define for_each_task_context_nr(ctxn) \ |
| 2366 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2367 | |
| 2368 | /* |
| 2369 | * Called from scheduler to remove the events of the current task, |
| 2370 | * with interrupts disabled. |
| 2371 | * |
| 2372 | * We stop each event and update the event value in event->count. |
| 2373 | * |
| 2374 | * This does not protect us against NMI, but disable() |
| 2375 | * sets the disabled bit in the control field of event _before_ |
| 2376 | * accessing the event control register. If a NMI hits, then it will |
| 2377 | * not restart the event. |
| 2378 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2379 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2380 | struct task_struct *next) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2381 | { |
| 2382 | int ctxn; |
| 2383 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2384 | for_each_task_context_nr(ctxn) |
| 2385 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2386 | |
| 2387 | /* |
| 2388 | * if cgroup events exist on this CPU, then we need |
| 2389 | * to check if we have to switch out PMU state. |
| 2390 | * cgroup event are system-wide mode only |
| 2391 | */ |
| 2392 | if (atomic_read(&__get_cpu_var(perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2393 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2396 | static void task_ctx_sched_out(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2397 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2398 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2399 | |
| 2400 | if (!cpuctx->task_ctx) |
| 2401 | return; |
| 2402 | |
| 2403 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2404 | return; |
| 2405 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2406 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2407 | cpuctx->task_ctx = NULL; |
| 2408 | } |
| 2409 | |
| 2410 | /* |
| 2411 | * Called with IRQs disabled |
| 2412 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2413 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2414 | enum event_type_t event_type) |
| 2415 | { |
| 2416 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2420 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2421 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2422 | { |
| 2423 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2424 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2425 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2426 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2427 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2428 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2429 | continue; |
| 2430 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2431 | /* may need to reset tstamp_enabled */ |
| 2432 | if (is_cgroup_event(event)) |
| 2433 | perf_cgroup_mark_enabled(event, ctx); |
| 2434 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2435 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2436 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2437 | |
| 2438 | /* |
| 2439 | * If this pinned group hasn't been scheduled, |
| 2440 | * put it in error state. |
| 2441 | */ |
| 2442 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2443 | update_group_times(event); |
| 2444 | event->state = PERF_EVENT_STATE_ERROR; |
| 2445 | } |
| 2446 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | static void |
| 2450 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2451 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2452 | { |
| 2453 | struct perf_event *event; |
| 2454 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2455 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2456 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2457 | /* Ignore events in OFF or ERROR state */ |
| 2458 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2459 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2460 | /* |
| 2461 | * Listen to the 'cpu' scheduling filter constraint |
| 2462 | * of events: |
| 2463 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2464 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2465 | continue; |
| 2466 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2467 | /* may need to reset tstamp_enabled */ |
| 2468 | if (is_cgroup_event(event)) |
| 2469 | perf_cgroup_mark_enabled(event, ctx); |
| 2470 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2471 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2472 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2473 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2474 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2475 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | static void |
| 2479 | ctx_sched_in(struct perf_event_context *ctx, |
| 2480 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2481 | enum event_type_t event_type, |
| 2482 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2483 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2484 | u64 now; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2485 | int is_active = ctx->is_active; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2486 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2487 | ctx->is_active |= event_type; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2488 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2489 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2490 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2491 | now = perf_clock(); |
| 2492 | ctx->timestamp = now; |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2493 | perf_cgroup_set_timestamp(task, ctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2494 | /* |
| 2495 | * First go through the list and put on any pinned groups |
| 2496 | * in order to give them the best chance of going on. |
| 2497 | */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2498 | if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2499 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2500 | |
| 2501 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2502 | if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2503 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2504 | } |
| 2505 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2506 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2507 | enum event_type_t event_type, |
| 2508 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2509 | { |
| 2510 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2511 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2512 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2513 | } |
| 2514 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2515 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2516 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2517 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2518 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2519 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2520 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2521 | if (cpuctx->task_ctx == ctx) |
| 2522 | return; |
| 2523 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2524 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2525 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2526 | /* |
| 2527 | * We want to keep the following priority order: |
| 2528 | * cpu pinned (that don't need to move), task pinned, |
| 2529 | * cpu flexible, task flexible. |
| 2530 | */ |
| 2531 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2532 | |
Gleb Natapov | 1d5f003 | 2011-10-23 19:10:33 +0200 | [diff] [blame] | 2533 | if (ctx->nr_events) |
| 2534 | cpuctx->task_ctx = ctx; |
eranian@google.com | 9b33fa6 | 2010-03-10 22:26:05 -0800 | [diff] [blame] | 2535 | |
Gleb Natapov | 86b47c2 | 2011-11-22 16:08:21 +0200 | [diff] [blame] | 2536 | perf_event_sched_in(cpuctx, cpuctx->task_ctx, task); |
| 2537 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2538 | perf_pmu_enable(ctx->pmu); |
| 2539 | perf_ctx_unlock(cpuctx, ctx); |
| 2540 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2541 | /* |
| 2542 | * Since these rotations are per-cpu, we need to ensure the |
| 2543 | * cpu-context we got scheduled on is actually rotating. |
| 2544 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2545 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2546 | } |
| 2547 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2548 | /* |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2549 | * When sampling the branck stack in system-wide, it may be necessary |
| 2550 | * to flush the stack on context switch. This happens when the branch |
| 2551 | * stack does not tag its entries with the pid of the current task. |
| 2552 | * Otherwise it becomes impossible to associate a branch entry with a |
| 2553 | * task. This ambiguity is more likely to appear when the branch stack |
| 2554 | * supports priv level filtering and the user sets it to monitor only |
| 2555 | * at the user level (which could be a useful measurement in system-wide |
| 2556 | * mode). In that case, the risk is high of having a branch stack with |
| 2557 | * branch from multiple tasks. Flushing may mean dropping the existing |
| 2558 | * entries or stashing them somewhere in the PMU specific code layer. |
| 2559 | * |
| 2560 | * This function provides the context switch callback to the lower code |
| 2561 | * layer. It is invoked ONLY when there is at least one system-wide context |
| 2562 | * with at least one active event using taken branch sampling. |
| 2563 | */ |
| 2564 | static void perf_branch_stack_sched_in(struct task_struct *prev, |
| 2565 | struct task_struct *task) |
| 2566 | { |
| 2567 | struct perf_cpu_context *cpuctx; |
| 2568 | struct pmu *pmu; |
| 2569 | unsigned long flags; |
| 2570 | |
| 2571 | /* no need to flush branch stack if not changing task */ |
| 2572 | if (prev == task) |
| 2573 | return; |
| 2574 | |
| 2575 | local_irq_save(flags); |
| 2576 | |
| 2577 | rcu_read_lock(); |
| 2578 | |
| 2579 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 2580 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2581 | |
| 2582 | /* |
| 2583 | * check if the context has at least one |
| 2584 | * event using PERF_SAMPLE_BRANCH_STACK |
| 2585 | */ |
| 2586 | if (cpuctx->ctx.nr_branch_stack > 0 |
| 2587 | && pmu->flush_branch_stack) { |
| 2588 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2589 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2590 | |
| 2591 | perf_pmu_disable(pmu); |
| 2592 | |
| 2593 | pmu->flush_branch_stack(); |
| 2594 | |
| 2595 | perf_pmu_enable(pmu); |
| 2596 | |
| 2597 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | rcu_read_unlock(); |
| 2602 | |
| 2603 | local_irq_restore(flags); |
| 2604 | } |
| 2605 | |
| 2606 | /* |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2607 | * Called from scheduler to add the events of the current task |
| 2608 | * with interrupts disabled. |
| 2609 | * |
| 2610 | * We restore the event value and then enable it. |
| 2611 | * |
| 2612 | * This does not protect us against NMI, but enable() |
| 2613 | * sets the enabled bit in the control field of event _before_ |
| 2614 | * accessing the event control register. If a NMI hits, then it will |
| 2615 | * keep the event running. |
| 2616 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2617 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 2618 | struct task_struct *task) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2619 | { |
| 2620 | struct perf_event_context *ctx; |
| 2621 | int ctxn; |
| 2622 | |
| 2623 | for_each_task_context_nr(ctxn) { |
| 2624 | ctx = task->perf_event_ctxp[ctxn]; |
| 2625 | if (likely(!ctx)) |
| 2626 | continue; |
| 2627 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2628 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2629 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2630 | /* |
| 2631 | * if cgroup events exist on this CPU, then we need |
| 2632 | * to check if we have to switch in PMU state. |
| 2633 | * cgroup event are system-wide mode only |
| 2634 | */ |
| 2635 | if (atomic_read(&__get_cpu_var(perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2636 | perf_cgroup_sched_in(prev, task); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2637 | |
| 2638 | /* check for system-wide branch_stack events */ |
| 2639 | if (atomic_read(&__get_cpu_var(perf_branch_stack_events))) |
| 2640 | perf_branch_stack_sched_in(prev, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2641 | } |
| 2642 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2643 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 2644 | { |
| 2645 | u64 frequency = event->attr.sample_freq; |
| 2646 | u64 sec = NSEC_PER_SEC; |
| 2647 | u64 divisor, dividend; |
| 2648 | |
| 2649 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 2650 | |
| 2651 | count_fls = fls64(count); |
| 2652 | nsec_fls = fls64(nsec); |
| 2653 | frequency_fls = fls64(frequency); |
| 2654 | sec_fls = 30; |
| 2655 | |
| 2656 | /* |
| 2657 | * We got @count in @nsec, with a target of sample_freq HZ |
| 2658 | * the target period becomes: |
| 2659 | * |
| 2660 | * @count * 10^9 |
| 2661 | * period = ------------------- |
| 2662 | * @nsec * sample_freq |
| 2663 | * |
| 2664 | */ |
| 2665 | |
| 2666 | /* |
| 2667 | * Reduce accuracy by one bit such that @a and @b converge |
| 2668 | * to a similar magnitude. |
| 2669 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2670 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2671 | do { \ |
| 2672 | if (a##_fls > b##_fls) { \ |
| 2673 | a >>= 1; \ |
| 2674 | a##_fls--; \ |
| 2675 | } else { \ |
| 2676 | b >>= 1; \ |
| 2677 | b##_fls--; \ |
| 2678 | } \ |
| 2679 | } while (0) |
| 2680 | |
| 2681 | /* |
| 2682 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 2683 | * the other, so that finally we can do a u64/u64 division. |
| 2684 | */ |
| 2685 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 2686 | REDUCE_FLS(nsec, frequency); |
| 2687 | REDUCE_FLS(sec, count); |
| 2688 | } |
| 2689 | |
| 2690 | if (count_fls + sec_fls > 64) { |
| 2691 | divisor = nsec * frequency; |
| 2692 | |
| 2693 | while (count_fls + sec_fls > 64) { |
| 2694 | REDUCE_FLS(count, sec); |
| 2695 | divisor >>= 1; |
| 2696 | } |
| 2697 | |
| 2698 | dividend = count * sec; |
| 2699 | } else { |
| 2700 | dividend = count * sec; |
| 2701 | |
| 2702 | while (nsec_fls + frequency_fls > 64) { |
| 2703 | REDUCE_FLS(nsec, frequency); |
| 2704 | dividend >>= 1; |
| 2705 | } |
| 2706 | |
| 2707 | divisor = nsec * frequency; |
| 2708 | } |
| 2709 | |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2710 | if (!divisor) |
| 2711 | return dividend; |
| 2712 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2713 | return div64_u64(dividend, divisor); |
| 2714 | } |
| 2715 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2716 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 2717 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 2718 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2719 | 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] | 2720 | { |
| 2721 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2722 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2723 | s64 delta; |
| 2724 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2725 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2726 | |
| 2727 | delta = (s64)(period - hwc->sample_period); |
| 2728 | delta = (delta + 7) / 8; /* low pass filter */ |
| 2729 | |
| 2730 | sample_period = hwc->sample_period + delta; |
| 2731 | |
| 2732 | if (!sample_period) |
| 2733 | sample_period = 1; |
| 2734 | |
| 2735 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2736 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2737 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2738 | if (disable) |
| 2739 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2740 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2741 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2742 | |
| 2743 | if (disable) |
| 2744 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2745 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2746 | } |
| 2747 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2748 | /* |
| 2749 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 2750 | * events. At the same time, make sure, having freq events does not change |
| 2751 | * the rate of unthrottling as that would introduce bias. |
| 2752 | */ |
| 2753 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 2754 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2755 | { |
| 2756 | struct perf_event *event; |
| 2757 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2758 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2759 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2760 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2761 | /* |
| 2762 | * only need to iterate over all events iff: |
| 2763 | * - context have events in frequency mode (needs freq adjust) |
| 2764 | * - there are events to unthrottle on this cpu |
| 2765 | */ |
| 2766 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2767 | return; |
| 2768 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2769 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2770 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2771 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 2772 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2773 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 2774 | continue; |
| 2775 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2776 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 2777 | continue; |
| 2778 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2779 | perf_pmu_disable(event->pmu); |
| 2780 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2781 | hwc = &event->hw; |
| 2782 | |
Jiri Olsa | ae23bff | 2013-08-24 16:45:54 +0200 | [diff] [blame] | 2783 | if (hwc->interrupts == MAX_INTERRUPTS) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2784 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2785 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2786 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | if (!event->attr.freq || !event->attr.sample_freq) |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2790 | goto next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2791 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2792 | /* |
| 2793 | * stop the event and update event->count |
| 2794 | */ |
| 2795 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2796 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2797 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2798 | delta = now - hwc->freq_count_stamp; |
| 2799 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2800 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2801 | /* |
| 2802 | * restart the event |
| 2803 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2804 | * we have stopped the event so tell that |
| 2805 | * to perf_adjust_period() to avoid stopping it |
| 2806 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2807 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2808 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2809 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2810 | |
| 2811 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2812 | next: |
| 2813 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2814 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2815 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2816 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2817 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2818 | } |
| 2819 | |
| 2820 | /* |
| 2821 | * Round-robin a context's events: |
| 2822 | */ |
| 2823 | static void rotate_ctx(struct perf_event_context *ctx) |
| 2824 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 2825 | /* |
| 2826 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 2827 | * disabled by the inheritance code. |
| 2828 | */ |
| 2829 | if (!ctx->rotate_disable) |
| 2830 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2831 | } |
| 2832 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2833 | /* |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2834 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 2835 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 2836 | * disabled, while rotate_context is called from IRQ context. |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2837 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2838 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2839 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2840 | struct perf_event_context *ctx = NULL; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2841 | int rotate = 0, remove = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2842 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2843 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2844 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2845 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 2846 | rotate = 1; |
| 2847 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2848 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2849 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2850 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2851 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2852 | if (ctx->nr_events != ctx->nr_active) |
| 2853 | rotate = 1; |
| 2854 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2855 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2856 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2857 | goto done; |
| 2858 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2859 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2860 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2861 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2862 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2863 | if (ctx) |
| 2864 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 2865 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2866 | rotate_ctx(&cpuctx->ctx); |
| 2867 | if (ctx) |
| 2868 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2869 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2870 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2871 | |
| 2872 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 2873 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2874 | done: |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2875 | if (remove) |
| 2876 | list_del_init(&cpuctx->rotation_list); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2877 | |
| 2878 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2879 | } |
| 2880 | |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2881 | #ifdef CONFIG_NO_HZ_FULL |
| 2882 | bool perf_event_can_stop_tick(void) |
| 2883 | { |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 2884 | if (atomic_read(&nr_freq_events) || |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 2885 | __this_cpu_read(perf_throttled_count)) |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2886 | return false; |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 2887 | else |
| 2888 | return true; |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2889 | } |
| 2890 | #endif |
| 2891 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2892 | void perf_event_task_tick(void) |
| 2893 | { |
| 2894 | struct list_head *head = &__get_cpu_var(rotation_list); |
| 2895 | struct perf_cpu_context *cpuctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2896 | struct perf_event_context *ctx; |
| 2897 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2898 | |
| 2899 | WARN_ON(!irqs_disabled()); |
| 2900 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2901 | __this_cpu_inc(perf_throttled_seq); |
| 2902 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
| 2903 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2904 | list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2905 | ctx = &cpuctx->ctx; |
| 2906 | perf_adjust_freq_unthr_context(ctx, throttled); |
| 2907 | |
| 2908 | ctx = cpuctx->task_ctx; |
| 2909 | if (ctx) |
| 2910 | perf_adjust_freq_unthr_context(ctx, throttled); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2911 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2912 | } |
| 2913 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2914 | static int event_enable_on_exec(struct perf_event *event, |
| 2915 | struct perf_event_context *ctx) |
| 2916 | { |
| 2917 | if (!event->attr.enable_on_exec) |
| 2918 | return 0; |
| 2919 | |
| 2920 | event->attr.enable_on_exec = 0; |
| 2921 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2922 | return 0; |
| 2923 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2924 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2925 | |
| 2926 | return 1; |
| 2927 | } |
| 2928 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2929 | /* |
| 2930 | * Enable all of a task's events that have been marked enable-on-exec. |
| 2931 | * This expects task == current. |
| 2932 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2933 | static void perf_event_enable_on_exec(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2934 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2935 | struct perf_event *event; |
| 2936 | unsigned long flags; |
| 2937 | int enabled = 0; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2938 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2939 | |
| 2940 | local_irq_save(flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2941 | if (!ctx || !ctx->nr_events) |
| 2942 | goto out; |
| 2943 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 2944 | /* |
| 2945 | * We must ctxsw out cgroup events to avoid conflict |
| 2946 | * when invoking perf_task_event_sched_in() later on |
| 2947 | * in this function. Otherwise we end up trying to |
| 2948 | * ctxswin cgroup events which are already scheduled |
| 2949 | * in. |
| 2950 | */ |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2951 | perf_cgroup_sched_out(current, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2952 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2953 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2954 | task_ctx_sched_out(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2955 | |
Peter Zijlstra | b79387e | 2011-11-22 11:25:43 +0100 | [diff] [blame] | 2956 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2957 | ret = event_enable_on_exec(event, ctx); |
| 2958 | if (ret) |
| 2959 | enabled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | /* |
| 2963 | * Unclone this context if we enabled any event. |
| 2964 | */ |
| 2965 | if (enabled) |
| 2966 | unclone_ctx(ctx); |
| 2967 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2968 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2969 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 2970 | /* |
| 2971 | * Also calls ctxswin for cgroup events, if any: |
| 2972 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2973 | perf_event_context_sched_in(ctx, ctx->task); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2974 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2975 | local_irq_restore(flags); |
| 2976 | } |
| 2977 | |
Peter Zijlstra | e041e32 | 2014-05-21 17:32:19 +0200 | [diff] [blame] | 2978 | void perf_event_exec(void) |
| 2979 | { |
| 2980 | struct perf_event_context *ctx; |
| 2981 | int ctxn; |
| 2982 | |
| 2983 | rcu_read_lock(); |
| 2984 | for_each_task_context_nr(ctxn) { |
| 2985 | ctx = current->perf_event_ctxp[ctxn]; |
| 2986 | if (!ctx) |
| 2987 | continue; |
| 2988 | |
| 2989 | perf_event_enable_on_exec(ctx); |
| 2990 | } |
| 2991 | rcu_read_unlock(); |
| 2992 | } |
| 2993 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2994 | /* |
| 2995 | * Cross CPU call to read the hardware event |
| 2996 | */ |
| 2997 | static void __perf_event_read(void *info) |
| 2998 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2999 | struct perf_event *event = info; |
| 3000 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3001 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3002 | |
| 3003 | /* |
| 3004 | * If this is a task context, we need to check whether it is |
| 3005 | * the current task context of this cpu. If not it has been |
| 3006 | * scheduled out before the smp call arrived. In that case |
| 3007 | * event->count would have been updated to a recent sample |
| 3008 | * when the event was scheduled out. |
| 3009 | */ |
| 3010 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 3011 | return; |
| 3012 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3013 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3014 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3015 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3016 | update_cgrp_time_from_event(event); |
| 3017 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3018 | update_event_times(event); |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3019 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
| 3020 | event->pmu->read(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3021 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3022 | } |
| 3023 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3024 | static inline u64 perf_event_count(struct perf_event *event) |
| 3025 | { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3026 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3027 | } |
| 3028 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3029 | static u64 perf_event_read(struct perf_event *event) |
| 3030 | { |
| 3031 | /* |
| 3032 | * If event is enabled and currently active on a CPU, update the |
| 3033 | * value in the event structure: |
| 3034 | */ |
| 3035 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
| 3036 | smp_call_function_single(event->oncpu, |
| 3037 | __perf_event_read, event, 1); |
| 3038 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 3039 | struct perf_event_context *ctx = event->ctx; |
| 3040 | unsigned long flags; |
| 3041 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3042 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3043 | /* |
| 3044 | * may read while context is not active |
| 3045 | * (e.g., thread is blocked), in that case |
| 3046 | * we cannot update context time |
| 3047 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3048 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3049 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3050 | update_cgrp_time_from_event(event); |
| 3051 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3052 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3053 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3054 | } |
| 3055 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3056 | return perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | /* |
| 3060 | * Initialize the perf_event context in a task_struct: |
| 3061 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3062 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3063 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3064 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3065 | mutex_init(&ctx->mutex); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3066 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 3067 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3068 | INIT_LIST_HEAD(&ctx->event_list); |
| 3069 | atomic_set(&ctx->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3070 | } |
| 3071 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3072 | static struct perf_event_context * |
| 3073 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3074 | { |
| 3075 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3076 | |
| 3077 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 3078 | if (!ctx) |
| 3079 | return NULL; |
| 3080 | |
| 3081 | __perf_event_init_context(ctx); |
| 3082 | if (task) { |
| 3083 | ctx->task = task; |
| 3084 | get_task_struct(task); |
| 3085 | } |
| 3086 | ctx->pmu = pmu; |
| 3087 | |
| 3088 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3089 | } |
| 3090 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3091 | static struct task_struct * |
| 3092 | find_lively_task_by_vpid(pid_t vpid) |
| 3093 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3094 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3095 | int err; |
| 3096 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3097 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3098 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3099 | task = current; |
| 3100 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3101 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3102 | if (task) |
| 3103 | get_task_struct(task); |
| 3104 | rcu_read_unlock(); |
| 3105 | |
| 3106 | if (!task) |
| 3107 | return ERR_PTR(-ESRCH); |
| 3108 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3109 | /* Reuse ptrace permission checks for now. */ |
| 3110 | err = -EACCES; |
| 3111 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 3112 | goto errout; |
| 3113 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3114 | return task; |
| 3115 | errout: |
| 3116 | put_task_struct(task); |
| 3117 | return ERR_PTR(err); |
| 3118 | |
| 3119 | } |
| 3120 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3121 | /* |
| 3122 | * Returns a matching context with refcount and pincount. |
| 3123 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3124 | static struct perf_event_context * |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 3125 | find_get_context(struct pmu *pmu, struct task_struct *task, int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3126 | { |
| 3127 | struct perf_event_context *ctx; |
| 3128 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3129 | unsigned long flags; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3130 | int ctxn, err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3131 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3132 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3133 | /* Must be root to operate on a CPU event: */ |
| 3134 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3135 | return ERR_PTR(-EACCES); |
| 3136 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3137 | /* |
| 3138 | * We could be clever and allow to attach a event to an |
| 3139 | * offline CPU and activate it when the CPU comes up, but |
| 3140 | * that's for later. |
| 3141 | */ |
| 3142 | if (!cpu_online(cpu)) |
| 3143 | return ERR_PTR(-ENODEV); |
| 3144 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3145 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3146 | ctx = &cpuctx->ctx; |
| 3147 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3148 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3149 | |
| 3150 | return ctx; |
| 3151 | } |
| 3152 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3153 | err = -EINVAL; |
| 3154 | ctxn = pmu->task_ctx_nr; |
| 3155 | if (ctxn < 0) |
| 3156 | goto errout; |
| 3157 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3158 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3159 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3160 | if (ctx) { |
| 3161 | unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3162 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3163 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3164 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3165 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3166 | err = -ENOMEM; |
| 3167 | if (!ctx) |
| 3168 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3169 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3170 | err = 0; |
| 3171 | mutex_lock(&task->perf_event_mutex); |
| 3172 | /* |
| 3173 | * If it has already passed perf_event_exit_task(). |
| 3174 | * we must see PF_EXITING, it takes this mutex too. |
| 3175 | */ |
| 3176 | if (task->flags & PF_EXITING) |
| 3177 | err = -ESRCH; |
| 3178 | else if (task->perf_event_ctxp[ctxn]) |
| 3179 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3180 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3181 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3182 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3183 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3184 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3185 | mutex_unlock(&task->perf_event_mutex); |
| 3186 | |
| 3187 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3188 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3189 | |
| 3190 | if (err == -EAGAIN) |
| 3191 | goto retry; |
| 3192 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3193 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3194 | } |
| 3195 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3196 | return ctx; |
| 3197 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3198 | errout: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3199 | return ERR_PTR(err); |
| 3200 | } |
| 3201 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3202 | static void perf_event_free_filter(struct perf_event *event); |
| 3203 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3204 | static void free_event_rcu(struct rcu_head *head) |
| 3205 | { |
| 3206 | struct perf_event *event; |
| 3207 | |
| 3208 | event = container_of(head, struct perf_event, rcu_head); |
| 3209 | if (event->ns) |
| 3210 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3211 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3212 | kfree(event); |
| 3213 | } |
| 3214 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3215 | static void ring_buffer_put(struct ring_buffer *rb); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3216 | static void ring_buffer_attach(struct perf_event *event, |
| 3217 | struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3218 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3219 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
| 3220 | { |
| 3221 | if (event->parent) |
| 3222 | return; |
| 3223 | |
| 3224 | if (has_branch_stack(event)) { |
| 3225 | if (!(event->attach_state & PERF_ATTACH_TASK)) |
| 3226 | atomic_dec(&per_cpu(perf_branch_stack_events, cpu)); |
| 3227 | } |
| 3228 | if (is_cgroup_event(event)) |
| 3229 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); |
| 3230 | } |
| 3231 | |
| 3232 | static void unaccount_event(struct perf_event *event) |
| 3233 | { |
| 3234 | if (event->parent) |
| 3235 | return; |
| 3236 | |
| 3237 | if (event->attach_state & PERF_ATTACH_TASK) |
| 3238 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3239 | if (event->attr.mmap || event->attr.mmap_data) |
| 3240 | atomic_dec(&nr_mmap_events); |
| 3241 | if (event->attr.comm) |
| 3242 | atomic_dec(&nr_comm_events); |
| 3243 | if (event->attr.task) |
| 3244 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3245 | if (event->attr.freq) |
| 3246 | atomic_dec(&nr_freq_events); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3247 | if (is_cgroup_event(event)) |
| 3248 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3249 | if (has_branch_stack(event)) |
| 3250 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3251 | |
| 3252 | unaccount_event_cpu(event, event->cpu); |
| 3253 | } |
| 3254 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3255 | static void __free_event(struct perf_event *event) |
| 3256 | { |
| 3257 | if (!event->parent) { |
| 3258 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 3259 | put_callchain_buffers(); |
| 3260 | } |
| 3261 | |
| 3262 | if (event->destroy) |
| 3263 | event->destroy(event); |
| 3264 | |
| 3265 | if (event->ctx) |
| 3266 | put_ctx(event->ctx); |
| 3267 | |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 3268 | if (event->pmu) |
| 3269 | module_put(event->pmu->module); |
| 3270 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3271 | call_rcu(&event->rcu_head, free_event_rcu); |
| 3272 | } |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3273 | |
| 3274 | static void _free_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3275 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3276 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3277 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3278 | unaccount_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3279 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3280 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3281 | /* |
| 3282 | * Can happen when we close an event with re-directed output. |
| 3283 | * |
| 3284 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 3285 | * over us; possibly making our ring_buffer_put() the last. |
| 3286 | */ |
| 3287 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3288 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3289 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3290 | } |
| 3291 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3292 | if (is_cgroup_event(event)) |
| 3293 | perf_detach_cgroup(event); |
| 3294 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3295 | __free_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3296 | } |
| 3297 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3298 | /* |
| 3299 | * Used to free events which have a known refcount of 1, such as in error paths |
| 3300 | * where the event isn't exposed yet and inherited events. |
| 3301 | */ |
| 3302 | static void free_event(struct perf_event *event) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3303 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3304 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
| 3305 | "unexpected event refcount: %ld; ptr=%p\n", |
| 3306 | atomic_long_read(&event->refcount), event)) { |
| 3307 | /* leak to avoid use-after-free */ |
| 3308 | return; |
| 3309 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3310 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3311 | _free_event(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3312 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3313 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3314 | /* |
| 3315 | * Called when the last reference to the file is gone. |
| 3316 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3317 | static void put_event(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3318 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3319 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3320 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3321 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3322 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 3323 | return; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3324 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3325 | rcu_read_lock(); |
| 3326 | owner = ACCESS_ONCE(event->owner); |
| 3327 | /* |
| 3328 | * Matches the smp_wmb() in perf_event_exit_task(). If we observe |
| 3329 | * !owner it means the list deletion is complete and we can indeed |
| 3330 | * free this event, otherwise we need to serialize on |
| 3331 | * owner->perf_event_mutex. |
| 3332 | */ |
| 3333 | smp_read_barrier_depends(); |
| 3334 | if (owner) { |
| 3335 | /* |
| 3336 | * Since delayed_put_task_struct() also drops the last |
| 3337 | * task reference we can safely take a new reference |
| 3338 | * while holding the rcu_read_lock(). |
| 3339 | */ |
| 3340 | get_task_struct(owner); |
| 3341 | } |
| 3342 | rcu_read_unlock(); |
| 3343 | |
| 3344 | if (owner) { |
| 3345 | mutex_lock(&owner->perf_event_mutex); |
| 3346 | /* |
| 3347 | * We have to re-check the event->owner field, if it is cleared |
| 3348 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 3349 | * ensured they're done, and we can proceed with freeing the |
| 3350 | * event. |
| 3351 | */ |
| 3352 | if (event->owner) |
| 3353 | list_del_init(&event->owner_entry); |
| 3354 | mutex_unlock(&owner->perf_event_mutex); |
| 3355 | put_task_struct(owner); |
| 3356 | } |
| 3357 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3358 | WARN_ON_ONCE(ctx->parent_ctx); |
| 3359 | /* |
| 3360 | * There are two ways this annotation is useful: |
| 3361 | * |
| 3362 | * 1) there is a lock recursion from perf_event_exit_task |
| 3363 | * see the comment there. |
| 3364 | * |
| 3365 | * 2) there is a lock-inversion with mmap_sem through |
| 3366 | * perf_event_read_group(), which takes faults while |
| 3367 | * holding ctx->mutex, however this is called after |
| 3368 | * the last filedesc died, so there is no possibility |
| 3369 | * to trigger the AB-BA case. |
| 3370 | */ |
| 3371 | mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING); |
| 3372 | perf_remove_from_context(event, true); |
| 3373 | mutex_unlock(&ctx->mutex); |
| 3374 | |
| 3375 | _free_event(event); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3376 | } |
| 3377 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3378 | int perf_event_release_kernel(struct perf_event *event) |
| 3379 | { |
| 3380 | put_event(event); |
| 3381 | return 0; |
| 3382 | } |
| 3383 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 3384 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3385 | static int perf_release(struct inode *inode, struct file *file) |
| 3386 | { |
| 3387 | put_event(file->private_data); |
| 3388 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3389 | } |
| 3390 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3391 | 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] | 3392 | { |
| 3393 | struct perf_event *child; |
| 3394 | u64 total = 0; |
| 3395 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3396 | *enabled = 0; |
| 3397 | *running = 0; |
| 3398 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3399 | mutex_lock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3400 | total += perf_event_read(event); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3401 | *enabled += event->total_time_enabled + |
| 3402 | atomic64_read(&event->child_total_time_enabled); |
| 3403 | *running += event->total_time_running + |
| 3404 | atomic64_read(&event->child_total_time_running); |
| 3405 | |
| 3406 | list_for_each_entry(child, &event->child_list, child_list) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3407 | total += perf_event_read(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3408 | *enabled += child->total_time_enabled; |
| 3409 | *running += child->total_time_running; |
| 3410 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3411 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3412 | |
| 3413 | return total; |
| 3414 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3415 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3416 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3417 | static int perf_event_read_group(struct perf_event *event, |
| 3418 | u64 read_format, char __user *buf) |
| 3419 | { |
| 3420 | struct perf_event *leader = event->group_leader, *sub; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3421 | int n = 0, size = 0, ret = -EFAULT; |
| 3422 | struct perf_event_context *ctx = leader->ctx; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3423 | u64 values[5]; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3424 | u64 count, enabled, running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3425 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3426 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3427 | count = perf_event_read_value(leader, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3428 | |
| 3429 | values[n++] = 1 + leader->nr_siblings; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3430 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3431 | values[n++] = enabled; |
| 3432 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3433 | values[n++] = running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3434 | values[n++] = count; |
| 3435 | if (read_format & PERF_FORMAT_ID) |
| 3436 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3437 | |
| 3438 | size = n * sizeof(u64); |
| 3439 | |
| 3440 | if (copy_to_user(buf, values, size)) |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3441 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3442 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3443 | ret = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3444 | |
| 3445 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3446 | n = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3447 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3448 | values[n++] = perf_event_read_value(sub, &enabled, &running); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3449 | if (read_format & PERF_FORMAT_ID) |
| 3450 | values[n++] = primary_event_id(sub); |
| 3451 | |
| 3452 | size = n * sizeof(u64); |
| 3453 | |
Stephane Eranian | 184d3da | 2009-11-23 21:40:49 -0800 | [diff] [blame] | 3454 | if (copy_to_user(buf + ret, values, size)) { |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3455 | ret = -EFAULT; |
| 3456 | goto unlock; |
| 3457 | } |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3458 | |
| 3459 | ret += size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3460 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3461 | unlock: |
| 3462 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3463 | |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3464 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | static int perf_event_read_one(struct perf_event *event, |
| 3468 | u64 read_format, char __user *buf) |
| 3469 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3470 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3471 | u64 values[4]; |
| 3472 | int n = 0; |
| 3473 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3474 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 3475 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3476 | values[n++] = enabled; |
| 3477 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3478 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3479 | if (read_format & PERF_FORMAT_ID) |
| 3480 | values[n++] = primary_event_id(event); |
| 3481 | |
| 3482 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 3483 | return -EFAULT; |
| 3484 | |
| 3485 | return n * sizeof(u64); |
| 3486 | } |
| 3487 | |
| 3488 | /* |
| 3489 | * Read the performance event - simple non blocking version for now |
| 3490 | */ |
| 3491 | static ssize_t |
| 3492 | perf_read_hw(struct perf_event *event, char __user *buf, size_t count) |
| 3493 | { |
| 3494 | u64 read_format = event->attr.read_format; |
| 3495 | int ret; |
| 3496 | |
| 3497 | /* |
| 3498 | * Return end-of-file for a read on a event that is in |
| 3499 | * error state (i.e. because it was pinned but it couldn't be |
| 3500 | * scheduled on to the CPU at some point). |
| 3501 | */ |
| 3502 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 3503 | return 0; |
| 3504 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 3505 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3506 | return -ENOSPC; |
| 3507 | |
| 3508 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3509 | if (read_format & PERF_FORMAT_GROUP) |
| 3510 | ret = perf_event_read_group(event, read_format, buf); |
| 3511 | else |
| 3512 | ret = perf_event_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3513 | |
| 3514 | return ret; |
| 3515 | } |
| 3516 | |
| 3517 | static ssize_t |
| 3518 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 3519 | { |
| 3520 | struct perf_event *event = file->private_data; |
| 3521 | |
| 3522 | return perf_read_hw(event, buf, count); |
| 3523 | } |
| 3524 | |
| 3525 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 3526 | { |
| 3527 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3528 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3529 | unsigned int events = POLL_HUP; |
| 3530 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3531 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3532 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 3533 | * 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] | 3534 | */ |
| 3535 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3536 | rb = event->rb; |
| 3537 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3538 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3539 | mutex_unlock(&event->mmap_mutex); |
| 3540 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3541 | poll_wait(file, &event->waitq, wait); |
| 3542 | |
| 3543 | return events; |
| 3544 | } |
| 3545 | |
| 3546 | static void perf_event_reset(struct perf_event *event) |
| 3547 | { |
| 3548 | (void)perf_event_read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3549 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3550 | perf_event_update_userpage(event); |
| 3551 | } |
| 3552 | |
| 3553 | /* |
| 3554 | * Holding the top-level event's child_mutex means that any |
| 3555 | * descendant process that has inherited this event will block |
| 3556 | * in sync_child_event if it goes to exit, thus satisfying the |
| 3557 | * task existence requirements of perf_event_enable/disable. |
| 3558 | */ |
| 3559 | static void perf_event_for_each_child(struct perf_event *event, |
| 3560 | void (*func)(struct perf_event *)) |
| 3561 | { |
| 3562 | struct perf_event *child; |
| 3563 | |
| 3564 | WARN_ON_ONCE(event->ctx->parent_ctx); |
| 3565 | mutex_lock(&event->child_mutex); |
| 3566 | func(event); |
| 3567 | list_for_each_entry(child, &event->child_list, child_list) |
| 3568 | func(child); |
| 3569 | mutex_unlock(&event->child_mutex); |
| 3570 | } |
| 3571 | |
| 3572 | static void perf_event_for_each(struct perf_event *event, |
| 3573 | void (*func)(struct perf_event *)) |
| 3574 | { |
| 3575 | struct perf_event_context *ctx = event->ctx; |
| 3576 | struct perf_event *sibling; |
| 3577 | |
| 3578 | WARN_ON_ONCE(ctx->parent_ctx); |
| 3579 | mutex_lock(&ctx->mutex); |
| 3580 | event = event->group_leader; |
| 3581 | |
| 3582 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3583 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 3584 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3585 | mutex_unlock(&ctx->mutex); |
| 3586 | } |
| 3587 | |
| 3588 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 3589 | { |
| 3590 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3591 | int ret = 0, active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3592 | u64 value; |
| 3593 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 3594 | if (!is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3595 | return -EINVAL; |
| 3596 | |
John Blackwood | ad0cf34 | 2010-09-28 18:03:11 -0400 | [diff] [blame] | 3597 | if (copy_from_user(&value, arg, sizeof(value))) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3598 | return -EFAULT; |
| 3599 | |
| 3600 | if (!value) |
| 3601 | return -EINVAL; |
| 3602 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3603 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3604 | if (event->attr.freq) { |
| 3605 | if (value > sysctl_perf_event_sample_rate) { |
| 3606 | ret = -EINVAL; |
| 3607 | goto unlock; |
| 3608 | } |
| 3609 | |
| 3610 | event->attr.sample_freq = value; |
| 3611 | } else { |
| 3612 | event->attr.sample_period = value; |
| 3613 | event->hw.sample_period = value; |
| 3614 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3615 | |
| 3616 | active = (event->state == PERF_EVENT_STATE_ACTIVE); |
| 3617 | if (active) { |
| 3618 | perf_pmu_disable(ctx->pmu); |
| 3619 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3620 | } |
| 3621 | |
| 3622 | local64_set(&event->hw.period_left, 0); |
| 3623 | |
| 3624 | if (active) { |
| 3625 | event->pmu->start(event, PERF_EF_RELOAD); |
| 3626 | perf_pmu_enable(ctx->pmu); |
| 3627 | } |
| 3628 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3629 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3630 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3631 | |
| 3632 | return ret; |
| 3633 | } |
| 3634 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3635 | static const struct file_operations perf_fops; |
| 3636 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3637 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3638 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3639 | struct fd f = fdget(fd); |
| 3640 | if (!f.file) |
| 3641 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3642 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3643 | if (f.file->f_op != &perf_fops) { |
| 3644 | fdput(f); |
| 3645 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3646 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3647 | *p = f; |
| 3648 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | static int perf_event_set_output(struct perf_event *event, |
| 3652 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3653 | 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] | 3654 | |
| 3655 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 3656 | { |
| 3657 | struct perf_event *event = file->private_data; |
| 3658 | void (*func)(struct perf_event *); |
| 3659 | u32 flags = arg; |
| 3660 | |
| 3661 | switch (cmd) { |
| 3662 | case PERF_EVENT_IOC_ENABLE: |
| 3663 | func = perf_event_enable; |
| 3664 | break; |
| 3665 | case PERF_EVENT_IOC_DISABLE: |
| 3666 | func = perf_event_disable; |
| 3667 | break; |
| 3668 | case PERF_EVENT_IOC_RESET: |
| 3669 | func = perf_event_reset; |
| 3670 | break; |
| 3671 | |
| 3672 | case PERF_EVENT_IOC_REFRESH: |
| 3673 | return perf_event_refresh(event, arg); |
| 3674 | |
| 3675 | case PERF_EVENT_IOC_PERIOD: |
| 3676 | return perf_event_period(event, (u64 __user *)arg); |
| 3677 | |
Jiri Olsa | cf4957f | 2012-10-24 13:37:58 +0200 | [diff] [blame] | 3678 | case PERF_EVENT_IOC_ID: |
| 3679 | { |
| 3680 | u64 id = primary_event_id(event); |
| 3681 | |
| 3682 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) |
| 3683 | return -EFAULT; |
| 3684 | return 0; |
| 3685 | } |
| 3686 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3687 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3688 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3689 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3690 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3691 | struct perf_event *output_event; |
| 3692 | struct fd output; |
| 3693 | ret = perf_fget_light(arg, &output); |
| 3694 | if (ret) |
| 3695 | return ret; |
| 3696 | output_event = output.file->private_data; |
| 3697 | ret = perf_event_set_output(event, output_event); |
| 3698 | fdput(output); |
| 3699 | } else { |
| 3700 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3701 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3702 | return ret; |
| 3703 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3704 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3705 | case PERF_EVENT_IOC_SET_FILTER: |
| 3706 | return perf_event_set_filter(event, (void __user *)arg); |
| 3707 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3708 | default: |
| 3709 | return -ENOTTY; |
| 3710 | } |
| 3711 | |
| 3712 | if (flags & PERF_IOC_FLAG_GROUP) |
| 3713 | perf_event_for_each(event, func); |
| 3714 | else |
| 3715 | perf_event_for_each_child(event, func); |
| 3716 | |
| 3717 | return 0; |
| 3718 | } |
| 3719 | |
| 3720 | int perf_event_task_enable(void) |
| 3721 | { |
| 3722 | struct perf_event *event; |
| 3723 | |
| 3724 | mutex_lock(¤t->perf_event_mutex); |
| 3725 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3726 | perf_event_for_each_child(event, perf_event_enable); |
| 3727 | mutex_unlock(¤t->perf_event_mutex); |
| 3728 | |
| 3729 | return 0; |
| 3730 | } |
| 3731 | |
| 3732 | int perf_event_task_disable(void) |
| 3733 | { |
| 3734 | struct perf_event *event; |
| 3735 | |
| 3736 | mutex_lock(¤t->perf_event_mutex); |
| 3737 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3738 | perf_event_for_each_child(event, perf_event_disable); |
| 3739 | mutex_unlock(¤t->perf_event_mutex); |
| 3740 | |
| 3741 | return 0; |
| 3742 | } |
| 3743 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3744 | static int perf_event_index(struct perf_event *event) |
| 3745 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 3746 | if (event->hw.state & PERF_HES_STOPPED) |
| 3747 | return 0; |
| 3748 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3749 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 3750 | return 0; |
| 3751 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 3752 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3753 | } |
| 3754 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3755 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3756 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3757 | u64 *enabled, |
| 3758 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3759 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3760 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3761 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3762 | *now = perf_clock(); |
| 3763 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3764 | *enabled = ctx_time - event->tstamp_enabled; |
| 3765 | *running = ctx_time - event->tstamp_running; |
| 3766 | } |
| 3767 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 3768 | static void perf_event_init_userpage(struct perf_event *event) |
| 3769 | { |
| 3770 | struct perf_event_mmap_page *userpg; |
| 3771 | struct ring_buffer *rb; |
| 3772 | |
| 3773 | rcu_read_lock(); |
| 3774 | rb = rcu_dereference(event->rb); |
| 3775 | if (!rb) |
| 3776 | goto unlock; |
| 3777 | |
| 3778 | userpg = rb->user_page; |
| 3779 | |
| 3780 | /* Allow new userspace to detect that bit 0 is deprecated */ |
| 3781 | userpg->cap_bit0_is_deprecated = 1; |
| 3782 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); |
| 3783 | |
| 3784 | unlock: |
| 3785 | rcu_read_unlock(); |
| 3786 | } |
| 3787 | |
Peter Zijlstra | c720620 | 2012-03-22 17:26:36 +0100 | [diff] [blame] | 3788 | 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] | 3789 | { |
| 3790 | } |
| 3791 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3792 | /* |
| 3793 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 3794 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 3795 | * code calls this from NMI context. |
| 3796 | */ |
| 3797 | void perf_event_update_userpage(struct perf_event *event) |
| 3798 | { |
| 3799 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3800 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3801 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3802 | |
| 3803 | rcu_read_lock(); |
Peter Zijlstra | 5ec4c59 | 2013-08-02 21:16:30 +0200 | [diff] [blame] | 3804 | rb = rcu_dereference(event->rb); |
| 3805 | if (!rb) |
| 3806 | goto unlock; |
| 3807 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3808 | /* |
| 3809 | * compute total_time_enabled, total_time_running |
| 3810 | * based on snapshot values taken when the event |
| 3811 | * was last scheduled in. |
| 3812 | * |
| 3813 | * we cannot simply called update_context_time() |
| 3814 | * because of locking issue as we can be called in |
| 3815 | * NMI context |
| 3816 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3817 | calc_timer_values(event, &now, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3818 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3819 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3820 | /* |
| 3821 | * Disable preemption so as to not let the corresponding user-space |
| 3822 | * spin too long if we get preempted. |
| 3823 | */ |
| 3824 | preempt_disable(); |
| 3825 | ++userpg->lock; |
| 3826 | barrier(); |
| 3827 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3828 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 3829 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3830 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3831 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3832 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3833 | atomic64_read(&event->child_total_time_enabled); |
| 3834 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3835 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3836 | atomic64_read(&event->child_total_time_running); |
| 3837 | |
Peter Zijlstra | c720620 | 2012-03-22 17:26:36 +0100 | [diff] [blame] | 3838 | arch_perf_update_userpage(userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3839 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3840 | barrier(); |
| 3841 | ++userpg->lock; |
| 3842 | preempt_enable(); |
| 3843 | unlock: |
| 3844 | rcu_read_unlock(); |
| 3845 | } |
| 3846 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3847 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 3848 | { |
| 3849 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3850 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3851 | int ret = VM_FAULT_SIGBUS; |
| 3852 | |
| 3853 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 3854 | if (vmf->pgoff == 0) |
| 3855 | ret = 0; |
| 3856 | return ret; |
| 3857 | } |
| 3858 | |
| 3859 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3860 | rb = rcu_dereference(event->rb); |
| 3861 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3862 | goto unlock; |
| 3863 | |
| 3864 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 3865 | goto unlock; |
| 3866 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3867 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3868 | if (!vmf->page) |
| 3869 | goto unlock; |
| 3870 | |
| 3871 | get_page(vmf->page); |
| 3872 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 3873 | vmf->page->index = vmf->pgoff; |
| 3874 | |
| 3875 | ret = 0; |
| 3876 | unlock: |
| 3877 | rcu_read_unlock(); |
| 3878 | |
| 3879 | return ret; |
| 3880 | } |
| 3881 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3882 | static void ring_buffer_attach(struct perf_event *event, |
| 3883 | struct ring_buffer *rb) |
| 3884 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3885 | struct ring_buffer *old_rb = NULL; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3886 | unsigned long flags; |
| 3887 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3888 | if (event->rb) { |
| 3889 | /* |
| 3890 | * Should be impossible, we set this when removing |
| 3891 | * event->rb_entry and wait/clear when adding event->rb_entry. |
| 3892 | */ |
| 3893 | WARN_ON_ONCE(event->rcu_pending); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3894 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3895 | old_rb = event->rb; |
| 3896 | event->rcu_batches = get_state_synchronize_rcu(); |
| 3897 | event->rcu_pending = 1; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3898 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3899 | spin_lock_irqsave(&old_rb->event_lock, flags); |
| 3900 | list_del_rcu(&event->rb_entry); |
| 3901 | spin_unlock_irqrestore(&old_rb->event_lock, flags); |
| 3902 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3903 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3904 | if (event->rcu_pending && rb) { |
| 3905 | cond_synchronize_rcu(event->rcu_batches); |
| 3906 | event->rcu_pending = 0; |
| 3907 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3908 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3909 | if (rb) { |
| 3910 | spin_lock_irqsave(&rb->event_lock, flags); |
| 3911 | list_add_rcu(&event->rb_entry, &rb->event_list); |
| 3912 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 3913 | } |
| 3914 | |
| 3915 | rcu_assign_pointer(event->rb, rb); |
| 3916 | |
| 3917 | if (old_rb) { |
| 3918 | ring_buffer_put(old_rb); |
| 3919 | /* |
| 3920 | * Since we detached before setting the new rb, so that we |
| 3921 | * could attach the new rb, we could have missed a wakeup. |
| 3922 | * Provide it now. |
| 3923 | */ |
| 3924 | wake_up_all(&event->waitq); |
| 3925 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | static void ring_buffer_wakeup(struct perf_event *event) |
| 3929 | { |
| 3930 | struct ring_buffer *rb; |
| 3931 | |
| 3932 | rcu_read_lock(); |
| 3933 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3934 | if (rb) { |
| 3935 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 3936 | wake_up_all(&event->waitq); |
| 3937 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3938 | rcu_read_unlock(); |
| 3939 | } |
| 3940 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3941 | static void rb_free_rcu(struct rcu_head *rcu_head) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3942 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3943 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3944 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3945 | rb = container_of(rcu_head, struct ring_buffer, rcu_head); |
| 3946 | rb_free(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3947 | } |
| 3948 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3949 | static struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3950 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3951 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3952 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3953 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3954 | rb = rcu_dereference(event->rb); |
| 3955 | if (rb) { |
| 3956 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 3957 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3958 | } |
| 3959 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3960 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3961 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3962 | } |
| 3963 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3964 | static void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3965 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3966 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3967 | return; |
| 3968 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3969 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3970 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3971 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 3975 | { |
| 3976 | struct perf_event *event = vma->vm_file->private_data; |
| 3977 | |
| 3978 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3979 | atomic_inc(&event->rb->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3980 | } |
| 3981 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3982 | /* |
| 3983 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 3984 | * event, or through other events by use of perf_event_set_output(). |
| 3985 | * |
| 3986 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 3987 | * the buffer here, where we still have a VM context. This means we need |
| 3988 | * to detach all events redirecting to us. |
| 3989 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3990 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 3991 | { |
| 3992 | struct perf_event *event = vma->vm_file->private_data; |
| 3993 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3994 | struct ring_buffer *rb = ring_buffer_get(event); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3995 | struct user_struct *mmap_user = rb->mmap_user; |
| 3996 | int mmap_locked = rb->mmap_locked; |
| 3997 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3998 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3999 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4000 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4001 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4002 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4003 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4004 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4005 | mutex_unlock(&event->mmap_mutex); |
| 4006 | |
| 4007 | /* If there's still other mmap()s of this buffer, we're done. */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4008 | if (atomic_read(&rb->mmap_count)) |
| 4009 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4010 | |
| 4011 | /* |
| 4012 | * No other mmap()s, detach from all other events that might redirect |
| 4013 | * into the now unreachable buffer. Somewhat complicated by the |
| 4014 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 4015 | */ |
| 4016 | again: |
| 4017 | rcu_read_lock(); |
| 4018 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 4019 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 4020 | /* |
| 4021 | * This event is en-route to free_event() which will |
| 4022 | * detach it and remove it from the list. |
| 4023 | */ |
| 4024 | continue; |
| 4025 | } |
| 4026 | rcu_read_unlock(); |
| 4027 | |
| 4028 | mutex_lock(&event->mmap_mutex); |
| 4029 | /* |
| 4030 | * Check we didn't race with perf_event_set_output() which can |
| 4031 | * swizzle the rb from under us while we were waiting to |
| 4032 | * acquire mmap_mutex. |
| 4033 | * |
| 4034 | * If we find a different rb; ignore this event, a next |
| 4035 | * iteration will no longer find it on the list. We have to |
| 4036 | * still restart the iteration to make sure we're not now |
| 4037 | * iterating the wrong list. |
| 4038 | */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4039 | if (event->rb == rb) |
| 4040 | ring_buffer_attach(event, NULL); |
| 4041 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4042 | mutex_unlock(&event->mmap_mutex); |
| 4043 | put_event(event); |
| 4044 | |
| 4045 | /* |
| 4046 | * Restart the iteration; either we're on the wrong list or |
| 4047 | * destroyed its integrity by doing a deletion. |
| 4048 | */ |
| 4049 | goto again; |
| 4050 | } |
| 4051 | rcu_read_unlock(); |
| 4052 | |
| 4053 | /* |
| 4054 | * It could be there's still a few 0-ref events on the list; they'll |
| 4055 | * get cleaned up by free_event() -- they'll also still have their |
| 4056 | * ref on the rb and will free it whenever they are done with it. |
| 4057 | * |
| 4058 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 4059 | * undo the VM accounting. |
| 4060 | */ |
| 4061 | |
| 4062 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 4063 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 4064 | free_uid(mmap_user); |
| 4065 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4066 | out_put: |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4067 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4068 | } |
| 4069 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 4070 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4071 | .open = perf_mmap_open, |
| 4072 | .close = perf_mmap_close, |
| 4073 | .fault = perf_mmap_fault, |
| 4074 | .page_mkwrite = perf_mmap_fault, |
| 4075 | }; |
| 4076 | |
| 4077 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 4078 | { |
| 4079 | struct perf_event *event = file->private_data; |
| 4080 | unsigned long user_locked, user_lock_limit; |
| 4081 | struct user_struct *user = current_user(); |
| 4082 | unsigned long locked, lock_limit; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4083 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4084 | unsigned long vma_size; |
| 4085 | unsigned long nr_pages; |
| 4086 | long user_extra, extra; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4087 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4088 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4089 | /* |
| 4090 | * Don't allow mmap() of inherited per-task counters. This would |
| 4091 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4092 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4093 | */ |
| 4094 | if (event->cpu == -1 && event->attr.inherit) |
| 4095 | return -EINVAL; |
| 4096 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4097 | if (!(vma->vm_flags & VM_SHARED)) |
| 4098 | return -EINVAL; |
| 4099 | |
| 4100 | vma_size = vma->vm_end - vma->vm_start; |
| 4101 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 4102 | |
| 4103 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4104 | * 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] | 4105 | * can do bitmasks instead of modulo. |
| 4106 | */ |
| 4107 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
| 4108 | return -EINVAL; |
| 4109 | |
| 4110 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 4111 | return -EINVAL; |
| 4112 | |
| 4113 | if (vma->vm_pgoff != 0) |
| 4114 | return -EINVAL; |
| 4115 | |
| 4116 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4117 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4118 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4119 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4120 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4121 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4122 | goto unlock; |
| 4123 | } |
| 4124 | |
| 4125 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 4126 | /* |
| 4127 | * Raced against perf_mmap_close() through |
| 4128 | * perf_event_set_output(). Try again, hope for better |
| 4129 | * luck. |
| 4130 | */ |
| 4131 | mutex_unlock(&event->mmap_mutex); |
| 4132 | goto again; |
| 4133 | } |
| 4134 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4135 | goto unlock; |
| 4136 | } |
| 4137 | |
| 4138 | user_extra = nr_pages + 1; |
| 4139 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 4140 | |
| 4141 | /* |
| 4142 | * Increase the limit linearly with more CPUs: |
| 4143 | */ |
| 4144 | user_lock_limit *= num_online_cpus(); |
| 4145 | |
| 4146 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 4147 | |
| 4148 | extra = 0; |
| 4149 | if (user_locked > user_lock_limit) |
| 4150 | extra = user_locked - user_lock_limit; |
| 4151 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 4152 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4153 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 4154 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4155 | |
| 4156 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 4157 | !capable(CAP_IPC_LOCK)) { |
| 4158 | ret = -EPERM; |
| 4159 | goto unlock; |
| 4160 | } |
| 4161 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4162 | WARN_ON(event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4163 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4164 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4165 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4166 | |
Vince Weaver | 4ec8363 | 2011-06-01 15:15:36 -0400 | [diff] [blame] | 4167 | rb = rb_alloc(nr_pages, |
| 4168 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 4169 | event->cpu, flags); |
| 4170 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4171 | if (!rb) { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4172 | ret = -ENOMEM; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4173 | goto unlock; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4174 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4175 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4176 | atomic_set(&rb->mmap_count, 1); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4177 | rb->mmap_locked = extra; |
| 4178 | rb->mmap_user = get_current_user(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4179 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4180 | atomic_long_add(user_extra, &user->locked_vm); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4181 | vma->vm_mm->pinned_vm += extra; |
| 4182 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4183 | ring_buffer_attach(event, rb); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4184 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4185 | perf_event_init_userpage(event); |
Peter Zijlstra | 9a0f05c | 2011-11-21 15:13:29 +0100 | [diff] [blame] | 4186 | perf_event_update_userpage(event); |
| 4187 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4188 | unlock: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4189 | if (!ret) |
| 4190 | atomic_inc(&event->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4191 | mutex_unlock(&event->mmap_mutex); |
| 4192 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4193 | /* |
| 4194 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 4195 | * vma. |
| 4196 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4197 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4198 | vma->vm_ops = &perf_mmap_vmops; |
| 4199 | |
| 4200 | return ret; |
| 4201 | } |
| 4202 | |
| 4203 | static int perf_fasync(int fd, struct file *filp, int on) |
| 4204 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4205 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4206 | struct perf_event *event = filp->private_data; |
| 4207 | int retval; |
| 4208 | |
| 4209 | mutex_lock(&inode->i_mutex); |
| 4210 | retval = fasync_helper(fd, filp, on, &event->fasync); |
| 4211 | mutex_unlock(&inode->i_mutex); |
| 4212 | |
| 4213 | if (retval < 0) |
| 4214 | return retval; |
| 4215 | |
| 4216 | return 0; |
| 4217 | } |
| 4218 | |
| 4219 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 4220 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4221 | .release = perf_release, |
| 4222 | .read = perf_read, |
| 4223 | .poll = perf_poll, |
| 4224 | .unlocked_ioctl = perf_ioctl, |
| 4225 | .compat_ioctl = perf_ioctl, |
| 4226 | .mmap = perf_mmap, |
| 4227 | .fasync = perf_fasync, |
| 4228 | }; |
| 4229 | |
| 4230 | /* |
| 4231 | * Perf event wakeup |
| 4232 | * |
| 4233 | * If there's data, ensure we set the poll() state and publish everything |
| 4234 | * to user-space before waking everybody up. |
| 4235 | */ |
| 4236 | |
| 4237 | void perf_event_wakeup(struct perf_event *event) |
| 4238 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4239 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4240 | |
| 4241 | if (event->pending_kill) { |
| 4242 | kill_fasync(&event->fasync, SIGIO, event->pending_kill); |
| 4243 | event->pending_kill = 0; |
| 4244 | } |
| 4245 | } |
| 4246 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 4247 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4248 | { |
| 4249 | struct perf_event *event = container_of(entry, |
| 4250 | struct perf_event, pending); |
| 4251 | |
| 4252 | if (event->pending_disable) { |
| 4253 | event->pending_disable = 0; |
| 4254 | __perf_event_disable(event); |
| 4255 | } |
| 4256 | |
| 4257 | if (event->pending_wakeup) { |
| 4258 | event->pending_wakeup = 0; |
| 4259 | perf_event_wakeup(event); |
| 4260 | } |
| 4261 | } |
| 4262 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4263 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 4264 | * We assume there is only KVM supporting the callbacks. |
| 4265 | * Later on, we might change it to a list if there is |
| 4266 | * another virtualization implementation supporting the callbacks. |
| 4267 | */ |
| 4268 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 4269 | |
| 4270 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4271 | { |
| 4272 | perf_guest_cbs = cbs; |
| 4273 | return 0; |
| 4274 | } |
| 4275 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 4276 | |
| 4277 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4278 | { |
| 4279 | perf_guest_cbs = NULL; |
| 4280 | return 0; |
| 4281 | } |
| 4282 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 4283 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4284 | static void |
| 4285 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 4286 | struct pt_regs *regs, u64 mask) |
| 4287 | { |
| 4288 | int bit; |
| 4289 | |
| 4290 | for_each_set_bit(bit, (const unsigned long *) &mask, |
| 4291 | sizeof(mask) * BITS_PER_BYTE) { |
| 4292 | u64 val; |
| 4293 | |
| 4294 | val = perf_reg_value(regs, bit); |
| 4295 | perf_output_put(handle, val); |
| 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | static void perf_sample_regs_user(struct perf_regs_user *regs_user, |
| 4300 | struct pt_regs *regs) |
| 4301 | { |
| 4302 | if (!user_mode(regs)) { |
| 4303 | if (current->mm) |
| 4304 | regs = task_pt_regs(current); |
| 4305 | else |
| 4306 | regs = NULL; |
| 4307 | } |
| 4308 | |
| 4309 | if (regs) { |
| 4310 | regs_user->regs = regs; |
| 4311 | regs_user->abi = perf_reg_abi(current); |
| 4312 | } |
| 4313 | } |
| 4314 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4315 | /* |
| 4316 | * Get remaining task size from user stack pointer. |
| 4317 | * |
| 4318 | * It'd be better to take stack vma map and limit this more |
| 4319 | * precisly, but there's no way to get it safely under interrupt, |
| 4320 | * so using TASK_SIZE as limit. |
| 4321 | */ |
| 4322 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 4323 | { |
| 4324 | unsigned long addr = perf_user_stack_pointer(regs); |
| 4325 | |
| 4326 | if (!addr || addr >= TASK_SIZE) |
| 4327 | return 0; |
| 4328 | |
| 4329 | return TASK_SIZE - addr; |
| 4330 | } |
| 4331 | |
| 4332 | static u16 |
| 4333 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 4334 | struct pt_regs *regs) |
| 4335 | { |
| 4336 | u64 task_size; |
| 4337 | |
| 4338 | /* No regs, no stack pointer, no dump. */ |
| 4339 | if (!regs) |
| 4340 | return 0; |
| 4341 | |
| 4342 | /* |
| 4343 | * Check if we fit in with the requested stack size into the: |
| 4344 | * - TASK_SIZE |
| 4345 | * If we don't, we limit the size to the TASK_SIZE. |
| 4346 | * |
| 4347 | * - remaining sample size |
| 4348 | * If we don't, we customize the stack size to |
| 4349 | * fit in to the remaining sample size. |
| 4350 | */ |
| 4351 | |
| 4352 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 4353 | stack_size = min(stack_size, (u16) task_size); |
| 4354 | |
| 4355 | /* Current header size plus static size and dynamic size. */ |
| 4356 | header_size += 2 * sizeof(u64); |
| 4357 | |
| 4358 | /* Do we fit in with the current stack dump size? */ |
| 4359 | if ((u16) (header_size + stack_size) < header_size) { |
| 4360 | /* |
| 4361 | * If we overflow the maximum size for the sample, |
| 4362 | * we customize the stack dump size to fit in. |
| 4363 | */ |
| 4364 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 4365 | stack_size = round_up(stack_size, sizeof(u64)); |
| 4366 | } |
| 4367 | |
| 4368 | return stack_size; |
| 4369 | } |
| 4370 | |
| 4371 | static void |
| 4372 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 4373 | struct pt_regs *regs) |
| 4374 | { |
| 4375 | /* Case of a kernel thread, nothing to dump */ |
| 4376 | if (!regs) { |
| 4377 | u64 size = 0; |
| 4378 | perf_output_put(handle, size); |
| 4379 | } else { |
| 4380 | unsigned long sp; |
| 4381 | unsigned int rem; |
| 4382 | u64 dyn_size; |
| 4383 | |
| 4384 | /* |
| 4385 | * We dump: |
| 4386 | * static size |
| 4387 | * - the size requested by user or the best one we can fit |
| 4388 | * in to the sample max size |
| 4389 | * data |
| 4390 | * - user stack dump data |
| 4391 | * dynamic size |
| 4392 | * - the actual dumped size |
| 4393 | */ |
| 4394 | |
| 4395 | /* Static size. */ |
| 4396 | perf_output_put(handle, dump_size); |
| 4397 | |
| 4398 | /* Data. */ |
| 4399 | sp = perf_user_stack_pointer(regs); |
| 4400 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 4401 | dyn_size = dump_size - rem; |
| 4402 | |
| 4403 | perf_output_skip(handle, rem); |
| 4404 | |
| 4405 | /* Dynamic size. */ |
| 4406 | perf_output_put(handle, dyn_size); |
| 4407 | } |
| 4408 | } |
| 4409 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4410 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 4411 | struct perf_sample_data *data, |
| 4412 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4413 | { |
| 4414 | u64 sample_type = event->attr.sample_type; |
| 4415 | |
| 4416 | data->type = sample_type; |
| 4417 | header->size += event->id_header_size; |
| 4418 | |
| 4419 | if (sample_type & PERF_SAMPLE_TID) { |
| 4420 | /* namespace issues */ |
| 4421 | data->tid_entry.pid = perf_event_pid(event, current); |
| 4422 | data->tid_entry.tid = perf_event_tid(event, current); |
| 4423 | } |
| 4424 | |
| 4425 | if (sample_type & PERF_SAMPLE_TIME) |
| 4426 | data->time = perf_clock(); |
| 4427 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4428 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4429 | data->id = primary_event_id(event); |
| 4430 | |
| 4431 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4432 | data->stream_id = event->id; |
| 4433 | |
| 4434 | if (sample_type & PERF_SAMPLE_CPU) { |
| 4435 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 4436 | data->cpu_entry.reserved = 0; |
| 4437 | } |
| 4438 | } |
| 4439 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4440 | void perf_event_header__init_id(struct perf_event_header *header, |
| 4441 | struct perf_sample_data *data, |
| 4442 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4443 | { |
| 4444 | if (event->attr.sample_id_all) |
| 4445 | __perf_event_header__init_id(header, data, event); |
| 4446 | } |
| 4447 | |
| 4448 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 4449 | struct perf_sample_data *data) |
| 4450 | { |
| 4451 | u64 sample_type = data->type; |
| 4452 | |
| 4453 | if (sample_type & PERF_SAMPLE_TID) |
| 4454 | perf_output_put(handle, data->tid_entry); |
| 4455 | |
| 4456 | if (sample_type & PERF_SAMPLE_TIME) |
| 4457 | perf_output_put(handle, data->time); |
| 4458 | |
| 4459 | if (sample_type & PERF_SAMPLE_ID) |
| 4460 | perf_output_put(handle, data->id); |
| 4461 | |
| 4462 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4463 | perf_output_put(handle, data->stream_id); |
| 4464 | |
| 4465 | if (sample_type & PERF_SAMPLE_CPU) |
| 4466 | perf_output_put(handle, data->cpu_entry); |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4467 | |
| 4468 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4469 | perf_output_put(handle, data->id); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4470 | } |
| 4471 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4472 | void perf_event__output_id_sample(struct perf_event *event, |
| 4473 | struct perf_output_handle *handle, |
| 4474 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4475 | { |
| 4476 | if (event->attr.sample_id_all) |
| 4477 | __perf_event__output_id_sample(handle, sample); |
| 4478 | } |
| 4479 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4480 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4481 | struct perf_event *event, |
| 4482 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4483 | { |
| 4484 | u64 read_format = event->attr.read_format; |
| 4485 | u64 values[4]; |
| 4486 | int n = 0; |
| 4487 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4488 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4489 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4490 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4491 | atomic64_read(&event->child_total_time_enabled); |
| 4492 | } |
| 4493 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4494 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4495 | atomic64_read(&event->child_total_time_running); |
| 4496 | } |
| 4497 | if (read_format & PERF_FORMAT_ID) |
| 4498 | values[n++] = primary_event_id(event); |
| 4499 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4500 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | /* |
| 4504 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 4505 | */ |
| 4506 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4507 | struct perf_event *event, |
| 4508 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4509 | { |
| 4510 | struct perf_event *leader = event->group_leader, *sub; |
| 4511 | u64 read_format = event->attr.read_format; |
| 4512 | u64 values[5]; |
| 4513 | int n = 0; |
| 4514 | |
| 4515 | values[n++] = 1 + leader->nr_siblings; |
| 4516 | |
| 4517 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4518 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4519 | |
| 4520 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4521 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4522 | |
| 4523 | if (leader != event) |
| 4524 | leader->pmu->read(leader); |
| 4525 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4526 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4527 | if (read_format & PERF_FORMAT_ID) |
| 4528 | values[n++] = primary_event_id(leader); |
| 4529 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4530 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4531 | |
| 4532 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 4533 | n = 0; |
| 4534 | |
Jiri Olsa | 6f5ab00 | 2012-10-15 20:13:45 +0200 | [diff] [blame] | 4535 | if ((sub != event) && |
| 4536 | (sub->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4537 | sub->pmu->read(sub); |
| 4538 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4539 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4540 | if (read_format & PERF_FORMAT_ID) |
| 4541 | values[n++] = primary_event_id(sub); |
| 4542 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4543 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4544 | } |
| 4545 | } |
| 4546 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4547 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 4548 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4549 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4550 | static void perf_output_read(struct perf_output_handle *handle, |
| 4551 | struct perf_event *event) |
| 4552 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4553 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4554 | u64 read_format = event->attr.read_format; |
| 4555 | |
| 4556 | /* |
| 4557 | * compute total_time_enabled, total_time_running |
| 4558 | * based on snapshot values taken when the event |
| 4559 | * was last scheduled in. |
| 4560 | * |
| 4561 | * we cannot simply called update_context_time() |
| 4562 | * because of locking issue as we are called in |
| 4563 | * NMI context |
| 4564 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4565 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4566 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4567 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4568 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4569 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4570 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4571 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | void perf_output_sample(struct perf_output_handle *handle, |
| 4575 | struct perf_event_header *header, |
| 4576 | struct perf_sample_data *data, |
| 4577 | struct perf_event *event) |
| 4578 | { |
| 4579 | u64 sample_type = data->type; |
| 4580 | |
| 4581 | perf_output_put(handle, *header); |
| 4582 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4583 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4584 | perf_output_put(handle, data->id); |
| 4585 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4586 | if (sample_type & PERF_SAMPLE_IP) |
| 4587 | perf_output_put(handle, data->ip); |
| 4588 | |
| 4589 | if (sample_type & PERF_SAMPLE_TID) |
| 4590 | perf_output_put(handle, data->tid_entry); |
| 4591 | |
| 4592 | if (sample_type & PERF_SAMPLE_TIME) |
| 4593 | perf_output_put(handle, data->time); |
| 4594 | |
| 4595 | if (sample_type & PERF_SAMPLE_ADDR) |
| 4596 | perf_output_put(handle, data->addr); |
| 4597 | |
| 4598 | if (sample_type & PERF_SAMPLE_ID) |
| 4599 | perf_output_put(handle, data->id); |
| 4600 | |
| 4601 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4602 | perf_output_put(handle, data->stream_id); |
| 4603 | |
| 4604 | if (sample_type & PERF_SAMPLE_CPU) |
| 4605 | perf_output_put(handle, data->cpu_entry); |
| 4606 | |
| 4607 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 4608 | perf_output_put(handle, data->period); |
| 4609 | |
| 4610 | if (sample_type & PERF_SAMPLE_READ) |
| 4611 | perf_output_read(handle, event); |
| 4612 | |
| 4613 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4614 | if (data->callchain) { |
| 4615 | int size = 1; |
| 4616 | |
| 4617 | if (data->callchain) |
| 4618 | size += data->callchain->nr; |
| 4619 | |
| 4620 | size *= sizeof(u64); |
| 4621 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4622 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4623 | } else { |
| 4624 | u64 nr = 0; |
| 4625 | perf_output_put(handle, nr); |
| 4626 | } |
| 4627 | } |
| 4628 | |
| 4629 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4630 | if (data->raw) { |
| 4631 | perf_output_put(handle, data->raw->size); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4632 | __output_copy(handle, data->raw->data, |
| 4633 | data->raw->size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4634 | } else { |
| 4635 | struct { |
| 4636 | u32 size; |
| 4637 | u32 data; |
| 4638 | } raw = { |
| 4639 | .size = sizeof(u32), |
| 4640 | .data = 0, |
| 4641 | }; |
| 4642 | perf_output_put(handle, raw); |
| 4643 | } |
| 4644 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4645 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4646 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4647 | if (data->br_stack) { |
| 4648 | size_t size; |
| 4649 | |
| 4650 | size = data->br_stack->nr |
| 4651 | * sizeof(struct perf_branch_entry); |
| 4652 | |
| 4653 | perf_output_put(handle, data->br_stack->nr); |
| 4654 | perf_output_copy(handle, data->br_stack->entries, size); |
| 4655 | } else { |
| 4656 | /* |
| 4657 | * we always store at least the value of nr |
| 4658 | */ |
| 4659 | u64 nr = 0; |
| 4660 | perf_output_put(handle, nr); |
| 4661 | } |
| 4662 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4663 | |
| 4664 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 4665 | u64 abi = data->regs_user.abi; |
| 4666 | |
| 4667 | /* |
| 4668 | * If there are no regs to dump, notice it through |
| 4669 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 4670 | */ |
| 4671 | perf_output_put(handle, abi); |
| 4672 | |
| 4673 | if (abi) { |
| 4674 | u64 mask = event->attr.sample_regs_user; |
| 4675 | perf_output_sample_regs(handle, |
| 4676 | data->regs_user.regs, |
| 4677 | mask); |
| 4678 | } |
| 4679 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4680 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4681 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4682 | perf_output_sample_ustack(handle, |
| 4683 | data->stack_user_size, |
| 4684 | data->regs_user.regs); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4685 | } |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 4686 | |
| 4687 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 4688 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 4689 | |
| 4690 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 4691 | perf_output_put(handle, data->data_src.val); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4692 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 4693 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 4694 | perf_output_put(handle, data->txn); |
| 4695 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4696 | if (!event->attr.watermark) { |
| 4697 | int wakeup_events = event->attr.wakeup_events; |
| 4698 | |
| 4699 | if (wakeup_events) { |
| 4700 | struct ring_buffer *rb = handle->rb; |
| 4701 | int events = local_inc_return(&rb->events); |
| 4702 | |
| 4703 | if (events >= wakeup_events) { |
| 4704 | local_sub(wakeup_events, &rb->events); |
| 4705 | local_inc(&rb->wakeup); |
| 4706 | } |
| 4707 | } |
| 4708 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | void perf_prepare_sample(struct perf_event_header *header, |
| 4712 | struct perf_sample_data *data, |
| 4713 | struct perf_event *event, |
| 4714 | struct pt_regs *regs) |
| 4715 | { |
| 4716 | u64 sample_type = event->attr.sample_type; |
| 4717 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4718 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4719 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4720 | |
| 4721 | header->misc = 0; |
| 4722 | header->misc |= perf_misc_flags(regs); |
| 4723 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4724 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4725 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4726 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4727 | data->ip = perf_instruction_pointer(regs); |
| 4728 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4729 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4730 | int size = 1; |
| 4731 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 4732 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4733 | |
| 4734 | if (data->callchain) |
| 4735 | size += data->callchain->nr; |
| 4736 | |
| 4737 | header->size += size * sizeof(u64); |
| 4738 | } |
| 4739 | |
| 4740 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4741 | int size = sizeof(u32); |
| 4742 | |
| 4743 | if (data->raw) |
| 4744 | size += data->raw->size; |
| 4745 | else |
| 4746 | size += sizeof(u32); |
| 4747 | |
| 4748 | WARN_ON_ONCE(size & (sizeof(u64)-1)); |
| 4749 | header->size += size; |
| 4750 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4751 | |
| 4752 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4753 | int size = sizeof(u64); /* nr */ |
| 4754 | if (data->br_stack) { |
| 4755 | size += data->br_stack->nr |
| 4756 | * sizeof(struct perf_branch_entry); |
| 4757 | } |
| 4758 | header->size += size; |
| 4759 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4760 | |
| 4761 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 4762 | /* regs dump ABI info */ |
| 4763 | int size = sizeof(u64); |
| 4764 | |
| 4765 | perf_sample_regs_user(&data->regs_user, regs); |
| 4766 | |
| 4767 | if (data->regs_user.regs) { |
| 4768 | u64 mask = event->attr.sample_regs_user; |
| 4769 | size += hweight64(mask) * sizeof(u64); |
| 4770 | } |
| 4771 | |
| 4772 | header->size += size; |
| 4773 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4774 | |
| 4775 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 4776 | /* |
| 4777 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 4778 | * processed as the last one or have additional check added |
| 4779 | * in case new sample type is added, because we could eat |
| 4780 | * up the rest of the sample size. |
| 4781 | */ |
| 4782 | struct perf_regs_user *uregs = &data->regs_user; |
| 4783 | u16 stack_size = event->attr.sample_stack_user; |
| 4784 | u16 size = sizeof(u64); |
| 4785 | |
| 4786 | if (!uregs->abi) |
| 4787 | perf_sample_regs_user(uregs, regs); |
| 4788 | |
| 4789 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
| 4790 | uregs->regs); |
| 4791 | |
| 4792 | /* |
| 4793 | * If there is something to dump, add space for the dump |
| 4794 | * itself and for the field that tells the dynamic size, |
| 4795 | * which is how many have been actually dumped. |
| 4796 | */ |
| 4797 | if (stack_size) |
| 4798 | size += sizeof(u64) + stack_size; |
| 4799 | |
| 4800 | data->stack_user_size = stack_size; |
| 4801 | header->size += size; |
| 4802 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4803 | } |
| 4804 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 4805 | static void perf_event_output(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4806 | struct perf_sample_data *data, |
| 4807 | struct pt_regs *regs) |
| 4808 | { |
| 4809 | struct perf_output_handle handle; |
| 4810 | struct perf_event_header header; |
| 4811 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4812 | /* protect the callchain buffers */ |
| 4813 | rcu_read_lock(); |
| 4814 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4815 | perf_prepare_sample(&header, data, event, regs); |
| 4816 | |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4817 | if (perf_output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4818 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4819 | |
| 4820 | perf_output_sample(&handle, &header, data, event); |
| 4821 | |
| 4822 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4823 | |
| 4824 | exit: |
| 4825 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | /* |
| 4829 | * read event_id |
| 4830 | */ |
| 4831 | |
| 4832 | struct perf_read_event { |
| 4833 | struct perf_event_header header; |
| 4834 | |
| 4835 | u32 pid; |
| 4836 | u32 tid; |
| 4837 | }; |
| 4838 | |
| 4839 | static void |
| 4840 | perf_event_read_event(struct perf_event *event, |
| 4841 | struct task_struct *task) |
| 4842 | { |
| 4843 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4844 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4845 | struct perf_read_event read_event = { |
| 4846 | .header = { |
| 4847 | .type = PERF_RECORD_READ, |
| 4848 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4849 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4850 | }, |
| 4851 | .pid = perf_event_pid(event, task), |
| 4852 | .tid = perf_event_tid(event, task), |
| 4853 | }; |
| 4854 | int ret; |
| 4855 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4856 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4857 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4858 | if (ret) |
| 4859 | return; |
| 4860 | |
| 4861 | perf_output_put(&handle, read_event); |
| 4862 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4863 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4864 | |
| 4865 | perf_output_end(&handle); |
| 4866 | } |
| 4867 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4868 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
| 4869 | |
| 4870 | static void |
| 4871 | perf_event_aux_ctx(struct perf_event_context *ctx, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4872 | perf_event_aux_output_cb output, |
| 4873 | void *data) |
| 4874 | { |
| 4875 | struct perf_event *event; |
| 4876 | |
| 4877 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 4878 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 4879 | continue; |
| 4880 | if (!event_filter_match(event)) |
| 4881 | continue; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4882 | output(event, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4883 | } |
| 4884 | } |
| 4885 | |
| 4886 | static void |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4887 | perf_event_aux(perf_event_aux_output_cb output, void *data, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4888 | struct perf_event_context *task_ctx) |
| 4889 | { |
| 4890 | struct perf_cpu_context *cpuctx; |
| 4891 | struct perf_event_context *ctx; |
| 4892 | struct pmu *pmu; |
| 4893 | int ctxn; |
| 4894 | |
| 4895 | rcu_read_lock(); |
| 4896 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 4897 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 4898 | if (cpuctx->unique_pmu != pmu) |
| 4899 | goto next; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4900 | perf_event_aux_ctx(&cpuctx->ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4901 | if (task_ctx) |
| 4902 | goto next; |
| 4903 | ctxn = pmu->task_ctx_nr; |
| 4904 | if (ctxn < 0) |
| 4905 | goto next; |
| 4906 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 4907 | if (ctx) |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4908 | perf_event_aux_ctx(ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4909 | next: |
| 4910 | put_cpu_ptr(pmu->pmu_cpu_context); |
| 4911 | } |
| 4912 | |
| 4913 | if (task_ctx) { |
| 4914 | preempt_disable(); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4915 | perf_event_aux_ctx(task_ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4916 | preempt_enable(); |
| 4917 | } |
| 4918 | rcu_read_unlock(); |
| 4919 | } |
| 4920 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4921 | /* |
| 4922 | * task tracking -- fork/exit |
| 4923 | * |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 4924 | * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4925 | */ |
| 4926 | |
| 4927 | struct perf_task_event { |
| 4928 | struct task_struct *task; |
| 4929 | struct perf_event_context *task_ctx; |
| 4930 | |
| 4931 | struct { |
| 4932 | struct perf_event_header header; |
| 4933 | |
| 4934 | u32 pid; |
| 4935 | u32 ppid; |
| 4936 | u32 tid; |
| 4937 | u32 ptid; |
| 4938 | u64 time; |
| 4939 | } event_id; |
| 4940 | }; |
| 4941 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4942 | static int perf_event_task_match(struct perf_event *event) |
| 4943 | { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 4944 | return event->attr.comm || event->attr.mmap || |
| 4945 | event->attr.mmap2 || event->attr.mmap_data || |
| 4946 | event->attr.task; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4947 | } |
| 4948 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4949 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4950 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4951 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4952 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4953 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4954 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4955 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4956 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 4957 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4958 | if (!perf_event_task_match(event)) |
| 4959 | return; |
| 4960 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4961 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4962 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4963 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4964 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 4965 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4966 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4967 | |
| 4968 | task_event->event_id.pid = perf_event_pid(event, task); |
| 4969 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 4970 | |
| 4971 | task_event->event_id.tid = perf_event_tid(event, task); |
| 4972 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 4973 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4974 | perf_output_put(&handle, task_event->event_id); |
| 4975 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4976 | perf_event__output_id_sample(event, &handle, &sample); |
| 4977 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4978 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4979 | out: |
| 4980 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4981 | } |
| 4982 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4983 | static void perf_event_task(struct task_struct *task, |
| 4984 | struct perf_event_context *task_ctx, |
| 4985 | int new) |
| 4986 | { |
| 4987 | struct perf_task_event task_event; |
| 4988 | |
| 4989 | if (!atomic_read(&nr_comm_events) && |
| 4990 | !atomic_read(&nr_mmap_events) && |
| 4991 | !atomic_read(&nr_task_events)) |
| 4992 | return; |
| 4993 | |
| 4994 | task_event = (struct perf_task_event){ |
| 4995 | .task = task, |
| 4996 | .task_ctx = task_ctx, |
| 4997 | .event_id = { |
| 4998 | .header = { |
| 4999 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 5000 | .misc = 0, |
| 5001 | .size = sizeof(task_event.event_id), |
| 5002 | }, |
| 5003 | /* .pid */ |
| 5004 | /* .ppid */ |
| 5005 | /* .tid */ |
| 5006 | /* .ptid */ |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 5007 | .time = perf_clock(), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5008 | }, |
| 5009 | }; |
| 5010 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5011 | perf_event_aux(perf_event_task_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5012 | &task_event, |
| 5013 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5014 | } |
| 5015 | |
| 5016 | void perf_event_fork(struct task_struct *task) |
| 5017 | { |
| 5018 | perf_event_task(task, NULL, 1); |
| 5019 | } |
| 5020 | |
| 5021 | /* |
| 5022 | * comm tracking |
| 5023 | */ |
| 5024 | |
| 5025 | struct perf_comm_event { |
| 5026 | struct task_struct *task; |
| 5027 | char *comm; |
| 5028 | int comm_size; |
| 5029 | |
| 5030 | struct { |
| 5031 | struct perf_event_header header; |
| 5032 | |
| 5033 | u32 pid; |
| 5034 | u32 tid; |
| 5035 | } event_id; |
| 5036 | }; |
| 5037 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5038 | static int perf_event_comm_match(struct perf_event *event) |
| 5039 | { |
| 5040 | return event->attr.comm; |
| 5041 | } |
| 5042 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5043 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5044 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5045 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5046 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5047 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5048 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5049 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5050 | int ret; |
| 5051 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5052 | if (!perf_event_comm_match(event)) |
| 5053 | return; |
| 5054 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5055 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 5056 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5057 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5058 | |
| 5059 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5060 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5061 | |
| 5062 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 5063 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 5064 | |
| 5065 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5066 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5067 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5068 | |
| 5069 | perf_event__output_id_sample(event, &handle, &sample); |
| 5070 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5071 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5072 | out: |
| 5073 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5074 | } |
| 5075 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5076 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 5077 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5078 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5079 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5080 | |
| 5081 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 5082 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5083 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 5084 | |
| 5085 | comm_event->comm = comm; |
| 5086 | comm_event->comm_size = size; |
| 5087 | |
| 5088 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5089 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5090 | perf_event_aux(perf_event_comm_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5091 | comm_event, |
| 5092 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5093 | } |
| 5094 | |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5095 | void perf_event_comm(struct task_struct *task, bool exec) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5096 | { |
| 5097 | struct perf_comm_event comm_event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5098 | |
| 5099 | if (!atomic_read(&nr_comm_events)) |
| 5100 | return; |
| 5101 | |
| 5102 | comm_event = (struct perf_comm_event){ |
| 5103 | .task = task, |
| 5104 | /* .comm */ |
| 5105 | /* .comm_size */ |
| 5106 | .event_id = { |
| 5107 | .header = { |
| 5108 | .type = PERF_RECORD_COMM, |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5109 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5110 | /* .size */ |
| 5111 | }, |
| 5112 | /* .pid */ |
| 5113 | /* .tid */ |
| 5114 | }, |
| 5115 | }; |
| 5116 | |
| 5117 | perf_event_comm_event(&comm_event); |
| 5118 | } |
| 5119 | |
| 5120 | /* |
| 5121 | * mmap tracking |
| 5122 | */ |
| 5123 | |
| 5124 | struct perf_mmap_event { |
| 5125 | struct vm_area_struct *vma; |
| 5126 | |
| 5127 | const char *file_name; |
| 5128 | int file_size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5129 | int maj, min; |
| 5130 | u64 ino; |
| 5131 | u64 ino_generation; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5132 | u32 prot, flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5133 | |
| 5134 | struct { |
| 5135 | struct perf_event_header header; |
| 5136 | |
| 5137 | u32 pid; |
| 5138 | u32 tid; |
| 5139 | u64 start; |
| 5140 | u64 len; |
| 5141 | u64 pgoff; |
| 5142 | } event_id; |
| 5143 | }; |
| 5144 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5145 | static int perf_event_mmap_match(struct perf_event *event, |
| 5146 | void *data) |
| 5147 | { |
| 5148 | struct perf_mmap_event *mmap_event = data; |
| 5149 | struct vm_area_struct *vma = mmap_event->vma; |
| 5150 | int executable = vma->vm_flags & VM_EXEC; |
| 5151 | |
| 5152 | return (!executable && event->attr.mmap_data) || |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5153 | (executable && (event->attr.mmap || event->attr.mmap2)); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5154 | } |
| 5155 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5156 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5157 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5158 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5159 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5160 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5161 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5162 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5163 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5164 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5165 | if (!perf_event_mmap_match(event, data)) |
| 5166 | return; |
| 5167 | |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5168 | if (event->attr.mmap2) { |
| 5169 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; |
| 5170 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); |
| 5171 | mmap_event->event_id.header.size += sizeof(mmap_event->min); |
| 5172 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); |
Arnaldo Carvalho de Melo | d008d52 | 2013-09-10 10:24:05 -0300 | [diff] [blame] | 5173 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5174 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
| 5175 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5176 | } |
| 5177 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5178 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 5179 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5180 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5181 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5182 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5183 | |
| 5184 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 5185 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 5186 | |
| 5187 | perf_output_put(&handle, mmap_event->event_id); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5188 | |
| 5189 | if (event->attr.mmap2) { |
| 5190 | perf_output_put(&handle, mmap_event->maj); |
| 5191 | perf_output_put(&handle, mmap_event->min); |
| 5192 | perf_output_put(&handle, mmap_event->ino); |
| 5193 | perf_output_put(&handle, mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5194 | perf_output_put(&handle, mmap_event->prot); |
| 5195 | perf_output_put(&handle, mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5196 | } |
| 5197 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5198 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5199 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5200 | |
| 5201 | perf_event__output_id_sample(event, &handle, &sample); |
| 5202 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5203 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5204 | out: |
| 5205 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5206 | } |
| 5207 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5208 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 5209 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5210 | struct vm_area_struct *vma = mmap_event->vma; |
| 5211 | struct file *file = vma->vm_file; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5212 | int maj = 0, min = 0; |
| 5213 | u64 ino = 0, gen = 0; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5214 | u32 prot = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5215 | unsigned int size; |
| 5216 | char tmp[16]; |
| 5217 | char *buf = NULL; |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5218 | char *name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5219 | |
| 5220 | if (file) { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5221 | struct inode *inode; |
| 5222 | dev_t dev; |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5223 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5224 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5225 | if (!buf) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5226 | name = "//enomem"; |
| 5227 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5228 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5229 | /* |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5230 | * d_path() works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5231 | * need to add enough zero bytes after the string to handle |
| 5232 | * the 64bit alignment we do later. |
| 5233 | */ |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5234 | name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5235 | if (IS_ERR(name)) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5236 | name = "//toolong"; |
| 5237 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5238 | } |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5239 | inode = file_inode(vma->vm_file); |
| 5240 | dev = inode->i_sb->s_dev; |
| 5241 | ino = inode->i_ino; |
| 5242 | gen = inode->i_generation; |
| 5243 | maj = MAJOR(dev); |
| 5244 | min = MINOR(dev); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5245 | |
| 5246 | if (vma->vm_flags & VM_READ) |
| 5247 | prot |= PROT_READ; |
| 5248 | if (vma->vm_flags & VM_WRITE) |
| 5249 | prot |= PROT_WRITE; |
| 5250 | if (vma->vm_flags & VM_EXEC) |
| 5251 | prot |= PROT_EXEC; |
| 5252 | |
| 5253 | if (vma->vm_flags & VM_MAYSHARE) |
| 5254 | flags = MAP_SHARED; |
| 5255 | else |
| 5256 | flags = MAP_PRIVATE; |
| 5257 | |
| 5258 | if (vma->vm_flags & VM_DENYWRITE) |
| 5259 | flags |= MAP_DENYWRITE; |
| 5260 | if (vma->vm_flags & VM_MAYEXEC) |
| 5261 | flags |= MAP_EXECUTABLE; |
| 5262 | if (vma->vm_flags & VM_LOCKED) |
| 5263 | flags |= MAP_LOCKED; |
| 5264 | if (vma->vm_flags & VM_HUGETLB) |
| 5265 | flags |= MAP_HUGETLB; |
| 5266 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5267 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5268 | } else { |
Jiri Olsa | fbe26ab | 2014-07-14 17:57:19 +0200 | [diff] [blame] | 5269 | if (vma->vm_ops && vma->vm_ops->name) { |
| 5270 | name = (char *) vma->vm_ops->name(vma); |
| 5271 | if (name) |
| 5272 | goto cpy_name; |
| 5273 | } |
| 5274 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5275 | name = (char *)arch_vma_name(vma); |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5276 | if (name) |
| 5277 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5278 | |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5279 | if (vma->vm_start <= vma->vm_mm->start_brk && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5280 | vma->vm_end >= vma->vm_mm->brk) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5281 | name = "[heap]"; |
| 5282 | goto cpy_name; |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5283 | } |
| 5284 | if (vma->vm_start <= vma->vm_mm->start_stack && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5285 | vma->vm_end >= vma->vm_mm->start_stack) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5286 | name = "[stack]"; |
| 5287 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5288 | } |
| 5289 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5290 | name = "//anon"; |
| 5291 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5292 | } |
| 5293 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5294 | cpy_name: |
| 5295 | strlcpy(tmp, name, sizeof(tmp)); |
| 5296 | name = tmp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5297 | got_name: |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5298 | /* |
| 5299 | * Since our buffer works in 8 byte units we need to align our string |
| 5300 | * size to a multiple of 8. However, we must guarantee the tail end is |
| 5301 | * zero'd out to avoid leaking random bits to userspace. |
| 5302 | */ |
| 5303 | size = strlen(name)+1; |
| 5304 | while (!IS_ALIGNED(size, sizeof(u64))) |
| 5305 | name[size++] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5306 | |
| 5307 | mmap_event->file_name = name; |
| 5308 | mmap_event->file_size = size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5309 | mmap_event->maj = maj; |
| 5310 | mmap_event->min = min; |
| 5311 | mmap_event->ino = ino; |
| 5312 | mmap_event->ino_generation = gen; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5313 | mmap_event->prot = prot; |
| 5314 | mmap_event->flags = flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5315 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 5316 | if (!(vma->vm_flags & VM_EXEC)) |
| 5317 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 5318 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5319 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 5320 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5321 | perf_event_aux(perf_event_mmap_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5322 | mmap_event, |
| 5323 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5324 | |
| 5325 | kfree(buf); |
| 5326 | } |
| 5327 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 5328 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5329 | { |
| 5330 | struct perf_mmap_event mmap_event; |
| 5331 | |
| 5332 | if (!atomic_read(&nr_mmap_events)) |
| 5333 | return; |
| 5334 | |
| 5335 | mmap_event = (struct perf_mmap_event){ |
| 5336 | .vma = vma, |
| 5337 | /* .file_name */ |
| 5338 | /* .file_size */ |
| 5339 | .event_id = { |
| 5340 | .header = { |
| 5341 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5342 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5343 | /* .size */ |
| 5344 | }, |
| 5345 | /* .pid */ |
| 5346 | /* .tid */ |
| 5347 | .start = vma->vm_start, |
| 5348 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 5349 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5350 | }, |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5351 | /* .maj (attr_mmap2 only) */ |
| 5352 | /* .min (attr_mmap2 only) */ |
| 5353 | /* .ino (attr_mmap2 only) */ |
| 5354 | /* .ino_generation (attr_mmap2 only) */ |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5355 | /* .prot (attr_mmap2 only) */ |
| 5356 | /* .flags (attr_mmap2 only) */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5357 | }; |
| 5358 | |
| 5359 | perf_event_mmap_event(&mmap_event); |
| 5360 | } |
| 5361 | |
| 5362 | /* |
| 5363 | * IRQ throttle logging |
| 5364 | */ |
| 5365 | |
| 5366 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 5367 | { |
| 5368 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5369 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5370 | int ret; |
| 5371 | |
| 5372 | struct { |
| 5373 | struct perf_event_header header; |
| 5374 | u64 time; |
| 5375 | u64 id; |
| 5376 | u64 stream_id; |
| 5377 | } throttle_event = { |
| 5378 | .header = { |
| 5379 | .type = PERF_RECORD_THROTTLE, |
| 5380 | .misc = 0, |
| 5381 | .size = sizeof(throttle_event), |
| 5382 | }, |
| 5383 | .time = perf_clock(), |
| 5384 | .id = primary_event_id(event), |
| 5385 | .stream_id = event->id, |
| 5386 | }; |
| 5387 | |
| 5388 | if (enable) |
| 5389 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 5390 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5391 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 5392 | |
| 5393 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5394 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5395 | if (ret) |
| 5396 | return; |
| 5397 | |
| 5398 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5399 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5400 | perf_output_end(&handle); |
| 5401 | } |
| 5402 | |
| 5403 | /* |
| 5404 | * Generic event overflow handling, sampling. |
| 5405 | */ |
| 5406 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5407 | static int __perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5408 | int throttle, struct perf_sample_data *data, |
| 5409 | struct pt_regs *regs) |
| 5410 | { |
| 5411 | int events = atomic_read(&event->event_limit); |
| 5412 | struct hw_perf_event *hwc = &event->hw; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5413 | u64 seq; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5414 | int ret = 0; |
| 5415 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 5416 | /* |
| 5417 | * Non-sampling counters might still use the PMI to fold short |
| 5418 | * hardware counters, ignore those. |
| 5419 | */ |
| 5420 | if (unlikely(!is_sampling_event(event))) |
| 5421 | return 0; |
| 5422 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5423 | seq = __this_cpu_read(perf_throttled_seq); |
| 5424 | if (seq != hwc->interrupts_seq) { |
| 5425 | hwc->interrupts_seq = seq; |
| 5426 | hwc->interrupts = 1; |
| 5427 | } else { |
| 5428 | hwc->interrupts++; |
| 5429 | if (unlikely(throttle |
| 5430 | && hwc->interrupts >= max_samples_per_tick)) { |
| 5431 | __this_cpu_inc(perf_throttled_count); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 5432 | hwc->interrupts = MAX_INTERRUPTS; |
| 5433 | perf_log_throttle(event, 0); |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 5434 | tick_nohz_full_kick(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5435 | ret = 1; |
| 5436 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5437 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5438 | |
| 5439 | if (event->attr.freq) { |
| 5440 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5441 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5442 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5443 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5444 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5445 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 5446 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | /* |
| 5450 | * XXX event_limit might not quite work as expected on inherited |
| 5451 | * events |
| 5452 | */ |
| 5453 | |
| 5454 | event->pending_kill = POLL_IN; |
| 5455 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 5456 | ret = 1; |
| 5457 | event->pending_kill = POLL_HUP; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5458 | event->pending_disable = 1; |
| 5459 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5460 | } |
| 5461 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5462 | if (event->overflow_handler) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5463 | event->overflow_handler(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5464 | else |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5465 | perf_event_output(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5466 | |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5467 | if (event->fasync && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5468 | event->pending_wakeup = 1; |
| 5469 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5470 | } |
| 5471 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5472 | return ret; |
| 5473 | } |
| 5474 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5475 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5476 | struct perf_sample_data *data, |
| 5477 | struct pt_regs *regs) |
| 5478 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5479 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | /* |
| 5483 | * Generic software event infrastructure |
| 5484 | */ |
| 5485 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5486 | struct swevent_htable { |
| 5487 | struct swevent_hlist *swevent_hlist; |
| 5488 | struct mutex hlist_mutex; |
| 5489 | int hlist_refcount; |
| 5490 | |
| 5491 | /* Recursion avoidance in each contexts */ |
| 5492 | int recursion[PERF_NR_CONTEXTS]; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5493 | |
| 5494 | /* Keeps track of cpu being initialized/exited */ |
| 5495 | bool online; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5496 | }; |
| 5497 | |
| 5498 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 5499 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5500 | /* |
| 5501 | * We directly increment event->count and keep a second value in |
| 5502 | * event->hw.period_left to count intervals. This period event |
| 5503 | * is kept in the range [-sample_period, 0] so that we can use the |
| 5504 | * sign as trigger. |
| 5505 | */ |
| 5506 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 5507 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5508 | { |
| 5509 | struct hw_perf_event *hwc = &event->hw; |
| 5510 | u64 period = hwc->last_period; |
| 5511 | u64 nr, offset; |
| 5512 | s64 old, val; |
| 5513 | |
| 5514 | hwc->last_period = hwc->sample_period; |
| 5515 | |
| 5516 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5517 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5518 | if (val < 0) |
| 5519 | return 0; |
| 5520 | |
| 5521 | nr = div64_u64(period + val, period); |
| 5522 | offset = nr * period; |
| 5523 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5524 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5525 | goto again; |
| 5526 | |
| 5527 | return nr; |
| 5528 | } |
| 5529 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5530 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5531 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5532 | struct pt_regs *regs) |
| 5533 | { |
| 5534 | struct hw_perf_event *hwc = &event->hw; |
| 5535 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5536 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5537 | if (!overflow) |
| 5538 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5539 | |
| 5540 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 5541 | return; |
| 5542 | |
| 5543 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5544 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5545 | data, regs)) { |
| 5546 | /* |
| 5547 | * We inhibit the overflow from happening when |
| 5548 | * hwc->interrupts == MAX_INTERRUPTS. |
| 5549 | */ |
| 5550 | break; |
| 5551 | } |
| 5552 | throttle = 1; |
| 5553 | } |
| 5554 | } |
| 5555 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5556 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5557 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5558 | struct pt_regs *regs) |
| 5559 | { |
| 5560 | struct hw_perf_event *hwc = &event->hw; |
| 5561 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5562 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5563 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5564 | if (!regs) |
| 5565 | return; |
| 5566 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5567 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5568 | return; |
| 5569 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 5570 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 5571 | data->period = nr; |
| 5572 | return perf_swevent_overflow(event, 1, data, regs); |
| 5573 | } else |
| 5574 | data->period = event->hw.last_period; |
| 5575 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5576 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5577 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5578 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5579 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5580 | return; |
| 5581 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5582 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5583 | } |
| 5584 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5585 | static int perf_exclude_event(struct perf_event *event, |
| 5586 | struct pt_regs *regs) |
| 5587 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5588 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 5589 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5590 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5591 | if (regs) { |
| 5592 | if (event->attr.exclude_user && user_mode(regs)) |
| 5593 | return 1; |
| 5594 | |
| 5595 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 5596 | return 1; |
| 5597 | } |
| 5598 | |
| 5599 | return 0; |
| 5600 | } |
| 5601 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5602 | static int perf_swevent_match(struct perf_event *event, |
| 5603 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5604 | u32 event_id, |
| 5605 | struct perf_sample_data *data, |
| 5606 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5607 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5608 | if (event->attr.type != type) |
| 5609 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5610 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5611 | if (event->attr.config != event_id) |
| 5612 | return 0; |
| 5613 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5614 | if (perf_exclude_event(event, regs)) |
| 5615 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5616 | |
| 5617 | return 1; |
| 5618 | } |
| 5619 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5620 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5621 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5622 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5623 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5624 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 5625 | } |
| 5626 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5627 | static inline struct hlist_head * |
| 5628 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5629 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5630 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5631 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5632 | return &hlist->heads[hash]; |
| 5633 | } |
| 5634 | |
| 5635 | /* For the read side: events when they trigger */ |
| 5636 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5637 | 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] | 5638 | { |
| 5639 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5640 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5641 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5642 | if (!hlist) |
| 5643 | return NULL; |
| 5644 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5645 | return __find_swevent_head(hlist, type, event_id); |
| 5646 | } |
| 5647 | |
| 5648 | /* For the event head insertion and removal in the hlist */ |
| 5649 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5650 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5651 | { |
| 5652 | struct swevent_hlist *hlist; |
| 5653 | u32 event_id = event->attr.config; |
| 5654 | u64 type = event->attr.type; |
| 5655 | |
| 5656 | /* |
| 5657 | * Event scheduling is always serialized against hlist allocation |
| 5658 | * and release. Which makes the protected version suitable here. |
| 5659 | * The context lock guarantees that. |
| 5660 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5661 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5662 | lockdep_is_held(&event->ctx->lock)); |
| 5663 | if (!hlist) |
| 5664 | return NULL; |
| 5665 | |
| 5666 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | 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] | 5670 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5671 | struct perf_sample_data *data, |
| 5672 | struct pt_regs *regs) |
| 5673 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5674 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5675 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5676 | struct hlist_head *head; |
| 5677 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5678 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5679 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5680 | if (!head) |
| 5681 | goto end; |
| 5682 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5683 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5684 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5685 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5686 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5687 | end: |
| 5688 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5689 | } |
| 5690 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5691 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5692 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5693 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5694 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5695 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5696 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 5697 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5698 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 5699 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5700 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5701 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5702 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5703 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5704 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5705 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5706 | 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] | 5707 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5708 | struct perf_sample_data data; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5709 | int rctx; |
| 5710 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5711 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5712 | rctx = perf_swevent_get_recursion_context(); |
| 5713 | if (rctx < 0) |
| 5714 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5715 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 5716 | perf_sample_data_init(&data, addr, 0); |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5717 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5718 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5719 | |
| 5720 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5721 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | static void perf_swevent_read(struct perf_event *event) |
| 5725 | { |
| 5726 | } |
| 5727 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5728 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5729 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5730 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5731 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5732 | struct hlist_head *head; |
| 5733 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5734 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5735 | hwc->last_period = hwc->sample_period; |
| 5736 | perf_swevent_set_period(event); |
| 5737 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5738 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5739 | hwc->state = !(flags & PERF_EF_START); |
| 5740 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5741 | head = find_swevent_head(swhash, event); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5742 | if (!head) { |
| 5743 | /* |
| 5744 | * We can race with cpu hotplug code. Do not |
| 5745 | * WARN if the cpu just got unplugged. |
| 5746 | */ |
| 5747 | WARN_ON_ONCE(swhash->online); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5748 | return -EINVAL; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5749 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5750 | |
| 5751 | hlist_add_head_rcu(&event->hlist_entry, head); |
| 5752 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5753 | return 0; |
| 5754 | } |
| 5755 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5756 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5757 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5758 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5759 | } |
| 5760 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5761 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5762 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5763 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5764 | } |
| 5765 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5766 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5767 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5768 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5769 | } |
| 5770 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5771 | /* Deref the hlist from the update side */ |
| 5772 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5773 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5774 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5775 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 5776 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5777 | } |
| 5778 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5779 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5780 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5781 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5782 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5783 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5784 | return; |
| 5785 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5786 | rcu_assign_pointer(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 5787 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | static void swevent_hlist_put_cpu(struct perf_event *event, int cpu) |
| 5791 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5792 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5793 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5794 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5795 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5796 | if (!--swhash->hlist_refcount) |
| 5797 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5798 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5799 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | static void swevent_hlist_put(struct perf_event *event) |
| 5803 | { |
| 5804 | int cpu; |
| 5805 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5806 | for_each_possible_cpu(cpu) |
| 5807 | swevent_hlist_put_cpu(event, cpu); |
| 5808 | } |
| 5809 | |
| 5810 | static int swevent_hlist_get_cpu(struct perf_event *event, int cpu) |
| 5811 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5812 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5813 | int err = 0; |
| 5814 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5815 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5816 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5817 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5818 | struct swevent_hlist *hlist; |
| 5819 | |
| 5820 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 5821 | if (!hlist) { |
| 5822 | err = -ENOMEM; |
| 5823 | goto exit; |
| 5824 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5825 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5826 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5827 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 5828 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5829 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5830 | |
| 5831 | return err; |
| 5832 | } |
| 5833 | |
| 5834 | static int swevent_hlist_get(struct perf_event *event) |
| 5835 | { |
| 5836 | int err; |
| 5837 | int cpu, failed_cpu; |
| 5838 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5839 | get_online_cpus(); |
| 5840 | for_each_possible_cpu(cpu) { |
| 5841 | err = swevent_hlist_get_cpu(event, cpu); |
| 5842 | if (err) { |
| 5843 | failed_cpu = cpu; |
| 5844 | goto fail; |
| 5845 | } |
| 5846 | } |
| 5847 | put_online_cpus(); |
| 5848 | |
| 5849 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 5850 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5851 | for_each_possible_cpu(cpu) { |
| 5852 | if (cpu == failed_cpu) |
| 5853 | break; |
| 5854 | swevent_hlist_put_cpu(event, cpu); |
| 5855 | } |
| 5856 | |
| 5857 | put_online_cpus(); |
| 5858 | return err; |
| 5859 | } |
| 5860 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 5861 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5862 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5863 | static void sw_perf_event_destroy(struct perf_event *event) |
| 5864 | { |
| 5865 | u64 event_id = event->attr.config; |
| 5866 | |
| 5867 | WARN_ON(event->parent); |
| 5868 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 5869 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5870 | swevent_hlist_put(event); |
| 5871 | } |
| 5872 | |
| 5873 | static int perf_swevent_init(struct perf_event *event) |
| 5874 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 5875 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5876 | |
| 5877 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 5878 | return -ENOENT; |
| 5879 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 5880 | /* |
| 5881 | * no branch sampling for software events |
| 5882 | */ |
| 5883 | if (has_branch_stack(event)) |
| 5884 | return -EOPNOTSUPP; |
| 5885 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5886 | switch (event_id) { |
| 5887 | case PERF_COUNT_SW_CPU_CLOCK: |
| 5888 | case PERF_COUNT_SW_TASK_CLOCK: |
| 5889 | return -ENOENT; |
| 5890 | |
| 5891 | default: |
| 5892 | break; |
| 5893 | } |
| 5894 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 5895 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5896 | return -ENOENT; |
| 5897 | |
| 5898 | if (!event->parent) { |
| 5899 | int err; |
| 5900 | |
| 5901 | err = swevent_hlist_get(event); |
| 5902 | if (err) |
| 5903 | return err; |
| 5904 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 5905 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5906 | event->destroy = sw_perf_event_destroy; |
| 5907 | } |
| 5908 | |
| 5909 | return 0; |
| 5910 | } |
| 5911 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 5912 | static int perf_swevent_event_idx(struct perf_event *event) |
| 5913 | { |
| 5914 | return 0; |
| 5915 | } |
| 5916 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5917 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 5918 | .task_ctx_nr = perf_sw_context, |
| 5919 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5920 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5921 | .add = perf_swevent_add, |
| 5922 | .del = perf_swevent_del, |
| 5923 | .start = perf_swevent_start, |
| 5924 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5925 | .read = perf_swevent_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 5926 | |
| 5927 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5928 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5929 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5930 | #ifdef CONFIG_EVENT_TRACING |
| 5931 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5932 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5933 | struct perf_sample_data *data) |
| 5934 | { |
| 5935 | void *record = data->raw->data; |
| 5936 | |
| 5937 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 5938 | return 1; |
| 5939 | return 0; |
| 5940 | } |
| 5941 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5942 | static int perf_tp_event_match(struct perf_event *event, |
| 5943 | struct perf_sample_data *data, |
| 5944 | struct pt_regs *regs) |
| 5945 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 5946 | if (event->hw.state & PERF_HES_STOPPED) |
| 5947 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 5948 | /* |
| 5949 | * All tracepoints are from kernel-space. |
| 5950 | */ |
| 5951 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5952 | return 0; |
| 5953 | |
| 5954 | if (!perf_tp_filter_match(event, data)) |
| 5955 | return 0; |
| 5956 | |
| 5957 | return 1; |
| 5958 | } |
| 5959 | |
| 5960 | 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] | 5961 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 5962 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5963 | { |
| 5964 | struct perf_sample_data data; |
| 5965 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5966 | |
| 5967 | struct perf_raw_record raw = { |
| 5968 | .size = entry_size, |
| 5969 | .data = record, |
| 5970 | }; |
| 5971 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 5972 | perf_sample_data_init(&data, addr, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5973 | data.raw = &raw; |
| 5974 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5975 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5976 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5977 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5978 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 5979 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5980 | /* |
| 5981 | * If we got specified a target task, also iterate its context and |
| 5982 | * deliver this event there too. |
| 5983 | */ |
| 5984 | if (task && task != current) { |
| 5985 | struct perf_event_context *ctx; |
| 5986 | struct trace_entry *entry = record; |
| 5987 | |
| 5988 | rcu_read_lock(); |
| 5989 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 5990 | if (!ctx) |
| 5991 | goto unlock; |
| 5992 | |
| 5993 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 5994 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 5995 | continue; |
| 5996 | if (event->attr.config != entry->type) |
| 5997 | continue; |
| 5998 | if (perf_tp_event_match(event, &data, regs)) |
| 5999 | perf_swevent_event(event, count, &data, regs); |
| 6000 | } |
| 6001 | unlock: |
| 6002 | rcu_read_unlock(); |
| 6003 | } |
| 6004 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 6005 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6006 | } |
| 6007 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 6008 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6009 | static void tp_perf_event_destroy(struct perf_event *event) |
| 6010 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6011 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6012 | } |
| 6013 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6014 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6015 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6016 | int err; |
| 6017 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6018 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6019 | return -ENOENT; |
| 6020 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6021 | /* |
| 6022 | * no branch sampling for tracepoint events |
| 6023 | */ |
| 6024 | if (has_branch_stack(event)) |
| 6025 | return -EOPNOTSUPP; |
| 6026 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6027 | err = perf_trace_init(event); |
| 6028 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6029 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6030 | |
| 6031 | event->destroy = tp_perf_event_destroy; |
| 6032 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6033 | return 0; |
| 6034 | } |
| 6035 | |
| 6036 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6037 | .task_ctx_nr = perf_sw_context, |
| 6038 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6039 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6040 | .add = perf_trace_add, |
| 6041 | .del = perf_trace_del, |
| 6042 | .start = perf_swevent_start, |
| 6043 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6044 | .read = perf_swevent_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6045 | |
| 6046 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6047 | }; |
| 6048 | |
| 6049 | static inline void perf_tp_register(void) |
| 6050 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6051 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6052 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6053 | |
| 6054 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6055 | { |
| 6056 | char *filter_str; |
| 6057 | int ret; |
| 6058 | |
| 6059 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6060 | return -EINVAL; |
| 6061 | |
| 6062 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 6063 | if (IS_ERR(filter_str)) |
| 6064 | return PTR_ERR(filter_str); |
| 6065 | |
| 6066 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); |
| 6067 | |
| 6068 | kfree(filter_str); |
| 6069 | return ret; |
| 6070 | } |
| 6071 | |
| 6072 | static void perf_event_free_filter(struct perf_event *event) |
| 6073 | { |
| 6074 | ftrace_profile_free_filter(event); |
| 6075 | } |
| 6076 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6077 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6078 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6079 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6080 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6081 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6082 | |
| 6083 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6084 | { |
| 6085 | return -ENOENT; |
| 6086 | } |
| 6087 | |
| 6088 | static void perf_event_free_filter(struct perf_event *event) |
| 6089 | { |
| 6090 | } |
| 6091 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 6092 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6093 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6094 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6095 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6096 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6097 | struct perf_sample_data sample; |
| 6098 | struct pt_regs *regs = data; |
| 6099 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6100 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6101 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6102 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6103 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6104 | } |
| 6105 | #endif |
| 6106 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6107 | /* |
| 6108 | * hrtimer based swevent callback |
| 6109 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6110 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6111 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6112 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6113 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 6114 | struct perf_sample_data data; |
| 6115 | struct pt_regs *regs; |
| 6116 | struct perf_event *event; |
| 6117 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6118 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6119 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6120 | |
| 6121 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 6122 | return HRTIMER_NORESTART; |
| 6123 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6124 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6125 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6126 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6127 | regs = get_irq_regs(); |
| 6128 | |
| 6129 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 6130 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 6131 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6132 | ret = HRTIMER_NORESTART; |
| 6133 | } |
| 6134 | |
| 6135 | period = max_t(u64, 10000, event->hw.sample_period); |
| 6136 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 6137 | |
| 6138 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6139 | } |
| 6140 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6141 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6142 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6143 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6144 | s64 period; |
| 6145 | |
| 6146 | if (!is_sampling_event(event)) |
| 6147 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6148 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6149 | period = local64_read(&hwc->period_left); |
| 6150 | if (period) { |
| 6151 | if (period < 0) |
| 6152 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6153 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6154 | local64_set(&hwc->period_left, 0); |
| 6155 | } else { |
| 6156 | period = max_t(u64, 10000, hwc->sample_period); |
| 6157 | } |
| 6158 | __hrtimer_start_range_ns(&hwc->hrtimer, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6159 | ns_to_ktime(period), 0, |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 6160 | HRTIMER_MODE_REL_PINNED, 0); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 6164 | { |
| 6165 | struct hw_perf_event *hwc = &event->hw; |
| 6166 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6167 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6168 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6169 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6170 | |
| 6171 | hrtimer_cancel(&hwc->hrtimer); |
| 6172 | } |
| 6173 | } |
| 6174 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6175 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 6176 | { |
| 6177 | struct hw_perf_event *hwc = &event->hw; |
| 6178 | |
| 6179 | if (!is_sampling_event(event)) |
| 6180 | return; |
| 6181 | |
| 6182 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 6183 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 6184 | |
| 6185 | /* |
| 6186 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 6187 | * mapping and avoid the whole period adjust feedback stuff. |
| 6188 | */ |
| 6189 | if (event->attr.freq) { |
| 6190 | long freq = event->attr.sample_freq; |
| 6191 | |
| 6192 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 6193 | hwc->sample_period = event->attr.sample_period; |
| 6194 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 6195 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6196 | event->attr.freq = 0; |
| 6197 | } |
| 6198 | } |
| 6199 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6200 | /* |
| 6201 | * Software event: cpu wall time clock |
| 6202 | */ |
| 6203 | |
| 6204 | static void cpu_clock_event_update(struct perf_event *event) |
| 6205 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6206 | s64 prev; |
| 6207 | u64 now; |
| 6208 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6209 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6210 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6211 | local64_add(now - prev, &event->count); |
| 6212 | } |
| 6213 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6214 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6215 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6216 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6217 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6218 | } |
| 6219 | |
| 6220 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 6221 | { |
| 6222 | perf_swevent_cancel_hrtimer(event); |
| 6223 | cpu_clock_event_update(event); |
| 6224 | } |
| 6225 | |
| 6226 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 6227 | { |
| 6228 | if (flags & PERF_EF_START) |
| 6229 | cpu_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6230 | |
| 6231 | return 0; |
| 6232 | } |
| 6233 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6234 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6235 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6236 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6237 | } |
| 6238 | |
| 6239 | static void cpu_clock_event_read(struct perf_event *event) |
| 6240 | { |
| 6241 | cpu_clock_event_update(event); |
| 6242 | } |
| 6243 | |
| 6244 | static int cpu_clock_event_init(struct perf_event *event) |
| 6245 | { |
| 6246 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6247 | return -ENOENT; |
| 6248 | |
| 6249 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 6250 | return -ENOENT; |
| 6251 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6252 | /* |
| 6253 | * no branch sampling for software events |
| 6254 | */ |
| 6255 | if (has_branch_stack(event)) |
| 6256 | return -EOPNOTSUPP; |
| 6257 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6258 | perf_swevent_init_hrtimer(event); |
| 6259 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6260 | return 0; |
| 6261 | } |
| 6262 | |
| 6263 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6264 | .task_ctx_nr = perf_sw_context, |
| 6265 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6266 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6267 | .add = cpu_clock_event_add, |
| 6268 | .del = cpu_clock_event_del, |
| 6269 | .start = cpu_clock_event_start, |
| 6270 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6271 | .read = cpu_clock_event_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6272 | |
| 6273 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6274 | }; |
| 6275 | |
| 6276 | /* |
| 6277 | * Software event: task time clock |
| 6278 | */ |
| 6279 | |
| 6280 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 6281 | { |
| 6282 | u64 prev; |
| 6283 | s64 delta; |
| 6284 | |
| 6285 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6286 | delta = now - prev; |
| 6287 | local64_add(delta, &event->count); |
| 6288 | } |
| 6289 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6290 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6291 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6292 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6293 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 6297 | { |
| 6298 | perf_swevent_cancel_hrtimer(event); |
| 6299 | task_clock_event_update(event, event->ctx->time); |
| 6300 | } |
| 6301 | |
| 6302 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 6303 | { |
| 6304 | if (flags & PERF_EF_START) |
| 6305 | task_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6306 | |
| 6307 | return 0; |
| 6308 | } |
| 6309 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6310 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6311 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6312 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6313 | } |
| 6314 | |
| 6315 | static void task_clock_event_read(struct perf_event *event) |
| 6316 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 6317 | u64 now = perf_clock(); |
| 6318 | u64 delta = now - event->ctx->timestamp; |
| 6319 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6320 | |
| 6321 | task_clock_event_update(event, time); |
| 6322 | } |
| 6323 | |
| 6324 | static int task_clock_event_init(struct perf_event *event) |
| 6325 | { |
| 6326 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6327 | return -ENOENT; |
| 6328 | |
| 6329 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 6330 | return -ENOENT; |
| 6331 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6332 | /* |
| 6333 | * no branch sampling for software events |
| 6334 | */ |
| 6335 | if (has_branch_stack(event)) |
| 6336 | return -EOPNOTSUPP; |
| 6337 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6338 | perf_swevent_init_hrtimer(event); |
| 6339 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6340 | return 0; |
| 6341 | } |
| 6342 | |
| 6343 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6344 | .task_ctx_nr = perf_sw_context, |
| 6345 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6346 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6347 | .add = task_clock_event_add, |
| 6348 | .del = task_clock_event_del, |
| 6349 | .start = task_clock_event_start, |
| 6350 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6351 | .read = task_clock_event_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6352 | |
| 6353 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6354 | }; |
| 6355 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6356 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 6357 | { |
| 6358 | } |
| 6359 | |
| 6360 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 6361 | { |
| 6362 | return 0; |
| 6363 | } |
| 6364 | |
| 6365 | static void perf_pmu_start_txn(struct pmu *pmu) |
| 6366 | { |
| 6367 | perf_pmu_disable(pmu); |
| 6368 | } |
| 6369 | |
| 6370 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 6371 | { |
| 6372 | perf_pmu_enable(pmu); |
| 6373 | return 0; |
| 6374 | } |
| 6375 | |
| 6376 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 6377 | { |
| 6378 | perf_pmu_enable(pmu); |
| 6379 | } |
| 6380 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6381 | static int perf_event_idx_default(struct perf_event *event) |
| 6382 | { |
| 6383 | return event->hw.idx + 1; |
| 6384 | } |
| 6385 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6386 | /* |
| 6387 | * Ensures all contexts with the same task_ctx_nr have the same |
| 6388 | * pmu_cpu_context too. |
| 6389 | */ |
Mark Rutland | 9e31704 | 2014-02-10 17:44:18 +0000 | [diff] [blame] | 6390 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6391 | { |
| 6392 | struct pmu *pmu; |
| 6393 | |
| 6394 | if (ctxn < 0) |
| 6395 | return NULL; |
| 6396 | |
| 6397 | list_for_each_entry(pmu, &pmus, entry) { |
| 6398 | if (pmu->task_ctx_nr == ctxn) |
| 6399 | return pmu->pmu_cpu_context; |
| 6400 | } |
| 6401 | |
| 6402 | return NULL; |
| 6403 | } |
| 6404 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6405 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6406 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6407 | int cpu; |
| 6408 | |
| 6409 | for_each_possible_cpu(cpu) { |
| 6410 | struct perf_cpu_context *cpuctx; |
| 6411 | |
| 6412 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6413 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6414 | if (cpuctx->unique_pmu == old_pmu) |
| 6415 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6416 | } |
| 6417 | } |
| 6418 | |
| 6419 | static void free_pmu_context(struct pmu *pmu) |
| 6420 | { |
| 6421 | struct pmu *i; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6422 | |
| 6423 | mutex_lock(&pmus_lock); |
| 6424 | /* |
| 6425 | * Like a real lame refcount. |
| 6426 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6427 | list_for_each_entry(i, &pmus, entry) { |
| 6428 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 6429 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6430 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6431 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6432 | } |
| 6433 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6434 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6435 | out: |
| 6436 | mutex_unlock(&pmus_lock); |
| 6437 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6438 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6439 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6440 | static ssize_t |
| 6441 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 6442 | { |
| 6443 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6444 | |
| 6445 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 6446 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6447 | static DEVICE_ATTR_RO(type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6448 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6449 | static ssize_t |
| 6450 | perf_event_mux_interval_ms_show(struct device *dev, |
| 6451 | struct device_attribute *attr, |
| 6452 | char *page) |
| 6453 | { |
| 6454 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6455 | |
| 6456 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 6457 | } |
| 6458 | |
| 6459 | static ssize_t |
| 6460 | perf_event_mux_interval_ms_store(struct device *dev, |
| 6461 | struct device_attribute *attr, |
| 6462 | const char *buf, size_t count) |
| 6463 | { |
| 6464 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6465 | int timer, cpu, ret; |
| 6466 | |
| 6467 | ret = kstrtoint(buf, 0, &timer); |
| 6468 | if (ret) |
| 6469 | return ret; |
| 6470 | |
| 6471 | if (timer < 1) |
| 6472 | return -EINVAL; |
| 6473 | |
| 6474 | /* same value, noting to do */ |
| 6475 | if (timer == pmu->hrtimer_interval_ms) |
| 6476 | return count; |
| 6477 | |
| 6478 | pmu->hrtimer_interval_ms = timer; |
| 6479 | |
| 6480 | /* update all cpuctx for this PMU */ |
| 6481 | for_each_possible_cpu(cpu) { |
| 6482 | struct perf_cpu_context *cpuctx; |
| 6483 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6484 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 6485 | |
| 6486 | if (hrtimer_active(&cpuctx->hrtimer)) |
| 6487 | hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval); |
| 6488 | } |
| 6489 | |
| 6490 | return count; |
| 6491 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6492 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6493 | |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6494 | static struct attribute *pmu_dev_attrs[] = { |
| 6495 | &dev_attr_type.attr, |
| 6496 | &dev_attr_perf_event_mux_interval_ms.attr, |
| 6497 | NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6498 | }; |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6499 | ATTRIBUTE_GROUPS(pmu_dev); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6500 | |
| 6501 | static int pmu_bus_running; |
| 6502 | static struct bus_type pmu_bus = { |
| 6503 | .name = "event_source", |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6504 | .dev_groups = pmu_dev_groups, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6505 | }; |
| 6506 | |
| 6507 | static void pmu_dev_release(struct device *dev) |
| 6508 | { |
| 6509 | kfree(dev); |
| 6510 | } |
| 6511 | |
| 6512 | static int pmu_dev_alloc(struct pmu *pmu) |
| 6513 | { |
| 6514 | int ret = -ENOMEM; |
| 6515 | |
| 6516 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 6517 | if (!pmu->dev) |
| 6518 | goto out; |
| 6519 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 6520 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6521 | device_initialize(pmu->dev); |
| 6522 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 6523 | if (ret) |
| 6524 | goto free_dev; |
| 6525 | |
| 6526 | dev_set_drvdata(pmu->dev, pmu); |
| 6527 | pmu->dev->bus = &pmu_bus; |
| 6528 | pmu->dev->release = pmu_dev_release; |
| 6529 | ret = device_add(pmu->dev); |
| 6530 | if (ret) |
| 6531 | goto free_dev; |
| 6532 | |
| 6533 | out: |
| 6534 | return ret; |
| 6535 | |
| 6536 | free_dev: |
| 6537 | put_device(pmu->dev); |
| 6538 | goto out; |
| 6539 | } |
| 6540 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6541 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6542 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6543 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 6544 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6545 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6546 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6547 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6548 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6549 | ret = -ENOMEM; |
| 6550 | pmu->pmu_disable_count = alloc_percpu(int); |
| 6551 | if (!pmu->pmu_disable_count) |
| 6552 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6553 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6554 | pmu->type = -1; |
| 6555 | if (!name) |
| 6556 | goto skip_type; |
| 6557 | pmu->name = name; |
| 6558 | |
| 6559 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 6560 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 6561 | if (type < 0) { |
| 6562 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6563 | goto free_pdc; |
| 6564 | } |
| 6565 | } |
| 6566 | pmu->type = type; |
| 6567 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6568 | if (pmu_bus_running) { |
| 6569 | ret = pmu_dev_alloc(pmu); |
| 6570 | if (ret) |
| 6571 | goto free_idr; |
| 6572 | } |
| 6573 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6574 | skip_type: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6575 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 6576 | if (pmu->pmu_cpu_context) |
| 6577 | goto got_cpu_context; |
| 6578 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 6579 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6580 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 6581 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6582 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6583 | |
| 6584 | for_each_possible_cpu(cpu) { |
| 6585 | struct perf_cpu_context *cpuctx; |
| 6586 | |
| 6587 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 6588 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6589 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6590 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6591 | cpuctx->ctx.type = cpu_context; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6592 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 6593 | |
| 6594 | __perf_cpu_hrtimer_init(cpuctx, cpu); |
| 6595 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 6596 | INIT_LIST_HEAD(&cpuctx->rotation_list); |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6597 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6598 | } |
| 6599 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6600 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6601 | if (!pmu->start_txn) { |
| 6602 | if (pmu->pmu_enable) { |
| 6603 | /* |
| 6604 | * If we have pmu_enable/pmu_disable calls, install |
| 6605 | * transaction stubs that use that to try and batch |
| 6606 | * hardware accesses. |
| 6607 | */ |
| 6608 | pmu->start_txn = perf_pmu_start_txn; |
| 6609 | pmu->commit_txn = perf_pmu_commit_txn; |
| 6610 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 6611 | } else { |
| 6612 | pmu->start_txn = perf_pmu_nop_void; |
| 6613 | pmu->commit_txn = perf_pmu_nop_int; |
| 6614 | pmu->cancel_txn = perf_pmu_nop_void; |
| 6615 | } |
| 6616 | } |
| 6617 | |
| 6618 | if (!pmu->pmu_enable) { |
| 6619 | pmu->pmu_enable = perf_pmu_nop_void; |
| 6620 | pmu->pmu_disable = perf_pmu_nop_void; |
| 6621 | } |
| 6622 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6623 | if (!pmu->event_idx) |
| 6624 | pmu->event_idx = perf_event_idx_default; |
| 6625 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6626 | list_add_rcu(&pmu->entry, &pmus); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6627 | ret = 0; |
| 6628 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6629 | mutex_unlock(&pmus_lock); |
| 6630 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6631 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6632 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6633 | free_dev: |
| 6634 | device_del(pmu->dev); |
| 6635 | put_device(pmu->dev); |
| 6636 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6637 | free_idr: |
| 6638 | if (pmu->type >= PERF_TYPE_MAX) |
| 6639 | idr_remove(&pmu_idr, pmu->type); |
| 6640 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6641 | free_pdc: |
| 6642 | free_percpu(pmu->pmu_disable_count); |
| 6643 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6644 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6645 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6646 | |
| 6647 | void perf_pmu_unregister(struct pmu *pmu) |
| 6648 | { |
| 6649 | mutex_lock(&pmus_lock); |
| 6650 | list_del_rcu(&pmu->entry); |
| 6651 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6652 | |
| 6653 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6654 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 6655 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6656 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6657 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6658 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6659 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6660 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6661 | if (pmu->type >= PERF_TYPE_MAX) |
| 6662 | idr_remove(&pmu_idr, pmu->type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6663 | device_del(pmu->dev); |
| 6664 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6665 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6666 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6667 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6668 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6669 | struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6670 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6671 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6672 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6673 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6674 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6675 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6676 | |
| 6677 | rcu_read_lock(); |
| 6678 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 6679 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6680 | if (pmu) { |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6681 | if (!try_module_get(pmu->module)) { |
| 6682 | pmu = ERR_PTR(-ENODEV); |
| 6683 | goto unlock; |
| 6684 | } |
Mark Rutland | 7e5b2a0 | 2011-08-11 12:31:20 +0100 | [diff] [blame] | 6685 | event->pmu = pmu; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6686 | ret = pmu->event_init(event); |
| 6687 | if (ret) |
| 6688 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6689 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6690 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6691 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6692 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6693 | if (!try_module_get(pmu->module)) { |
| 6694 | pmu = ERR_PTR(-ENODEV); |
| 6695 | goto unlock; |
| 6696 | } |
Mark Rutland | 7e5b2a0 | 2011-08-11 12:31:20 +0100 | [diff] [blame] | 6697 | event->pmu = pmu; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6698 | ret = pmu->event_init(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6699 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6700 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6701 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6702 | if (ret != -ENOENT) { |
| 6703 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6704 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6705 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6706 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6707 | pmu = ERR_PTR(-ENOENT); |
| 6708 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6709 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6710 | |
| 6711 | return pmu; |
| 6712 | } |
| 6713 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6714 | static void account_event_cpu(struct perf_event *event, int cpu) |
| 6715 | { |
| 6716 | if (event->parent) |
| 6717 | return; |
| 6718 | |
| 6719 | if (has_branch_stack(event)) { |
| 6720 | if (!(event->attach_state & PERF_ATTACH_TASK)) |
| 6721 | atomic_inc(&per_cpu(perf_branch_stack_events, cpu)); |
| 6722 | } |
| 6723 | if (is_cgroup_event(event)) |
| 6724 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); |
| 6725 | } |
| 6726 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6727 | static void account_event(struct perf_event *event) |
| 6728 | { |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6729 | if (event->parent) |
| 6730 | return; |
| 6731 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6732 | if (event->attach_state & PERF_ATTACH_TASK) |
| 6733 | static_key_slow_inc(&perf_sched_events.key); |
| 6734 | if (event->attr.mmap || event->attr.mmap_data) |
| 6735 | atomic_inc(&nr_mmap_events); |
| 6736 | if (event->attr.comm) |
| 6737 | atomic_inc(&nr_comm_events); |
| 6738 | if (event->attr.task) |
| 6739 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 6740 | if (event->attr.freq) { |
| 6741 | if (atomic_inc_return(&nr_freq_events) == 1) |
| 6742 | tick_nohz_full_kick_all(); |
| 6743 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6744 | if (has_branch_stack(event)) |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6745 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6746 | if (is_cgroup_event(event)) |
| 6747 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6748 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6749 | account_event_cpu(event, event->cpu); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6750 | } |
| 6751 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6752 | /* |
| 6753 | * Allocate and initialize a event structure |
| 6754 | */ |
| 6755 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6756 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6757 | struct task_struct *task, |
| 6758 | struct perf_event *group_leader, |
| 6759 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6760 | perf_overflow_handler_t overflow_handler, |
| 6761 | void *context) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6762 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6763 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6764 | struct perf_event *event; |
| 6765 | struct hw_perf_event *hwc; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6766 | long err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6767 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 6768 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 6769 | if (!task || cpu != -1) |
| 6770 | return ERR_PTR(-EINVAL); |
| 6771 | } |
| 6772 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6773 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6774 | if (!event) |
| 6775 | return ERR_PTR(-ENOMEM); |
| 6776 | |
| 6777 | /* |
| 6778 | * Single events are their own group leaders, with an |
| 6779 | * empty sibling list: |
| 6780 | */ |
| 6781 | if (!group_leader) |
| 6782 | group_leader = event; |
| 6783 | |
| 6784 | mutex_init(&event->child_mutex); |
| 6785 | INIT_LIST_HEAD(&event->child_list); |
| 6786 | |
| 6787 | INIT_LIST_HEAD(&event->group_entry); |
| 6788 | INIT_LIST_HEAD(&event->event_entry); |
| 6789 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 6790 | INIT_LIST_HEAD(&event->rb_entry); |
Stephane Eranian | 71ad88e | 2013-11-12 17:58:48 +0100 | [diff] [blame] | 6791 | INIT_LIST_HEAD(&event->active_entry); |
Stephane Eranian | f3ae75d | 2014-01-08 11:15:52 +0100 | [diff] [blame] | 6792 | INIT_HLIST_NODE(&event->hlist_entry); |
| 6793 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 6794 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6795 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 6796 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6797 | |
| 6798 | mutex_init(&event->mmap_mutex); |
| 6799 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 6800 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6801 | event->cpu = cpu; |
| 6802 | event->attr = *attr; |
| 6803 | event->group_leader = group_leader; |
| 6804 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6805 | event->oncpu = -1; |
| 6806 | |
| 6807 | event->parent = parent_event; |
| 6808 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 6809 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6810 | event->id = atomic64_inc_return(&perf_event_id); |
| 6811 | |
| 6812 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 6813 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6814 | if (task) { |
| 6815 | event->attach_state = PERF_ATTACH_TASK; |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 6816 | |
| 6817 | if (attr->type == PERF_TYPE_TRACEPOINT) |
| 6818 | event->hw.tp_target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6819 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
| 6820 | /* |
| 6821 | * hw_breakpoint is a bit difficult here.. |
| 6822 | */ |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 6823 | else if (attr->type == PERF_TYPE_BREAKPOINT) |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6824 | event->hw.bp_target = task; |
| 6825 | #endif |
| 6826 | } |
| 6827 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6828 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 6829 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6830 | context = parent_event->overflow_handler_context; |
| 6831 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 6832 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 6833 | event->overflow_handler = overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6834 | event->overflow_handler_context = context; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 6835 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 6836 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6837 | |
| 6838 | pmu = NULL; |
| 6839 | |
| 6840 | hwc = &event->hw; |
| 6841 | hwc->sample_period = attr->sample_period; |
| 6842 | if (attr->freq && attr->sample_freq) |
| 6843 | hwc->sample_period = 1; |
| 6844 | hwc->last_period = hwc->sample_period; |
| 6845 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6846 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6847 | |
| 6848 | /* |
| 6849 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 6850 | */ |
| 6851 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6852 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6853 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6854 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6855 | if (!pmu) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6856 | goto err_ns; |
| 6857 | else if (IS_ERR(pmu)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6858 | err = PTR_ERR(pmu); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6859 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6860 | } |
| 6861 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6862 | if (!event->parent) { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6863 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 6864 | err = get_callchain_buffers(); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6865 | if (err) |
| 6866 | goto err_pmu; |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 6867 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6868 | } |
| 6869 | |
| 6870 | return event; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6871 | |
| 6872 | err_pmu: |
| 6873 | if (event->destroy) |
| 6874 | event->destroy(event); |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6875 | module_put(pmu->module); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6876 | err_ns: |
| 6877 | if (event->ns) |
| 6878 | put_pid_ns(event->ns); |
| 6879 | kfree(event); |
| 6880 | |
| 6881 | return ERR_PTR(err); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 6885 | struct perf_event_attr *attr) |
| 6886 | { |
| 6887 | u32 size; |
| 6888 | int ret; |
| 6889 | |
| 6890 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 6891 | return -EFAULT; |
| 6892 | |
| 6893 | /* |
| 6894 | * zero the full structure, so that a short copy will be nice. |
| 6895 | */ |
| 6896 | memset(attr, 0, sizeof(*attr)); |
| 6897 | |
| 6898 | ret = get_user(size, &uattr->size); |
| 6899 | if (ret) |
| 6900 | return ret; |
| 6901 | |
| 6902 | if (size > PAGE_SIZE) /* silly large */ |
| 6903 | goto err_size; |
| 6904 | |
| 6905 | if (!size) /* abi compat */ |
| 6906 | size = PERF_ATTR_SIZE_VER0; |
| 6907 | |
| 6908 | if (size < PERF_ATTR_SIZE_VER0) |
| 6909 | goto err_size; |
| 6910 | |
| 6911 | /* |
| 6912 | * If we're handed a bigger struct than we know of, |
| 6913 | * ensure all the unknown bits are 0 - i.e. new |
| 6914 | * user-space does not rely on any kernel feature |
| 6915 | * extensions we dont know about yet. |
| 6916 | */ |
| 6917 | if (size > sizeof(*attr)) { |
| 6918 | unsigned char __user *addr; |
| 6919 | unsigned char __user *end; |
| 6920 | unsigned char val; |
| 6921 | |
| 6922 | addr = (void __user *)uattr + sizeof(*attr); |
| 6923 | end = (void __user *)uattr + size; |
| 6924 | |
| 6925 | for (; addr < end; addr++) { |
| 6926 | ret = get_user(val, addr); |
| 6927 | if (ret) |
| 6928 | return ret; |
| 6929 | if (val) |
| 6930 | goto err_size; |
| 6931 | } |
| 6932 | size = sizeof(*attr); |
| 6933 | } |
| 6934 | |
| 6935 | ret = copy_from_user(attr, uattr, size); |
| 6936 | if (ret) |
| 6937 | return -EFAULT; |
| 6938 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 6939 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6940 | return -EINVAL; |
| 6941 | |
| 6942 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 6943 | return -EINVAL; |
| 6944 | |
| 6945 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 6946 | return -EINVAL; |
| 6947 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 6948 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 6949 | u64 mask = attr->branch_sample_type; |
| 6950 | |
| 6951 | /* only using defined bits */ |
| 6952 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 6953 | return -EINVAL; |
| 6954 | |
| 6955 | /* at least one branch bit must be set */ |
| 6956 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 6957 | return -EINVAL; |
| 6958 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 6959 | /* propagate priv level, when not set for branch */ |
| 6960 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 6961 | |
| 6962 | /* exclude_kernel checked on syscall entry */ |
| 6963 | if (!attr->exclude_kernel) |
| 6964 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 6965 | |
| 6966 | if (!attr->exclude_user) |
| 6967 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 6968 | |
| 6969 | if (!attr->exclude_hv) |
| 6970 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 6971 | /* |
| 6972 | * adjust user setting (for HW filter setup) |
| 6973 | */ |
| 6974 | attr->branch_sample_type = mask; |
| 6975 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 6976 | /* privileged levels capture (kernel, hv): check permissions */ |
| 6977 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 6978 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 6979 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 6980 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 6981 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6982 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 6983 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6984 | if (ret) |
| 6985 | return ret; |
| 6986 | } |
| 6987 | |
| 6988 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 6989 | if (!arch_perf_have_user_stack_dump()) |
| 6990 | return -ENOSYS; |
| 6991 | |
| 6992 | /* |
| 6993 | * We have __u32 type for the size, but so far |
| 6994 | * we can only use __u16 as maximum due to the |
| 6995 | * __u16 sample size limit. |
| 6996 | */ |
| 6997 | if (attr->sample_stack_user >= USHRT_MAX) |
| 6998 | ret = -EINVAL; |
| 6999 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
| 7000 | ret = -EINVAL; |
| 7001 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7002 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7003 | out: |
| 7004 | return ret; |
| 7005 | |
| 7006 | err_size: |
| 7007 | put_user(sizeof(*attr), &uattr->size); |
| 7008 | ret = -E2BIG; |
| 7009 | goto out; |
| 7010 | } |
| 7011 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7012 | static int |
| 7013 | 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] | 7014 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7015 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7016 | int ret = -EINVAL; |
| 7017 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7018 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7019 | goto set; |
| 7020 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7021 | /* don't allow circular references */ |
| 7022 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7023 | goto out; |
| 7024 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 7025 | /* |
| 7026 | * Don't allow cross-cpu buffers |
| 7027 | */ |
| 7028 | if (output_event->cpu != event->cpu) |
| 7029 | goto out; |
| 7030 | |
| 7031 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7032 | * 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] | 7033 | */ |
| 7034 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 7035 | goto out; |
| 7036 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7037 | set: |
| 7038 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7039 | /* Can't redirect output if we've got an active mmap() */ |
| 7040 | if (atomic_read(&event->mmap_count)) |
| 7041 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7042 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7043 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7044 | /* get the rb we want to redirect to */ |
| 7045 | rb = ring_buffer_get(output_event); |
| 7046 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7047 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7048 | } |
| 7049 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7050 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 7051 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7052 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7053 | unlock: |
| 7054 | mutex_unlock(&event->mmap_mutex); |
| 7055 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7056 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7057 | return ret; |
| 7058 | } |
| 7059 | |
| 7060 | /** |
| 7061 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 7062 | * |
| 7063 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 7064 | * @pid: target pid |
| 7065 | * @cpu: target cpu |
| 7066 | * @group_fd: group leader event fd |
| 7067 | */ |
| 7068 | SYSCALL_DEFINE5(perf_event_open, |
| 7069 | struct perf_event_attr __user *, attr_uptr, |
| 7070 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 7071 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7072 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 7073 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7074 | struct perf_event_attr attr; |
| 7075 | struct perf_event_context *ctx; |
| 7076 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7077 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7078 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7079 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7080 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7081 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7082 | int err; |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7083 | int f_flags = O_RDWR; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7084 | |
| 7085 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7086 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7087 | return -EINVAL; |
| 7088 | |
| 7089 | err = perf_copy_attr(attr_uptr, &attr); |
| 7090 | if (err) |
| 7091 | return err; |
| 7092 | |
| 7093 | if (!attr.exclude_kernel) { |
| 7094 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 7095 | return -EACCES; |
| 7096 | } |
| 7097 | |
| 7098 | if (attr.freq) { |
| 7099 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 7100 | return -EINVAL; |
Peter Zijlstra | 0819b2e | 2014-05-15 20:23:48 +0200 | [diff] [blame] | 7101 | } else { |
| 7102 | if (attr.sample_period & (1ULL << 63)) |
| 7103 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7104 | } |
| 7105 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7106 | /* |
| 7107 | * In cgroup mode, the pid argument is used to pass the fd |
| 7108 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 7109 | * designates the cpu on which to monitor threads from that |
| 7110 | * cgroup. |
| 7111 | */ |
| 7112 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 7113 | return -EINVAL; |
| 7114 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7115 | if (flags & PERF_FLAG_FD_CLOEXEC) |
| 7116 | f_flags |= O_CLOEXEC; |
| 7117 | |
| 7118 | event_fd = get_unused_fd_flags(f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7119 | if (event_fd < 0) |
| 7120 | return event_fd; |
| 7121 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7122 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7123 | err = perf_fget_light(group_fd, &group); |
| 7124 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7125 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7126 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7127 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 7128 | output_event = group_leader; |
| 7129 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 7130 | group_leader = NULL; |
| 7131 | } |
| 7132 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7133 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7134 | task = find_lively_task_by_vpid(pid); |
| 7135 | if (IS_ERR(task)) { |
| 7136 | err = PTR_ERR(task); |
| 7137 | goto err_group_fd; |
| 7138 | } |
| 7139 | } |
| 7140 | |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7141 | if (task && group_leader && |
| 7142 | group_leader->attr.inherit != attr.inherit) { |
| 7143 | err = -EINVAL; |
| 7144 | goto err_task; |
| 7145 | } |
| 7146 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7147 | get_online_cpus(); |
| 7148 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7149 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
| 7150 | NULL, NULL); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7151 | if (IS_ERR(event)) { |
| 7152 | err = PTR_ERR(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7153 | goto err_cpus; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7154 | } |
| 7155 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7156 | if (flags & PERF_FLAG_PID_CGROUP) { |
| 7157 | err = perf_cgroup_connect(pid, event, &attr, group_leader); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7158 | if (err) { |
| 7159 | __free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7160 | goto err_cpus; |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7161 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7162 | } |
| 7163 | |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 7164 | if (is_sampling_event(event)) { |
| 7165 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { |
| 7166 | err = -ENOTSUPP; |
| 7167 | goto err_alloc; |
| 7168 | } |
| 7169 | } |
| 7170 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7171 | account_event(event); |
| 7172 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7173 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7174 | * Special case software events and allow them to be part of |
| 7175 | * any hardware group. |
| 7176 | */ |
| 7177 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7178 | |
| 7179 | if (group_leader && |
| 7180 | (is_software_event(event) != is_software_event(group_leader))) { |
| 7181 | if (is_software_event(event)) { |
| 7182 | /* |
| 7183 | * If event and group_leader are not both a software |
| 7184 | * event, and event is, then group leader is not. |
| 7185 | * |
| 7186 | * Allow the addition of software events to !software |
| 7187 | * groups, this is safe because software events never |
| 7188 | * fail to schedule. |
| 7189 | */ |
| 7190 | pmu = group_leader->pmu; |
| 7191 | } else if (is_software_event(group_leader) && |
| 7192 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 7193 | /* |
| 7194 | * In case the group is a pure software group, and we |
| 7195 | * try to add a hardware event, move the whole group to |
| 7196 | * the hardware context. |
| 7197 | */ |
| 7198 | move_group = 1; |
| 7199 | } |
| 7200 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7201 | |
| 7202 | /* |
| 7203 | * Get the target context (task or percpu): |
| 7204 | */ |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7205 | ctx = find_get_context(pmu, task, event->cpu); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7206 | if (IS_ERR(ctx)) { |
| 7207 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7208 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7209 | } |
| 7210 | |
Peter Zijlstra | fd1edb3 | 2011-03-28 13:13:56 +0200 | [diff] [blame] | 7211 | if (task) { |
| 7212 | put_task_struct(task); |
| 7213 | task = NULL; |
| 7214 | } |
| 7215 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7216 | /* |
| 7217 | * Look up the group leader (we will attach this event to it): |
| 7218 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7219 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7220 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7221 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7222 | /* |
| 7223 | * Do not allow a recursive hierarchy (this new sibling |
| 7224 | * becoming part of another group-sibling): |
| 7225 | */ |
| 7226 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7227 | goto err_context; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7228 | /* |
| 7229 | * Do not allow to attach to a group in a different |
| 7230 | * task or CPU context: |
| 7231 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7232 | if (move_group) { |
| 7233 | if (group_leader->ctx->type != ctx->type) |
| 7234 | goto err_context; |
| 7235 | } else { |
| 7236 | if (group_leader->ctx != ctx) |
| 7237 | goto err_context; |
| 7238 | } |
| 7239 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7240 | /* |
| 7241 | * Only a group leader can be exclusive or pinned |
| 7242 | */ |
| 7243 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7244 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7245 | } |
| 7246 | |
| 7247 | if (output_event) { |
| 7248 | err = perf_event_set_output(event, output_event); |
| 7249 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7250 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7251 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7252 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7253 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
| 7254 | f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7255 | if (IS_ERR(event_file)) { |
| 7256 | err = PTR_ERR(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7257 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7258 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7259 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7260 | if (move_group) { |
| 7261 | struct perf_event_context *gctx = group_leader->ctx; |
| 7262 | |
| 7263 | mutex_lock(&gctx->mutex); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7264 | perf_remove_from_context(group_leader, false); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7265 | |
| 7266 | /* |
| 7267 | * Removing from the context ends up with disabled |
| 7268 | * event. What we want here is event in the initial |
| 7269 | * startup state, ready to be add into new context. |
| 7270 | */ |
| 7271 | perf_event__state_init(group_leader); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7272 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7273 | group_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7274 | perf_remove_from_context(sibling, false); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7275 | perf_event__state_init(sibling); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7276 | put_ctx(gctx); |
| 7277 | } |
| 7278 | mutex_unlock(&gctx->mutex); |
| 7279 | put_ctx(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7280 | } |
| 7281 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7282 | WARN_ON_ONCE(ctx->parent_ctx); |
| 7283 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7284 | |
| 7285 | if (move_group) { |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7286 | synchronize_rcu(); |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7287 | perf_install_in_context(ctx, group_leader, event->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7288 | get_ctx(ctx); |
| 7289 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7290 | group_entry) { |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7291 | perf_install_in_context(ctx, sibling, event->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7292 | get_ctx(ctx); |
| 7293 | } |
| 7294 | } |
| 7295 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7296 | perf_install_in_context(ctx, event, event->cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7297 | perf_unpin_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7298 | mutex_unlock(&ctx->mutex); |
| 7299 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7300 | put_online_cpus(); |
| 7301 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7302 | event->owner = current; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7303 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7304 | mutex_lock(¤t->perf_event_mutex); |
| 7305 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 7306 | mutex_unlock(¤t->perf_event_mutex); |
| 7307 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7308 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7309 | * Precalculate sample_data sizes |
| 7310 | */ |
| 7311 | perf_event__header_size(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7312 | perf_event__id_header_size(event); |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7313 | |
| 7314 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7315 | * Drop the reference on the group_event after placing the |
| 7316 | * new event on the sibling_list. This ensures destruction |
| 7317 | * of the group leader will find the pointer to itself in |
| 7318 | * perf_group_detach(). |
| 7319 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7320 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7321 | fd_install(event_fd, event_file); |
| 7322 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7323 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7324 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7325 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7326 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7327 | err_alloc: |
| 7328 | free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7329 | err_cpus: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7330 | put_online_cpus(); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7331 | err_task: |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 7332 | if (task) |
| 7333 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7334 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7335 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7336 | err_fd: |
| 7337 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7338 | return err; |
| 7339 | } |
| 7340 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7341 | /** |
| 7342 | * perf_event_create_kernel_counter |
| 7343 | * |
| 7344 | * @attr: attributes of the counter to create |
| 7345 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7346 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7347 | */ |
| 7348 | struct perf_event * |
| 7349 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7350 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7351 | perf_overflow_handler_t overflow_handler, |
| 7352 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7353 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7354 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7355 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7356 | int err; |
| 7357 | |
| 7358 | /* |
| 7359 | * Get the target context (task or percpu): |
| 7360 | */ |
| 7361 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7362 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
| 7363 | overflow_handler, context); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7364 | if (IS_ERR(event)) { |
| 7365 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7366 | goto err; |
| 7367 | } |
| 7368 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7369 | account_event(event); |
| 7370 | |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7371 | ctx = find_get_context(event->pmu, task, cpu); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7372 | if (IS_ERR(ctx)) { |
| 7373 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7374 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7375 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7376 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7377 | WARN_ON_ONCE(ctx->parent_ctx); |
| 7378 | mutex_lock(&ctx->mutex); |
| 7379 | perf_install_in_context(ctx, event, cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7380 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7381 | mutex_unlock(&ctx->mutex); |
| 7382 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7383 | return event; |
| 7384 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7385 | err_free: |
| 7386 | free_event(event); |
| 7387 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7388 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7389 | } |
| 7390 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 7391 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7392 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 7393 | { |
| 7394 | struct perf_event_context *src_ctx; |
| 7395 | struct perf_event_context *dst_ctx; |
| 7396 | struct perf_event *event, *tmp; |
| 7397 | LIST_HEAD(events); |
| 7398 | |
| 7399 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 7400 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 7401 | |
| 7402 | mutex_lock(&src_ctx->mutex); |
| 7403 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 7404 | event_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7405 | perf_remove_from_context(event, false); |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7406 | unaccount_event_cpu(event, src_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7407 | put_ctx(src_ctx); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7408 | list_add(&event->migrate_entry, &events); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7409 | } |
| 7410 | mutex_unlock(&src_ctx->mutex); |
| 7411 | |
| 7412 | synchronize_rcu(); |
| 7413 | |
| 7414 | mutex_lock(&dst_ctx->mutex); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7415 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 7416 | list_del(&event->migrate_entry); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7417 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 7418 | event->state = PERF_EVENT_STATE_INACTIVE; |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7419 | account_event_cpu(event, dst_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7420 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 7421 | get_ctx(dst_ctx); |
| 7422 | } |
| 7423 | mutex_unlock(&dst_ctx->mutex); |
| 7424 | } |
| 7425 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 7426 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7427 | static void sync_child_event(struct perf_event *child_event, |
| 7428 | struct task_struct *child) |
| 7429 | { |
| 7430 | struct perf_event *parent_event = child_event->parent; |
| 7431 | u64 child_val; |
| 7432 | |
| 7433 | if (child_event->attr.inherit_stat) |
| 7434 | perf_event_read_event(child_event, child); |
| 7435 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 7436 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7437 | |
| 7438 | /* |
| 7439 | * Add back the child's count to the parent's count: |
| 7440 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 7441 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7442 | atomic64_add(child_event->total_time_enabled, |
| 7443 | &parent_event->child_total_time_enabled); |
| 7444 | atomic64_add(child_event->total_time_running, |
| 7445 | &parent_event->child_total_time_running); |
| 7446 | |
| 7447 | /* |
| 7448 | * Remove this event from the parent's list |
| 7449 | */ |
| 7450 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7451 | mutex_lock(&parent_event->child_mutex); |
| 7452 | list_del_init(&child_event->child_list); |
| 7453 | mutex_unlock(&parent_event->child_mutex); |
| 7454 | |
| 7455 | /* |
| 7456 | * Release the parent event, if this was the last |
| 7457 | * reference to it. |
| 7458 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7459 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7460 | } |
| 7461 | |
| 7462 | static void |
| 7463 | __perf_event_exit_task(struct perf_event *child_event, |
| 7464 | struct perf_event_context *child_ctx, |
| 7465 | struct task_struct *child) |
| 7466 | { |
Peter Zijlstra | 1903d50 | 2014-07-15 17:27:27 +0200 | [diff] [blame] | 7467 | /* |
| 7468 | * Do not destroy the 'original' grouping; because of the context |
| 7469 | * switch optimization the original events could've ended up in a |
| 7470 | * random child task. |
| 7471 | * |
| 7472 | * If we were to destroy the original group, all group related |
| 7473 | * operations would cease to function properly after this random |
| 7474 | * child dies. |
| 7475 | * |
| 7476 | * Do destroy all inherited groups, we don't care about those |
| 7477 | * and being thorough is better. |
| 7478 | */ |
| 7479 | perf_remove_from_context(child_event, !!child_event->parent); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7480 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7481 | /* |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7482 | * It can happen that the parent exits first, and has events |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7483 | * that are still around due to the child reference. These |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7484 | * events need to be zapped. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7485 | */ |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7486 | if (child_event->parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7487 | sync_child_event(child_event, child); |
| 7488 | free_event(child_event); |
| 7489 | } |
| 7490 | } |
| 7491 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7492 | 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] | 7493 | { |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 7494 | struct perf_event *child_event, *next; |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 7495 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7496 | unsigned long flags; |
| 7497 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7498 | if (likely(!child->perf_event_ctxp[ctxn])) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7499 | perf_event_task(child, NULL, 0); |
| 7500 | return; |
| 7501 | } |
| 7502 | |
| 7503 | local_irq_save(flags); |
| 7504 | /* |
| 7505 | * We can't reschedule here because interrupts are disabled, |
| 7506 | * and either child is current or it is a task that can't be |
| 7507 | * scheduled, so we are now safe from rescheduling changing |
| 7508 | * our context. |
| 7509 | */ |
Oleg Nesterov | 806839b | 2011-01-21 18:45:47 +0100 | [diff] [blame] | 7510 | child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7511 | |
| 7512 | /* |
| 7513 | * Take the context lock here so that if find_get_context is |
| 7514 | * reading child->perf_event_ctxp, we wait until it has |
| 7515 | * incremented the context's refcount before we do put_ctx below. |
| 7516 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7517 | raw_spin_lock(&child_ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 7518 | task_ctx_sched_out(child_ctx); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7519 | child->perf_event_ctxp[ctxn] = NULL; |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 7520 | |
| 7521 | /* |
| 7522 | * In order to avoid freeing: child_ctx->parent_ctx->task |
| 7523 | * under perf_event_context::lock, grab another reference. |
| 7524 | */ |
| 7525 | parent_ctx = child_ctx->parent_ctx; |
| 7526 | if (parent_ctx) |
| 7527 | get_ctx(parent_ctx); |
| 7528 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7529 | /* |
| 7530 | * If this context is a clone; unclone it so it can't get |
| 7531 | * swapped to another process while we're removing all |
| 7532 | * the events from it. |
| 7533 | */ |
| 7534 | unclone_ctx(child_ctx); |
Peter Zijlstra | 5e942bb | 2009-11-23 11:37:26 +0100 | [diff] [blame] | 7535 | update_context_time(child_ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7536 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7537 | |
| 7538 | /* |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 7539 | * Now that we no longer hold perf_event_context::lock, drop |
| 7540 | * our extra child_ctx->parent_ctx reference. |
| 7541 | */ |
| 7542 | if (parent_ctx) |
| 7543 | put_ctx(parent_ctx); |
| 7544 | |
| 7545 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7546 | * Report the task dead after unscheduling the events so that we |
| 7547 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 7548 | * get a few PERF_RECORD_READ events. |
| 7549 | */ |
| 7550 | perf_event_task(child, child_ctx, 0); |
| 7551 | |
| 7552 | /* |
| 7553 | * We can recurse on the same lock type through: |
| 7554 | * |
| 7555 | * __perf_event_exit_task() |
| 7556 | * sync_child_event() |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7557 | * put_event() |
| 7558 | * mutex_lock(&ctx->mutex) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7559 | * |
| 7560 | * But since its the parent context it won't be the same instance. |
| 7561 | */ |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 7562 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7563 | |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 7564 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7565 | __perf_event_exit_task(child_event, child_ctx, child); |
| 7566 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7567 | mutex_unlock(&child_ctx->mutex); |
| 7568 | |
| 7569 | put_ctx(child_ctx); |
| 7570 | } |
| 7571 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7572 | /* |
| 7573 | * When a child task exits, feed back event values to parent events. |
| 7574 | */ |
| 7575 | void perf_event_exit_task(struct task_struct *child) |
| 7576 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7577 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7578 | int ctxn; |
| 7579 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7580 | mutex_lock(&child->perf_event_mutex); |
| 7581 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 7582 | owner_entry) { |
| 7583 | list_del_init(&event->owner_entry); |
| 7584 | |
| 7585 | /* |
| 7586 | * Ensure the list deletion is visible before we clear |
| 7587 | * the owner, closes a race against perf_release() where |
| 7588 | * we need to serialize on the owner->perf_event_mutex. |
| 7589 | */ |
| 7590 | smp_wmb(); |
| 7591 | event->owner = NULL; |
| 7592 | } |
| 7593 | mutex_unlock(&child->perf_event_mutex); |
| 7594 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7595 | for_each_task_context_nr(ctxn) |
| 7596 | perf_event_exit_task_context(child, ctxn); |
| 7597 | } |
| 7598 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7599 | static void perf_free_event(struct perf_event *event, |
| 7600 | struct perf_event_context *ctx) |
| 7601 | { |
| 7602 | struct perf_event *parent = event->parent; |
| 7603 | |
| 7604 | if (WARN_ON_ONCE(!parent)) |
| 7605 | return; |
| 7606 | |
| 7607 | mutex_lock(&parent->child_mutex); |
| 7608 | list_del_init(&event->child_list); |
| 7609 | mutex_unlock(&parent->child_mutex); |
| 7610 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7611 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7612 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7613 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7614 | list_del_event(event, ctx); |
| 7615 | free_event(event); |
| 7616 | } |
| 7617 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7618 | /* |
| 7619 | * free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7620 | * perf_event_init_task below, used by fork() in case of fail. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7621 | */ |
| 7622 | void perf_event_free_task(struct task_struct *task) |
| 7623 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7624 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7625 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7626 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7627 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7628 | for_each_task_context_nr(ctxn) { |
| 7629 | ctx = task->perf_event_ctxp[ctxn]; |
| 7630 | if (!ctx) |
| 7631 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7632 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7633 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7634 | again: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7635 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 7636 | group_entry) |
| 7637 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7638 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7639 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 7640 | group_entry) |
| 7641 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7642 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7643 | if (!list_empty(&ctx->pinned_groups) || |
| 7644 | !list_empty(&ctx->flexible_groups)) |
| 7645 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7646 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7647 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7648 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7649 | put_ctx(ctx); |
| 7650 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7651 | } |
| 7652 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 7653 | void perf_event_delayed_put(struct task_struct *task) |
| 7654 | { |
| 7655 | int ctxn; |
| 7656 | |
| 7657 | for_each_task_context_nr(ctxn) |
| 7658 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 7659 | } |
| 7660 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7661 | /* |
| 7662 | * inherit a event from parent task to child task: |
| 7663 | */ |
| 7664 | static struct perf_event * |
| 7665 | inherit_event(struct perf_event *parent_event, |
| 7666 | struct task_struct *parent, |
| 7667 | struct perf_event_context *parent_ctx, |
| 7668 | struct task_struct *child, |
| 7669 | struct perf_event *group_leader, |
| 7670 | struct perf_event_context *child_ctx) |
| 7671 | { |
| 7672 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7673 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7674 | |
| 7675 | /* |
| 7676 | * Instead of creating recursive hierarchies of events, |
| 7677 | * we link inherited events back to the original parent, |
| 7678 | * which has a filp for sure, which we use as the reference |
| 7679 | * count: |
| 7680 | */ |
| 7681 | if (parent_event->parent) |
| 7682 | parent_event = parent_event->parent; |
| 7683 | |
| 7684 | child_event = perf_event_alloc(&parent_event->attr, |
| 7685 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7686 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7687 | group_leader, parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7688 | NULL, NULL); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7689 | if (IS_ERR(child_event)) |
| 7690 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7691 | |
| 7692 | if (!atomic_long_inc_not_zero(&parent_event->refcount)) { |
| 7693 | free_event(child_event); |
| 7694 | return NULL; |
| 7695 | } |
| 7696 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7697 | get_ctx(child_ctx); |
| 7698 | |
| 7699 | /* |
| 7700 | * Make the child state follow the state of the parent event, |
| 7701 | * not its attr.disabled bit. We hold the parent's mutex, |
| 7702 | * so we won't race with perf_event_{en, dis}able_family. |
| 7703 | */ |
| 7704 | if (parent_event->state >= PERF_EVENT_STATE_INACTIVE) |
| 7705 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 7706 | else |
| 7707 | child_event->state = PERF_EVENT_STATE_OFF; |
| 7708 | |
| 7709 | if (parent_event->attr.freq) { |
| 7710 | u64 sample_period = parent_event->hw.sample_period; |
| 7711 | struct hw_perf_event *hwc = &child_event->hw; |
| 7712 | |
| 7713 | hwc->sample_period = sample_period; |
| 7714 | hwc->last_period = sample_period; |
| 7715 | |
| 7716 | local64_set(&hwc->period_left, sample_period); |
| 7717 | } |
| 7718 | |
| 7719 | child_event->ctx = child_ctx; |
| 7720 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7721 | child_event->overflow_handler_context |
| 7722 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7723 | |
| 7724 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 7725 | * Precalculate sample_data sizes |
| 7726 | */ |
| 7727 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7728 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 7729 | |
| 7730 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7731 | * Link it up in the child's context: |
| 7732 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7733 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7734 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7735 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7736 | |
| 7737 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7738 | * Link this into the parent event's child list |
| 7739 | */ |
| 7740 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7741 | mutex_lock(&parent_event->child_mutex); |
| 7742 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 7743 | mutex_unlock(&parent_event->child_mutex); |
| 7744 | |
| 7745 | return child_event; |
| 7746 | } |
| 7747 | |
| 7748 | static int inherit_group(struct perf_event *parent_event, |
| 7749 | struct task_struct *parent, |
| 7750 | struct perf_event_context *parent_ctx, |
| 7751 | struct task_struct *child, |
| 7752 | struct perf_event_context *child_ctx) |
| 7753 | { |
| 7754 | struct perf_event *leader; |
| 7755 | struct perf_event *sub; |
| 7756 | struct perf_event *child_ctr; |
| 7757 | |
| 7758 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 7759 | child, NULL, child_ctx); |
| 7760 | if (IS_ERR(leader)) |
| 7761 | return PTR_ERR(leader); |
| 7762 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 7763 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 7764 | child, leader, child_ctx); |
| 7765 | if (IS_ERR(child_ctr)) |
| 7766 | return PTR_ERR(child_ctr); |
| 7767 | } |
| 7768 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7769 | } |
| 7770 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7771 | static int |
| 7772 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 7773 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7774 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7775 | int *inherited_all) |
| 7776 | { |
| 7777 | int ret; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7778 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7779 | |
| 7780 | if (!event->attr.inherit) { |
| 7781 | *inherited_all = 0; |
| 7782 | return 0; |
| 7783 | } |
| 7784 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7785 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7786 | if (!child_ctx) { |
| 7787 | /* |
| 7788 | * This is executed from the parent task context, so |
| 7789 | * inherit events that have been marked for cloning. |
| 7790 | * First allocate and initialize a context for the |
| 7791 | * child. |
| 7792 | */ |
| 7793 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 7794 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7795 | if (!child_ctx) |
| 7796 | return -ENOMEM; |
| 7797 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7798 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7799 | } |
| 7800 | |
| 7801 | ret = inherit_group(event, parent, parent_ctx, |
| 7802 | child, child_ctx); |
| 7803 | |
| 7804 | if (ret) |
| 7805 | *inherited_all = 0; |
| 7806 | |
| 7807 | return ret; |
| 7808 | } |
| 7809 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7810 | /* |
| 7811 | * Initialize the perf_event context in task_struct |
| 7812 | */ |
Jiri Olsa | 985c8dc | 2014-06-24 10:20:24 +0200 | [diff] [blame] | 7813 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7814 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7815 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7816 | struct perf_event_context *cloned_ctx; |
| 7817 | struct perf_event *event; |
| 7818 | struct task_struct *parent = current; |
| 7819 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7820 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7821 | int ret = 0; |
| 7822 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7823 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7824 | return 0; |
| 7825 | |
| 7826 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7827 | * If the parent's context is a clone, pin it so it won't get |
| 7828 | * swapped under us. |
| 7829 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7830 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Peter Zijlstra | ffb4ef2 | 2014-05-05 19:12:20 +0200 | [diff] [blame] | 7831 | if (!parent_ctx) |
| 7832 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7833 | |
| 7834 | /* |
| 7835 | * No need to check if parent_ctx != NULL here; since we saw |
| 7836 | * it non-NULL earlier, the only reason for it to become NULL |
| 7837 | * is if we exit, and since we're currently in the middle of |
| 7838 | * a fork we can't be exiting at the same time. |
| 7839 | */ |
| 7840 | |
| 7841 | /* |
| 7842 | * Lock the parent list. No need to lock the child - not PID |
| 7843 | * hashed yet and not running, so nobody can access it. |
| 7844 | */ |
| 7845 | mutex_lock(&parent_ctx->mutex); |
| 7846 | |
| 7847 | /* |
| 7848 | * We dont have to disable NMIs - we are only looking at |
| 7849 | * the list, not manipulating it: |
| 7850 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7851 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7852 | ret = inherit_task_group(event, parent, parent_ctx, |
| 7853 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7854 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7855 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7856 | } |
| 7857 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7858 | /* |
| 7859 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 7860 | * to allocations, but we need to prevent rotation because |
| 7861 | * rotate_ctx() will change the list from interrupt context. |
| 7862 | */ |
| 7863 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 7864 | parent_ctx->rotate_disable = 1; |
| 7865 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 7866 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7867 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7868 | ret = inherit_task_group(event, parent, parent_ctx, |
| 7869 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7870 | if (ret) |
| 7871 | break; |
| 7872 | } |
| 7873 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7874 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 7875 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7876 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7877 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7878 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 7879 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7880 | /* |
| 7881 | * Mark the child context as a clone of the parent |
| 7882 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7883 | * |
| 7884 | * Note that if the parent is a clone, the holding of |
| 7885 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7886 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7887 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7888 | if (cloned_ctx) { |
| 7889 | child_ctx->parent_ctx = cloned_ctx; |
| 7890 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 7891 | } else { |
| 7892 | child_ctx->parent_ctx = parent_ctx; |
| 7893 | child_ctx->parent_gen = parent_ctx->generation; |
| 7894 | } |
| 7895 | get_ctx(child_ctx->parent_ctx); |
| 7896 | } |
| 7897 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7898 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7899 | mutex_unlock(&parent_ctx->mutex); |
| 7900 | |
| 7901 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7902 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7903 | |
| 7904 | return ret; |
| 7905 | } |
| 7906 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7907 | /* |
| 7908 | * Initialize the perf_event context in task_struct |
| 7909 | */ |
| 7910 | int perf_event_init_task(struct task_struct *child) |
| 7911 | { |
| 7912 | int ctxn, ret; |
| 7913 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 7914 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 7915 | mutex_init(&child->perf_event_mutex); |
| 7916 | INIT_LIST_HEAD(&child->perf_event_list); |
| 7917 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7918 | for_each_task_context_nr(ctxn) { |
| 7919 | ret = perf_event_init_context(child, ctxn); |
| 7920 | if (ret) |
| 7921 | return ret; |
| 7922 | } |
| 7923 | |
| 7924 | return 0; |
| 7925 | } |
| 7926 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7927 | static void __init perf_event_init_all_cpus(void) |
| 7928 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7929 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7930 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7931 | |
| 7932 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7933 | swhash = &per_cpu(swevent_htable, cpu); |
| 7934 | mutex_init(&swhash->hlist_mutex); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7935 | INIT_LIST_HEAD(&per_cpu(rotation_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7936 | } |
| 7937 | } |
| 7938 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 7939 | static void perf_event_init_cpu(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7940 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7941 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7942 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7943 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 7944 | swhash->online = true; |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 7945 | if (swhash->hlist_refcount > 0) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7946 | struct swevent_hlist *hlist; |
| 7947 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7948 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 7949 | WARN_ON(!hlist); |
| 7950 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7951 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7952 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7953 | } |
| 7954 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 7955 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7956 | static void perf_pmu_rotate_stop(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7957 | { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7958 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 7959 | |
| 7960 | WARN_ON(!irqs_disabled()); |
| 7961 | |
| 7962 | list_del_init(&cpuctx->rotation_list); |
| 7963 | } |
| 7964 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7965 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7966 | { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7967 | struct remove_event re = { .detach_group = false }; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7968 | struct perf_event_context *ctx = __info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7969 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7970 | perf_pmu_rotate_stop(ctx->pmu); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 7971 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 7972 | rcu_read_lock(); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7973 | list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry) |
| 7974 | __perf_remove_from_context(&re); |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 7975 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7976 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7977 | |
| 7978 | static void perf_event_exit_cpu_context(int cpu) |
| 7979 | { |
| 7980 | struct perf_event_context *ctx; |
| 7981 | struct pmu *pmu; |
| 7982 | int idx; |
| 7983 | |
| 7984 | idx = srcu_read_lock(&pmus_srcu); |
| 7985 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 7986 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7987 | |
| 7988 | mutex_lock(&ctx->mutex); |
| 7989 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 7990 | mutex_unlock(&ctx->mutex); |
| 7991 | } |
| 7992 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7993 | } |
| 7994 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7995 | static void perf_event_exit_cpu(int cpu) |
| 7996 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7997 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7998 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 7999 | perf_event_exit_cpu_context(cpu); |
| 8000 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8001 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 8002 | swhash->online = false; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8003 | swevent_hlist_release(swhash); |
| 8004 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8005 | } |
| 8006 | #else |
| 8007 | static inline void perf_event_exit_cpu(int cpu) { } |
| 8008 | #endif |
| 8009 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8010 | static int |
| 8011 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 8012 | { |
| 8013 | int cpu; |
| 8014 | |
| 8015 | for_each_online_cpu(cpu) |
| 8016 | perf_event_exit_cpu(cpu); |
| 8017 | |
| 8018 | return NOTIFY_OK; |
| 8019 | } |
| 8020 | |
| 8021 | /* |
| 8022 | * Run the perf reboot notifier at the very last possible moment so that |
| 8023 | * the generic watchdog code runs as long as possible. |
| 8024 | */ |
| 8025 | static struct notifier_block perf_reboot_notifier = { |
| 8026 | .notifier_call = perf_reboot, |
| 8027 | .priority = INT_MIN, |
| 8028 | }; |
| 8029 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 8030 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8031 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 8032 | { |
| 8033 | unsigned int cpu = (long)hcpu; |
| 8034 | |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 8035 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8036 | |
| 8037 | case CPU_UP_PREPARE: |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8038 | case CPU_DOWN_FAILED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8039 | perf_event_init_cpu(cpu); |
| 8040 | break; |
| 8041 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8042 | case CPU_UP_CANCELED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8043 | case CPU_DOWN_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8044 | perf_event_exit_cpu(cpu); |
| 8045 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8046 | default: |
| 8047 | break; |
| 8048 | } |
| 8049 | |
| 8050 | return NOTIFY_OK; |
| 8051 | } |
| 8052 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8053 | void __init perf_event_init(void) |
| 8054 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8055 | int ret; |
| 8056 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8057 | idr_init(&pmu_idr); |
| 8058 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8059 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8060 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8061 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 8062 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 8063 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8064 | perf_tp_register(); |
| 8065 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8066 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8067 | |
| 8068 | ret = init_hw_breakpoint(); |
| 8069 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 8070 | |
| 8071 | /* do not patch jump label more than once per second */ |
| 8072 | jump_label_rate_limit(&perf_sched_events, HZ); |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 8073 | |
| 8074 | /* |
| 8075 | * Build time assertion that we keep the data_head at the intended |
| 8076 | * location. IOW, validation we got the __reserved[] size right. |
| 8077 | */ |
| 8078 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 8079 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8080 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8081 | |
| 8082 | static int __init perf_event_sysfs_init(void) |
| 8083 | { |
| 8084 | struct pmu *pmu; |
| 8085 | int ret; |
| 8086 | |
| 8087 | mutex_lock(&pmus_lock); |
| 8088 | |
| 8089 | ret = bus_register(&pmu_bus); |
| 8090 | if (ret) |
| 8091 | goto unlock; |
| 8092 | |
| 8093 | list_for_each_entry(pmu, &pmus, entry) { |
| 8094 | if (!pmu->name || pmu->type < 0) |
| 8095 | continue; |
| 8096 | |
| 8097 | ret = pmu_dev_alloc(pmu); |
| 8098 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 8099 | } |
| 8100 | pmu_bus_running = 1; |
| 8101 | ret = 0; |
| 8102 | |
| 8103 | unlock: |
| 8104 | mutex_unlock(&pmus_lock); |
| 8105 | |
| 8106 | return ret; |
| 8107 | } |
| 8108 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8109 | |
| 8110 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8111 | static struct cgroup_subsys_state * |
| 8112 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8113 | { |
| 8114 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8115 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 8116 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8117 | if (!jc) |
| 8118 | return ERR_PTR(-ENOMEM); |
| 8119 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8120 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 8121 | if (!jc->info) { |
| 8122 | kfree(jc); |
| 8123 | return ERR_PTR(-ENOMEM); |
| 8124 | } |
| 8125 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8126 | return &jc->css; |
| 8127 | } |
| 8128 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8129 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8130 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8131 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
| 8132 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8133 | free_percpu(jc->info); |
| 8134 | kfree(jc); |
| 8135 | } |
| 8136 | |
| 8137 | static int __perf_cgroup_move(void *info) |
| 8138 | { |
| 8139 | struct task_struct *task = info; |
| 8140 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
| 8141 | return 0; |
| 8142 | } |
| 8143 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8144 | static void perf_cgroup_attach(struct cgroup_subsys_state *css, |
| 8145 | struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8146 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8147 | struct task_struct *task; |
| 8148 | |
Tejun Heo | 924f0d9a | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 8149 | cgroup_taskset_for_each(task, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8150 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8151 | } |
| 8152 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8153 | static void perf_cgroup_exit(struct cgroup_subsys_state *css, |
| 8154 | struct cgroup_subsys_state *old_css, |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 8155 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8156 | { |
| 8157 | /* |
| 8158 | * cgroup_exit() is called in the copy_process() failure path. |
| 8159 | * Ignore this case since the task hasn't ran yet, this avoids |
| 8160 | * trying to poke a half freed task state from generic code. |
| 8161 | */ |
| 8162 | if (!(task->flags & PF_EXITING)) |
| 8163 | return; |
| 8164 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8165 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8166 | } |
| 8167 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 8168 | struct cgroup_subsys perf_event_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 8169 | .css_alloc = perf_cgroup_css_alloc, |
| 8170 | .css_free = perf_cgroup_css_free, |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 8171 | .exit = perf_cgroup_exit, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8172 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8173 | }; |
| 8174 | #endif /* CONFIG_CGROUP_PERF */ |