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 | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 612 | css = css_tryget_from_dir(f.file->f_dentry, &perf_event_cgrp_subsys); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 613 | if (IS_ERR(css)) { |
| 614 | ret = PTR_ERR(css); |
| 615 | goto out; |
| 616 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 617 | |
| 618 | cgrp = container_of(css, struct perf_cgroup, css); |
| 619 | event->cgrp = cgrp; |
| 620 | |
| 621 | /* |
| 622 | * all events in a group must monitor |
| 623 | * the same cgroup because a task belongs |
| 624 | * to only one perf cgroup at a time |
| 625 | */ |
| 626 | if (group_leader && group_leader->cgrp != cgrp) { |
| 627 | perf_detach_cgroup(event); |
| 628 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 629 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 630 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 631 | fdput(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 632 | return ret; |
| 633 | } |
| 634 | |
| 635 | static inline void |
| 636 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 637 | { |
| 638 | struct perf_cgroup_info *t; |
| 639 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 640 | event->shadow_ctx_time = now - t->timestamp; |
| 641 | } |
| 642 | |
| 643 | static inline void |
| 644 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 645 | { |
| 646 | /* |
| 647 | * when the current task's perf cgroup does not match |
| 648 | * the event's, we need to remember to call the |
| 649 | * perf_mark_enable() function the first time a task with |
| 650 | * a matching perf cgroup is scheduled in. |
| 651 | */ |
| 652 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 653 | event->cgrp_defer_enabled = 1; |
| 654 | } |
| 655 | |
| 656 | static inline void |
| 657 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 658 | struct perf_event_context *ctx) |
| 659 | { |
| 660 | struct perf_event *sub; |
| 661 | u64 tstamp = perf_event_time(event); |
| 662 | |
| 663 | if (!event->cgrp_defer_enabled) |
| 664 | return; |
| 665 | |
| 666 | event->cgrp_defer_enabled = 0; |
| 667 | |
| 668 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 669 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 670 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 671 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 672 | sub->cgrp_defer_enabled = 0; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | #else /* !CONFIG_CGROUP_PERF */ |
| 677 | |
| 678 | static inline bool |
| 679 | perf_cgroup_match(struct perf_event *event) |
| 680 | { |
| 681 | return true; |
| 682 | } |
| 683 | |
| 684 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 685 | {} |
| 686 | |
| 687 | static inline int is_cgroup_event(struct perf_event *event) |
| 688 | { |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 693 | { |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 698 | { |
| 699 | } |
| 700 | |
| 701 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 702 | { |
| 703 | } |
| 704 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 705 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 706 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 707 | { |
| 708 | } |
| 709 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 710 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 711 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 712 | { |
| 713 | } |
| 714 | |
| 715 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 716 | struct perf_event_attr *attr, |
| 717 | struct perf_event *group_leader) |
| 718 | { |
| 719 | return -EINVAL; |
| 720 | } |
| 721 | |
| 722 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 723 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 724 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 725 | { |
| 726 | } |
| 727 | |
| 728 | void |
| 729 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 730 | { |
| 731 | } |
| 732 | |
| 733 | static inline void |
| 734 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 735 | { |
| 736 | } |
| 737 | |
| 738 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 739 | { |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static inline void |
| 744 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 745 | { |
| 746 | } |
| 747 | |
| 748 | static inline void |
| 749 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 750 | struct perf_event_context *ctx) |
| 751 | { |
| 752 | } |
| 753 | #endif |
| 754 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 755 | /* |
| 756 | * set default to be dependent on timer tick just |
| 757 | * like original code |
| 758 | */ |
| 759 | #define PERF_CPU_HRTIMER (1000 / HZ) |
| 760 | /* |
| 761 | * function must be called with interrupts disbled |
| 762 | */ |
| 763 | static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr) |
| 764 | { |
| 765 | struct perf_cpu_context *cpuctx; |
| 766 | enum hrtimer_restart ret = HRTIMER_NORESTART; |
| 767 | int rotations = 0; |
| 768 | |
| 769 | WARN_ON(!irqs_disabled()); |
| 770 | |
| 771 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); |
| 772 | |
| 773 | rotations = perf_rotate_context(cpuctx); |
| 774 | |
| 775 | /* |
| 776 | * arm timer if needed |
| 777 | */ |
| 778 | if (rotations) { |
| 779 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
| 780 | ret = HRTIMER_RESTART; |
| 781 | } |
| 782 | |
| 783 | return ret; |
| 784 | } |
| 785 | |
| 786 | /* CPU is going down */ |
| 787 | void perf_cpu_hrtimer_cancel(int cpu) |
| 788 | { |
| 789 | struct perf_cpu_context *cpuctx; |
| 790 | struct pmu *pmu; |
| 791 | unsigned long flags; |
| 792 | |
| 793 | if (WARN_ON(cpu != smp_processor_id())) |
| 794 | return; |
| 795 | |
| 796 | local_irq_save(flags); |
| 797 | |
| 798 | rcu_read_lock(); |
| 799 | |
| 800 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 801 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 802 | |
| 803 | if (pmu->task_ctx_nr == perf_sw_context) |
| 804 | continue; |
| 805 | |
| 806 | hrtimer_cancel(&cpuctx->hrtimer); |
| 807 | } |
| 808 | |
| 809 | rcu_read_unlock(); |
| 810 | |
| 811 | local_irq_restore(flags); |
| 812 | } |
| 813 | |
| 814 | static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
| 815 | { |
| 816 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 817 | struct pmu *pmu = cpuctx->ctx.pmu; |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 818 | int timer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 819 | |
| 820 | /* no multiplexing needed for SW PMU */ |
| 821 | if (pmu->task_ctx_nr == perf_sw_context) |
| 822 | return; |
| 823 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 824 | /* |
| 825 | * check default is sane, if not set then force to |
| 826 | * default interval (1/tick) |
| 827 | */ |
| 828 | timer = pmu->hrtimer_interval_ms; |
| 829 | if (timer < 1) |
| 830 | timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; |
| 831 | |
| 832 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 833 | |
| 834 | hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
| 835 | hr->function = perf_cpu_hrtimer_handler; |
| 836 | } |
| 837 | |
| 838 | static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx) |
| 839 | { |
| 840 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 841 | struct pmu *pmu = cpuctx->ctx.pmu; |
| 842 | |
| 843 | /* not for SW PMU */ |
| 844 | if (pmu->task_ctx_nr == perf_sw_context) |
| 845 | return; |
| 846 | |
| 847 | if (hrtimer_active(hr)) |
| 848 | return; |
| 849 | |
| 850 | if (!hrtimer_callback_running(hr)) |
| 851 | __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval, |
| 852 | 0, HRTIMER_MODE_REL_PINNED, 0); |
| 853 | } |
| 854 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 855 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 856 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 857 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 858 | if (!(*count)++) |
| 859 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 860 | } |
| 861 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 862 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 863 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 864 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 865 | if (!--(*count)) |
| 866 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 867 | } |
| 868 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 869 | static DEFINE_PER_CPU(struct list_head, rotation_list); |
| 870 | |
| 871 | /* |
| 872 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 873 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 874 | * disabled, while rotate_context is called from IRQ context. |
| 875 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 876 | static void perf_pmu_rotate_start(struct pmu *pmu) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 877 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 878 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 879 | struct list_head *head = &__get_cpu_var(rotation_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 880 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 881 | WARN_ON(!irqs_disabled()); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 882 | |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 883 | if (list_empty(&cpuctx->rotation_list)) |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 884 | list_add(&cpuctx->rotation_list, head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static void get_ctx(struct perf_event_context *ctx) |
| 888 | { |
| 889 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 890 | } |
| 891 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 892 | static void put_ctx(struct perf_event_context *ctx) |
| 893 | { |
| 894 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 895 | if (ctx->parent_ctx) |
| 896 | put_ctx(ctx->parent_ctx); |
| 897 | if (ctx->task) |
| 898 | put_task_struct(ctx->task); |
Lai Jiangshan | cb796ff | 2011-03-18 12:07:41 +0800 | [diff] [blame] | 899 | kfree_rcu(ctx, rcu_head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | static void unclone_ctx(struct perf_event_context *ctx) |
| 904 | { |
| 905 | if (ctx->parent_ctx) { |
| 906 | put_ctx(ctx->parent_ctx); |
| 907 | ctx->parent_ctx = NULL; |
| 908 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 909 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 912 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 913 | { |
| 914 | /* |
| 915 | * only top level events have the pid namespace they were created in |
| 916 | */ |
| 917 | if (event->parent) |
| 918 | event = event->parent; |
| 919 | |
| 920 | return task_tgid_nr_ns(p, event->ns); |
| 921 | } |
| 922 | |
| 923 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 924 | { |
| 925 | /* |
| 926 | * only top level events have the pid namespace they were created in |
| 927 | */ |
| 928 | if (event->parent) |
| 929 | event = event->parent; |
| 930 | |
| 931 | return task_pid_nr_ns(p, event->ns); |
| 932 | } |
| 933 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 934 | /* |
| 935 | * If we inherit events we want to return the parent event id |
| 936 | * to userspace. |
| 937 | */ |
| 938 | static u64 primary_event_id(struct perf_event *event) |
| 939 | { |
| 940 | u64 id = event->id; |
| 941 | |
| 942 | if (event->parent) |
| 943 | id = event->parent->id; |
| 944 | |
| 945 | return id; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * Get the perf_event_context for a task and lock it. |
| 950 | * This has to cope with with the fact that until it is locked, |
| 951 | * the context could get moved to another task. |
| 952 | */ |
| 953 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 954 | 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] | 955 | { |
| 956 | struct perf_event_context *ctx; |
| 957 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 958 | retry: |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 959 | /* |
| 960 | * One of the few rules of preemptible RCU is that one cannot do |
| 961 | * rcu_read_unlock() while holding a scheduler (or nested) lock when |
| 962 | * part of the read side critical section was preemptible -- see |
| 963 | * rcu_read_unlock_special(). |
| 964 | * |
| 965 | * Since ctx->lock nests under rq->lock we must ensure the entire read |
| 966 | * side critical section is non-preemptible. |
| 967 | */ |
| 968 | preempt_disable(); |
| 969 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 970 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 971 | if (ctx) { |
| 972 | /* |
| 973 | * If this context is a clone of another, it might |
| 974 | * get swapped for another underneath us by |
| 975 | * perf_event_task_sched_out, though the |
| 976 | * rcu_read_lock() protects us from any context |
| 977 | * getting freed. Lock the context and check if it |
| 978 | * got swapped before we could get the lock, and retry |
| 979 | * if so. If we locked the right context, then it |
| 980 | * can't get swapped on us any more. |
| 981 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 982 | raw_spin_lock_irqsave(&ctx->lock, *flags); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 983 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 984 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 985 | rcu_read_unlock(); |
| 986 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 987 | goto retry; |
| 988 | } |
| 989 | |
| 990 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 991 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 992 | ctx = NULL; |
| 993 | } |
| 994 | } |
| 995 | rcu_read_unlock(); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 996 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 997 | return ctx; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * Get the context for a task and increment its pin_count so it |
| 1002 | * can't get swapped to another task. This also increments its |
| 1003 | * reference count so that the context can't get freed. |
| 1004 | */ |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1005 | static struct perf_event_context * |
| 1006 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1007 | { |
| 1008 | struct perf_event_context *ctx; |
| 1009 | unsigned long flags; |
| 1010 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1011 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1012 | if (ctx) { |
| 1013 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1014 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1015 | } |
| 1016 | return ctx; |
| 1017 | } |
| 1018 | |
| 1019 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 1020 | { |
| 1021 | unsigned long flags; |
| 1022 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1023 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1024 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1025 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1026 | } |
| 1027 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1028 | /* |
| 1029 | * Update the record of the current time in a context. |
| 1030 | */ |
| 1031 | static void update_context_time(struct perf_event_context *ctx) |
| 1032 | { |
| 1033 | u64 now = perf_clock(); |
| 1034 | |
| 1035 | ctx->time += now - ctx->timestamp; |
| 1036 | ctx->timestamp = now; |
| 1037 | } |
| 1038 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1039 | static u64 perf_event_time(struct perf_event *event) |
| 1040 | { |
| 1041 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1042 | |
| 1043 | if (is_cgroup_event(event)) |
| 1044 | return perf_cgroup_event_time(event); |
| 1045 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1046 | return ctx ? ctx->time : 0; |
| 1047 | } |
| 1048 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1049 | /* |
| 1050 | * 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] | 1051 | * The caller of this function needs to hold the ctx->lock. |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1052 | */ |
| 1053 | static void update_event_times(struct perf_event *event) |
| 1054 | { |
| 1055 | struct perf_event_context *ctx = event->ctx; |
| 1056 | u64 run_end; |
| 1057 | |
| 1058 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1059 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1060 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1061 | /* |
| 1062 | * in cgroup mode, time_enabled represents |
| 1063 | * the time the event was enabled AND active |
| 1064 | * tasks were in the monitored cgroup. This is |
| 1065 | * independent of the activity of the context as |
| 1066 | * there may be a mix of cgroup and non-cgroup events. |
| 1067 | * |
| 1068 | * That is why we treat cgroup events differently |
| 1069 | * here. |
| 1070 | */ |
| 1071 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1072 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1073 | else if (ctx->is_active) |
| 1074 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1075 | else |
| 1076 | run_end = event->tstamp_stopped; |
| 1077 | |
| 1078 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1079 | |
| 1080 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1081 | run_end = event->tstamp_stopped; |
| 1082 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1083 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1084 | |
| 1085 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1086 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1089 | /* |
| 1090 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1091 | */ |
| 1092 | static void update_group_times(struct perf_event *leader) |
| 1093 | { |
| 1094 | struct perf_event *event; |
| 1095 | |
| 1096 | update_event_times(leader); |
| 1097 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1098 | update_event_times(event); |
| 1099 | } |
| 1100 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1101 | static struct list_head * |
| 1102 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1103 | { |
| 1104 | if (event->attr.pinned) |
| 1105 | return &ctx->pinned_groups; |
| 1106 | else |
| 1107 | return &ctx->flexible_groups; |
| 1108 | } |
| 1109 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1110 | /* |
| 1111 | * Add a event from the lists for its context. |
| 1112 | * Must be called with ctx->mutex and ctx->lock held. |
| 1113 | */ |
| 1114 | static void |
| 1115 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1116 | { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1117 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1118 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1119 | |
| 1120 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1121 | * If we're a stand alone event or group leader, we go to the context |
| 1122 | * list, group events are kept attached to the group so that |
| 1123 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1124 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1125 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1126 | struct list_head *list; |
| 1127 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1128 | if (is_software_event(event)) |
| 1129 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 1130 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1131 | list = ctx_group_list(event, ctx); |
| 1132 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 1135 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1136 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1137 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1138 | if (has_branch_stack(event)) |
| 1139 | ctx->nr_branch_stack++; |
| 1140 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1141 | list_add_rcu(&event->event_entry, &ctx->event_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1142 | if (!ctx->nr_events) |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1143 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1144 | ctx->nr_events++; |
| 1145 | if (event->attr.inherit_stat) |
| 1146 | ctx->nr_stat++; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1147 | |
| 1148 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1151 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1152 | * Initialize event state based on the perf_event_attr::disabled. |
| 1153 | */ |
| 1154 | static inline void perf_event__state_init(struct perf_event *event) |
| 1155 | { |
| 1156 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1157 | PERF_EVENT_STATE_INACTIVE; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1161 | * Called at perf_event creation and when events are attached/detached from a |
| 1162 | * group. |
| 1163 | */ |
| 1164 | static void perf_event__read_size(struct perf_event *event) |
| 1165 | { |
| 1166 | int entry = sizeof(u64); /* value */ |
| 1167 | int size = 0; |
| 1168 | int nr = 1; |
| 1169 | |
| 1170 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1171 | size += sizeof(u64); |
| 1172 | |
| 1173 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1174 | size += sizeof(u64); |
| 1175 | |
| 1176 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1177 | entry += sizeof(u64); |
| 1178 | |
| 1179 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
| 1180 | nr += event->group_leader->nr_siblings; |
| 1181 | size += sizeof(u64); |
| 1182 | } |
| 1183 | |
| 1184 | size += entry * nr; |
| 1185 | event->read_size = size; |
| 1186 | } |
| 1187 | |
| 1188 | static void perf_event__header_size(struct perf_event *event) |
| 1189 | { |
| 1190 | struct perf_sample_data *data; |
| 1191 | u64 sample_type = event->attr.sample_type; |
| 1192 | u16 size = 0; |
| 1193 | |
| 1194 | perf_event__read_size(event); |
| 1195 | |
| 1196 | if (sample_type & PERF_SAMPLE_IP) |
| 1197 | size += sizeof(data->ip); |
| 1198 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1199 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1200 | size += sizeof(data->addr); |
| 1201 | |
| 1202 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1203 | size += sizeof(data->period); |
| 1204 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1205 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1206 | size += sizeof(data->weight); |
| 1207 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1208 | if (sample_type & PERF_SAMPLE_READ) |
| 1209 | size += event->read_size; |
| 1210 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1211 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1212 | size += sizeof(data->data_src.val); |
| 1213 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 1214 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1215 | size += sizeof(data->txn); |
| 1216 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1217 | event->header_size = size; |
| 1218 | } |
| 1219 | |
| 1220 | static void perf_event__id_header_size(struct perf_event *event) |
| 1221 | { |
| 1222 | struct perf_sample_data *data; |
| 1223 | u64 sample_type = event->attr.sample_type; |
| 1224 | u16 size = 0; |
| 1225 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1226 | if (sample_type & PERF_SAMPLE_TID) |
| 1227 | size += sizeof(data->tid_entry); |
| 1228 | |
| 1229 | if (sample_type & PERF_SAMPLE_TIME) |
| 1230 | size += sizeof(data->time); |
| 1231 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 1232 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1233 | size += sizeof(data->id); |
| 1234 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1235 | if (sample_type & PERF_SAMPLE_ID) |
| 1236 | size += sizeof(data->id); |
| 1237 | |
| 1238 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1239 | size += sizeof(data->stream_id); |
| 1240 | |
| 1241 | if (sample_type & PERF_SAMPLE_CPU) |
| 1242 | size += sizeof(data->cpu_entry); |
| 1243 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1244 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1245 | } |
| 1246 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1247 | static void perf_group_attach(struct perf_event *event) |
| 1248 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1249 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1250 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1251 | /* |
| 1252 | * We can have double attach due to group movement in perf_event_open. |
| 1253 | */ |
| 1254 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1255 | return; |
| 1256 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1257 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1258 | |
| 1259 | if (group_leader == event) |
| 1260 | return; |
| 1261 | |
| 1262 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 1263 | !is_software_event(event)) |
| 1264 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 1265 | |
| 1266 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1267 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1268 | |
| 1269 | perf_event__header_size(group_leader); |
| 1270 | |
| 1271 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1272 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1275 | /* |
| 1276 | * Remove a event from the lists for its context. |
| 1277 | * Must be called with ctx->mutex and ctx->lock held. |
| 1278 | */ |
| 1279 | static void |
| 1280 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1281 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1282 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1283 | /* |
| 1284 | * We can have double detach due to exit/hot-unplug + close. |
| 1285 | */ |
| 1286 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1287 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1288 | |
| 1289 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1290 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1291 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1292 | ctx->nr_cgroups--; |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1293 | cpuctx = __get_cpu_context(ctx); |
| 1294 | /* |
| 1295 | * if there are no more cgroup events |
| 1296 | * then cler cgrp to avoid stale pointer |
| 1297 | * in update_cgrp_time_from_cpuctx() |
| 1298 | */ |
| 1299 | if (!ctx->nr_cgroups) |
| 1300 | cpuctx->cgrp = NULL; |
| 1301 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1302 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1303 | if (has_branch_stack(event)) |
| 1304 | ctx->nr_branch_stack--; |
| 1305 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1306 | ctx->nr_events--; |
| 1307 | if (event->attr.inherit_stat) |
| 1308 | ctx->nr_stat--; |
| 1309 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1310 | list_del_rcu(&event->event_entry); |
| 1311 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1312 | if (event->group_leader == event) |
| 1313 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1314 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1315 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1316 | |
| 1317 | /* |
| 1318 | * If event was in error state, then keep it |
| 1319 | * that way, otherwise bogus counts will be |
| 1320 | * returned on read(). The only way to get out |
| 1321 | * of error state is by explicit re-enabling |
| 1322 | * of the event |
| 1323 | */ |
| 1324 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1325 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1326 | |
| 1327 | ctx->generation++; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1328 | } |
| 1329 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1330 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1331 | { |
| 1332 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1333 | struct list_head *list = NULL; |
| 1334 | |
| 1335 | /* |
| 1336 | * We can have double detach due to exit/hot-unplug + close. |
| 1337 | */ |
| 1338 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1339 | return; |
| 1340 | |
| 1341 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1342 | |
| 1343 | /* |
| 1344 | * If this is a sibling, remove it from its group. |
| 1345 | */ |
| 1346 | if (event->group_leader != event) { |
| 1347 | list_del_init(&event->group_entry); |
| 1348 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1349 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | if (!list_empty(&event->group_entry)) |
| 1353 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1354 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1355 | /* |
| 1356 | * If this was a group event with sibling events then |
| 1357 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1358 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1359 | */ |
| 1360 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1361 | if (list) |
| 1362 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1363 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1364 | |
| 1365 | /* Inherit group flags from the previous leader */ |
| 1366 | sibling->group_flags = event->group_flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1367 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1368 | |
| 1369 | out: |
| 1370 | perf_event__header_size(event->group_leader); |
| 1371 | |
| 1372 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1373 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1374 | } |
| 1375 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1376 | static inline int |
| 1377 | event_filter_match(struct perf_event *event) |
| 1378 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1379 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
| 1380 | && perf_cgroup_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1381 | } |
| 1382 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1383 | static void |
| 1384 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1385 | struct perf_cpu_context *cpuctx, |
| 1386 | struct perf_event_context *ctx) |
| 1387 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1388 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1389 | u64 delta; |
| 1390 | /* |
| 1391 | * An event which could not be activated because of |
| 1392 | * filter mismatch still needs to have its timings |
| 1393 | * maintained, otherwise bogus information is return |
| 1394 | * via read() for time_enabled, time_running: |
| 1395 | */ |
| 1396 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1397 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1398 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1399 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1400 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1401 | } |
| 1402 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1403 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1404 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1405 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1406 | perf_pmu_disable(event->pmu); |
| 1407 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1408 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1409 | if (event->pending_disable) { |
| 1410 | event->pending_disable = 0; |
| 1411 | event->state = PERF_EVENT_STATE_OFF; |
| 1412 | } |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1413 | event->tstamp_stopped = tstamp; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1414 | event->pmu->del(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1415 | event->oncpu = -1; |
| 1416 | |
| 1417 | if (!is_software_event(event)) |
| 1418 | cpuctx->active_oncpu--; |
| 1419 | ctx->nr_active--; |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1420 | if (event->attr.freq && event->attr.sample_freq) |
| 1421 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1422 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1423 | cpuctx->exclusive = 0; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1424 | |
| 1425 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | static void |
| 1429 | group_sched_out(struct perf_event *group_event, |
| 1430 | struct perf_cpu_context *cpuctx, |
| 1431 | struct perf_event_context *ctx) |
| 1432 | { |
| 1433 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1434 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1435 | |
| 1436 | event_sched_out(group_event, cpuctx, ctx); |
| 1437 | |
| 1438 | /* |
| 1439 | * Schedule out siblings (if any): |
| 1440 | */ |
| 1441 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1442 | event_sched_out(event, cpuctx, ctx); |
| 1443 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1444 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1445 | cpuctx->exclusive = 0; |
| 1446 | } |
| 1447 | |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1448 | struct remove_event { |
| 1449 | struct perf_event *event; |
| 1450 | bool detach_group; |
| 1451 | }; |
| 1452 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1453 | /* |
| 1454 | * Cross CPU call to remove a performance event |
| 1455 | * |
| 1456 | * We disable the event on the hardware level first. After that we |
| 1457 | * remove it from the context list. |
| 1458 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1459 | static int __perf_remove_from_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1460 | { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1461 | struct remove_event *re = info; |
| 1462 | struct perf_event *event = re->event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1463 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1464 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1465 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1466 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1467 | event_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1468 | if (re->detach_group) |
| 1469 | perf_group_detach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1470 | list_del_event(event, ctx); |
Peter Zijlstra | 64ce312 | 2011-04-09 21:17:48 +0200 | [diff] [blame] | 1471 | if (!ctx->nr_events && cpuctx->task_ctx == ctx) { |
| 1472 | ctx->is_active = 0; |
| 1473 | cpuctx->task_ctx = NULL; |
| 1474 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1475 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1476 | |
| 1477 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | /* |
| 1482 | * Remove the event from a task's (or a CPU's) list of events. |
| 1483 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1484 | * CPU events are removed with a smp call. For task events we only |
| 1485 | * call when the task is on a CPU. |
| 1486 | * |
| 1487 | * If event->ctx is a cloned context, callers must make sure that |
| 1488 | * every task struct that event->ctx->task could possibly point to |
| 1489 | * remains valid. This is OK when called from perf_release since |
| 1490 | * that only calls us on the top-level context, which can't be a clone. |
| 1491 | * When called from perf_event_exit_task, it's OK because the |
| 1492 | * context has been detached from its task. |
| 1493 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1494 | 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] | 1495 | { |
| 1496 | struct perf_event_context *ctx = event->ctx; |
| 1497 | struct task_struct *task = ctx->task; |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1498 | struct remove_event re = { |
| 1499 | .event = event, |
| 1500 | .detach_group = detach_group, |
| 1501 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1502 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1503 | lockdep_assert_held(&ctx->mutex); |
| 1504 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1505 | if (!task) { |
| 1506 | /* |
| 1507 | * Per cpu events are removed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1508 | * the removal is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1509 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1510 | cpu_function_call(event->cpu, __perf_remove_from_context, &re); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1511 | return; |
| 1512 | } |
| 1513 | |
| 1514 | retry: |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1515 | if (!task_function_call(task, __perf_remove_from_context, &re)) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1516 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1517 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1518 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1519 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1520 | * If we failed to find a running task, but find the context active now |
| 1521 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1522 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1523 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1524 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1525 | goto retry; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1529 | * Since the task isn't running, its safe to remove the event, us |
| 1530 | * holding the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1531 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1532 | if (detach_group) |
| 1533 | perf_group_detach(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1534 | list_del_event(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1535 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1536 | } |
| 1537 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1538 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1539 | * Cross CPU call to disable a performance event |
| 1540 | */ |
K.Prasad | 500ad2d | 2012-08-02 13:46:35 +0530 | [diff] [blame] | 1541 | int __perf_event_disable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1542 | { |
| 1543 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1544 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1545 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1546 | |
| 1547 | /* |
| 1548 | * If this is a per-task event, need to check whether this |
| 1549 | * event's task is the current task on this cpu. |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1550 | * |
| 1551 | * Can trigger due to concurrent perf_event_context_sched_out() |
| 1552 | * flipping contexts around. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1553 | */ |
| 1554 | if (ctx->task && cpuctx->task_ctx != ctx) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1555 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1556 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1557 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1558 | |
| 1559 | /* |
| 1560 | * If the event is on, turn it off. |
| 1561 | * If it is in error state, leave it in error state. |
| 1562 | */ |
| 1563 | if (event->state >= PERF_EVENT_STATE_INACTIVE) { |
| 1564 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1565 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1566 | update_group_times(event); |
| 1567 | if (event == event->group_leader) |
| 1568 | group_sched_out(event, cpuctx, ctx); |
| 1569 | else |
| 1570 | event_sched_out(event, cpuctx, ctx); |
| 1571 | event->state = PERF_EVENT_STATE_OFF; |
| 1572 | } |
| 1573 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1574 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1575 | |
| 1576 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * Disable a event. |
| 1581 | * |
| 1582 | * If event->ctx is a cloned context, callers must make sure that |
| 1583 | * every task struct that event->ctx->task could possibly point to |
| 1584 | * remains valid. This condition is satisifed when called through |
| 1585 | * perf_event_for_each_child or perf_event_for_each because they |
| 1586 | * hold the top-level event's child_mutex, so any descendant that |
| 1587 | * goes to exit will block in sync_child_event. |
| 1588 | * When called from perf_pending_event it's OK because event->ctx |
| 1589 | * is the current context on this CPU and preemption is disabled, |
| 1590 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1591 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 1592 | void perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1593 | { |
| 1594 | struct perf_event_context *ctx = event->ctx; |
| 1595 | struct task_struct *task = ctx->task; |
| 1596 | |
| 1597 | if (!task) { |
| 1598 | /* |
| 1599 | * Disable the event on the cpu that it's on |
| 1600 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1601 | cpu_function_call(event->cpu, __perf_event_disable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1602 | return; |
| 1603 | } |
| 1604 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1605 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1606 | if (!task_function_call(task, __perf_event_disable, event)) |
| 1607 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1608 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1609 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1610 | /* |
| 1611 | * If the event is still active, we need to retry the cross-call. |
| 1612 | */ |
| 1613 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1614 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1615 | /* |
| 1616 | * Reload the task pointer, it might have been changed by |
| 1617 | * a concurrent perf_event_context_sched_out(). |
| 1618 | */ |
| 1619 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1620 | goto retry; |
| 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | * Since we have the lock this context can't be scheduled |
| 1625 | * in, so we can change the state safely. |
| 1626 | */ |
| 1627 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 1628 | update_group_times(event); |
| 1629 | event->state = PERF_EVENT_STATE_OFF; |
| 1630 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1631 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1632 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 1633 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1634 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1635 | static void perf_set_shadow_time(struct perf_event *event, |
| 1636 | struct perf_event_context *ctx, |
| 1637 | u64 tstamp) |
| 1638 | { |
| 1639 | /* |
| 1640 | * use the correct time source for the time snapshot |
| 1641 | * |
| 1642 | * We could get by without this by leveraging the |
| 1643 | * fact that to get to this function, the caller |
| 1644 | * has most likely already called update_context_time() |
| 1645 | * and update_cgrp_time_xx() and thus both timestamp |
| 1646 | * are identical (or very close). Given that tstamp is, |
| 1647 | * already adjusted for cgroup, we could say that: |
| 1648 | * tstamp - ctx->timestamp |
| 1649 | * is equivalent to |
| 1650 | * tstamp - cgrp->timestamp. |
| 1651 | * |
| 1652 | * Then, in perf_output_read(), the calculation would |
| 1653 | * work with no changes because: |
| 1654 | * - event is guaranteed scheduled in |
| 1655 | * - no scheduled out in between |
| 1656 | * - thus the timestamp would be the same |
| 1657 | * |
| 1658 | * But this is a bit hairy. |
| 1659 | * |
| 1660 | * So instead, we have an explicit cgroup call to remain |
| 1661 | * within the time time source all along. We believe it |
| 1662 | * is cleaner and simpler to understand. |
| 1663 | */ |
| 1664 | if (is_cgroup_event(event)) |
| 1665 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1666 | else |
| 1667 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1668 | } |
| 1669 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1670 | #define MAX_INTERRUPTS (~0ULL) |
| 1671 | |
| 1672 | static void perf_log_throttle(struct perf_event *event, int enable); |
| 1673 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1674 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1675 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1676 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1677 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1678 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1679 | u64 tstamp = perf_event_time(event); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1680 | int ret = 0; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1681 | |
Peter Zijlstra | 6334241 | 2014-05-05 11:49:16 +0200 | [diff] [blame] | 1682 | lockdep_assert_held(&ctx->lock); |
| 1683 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1684 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1685 | return 0; |
| 1686 | |
| 1687 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1688 | event->oncpu = smp_processor_id(); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1689 | |
| 1690 | /* |
| 1691 | * Unthrottle events, since we scheduled we might have missed several |
| 1692 | * ticks already, also for a heavily scheduling task there is little |
| 1693 | * guarantee it'll get a tick in a timely manner. |
| 1694 | */ |
| 1695 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1696 | perf_log_throttle(event, 1); |
| 1697 | event->hw.interrupts = 0; |
| 1698 | } |
| 1699 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1700 | /* |
| 1701 | * The new state must be visible before we turn it on in the hardware: |
| 1702 | */ |
| 1703 | smp_wmb(); |
| 1704 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1705 | perf_pmu_disable(event->pmu); |
| 1706 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1707 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1708 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1709 | event->oncpu = -1; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1710 | ret = -EAGAIN; |
| 1711 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1712 | } |
| 1713 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1714 | event->tstamp_running += tstamp - event->tstamp_stopped; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1715 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1716 | perf_set_shadow_time(event, ctx, tstamp); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 1717 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1718 | if (!is_software_event(event)) |
| 1719 | cpuctx->active_oncpu++; |
| 1720 | ctx->nr_active++; |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1721 | if (event->attr.freq && event->attr.sample_freq) |
| 1722 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1723 | |
| 1724 | if (event->attr.exclusive) |
| 1725 | cpuctx->exclusive = 1; |
| 1726 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1727 | out: |
| 1728 | perf_pmu_enable(event->pmu); |
| 1729 | |
| 1730 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | static int |
| 1734 | group_sched_in(struct perf_event *group_event, |
| 1735 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1736 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1737 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1738 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 4a23459 | 2014-02-24 12:43:31 +0100 | [diff] [blame] | 1739 | struct pmu *pmu = ctx->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1740 | u64 now = ctx->time; |
| 1741 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1742 | |
| 1743 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1744 | return 0; |
| 1745 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1746 | pmu->start_txn(pmu); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1747 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1748 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1749 | pmu->cancel_txn(pmu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1750 | perf_cpu_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1751 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1752 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1753 | |
| 1754 | /* |
| 1755 | * Schedule in siblings as one group (if any): |
| 1756 | */ |
| 1757 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1758 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1759 | partial_group = event; |
| 1760 | goto group_error; |
| 1761 | } |
| 1762 | } |
| 1763 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1764 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 1765 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1766 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1767 | group_error: |
| 1768 | /* |
| 1769 | * Groups can be scheduled in as one unit only, so undo any |
| 1770 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1771 | * The events up to the failed event are scheduled out normally, |
| 1772 | * tstamp_stopped will be updated. |
| 1773 | * |
| 1774 | * The failed events and the remaining siblings need to have |
| 1775 | * their timings updated as if they had gone thru event_sched_in() |
| 1776 | * and event_sched_out(). This is required to get consistent timings |
| 1777 | * across the group. This also takes care of the case where the group |
| 1778 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 1779 | * the time the event was actually stopped, such that time delta |
| 1780 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1781 | */ |
| 1782 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 1783 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1784 | simulate = true; |
| 1785 | |
| 1786 | if (simulate) { |
| 1787 | event->tstamp_running += now - event->tstamp_stopped; |
| 1788 | event->tstamp_stopped = now; |
| 1789 | } else { |
| 1790 | event_sched_out(event, cpuctx, ctx); |
| 1791 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1792 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1793 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1794 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1795 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1796 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1797 | perf_cpu_hrtimer_restart(cpuctx); |
| 1798 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1799 | return -EAGAIN; |
| 1800 | } |
| 1801 | |
| 1802 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1803 | * Work out whether we can put this event group on the CPU now. |
| 1804 | */ |
| 1805 | static int group_can_go_on(struct perf_event *event, |
| 1806 | struct perf_cpu_context *cpuctx, |
| 1807 | int can_add_hw) |
| 1808 | { |
| 1809 | /* |
| 1810 | * Groups consisting entirely of software events can always go on. |
| 1811 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1812 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1813 | return 1; |
| 1814 | /* |
| 1815 | * If an exclusive group is already on, no other hardware |
| 1816 | * events can go on. |
| 1817 | */ |
| 1818 | if (cpuctx->exclusive) |
| 1819 | return 0; |
| 1820 | /* |
| 1821 | * If this group is exclusive and there are already |
| 1822 | * events on the CPU, it can't go on. |
| 1823 | */ |
| 1824 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 1825 | return 0; |
| 1826 | /* |
| 1827 | * Otherwise, try to add it if all previous groups were able |
| 1828 | * to go on. |
| 1829 | */ |
| 1830 | return can_add_hw; |
| 1831 | } |
| 1832 | |
| 1833 | static void add_event_to_ctx(struct perf_event *event, |
| 1834 | struct perf_event_context *ctx) |
| 1835 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1836 | u64 tstamp = perf_event_time(event); |
| 1837 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1838 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1839 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1840 | event->tstamp_enabled = tstamp; |
| 1841 | event->tstamp_running = tstamp; |
| 1842 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1843 | } |
| 1844 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1845 | static void task_ctx_sched_out(struct perf_event_context *ctx); |
| 1846 | static void |
| 1847 | ctx_sched_in(struct perf_event_context *ctx, |
| 1848 | struct perf_cpu_context *cpuctx, |
| 1849 | enum event_type_t event_type, |
| 1850 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1851 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 1852 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 1853 | struct perf_event_context *ctx, |
| 1854 | struct task_struct *task) |
| 1855 | { |
| 1856 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 1857 | if (ctx) |
| 1858 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 1859 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 1860 | if (ctx) |
| 1861 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 1862 | } |
| 1863 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1864 | /* |
| 1865 | * Cross CPU call to install and enable a performance event |
| 1866 | * |
| 1867 | * Must be called with ctx->mutex held |
| 1868 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1869 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1870 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1871 | struct perf_event *event = info; |
| 1872 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1873 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1874 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
| 1875 | struct task_struct *task = current; |
| 1876 | |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1877 | perf_ctx_lock(cpuctx, task_ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1878 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1879 | |
| 1880 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1881 | * If there was an active task_ctx schedule it out. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1882 | */ |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1883 | if (task_ctx) |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1884 | task_ctx_sched_out(task_ctx); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1885 | |
| 1886 | /* |
| 1887 | * If the context we're installing events in is not the |
| 1888 | * active task_ctx, flip them. |
| 1889 | */ |
| 1890 | if (ctx->task && task_ctx != ctx) { |
| 1891 | if (task_ctx) |
| 1892 | raw_spin_unlock(&task_ctx->lock); |
| 1893 | raw_spin_lock(&ctx->lock); |
| 1894 | task_ctx = ctx; |
| 1895 | } |
| 1896 | |
| 1897 | if (task_ctx) { |
| 1898 | cpuctx->task_ctx = task_ctx; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1899 | task = task_ctx->task; |
| 1900 | } |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1901 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1902 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1903 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1904 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1905 | /* |
| 1906 | * update cgrp time only if current cgrp |
| 1907 | * matches event->cgrp. Must be done before |
| 1908 | * calling add_event_to_ctx() |
| 1909 | */ |
| 1910 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1911 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1912 | add_event_to_ctx(event, ctx); |
| 1913 | |
| 1914 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1915 | * Schedule everything back in |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1916 | */ |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 1917 | perf_event_sched_in(cpuctx, task_ctx, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1918 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1919 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 1920 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1921 | |
| 1922 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | /* |
| 1926 | * Attach a performance event to a context |
| 1927 | * |
| 1928 | * First we add the event to the list with the hardware enable bit |
| 1929 | * in event->hw_config cleared. |
| 1930 | * |
| 1931 | * If the event is attached to a task which is on a CPU we use a smp |
| 1932 | * call to enable it in the task context. The task might have been |
| 1933 | * scheduled away, but we check this in the smp call again. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1934 | */ |
| 1935 | static void |
| 1936 | perf_install_in_context(struct perf_event_context *ctx, |
| 1937 | struct perf_event *event, |
| 1938 | int cpu) |
| 1939 | { |
| 1940 | struct task_struct *task = ctx->task; |
| 1941 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1942 | lockdep_assert_held(&ctx->mutex); |
| 1943 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 1944 | event->ctx = ctx; |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 1945 | if (event->cpu != -1) |
| 1946 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 1947 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1948 | if (!task) { |
| 1949 | /* |
| 1950 | * Per cpu events are installed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1951 | * the install is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1952 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1953 | cpu_function_call(cpu, __perf_install_in_context, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1954 | return; |
| 1955 | } |
| 1956 | |
| 1957 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1958 | if (!task_function_call(task, __perf_install_in_context, event)) |
| 1959 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1960 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1961 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1962 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1963 | * If we failed to find a running task, but find the context active now |
| 1964 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1965 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1966 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1967 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1968 | goto retry; |
| 1969 | } |
| 1970 | |
| 1971 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1972 | * Since the task isn't running, its safe to add the event, us holding |
| 1973 | * the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1974 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1975 | add_event_to_ctx(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1976 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | /* |
| 1980 | * Put a event into inactive state and update time fields. |
| 1981 | * Enabling the leader of a group effectively enables all |
| 1982 | * the group members that aren't explicitly disabled, so we |
| 1983 | * have to update their ->tstamp_enabled also. |
| 1984 | * Note: this works for group members as well as group leaders |
| 1985 | * since the non-leader members' sibling_lists will be empty. |
| 1986 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 1987 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1988 | { |
| 1989 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1990 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1991 | |
| 1992 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1993 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1994 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1995 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 1996 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1997 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | /* |
| 2001 | * Cross CPU call to enable a performance event |
| 2002 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2003 | static int __perf_event_enable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2004 | { |
| 2005 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2006 | struct perf_event_context *ctx = event->ctx; |
| 2007 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2008 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2009 | int err; |
| 2010 | |
Jiri Olsa | 06f4179 | 2013-07-09 17:44:11 +0200 | [diff] [blame] | 2011 | /* |
| 2012 | * There's a time window between 'ctx->is_active' check |
| 2013 | * in perf_event_enable function and this place having: |
| 2014 | * - IRQs on |
| 2015 | * - ctx->lock unlocked |
| 2016 | * |
| 2017 | * where the task could be killed and 'ctx' deactivated |
| 2018 | * by perf_event_exit_task. |
| 2019 | */ |
| 2020 | if (!ctx->is_active) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2021 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2022 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2023 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2024 | update_context_time(ctx); |
| 2025 | |
| 2026 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2027 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2028 | |
| 2029 | /* |
| 2030 | * set current task's cgroup time reference point |
| 2031 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2032 | perf_cgroup_set_timestamp(current, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2033 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2034 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2035 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2036 | if (!event_filter_match(event)) { |
| 2037 | if (is_cgroup_event(event)) |
| 2038 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2039 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2040 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2041 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2042 | /* |
| 2043 | * If the event is in a group and isn't the group leader, |
| 2044 | * then don't put it on unless the group is on. |
| 2045 | */ |
| 2046 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) |
| 2047 | goto unlock; |
| 2048 | |
| 2049 | if (!group_can_go_on(event, cpuctx, 1)) { |
| 2050 | err = -EEXIST; |
| 2051 | } else { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2052 | if (event == leader) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2053 | err = group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2054 | else |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2055 | err = event_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | if (err) { |
| 2059 | /* |
| 2060 | * If this event can't go on and it's part of a |
| 2061 | * group, then the whole group has to come off. |
| 2062 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2063 | if (leader != event) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2064 | group_sched_out(leader, cpuctx, ctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2065 | perf_cpu_hrtimer_restart(cpuctx); |
| 2066 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2067 | if (leader->attr.pinned) { |
| 2068 | update_group_times(leader); |
| 2069 | leader->state = PERF_EVENT_STATE_ERROR; |
| 2070 | } |
| 2071 | } |
| 2072 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2073 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2074 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2075 | |
| 2076 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | /* |
| 2080 | * Enable a event. |
| 2081 | * |
| 2082 | * If event->ctx is a cloned context, callers must make sure that |
| 2083 | * every task struct that event->ctx->task could possibly point to |
| 2084 | * remains valid. This condition is satisfied when called through |
| 2085 | * perf_event_for_each_child or perf_event_for_each as described |
| 2086 | * for perf_event_disable. |
| 2087 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 2088 | void perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2089 | { |
| 2090 | struct perf_event_context *ctx = event->ctx; |
| 2091 | struct task_struct *task = ctx->task; |
| 2092 | |
| 2093 | if (!task) { |
| 2094 | /* |
| 2095 | * Enable the event on the cpu that it's on |
| 2096 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2097 | cpu_function_call(event->cpu, __perf_event_enable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2098 | return; |
| 2099 | } |
| 2100 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2101 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2102 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2103 | goto out; |
| 2104 | |
| 2105 | /* |
| 2106 | * If the event is in error state, clear that first. |
| 2107 | * That way, if we see the event in error state below, we |
| 2108 | * know that it has gone back into error state, as distinct |
| 2109 | * from the task having been scheduled away before the |
| 2110 | * cross-call arrived. |
| 2111 | */ |
| 2112 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2113 | event->state = PERF_EVENT_STATE_OFF; |
| 2114 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2115 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2116 | if (!ctx->is_active) { |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2117 | __perf_event_mark_enabled(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2118 | goto out; |
| 2119 | } |
| 2120 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2121 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2122 | |
| 2123 | if (!task_function_call(task, __perf_event_enable, event)) |
| 2124 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2125 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2126 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2127 | |
| 2128 | /* |
| 2129 | * If the context is active and the event is still off, |
| 2130 | * we need to retry the cross-call. |
| 2131 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2132 | if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) { |
| 2133 | /* |
| 2134 | * task could have been flipped by a concurrent |
| 2135 | * perf_event_context_sched_out() |
| 2136 | */ |
| 2137 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2138 | goto retry; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2139 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2140 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2141 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2142 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2143 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2144 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2145 | |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2146 | int perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2147 | { |
| 2148 | /* |
| 2149 | * not supported on inherited events |
| 2150 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2151 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2152 | return -EINVAL; |
| 2153 | |
| 2154 | atomic_add(refresh, &event->event_limit); |
| 2155 | perf_event_enable(event); |
| 2156 | |
| 2157 | return 0; |
| 2158 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2159 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2160 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2161 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2162 | struct perf_cpu_context *cpuctx, |
| 2163 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2164 | { |
| 2165 | struct perf_event *event; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2166 | int is_active = ctx->is_active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2167 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2168 | ctx->is_active &= ~event_type; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2169 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2170 | return; |
| 2171 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2172 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2173 | update_cgrp_time_from_cpuctx(cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2174 | if (!ctx->nr_active) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2175 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2176 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2177 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2178 | if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2179 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2180 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2181 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2182 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2183 | if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2184 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2185 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2186 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2187 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | /* |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2191 | * Test whether two contexts are equivalent, i.e. whether they have both been |
| 2192 | * cloned from the same version of the same context. |
| 2193 | * |
| 2194 | * Equivalence is measured using a generation number in the context that is |
| 2195 | * incremented on each modification to it; see unclone_ctx(), list_add_event() |
| 2196 | * and list_del_event(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2197 | */ |
| 2198 | static int context_equiv(struct perf_event_context *ctx1, |
| 2199 | struct perf_event_context *ctx2) |
| 2200 | { |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2201 | /* Pinning disables the swap optimization */ |
| 2202 | if (ctx1->pin_count || ctx2->pin_count) |
| 2203 | return 0; |
| 2204 | |
| 2205 | /* If ctx1 is the parent of ctx2 */ |
| 2206 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) |
| 2207 | return 1; |
| 2208 | |
| 2209 | /* If ctx2 is the parent of ctx1 */ |
| 2210 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) |
| 2211 | return 1; |
| 2212 | |
| 2213 | /* |
| 2214 | * If ctx1 and ctx2 have the same parent; we flatten the parent |
| 2215 | * hierarchy, see perf_event_init_context(). |
| 2216 | */ |
| 2217 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && |
| 2218 | ctx1->parent_gen == ctx2->parent_gen) |
| 2219 | return 1; |
| 2220 | |
| 2221 | /* Unmatched */ |
| 2222 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2223 | } |
| 2224 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2225 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2226 | struct perf_event *next_event) |
| 2227 | { |
| 2228 | u64 value; |
| 2229 | |
| 2230 | if (!event->attr.inherit_stat) |
| 2231 | return; |
| 2232 | |
| 2233 | /* |
| 2234 | * Update the event value, we cannot use perf_event_read() |
| 2235 | * because we're in the middle of a context switch and have IRQs |
| 2236 | * disabled, which upsets smp_call_function_single(), however |
| 2237 | * we know the event must be on the current CPU, therefore we |
| 2238 | * don't need to use it. |
| 2239 | */ |
| 2240 | switch (event->state) { |
| 2241 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2242 | event->pmu->read(event); |
| 2243 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2244 | |
| 2245 | case PERF_EVENT_STATE_INACTIVE: |
| 2246 | update_event_times(event); |
| 2247 | break; |
| 2248 | |
| 2249 | default: |
| 2250 | break; |
| 2251 | } |
| 2252 | |
| 2253 | /* |
| 2254 | * In order to keep per-task stats reliable we need to flip the event |
| 2255 | * values when we flip the contexts. |
| 2256 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2257 | value = local64_read(&next_event->count); |
| 2258 | value = local64_xchg(&event->count, value); |
| 2259 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2260 | |
| 2261 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2262 | swap(event->total_time_running, next_event->total_time_running); |
| 2263 | |
| 2264 | /* |
| 2265 | * Since we swizzled the values, update the user visible data too. |
| 2266 | */ |
| 2267 | perf_event_update_userpage(event); |
| 2268 | perf_event_update_userpage(next_event); |
| 2269 | } |
| 2270 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2271 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2272 | struct perf_event_context *next_ctx) |
| 2273 | { |
| 2274 | struct perf_event *event, *next_event; |
| 2275 | |
| 2276 | if (!ctx->nr_stat) |
| 2277 | return; |
| 2278 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2279 | update_context_time(ctx); |
| 2280 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2281 | event = list_first_entry(&ctx->event_list, |
| 2282 | struct perf_event, event_entry); |
| 2283 | |
| 2284 | next_event = list_first_entry(&next_ctx->event_list, |
| 2285 | struct perf_event, event_entry); |
| 2286 | |
| 2287 | while (&event->event_entry != &ctx->event_list && |
| 2288 | &next_event->event_entry != &next_ctx->event_list) { |
| 2289 | |
| 2290 | __perf_event_sync_stat(event, next_event); |
| 2291 | |
| 2292 | event = list_next_entry(event, event_entry); |
| 2293 | next_event = list_next_entry(next_event, event_entry); |
| 2294 | } |
| 2295 | } |
| 2296 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2297 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2298 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2299 | { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2300 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2301 | struct perf_event_context *next_ctx; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2302 | struct perf_event_context *parent, *next_parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2303 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2304 | int do_switch = 1; |
| 2305 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2306 | if (likely(!ctx)) |
| 2307 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2308 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2309 | cpuctx = __get_cpu_context(ctx); |
| 2310 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2311 | return; |
| 2312 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2313 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2314 | next_ctx = next->perf_event_ctxp[ctxn]; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2315 | if (!next_ctx) |
| 2316 | goto unlock; |
| 2317 | |
| 2318 | parent = rcu_dereference(ctx->parent_ctx); |
| 2319 | next_parent = rcu_dereference(next_ctx->parent_ctx); |
| 2320 | |
| 2321 | /* If neither context have a parent context; they cannot be clones. */ |
| 2322 | if (!parent && !next_parent) |
| 2323 | goto unlock; |
| 2324 | |
| 2325 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2326 | /* |
| 2327 | * Looks like the two contexts are clones, so we might be |
| 2328 | * able to optimize the context switch. We lock both |
| 2329 | * contexts and check that they are clones under the |
| 2330 | * lock (including re-checking that neither has been |
| 2331 | * uncloned in the meantime). It doesn't matter which |
| 2332 | * order we take the locks because no other cpu could |
| 2333 | * be trying to lock both of these tasks. |
| 2334 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2335 | raw_spin_lock(&ctx->lock); |
| 2336 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2337 | if (context_equiv(ctx, next_ctx)) { |
| 2338 | /* |
| 2339 | * XXX do we need a memory barrier of sorts |
| 2340 | * wrt to rcu_dereference() of perf_event_ctxp |
| 2341 | */ |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2342 | task->perf_event_ctxp[ctxn] = next_ctx; |
| 2343 | next->perf_event_ctxp[ctxn] = ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2344 | ctx->task = next; |
| 2345 | next_ctx->task = task; |
| 2346 | do_switch = 0; |
| 2347 | |
| 2348 | perf_event_sync_stat(ctx, next_ctx); |
| 2349 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2350 | raw_spin_unlock(&next_ctx->lock); |
| 2351 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2352 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2353 | unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2354 | rcu_read_unlock(); |
| 2355 | |
| 2356 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2357 | raw_spin_lock(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2358 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2359 | cpuctx->task_ctx = NULL; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2360 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2364 | #define for_each_task_context_nr(ctxn) \ |
| 2365 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2366 | |
| 2367 | /* |
| 2368 | * Called from scheduler to remove the events of the current task, |
| 2369 | * with interrupts disabled. |
| 2370 | * |
| 2371 | * We stop each event and update the event value in event->count. |
| 2372 | * |
| 2373 | * This does not protect us against NMI, but disable() |
| 2374 | * sets the disabled bit in the control field of event _before_ |
| 2375 | * accessing the event control register. If a NMI hits, then it will |
| 2376 | * not restart the event. |
| 2377 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2378 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2379 | struct task_struct *next) |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2380 | { |
| 2381 | int ctxn; |
| 2382 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2383 | for_each_task_context_nr(ctxn) |
| 2384 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2385 | |
| 2386 | /* |
| 2387 | * if cgroup events exist on this CPU, then we need |
| 2388 | * to check if we have to switch out PMU state. |
| 2389 | * cgroup event are system-wide mode only |
| 2390 | */ |
| 2391 | if (atomic_read(&__get_cpu_var(perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2392 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2393 | } |
| 2394 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2395 | static void task_ctx_sched_out(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2396 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2397 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2398 | |
| 2399 | if (!cpuctx->task_ctx) |
| 2400 | return; |
| 2401 | |
| 2402 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2403 | return; |
| 2404 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2405 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2406 | cpuctx->task_ctx = NULL; |
| 2407 | } |
| 2408 | |
| 2409 | /* |
| 2410 | * Called with IRQs disabled |
| 2411 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2412 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2413 | enum event_type_t event_type) |
| 2414 | { |
| 2415 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2419 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2420 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2421 | { |
| 2422 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2423 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2424 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2425 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2426 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2427 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2428 | continue; |
| 2429 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2430 | /* may need to reset tstamp_enabled */ |
| 2431 | if (is_cgroup_event(event)) |
| 2432 | perf_cgroup_mark_enabled(event, ctx); |
| 2433 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2434 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2435 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2436 | |
| 2437 | /* |
| 2438 | * If this pinned group hasn't been scheduled, |
| 2439 | * put it in error state. |
| 2440 | */ |
| 2441 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2442 | update_group_times(event); |
| 2443 | event->state = PERF_EVENT_STATE_ERROR; |
| 2444 | } |
| 2445 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | static void |
| 2449 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2450 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2451 | { |
| 2452 | struct perf_event *event; |
| 2453 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2454 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2455 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2456 | /* Ignore events in OFF or ERROR state */ |
| 2457 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2458 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2459 | /* |
| 2460 | * Listen to the 'cpu' scheduling filter constraint |
| 2461 | * of events: |
| 2462 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2463 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2464 | continue; |
| 2465 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2466 | /* may need to reset tstamp_enabled */ |
| 2467 | if (is_cgroup_event(event)) |
| 2468 | perf_cgroup_mark_enabled(event, ctx); |
| 2469 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2470 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2471 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2472 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2473 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2474 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | static void |
| 2478 | ctx_sched_in(struct perf_event_context *ctx, |
| 2479 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2480 | enum event_type_t event_type, |
| 2481 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2482 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2483 | u64 now; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2484 | int is_active = ctx->is_active; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2485 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2486 | ctx->is_active |= event_type; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2487 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2488 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2489 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2490 | now = perf_clock(); |
| 2491 | ctx->timestamp = now; |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2492 | perf_cgroup_set_timestamp(task, ctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2493 | /* |
| 2494 | * First go through the list and put on any pinned groups |
| 2495 | * in order to give them the best chance of going on. |
| 2496 | */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2497 | if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2498 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2499 | |
| 2500 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2501 | if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2502 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2503 | } |
| 2504 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2505 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2506 | enum event_type_t event_type, |
| 2507 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2508 | { |
| 2509 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2510 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2511 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2512 | } |
| 2513 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2514 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2515 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2516 | { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2517 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2518 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2519 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2520 | if (cpuctx->task_ctx == ctx) |
| 2521 | return; |
| 2522 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2523 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2524 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2525 | /* |
| 2526 | * We want to keep the following priority order: |
| 2527 | * cpu pinned (that don't need to move), task pinned, |
| 2528 | * cpu flexible, task flexible. |
| 2529 | */ |
| 2530 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2531 | |
Gleb Natapov | 1d5f003 | 2011-10-23 19:10:33 +0200 | [diff] [blame] | 2532 | if (ctx->nr_events) |
| 2533 | cpuctx->task_ctx = ctx; |
eranian@google.com | 9b33fa6 | 2010-03-10 22:26:05 -0800 | [diff] [blame] | 2534 | |
Gleb Natapov | 86b47c2 | 2011-11-22 16:08:21 +0200 | [diff] [blame] | 2535 | perf_event_sched_in(cpuctx, cpuctx->task_ctx, task); |
| 2536 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2537 | perf_pmu_enable(ctx->pmu); |
| 2538 | perf_ctx_unlock(cpuctx, ctx); |
| 2539 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2540 | /* |
| 2541 | * Since these rotations are per-cpu, we need to ensure the |
| 2542 | * cpu-context we got scheduled on is actually rotating. |
| 2543 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2544 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2545 | } |
| 2546 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2547 | /* |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2548 | * When sampling the branck stack in system-wide, it may be necessary |
| 2549 | * to flush the stack on context switch. This happens when the branch |
| 2550 | * stack does not tag its entries with the pid of the current task. |
| 2551 | * Otherwise it becomes impossible to associate a branch entry with a |
| 2552 | * task. This ambiguity is more likely to appear when the branch stack |
| 2553 | * supports priv level filtering and the user sets it to monitor only |
| 2554 | * at the user level (which could be a useful measurement in system-wide |
| 2555 | * mode). In that case, the risk is high of having a branch stack with |
| 2556 | * branch from multiple tasks. Flushing may mean dropping the existing |
| 2557 | * entries or stashing them somewhere in the PMU specific code layer. |
| 2558 | * |
| 2559 | * This function provides the context switch callback to the lower code |
| 2560 | * layer. It is invoked ONLY when there is at least one system-wide context |
| 2561 | * with at least one active event using taken branch sampling. |
| 2562 | */ |
| 2563 | static void perf_branch_stack_sched_in(struct task_struct *prev, |
| 2564 | struct task_struct *task) |
| 2565 | { |
| 2566 | struct perf_cpu_context *cpuctx; |
| 2567 | struct pmu *pmu; |
| 2568 | unsigned long flags; |
| 2569 | |
| 2570 | /* no need to flush branch stack if not changing task */ |
| 2571 | if (prev == task) |
| 2572 | return; |
| 2573 | |
| 2574 | local_irq_save(flags); |
| 2575 | |
| 2576 | rcu_read_lock(); |
| 2577 | |
| 2578 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 2579 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2580 | |
| 2581 | /* |
| 2582 | * check if the context has at least one |
| 2583 | * event using PERF_SAMPLE_BRANCH_STACK |
| 2584 | */ |
| 2585 | if (cpuctx->ctx.nr_branch_stack > 0 |
| 2586 | && pmu->flush_branch_stack) { |
| 2587 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2588 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2589 | |
| 2590 | perf_pmu_disable(pmu); |
| 2591 | |
| 2592 | pmu->flush_branch_stack(); |
| 2593 | |
| 2594 | perf_pmu_enable(pmu); |
| 2595 | |
| 2596 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
| 2597 | } |
| 2598 | } |
| 2599 | |
| 2600 | rcu_read_unlock(); |
| 2601 | |
| 2602 | local_irq_restore(flags); |
| 2603 | } |
| 2604 | |
| 2605 | /* |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2606 | * Called from scheduler to add the events of the current task |
| 2607 | * with interrupts disabled. |
| 2608 | * |
| 2609 | * We restore the event value and then enable it. |
| 2610 | * |
| 2611 | * This does not protect us against NMI, but enable() |
| 2612 | * sets the enabled bit in the control field of event _before_ |
| 2613 | * accessing the event control register. If a NMI hits, then it will |
| 2614 | * keep the event running. |
| 2615 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2616 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 2617 | struct task_struct *task) |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2618 | { |
| 2619 | struct perf_event_context *ctx; |
| 2620 | int ctxn; |
| 2621 | |
| 2622 | for_each_task_context_nr(ctxn) { |
| 2623 | ctx = task->perf_event_ctxp[ctxn]; |
| 2624 | if (likely(!ctx)) |
| 2625 | continue; |
| 2626 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2627 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2628 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2629 | /* |
| 2630 | * if cgroup events exist on this CPU, then we need |
| 2631 | * to check if we have to switch in PMU state. |
| 2632 | * cgroup event are system-wide mode only |
| 2633 | */ |
| 2634 | if (atomic_read(&__get_cpu_var(perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2635 | perf_cgroup_sched_in(prev, task); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2636 | |
| 2637 | /* check for system-wide branch_stack events */ |
| 2638 | if (atomic_read(&__get_cpu_var(perf_branch_stack_events))) |
| 2639 | perf_branch_stack_sched_in(prev, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2640 | } |
| 2641 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2642 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 2643 | { |
| 2644 | u64 frequency = event->attr.sample_freq; |
| 2645 | u64 sec = NSEC_PER_SEC; |
| 2646 | u64 divisor, dividend; |
| 2647 | |
| 2648 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 2649 | |
| 2650 | count_fls = fls64(count); |
| 2651 | nsec_fls = fls64(nsec); |
| 2652 | frequency_fls = fls64(frequency); |
| 2653 | sec_fls = 30; |
| 2654 | |
| 2655 | /* |
| 2656 | * We got @count in @nsec, with a target of sample_freq HZ |
| 2657 | * the target period becomes: |
| 2658 | * |
| 2659 | * @count * 10^9 |
| 2660 | * period = ------------------- |
| 2661 | * @nsec * sample_freq |
| 2662 | * |
| 2663 | */ |
| 2664 | |
| 2665 | /* |
| 2666 | * Reduce accuracy by one bit such that @a and @b converge |
| 2667 | * to a similar magnitude. |
| 2668 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2669 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2670 | do { \ |
| 2671 | if (a##_fls > b##_fls) { \ |
| 2672 | a >>= 1; \ |
| 2673 | a##_fls--; \ |
| 2674 | } else { \ |
| 2675 | b >>= 1; \ |
| 2676 | b##_fls--; \ |
| 2677 | } \ |
| 2678 | } while (0) |
| 2679 | |
| 2680 | /* |
| 2681 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 2682 | * the other, so that finally we can do a u64/u64 division. |
| 2683 | */ |
| 2684 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 2685 | REDUCE_FLS(nsec, frequency); |
| 2686 | REDUCE_FLS(sec, count); |
| 2687 | } |
| 2688 | |
| 2689 | if (count_fls + sec_fls > 64) { |
| 2690 | divisor = nsec * frequency; |
| 2691 | |
| 2692 | while (count_fls + sec_fls > 64) { |
| 2693 | REDUCE_FLS(count, sec); |
| 2694 | divisor >>= 1; |
| 2695 | } |
| 2696 | |
| 2697 | dividend = count * sec; |
| 2698 | } else { |
| 2699 | dividend = count * sec; |
| 2700 | |
| 2701 | while (nsec_fls + frequency_fls > 64) { |
| 2702 | REDUCE_FLS(nsec, frequency); |
| 2703 | dividend >>= 1; |
| 2704 | } |
| 2705 | |
| 2706 | divisor = nsec * frequency; |
| 2707 | } |
| 2708 | |
Peter Zijlstra | f6ab91a | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2709 | if (!divisor) |
| 2710 | return dividend; |
| 2711 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2712 | return div64_u64(dividend, divisor); |
| 2713 | } |
| 2714 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2715 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 2716 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 2717 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2718 | 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] | 2719 | { |
| 2720 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91a | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2721 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2722 | s64 delta; |
| 2723 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2724 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2725 | |
| 2726 | delta = (s64)(period - hwc->sample_period); |
| 2727 | delta = (delta + 7) / 8; /* low pass filter */ |
| 2728 | |
| 2729 | sample_period = hwc->sample_period + delta; |
| 2730 | |
| 2731 | if (!sample_period) |
| 2732 | sample_period = 1; |
| 2733 | |
| 2734 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2735 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2736 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2737 | if (disable) |
| 2738 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2739 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2740 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2741 | |
| 2742 | if (disable) |
| 2743 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2744 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2745 | } |
| 2746 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2747 | /* |
| 2748 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 2749 | * events. At the same time, make sure, having freq events does not change |
| 2750 | * the rate of unthrottling as that would introduce bias. |
| 2751 | */ |
| 2752 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 2753 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2754 | { |
| 2755 | struct perf_event *event; |
| 2756 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2757 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2758 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2759 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2760 | /* |
| 2761 | * only need to iterate over all events iff: |
| 2762 | * - context have events in frequency mode (needs freq adjust) |
| 2763 | * - there are events to unthrottle on this cpu |
| 2764 | */ |
| 2765 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2766 | return; |
| 2767 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2768 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2769 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2770 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 2771 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2772 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 2773 | continue; |
| 2774 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2775 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 2776 | continue; |
| 2777 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2778 | perf_pmu_disable(event->pmu); |
| 2779 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2780 | hwc = &event->hw; |
| 2781 | |
Jiri Olsa | ae23bff | 2013-08-24 16:45:54 +0200 | [diff] [blame] | 2782 | if (hwc->interrupts == MAX_INTERRUPTS) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2783 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2784 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2785 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | if (!event->attr.freq || !event->attr.sample_freq) |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2789 | goto next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2790 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2791 | /* |
| 2792 | * stop the event and update event->count |
| 2793 | */ |
| 2794 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2795 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2796 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2797 | delta = now - hwc->freq_count_stamp; |
| 2798 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2799 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2800 | /* |
| 2801 | * restart the event |
| 2802 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2803 | * we have stopped the event so tell that |
| 2804 | * to perf_adjust_period() to avoid stopping it |
| 2805 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2806 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2807 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2808 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2809 | |
| 2810 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2811 | next: |
| 2812 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2813 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2814 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2815 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2816 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | /* |
| 2820 | * Round-robin a context's events: |
| 2821 | */ |
| 2822 | static void rotate_ctx(struct perf_event_context *ctx) |
| 2823 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 2824 | /* |
| 2825 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 2826 | * disabled by the inheritance code. |
| 2827 | */ |
| 2828 | if (!ctx->rotate_disable) |
| 2829 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2830 | } |
| 2831 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2832 | /* |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2833 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 2834 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 2835 | * disabled, while rotate_context is called from IRQ context. |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2836 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2837 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2838 | { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2839 | struct perf_event_context *ctx = NULL; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2840 | int rotate = 0, remove = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2841 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2842 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2843 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2844 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 2845 | rotate = 1; |
| 2846 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2847 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2848 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2849 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2850 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2851 | if (ctx->nr_events != ctx->nr_active) |
| 2852 | rotate = 1; |
| 2853 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2854 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2855 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2856 | goto done; |
| 2857 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2858 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2859 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2860 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2861 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2862 | if (ctx) |
| 2863 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 2864 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2865 | rotate_ctx(&cpuctx->ctx); |
| 2866 | if (ctx) |
| 2867 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2868 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2869 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2870 | |
| 2871 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 2872 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2873 | done: |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2874 | if (remove) |
| 2875 | list_del_init(&cpuctx->rotation_list); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2876 | |
| 2877 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2878 | } |
| 2879 | |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2880 | #ifdef CONFIG_NO_HZ_FULL |
| 2881 | bool perf_event_can_stop_tick(void) |
| 2882 | { |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 2883 | if (atomic_read(&nr_freq_events) || |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 2884 | __this_cpu_read(perf_throttled_count)) |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2885 | return false; |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 2886 | else |
| 2887 | return true; |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2888 | } |
| 2889 | #endif |
| 2890 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2891 | void perf_event_task_tick(void) |
| 2892 | { |
| 2893 | struct list_head *head = &__get_cpu_var(rotation_list); |
| 2894 | struct perf_cpu_context *cpuctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2895 | struct perf_event_context *ctx; |
| 2896 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2897 | |
| 2898 | WARN_ON(!irqs_disabled()); |
| 2899 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2900 | __this_cpu_inc(perf_throttled_seq); |
| 2901 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
| 2902 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2903 | list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2904 | ctx = &cpuctx->ctx; |
| 2905 | perf_adjust_freq_unthr_context(ctx, throttled); |
| 2906 | |
| 2907 | ctx = cpuctx->task_ctx; |
| 2908 | if (ctx) |
| 2909 | perf_adjust_freq_unthr_context(ctx, throttled); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2910 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2911 | } |
| 2912 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2913 | static int event_enable_on_exec(struct perf_event *event, |
| 2914 | struct perf_event_context *ctx) |
| 2915 | { |
| 2916 | if (!event->attr.enable_on_exec) |
| 2917 | return 0; |
| 2918 | |
| 2919 | event->attr.enable_on_exec = 0; |
| 2920 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2921 | return 0; |
| 2922 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2923 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2924 | |
| 2925 | return 1; |
| 2926 | } |
| 2927 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2928 | /* |
| 2929 | * Enable all of a task's events that have been marked enable-on-exec. |
| 2930 | * This expects task == current. |
| 2931 | */ |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2932 | static void perf_event_enable_on_exec(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2933 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2934 | struct perf_event *event; |
| 2935 | unsigned long flags; |
| 2936 | int enabled = 0; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2937 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2938 | |
| 2939 | local_irq_save(flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2940 | if (!ctx || !ctx->nr_events) |
| 2941 | goto out; |
| 2942 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 2943 | /* |
| 2944 | * We must ctxsw out cgroup events to avoid conflict |
| 2945 | * when invoking perf_task_event_sched_in() later on |
| 2946 | * in this function. Otherwise we end up trying to |
| 2947 | * ctxswin cgroup events which are already scheduled |
| 2948 | * in. |
| 2949 | */ |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2950 | perf_cgroup_sched_out(current, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2951 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2952 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2953 | task_ctx_sched_out(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2954 | |
Peter Zijlstra | b79387e | 2011-11-22 11:25:43 +0100 | [diff] [blame] | 2955 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2956 | ret = event_enable_on_exec(event, ctx); |
| 2957 | if (ret) |
| 2958 | enabled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | /* |
| 2962 | * Unclone this context if we enabled any event. |
| 2963 | */ |
| 2964 | if (enabled) |
| 2965 | unclone_ctx(ctx); |
| 2966 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2967 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2968 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 2969 | /* |
| 2970 | * Also calls ctxswin for cgroup events, if any: |
| 2971 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2972 | perf_event_context_sched_in(ctx, ctx->task); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2973 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2974 | local_irq_restore(flags); |
| 2975 | } |
| 2976 | |
Peter Zijlstra | e041e32 | 2014-05-21 17:32:19 +0200 | [diff] [blame] | 2977 | void perf_event_exec(void) |
| 2978 | { |
| 2979 | struct perf_event_context *ctx; |
| 2980 | int ctxn; |
| 2981 | |
| 2982 | rcu_read_lock(); |
| 2983 | for_each_task_context_nr(ctxn) { |
| 2984 | ctx = current->perf_event_ctxp[ctxn]; |
| 2985 | if (!ctx) |
| 2986 | continue; |
| 2987 | |
| 2988 | perf_event_enable_on_exec(ctx); |
| 2989 | } |
| 2990 | rcu_read_unlock(); |
| 2991 | } |
| 2992 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2993 | /* |
| 2994 | * Cross CPU call to read the hardware event |
| 2995 | */ |
| 2996 | static void __perf_event_read(void *info) |
| 2997 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2998 | struct perf_event *event = info; |
| 2999 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3000 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3001 | |
| 3002 | /* |
| 3003 | * If this is a task context, we need to check whether it is |
| 3004 | * the current task context of this cpu. If not it has been |
| 3005 | * scheduled out before the smp call arrived. In that case |
| 3006 | * event->count would have been updated to a recent sample |
| 3007 | * when the event was scheduled out. |
| 3008 | */ |
| 3009 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 3010 | return; |
| 3011 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3012 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3013 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3014 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3015 | update_cgrp_time_from_event(event); |
| 3016 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3017 | update_event_times(event); |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3018 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
| 3019 | event->pmu->read(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3020 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3021 | } |
| 3022 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3023 | static inline u64 perf_event_count(struct perf_event *event) |
| 3024 | { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3025 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3026 | } |
| 3027 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3028 | static u64 perf_event_read(struct perf_event *event) |
| 3029 | { |
| 3030 | /* |
| 3031 | * If event is enabled and currently active on a CPU, update the |
| 3032 | * value in the event structure: |
| 3033 | */ |
| 3034 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
| 3035 | smp_call_function_single(event->oncpu, |
| 3036 | __perf_event_read, event, 1); |
| 3037 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 3038 | struct perf_event_context *ctx = event->ctx; |
| 3039 | unsigned long flags; |
| 3040 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3041 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3042 | /* |
| 3043 | * may read while context is not active |
| 3044 | * (e.g., thread is blocked), in that case |
| 3045 | * we cannot update context time |
| 3046 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3047 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3048 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3049 | update_cgrp_time_from_event(event); |
| 3050 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3051 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3052 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3053 | } |
| 3054 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3055 | return perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | /* |
| 3059 | * Initialize the perf_event context in a task_struct: |
| 3060 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3061 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3062 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3063 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3064 | mutex_init(&ctx->mutex); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3065 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 3066 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3067 | INIT_LIST_HEAD(&ctx->event_list); |
| 3068 | atomic_set(&ctx->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3071 | static struct perf_event_context * |
| 3072 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3073 | { |
| 3074 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3075 | |
| 3076 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 3077 | if (!ctx) |
| 3078 | return NULL; |
| 3079 | |
| 3080 | __perf_event_init_context(ctx); |
| 3081 | if (task) { |
| 3082 | ctx->task = task; |
| 3083 | get_task_struct(task); |
| 3084 | } |
| 3085 | ctx->pmu = pmu; |
| 3086 | |
| 3087 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3088 | } |
| 3089 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3090 | static struct task_struct * |
| 3091 | find_lively_task_by_vpid(pid_t vpid) |
| 3092 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3093 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3094 | int err; |
| 3095 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3096 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3097 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3098 | task = current; |
| 3099 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3100 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3101 | if (task) |
| 3102 | get_task_struct(task); |
| 3103 | rcu_read_unlock(); |
| 3104 | |
| 3105 | if (!task) |
| 3106 | return ERR_PTR(-ESRCH); |
| 3107 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3108 | /* Reuse ptrace permission checks for now. */ |
| 3109 | err = -EACCES; |
| 3110 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 3111 | goto errout; |
| 3112 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3113 | return task; |
| 3114 | errout: |
| 3115 | put_task_struct(task); |
| 3116 | return ERR_PTR(err); |
| 3117 | |
| 3118 | } |
| 3119 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3120 | /* |
| 3121 | * Returns a matching context with refcount and pincount. |
| 3122 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3123 | static struct perf_event_context * |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 3124 | find_get_context(struct pmu *pmu, struct task_struct *task, int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3125 | { |
| 3126 | struct perf_event_context *ctx; |
| 3127 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3128 | unsigned long flags; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3129 | int ctxn, err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3130 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3131 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3132 | /* Must be root to operate on a CPU event: */ |
| 3133 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3134 | return ERR_PTR(-EACCES); |
| 3135 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3136 | /* |
| 3137 | * We could be clever and allow to attach a event to an |
| 3138 | * offline CPU and activate it when the CPU comes up, but |
| 3139 | * that's for later. |
| 3140 | */ |
| 3141 | if (!cpu_online(cpu)) |
| 3142 | return ERR_PTR(-ENODEV); |
| 3143 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3144 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3145 | ctx = &cpuctx->ctx; |
| 3146 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3147 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3148 | |
| 3149 | return ctx; |
| 3150 | } |
| 3151 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3152 | err = -EINVAL; |
| 3153 | ctxn = pmu->task_ctx_nr; |
| 3154 | if (ctxn < 0) |
| 3155 | goto errout; |
| 3156 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3157 | retry: |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3158 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3159 | if (ctx) { |
| 3160 | unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3161 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3162 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3163 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3164 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3165 | err = -ENOMEM; |
| 3166 | if (!ctx) |
| 3167 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3168 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3169 | err = 0; |
| 3170 | mutex_lock(&task->perf_event_mutex); |
| 3171 | /* |
| 3172 | * If it has already passed perf_event_exit_task(). |
| 3173 | * we must see PF_EXITING, it takes this mutex too. |
| 3174 | */ |
| 3175 | if (task->flags & PF_EXITING) |
| 3176 | err = -ESRCH; |
| 3177 | else if (task->perf_event_ctxp[ctxn]) |
| 3178 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3179 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3180 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3181 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3182 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3183 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3184 | mutex_unlock(&task->perf_event_mutex); |
| 3185 | |
| 3186 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3187 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3188 | |
| 3189 | if (err == -EAGAIN) |
| 3190 | goto retry; |
| 3191 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3192 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3193 | } |
| 3194 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3195 | return ctx; |
| 3196 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3197 | errout: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3198 | return ERR_PTR(err); |
| 3199 | } |
| 3200 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3201 | static void perf_event_free_filter(struct perf_event *event); |
| 3202 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3203 | static void free_event_rcu(struct rcu_head *head) |
| 3204 | { |
| 3205 | struct perf_event *event; |
| 3206 | |
| 3207 | event = container_of(head, struct perf_event, rcu_head); |
| 3208 | if (event->ns) |
| 3209 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3210 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3211 | kfree(event); |
| 3212 | } |
| 3213 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3214 | static void ring_buffer_put(struct ring_buffer *rb); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3215 | static void ring_buffer_attach(struct perf_event *event, |
| 3216 | struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3217 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3218 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
| 3219 | { |
| 3220 | if (event->parent) |
| 3221 | return; |
| 3222 | |
| 3223 | if (has_branch_stack(event)) { |
| 3224 | if (!(event->attach_state & PERF_ATTACH_TASK)) |
| 3225 | atomic_dec(&per_cpu(perf_branch_stack_events, cpu)); |
| 3226 | } |
| 3227 | if (is_cgroup_event(event)) |
| 3228 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); |
| 3229 | } |
| 3230 | |
| 3231 | static void unaccount_event(struct perf_event *event) |
| 3232 | { |
| 3233 | if (event->parent) |
| 3234 | return; |
| 3235 | |
| 3236 | if (event->attach_state & PERF_ATTACH_TASK) |
| 3237 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3238 | if (event->attr.mmap || event->attr.mmap_data) |
| 3239 | atomic_dec(&nr_mmap_events); |
| 3240 | if (event->attr.comm) |
| 3241 | atomic_dec(&nr_comm_events); |
| 3242 | if (event->attr.task) |
| 3243 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3244 | if (event->attr.freq) |
| 3245 | atomic_dec(&nr_freq_events); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3246 | if (is_cgroup_event(event)) |
| 3247 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3248 | if (has_branch_stack(event)) |
| 3249 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3250 | |
| 3251 | unaccount_event_cpu(event, event->cpu); |
| 3252 | } |
| 3253 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3254 | static void __free_event(struct perf_event *event) |
| 3255 | { |
| 3256 | if (!event->parent) { |
| 3257 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 3258 | put_callchain_buffers(); |
| 3259 | } |
| 3260 | |
| 3261 | if (event->destroy) |
| 3262 | event->destroy(event); |
| 3263 | |
| 3264 | if (event->ctx) |
| 3265 | put_ctx(event->ctx); |
| 3266 | |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 3267 | if (event->pmu) |
| 3268 | module_put(event->pmu->module); |
| 3269 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3270 | call_rcu(&event->rcu_head, free_event_rcu); |
| 3271 | } |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3272 | |
| 3273 | static void _free_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3274 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3275 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3276 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3277 | unaccount_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3278 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3279 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3280 | /* |
| 3281 | * Can happen when we close an event with re-directed output. |
| 3282 | * |
| 3283 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 3284 | * over us; possibly making our ring_buffer_put() the last. |
| 3285 | */ |
| 3286 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3287 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3288 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3289 | } |
| 3290 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3291 | if (is_cgroup_event(event)) |
| 3292 | perf_detach_cgroup(event); |
| 3293 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3294 | __free_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3295 | } |
| 3296 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3297 | /* |
| 3298 | * Used to free events which have a known refcount of 1, such as in error paths |
| 3299 | * where the event isn't exposed yet and inherited events. |
| 3300 | */ |
| 3301 | static void free_event(struct perf_event *event) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3302 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3303 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
| 3304 | "unexpected event refcount: %ld; ptr=%p\n", |
| 3305 | atomic_long_read(&event->refcount), event)) { |
| 3306 | /* leak to avoid use-after-free */ |
| 3307 | return; |
| 3308 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3309 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3310 | _free_event(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3311 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3312 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3313 | /* |
| 3314 | * Called when the last reference to the file is gone. |
| 3315 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3316 | static void put_event(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3317 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3318 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3319 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3320 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3321 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 3322 | return; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3323 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3324 | rcu_read_lock(); |
| 3325 | owner = ACCESS_ONCE(event->owner); |
| 3326 | /* |
| 3327 | * Matches the smp_wmb() in perf_event_exit_task(). If we observe |
| 3328 | * !owner it means the list deletion is complete and we can indeed |
| 3329 | * free this event, otherwise we need to serialize on |
| 3330 | * owner->perf_event_mutex. |
| 3331 | */ |
| 3332 | smp_read_barrier_depends(); |
| 3333 | if (owner) { |
| 3334 | /* |
| 3335 | * Since delayed_put_task_struct() also drops the last |
| 3336 | * task reference we can safely take a new reference |
| 3337 | * while holding the rcu_read_lock(). |
| 3338 | */ |
| 3339 | get_task_struct(owner); |
| 3340 | } |
| 3341 | rcu_read_unlock(); |
| 3342 | |
| 3343 | if (owner) { |
| 3344 | mutex_lock(&owner->perf_event_mutex); |
| 3345 | /* |
| 3346 | * We have to re-check the event->owner field, if it is cleared |
| 3347 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 3348 | * ensured they're done, and we can proceed with freeing the |
| 3349 | * event. |
| 3350 | */ |
| 3351 | if (event->owner) |
| 3352 | list_del_init(&event->owner_entry); |
| 3353 | mutex_unlock(&owner->perf_event_mutex); |
| 3354 | put_task_struct(owner); |
| 3355 | } |
| 3356 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3357 | WARN_ON_ONCE(ctx->parent_ctx); |
| 3358 | /* |
| 3359 | * There are two ways this annotation is useful: |
| 3360 | * |
| 3361 | * 1) there is a lock recursion from perf_event_exit_task |
| 3362 | * see the comment there. |
| 3363 | * |
| 3364 | * 2) there is a lock-inversion with mmap_sem through |
| 3365 | * perf_event_read_group(), which takes faults while |
| 3366 | * holding ctx->mutex, however this is called after |
| 3367 | * the last filedesc died, so there is no possibility |
| 3368 | * to trigger the AB-BA case. |
| 3369 | */ |
| 3370 | mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING); |
| 3371 | perf_remove_from_context(event, true); |
| 3372 | mutex_unlock(&ctx->mutex); |
| 3373 | |
| 3374 | _free_event(event); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3375 | } |
| 3376 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3377 | int perf_event_release_kernel(struct perf_event *event) |
| 3378 | { |
| 3379 | put_event(event); |
| 3380 | return 0; |
| 3381 | } |
| 3382 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 3383 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3384 | static int perf_release(struct inode *inode, struct file *file) |
| 3385 | { |
| 3386 | put_event(file->private_data); |
| 3387 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3388 | } |
| 3389 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3390 | 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] | 3391 | { |
| 3392 | struct perf_event *child; |
| 3393 | u64 total = 0; |
| 3394 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3395 | *enabled = 0; |
| 3396 | *running = 0; |
| 3397 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3398 | mutex_lock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3399 | total += perf_event_read(event); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3400 | *enabled += event->total_time_enabled + |
| 3401 | atomic64_read(&event->child_total_time_enabled); |
| 3402 | *running += event->total_time_running + |
| 3403 | atomic64_read(&event->child_total_time_running); |
| 3404 | |
| 3405 | list_for_each_entry(child, &event->child_list, child_list) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3406 | total += perf_event_read(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3407 | *enabled += child->total_time_enabled; |
| 3408 | *running += child->total_time_running; |
| 3409 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3410 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3411 | |
| 3412 | return total; |
| 3413 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3414 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3415 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3416 | static int perf_event_read_group(struct perf_event *event, |
| 3417 | u64 read_format, char __user *buf) |
| 3418 | { |
| 3419 | struct perf_event *leader = event->group_leader, *sub; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3420 | int n = 0, size = 0, ret = -EFAULT; |
| 3421 | struct perf_event_context *ctx = leader->ctx; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3422 | u64 values[5]; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3423 | u64 count, enabled, running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3424 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3425 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3426 | count = perf_event_read_value(leader, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3427 | |
| 3428 | values[n++] = 1 + leader->nr_siblings; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3429 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3430 | values[n++] = enabled; |
| 3431 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3432 | values[n++] = running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3433 | values[n++] = count; |
| 3434 | if (read_format & PERF_FORMAT_ID) |
| 3435 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3436 | |
| 3437 | size = n * sizeof(u64); |
| 3438 | |
| 3439 | if (copy_to_user(buf, values, size)) |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3440 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3441 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3442 | ret = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3443 | |
| 3444 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3445 | n = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3446 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3447 | values[n++] = perf_event_read_value(sub, &enabled, &running); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3448 | if (read_format & PERF_FORMAT_ID) |
| 3449 | values[n++] = primary_event_id(sub); |
| 3450 | |
| 3451 | size = n * sizeof(u64); |
| 3452 | |
Stephane Eranian | 184d3da | 2009-11-23 21:40:49 -0800 | [diff] [blame] | 3453 | if (copy_to_user(buf + ret, values, size)) { |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3454 | ret = -EFAULT; |
| 3455 | goto unlock; |
| 3456 | } |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3457 | |
| 3458 | ret += size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3459 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3460 | unlock: |
| 3461 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3462 | |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3463 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | static int perf_event_read_one(struct perf_event *event, |
| 3467 | u64 read_format, char __user *buf) |
| 3468 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3469 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3470 | u64 values[4]; |
| 3471 | int n = 0; |
| 3472 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3473 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 3474 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3475 | values[n++] = enabled; |
| 3476 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3477 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3478 | if (read_format & PERF_FORMAT_ID) |
| 3479 | values[n++] = primary_event_id(event); |
| 3480 | |
| 3481 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 3482 | return -EFAULT; |
| 3483 | |
| 3484 | return n * sizeof(u64); |
| 3485 | } |
| 3486 | |
| 3487 | /* |
| 3488 | * Read the performance event - simple non blocking version for now |
| 3489 | */ |
| 3490 | static ssize_t |
| 3491 | perf_read_hw(struct perf_event *event, char __user *buf, size_t count) |
| 3492 | { |
| 3493 | u64 read_format = event->attr.read_format; |
| 3494 | int ret; |
| 3495 | |
| 3496 | /* |
| 3497 | * Return end-of-file for a read on a event that is in |
| 3498 | * error state (i.e. because it was pinned but it couldn't be |
| 3499 | * scheduled on to the CPU at some point). |
| 3500 | */ |
| 3501 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 3502 | return 0; |
| 3503 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 3504 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3505 | return -ENOSPC; |
| 3506 | |
| 3507 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3508 | if (read_format & PERF_FORMAT_GROUP) |
| 3509 | ret = perf_event_read_group(event, read_format, buf); |
| 3510 | else |
| 3511 | ret = perf_event_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3512 | |
| 3513 | return ret; |
| 3514 | } |
| 3515 | |
| 3516 | static ssize_t |
| 3517 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 3518 | { |
| 3519 | struct perf_event *event = file->private_data; |
| 3520 | |
| 3521 | return perf_read_hw(event, buf, count); |
| 3522 | } |
| 3523 | |
| 3524 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 3525 | { |
| 3526 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3527 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3528 | unsigned int events = POLL_HUP; |
| 3529 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3530 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3531 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 3532 | * 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] | 3533 | */ |
| 3534 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3535 | rb = event->rb; |
| 3536 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3537 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3538 | mutex_unlock(&event->mmap_mutex); |
| 3539 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3540 | poll_wait(file, &event->waitq, wait); |
| 3541 | |
| 3542 | return events; |
| 3543 | } |
| 3544 | |
| 3545 | static void perf_event_reset(struct perf_event *event) |
| 3546 | { |
| 3547 | (void)perf_event_read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3548 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3549 | perf_event_update_userpage(event); |
| 3550 | } |
| 3551 | |
| 3552 | /* |
| 3553 | * Holding the top-level event's child_mutex means that any |
| 3554 | * descendant process that has inherited this event will block |
| 3555 | * in sync_child_event if it goes to exit, thus satisfying the |
| 3556 | * task existence requirements of perf_event_enable/disable. |
| 3557 | */ |
| 3558 | static void perf_event_for_each_child(struct perf_event *event, |
| 3559 | void (*func)(struct perf_event *)) |
| 3560 | { |
| 3561 | struct perf_event *child; |
| 3562 | |
| 3563 | WARN_ON_ONCE(event->ctx->parent_ctx); |
| 3564 | mutex_lock(&event->child_mutex); |
| 3565 | func(event); |
| 3566 | list_for_each_entry(child, &event->child_list, child_list) |
| 3567 | func(child); |
| 3568 | mutex_unlock(&event->child_mutex); |
| 3569 | } |
| 3570 | |
| 3571 | static void perf_event_for_each(struct perf_event *event, |
| 3572 | void (*func)(struct perf_event *)) |
| 3573 | { |
| 3574 | struct perf_event_context *ctx = event->ctx; |
| 3575 | struct perf_event *sibling; |
| 3576 | |
| 3577 | WARN_ON_ONCE(ctx->parent_ctx); |
| 3578 | mutex_lock(&ctx->mutex); |
| 3579 | event = event->group_leader; |
| 3580 | |
| 3581 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3582 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 3583 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3584 | mutex_unlock(&ctx->mutex); |
| 3585 | } |
| 3586 | |
| 3587 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 3588 | { |
| 3589 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3590 | int ret = 0, active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3591 | u64 value; |
| 3592 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 3593 | if (!is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3594 | return -EINVAL; |
| 3595 | |
John Blackwood | ad0cf34 | 2010-09-28 18:03:11 -0400 | [diff] [blame] | 3596 | if (copy_from_user(&value, arg, sizeof(value))) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3597 | return -EFAULT; |
| 3598 | |
| 3599 | if (!value) |
| 3600 | return -EINVAL; |
| 3601 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3602 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3603 | if (event->attr.freq) { |
| 3604 | if (value > sysctl_perf_event_sample_rate) { |
| 3605 | ret = -EINVAL; |
| 3606 | goto unlock; |
| 3607 | } |
| 3608 | |
| 3609 | event->attr.sample_freq = value; |
| 3610 | } else { |
| 3611 | event->attr.sample_period = value; |
| 3612 | event->hw.sample_period = value; |
| 3613 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3614 | |
| 3615 | active = (event->state == PERF_EVENT_STATE_ACTIVE); |
| 3616 | if (active) { |
| 3617 | perf_pmu_disable(ctx->pmu); |
| 3618 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3619 | } |
| 3620 | |
| 3621 | local64_set(&event->hw.period_left, 0); |
| 3622 | |
| 3623 | if (active) { |
| 3624 | event->pmu->start(event, PERF_EF_RELOAD); |
| 3625 | perf_pmu_enable(ctx->pmu); |
| 3626 | } |
| 3627 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3628 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3629 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3630 | |
| 3631 | return ret; |
| 3632 | } |
| 3633 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3634 | static const struct file_operations perf_fops; |
| 3635 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3636 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3637 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3638 | struct fd f = fdget(fd); |
| 3639 | if (!f.file) |
| 3640 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3641 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3642 | if (f.file->f_op != &perf_fops) { |
| 3643 | fdput(f); |
| 3644 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3645 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3646 | *p = f; |
| 3647 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | static int perf_event_set_output(struct perf_event *event, |
| 3651 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3652 | 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] | 3653 | |
| 3654 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 3655 | { |
| 3656 | struct perf_event *event = file->private_data; |
| 3657 | void (*func)(struct perf_event *); |
| 3658 | u32 flags = arg; |
| 3659 | |
| 3660 | switch (cmd) { |
| 3661 | case PERF_EVENT_IOC_ENABLE: |
| 3662 | func = perf_event_enable; |
| 3663 | break; |
| 3664 | case PERF_EVENT_IOC_DISABLE: |
| 3665 | func = perf_event_disable; |
| 3666 | break; |
| 3667 | case PERF_EVENT_IOC_RESET: |
| 3668 | func = perf_event_reset; |
| 3669 | break; |
| 3670 | |
| 3671 | case PERF_EVENT_IOC_REFRESH: |
| 3672 | return perf_event_refresh(event, arg); |
| 3673 | |
| 3674 | case PERF_EVENT_IOC_PERIOD: |
| 3675 | return perf_event_period(event, (u64 __user *)arg); |
| 3676 | |
Jiri Olsa | cf4957f | 2012-10-24 13:37:58 +0200 | [diff] [blame] | 3677 | case PERF_EVENT_IOC_ID: |
| 3678 | { |
| 3679 | u64 id = primary_event_id(event); |
| 3680 | |
| 3681 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) |
| 3682 | return -EFAULT; |
| 3683 | return 0; |
| 3684 | } |
| 3685 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3686 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3687 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3688 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3689 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3690 | struct perf_event *output_event; |
| 3691 | struct fd output; |
| 3692 | ret = perf_fget_light(arg, &output); |
| 3693 | if (ret) |
| 3694 | return ret; |
| 3695 | output_event = output.file->private_data; |
| 3696 | ret = perf_event_set_output(event, output_event); |
| 3697 | fdput(output); |
| 3698 | } else { |
| 3699 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3700 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3701 | return ret; |
| 3702 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3703 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3704 | case PERF_EVENT_IOC_SET_FILTER: |
| 3705 | return perf_event_set_filter(event, (void __user *)arg); |
| 3706 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3707 | default: |
| 3708 | return -ENOTTY; |
| 3709 | } |
| 3710 | |
| 3711 | if (flags & PERF_IOC_FLAG_GROUP) |
| 3712 | perf_event_for_each(event, func); |
| 3713 | else |
| 3714 | perf_event_for_each_child(event, func); |
| 3715 | |
| 3716 | return 0; |
| 3717 | } |
| 3718 | |
| 3719 | int perf_event_task_enable(void) |
| 3720 | { |
| 3721 | struct perf_event *event; |
| 3722 | |
| 3723 | mutex_lock(¤t->perf_event_mutex); |
| 3724 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3725 | perf_event_for_each_child(event, perf_event_enable); |
| 3726 | mutex_unlock(¤t->perf_event_mutex); |
| 3727 | |
| 3728 | return 0; |
| 3729 | } |
| 3730 | |
| 3731 | int perf_event_task_disable(void) |
| 3732 | { |
| 3733 | struct perf_event *event; |
| 3734 | |
| 3735 | mutex_lock(¤t->perf_event_mutex); |
| 3736 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3737 | perf_event_for_each_child(event, perf_event_disable); |
| 3738 | mutex_unlock(¤t->perf_event_mutex); |
| 3739 | |
| 3740 | return 0; |
| 3741 | } |
| 3742 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3743 | static int perf_event_index(struct perf_event *event) |
| 3744 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 3745 | if (event->hw.state & PERF_HES_STOPPED) |
| 3746 | return 0; |
| 3747 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3748 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 3749 | return 0; |
| 3750 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 3751 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3752 | } |
| 3753 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3754 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3755 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3756 | u64 *enabled, |
| 3757 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3758 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3759 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3760 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3761 | *now = perf_clock(); |
| 3762 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3763 | *enabled = ctx_time - event->tstamp_enabled; |
| 3764 | *running = ctx_time - event->tstamp_running; |
| 3765 | } |
| 3766 | |
Peter Zijlstra | fa73158 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 3767 | static void perf_event_init_userpage(struct perf_event *event) |
| 3768 | { |
| 3769 | struct perf_event_mmap_page *userpg; |
| 3770 | struct ring_buffer *rb; |
| 3771 | |
| 3772 | rcu_read_lock(); |
| 3773 | rb = rcu_dereference(event->rb); |
| 3774 | if (!rb) |
| 3775 | goto unlock; |
| 3776 | |
| 3777 | userpg = rb->user_page; |
| 3778 | |
| 3779 | /* Allow new userspace to detect that bit 0 is deprecated */ |
| 3780 | userpg->cap_bit0_is_deprecated = 1; |
| 3781 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); |
| 3782 | |
| 3783 | unlock: |
| 3784 | rcu_read_unlock(); |
| 3785 | } |
| 3786 | |
Peter Zijlstra | c720620 | 2012-03-22 17:26:36 +0100 | [diff] [blame] | 3787 | 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] | 3788 | { |
| 3789 | } |
| 3790 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3791 | /* |
| 3792 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 3793 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 3794 | * code calls this from NMI context. |
| 3795 | */ |
| 3796 | void perf_event_update_userpage(struct perf_event *event) |
| 3797 | { |
| 3798 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3799 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3800 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3801 | |
| 3802 | rcu_read_lock(); |
Peter Zijlstra | 5ec4c59 | 2013-08-02 21:16:30 +0200 | [diff] [blame] | 3803 | rb = rcu_dereference(event->rb); |
| 3804 | if (!rb) |
| 3805 | goto unlock; |
| 3806 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3807 | /* |
| 3808 | * compute total_time_enabled, total_time_running |
| 3809 | * based on snapshot values taken when the event |
| 3810 | * was last scheduled in. |
| 3811 | * |
| 3812 | * we cannot simply called update_context_time() |
| 3813 | * because of locking issue as we can be called in |
| 3814 | * NMI context |
| 3815 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3816 | calc_timer_values(event, &now, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3817 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3818 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3819 | /* |
| 3820 | * Disable preemption so as to not let the corresponding user-space |
| 3821 | * spin too long if we get preempted. |
| 3822 | */ |
| 3823 | preempt_disable(); |
| 3824 | ++userpg->lock; |
| 3825 | barrier(); |
| 3826 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3827 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 3828 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3829 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3830 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3831 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3832 | atomic64_read(&event->child_total_time_enabled); |
| 3833 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3834 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3835 | atomic64_read(&event->child_total_time_running); |
| 3836 | |
Peter Zijlstra | c720620 | 2012-03-22 17:26:36 +0100 | [diff] [blame] | 3837 | arch_perf_update_userpage(userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3838 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3839 | barrier(); |
| 3840 | ++userpg->lock; |
| 3841 | preempt_enable(); |
| 3842 | unlock: |
| 3843 | rcu_read_unlock(); |
| 3844 | } |
| 3845 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3846 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 3847 | { |
| 3848 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3849 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3850 | int ret = VM_FAULT_SIGBUS; |
| 3851 | |
| 3852 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 3853 | if (vmf->pgoff == 0) |
| 3854 | ret = 0; |
| 3855 | return ret; |
| 3856 | } |
| 3857 | |
| 3858 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3859 | rb = rcu_dereference(event->rb); |
| 3860 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3861 | goto unlock; |
| 3862 | |
| 3863 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 3864 | goto unlock; |
| 3865 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3866 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3867 | if (!vmf->page) |
| 3868 | goto unlock; |
| 3869 | |
| 3870 | get_page(vmf->page); |
| 3871 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 3872 | vmf->page->index = vmf->pgoff; |
| 3873 | |
| 3874 | ret = 0; |
| 3875 | unlock: |
| 3876 | rcu_read_unlock(); |
| 3877 | |
| 3878 | return ret; |
| 3879 | } |
| 3880 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3881 | static void ring_buffer_attach(struct perf_event *event, |
| 3882 | struct ring_buffer *rb) |
| 3883 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3884 | struct ring_buffer *old_rb = NULL; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3885 | unsigned long flags; |
| 3886 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3887 | if (event->rb) { |
| 3888 | /* |
| 3889 | * Should be impossible, we set this when removing |
| 3890 | * event->rb_entry and wait/clear when adding event->rb_entry. |
| 3891 | */ |
| 3892 | WARN_ON_ONCE(event->rcu_pending); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3893 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3894 | old_rb = event->rb; |
| 3895 | event->rcu_batches = get_state_synchronize_rcu(); |
| 3896 | event->rcu_pending = 1; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3897 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3898 | spin_lock_irqsave(&old_rb->event_lock, flags); |
| 3899 | list_del_rcu(&event->rb_entry); |
| 3900 | spin_unlock_irqrestore(&old_rb->event_lock, flags); |
| 3901 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3902 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3903 | if (event->rcu_pending && rb) { |
| 3904 | cond_synchronize_rcu(event->rcu_batches); |
| 3905 | event->rcu_pending = 0; |
| 3906 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3907 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3908 | if (rb) { |
| 3909 | spin_lock_irqsave(&rb->event_lock, flags); |
| 3910 | list_add_rcu(&event->rb_entry, &rb->event_list); |
| 3911 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 3912 | } |
| 3913 | |
| 3914 | rcu_assign_pointer(event->rb, rb); |
| 3915 | |
| 3916 | if (old_rb) { |
| 3917 | ring_buffer_put(old_rb); |
| 3918 | /* |
| 3919 | * Since we detached before setting the new rb, so that we |
| 3920 | * could attach the new rb, we could have missed a wakeup. |
| 3921 | * Provide it now. |
| 3922 | */ |
| 3923 | wake_up_all(&event->waitq); |
| 3924 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | static void ring_buffer_wakeup(struct perf_event *event) |
| 3928 | { |
| 3929 | struct ring_buffer *rb; |
| 3930 | |
| 3931 | rcu_read_lock(); |
| 3932 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3933 | if (rb) { |
| 3934 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 3935 | wake_up_all(&event->waitq); |
| 3936 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3937 | rcu_read_unlock(); |
| 3938 | } |
| 3939 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3940 | static void rb_free_rcu(struct rcu_head *rcu_head) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3941 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3942 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 3943 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3944 | rb = container_of(rcu_head, struct ring_buffer, rcu_head); |
| 3945 | rb_free(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3946 | } |
| 3947 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3948 | static struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3949 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3950 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3951 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3952 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3953 | rb = rcu_dereference(event->rb); |
| 3954 | if (rb) { |
| 3955 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 3956 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3957 | } |
| 3958 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3959 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3960 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3961 | } |
| 3962 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3963 | static void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3964 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3965 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3966 | return; |
| 3967 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3968 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3969 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3970 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 3974 | { |
| 3975 | struct perf_event *event = vma->vm_file->private_data; |
| 3976 | |
| 3977 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3978 | atomic_inc(&event->rb->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3979 | } |
| 3980 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3981 | /* |
| 3982 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 3983 | * event, or through other events by use of perf_event_set_output(). |
| 3984 | * |
| 3985 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 3986 | * the buffer here, where we still have a VM context. This means we need |
| 3987 | * to detach all events redirecting to us. |
| 3988 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3989 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 3990 | { |
| 3991 | struct perf_event *event = vma->vm_file->private_data; |
| 3992 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3993 | struct ring_buffer *rb = ring_buffer_get(event); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3994 | struct user_struct *mmap_user = rb->mmap_user; |
| 3995 | int mmap_locked = rb->mmap_locked; |
| 3996 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3997 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3998 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3999 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4000 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4001 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4002 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4003 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4004 | mutex_unlock(&event->mmap_mutex); |
| 4005 | |
| 4006 | /* 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] | 4007 | if (atomic_read(&rb->mmap_count)) |
| 4008 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4009 | |
| 4010 | /* |
| 4011 | * No other mmap()s, detach from all other events that might redirect |
| 4012 | * into the now unreachable buffer. Somewhat complicated by the |
| 4013 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 4014 | */ |
| 4015 | again: |
| 4016 | rcu_read_lock(); |
| 4017 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 4018 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 4019 | /* |
| 4020 | * This event is en-route to free_event() which will |
| 4021 | * detach it and remove it from the list. |
| 4022 | */ |
| 4023 | continue; |
| 4024 | } |
| 4025 | rcu_read_unlock(); |
| 4026 | |
| 4027 | mutex_lock(&event->mmap_mutex); |
| 4028 | /* |
| 4029 | * Check we didn't race with perf_event_set_output() which can |
| 4030 | * swizzle the rb from under us while we were waiting to |
| 4031 | * acquire mmap_mutex. |
| 4032 | * |
| 4033 | * If we find a different rb; ignore this event, a next |
| 4034 | * iteration will no longer find it on the list. We have to |
| 4035 | * still restart the iteration to make sure we're not now |
| 4036 | * iterating the wrong list. |
| 4037 | */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4038 | if (event->rb == rb) |
| 4039 | ring_buffer_attach(event, NULL); |
| 4040 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4041 | mutex_unlock(&event->mmap_mutex); |
| 4042 | put_event(event); |
| 4043 | |
| 4044 | /* |
| 4045 | * Restart the iteration; either we're on the wrong list or |
| 4046 | * destroyed its integrity by doing a deletion. |
| 4047 | */ |
| 4048 | goto again; |
| 4049 | } |
| 4050 | rcu_read_unlock(); |
| 4051 | |
| 4052 | /* |
| 4053 | * It could be there's still a few 0-ref events on the list; they'll |
| 4054 | * get cleaned up by free_event() -- they'll also still have their |
| 4055 | * ref on the rb and will free it whenever they are done with it. |
| 4056 | * |
| 4057 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 4058 | * undo the VM accounting. |
| 4059 | */ |
| 4060 | |
| 4061 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 4062 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 4063 | free_uid(mmap_user); |
| 4064 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4065 | out_put: |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4066 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4067 | } |
| 4068 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 4069 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4070 | .open = perf_mmap_open, |
| 4071 | .close = perf_mmap_close, |
| 4072 | .fault = perf_mmap_fault, |
| 4073 | .page_mkwrite = perf_mmap_fault, |
| 4074 | }; |
| 4075 | |
| 4076 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 4077 | { |
| 4078 | struct perf_event *event = file->private_data; |
| 4079 | unsigned long user_locked, user_lock_limit; |
| 4080 | struct user_struct *user = current_user(); |
| 4081 | unsigned long locked, lock_limit; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4082 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4083 | unsigned long vma_size; |
| 4084 | unsigned long nr_pages; |
| 4085 | long user_extra, extra; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4086 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4087 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4088 | /* |
| 4089 | * Don't allow mmap() of inherited per-task counters. This would |
| 4090 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4091 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4092 | */ |
| 4093 | if (event->cpu == -1 && event->attr.inherit) |
| 4094 | return -EINVAL; |
| 4095 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4096 | if (!(vma->vm_flags & VM_SHARED)) |
| 4097 | return -EINVAL; |
| 4098 | |
| 4099 | vma_size = vma->vm_end - vma->vm_start; |
| 4100 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 4101 | |
| 4102 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4103 | * 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] | 4104 | * can do bitmasks instead of modulo. |
| 4105 | */ |
| 4106 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
| 4107 | return -EINVAL; |
| 4108 | |
| 4109 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 4110 | return -EINVAL; |
| 4111 | |
| 4112 | if (vma->vm_pgoff != 0) |
| 4113 | return -EINVAL; |
| 4114 | |
| 4115 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4116 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4117 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4118 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4119 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4120 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4121 | goto unlock; |
| 4122 | } |
| 4123 | |
| 4124 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 4125 | /* |
| 4126 | * Raced against perf_mmap_close() through |
| 4127 | * perf_event_set_output(). Try again, hope for better |
| 4128 | * luck. |
| 4129 | */ |
| 4130 | mutex_unlock(&event->mmap_mutex); |
| 4131 | goto again; |
| 4132 | } |
| 4133 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4134 | goto unlock; |
| 4135 | } |
| 4136 | |
| 4137 | user_extra = nr_pages + 1; |
| 4138 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 4139 | |
| 4140 | /* |
| 4141 | * Increase the limit linearly with more CPUs: |
| 4142 | */ |
| 4143 | user_lock_limit *= num_online_cpus(); |
| 4144 | |
| 4145 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 4146 | |
| 4147 | extra = 0; |
| 4148 | if (user_locked > user_lock_limit) |
| 4149 | extra = user_locked - user_lock_limit; |
| 4150 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 4151 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4152 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 4153 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4154 | |
| 4155 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 4156 | !capable(CAP_IPC_LOCK)) { |
| 4157 | ret = -EPERM; |
| 4158 | goto unlock; |
| 4159 | } |
| 4160 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4161 | WARN_ON(event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4162 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4163 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4164 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4165 | |
Vince Weaver | 4ec8363 | 2011-06-01 15:15:36 -0400 | [diff] [blame] | 4166 | rb = rb_alloc(nr_pages, |
| 4167 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 4168 | event->cpu, flags); |
| 4169 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4170 | if (!rb) { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4171 | ret = -ENOMEM; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4172 | goto unlock; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4173 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4174 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4175 | atomic_set(&rb->mmap_count, 1); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4176 | rb->mmap_locked = extra; |
| 4177 | rb->mmap_user = get_current_user(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4178 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4179 | atomic_long_add(user_extra, &user->locked_vm); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4180 | vma->vm_mm->pinned_vm += extra; |
| 4181 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4182 | ring_buffer_attach(event, rb); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4183 | |
Peter Zijlstra | fa73158 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4184 | perf_event_init_userpage(event); |
Peter Zijlstra | 9a0f05c | 2011-11-21 15:13:29 +0100 | [diff] [blame] | 4185 | perf_event_update_userpage(event); |
| 4186 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4187 | unlock: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4188 | if (!ret) |
| 4189 | atomic_inc(&event->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4190 | mutex_unlock(&event->mmap_mutex); |
| 4191 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4192 | /* |
| 4193 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 4194 | * vma. |
| 4195 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4196 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4197 | vma->vm_ops = &perf_mmap_vmops; |
| 4198 | |
| 4199 | return ret; |
| 4200 | } |
| 4201 | |
| 4202 | static int perf_fasync(int fd, struct file *filp, int on) |
| 4203 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4204 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4205 | struct perf_event *event = filp->private_data; |
| 4206 | int retval; |
| 4207 | |
| 4208 | mutex_lock(&inode->i_mutex); |
| 4209 | retval = fasync_helper(fd, filp, on, &event->fasync); |
| 4210 | mutex_unlock(&inode->i_mutex); |
| 4211 | |
| 4212 | if (retval < 0) |
| 4213 | return retval; |
| 4214 | |
| 4215 | return 0; |
| 4216 | } |
| 4217 | |
| 4218 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 4219 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4220 | .release = perf_release, |
| 4221 | .read = perf_read, |
| 4222 | .poll = perf_poll, |
| 4223 | .unlocked_ioctl = perf_ioctl, |
| 4224 | .compat_ioctl = perf_ioctl, |
| 4225 | .mmap = perf_mmap, |
| 4226 | .fasync = perf_fasync, |
| 4227 | }; |
| 4228 | |
| 4229 | /* |
| 4230 | * Perf event wakeup |
| 4231 | * |
| 4232 | * If there's data, ensure we set the poll() state and publish everything |
| 4233 | * to user-space before waking everybody up. |
| 4234 | */ |
| 4235 | |
| 4236 | void perf_event_wakeup(struct perf_event *event) |
| 4237 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4238 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4239 | |
| 4240 | if (event->pending_kill) { |
| 4241 | kill_fasync(&event->fasync, SIGIO, event->pending_kill); |
| 4242 | event->pending_kill = 0; |
| 4243 | } |
| 4244 | } |
| 4245 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 4246 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4247 | { |
| 4248 | struct perf_event *event = container_of(entry, |
| 4249 | struct perf_event, pending); |
| 4250 | |
| 4251 | if (event->pending_disable) { |
| 4252 | event->pending_disable = 0; |
| 4253 | __perf_event_disable(event); |
| 4254 | } |
| 4255 | |
| 4256 | if (event->pending_wakeup) { |
| 4257 | event->pending_wakeup = 0; |
| 4258 | perf_event_wakeup(event); |
| 4259 | } |
| 4260 | } |
| 4261 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4262 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 4263 | * We assume there is only KVM supporting the callbacks. |
| 4264 | * Later on, we might change it to a list if there is |
| 4265 | * another virtualization implementation supporting the callbacks. |
| 4266 | */ |
| 4267 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 4268 | |
| 4269 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4270 | { |
| 4271 | perf_guest_cbs = cbs; |
| 4272 | return 0; |
| 4273 | } |
| 4274 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 4275 | |
| 4276 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4277 | { |
| 4278 | perf_guest_cbs = NULL; |
| 4279 | return 0; |
| 4280 | } |
| 4281 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 4282 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4283 | static void |
| 4284 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 4285 | struct pt_regs *regs, u64 mask) |
| 4286 | { |
| 4287 | int bit; |
| 4288 | |
| 4289 | for_each_set_bit(bit, (const unsigned long *) &mask, |
| 4290 | sizeof(mask) * BITS_PER_BYTE) { |
| 4291 | u64 val; |
| 4292 | |
| 4293 | val = perf_reg_value(regs, bit); |
| 4294 | perf_output_put(handle, val); |
| 4295 | } |
| 4296 | } |
| 4297 | |
| 4298 | static void perf_sample_regs_user(struct perf_regs_user *regs_user, |
| 4299 | struct pt_regs *regs) |
| 4300 | { |
| 4301 | if (!user_mode(regs)) { |
| 4302 | if (current->mm) |
| 4303 | regs = task_pt_regs(current); |
| 4304 | else |
| 4305 | regs = NULL; |
| 4306 | } |
| 4307 | |
| 4308 | if (regs) { |
| 4309 | regs_user->regs = regs; |
| 4310 | regs_user->abi = perf_reg_abi(current); |
| 4311 | } |
| 4312 | } |
| 4313 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4314 | /* |
| 4315 | * Get remaining task size from user stack pointer. |
| 4316 | * |
| 4317 | * It'd be better to take stack vma map and limit this more |
| 4318 | * precisly, but there's no way to get it safely under interrupt, |
| 4319 | * so using TASK_SIZE as limit. |
| 4320 | */ |
| 4321 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 4322 | { |
| 4323 | unsigned long addr = perf_user_stack_pointer(regs); |
| 4324 | |
| 4325 | if (!addr || addr >= TASK_SIZE) |
| 4326 | return 0; |
| 4327 | |
| 4328 | return TASK_SIZE - addr; |
| 4329 | } |
| 4330 | |
| 4331 | static u16 |
| 4332 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 4333 | struct pt_regs *regs) |
| 4334 | { |
| 4335 | u64 task_size; |
| 4336 | |
| 4337 | /* No regs, no stack pointer, no dump. */ |
| 4338 | if (!regs) |
| 4339 | return 0; |
| 4340 | |
| 4341 | /* |
| 4342 | * Check if we fit in with the requested stack size into the: |
| 4343 | * - TASK_SIZE |
| 4344 | * If we don't, we limit the size to the TASK_SIZE. |
| 4345 | * |
| 4346 | * - remaining sample size |
| 4347 | * If we don't, we customize the stack size to |
| 4348 | * fit in to the remaining sample size. |
| 4349 | */ |
| 4350 | |
| 4351 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 4352 | stack_size = min(stack_size, (u16) task_size); |
| 4353 | |
| 4354 | /* Current header size plus static size and dynamic size. */ |
| 4355 | header_size += 2 * sizeof(u64); |
| 4356 | |
| 4357 | /* Do we fit in with the current stack dump size? */ |
| 4358 | if ((u16) (header_size + stack_size) < header_size) { |
| 4359 | /* |
| 4360 | * If we overflow the maximum size for the sample, |
| 4361 | * we customize the stack dump size to fit in. |
| 4362 | */ |
| 4363 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 4364 | stack_size = round_up(stack_size, sizeof(u64)); |
| 4365 | } |
| 4366 | |
| 4367 | return stack_size; |
| 4368 | } |
| 4369 | |
| 4370 | static void |
| 4371 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 4372 | struct pt_regs *regs) |
| 4373 | { |
| 4374 | /* Case of a kernel thread, nothing to dump */ |
| 4375 | if (!regs) { |
| 4376 | u64 size = 0; |
| 4377 | perf_output_put(handle, size); |
| 4378 | } else { |
| 4379 | unsigned long sp; |
| 4380 | unsigned int rem; |
| 4381 | u64 dyn_size; |
| 4382 | |
| 4383 | /* |
| 4384 | * We dump: |
| 4385 | * static size |
| 4386 | * - the size requested by user or the best one we can fit |
| 4387 | * in to the sample max size |
| 4388 | * data |
| 4389 | * - user stack dump data |
| 4390 | * dynamic size |
| 4391 | * - the actual dumped size |
| 4392 | */ |
| 4393 | |
| 4394 | /* Static size. */ |
| 4395 | perf_output_put(handle, dump_size); |
| 4396 | |
| 4397 | /* Data. */ |
| 4398 | sp = perf_user_stack_pointer(regs); |
| 4399 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 4400 | dyn_size = dump_size - rem; |
| 4401 | |
| 4402 | perf_output_skip(handle, rem); |
| 4403 | |
| 4404 | /* Dynamic size. */ |
| 4405 | perf_output_put(handle, dyn_size); |
| 4406 | } |
| 4407 | } |
| 4408 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4409 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 4410 | struct perf_sample_data *data, |
| 4411 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4412 | { |
| 4413 | u64 sample_type = event->attr.sample_type; |
| 4414 | |
| 4415 | data->type = sample_type; |
| 4416 | header->size += event->id_header_size; |
| 4417 | |
| 4418 | if (sample_type & PERF_SAMPLE_TID) { |
| 4419 | /* namespace issues */ |
| 4420 | data->tid_entry.pid = perf_event_pid(event, current); |
| 4421 | data->tid_entry.tid = perf_event_tid(event, current); |
| 4422 | } |
| 4423 | |
| 4424 | if (sample_type & PERF_SAMPLE_TIME) |
| 4425 | data->time = perf_clock(); |
| 4426 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4427 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4428 | data->id = primary_event_id(event); |
| 4429 | |
| 4430 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4431 | data->stream_id = event->id; |
| 4432 | |
| 4433 | if (sample_type & PERF_SAMPLE_CPU) { |
| 4434 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 4435 | data->cpu_entry.reserved = 0; |
| 4436 | } |
| 4437 | } |
| 4438 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4439 | void perf_event_header__init_id(struct perf_event_header *header, |
| 4440 | struct perf_sample_data *data, |
| 4441 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4442 | { |
| 4443 | if (event->attr.sample_id_all) |
| 4444 | __perf_event_header__init_id(header, data, event); |
| 4445 | } |
| 4446 | |
| 4447 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 4448 | struct perf_sample_data *data) |
| 4449 | { |
| 4450 | u64 sample_type = data->type; |
| 4451 | |
| 4452 | if (sample_type & PERF_SAMPLE_TID) |
| 4453 | perf_output_put(handle, data->tid_entry); |
| 4454 | |
| 4455 | if (sample_type & PERF_SAMPLE_TIME) |
| 4456 | perf_output_put(handle, data->time); |
| 4457 | |
| 4458 | if (sample_type & PERF_SAMPLE_ID) |
| 4459 | perf_output_put(handle, data->id); |
| 4460 | |
| 4461 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4462 | perf_output_put(handle, data->stream_id); |
| 4463 | |
| 4464 | if (sample_type & PERF_SAMPLE_CPU) |
| 4465 | perf_output_put(handle, data->cpu_entry); |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4466 | |
| 4467 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4468 | perf_output_put(handle, data->id); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4469 | } |
| 4470 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4471 | void perf_event__output_id_sample(struct perf_event *event, |
| 4472 | struct perf_output_handle *handle, |
| 4473 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4474 | { |
| 4475 | if (event->attr.sample_id_all) |
| 4476 | __perf_event__output_id_sample(handle, sample); |
| 4477 | } |
| 4478 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4479 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4480 | struct perf_event *event, |
| 4481 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4482 | { |
| 4483 | u64 read_format = event->attr.read_format; |
| 4484 | u64 values[4]; |
| 4485 | int n = 0; |
| 4486 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4487 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4488 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4489 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4490 | atomic64_read(&event->child_total_time_enabled); |
| 4491 | } |
| 4492 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4493 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4494 | atomic64_read(&event->child_total_time_running); |
| 4495 | } |
| 4496 | if (read_format & PERF_FORMAT_ID) |
| 4497 | values[n++] = primary_event_id(event); |
| 4498 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4499 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | /* |
| 4503 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 4504 | */ |
| 4505 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4506 | struct perf_event *event, |
| 4507 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4508 | { |
| 4509 | struct perf_event *leader = event->group_leader, *sub; |
| 4510 | u64 read_format = event->attr.read_format; |
| 4511 | u64 values[5]; |
| 4512 | int n = 0; |
| 4513 | |
| 4514 | values[n++] = 1 + leader->nr_siblings; |
| 4515 | |
| 4516 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4517 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4518 | |
| 4519 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4520 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4521 | |
| 4522 | if (leader != event) |
| 4523 | leader->pmu->read(leader); |
| 4524 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4525 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4526 | if (read_format & PERF_FORMAT_ID) |
| 4527 | values[n++] = primary_event_id(leader); |
| 4528 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4529 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4530 | |
| 4531 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 4532 | n = 0; |
| 4533 | |
Jiri Olsa | 6f5ab00 | 2012-10-15 20:13:45 +0200 | [diff] [blame] | 4534 | if ((sub != event) && |
| 4535 | (sub->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4536 | sub->pmu->read(sub); |
| 4537 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4538 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4539 | if (read_format & PERF_FORMAT_ID) |
| 4540 | values[n++] = primary_event_id(sub); |
| 4541 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4542 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4543 | } |
| 4544 | } |
| 4545 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4546 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 4547 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4548 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4549 | static void perf_output_read(struct perf_output_handle *handle, |
| 4550 | struct perf_event *event) |
| 4551 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4552 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4553 | u64 read_format = event->attr.read_format; |
| 4554 | |
| 4555 | /* |
| 4556 | * compute total_time_enabled, total_time_running |
| 4557 | * based on snapshot values taken when the event |
| 4558 | * was last scheduled in. |
| 4559 | * |
| 4560 | * we cannot simply called update_context_time() |
| 4561 | * because of locking issue as we are called in |
| 4562 | * NMI context |
| 4563 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4564 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4565 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4566 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4567 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4568 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4569 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4570 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | void perf_output_sample(struct perf_output_handle *handle, |
| 4574 | struct perf_event_header *header, |
| 4575 | struct perf_sample_data *data, |
| 4576 | struct perf_event *event) |
| 4577 | { |
| 4578 | u64 sample_type = data->type; |
| 4579 | |
| 4580 | perf_output_put(handle, *header); |
| 4581 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4582 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4583 | perf_output_put(handle, data->id); |
| 4584 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4585 | if (sample_type & PERF_SAMPLE_IP) |
| 4586 | perf_output_put(handle, data->ip); |
| 4587 | |
| 4588 | if (sample_type & PERF_SAMPLE_TID) |
| 4589 | perf_output_put(handle, data->tid_entry); |
| 4590 | |
| 4591 | if (sample_type & PERF_SAMPLE_TIME) |
| 4592 | perf_output_put(handle, data->time); |
| 4593 | |
| 4594 | if (sample_type & PERF_SAMPLE_ADDR) |
| 4595 | perf_output_put(handle, data->addr); |
| 4596 | |
| 4597 | if (sample_type & PERF_SAMPLE_ID) |
| 4598 | perf_output_put(handle, data->id); |
| 4599 | |
| 4600 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4601 | perf_output_put(handle, data->stream_id); |
| 4602 | |
| 4603 | if (sample_type & PERF_SAMPLE_CPU) |
| 4604 | perf_output_put(handle, data->cpu_entry); |
| 4605 | |
| 4606 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 4607 | perf_output_put(handle, data->period); |
| 4608 | |
| 4609 | if (sample_type & PERF_SAMPLE_READ) |
| 4610 | perf_output_read(handle, event); |
| 4611 | |
| 4612 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4613 | if (data->callchain) { |
| 4614 | int size = 1; |
| 4615 | |
| 4616 | if (data->callchain) |
| 4617 | size += data->callchain->nr; |
| 4618 | |
| 4619 | size *= sizeof(u64); |
| 4620 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4621 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4622 | } else { |
| 4623 | u64 nr = 0; |
| 4624 | perf_output_put(handle, nr); |
| 4625 | } |
| 4626 | } |
| 4627 | |
| 4628 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4629 | if (data->raw) { |
| 4630 | perf_output_put(handle, data->raw->size); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4631 | __output_copy(handle, data->raw->data, |
| 4632 | data->raw->size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4633 | } else { |
| 4634 | struct { |
| 4635 | u32 size; |
| 4636 | u32 data; |
| 4637 | } raw = { |
| 4638 | .size = sizeof(u32), |
| 4639 | .data = 0, |
| 4640 | }; |
| 4641 | perf_output_put(handle, raw); |
| 4642 | } |
| 4643 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4644 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4645 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4646 | if (data->br_stack) { |
| 4647 | size_t size; |
| 4648 | |
| 4649 | size = data->br_stack->nr |
| 4650 | * sizeof(struct perf_branch_entry); |
| 4651 | |
| 4652 | perf_output_put(handle, data->br_stack->nr); |
| 4653 | perf_output_copy(handle, data->br_stack->entries, size); |
| 4654 | } else { |
| 4655 | /* |
| 4656 | * we always store at least the value of nr |
| 4657 | */ |
| 4658 | u64 nr = 0; |
| 4659 | perf_output_put(handle, nr); |
| 4660 | } |
| 4661 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4662 | |
| 4663 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 4664 | u64 abi = data->regs_user.abi; |
| 4665 | |
| 4666 | /* |
| 4667 | * If there are no regs to dump, notice it through |
| 4668 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 4669 | */ |
| 4670 | perf_output_put(handle, abi); |
| 4671 | |
| 4672 | if (abi) { |
| 4673 | u64 mask = event->attr.sample_regs_user; |
| 4674 | perf_output_sample_regs(handle, |
| 4675 | data->regs_user.regs, |
| 4676 | mask); |
| 4677 | } |
| 4678 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4679 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4680 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4681 | perf_output_sample_ustack(handle, |
| 4682 | data->stack_user_size, |
| 4683 | data->regs_user.regs); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4684 | } |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 4685 | |
| 4686 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 4687 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 4688 | |
| 4689 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 4690 | perf_output_put(handle, data->data_src.val); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4691 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 4692 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 4693 | perf_output_put(handle, data->txn); |
| 4694 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4695 | if (!event->attr.watermark) { |
| 4696 | int wakeup_events = event->attr.wakeup_events; |
| 4697 | |
| 4698 | if (wakeup_events) { |
| 4699 | struct ring_buffer *rb = handle->rb; |
| 4700 | int events = local_inc_return(&rb->events); |
| 4701 | |
| 4702 | if (events >= wakeup_events) { |
| 4703 | local_sub(wakeup_events, &rb->events); |
| 4704 | local_inc(&rb->wakeup); |
| 4705 | } |
| 4706 | } |
| 4707 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | void perf_prepare_sample(struct perf_event_header *header, |
| 4711 | struct perf_sample_data *data, |
| 4712 | struct perf_event *event, |
| 4713 | struct pt_regs *regs) |
| 4714 | { |
| 4715 | u64 sample_type = event->attr.sample_type; |
| 4716 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4717 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4718 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4719 | |
| 4720 | header->misc = 0; |
| 4721 | header->misc |= perf_misc_flags(regs); |
| 4722 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4723 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4724 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4725 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4726 | data->ip = perf_instruction_pointer(regs); |
| 4727 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4728 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4729 | int size = 1; |
| 4730 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 4731 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4732 | |
| 4733 | if (data->callchain) |
| 4734 | size += data->callchain->nr; |
| 4735 | |
| 4736 | header->size += size * sizeof(u64); |
| 4737 | } |
| 4738 | |
| 4739 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4740 | int size = sizeof(u32); |
| 4741 | |
| 4742 | if (data->raw) |
| 4743 | size += data->raw->size; |
| 4744 | else |
| 4745 | size += sizeof(u32); |
| 4746 | |
| 4747 | WARN_ON_ONCE(size & (sizeof(u64)-1)); |
| 4748 | header->size += size; |
| 4749 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4750 | |
| 4751 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4752 | int size = sizeof(u64); /* nr */ |
| 4753 | if (data->br_stack) { |
| 4754 | size += data->br_stack->nr |
| 4755 | * sizeof(struct perf_branch_entry); |
| 4756 | } |
| 4757 | header->size += size; |
| 4758 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4759 | |
| 4760 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 4761 | /* regs dump ABI info */ |
| 4762 | int size = sizeof(u64); |
| 4763 | |
| 4764 | perf_sample_regs_user(&data->regs_user, regs); |
| 4765 | |
| 4766 | if (data->regs_user.regs) { |
| 4767 | u64 mask = event->attr.sample_regs_user; |
| 4768 | size += hweight64(mask) * sizeof(u64); |
| 4769 | } |
| 4770 | |
| 4771 | header->size += size; |
| 4772 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4773 | |
| 4774 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 4775 | /* |
| 4776 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 4777 | * processed as the last one or have additional check added |
| 4778 | * in case new sample type is added, because we could eat |
| 4779 | * up the rest of the sample size. |
| 4780 | */ |
| 4781 | struct perf_regs_user *uregs = &data->regs_user; |
| 4782 | u16 stack_size = event->attr.sample_stack_user; |
| 4783 | u16 size = sizeof(u64); |
| 4784 | |
| 4785 | if (!uregs->abi) |
| 4786 | perf_sample_regs_user(uregs, regs); |
| 4787 | |
| 4788 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
| 4789 | uregs->regs); |
| 4790 | |
| 4791 | /* |
| 4792 | * If there is something to dump, add space for the dump |
| 4793 | * itself and for the field that tells the dynamic size, |
| 4794 | * which is how many have been actually dumped. |
| 4795 | */ |
| 4796 | if (stack_size) |
| 4797 | size += sizeof(u64) + stack_size; |
| 4798 | |
| 4799 | data->stack_user_size = stack_size; |
| 4800 | header->size += size; |
| 4801 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4802 | } |
| 4803 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 4804 | static void perf_event_output(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4805 | struct perf_sample_data *data, |
| 4806 | struct pt_regs *regs) |
| 4807 | { |
| 4808 | struct perf_output_handle handle; |
| 4809 | struct perf_event_header header; |
| 4810 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4811 | /* protect the callchain buffers */ |
| 4812 | rcu_read_lock(); |
| 4813 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4814 | perf_prepare_sample(&header, data, event, regs); |
| 4815 | |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4816 | if (perf_output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4817 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4818 | |
| 4819 | perf_output_sample(&handle, &header, data, event); |
| 4820 | |
| 4821 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4822 | |
| 4823 | exit: |
| 4824 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | /* |
| 4828 | * read event_id |
| 4829 | */ |
| 4830 | |
| 4831 | struct perf_read_event { |
| 4832 | struct perf_event_header header; |
| 4833 | |
| 4834 | u32 pid; |
| 4835 | u32 tid; |
| 4836 | }; |
| 4837 | |
| 4838 | static void |
| 4839 | perf_event_read_event(struct perf_event *event, |
| 4840 | struct task_struct *task) |
| 4841 | { |
| 4842 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4843 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4844 | struct perf_read_event read_event = { |
| 4845 | .header = { |
| 4846 | .type = PERF_RECORD_READ, |
| 4847 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4848 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4849 | }, |
| 4850 | .pid = perf_event_pid(event, task), |
| 4851 | .tid = perf_event_tid(event, task), |
| 4852 | }; |
| 4853 | int ret; |
| 4854 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4855 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4856 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4857 | if (ret) |
| 4858 | return; |
| 4859 | |
| 4860 | perf_output_put(&handle, read_event); |
| 4861 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4862 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4863 | |
| 4864 | perf_output_end(&handle); |
| 4865 | } |
| 4866 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4867 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
| 4868 | |
| 4869 | static void |
| 4870 | perf_event_aux_ctx(struct perf_event_context *ctx, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4871 | perf_event_aux_output_cb output, |
| 4872 | void *data) |
| 4873 | { |
| 4874 | struct perf_event *event; |
| 4875 | |
| 4876 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 4877 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 4878 | continue; |
| 4879 | if (!event_filter_match(event)) |
| 4880 | continue; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4881 | output(event, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4882 | } |
| 4883 | } |
| 4884 | |
| 4885 | static void |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4886 | perf_event_aux(perf_event_aux_output_cb output, void *data, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4887 | struct perf_event_context *task_ctx) |
| 4888 | { |
| 4889 | struct perf_cpu_context *cpuctx; |
| 4890 | struct perf_event_context *ctx; |
| 4891 | struct pmu *pmu; |
| 4892 | int ctxn; |
| 4893 | |
| 4894 | rcu_read_lock(); |
| 4895 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 4896 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 4897 | if (cpuctx->unique_pmu != pmu) |
| 4898 | goto next; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4899 | perf_event_aux_ctx(&cpuctx->ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4900 | if (task_ctx) |
| 4901 | goto next; |
| 4902 | ctxn = pmu->task_ctx_nr; |
| 4903 | if (ctxn < 0) |
| 4904 | goto next; |
| 4905 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 4906 | if (ctx) |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4907 | perf_event_aux_ctx(ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4908 | next: |
| 4909 | put_cpu_ptr(pmu->pmu_cpu_context); |
| 4910 | } |
| 4911 | |
| 4912 | if (task_ctx) { |
| 4913 | preempt_disable(); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4914 | perf_event_aux_ctx(task_ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4915 | preempt_enable(); |
| 4916 | } |
| 4917 | rcu_read_unlock(); |
| 4918 | } |
| 4919 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4920 | /* |
| 4921 | * task tracking -- fork/exit |
| 4922 | * |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 4923 | * 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] | 4924 | */ |
| 4925 | |
| 4926 | struct perf_task_event { |
| 4927 | struct task_struct *task; |
| 4928 | struct perf_event_context *task_ctx; |
| 4929 | |
| 4930 | struct { |
| 4931 | struct perf_event_header header; |
| 4932 | |
| 4933 | u32 pid; |
| 4934 | u32 ppid; |
| 4935 | u32 tid; |
| 4936 | u32 ptid; |
| 4937 | u64 time; |
| 4938 | } event_id; |
| 4939 | }; |
| 4940 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4941 | static int perf_event_task_match(struct perf_event *event) |
| 4942 | { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 4943 | return event->attr.comm || event->attr.mmap || |
| 4944 | event->attr.mmap2 || event->attr.mmap_data || |
| 4945 | event->attr.task; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4946 | } |
| 4947 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4948 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4949 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4950 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 4951 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4952 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4953 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4954 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4955 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 4956 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 4957 | if (!perf_event_task_match(event)) |
| 4958 | return; |
| 4959 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4960 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4961 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4962 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4963 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 4964 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4965 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4966 | |
| 4967 | task_event->event_id.pid = perf_event_pid(event, task); |
| 4968 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 4969 | |
| 4970 | task_event->event_id.tid = perf_event_tid(event, task); |
| 4971 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 4972 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4973 | perf_output_put(&handle, task_event->event_id); |
| 4974 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4975 | perf_event__output_id_sample(event, &handle, &sample); |
| 4976 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4977 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4978 | out: |
| 4979 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4980 | } |
| 4981 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4982 | static void perf_event_task(struct task_struct *task, |
| 4983 | struct perf_event_context *task_ctx, |
| 4984 | int new) |
| 4985 | { |
| 4986 | struct perf_task_event task_event; |
| 4987 | |
| 4988 | if (!atomic_read(&nr_comm_events) && |
| 4989 | !atomic_read(&nr_mmap_events) && |
| 4990 | !atomic_read(&nr_task_events)) |
| 4991 | return; |
| 4992 | |
| 4993 | task_event = (struct perf_task_event){ |
| 4994 | .task = task, |
| 4995 | .task_ctx = task_ctx, |
| 4996 | .event_id = { |
| 4997 | .header = { |
| 4998 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 4999 | .misc = 0, |
| 5000 | .size = sizeof(task_event.event_id), |
| 5001 | }, |
| 5002 | /* .pid */ |
| 5003 | /* .ppid */ |
| 5004 | /* .tid */ |
| 5005 | /* .ptid */ |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 5006 | .time = perf_clock(), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5007 | }, |
| 5008 | }; |
| 5009 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5010 | perf_event_aux(perf_event_task_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5011 | &task_event, |
| 5012 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | void perf_event_fork(struct task_struct *task) |
| 5016 | { |
| 5017 | perf_event_task(task, NULL, 1); |
| 5018 | } |
| 5019 | |
| 5020 | /* |
| 5021 | * comm tracking |
| 5022 | */ |
| 5023 | |
| 5024 | struct perf_comm_event { |
| 5025 | struct task_struct *task; |
| 5026 | char *comm; |
| 5027 | int comm_size; |
| 5028 | |
| 5029 | struct { |
| 5030 | struct perf_event_header header; |
| 5031 | |
| 5032 | u32 pid; |
| 5033 | u32 tid; |
| 5034 | } event_id; |
| 5035 | }; |
| 5036 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5037 | static int perf_event_comm_match(struct perf_event *event) |
| 5038 | { |
| 5039 | return event->attr.comm; |
| 5040 | } |
| 5041 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5042 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5043 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5044 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5045 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5046 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5047 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5048 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5049 | int ret; |
| 5050 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5051 | if (!perf_event_comm_match(event)) |
| 5052 | return; |
| 5053 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5054 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 5055 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5056 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5057 | |
| 5058 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5059 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5060 | |
| 5061 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 5062 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 5063 | |
| 5064 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5065 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5066 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5067 | |
| 5068 | perf_event__output_id_sample(event, &handle, &sample); |
| 5069 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5070 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5071 | out: |
| 5072 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5073 | } |
| 5074 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5075 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 5076 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5077 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5078 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5079 | |
| 5080 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 5081 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5082 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 5083 | |
| 5084 | comm_event->comm = comm; |
| 5085 | comm_event->comm_size = size; |
| 5086 | |
| 5087 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5088 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5089 | perf_event_aux(perf_event_comm_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5090 | comm_event, |
| 5091 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5092 | } |
| 5093 | |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5094 | void perf_event_comm(struct task_struct *task, bool exec) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5095 | { |
| 5096 | struct perf_comm_event comm_event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5097 | |
| 5098 | if (!atomic_read(&nr_comm_events)) |
| 5099 | return; |
| 5100 | |
| 5101 | comm_event = (struct perf_comm_event){ |
| 5102 | .task = task, |
| 5103 | /* .comm */ |
| 5104 | /* .comm_size */ |
| 5105 | .event_id = { |
| 5106 | .header = { |
| 5107 | .type = PERF_RECORD_COMM, |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5108 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5109 | /* .size */ |
| 5110 | }, |
| 5111 | /* .pid */ |
| 5112 | /* .tid */ |
| 5113 | }, |
| 5114 | }; |
| 5115 | |
| 5116 | perf_event_comm_event(&comm_event); |
| 5117 | } |
| 5118 | |
| 5119 | /* |
| 5120 | * mmap tracking |
| 5121 | */ |
| 5122 | |
| 5123 | struct perf_mmap_event { |
| 5124 | struct vm_area_struct *vma; |
| 5125 | |
| 5126 | const char *file_name; |
| 5127 | int file_size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5128 | int maj, min; |
| 5129 | u64 ino; |
| 5130 | u64 ino_generation; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame^] | 5131 | u32 prot, flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5132 | |
| 5133 | struct { |
| 5134 | struct perf_event_header header; |
| 5135 | |
| 5136 | u32 pid; |
| 5137 | u32 tid; |
| 5138 | u64 start; |
| 5139 | u64 len; |
| 5140 | u64 pgoff; |
| 5141 | } event_id; |
| 5142 | }; |
| 5143 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5144 | static int perf_event_mmap_match(struct perf_event *event, |
| 5145 | void *data) |
| 5146 | { |
| 5147 | struct perf_mmap_event *mmap_event = data; |
| 5148 | struct vm_area_struct *vma = mmap_event->vma; |
| 5149 | int executable = vma->vm_flags & VM_EXEC; |
| 5150 | |
| 5151 | return (!executable && event->attr.mmap_data) || |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5152 | (executable && (event->attr.mmap || event->attr.mmap2)); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5153 | } |
| 5154 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5155 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5156 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5157 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5158 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5159 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5160 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5161 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5162 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5163 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5164 | if (!perf_event_mmap_match(event, data)) |
| 5165 | return; |
| 5166 | |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5167 | if (event->attr.mmap2) { |
| 5168 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; |
| 5169 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); |
| 5170 | mmap_event->event_id.header.size += sizeof(mmap_event->min); |
| 5171 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); |
Arnaldo Carvalho de Melo | d008d52 | 2013-09-10 10:24:05 -0300 | [diff] [blame] | 5172 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame^] | 5173 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
| 5174 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5175 | } |
| 5176 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5177 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 5178 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5179 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5180 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5181 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5182 | |
| 5183 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 5184 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 5185 | |
| 5186 | perf_output_put(&handle, mmap_event->event_id); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5187 | |
| 5188 | if (event->attr.mmap2) { |
| 5189 | perf_output_put(&handle, mmap_event->maj); |
| 5190 | perf_output_put(&handle, mmap_event->min); |
| 5191 | perf_output_put(&handle, mmap_event->ino); |
| 5192 | perf_output_put(&handle, mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame^] | 5193 | perf_output_put(&handle, mmap_event->prot); |
| 5194 | perf_output_put(&handle, mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5195 | } |
| 5196 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5197 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5198 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5199 | |
| 5200 | perf_event__output_id_sample(event, &handle, &sample); |
| 5201 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5202 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5203 | out: |
| 5204 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5205 | } |
| 5206 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5207 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 5208 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5209 | struct vm_area_struct *vma = mmap_event->vma; |
| 5210 | struct file *file = vma->vm_file; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5211 | int maj = 0, min = 0; |
| 5212 | u64 ino = 0, gen = 0; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame^] | 5213 | u32 prot = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5214 | unsigned int size; |
| 5215 | char tmp[16]; |
| 5216 | char *buf = NULL; |
Peter Zijlstra | 2c42cfb | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5217 | char *name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5218 | |
| 5219 | if (file) { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5220 | struct inode *inode; |
| 5221 | dev_t dev; |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5222 | |
Peter Zijlstra | 2c42cfb | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5223 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5224 | if (!buf) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5225 | name = "//enomem"; |
| 5226 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5227 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5228 | /* |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5229 | * d_path() works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5230 | * need to add enough zero bytes after the string to handle |
| 5231 | * the 64bit alignment we do later. |
| 5232 | */ |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5233 | name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5234 | if (IS_ERR(name)) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5235 | name = "//toolong"; |
| 5236 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5237 | } |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5238 | inode = file_inode(vma->vm_file); |
| 5239 | dev = inode->i_sb->s_dev; |
| 5240 | ino = inode->i_ino; |
| 5241 | gen = inode->i_generation; |
| 5242 | maj = MAJOR(dev); |
| 5243 | min = MINOR(dev); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame^] | 5244 | |
| 5245 | if (vma->vm_flags & VM_READ) |
| 5246 | prot |= PROT_READ; |
| 5247 | if (vma->vm_flags & VM_WRITE) |
| 5248 | prot |= PROT_WRITE; |
| 5249 | if (vma->vm_flags & VM_EXEC) |
| 5250 | prot |= PROT_EXEC; |
| 5251 | |
| 5252 | if (vma->vm_flags & VM_MAYSHARE) |
| 5253 | flags = MAP_SHARED; |
| 5254 | else |
| 5255 | flags = MAP_PRIVATE; |
| 5256 | |
| 5257 | if (vma->vm_flags & VM_DENYWRITE) |
| 5258 | flags |= MAP_DENYWRITE; |
| 5259 | if (vma->vm_flags & VM_MAYEXEC) |
| 5260 | flags |= MAP_EXECUTABLE; |
| 5261 | if (vma->vm_flags & VM_LOCKED) |
| 5262 | flags |= MAP_LOCKED; |
| 5263 | if (vma->vm_flags & VM_HUGETLB) |
| 5264 | flags |= MAP_HUGETLB; |
| 5265 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5266 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5267 | } else { |
Peter Zijlstra | 2c42cfb | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5268 | name = (char *)arch_vma_name(vma); |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5269 | if (name) |
| 5270 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5271 | |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5272 | if (vma->vm_start <= vma->vm_mm->start_brk && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5273 | vma->vm_end >= vma->vm_mm->brk) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5274 | name = "[heap]"; |
| 5275 | goto cpy_name; |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5276 | } |
| 5277 | if (vma->vm_start <= vma->vm_mm->start_stack && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5278 | vma->vm_end >= vma->vm_mm->start_stack) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5279 | name = "[stack]"; |
| 5280 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5281 | } |
| 5282 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5283 | name = "//anon"; |
| 5284 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5285 | } |
| 5286 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5287 | cpy_name: |
| 5288 | strlcpy(tmp, name, sizeof(tmp)); |
| 5289 | name = tmp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5290 | got_name: |
Peter Zijlstra | 2c42cfb | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5291 | /* |
| 5292 | * Since our buffer works in 8 byte units we need to align our string |
| 5293 | * size to a multiple of 8. However, we must guarantee the tail end is |
| 5294 | * zero'd out to avoid leaking random bits to userspace. |
| 5295 | */ |
| 5296 | size = strlen(name)+1; |
| 5297 | while (!IS_ALIGNED(size, sizeof(u64))) |
| 5298 | name[size++] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5299 | |
| 5300 | mmap_event->file_name = name; |
| 5301 | mmap_event->file_size = size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5302 | mmap_event->maj = maj; |
| 5303 | mmap_event->min = min; |
| 5304 | mmap_event->ino = ino; |
| 5305 | mmap_event->ino_generation = gen; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame^] | 5306 | mmap_event->prot = prot; |
| 5307 | mmap_event->flags = flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5308 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 5309 | if (!(vma->vm_flags & VM_EXEC)) |
| 5310 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 5311 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5312 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 5313 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5314 | perf_event_aux(perf_event_mmap_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5315 | mmap_event, |
| 5316 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5317 | |
| 5318 | kfree(buf); |
| 5319 | } |
| 5320 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 5321 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5322 | { |
| 5323 | struct perf_mmap_event mmap_event; |
| 5324 | |
| 5325 | if (!atomic_read(&nr_mmap_events)) |
| 5326 | return; |
| 5327 | |
| 5328 | mmap_event = (struct perf_mmap_event){ |
| 5329 | .vma = vma, |
| 5330 | /* .file_name */ |
| 5331 | /* .file_size */ |
| 5332 | .event_id = { |
| 5333 | .header = { |
| 5334 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5335 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5336 | /* .size */ |
| 5337 | }, |
| 5338 | /* .pid */ |
| 5339 | /* .tid */ |
| 5340 | .start = vma->vm_start, |
| 5341 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 5342 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5343 | }, |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5344 | /* .maj (attr_mmap2 only) */ |
| 5345 | /* .min (attr_mmap2 only) */ |
| 5346 | /* .ino (attr_mmap2 only) */ |
| 5347 | /* .ino_generation (attr_mmap2 only) */ |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame^] | 5348 | /* .prot (attr_mmap2 only) */ |
| 5349 | /* .flags (attr_mmap2 only) */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5350 | }; |
| 5351 | |
| 5352 | perf_event_mmap_event(&mmap_event); |
| 5353 | } |
| 5354 | |
| 5355 | /* |
| 5356 | * IRQ throttle logging |
| 5357 | */ |
| 5358 | |
| 5359 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 5360 | { |
| 5361 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5362 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5363 | int ret; |
| 5364 | |
| 5365 | struct { |
| 5366 | struct perf_event_header header; |
| 5367 | u64 time; |
| 5368 | u64 id; |
| 5369 | u64 stream_id; |
| 5370 | } throttle_event = { |
| 5371 | .header = { |
| 5372 | .type = PERF_RECORD_THROTTLE, |
| 5373 | .misc = 0, |
| 5374 | .size = sizeof(throttle_event), |
| 5375 | }, |
| 5376 | .time = perf_clock(), |
| 5377 | .id = primary_event_id(event), |
| 5378 | .stream_id = event->id, |
| 5379 | }; |
| 5380 | |
| 5381 | if (enable) |
| 5382 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 5383 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5384 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 5385 | |
| 5386 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5387 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5388 | if (ret) |
| 5389 | return; |
| 5390 | |
| 5391 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5392 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5393 | perf_output_end(&handle); |
| 5394 | } |
| 5395 | |
| 5396 | /* |
| 5397 | * Generic event overflow handling, sampling. |
| 5398 | */ |
| 5399 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5400 | static int __perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5401 | int throttle, struct perf_sample_data *data, |
| 5402 | struct pt_regs *regs) |
| 5403 | { |
| 5404 | int events = atomic_read(&event->event_limit); |
| 5405 | struct hw_perf_event *hwc = &event->hw; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5406 | u64 seq; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5407 | int ret = 0; |
| 5408 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 5409 | /* |
| 5410 | * Non-sampling counters might still use the PMI to fold short |
| 5411 | * hardware counters, ignore those. |
| 5412 | */ |
| 5413 | if (unlikely(!is_sampling_event(event))) |
| 5414 | return 0; |
| 5415 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5416 | seq = __this_cpu_read(perf_throttled_seq); |
| 5417 | if (seq != hwc->interrupts_seq) { |
| 5418 | hwc->interrupts_seq = seq; |
| 5419 | hwc->interrupts = 1; |
| 5420 | } else { |
| 5421 | hwc->interrupts++; |
| 5422 | if (unlikely(throttle |
| 5423 | && hwc->interrupts >= max_samples_per_tick)) { |
| 5424 | __this_cpu_inc(perf_throttled_count); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 5425 | hwc->interrupts = MAX_INTERRUPTS; |
| 5426 | perf_log_throttle(event, 0); |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 5427 | tick_nohz_full_kick(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5428 | ret = 1; |
| 5429 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5430 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5431 | |
| 5432 | if (event->attr.freq) { |
| 5433 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5434 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5435 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5436 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5437 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5438 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 5439 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5440 | } |
| 5441 | |
| 5442 | /* |
| 5443 | * XXX event_limit might not quite work as expected on inherited |
| 5444 | * events |
| 5445 | */ |
| 5446 | |
| 5447 | event->pending_kill = POLL_IN; |
| 5448 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 5449 | ret = 1; |
| 5450 | event->pending_kill = POLL_HUP; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5451 | event->pending_disable = 1; |
| 5452 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5453 | } |
| 5454 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5455 | if (event->overflow_handler) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5456 | event->overflow_handler(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5457 | else |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5458 | perf_event_output(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5459 | |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5460 | if (event->fasync && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5461 | event->pending_wakeup = 1; |
| 5462 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5463 | } |
| 5464 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5465 | return ret; |
| 5466 | } |
| 5467 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5468 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5469 | struct perf_sample_data *data, |
| 5470 | struct pt_regs *regs) |
| 5471 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5472 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | /* |
| 5476 | * Generic software event infrastructure |
| 5477 | */ |
| 5478 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5479 | struct swevent_htable { |
| 5480 | struct swevent_hlist *swevent_hlist; |
| 5481 | struct mutex hlist_mutex; |
| 5482 | int hlist_refcount; |
| 5483 | |
| 5484 | /* Recursion avoidance in each contexts */ |
| 5485 | int recursion[PERF_NR_CONTEXTS]; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5486 | |
| 5487 | /* Keeps track of cpu being initialized/exited */ |
| 5488 | bool online; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5489 | }; |
| 5490 | |
| 5491 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 5492 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5493 | /* |
| 5494 | * We directly increment event->count and keep a second value in |
| 5495 | * event->hw.period_left to count intervals. This period event |
| 5496 | * is kept in the range [-sample_period, 0] so that we can use the |
| 5497 | * sign as trigger. |
| 5498 | */ |
| 5499 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 5500 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5501 | { |
| 5502 | struct hw_perf_event *hwc = &event->hw; |
| 5503 | u64 period = hwc->last_period; |
| 5504 | u64 nr, offset; |
| 5505 | s64 old, val; |
| 5506 | |
| 5507 | hwc->last_period = hwc->sample_period; |
| 5508 | |
| 5509 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5510 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5511 | if (val < 0) |
| 5512 | return 0; |
| 5513 | |
| 5514 | nr = div64_u64(period + val, period); |
| 5515 | offset = nr * period; |
| 5516 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5517 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5518 | goto again; |
| 5519 | |
| 5520 | return nr; |
| 5521 | } |
| 5522 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5523 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5524 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5525 | struct pt_regs *regs) |
| 5526 | { |
| 5527 | struct hw_perf_event *hwc = &event->hw; |
| 5528 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5529 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5530 | if (!overflow) |
| 5531 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5532 | |
| 5533 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 5534 | return; |
| 5535 | |
| 5536 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5537 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5538 | data, regs)) { |
| 5539 | /* |
| 5540 | * We inhibit the overflow from happening when |
| 5541 | * hwc->interrupts == MAX_INTERRUPTS. |
| 5542 | */ |
| 5543 | break; |
| 5544 | } |
| 5545 | throttle = 1; |
| 5546 | } |
| 5547 | } |
| 5548 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5549 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5550 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5551 | struct pt_regs *regs) |
| 5552 | { |
| 5553 | struct hw_perf_event *hwc = &event->hw; |
| 5554 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5555 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5556 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5557 | if (!regs) |
| 5558 | return; |
| 5559 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5560 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5561 | return; |
| 5562 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 5563 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 5564 | data->period = nr; |
| 5565 | return perf_swevent_overflow(event, 1, data, regs); |
| 5566 | } else |
| 5567 | data->period = event->hw.last_period; |
| 5568 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5569 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5570 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5571 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5572 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5573 | return; |
| 5574 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5575 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5576 | } |
| 5577 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5578 | static int perf_exclude_event(struct perf_event *event, |
| 5579 | struct pt_regs *regs) |
| 5580 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5581 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 5582 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5583 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5584 | if (regs) { |
| 5585 | if (event->attr.exclude_user && user_mode(regs)) |
| 5586 | return 1; |
| 5587 | |
| 5588 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 5589 | return 1; |
| 5590 | } |
| 5591 | |
| 5592 | return 0; |
| 5593 | } |
| 5594 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5595 | static int perf_swevent_match(struct perf_event *event, |
| 5596 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5597 | u32 event_id, |
| 5598 | struct perf_sample_data *data, |
| 5599 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5600 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5601 | if (event->attr.type != type) |
| 5602 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5603 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5604 | if (event->attr.config != event_id) |
| 5605 | return 0; |
| 5606 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5607 | if (perf_exclude_event(event, regs)) |
| 5608 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5609 | |
| 5610 | return 1; |
| 5611 | } |
| 5612 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5613 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5614 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5615 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5616 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5617 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 5618 | } |
| 5619 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5620 | static inline struct hlist_head * |
| 5621 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5622 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5623 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5624 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5625 | return &hlist->heads[hash]; |
| 5626 | } |
| 5627 | |
| 5628 | /* For the read side: events when they trigger */ |
| 5629 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5630 | 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] | 5631 | { |
| 5632 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5633 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5634 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5635 | if (!hlist) |
| 5636 | return NULL; |
| 5637 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5638 | return __find_swevent_head(hlist, type, event_id); |
| 5639 | } |
| 5640 | |
| 5641 | /* For the event head insertion and removal in the hlist */ |
| 5642 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5643 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5644 | { |
| 5645 | struct swevent_hlist *hlist; |
| 5646 | u32 event_id = event->attr.config; |
| 5647 | u64 type = event->attr.type; |
| 5648 | |
| 5649 | /* |
| 5650 | * Event scheduling is always serialized against hlist allocation |
| 5651 | * and release. Which makes the protected version suitable here. |
| 5652 | * The context lock guarantees that. |
| 5653 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5654 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5655 | lockdep_is_held(&event->ctx->lock)); |
| 5656 | if (!hlist) |
| 5657 | return NULL; |
| 5658 | |
| 5659 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | 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] | 5663 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5664 | struct perf_sample_data *data, |
| 5665 | struct pt_regs *regs) |
| 5666 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5667 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5668 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5669 | struct hlist_head *head; |
| 5670 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5671 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5672 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5673 | if (!head) |
| 5674 | goto end; |
| 5675 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5676 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5677 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5678 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5679 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5680 | end: |
| 5681 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5682 | } |
| 5683 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5684 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5685 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5686 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5687 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5688 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5689 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 5690 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5691 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 5692 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5693 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5694 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5695 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5696 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5697 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5698 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5699 | 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] | 5700 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5701 | struct perf_sample_data data; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5702 | int rctx; |
| 5703 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5704 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5705 | rctx = perf_swevent_get_recursion_context(); |
| 5706 | if (rctx < 0) |
| 5707 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5708 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 5709 | perf_sample_data_init(&data, addr, 0); |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5710 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5711 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5712 | |
| 5713 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5714 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | static void perf_swevent_read(struct perf_event *event) |
| 5718 | { |
| 5719 | } |
| 5720 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5721 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5722 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5723 | struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5724 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5725 | struct hlist_head *head; |
| 5726 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5727 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5728 | hwc->last_period = hwc->sample_period; |
| 5729 | perf_swevent_set_period(event); |
| 5730 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5731 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5732 | hwc->state = !(flags & PERF_EF_START); |
| 5733 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5734 | head = find_swevent_head(swhash, event); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5735 | if (!head) { |
| 5736 | /* |
| 5737 | * We can race with cpu hotplug code. Do not |
| 5738 | * WARN if the cpu just got unplugged. |
| 5739 | */ |
| 5740 | WARN_ON_ONCE(swhash->online); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5741 | return -EINVAL; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5742 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5743 | |
| 5744 | hlist_add_head_rcu(&event->hlist_entry, head); |
| 5745 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5746 | return 0; |
| 5747 | } |
| 5748 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5749 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5750 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5751 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5752 | } |
| 5753 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5754 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5755 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5756 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5757 | } |
| 5758 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5759 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5760 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5761 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5762 | } |
| 5763 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5764 | /* Deref the hlist from the update side */ |
| 5765 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5766 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5767 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5768 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 5769 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5770 | } |
| 5771 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5772 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5773 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5774 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5775 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5776 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5777 | return; |
| 5778 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5779 | rcu_assign_pointer(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 5780 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5781 | } |
| 5782 | |
| 5783 | static void swevent_hlist_put_cpu(struct perf_event *event, int cpu) |
| 5784 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5785 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5786 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5787 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5788 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5789 | if (!--swhash->hlist_refcount) |
| 5790 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5791 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5792 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | static void swevent_hlist_put(struct perf_event *event) |
| 5796 | { |
| 5797 | int cpu; |
| 5798 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5799 | for_each_possible_cpu(cpu) |
| 5800 | swevent_hlist_put_cpu(event, cpu); |
| 5801 | } |
| 5802 | |
| 5803 | static int swevent_hlist_get_cpu(struct perf_event *event, int cpu) |
| 5804 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5805 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5806 | int err = 0; |
| 5807 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5808 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5809 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5810 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5811 | struct swevent_hlist *hlist; |
| 5812 | |
| 5813 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 5814 | if (!hlist) { |
| 5815 | err = -ENOMEM; |
| 5816 | goto exit; |
| 5817 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5818 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5819 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5820 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 5821 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5822 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5823 | |
| 5824 | return err; |
| 5825 | } |
| 5826 | |
| 5827 | static int swevent_hlist_get(struct perf_event *event) |
| 5828 | { |
| 5829 | int err; |
| 5830 | int cpu, failed_cpu; |
| 5831 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5832 | get_online_cpus(); |
| 5833 | for_each_possible_cpu(cpu) { |
| 5834 | err = swevent_hlist_get_cpu(event, cpu); |
| 5835 | if (err) { |
| 5836 | failed_cpu = cpu; |
| 5837 | goto fail; |
| 5838 | } |
| 5839 | } |
| 5840 | put_online_cpus(); |
| 5841 | |
| 5842 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 5843 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5844 | for_each_possible_cpu(cpu) { |
| 5845 | if (cpu == failed_cpu) |
| 5846 | break; |
| 5847 | swevent_hlist_put_cpu(event, cpu); |
| 5848 | } |
| 5849 | |
| 5850 | put_online_cpus(); |
| 5851 | return err; |
| 5852 | } |
| 5853 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 5854 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5855 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5856 | static void sw_perf_event_destroy(struct perf_event *event) |
| 5857 | { |
| 5858 | u64 event_id = event->attr.config; |
| 5859 | |
| 5860 | WARN_ON(event->parent); |
| 5861 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 5862 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5863 | swevent_hlist_put(event); |
| 5864 | } |
| 5865 | |
| 5866 | static int perf_swevent_init(struct perf_event *event) |
| 5867 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 5868 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5869 | |
| 5870 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 5871 | return -ENOENT; |
| 5872 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 5873 | /* |
| 5874 | * no branch sampling for software events |
| 5875 | */ |
| 5876 | if (has_branch_stack(event)) |
| 5877 | return -EOPNOTSUPP; |
| 5878 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5879 | switch (event_id) { |
| 5880 | case PERF_COUNT_SW_CPU_CLOCK: |
| 5881 | case PERF_COUNT_SW_TASK_CLOCK: |
| 5882 | return -ENOENT; |
| 5883 | |
| 5884 | default: |
| 5885 | break; |
| 5886 | } |
| 5887 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 5888 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5889 | return -ENOENT; |
| 5890 | |
| 5891 | if (!event->parent) { |
| 5892 | int err; |
| 5893 | |
| 5894 | err = swevent_hlist_get(event); |
| 5895 | if (err) |
| 5896 | return err; |
| 5897 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 5898 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5899 | event->destroy = sw_perf_event_destroy; |
| 5900 | } |
| 5901 | |
| 5902 | return 0; |
| 5903 | } |
| 5904 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 5905 | static int perf_swevent_event_idx(struct perf_event *event) |
| 5906 | { |
| 5907 | return 0; |
| 5908 | } |
| 5909 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5910 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 5911 | .task_ctx_nr = perf_sw_context, |
| 5912 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5913 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5914 | .add = perf_swevent_add, |
| 5915 | .del = perf_swevent_del, |
| 5916 | .start = perf_swevent_start, |
| 5917 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5918 | .read = perf_swevent_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 5919 | |
| 5920 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5921 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5922 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5923 | #ifdef CONFIG_EVENT_TRACING |
| 5924 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5925 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 5926 | struct perf_sample_data *data) |
| 5927 | { |
| 5928 | void *record = data->raw->data; |
| 5929 | |
| 5930 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 5931 | return 1; |
| 5932 | return 0; |
| 5933 | } |
| 5934 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5935 | static int perf_tp_event_match(struct perf_event *event, |
| 5936 | struct perf_sample_data *data, |
| 5937 | struct pt_regs *regs) |
| 5938 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 5939 | if (event->hw.state & PERF_HES_STOPPED) |
| 5940 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 5941 | /* |
| 5942 | * All tracepoints are from kernel-space. |
| 5943 | */ |
| 5944 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5945 | return 0; |
| 5946 | |
| 5947 | if (!perf_tp_filter_match(event, data)) |
| 5948 | return 0; |
| 5949 | |
| 5950 | return 1; |
| 5951 | } |
| 5952 | |
| 5953 | 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] | 5954 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 5955 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5956 | { |
| 5957 | struct perf_sample_data data; |
| 5958 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5959 | |
| 5960 | struct perf_raw_record raw = { |
| 5961 | .size = entry_size, |
| 5962 | .data = record, |
| 5963 | }; |
| 5964 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 5965 | perf_sample_data_init(&data, addr, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5966 | data.raw = &raw; |
| 5967 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5968 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5969 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5970 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5971 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 5972 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5973 | /* |
| 5974 | * If we got specified a target task, also iterate its context and |
| 5975 | * deliver this event there too. |
| 5976 | */ |
| 5977 | if (task && task != current) { |
| 5978 | struct perf_event_context *ctx; |
| 5979 | struct trace_entry *entry = record; |
| 5980 | |
| 5981 | rcu_read_lock(); |
| 5982 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 5983 | if (!ctx) |
| 5984 | goto unlock; |
| 5985 | |
| 5986 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 5987 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 5988 | continue; |
| 5989 | if (event->attr.config != entry->type) |
| 5990 | continue; |
| 5991 | if (perf_tp_event_match(event, &data, regs)) |
| 5992 | perf_swevent_event(event, count, &data, regs); |
| 5993 | } |
| 5994 | unlock: |
| 5995 | rcu_read_unlock(); |
| 5996 | } |
| 5997 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 5998 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5999 | } |
| 6000 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 6001 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6002 | static void tp_perf_event_destroy(struct perf_event *event) |
| 6003 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6004 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6005 | } |
| 6006 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6007 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6008 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6009 | int err; |
| 6010 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6011 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6012 | return -ENOENT; |
| 6013 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6014 | /* |
| 6015 | * no branch sampling for tracepoint events |
| 6016 | */ |
| 6017 | if (has_branch_stack(event)) |
| 6018 | return -EOPNOTSUPP; |
| 6019 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6020 | err = perf_trace_init(event); |
| 6021 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6022 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6023 | |
| 6024 | event->destroy = tp_perf_event_destroy; |
| 6025 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6026 | return 0; |
| 6027 | } |
| 6028 | |
| 6029 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6030 | .task_ctx_nr = perf_sw_context, |
| 6031 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6032 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6033 | .add = perf_trace_add, |
| 6034 | .del = perf_trace_del, |
| 6035 | .start = perf_swevent_start, |
| 6036 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6037 | .read = perf_swevent_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6038 | |
| 6039 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6040 | }; |
| 6041 | |
| 6042 | static inline void perf_tp_register(void) |
| 6043 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6044 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6045 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6046 | |
| 6047 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6048 | { |
| 6049 | char *filter_str; |
| 6050 | int ret; |
| 6051 | |
| 6052 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6053 | return -EINVAL; |
| 6054 | |
| 6055 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 6056 | if (IS_ERR(filter_str)) |
| 6057 | return PTR_ERR(filter_str); |
| 6058 | |
| 6059 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); |
| 6060 | |
| 6061 | kfree(filter_str); |
| 6062 | return ret; |
| 6063 | } |
| 6064 | |
| 6065 | static void perf_event_free_filter(struct perf_event *event) |
| 6066 | { |
| 6067 | ftrace_profile_free_filter(event); |
| 6068 | } |
| 6069 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6070 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6071 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6072 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6073 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6074 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6075 | |
| 6076 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6077 | { |
| 6078 | return -ENOENT; |
| 6079 | } |
| 6080 | |
| 6081 | static void perf_event_free_filter(struct perf_event *event) |
| 6082 | { |
| 6083 | } |
| 6084 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 6085 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6086 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6087 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6088 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6089 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6090 | struct perf_sample_data sample; |
| 6091 | struct pt_regs *regs = data; |
| 6092 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6093 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6094 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6095 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6096 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6097 | } |
| 6098 | #endif |
| 6099 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6100 | /* |
| 6101 | * hrtimer based swevent callback |
| 6102 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6103 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6104 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6105 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6106 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 6107 | struct perf_sample_data data; |
| 6108 | struct pt_regs *regs; |
| 6109 | struct perf_event *event; |
| 6110 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6111 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6112 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6113 | |
| 6114 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 6115 | return HRTIMER_NORESTART; |
| 6116 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6117 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6118 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6119 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6120 | regs = get_irq_regs(); |
| 6121 | |
| 6122 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 6123 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 6124 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6125 | ret = HRTIMER_NORESTART; |
| 6126 | } |
| 6127 | |
| 6128 | period = max_t(u64, 10000, event->hw.sample_period); |
| 6129 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 6130 | |
| 6131 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6132 | } |
| 6133 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6134 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6135 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6136 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6137 | s64 period; |
| 6138 | |
| 6139 | if (!is_sampling_event(event)) |
| 6140 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6141 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6142 | period = local64_read(&hwc->period_left); |
| 6143 | if (period) { |
| 6144 | if (period < 0) |
| 6145 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6146 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6147 | local64_set(&hwc->period_left, 0); |
| 6148 | } else { |
| 6149 | period = max_t(u64, 10000, hwc->sample_period); |
| 6150 | } |
| 6151 | __hrtimer_start_range_ns(&hwc->hrtimer, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6152 | ns_to_ktime(period), 0, |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 6153 | HRTIMER_MODE_REL_PINNED, 0); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6154 | } |
| 6155 | |
| 6156 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 6157 | { |
| 6158 | struct hw_perf_event *hwc = &event->hw; |
| 6159 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6160 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6161 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6162 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6163 | |
| 6164 | hrtimer_cancel(&hwc->hrtimer); |
| 6165 | } |
| 6166 | } |
| 6167 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6168 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 6169 | { |
| 6170 | struct hw_perf_event *hwc = &event->hw; |
| 6171 | |
| 6172 | if (!is_sampling_event(event)) |
| 6173 | return; |
| 6174 | |
| 6175 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 6176 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 6177 | |
| 6178 | /* |
| 6179 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 6180 | * mapping and avoid the whole period adjust feedback stuff. |
| 6181 | */ |
| 6182 | if (event->attr.freq) { |
| 6183 | long freq = event->attr.sample_freq; |
| 6184 | |
| 6185 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 6186 | hwc->sample_period = event->attr.sample_period; |
| 6187 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 6188 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6189 | event->attr.freq = 0; |
| 6190 | } |
| 6191 | } |
| 6192 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6193 | /* |
| 6194 | * Software event: cpu wall time clock |
| 6195 | */ |
| 6196 | |
| 6197 | static void cpu_clock_event_update(struct perf_event *event) |
| 6198 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6199 | s64 prev; |
| 6200 | u64 now; |
| 6201 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6202 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6203 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6204 | local64_add(now - prev, &event->count); |
| 6205 | } |
| 6206 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6207 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6208 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6209 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6210 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6211 | } |
| 6212 | |
| 6213 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 6214 | { |
| 6215 | perf_swevent_cancel_hrtimer(event); |
| 6216 | cpu_clock_event_update(event); |
| 6217 | } |
| 6218 | |
| 6219 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 6220 | { |
| 6221 | if (flags & PERF_EF_START) |
| 6222 | cpu_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6223 | |
| 6224 | return 0; |
| 6225 | } |
| 6226 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6227 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6228 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6229 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | static void cpu_clock_event_read(struct perf_event *event) |
| 6233 | { |
| 6234 | cpu_clock_event_update(event); |
| 6235 | } |
| 6236 | |
| 6237 | static int cpu_clock_event_init(struct perf_event *event) |
| 6238 | { |
| 6239 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6240 | return -ENOENT; |
| 6241 | |
| 6242 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 6243 | return -ENOENT; |
| 6244 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6245 | /* |
| 6246 | * no branch sampling for software events |
| 6247 | */ |
| 6248 | if (has_branch_stack(event)) |
| 6249 | return -EOPNOTSUPP; |
| 6250 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6251 | perf_swevent_init_hrtimer(event); |
| 6252 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6253 | return 0; |
| 6254 | } |
| 6255 | |
| 6256 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6257 | .task_ctx_nr = perf_sw_context, |
| 6258 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6259 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6260 | .add = cpu_clock_event_add, |
| 6261 | .del = cpu_clock_event_del, |
| 6262 | .start = cpu_clock_event_start, |
| 6263 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6264 | .read = cpu_clock_event_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6265 | |
| 6266 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6267 | }; |
| 6268 | |
| 6269 | /* |
| 6270 | * Software event: task time clock |
| 6271 | */ |
| 6272 | |
| 6273 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 6274 | { |
| 6275 | u64 prev; |
| 6276 | s64 delta; |
| 6277 | |
| 6278 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6279 | delta = now - prev; |
| 6280 | local64_add(delta, &event->count); |
| 6281 | } |
| 6282 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6283 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6284 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6285 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6286 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6287 | } |
| 6288 | |
| 6289 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 6290 | { |
| 6291 | perf_swevent_cancel_hrtimer(event); |
| 6292 | task_clock_event_update(event, event->ctx->time); |
| 6293 | } |
| 6294 | |
| 6295 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 6296 | { |
| 6297 | if (flags & PERF_EF_START) |
| 6298 | task_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6299 | |
| 6300 | return 0; |
| 6301 | } |
| 6302 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6303 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6304 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6305 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6306 | } |
| 6307 | |
| 6308 | static void task_clock_event_read(struct perf_event *event) |
| 6309 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 6310 | u64 now = perf_clock(); |
| 6311 | u64 delta = now - event->ctx->timestamp; |
| 6312 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6313 | |
| 6314 | task_clock_event_update(event, time); |
| 6315 | } |
| 6316 | |
| 6317 | static int task_clock_event_init(struct perf_event *event) |
| 6318 | { |
| 6319 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6320 | return -ENOENT; |
| 6321 | |
| 6322 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 6323 | return -ENOENT; |
| 6324 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6325 | /* |
| 6326 | * no branch sampling for software events |
| 6327 | */ |
| 6328 | if (has_branch_stack(event)) |
| 6329 | return -EOPNOTSUPP; |
| 6330 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6331 | perf_swevent_init_hrtimer(event); |
| 6332 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6333 | return 0; |
| 6334 | } |
| 6335 | |
| 6336 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6337 | .task_ctx_nr = perf_sw_context, |
| 6338 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6339 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6340 | .add = task_clock_event_add, |
| 6341 | .del = task_clock_event_del, |
| 6342 | .start = task_clock_event_start, |
| 6343 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6344 | .read = task_clock_event_read, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6345 | |
| 6346 | .event_idx = perf_swevent_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6347 | }; |
| 6348 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6349 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 6350 | { |
| 6351 | } |
| 6352 | |
| 6353 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 6354 | { |
| 6355 | return 0; |
| 6356 | } |
| 6357 | |
| 6358 | static void perf_pmu_start_txn(struct pmu *pmu) |
| 6359 | { |
| 6360 | perf_pmu_disable(pmu); |
| 6361 | } |
| 6362 | |
| 6363 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 6364 | { |
| 6365 | perf_pmu_enable(pmu); |
| 6366 | return 0; |
| 6367 | } |
| 6368 | |
| 6369 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 6370 | { |
| 6371 | perf_pmu_enable(pmu); |
| 6372 | } |
| 6373 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6374 | static int perf_event_idx_default(struct perf_event *event) |
| 6375 | { |
| 6376 | return event->hw.idx + 1; |
| 6377 | } |
| 6378 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6379 | /* |
| 6380 | * Ensures all contexts with the same task_ctx_nr have the same |
| 6381 | * pmu_cpu_context too. |
| 6382 | */ |
Mark Rutland | 9e31704 | 2014-02-10 17:44:18 +0000 | [diff] [blame] | 6383 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6384 | { |
| 6385 | struct pmu *pmu; |
| 6386 | |
| 6387 | if (ctxn < 0) |
| 6388 | return NULL; |
| 6389 | |
| 6390 | list_for_each_entry(pmu, &pmus, entry) { |
| 6391 | if (pmu->task_ctx_nr == ctxn) |
| 6392 | return pmu->pmu_cpu_context; |
| 6393 | } |
| 6394 | |
| 6395 | return NULL; |
| 6396 | } |
| 6397 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6398 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6399 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6400 | int cpu; |
| 6401 | |
| 6402 | for_each_possible_cpu(cpu) { |
| 6403 | struct perf_cpu_context *cpuctx; |
| 6404 | |
| 6405 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6406 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6407 | if (cpuctx->unique_pmu == old_pmu) |
| 6408 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6409 | } |
| 6410 | } |
| 6411 | |
| 6412 | static void free_pmu_context(struct pmu *pmu) |
| 6413 | { |
| 6414 | struct pmu *i; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6415 | |
| 6416 | mutex_lock(&pmus_lock); |
| 6417 | /* |
| 6418 | * Like a real lame refcount. |
| 6419 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6420 | list_for_each_entry(i, &pmus, entry) { |
| 6421 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 6422 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6423 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6424 | } |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6425 | } |
| 6426 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6427 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6428 | out: |
| 6429 | mutex_unlock(&pmus_lock); |
| 6430 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6431 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6432 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6433 | static ssize_t |
| 6434 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 6435 | { |
| 6436 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6437 | |
| 6438 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 6439 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6440 | static DEVICE_ATTR_RO(type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6441 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6442 | static ssize_t |
| 6443 | perf_event_mux_interval_ms_show(struct device *dev, |
| 6444 | struct device_attribute *attr, |
| 6445 | char *page) |
| 6446 | { |
| 6447 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6448 | |
| 6449 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 6450 | } |
| 6451 | |
| 6452 | static ssize_t |
| 6453 | perf_event_mux_interval_ms_store(struct device *dev, |
| 6454 | struct device_attribute *attr, |
| 6455 | const char *buf, size_t count) |
| 6456 | { |
| 6457 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6458 | int timer, cpu, ret; |
| 6459 | |
| 6460 | ret = kstrtoint(buf, 0, &timer); |
| 6461 | if (ret) |
| 6462 | return ret; |
| 6463 | |
| 6464 | if (timer < 1) |
| 6465 | return -EINVAL; |
| 6466 | |
| 6467 | /* same value, noting to do */ |
| 6468 | if (timer == pmu->hrtimer_interval_ms) |
| 6469 | return count; |
| 6470 | |
| 6471 | pmu->hrtimer_interval_ms = timer; |
| 6472 | |
| 6473 | /* update all cpuctx for this PMU */ |
| 6474 | for_each_possible_cpu(cpu) { |
| 6475 | struct perf_cpu_context *cpuctx; |
| 6476 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6477 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 6478 | |
| 6479 | if (hrtimer_active(&cpuctx->hrtimer)) |
| 6480 | hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval); |
| 6481 | } |
| 6482 | |
| 6483 | return count; |
| 6484 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6485 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6486 | |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6487 | static struct attribute *pmu_dev_attrs[] = { |
| 6488 | &dev_attr_type.attr, |
| 6489 | &dev_attr_perf_event_mux_interval_ms.attr, |
| 6490 | NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6491 | }; |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6492 | ATTRIBUTE_GROUPS(pmu_dev); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6493 | |
| 6494 | static int pmu_bus_running; |
| 6495 | static struct bus_type pmu_bus = { |
| 6496 | .name = "event_source", |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6497 | .dev_groups = pmu_dev_groups, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6498 | }; |
| 6499 | |
| 6500 | static void pmu_dev_release(struct device *dev) |
| 6501 | { |
| 6502 | kfree(dev); |
| 6503 | } |
| 6504 | |
| 6505 | static int pmu_dev_alloc(struct pmu *pmu) |
| 6506 | { |
| 6507 | int ret = -ENOMEM; |
| 6508 | |
| 6509 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 6510 | if (!pmu->dev) |
| 6511 | goto out; |
| 6512 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 6513 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6514 | device_initialize(pmu->dev); |
| 6515 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 6516 | if (ret) |
| 6517 | goto free_dev; |
| 6518 | |
| 6519 | dev_set_drvdata(pmu->dev, pmu); |
| 6520 | pmu->dev->bus = &pmu_bus; |
| 6521 | pmu->dev->release = pmu_dev_release; |
| 6522 | ret = device_add(pmu->dev); |
| 6523 | if (ret) |
| 6524 | goto free_dev; |
| 6525 | |
| 6526 | out: |
| 6527 | return ret; |
| 6528 | |
| 6529 | free_dev: |
| 6530 | put_device(pmu->dev); |
| 6531 | goto out; |
| 6532 | } |
| 6533 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6534 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6535 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6536 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 6537 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6538 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6539 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6540 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6541 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6542 | ret = -ENOMEM; |
| 6543 | pmu->pmu_disable_count = alloc_percpu(int); |
| 6544 | if (!pmu->pmu_disable_count) |
| 6545 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6546 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6547 | pmu->type = -1; |
| 6548 | if (!name) |
| 6549 | goto skip_type; |
| 6550 | pmu->name = name; |
| 6551 | |
| 6552 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 6553 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 6554 | if (type < 0) { |
| 6555 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6556 | goto free_pdc; |
| 6557 | } |
| 6558 | } |
| 6559 | pmu->type = type; |
| 6560 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6561 | if (pmu_bus_running) { |
| 6562 | ret = pmu_dev_alloc(pmu); |
| 6563 | if (ret) |
| 6564 | goto free_idr; |
| 6565 | } |
| 6566 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6567 | skip_type: |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6568 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 6569 | if (pmu->pmu_cpu_context) |
| 6570 | goto got_cpu_context; |
| 6571 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 6572 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6573 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 6574 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6575 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6576 | |
| 6577 | for_each_possible_cpu(cpu) { |
| 6578 | struct perf_cpu_context *cpuctx; |
| 6579 | |
| 6580 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 6581 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6582 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6583 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6584 | cpuctx->ctx.type = cpu_context; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6585 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 6586 | |
| 6587 | __perf_cpu_hrtimer_init(cpuctx, cpu); |
| 6588 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 6589 | INIT_LIST_HEAD(&cpuctx->rotation_list); |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6590 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6591 | } |
| 6592 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6593 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6594 | if (!pmu->start_txn) { |
| 6595 | if (pmu->pmu_enable) { |
| 6596 | /* |
| 6597 | * If we have pmu_enable/pmu_disable calls, install |
| 6598 | * transaction stubs that use that to try and batch |
| 6599 | * hardware accesses. |
| 6600 | */ |
| 6601 | pmu->start_txn = perf_pmu_start_txn; |
| 6602 | pmu->commit_txn = perf_pmu_commit_txn; |
| 6603 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 6604 | } else { |
| 6605 | pmu->start_txn = perf_pmu_nop_void; |
| 6606 | pmu->commit_txn = perf_pmu_nop_int; |
| 6607 | pmu->cancel_txn = perf_pmu_nop_void; |
| 6608 | } |
| 6609 | } |
| 6610 | |
| 6611 | if (!pmu->pmu_enable) { |
| 6612 | pmu->pmu_enable = perf_pmu_nop_void; |
| 6613 | pmu->pmu_disable = perf_pmu_nop_void; |
| 6614 | } |
| 6615 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6616 | if (!pmu->event_idx) |
| 6617 | pmu->event_idx = perf_event_idx_default; |
| 6618 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6619 | list_add_rcu(&pmu->entry, &pmus); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6620 | ret = 0; |
| 6621 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6622 | mutex_unlock(&pmus_lock); |
| 6623 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6624 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6625 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6626 | free_dev: |
| 6627 | device_del(pmu->dev); |
| 6628 | put_device(pmu->dev); |
| 6629 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6630 | free_idr: |
| 6631 | if (pmu->type >= PERF_TYPE_MAX) |
| 6632 | idr_remove(&pmu_idr, pmu->type); |
| 6633 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6634 | free_pdc: |
| 6635 | free_percpu(pmu->pmu_disable_count); |
| 6636 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6637 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6638 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6639 | |
| 6640 | void perf_pmu_unregister(struct pmu *pmu) |
| 6641 | { |
| 6642 | mutex_lock(&pmus_lock); |
| 6643 | list_del_rcu(&pmu->entry); |
| 6644 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6645 | |
| 6646 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6647 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 6648 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6649 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6650 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6651 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6652 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6653 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6654 | if (pmu->type >= PERF_TYPE_MAX) |
| 6655 | idr_remove(&pmu_idr, pmu->type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6656 | device_del(pmu->dev); |
| 6657 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6658 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6659 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6660 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6661 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6662 | struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6663 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6664 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6665 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6666 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6667 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6668 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6669 | |
| 6670 | rcu_read_lock(); |
| 6671 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 6672 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6673 | if (pmu) { |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6674 | if (!try_module_get(pmu->module)) { |
| 6675 | pmu = ERR_PTR(-ENODEV); |
| 6676 | goto unlock; |
| 6677 | } |
Mark Rutland | 7e5b2a0 | 2011-08-11 12:31:20 +0100 | [diff] [blame] | 6678 | event->pmu = pmu; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6679 | ret = pmu->event_init(event); |
| 6680 | if (ret) |
| 6681 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6682 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6683 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6684 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6685 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6686 | if (!try_module_get(pmu->module)) { |
| 6687 | pmu = ERR_PTR(-ENODEV); |
| 6688 | goto unlock; |
| 6689 | } |
Mark Rutland | 7e5b2a0 | 2011-08-11 12:31:20 +0100 | [diff] [blame] | 6690 | event->pmu = pmu; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6691 | ret = pmu->event_init(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6692 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6693 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6694 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6695 | if (ret != -ENOENT) { |
| 6696 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6697 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6698 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6699 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6700 | pmu = ERR_PTR(-ENOENT); |
| 6701 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6702 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6703 | |
| 6704 | return pmu; |
| 6705 | } |
| 6706 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6707 | static void account_event_cpu(struct perf_event *event, int cpu) |
| 6708 | { |
| 6709 | if (event->parent) |
| 6710 | return; |
| 6711 | |
| 6712 | if (has_branch_stack(event)) { |
| 6713 | if (!(event->attach_state & PERF_ATTACH_TASK)) |
| 6714 | atomic_inc(&per_cpu(perf_branch_stack_events, cpu)); |
| 6715 | } |
| 6716 | if (is_cgroup_event(event)) |
| 6717 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); |
| 6718 | } |
| 6719 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6720 | static void account_event(struct perf_event *event) |
| 6721 | { |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6722 | if (event->parent) |
| 6723 | return; |
| 6724 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6725 | if (event->attach_state & PERF_ATTACH_TASK) |
| 6726 | static_key_slow_inc(&perf_sched_events.key); |
| 6727 | if (event->attr.mmap || event->attr.mmap_data) |
| 6728 | atomic_inc(&nr_mmap_events); |
| 6729 | if (event->attr.comm) |
| 6730 | atomic_inc(&nr_comm_events); |
| 6731 | if (event->attr.task) |
| 6732 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 6733 | if (event->attr.freq) { |
| 6734 | if (atomic_inc_return(&nr_freq_events) == 1) |
| 6735 | tick_nohz_full_kick_all(); |
| 6736 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6737 | if (has_branch_stack(event)) |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6738 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6739 | if (is_cgroup_event(event)) |
| 6740 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6741 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6742 | account_event_cpu(event, event->cpu); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6743 | } |
| 6744 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6745 | /* |
| 6746 | * Allocate and initialize a event structure |
| 6747 | */ |
| 6748 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6749 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6750 | struct task_struct *task, |
| 6751 | struct perf_event *group_leader, |
| 6752 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6753 | perf_overflow_handler_t overflow_handler, |
| 6754 | void *context) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6755 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6756 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6757 | struct perf_event *event; |
| 6758 | struct hw_perf_event *hwc; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6759 | long err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6760 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 6761 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 6762 | if (!task || cpu != -1) |
| 6763 | return ERR_PTR(-EINVAL); |
| 6764 | } |
| 6765 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6766 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6767 | if (!event) |
| 6768 | return ERR_PTR(-ENOMEM); |
| 6769 | |
| 6770 | /* |
| 6771 | * Single events are their own group leaders, with an |
| 6772 | * empty sibling list: |
| 6773 | */ |
| 6774 | if (!group_leader) |
| 6775 | group_leader = event; |
| 6776 | |
| 6777 | mutex_init(&event->child_mutex); |
| 6778 | INIT_LIST_HEAD(&event->child_list); |
| 6779 | |
| 6780 | INIT_LIST_HEAD(&event->group_entry); |
| 6781 | INIT_LIST_HEAD(&event->event_entry); |
| 6782 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 6783 | INIT_LIST_HEAD(&event->rb_entry); |
Stephane Eranian | 71ad88e | 2013-11-12 17:58:48 +0100 | [diff] [blame] | 6784 | INIT_LIST_HEAD(&event->active_entry); |
Stephane Eranian | f3ae75d | 2014-01-08 11:15:52 +0100 | [diff] [blame] | 6785 | INIT_HLIST_NODE(&event->hlist_entry); |
| 6786 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 6787 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6788 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 6789 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6790 | |
| 6791 | mutex_init(&event->mmap_mutex); |
| 6792 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 6793 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6794 | event->cpu = cpu; |
| 6795 | event->attr = *attr; |
| 6796 | event->group_leader = group_leader; |
| 6797 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6798 | event->oncpu = -1; |
| 6799 | |
| 6800 | event->parent = parent_event; |
| 6801 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 6802 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6803 | event->id = atomic64_inc_return(&perf_event_id); |
| 6804 | |
| 6805 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 6806 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6807 | if (task) { |
| 6808 | event->attach_state = PERF_ATTACH_TASK; |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 6809 | |
| 6810 | if (attr->type == PERF_TYPE_TRACEPOINT) |
| 6811 | event->hw.tp_target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6812 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
| 6813 | /* |
| 6814 | * hw_breakpoint is a bit difficult here.. |
| 6815 | */ |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 6816 | else if (attr->type == PERF_TYPE_BREAKPOINT) |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6817 | event->hw.bp_target = task; |
| 6818 | #endif |
| 6819 | } |
| 6820 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6821 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 6822 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6823 | context = parent_event->overflow_handler_context; |
| 6824 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 6825 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 6826 | event->overflow_handler = overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6827 | event->overflow_handler_context = context; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 6828 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 6829 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6830 | |
| 6831 | pmu = NULL; |
| 6832 | |
| 6833 | hwc = &event->hw; |
| 6834 | hwc->sample_period = attr->sample_period; |
| 6835 | if (attr->freq && attr->sample_freq) |
| 6836 | hwc->sample_period = 1; |
| 6837 | hwc->last_period = hwc->sample_period; |
| 6838 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6839 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6840 | |
| 6841 | /* |
| 6842 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 6843 | */ |
| 6844 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6845 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6846 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6847 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6848 | if (!pmu) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6849 | goto err_ns; |
| 6850 | else if (IS_ERR(pmu)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6851 | err = PTR_ERR(pmu); |
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 | } |
| 6854 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6855 | if (!event->parent) { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6856 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 6857 | err = get_callchain_buffers(); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6858 | if (err) |
| 6859 | goto err_pmu; |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 6860 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6861 | } |
| 6862 | |
| 6863 | return event; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6864 | |
| 6865 | err_pmu: |
| 6866 | if (event->destroy) |
| 6867 | event->destroy(event); |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6868 | module_put(pmu->module); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6869 | err_ns: |
| 6870 | if (event->ns) |
| 6871 | put_pid_ns(event->ns); |
| 6872 | kfree(event); |
| 6873 | |
| 6874 | return ERR_PTR(err); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6875 | } |
| 6876 | |
| 6877 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 6878 | struct perf_event_attr *attr) |
| 6879 | { |
| 6880 | u32 size; |
| 6881 | int ret; |
| 6882 | |
| 6883 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 6884 | return -EFAULT; |
| 6885 | |
| 6886 | /* |
| 6887 | * zero the full structure, so that a short copy will be nice. |
| 6888 | */ |
| 6889 | memset(attr, 0, sizeof(*attr)); |
| 6890 | |
| 6891 | ret = get_user(size, &uattr->size); |
| 6892 | if (ret) |
| 6893 | return ret; |
| 6894 | |
| 6895 | if (size > PAGE_SIZE) /* silly large */ |
| 6896 | goto err_size; |
| 6897 | |
| 6898 | if (!size) /* abi compat */ |
| 6899 | size = PERF_ATTR_SIZE_VER0; |
| 6900 | |
| 6901 | if (size < PERF_ATTR_SIZE_VER0) |
| 6902 | goto err_size; |
| 6903 | |
| 6904 | /* |
| 6905 | * If we're handed a bigger struct than we know of, |
| 6906 | * ensure all the unknown bits are 0 - i.e. new |
| 6907 | * user-space does not rely on any kernel feature |
| 6908 | * extensions we dont know about yet. |
| 6909 | */ |
| 6910 | if (size > sizeof(*attr)) { |
| 6911 | unsigned char __user *addr; |
| 6912 | unsigned char __user *end; |
| 6913 | unsigned char val; |
| 6914 | |
| 6915 | addr = (void __user *)uattr + sizeof(*attr); |
| 6916 | end = (void __user *)uattr + size; |
| 6917 | |
| 6918 | for (; addr < end; addr++) { |
| 6919 | ret = get_user(val, addr); |
| 6920 | if (ret) |
| 6921 | return ret; |
| 6922 | if (val) |
| 6923 | goto err_size; |
| 6924 | } |
| 6925 | size = sizeof(*attr); |
| 6926 | } |
| 6927 | |
| 6928 | ret = copy_from_user(attr, uattr, size); |
| 6929 | if (ret) |
| 6930 | return -EFAULT; |
| 6931 | |
Stephane Eranian | 3090ffb | 2013-10-17 19:32:15 +0200 | [diff] [blame] | 6932 | /* disabled for now */ |
| 6933 | if (attr->mmap2) |
| 6934 | return -EINVAL; |
| 6935 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 6936 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6937 | return -EINVAL; |
| 6938 | |
| 6939 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 6940 | return -EINVAL; |
| 6941 | |
| 6942 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 6943 | return -EINVAL; |
| 6944 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 6945 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 6946 | u64 mask = attr->branch_sample_type; |
| 6947 | |
| 6948 | /* only using defined bits */ |
| 6949 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 6950 | return -EINVAL; |
| 6951 | |
| 6952 | /* at least one branch bit must be set */ |
| 6953 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 6954 | return -EINVAL; |
| 6955 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 6956 | /* propagate priv level, when not set for branch */ |
| 6957 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 6958 | |
| 6959 | /* exclude_kernel checked on syscall entry */ |
| 6960 | if (!attr->exclude_kernel) |
| 6961 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 6962 | |
| 6963 | if (!attr->exclude_user) |
| 6964 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 6965 | |
| 6966 | if (!attr->exclude_hv) |
| 6967 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 6968 | /* |
| 6969 | * adjust user setting (for HW filter setup) |
| 6970 | */ |
| 6971 | attr->branch_sample_type = mask; |
| 6972 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 6973 | /* privileged levels capture (kernel, hv): check permissions */ |
| 6974 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 6975 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 6976 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 6977 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 6978 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6979 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 6980 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6981 | if (ret) |
| 6982 | return ret; |
| 6983 | } |
| 6984 | |
| 6985 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 6986 | if (!arch_perf_have_user_stack_dump()) |
| 6987 | return -ENOSYS; |
| 6988 | |
| 6989 | /* |
| 6990 | * We have __u32 type for the size, but so far |
| 6991 | * we can only use __u16 as maximum due to the |
| 6992 | * __u16 sample size limit. |
| 6993 | */ |
| 6994 | if (attr->sample_stack_user >= USHRT_MAX) |
| 6995 | ret = -EINVAL; |
| 6996 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
| 6997 | ret = -EINVAL; |
| 6998 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 6999 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7000 | out: |
| 7001 | return ret; |
| 7002 | |
| 7003 | err_size: |
| 7004 | put_user(sizeof(*attr), &uattr->size); |
| 7005 | ret = -E2BIG; |
| 7006 | goto out; |
| 7007 | } |
| 7008 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7009 | static int |
| 7010 | 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] | 7011 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7012 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7013 | int ret = -EINVAL; |
| 7014 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7015 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7016 | goto set; |
| 7017 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7018 | /* don't allow circular references */ |
| 7019 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7020 | goto out; |
| 7021 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 7022 | /* |
| 7023 | * Don't allow cross-cpu buffers |
| 7024 | */ |
| 7025 | if (output_event->cpu != event->cpu) |
| 7026 | goto out; |
| 7027 | |
| 7028 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7029 | * 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] | 7030 | */ |
| 7031 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 7032 | goto out; |
| 7033 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7034 | set: |
| 7035 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7036 | /* Can't redirect output if we've got an active mmap() */ |
| 7037 | if (atomic_read(&event->mmap_count)) |
| 7038 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7039 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7040 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7041 | /* get the rb we want to redirect to */ |
| 7042 | rb = ring_buffer_get(output_event); |
| 7043 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7044 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7045 | } |
| 7046 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7047 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 7048 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7049 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7050 | unlock: |
| 7051 | mutex_unlock(&event->mmap_mutex); |
| 7052 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7053 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7054 | return ret; |
| 7055 | } |
| 7056 | |
| 7057 | /** |
| 7058 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 7059 | * |
| 7060 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 7061 | * @pid: target pid |
| 7062 | * @cpu: target cpu |
| 7063 | * @group_fd: group leader event fd |
| 7064 | */ |
| 7065 | SYSCALL_DEFINE5(perf_event_open, |
| 7066 | struct perf_event_attr __user *, attr_uptr, |
| 7067 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 7068 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7069 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 7070 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7071 | struct perf_event_attr attr; |
| 7072 | struct perf_event_context *ctx; |
| 7073 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7074 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7075 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7076 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7077 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7078 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7079 | int err; |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7080 | int f_flags = O_RDWR; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7081 | |
| 7082 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7083 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7084 | return -EINVAL; |
| 7085 | |
| 7086 | err = perf_copy_attr(attr_uptr, &attr); |
| 7087 | if (err) |
| 7088 | return err; |
| 7089 | |
| 7090 | if (!attr.exclude_kernel) { |
| 7091 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 7092 | return -EACCES; |
| 7093 | } |
| 7094 | |
| 7095 | if (attr.freq) { |
| 7096 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 7097 | return -EINVAL; |
Peter Zijlstra | 0819b2e | 2014-05-15 20:23:48 +0200 | [diff] [blame] | 7098 | } else { |
| 7099 | if (attr.sample_period & (1ULL << 63)) |
| 7100 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7101 | } |
| 7102 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7103 | /* |
| 7104 | * In cgroup mode, the pid argument is used to pass the fd |
| 7105 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 7106 | * designates the cpu on which to monitor threads from that |
| 7107 | * cgroup. |
| 7108 | */ |
| 7109 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 7110 | return -EINVAL; |
| 7111 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7112 | if (flags & PERF_FLAG_FD_CLOEXEC) |
| 7113 | f_flags |= O_CLOEXEC; |
| 7114 | |
| 7115 | event_fd = get_unused_fd_flags(f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7116 | if (event_fd < 0) |
| 7117 | return event_fd; |
| 7118 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7119 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7120 | err = perf_fget_light(group_fd, &group); |
| 7121 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7122 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7123 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7124 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 7125 | output_event = group_leader; |
| 7126 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 7127 | group_leader = NULL; |
| 7128 | } |
| 7129 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7130 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7131 | task = find_lively_task_by_vpid(pid); |
| 7132 | if (IS_ERR(task)) { |
| 7133 | err = PTR_ERR(task); |
| 7134 | goto err_group_fd; |
| 7135 | } |
| 7136 | } |
| 7137 | |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7138 | if (task && group_leader && |
| 7139 | group_leader->attr.inherit != attr.inherit) { |
| 7140 | err = -EINVAL; |
| 7141 | goto err_task; |
| 7142 | } |
| 7143 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7144 | get_online_cpus(); |
| 7145 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7146 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
| 7147 | NULL, NULL); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7148 | if (IS_ERR(event)) { |
| 7149 | err = PTR_ERR(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7150 | goto err_cpus; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7151 | } |
| 7152 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7153 | if (flags & PERF_FLAG_PID_CGROUP) { |
| 7154 | err = perf_cgroup_connect(pid, event, &attr, group_leader); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7155 | if (err) { |
| 7156 | __free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7157 | goto err_cpus; |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7158 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7159 | } |
| 7160 | |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 7161 | if (is_sampling_event(event)) { |
| 7162 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { |
| 7163 | err = -ENOTSUPP; |
| 7164 | goto err_alloc; |
| 7165 | } |
| 7166 | } |
| 7167 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7168 | account_event(event); |
| 7169 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7170 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7171 | * Special case software events and allow them to be part of |
| 7172 | * any hardware group. |
| 7173 | */ |
| 7174 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7175 | |
| 7176 | if (group_leader && |
| 7177 | (is_software_event(event) != is_software_event(group_leader))) { |
| 7178 | if (is_software_event(event)) { |
| 7179 | /* |
| 7180 | * If event and group_leader are not both a software |
| 7181 | * event, and event is, then group leader is not. |
| 7182 | * |
| 7183 | * Allow the addition of software events to !software |
| 7184 | * groups, this is safe because software events never |
| 7185 | * fail to schedule. |
| 7186 | */ |
| 7187 | pmu = group_leader->pmu; |
| 7188 | } else if (is_software_event(group_leader) && |
| 7189 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 7190 | /* |
| 7191 | * In case the group is a pure software group, and we |
| 7192 | * try to add a hardware event, move the whole group to |
| 7193 | * the hardware context. |
| 7194 | */ |
| 7195 | move_group = 1; |
| 7196 | } |
| 7197 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7198 | |
| 7199 | /* |
| 7200 | * Get the target context (task or percpu): |
| 7201 | */ |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7202 | ctx = find_get_context(pmu, task, event->cpu); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7203 | if (IS_ERR(ctx)) { |
| 7204 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7205 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7206 | } |
| 7207 | |
Peter Zijlstra | fd1edb3 | 2011-03-28 13:13:56 +0200 | [diff] [blame] | 7208 | if (task) { |
| 7209 | put_task_struct(task); |
| 7210 | task = NULL; |
| 7211 | } |
| 7212 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7213 | /* |
| 7214 | * Look up the group leader (we will attach this event to it): |
| 7215 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7216 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7217 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7218 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7219 | /* |
| 7220 | * Do not allow a recursive hierarchy (this new sibling |
| 7221 | * becoming part of another group-sibling): |
| 7222 | */ |
| 7223 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7224 | goto err_context; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7225 | /* |
| 7226 | * Do not allow to attach to a group in a different |
| 7227 | * task or CPU context: |
| 7228 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7229 | if (move_group) { |
| 7230 | if (group_leader->ctx->type != ctx->type) |
| 7231 | goto err_context; |
| 7232 | } else { |
| 7233 | if (group_leader->ctx != ctx) |
| 7234 | goto err_context; |
| 7235 | } |
| 7236 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7237 | /* |
| 7238 | * Only a group leader can be exclusive or pinned |
| 7239 | */ |
| 7240 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7241 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7242 | } |
| 7243 | |
| 7244 | if (output_event) { |
| 7245 | err = perf_event_set_output(event, output_event); |
| 7246 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7247 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7248 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7249 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7250 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
| 7251 | f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7252 | if (IS_ERR(event_file)) { |
| 7253 | err = PTR_ERR(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7254 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7255 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7256 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7257 | if (move_group) { |
| 7258 | struct perf_event_context *gctx = group_leader->ctx; |
| 7259 | |
| 7260 | mutex_lock(&gctx->mutex); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7261 | perf_remove_from_context(group_leader, false); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7262 | |
| 7263 | /* |
| 7264 | * Removing from the context ends up with disabled |
| 7265 | * event. What we want here is event in the initial |
| 7266 | * startup state, ready to be add into new context. |
| 7267 | */ |
| 7268 | perf_event__state_init(group_leader); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7269 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7270 | group_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7271 | perf_remove_from_context(sibling, false); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7272 | perf_event__state_init(sibling); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7273 | put_ctx(gctx); |
| 7274 | } |
| 7275 | mutex_unlock(&gctx->mutex); |
| 7276 | put_ctx(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7277 | } |
| 7278 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7279 | WARN_ON_ONCE(ctx->parent_ctx); |
| 7280 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7281 | |
| 7282 | if (move_group) { |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7283 | synchronize_rcu(); |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7284 | perf_install_in_context(ctx, group_leader, event->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7285 | get_ctx(ctx); |
| 7286 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7287 | group_entry) { |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7288 | perf_install_in_context(ctx, sibling, event->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7289 | get_ctx(ctx); |
| 7290 | } |
| 7291 | } |
| 7292 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7293 | perf_install_in_context(ctx, event, event->cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7294 | perf_unpin_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7295 | mutex_unlock(&ctx->mutex); |
| 7296 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7297 | put_online_cpus(); |
| 7298 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7299 | event->owner = current; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7300 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7301 | mutex_lock(¤t->perf_event_mutex); |
| 7302 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 7303 | mutex_unlock(¤t->perf_event_mutex); |
| 7304 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7305 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7306 | * Precalculate sample_data sizes |
| 7307 | */ |
| 7308 | perf_event__header_size(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7309 | perf_event__id_header_size(event); |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7310 | |
| 7311 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7312 | * Drop the reference on the group_event after placing the |
| 7313 | * new event on the sibling_list. This ensures destruction |
| 7314 | * of the group leader will find the pointer to itself in |
| 7315 | * perf_group_detach(). |
| 7316 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7317 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7318 | fd_install(event_fd, event_file); |
| 7319 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7320 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7321 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7322 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7323 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7324 | err_alloc: |
| 7325 | free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7326 | err_cpus: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7327 | put_online_cpus(); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7328 | err_task: |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 7329 | if (task) |
| 7330 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7331 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7332 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7333 | err_fd: |
| 7334 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7335 | return err; |
| 7336 | } |
| 7337 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7338 | /** |
| 7339 | * perf_event_create_kernel_counter |
| 7340 | * |
| 7341 | * @attr: attributes of the counter to create |
| 7342 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7343 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7344 | */ |
| 7345 | struct perf_event * |
| 7346 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7347 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7348 | perf_overflow_handler_t overflow_handler, |
| 7349 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7350 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7351 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7352 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7353 | int err; |
| 7354 | |
| 7355 | /* |
| 7356 | * Get the target context (task or percpu): |
| 7357 | */ |
| 7358 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7359 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
| 7360 | overflow_handler, context); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7361 | if (IS_ERR(event)) { |
| 7362 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7363 | goto err; |
| 7364 | } |
| 7365 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7366 | account_event(event); |
| 7367 | |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7368 | ctx = find_get_context(event->pmu, task, cpu); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7369 | if (IS_ERR(ctx)) { |
| 7370 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7371 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7372 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7373 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7374 | WARN_ON_ONCE(ctx->parent_ctx); |
| 7375 | mutex_lock(&ctx->mutex); |
| 7376 | perf_install_in_context(ctx, event, cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7377 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7378 | mutex_unlock(&ctx->mutex); |
| 7379 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7380 | return event; |
| 7381 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7382 | err_free: |
| 7383 | free_event(event); |
| 7384 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7385 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7386 | } |
| 7387 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 7388 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7389 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 7390 | { |
| 7391 | struct perf_event_context *src_ctx; |
| 7392 | struct perf_event_context *dst_ctx; |
| 7393 | struct perf_event *event, *tmp; |
| 7394 | LIST_HEAD(events); |
| 7395 | |
| 7396 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 7397 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 7398 | |
| 7399 | mutex_lock(&src_ctx->mutex); |
| 7400 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 7401 | event_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7402 | perf_remove_from_context(event, false); |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7403 | unaccount_event_cpu(event, src_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7404 | put_ctx(src_ctx); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7405 | list_add(&event->migrate_entry, &events); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7406 | } |
| 7407 | mutex_unlock(&src_ctx->mutex); |
| 7408 | |
| 7409 | synchronize_rcu(); |
| 7410 | |
| 7411 | mutex_lock(&dst_ctx->mutex); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7412 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 7413 | list_del(&event->migrate_entry); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7414 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 7415 | event->state = PERF_EVENT_STATE_INACTIVE; |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7416 | account_event_cpu(event, dst_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7417 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 7418 | get_ctx(dst_ctx); |
| 7419 | } |
| 7420 | mutex_unlock(&dst_ctx->mutex); |
| 7421 | } |
| 7422 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 7423 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7424 | static void sync_child_event(struct perf_event *child_event, |
| 7425 | struct task_struct *child) |
| 7426 | { |
| 7427 | struct perf_event *parent_event = child_event->parent; |
| 7428 | u64 child_val; |
| 7429 | |
| 7430 | if (child_event->attr.inherit_stat) |
| 7431 | perf_event_read_event(child_event, child); |
| 7432 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 7433 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7434 | |
| 7435 | /* |
| 7436 | * Add back the child's count to the parent's count: |
| 7437 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 7438 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7439 | atomic64_add(child_event->total_time_enabled, |
| 7440 | &parent_event->child_total_time_enabled); |
| 7441 | atomic64_add(child_event->total_time_running, |
| 7442 | &parent_event->child_total_time_running); |
| 7443 | |
| 7444 | /* |
| 7445 | * Remove this event from the parent's list |
| 7446 | */ |
| 7447 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7448 | mutex_lock(&parent_event->child_mutex); |
| 7449 | list_del_init(&child_event->child_list); |
| 7450 | mutex_unlock(&parent_event->child_mutex); |
| 7451 | |
| 7452 | /* |
| 7453 | * Release the parent event, if this was the last |
| 7454 | * reference to it. |
| 7455 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7456 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7457 | } |
| 7458 | |
| 7459 | static void |
| 7460 | __perf_event_exit_task(struct perf_event *child_event, |
| 7461 | struct perf_event_context *child_ctx, |
| 7462 | struct task_struct *child) |
| 7463 | { |
Peter Zijlstra | 15a2d4d | 2014-05-05 11:41:02 +0200 | [diff] [blame] | 7464 | perf_remove_from_context(child_event, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7465 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7466 | /* |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7467 | * It can happen that the parent exits first, and has events |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7468 | * that are still around due to the child reference. These |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7469 | * events need to be zapped. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7470 | */ |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7471 | if (child_event->parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7472 | sync_child_event(child_event, child); |
| 7473 | free_event(child_event); |
| 7474 | } |
| 7475 | } |
| 7476 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7477 | 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] | 7478 | { |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 7479 | struct perf_event *child_event, *next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7480 | struct perf_event_context *child_ctx; |
| 7481 | unsigned long flags; |
| 7482 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7483 | if (likely(!child->perf_event_ctxp[ctxn])) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7484 | perf_event_task(child, NULL, 0); |
| 7485 | return; |
| 7486 | } |
| 7487 | |
| 7488 | local_irq_save(flags); |
| 7489 | /* |
| 7490 | * We can't reschedule here because interrupts are disabled, |
| 7491 | * and either child is current or it is a task that can't be |
| 7492 | * scheduled, so we are now safe from rescheduling changing |
| 7493 | * our context. |
| 7494 | */ |
Oleg Nesterov | 806839b | 2011-01-21 18:45:47 +0100 | [diff] [blame] | 7495 | child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7496 | |
| 7497 | /* |
| 7498 | * Take the context lock here so that if find_get_context is |
| 7499 | * reading child->perf_event_ctxp, we wait until it has |
| 7500 | * incremented the context's refcount before we do put_ctx below. |
| 7501 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7502 | raw_spin_lock(&child_ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 7503 | task_ctx_sched_out(child_ctx); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7504 | child->perf_event_ctxp[ctxn] = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7505 | /* |
| 7506 | * If this context is a clone; unclone it so it can't get |
| 7507 | * swapped to another process while we're removing all |
| 7508 | * the events from it. |
| 7509 | */ |
| 7510 | unclone_ctx(child_ctx); |
Peter Zijlstra | 5e942bb | 2009-11-23 11:37:26 +0100 | [diff] [blame] | 7511 | update_context_time(child_ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7512 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7513 | |
| 7514 | /* |
| 7515 | * Report the task dead after unscheduling the events so that we |
| 7516 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 7517 | * get a few PERF_RECORD_READ events. |
| 7518 | */ |
| 7519 | perf_event_task(child, child_ctx, 0); |
| 7520 | |
| 7521 | /* |
| 7522 | * We can recurse on the same lock type through: |
| 7523 | * |
| 7524 | * __perf_event_exit_task() |
| 7525 | * sync_child_event() |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7526 | * put_event() |
| 7527 | * mutex_lock(&ctx->mutex) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7528 | * |
| 7529 | * But since its the parent context it won't be the same instance. |
| 7530 | */ |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 7531 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7532 | |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 7533 | 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] | 7534 | __perf_event_exit_task(child_event, child_ctx, child); |
| 7535 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7536 | mutex_unlock(&child_ctx->mutex); |
| 7537 | |
| 7538 | put_ctx(child_ctx); |
| 7539 | } |
| 7540 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7541 | /* |
| 7542 | * When a child task exits, feed back event values to parent events. |
| 7543 | */ |
| 7544 | void perf_event_exit_task(struct task_struct *child) |
| 7545 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7546 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7547 | int ctxn; |
| 7548 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7549 | mutex_lock(&child->perf_event_mutex); |
| 7550 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 7551 | owner_entry) { |
| 7552 | list_del_init(&event->owner_entry); |
| 7553 | |
| 7554 | /* |
| 7555 | * Ensure the list deletion is visible before we clear |
| 7556 | * the owner, closes a race against perf_release() where |
| 7557 | * we need to serialize on the owner->perf_event_mutex. |
| 7558 | */ |
| 7559 | smp_wmb(); |
| 7560 | event->owner = NULL; |
| 7561 | } |
| 7562 | mutex_unlock(&child->perf_event_mutex); |
| 7563 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7564 | for_each_task_context_nr(ctxn) |
| 7565 | perf_event_exit_task_context(child, ctxn); |
| 7566 | } |
| 7567 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7568 | static void perf_free_event(struct perf_event *event, |
| 7569 | struct perf_event_context *ctx) |
| 7570 | { |
| 7571 | struct perf_event *parent = event->parent; |
| 7572 | |
| 7573 | if (WARN_ON_ONCE(!parent)) |
| 7574 | return; |
| 7575 | |
| 7576 | mutex_lock(&parent->child_mutex); |
| 7577 | list_del_init(&event->child_list); |
| 7578 | mutex_unlock(&parent->child_mutex); |
| 7579 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7580 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7581 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7582 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7583 | list_del_event(event, ctx); |
| 7584 | free_event(event); |
| 7585 | } |
| 7586 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7587 | /* |
| 7588 | * free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7589 | * perf_event_init_task below, used by fork() in case of fail. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7590 | */ |
| 7591 | void perf_event_free_task(struct task_struct *task) |
| 7592 | { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7593 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7594 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7595 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7596 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7597 | for_each_task_context_nr(ctxn) { |
| 7598 | ctx = task->perf_event_ctxp[ctxn]; |
| 7599 | if (!ctx) |
| 7600 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7601 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7602 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7603 | again: |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7604 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 7605 | group_entry) |
| 7606 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7607 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7608 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 7609 | group_entry) |
| 7610 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7611 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7612 | if (!list_empty(&ctx->pinned_groups) || |
| 7613 | !list_empty(&ctx->flexible_groups)) |
| 7614 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7615 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7616 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7617 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7618 | put_ctx(ctx); |
| 7619 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7620 | } |
| 7621 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 7622 | void perf_event_delayed_put(struct task_struct *task) |
| 7623 | { |
| 7624 | int ctxn; |
| 7625 | |
| 7626 | for_each_task_context_nr(ctxn) |
| 7627 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 7628 | } |
| 7629 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7630 | /* |
| 7631 | * inherit a event from parent task to child task: |
| 7632 | */ |
| 7633 | static struct perf_event * |
| 7634 | inherit_event(struct perf_event *parent_event, |
| 7635 | struct task_struct *parent, |
| 7636 | struct perf_event_context *parent_ctx, |
| 7637 | struct task_struct *child, |
| 7638 | struct perf_event *group_leader, |
| 7639 | struct perf_event_context *child_ctx) |
| 7640 | { |
| 7641 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7642 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7643 | |
| 7644 | /* |
| 7645 | * Instead of creating recursive hierarchies of events, |
| 7646 | * we link inherited events back to the original parent, |
| 7647 | * which has a filp for sure, which we use as the reference |
| 7648 | * count: |
| 7649 | */ |
| 7650 | if (parent_event->parent) |
| 7651 | parent_event = parent_event->parent; |
| 7652 | |
| 7653 | child_event = perf_event_alloc(&parent_event->attr, |
| 7654 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7655 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7656 | group_leader, parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7657 | NULL, NULL); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7658 | if (IS_ERR(child_event)) |
| 7659 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7660 | |
| 7661 | if (!atomic_long_inc_not_zero(&parent_event->refcount)) { |
| 7662 | free_event(child_event); |
| 7663 | return NULL; |
| 7664 | } |
| 7665 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7666 | get_ctx(child_ctx); |
| 7667 | |
| 7668 | /* |
| 7669 | * Make the child state follow the state of the parent event, |
| 7670 | * not its attr.disabled bit. We hold the parent's mutex, |
| 7671 | * so we won't race with perf_event_{en, dis}able_family. |
| 7672 | */ |
| 7673 | if (parent_event->state >= PERF_EVENT_STATE_INACTIVE) |
| 7674 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 7675 | else |
| 7676 | child_event->state = PERF_EVENT_STATE_OFF; |
| 7677 | |
| 7678 | if (parent_event->attr.freq) { |
| 7679 | u64 sample_period = parent_event->hw.sample_period; |
| 7680 | struct hw_perf_event *hwc = &child_event->hw; |
| 7681 | |
| 7682 | hwc->sample_period = sample_period; |
| 7683 | hwc->last_period = sample_period; |
| 7684 | |
| 7685 | local64_set(&hwc->period_left, sample_period); |
| 7686 | } |
| 7687 | |
| 7688 | child_event->ctx = child_ctx; |
| 7689 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7690 | child_event->overflow_handler_context |
| 7691 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7692 | |
| 7693 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 7694 | * Precalculate sample_data sizes |
| 7695 | */ |
| 7696 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7697 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 7698 | |
| 7699 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7700 | * Link it up in the child's context: |
| 7701 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7702 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7703 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7704 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7705 | |
| 7706 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7707 | * Link this into the parent event's child list |
| 7708 | */ |
| 7709 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7710 | mutex_lock(&parent_event->child_mutex); |
| 7711 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 7712 | mutex_unlock(&parent_event->child_mutex); |
| 7713 | |
| 7714 | return child_event; |
| 7715 | } |
| 7716 | |
| 7717 | static int inherit_group(struct perf_event *parent_event, |
| 7718 | struct task_struct *parent, |
| 7719 | struct perf_event_context *parent_ctx, |
| 7720 | struct task_struct *child, |
| 7721 | struct perf_event_context *child_ctx) |
| 7722 | { |
| 7723 | struct perf_event *leader; |
| 7724 | struct perf_event *sub; |
| 7725 | struct perf_event *child_ctr; |
| 7726 | |
| 7727 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 7728 | child, NULL, child_ctx); |
| 7729 | if (IS_ERR(leader)) |
| 7730 | return PTR_ERR(leader); |
| 7731 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 7732 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 7733 | child, leader, child_ctx); |
| 7734 | if (IS_ERR(child_ctr)) |
| 7735 | return PTR_ERR(child_ctr); |
| 7736 | } |
| 7737 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7738 | } |
| 7739 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7740 | static int |
| 7741 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 7742 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7743 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7744 | int *inherited_all) |
| 7745 | { |
| 7746 | int ret; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7747 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7748 | |
| 7749 | if (!event->attr.inherit) { |
| 7750 | *inherited_all = 0; |
| 7751 | return 0; |
| 7752 | } |
| 7753 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7754 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7755 | if (!child_ctx) { |
| 7756 | /* |
| 7757 | * This is executed from the parent task context, so |
| 7758 | * inherit events that have been marked for cloning. |
| 7759 | * First allocate and initialize a context for the |
| 7760 | * child. |
| 7761 | */ |
| 7762 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 7763 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7764 | if (!child_ctx) |
| 7765 | return -ENOMEM; |
| 7766 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7767 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7768 | } |
| 7769 | |
| 7770 | ret = inherit_group(event, parent, parent_ctx, |
| 7771 | child, child_ctx); |
| 7772 | |
| 7773 | if (ret) |
| 7774 | *inherited_all = 0; |
| 7775 | |
| 7776 | return ret; |
| 7777 | } |
| 7778 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7779 | /* |
| 7780 | * Initialize the perf_event context in task_struct |
| 7781 | */ |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7782 | int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7783 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7784 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7785 | struct perf_event_context *cloned_ctx; |
| 7786 | struct perf_event *event; |
| 7787 | struct task_struct *parent = current; |
| 7788 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7789 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7790 | int ret = 0; |
| 7791 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7792 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7793 | return 0; |
| 7794 | |
| 7795 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7796 | * If the parent's context is a clone, pin it so it won't get |
| 7797 | * swapped under us. |
| 7798 | */ |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7799 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Peter Zijlstra | ffb4ef2 | 2014-05-05 19:12:20 +0200 | [diff] [blame] | 7800 | if (!parent_ctx) |
| 7801 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7802 | |
| 7803 | /* |
| 7804 | * No need to check if parent_ctx != NULL here; since we saw |
| 7805 | * it non-NULL earlier, the only reason for it to become NULL |
| 7806 | * is if we exit, and since we're currently in the middle of |
| 7807 | * a fork we can't be exiting at the same time. |
| 7808 | */ |
| 7809 | |
| 7810 | /* |
| 7811 | * Lock the parent list. No need to lock the child - not PID |
| 7812 | * hashed yet and not running, so nobody can access it. |
| 7813 | */ |
| 7814 | mutex_lock(&parent_ctx->mutex); |
| 7815 | |
| 7816 | /* |
| 7817 | * We dont have to disable NMIs - we are only looking at |
| 7818 | * the list, not manipulating it: |
| 7819 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7820 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7821 | ret = inherit_task_group(event, parent, parent_ctx, |
| 7822 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7823 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7824 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7825 | } |
| 7826 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7827 | /* |
| 7828 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 7829 | * to allocations, but we need to prevent rotation because |
| 7830 | * rotate_ctx() will change the list from interrupt context. |
| 7831 | */ |
| 7832 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 7833 | parent_ctx->rotate_disable = 1; |
| 7834 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 7835 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7836 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7837 | ret = inherit_task_group(event, parent, parent_ctx, |
| 7838 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7839 | if (ret) |
| 7840 | break; |
| 7841 | } |
| 7842 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7843 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 7844 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 7845 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7846 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7847 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 7848 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7849 | /* |
| 7850 | * Mark the child context as a clone of the parent |
| 7851 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7852 | * |
| 7853 | * Note that if the parent is a clone, the holding of |
| 7854 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7855 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7856 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7857 | if (cloned_ctx) { |
| 7858 | child_ctx->parent_ctx = cloned_ctx; |
| 7859 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 7860 | } else { |
| 7861 | child_ctx->parent_ctx = parent_ctx; |
| 7862 | child_ctx->parent_gen = parent_ctx->generation; |
| 7863 | } |
| 7864 | get_ctx(child_ctx->parent_ctx); |
| 7865 | } |
| 7866 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 7867 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7868 | mutex_unlock(&parent_ctx->mutex); |
| 7869 | |
| 7870 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7871 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7872 | |
| 7873 | return ret; |
| 7874 | } |
| 7875 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7876 | /* |
| 7877 | * Initialize the perf_event context in task_struct |
| 7878 | */ |
| 7879 | int perf_event_init_task(struct task_struct *child) |
| 7880 | { |
| 7881 | int ctxn, ret; |
| 7882 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 7883 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 7884 | mutex_init(&child->perf_event_mutex); |
| 7885 | INIT_LIST_HEAD(&child->perf_event_list); |
| 7886 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7887 | for_each_task_context_nr(ctxn) { |
| 7888 | ret = perf_event_init_context(child, ctxn); |
| 7889 | if (ret) |
| 7890 | return ret; |
| 7891 | } |
| 7892 | |
| 7893 | return 0; |
| 7894 | } |
| 7895 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7896 | static void __init perf_event_init_all_cpus(void) |
| 7897 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7898 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7899 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7900 | |
| 7901 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7902 | swhash = &per_cpu(swevent_htable, cpu); |
| 7903 | mutex_init(&swhash->hlist_mutex); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7904 | INIT_LIST_HEAD(&per_cpu(rotation_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 7905 | } |
| 7906 | } |
| 7907 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 7908 | static void perf_event_init_cpu(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7909 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7910 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7911 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7912 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 7913 | swhash->online = true; |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 7914 | if (swhash->hlist_refcount > 0) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7915 | struct swevent_hlist *hlist; |
| 7916 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7917 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 7918 | WARN_ON(!hlist); |
| 7919 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7920 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7921 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7922 | } |
| 7923 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 7924 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7925 | static void perf_pmu_rotate_stop(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7926 | { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 7927 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 7928 | |
| 7929 | WARN_ON(!irqs_disabled()); |
| 7930 | |
| 7931 | list_del_init(&cpuctx->rotation_list); |
| 7932 | } |
| 7933 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7934 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7935 | { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7936 | struct remove_event re = { .detach_group = false }; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7937 | struct perf_event_context *ctx = __info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7938 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7939 | perf_pmu_rotate_stop(ctx->pmu); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 7940 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 7941 | rcu_read_lock(); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7942 | list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry) |
| 7943 | __perf_remove_from_context(&re); |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 7944 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7945 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7946 | |
| 7947 | static void perf_event_exit_cpu_context(int cpu) |
| 7948 | { |
| 7949 | struct perf_event_context *ctx; |
| 7950 | struct pmu *pmu; |
| 7951 | int idx; |
| 7952 | |
| 7953 | idx = srcu_read_lock(&pmus_srcu); |
| 7954 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 7955 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7956 | |
| 7957 | mutex_lock(&ctx->mutex); |
| 7958 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 7959 | mutex_unlock(&ctx->mutex); |
| 7960 | } |
| 7961 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7962 | } |
| 7963 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7964 | static void perf_event_exit_cpu(int cpu) |
| 7965 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7966 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7967 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 7968 | perf_event_exit_cpu_context(cpu); |
| 7969 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7970 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 7971 | swhash->online = false; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7972 | swevent_hlist_release(swhash); |
| 7973 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7974 | } |
| 7975 | #else |
| 7976 | static inline void perf_event_exit_cpu(int cpu) { } |
| 7977 | #endif |
| 7978 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 7979 | static int |
| 7980 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 7981 | { |
| 7982 | int cpu; |
| 7983 | |
| 7984 | for_each_online_cpu(cpu) |
| 7985 | perf_event_exit_cpu(cpu); |
| 7986 | |
| 7987 | return NOTIFY_OK; |
| 7988 | } |
| 7989 | |
| 7990 | /* |
| 7991 | * Run the perf reboot notifier at the very last possible moment so that |
| 7992 | * the generic watchdog code runs as long as possible. |
| 7993 | */ |
| 7994 | static struct notifier_block perf_reboot_notifier = { |
| 7995 | .notifier_call = perf_reboot, |
| 7996 | .priority = INT_MIN, |
| 7997 | }; |
| 7998 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 7999 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8000 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 8001 | { |
| 8002 | unsigned int cpu = (long)hcpu; |
| 8003 | |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 8004 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8005 | |
| 8006 | case CPU_UP_PREPARE: |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8007 | case CPU_DOWN_FAILED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8008 | perf_event_init_cpu(cpu); |
| 8009 | break; |
| 8010 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8011 | case CPU_UP_CANCELED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8012 | case CPU_DOWN_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8013 | perf_event_exit_cpu(cpu); |
| 8014 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8015 | default: |
| 8016 | break; |
| 8017 | } |
| 8018 | |
| 8019 | return NOTIFY_OK; |
| 8020 | } |
| 8021 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8022 | void __init perf_event_init(void) |
| 8023 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8024 | int ret; |
| 8025 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8026 | idr_init(&pmu_idr); |
| 8027 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8028 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8029 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8030 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 8031 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 8032 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8033 | perf_tp_register(); |
| 8034 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8035 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8036 | |
| 8037 | ret = init_hw_breakpoint(); |
| 8038 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 8039 | |
| 8040 | /* do not patch jump label more than once per second */ |
| 8041 | jump_label_rate_limit(&perf_sched_events, HZ); |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 8042 | |
| 8043 | /* |
| 8044 | * Build time assertion that we keep the data_head at the intended |
| 8045 | * location. IOW, validation we got the __reserved[] size right. |
| 8046 | */ |
| 8047 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 8048 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8049 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8050 | |
| 8051 | static int __init perf_event_sysfs_init(void) |
| 8052 | { |
| 8053 | struct pmu *pmu; |
| 8054 | int ret; |
| 8055 | |
| 8056 | mutex_lock(&pmus_lock); |
| 8057 | |
| 8058 | ret = bus_register(&pmu_bus); |
| 8059 | if (ret) |
| 8060 | goto unlock; |
| 8061 | |
| 8062 | list_for_each_entry(pmu, &pmus, entry) { |
| 8063 | if (!pmu->name || pmu->type < 0) |
| 8064 | continue; |
| 8065 | |
| 8066 | ret = pmu_dev_alloc(pmu); |
| 8067 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 8068 | } |
| 8069 | pmu_bus_running = 1; |
| 8070 | ret = 0; |
| 8071 | |
| 8072 | unlock: |
| 8073 | mutex_unlock(&pmus_lock); |
| 8074 | |
| 8075 | return ret; |
| 8076 | } |
| 8077 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8078 | |
| 8079 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8080 | static struct cgroup_subsys_state * |
| 8081 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8082 | { |
| 8083 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8084 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 8085 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8086 | if (!jc) |
| 8087 | return ERR_PTR(-ENOMEM); |
| 8088 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8089 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 8090 | if (!jc->info) { |
| 8091 | kfree(jc); |
| 8092 | return ERR_PTR(-ENOMEM); |
| 8093 | } |
| 8094 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8095 | return &jc->css; |
| 8096 | } |
| 8097 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8098 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8099 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8100 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
| 8101 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8102 | free_percpu(jc->info); |
| 8103 | kfree(jc); |
| 8104 | } |
| 8105 | |
| 8106 | static int __perf_cgroup_move(void *info) |
| 8107 | { |
| 8108 | struct task_struct *task = info; |
| 8109 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
| 8110 | return 0; |
| 8111 | } |
| 8112 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8113 | static void perf_cgroup_attach(struct cgroup_subsys_state *css, |
| 8114 | struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8115 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8116 | struct task_struct *task; |
| 8117 | |
Tejun Heo | 924f0d9 | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 8118 | cgroup_taskset_for_each(task, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8119 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8120 | } |
| 8121 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8122 | static void perf_cgroup_exit(struct cgroup_subsys_state *css, |
| 8123 | struct cgroup_subsys_state *old_css, |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 8124 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8125 | { |
| 8126 | /* |
| 8127 | * cgroup_exit() is called in the copy_process() failure path. |
| 8128 | * Ignore this case since the task hasn't ran yet, this avoids |
| 8129 | * trying to poke a half freed task state from generic code. |
| 8130 | */ |
| 8131 | if (!(task->flags & PF_EXITING)) |
| 8132 | return; |
| 8133 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8134 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8135 | } |
| 8136 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 8137 | struct cgroup_subsys perf_event_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 8138 | .css_alloc = perf_cgroup_css_alloc, |
| 8139 | .css_free = perf_cgroup_css_free, |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 8140 | .exit = perf_cgroup_exit, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8141 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8142 | }; |
| 8143 | #endif /* CONFIG_CGROUP_PERF */ |