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> |
Matt Fleming | 39bed6c | 2015-01-23 18:45:40 +0000 | [diff] [blame] | 37 | #include <linux/cgroup.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 38 | #include <linux/perf_event.h> |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 39 | #include <linux/ftrace_event.h> |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 40 | #include <linux/hw_breakpoint.h> |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 41 | #include <linux/mm_types.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> |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 44 | #include <linux/compat.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 45 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 46 | #include "internal.h" |
| 47 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 48 | #include <asm/irq_regs.h> |
| 49 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 50 | static struct workqueue_struct *perf_wq; |
| 51 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 52 | struct remote_function_call { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 53 | struct task_struct *p; |
| 54 | int (*func)(void *info); |
| 55 | void *info; |
| 56 | int ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static void remote_function(void *data) |
| 60 | { |
| 61 | struct remote_function_call *tfc = data; |
| 62 | struct task_struct *p = tfc->p; |
| 63 | |
| 64 | if (p) { |
| 65 | tfc->ret = -EAGAIN; |
| 66 | if (task_cpu(p) != smp_processor_id() || !task_curr(p)) |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | tfc->ret = tfc->func(tfc->info); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * task_function_call - call a function on the cpu on which a task runs |
| 75 | * @p: the task to evaluate |
| 76 | * @func: the function to be called |
| 77 | * @info: the function call argument |
| 78 | * |
| 79 | * Calls the function @func when the task is currently running. This might |
| 80 | * be on the current CPU, which just calls the function directly |
| 81 | * |
| 82 | * returns: @func return value, or |
| 83 | * -ESRCH - when the process isn't running |
| 84 | * -EAGAIN - when the process moved away |
| 85 | */ |
| 86 | static int |
| 87 | task_function_call(struct task_struct *p, int (*func) (void *info), void *info) |
| 88 | { |
| 89 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 90 | .p = p, |
| 91 | .func = func, |
| 92 | .info = info, |
| 93 | .ret = -ESRCH, /* No such (running) process */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | if (task_curr(p)) |
| 97 | smp_call_function_single(task_cpu(p), remote_function, &data, 1); |
| 98 | |
| 99 | return data.ret; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * cpu_function_call - call a function on the cpu |
| 104 | * @func: the function to be called |
| 105 | * @info: the function call argument |
| 106 | * |
| 107 | * Calls the function @func on the remote cpu. |
| 108 | * |
| 109 | * returns: @func return value or -ENXIO when the cpu is offline |
| 110 | */ |
| 111 | static int cpu_function_call(int cpu, int (*func) (void *info), void *info) |
| 112 | { |
| 113 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 114 | .p = NULL, |
| 115 | .func = func, |
| 116 | .info = info, |
| 117 | .ret = -ENXIO, /* No such CPU */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | smp_call_function_single(cpu, remote_function, &data, 1); |
| 121 | |
| 122 | return data.ret; |
| 123 | } |
| 124 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 125 | #define EVENT_OWNER_KERNEL ((void *) -1) |
| 126 | |
| 127 | static bool is_kernel_event(struct perf_event *event) |
| 128 | { |
| 129 | return event->owner == EVENT_OWNER_KERNEL; |
| 130 | } |
| 131 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 132 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
| 133 | PERF_FLAG_FD_OUTPUT |\ |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 134 | PERF_FLAG_PID_CGROUP |\ |
| 135 | PERF_FLAG_FD_CLOEXEC) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 136 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 137 | /* |
| 138 | * branch priv levels that need permission checks |
| 139 | */ |
| 140 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ |
| 141 | (PERF_SAMPLE_BRANCH_KERNEL |\ |
| 142 | PERF_SAMPLE_BRANCH_HV) |
| 143 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 144 | enum event_type_t { |
| 145 | EVENT_FLEXIBLE = 0x1, |
| 146 | EVENT_PINNED = 0x2, |
| 147 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
| 148 | }; |
| 149 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 150 | /* |
| 151 | * perf_sched_events : >0 events exist |
| 152 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu |
| 153 | */ |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 154 | struct static_key_deferred perf_sched_events __read_mostly; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 155 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 156 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 157 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 158 | static atomic_t nr_mmap_events __read_mostly; |
| 159 | static atomic_t nr_comm_events __read_mostly; |
| 160 | static atomic_t nr_task_events __read_mostly; |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 161 | static atomic_t nr_freq_events __read_mostly; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 162 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 163 | static LIST_HEAD(pmus); |
| 164 | static DEFINE_MUTEX(pmus_lock); |
| 165 | static struct srcu_struct pmus_srcu; |
| 166 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 167 | /* |
| 168 | * perf event paranoia level: |
| 169 | * -1 - not paranoid at all |
| 170 | * 0 - disallow raw tracepoint access for unpriv |
| 171 | * 1 - disallow cpu events for unpriv |
| 172 | * 2 - disallow kernel profiling for unpriv |
| 173 | */ |
| 174 | int sysctl_perf_event_paranoid __read_mostly = 1; |
| 175 | |
Frederic Weisbecker | 2044338 | 2011-03-31 03:33:29 +0200 | [diff] [blame] | 176 | /* Minimum for 512 kiB + 1 user control page */ |
| 177 | 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] | 178 | |
| 179 | /* |
| 180 | * max perf event sample rate |
| 181 | */ |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 182 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
| 183 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) |
| 184 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 |
| 185 | |
| 186 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; |
| 187 | |
| 188 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); |
| 189 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; |
| 190 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 191 | static int perf_sample_allowed_ns __read_mostly = |
| 192 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 193 | |
| 194 | void update_perf_cpu_limits(void) |
| 195 | { |
| 196 | u64 tmp = perf_sample_period_ns; |
| 197 | |
| 198 | tmp *= sysctl_perf_cpu_time_max_percent; |
Stephane Eranian | e530292 | 2013-07-05 00:30:11 +0200 | [diff] [blame] | 199 | do_div(tmp, 100); |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 200 | ACCESS_ONCE(perf_sample_allowed_ns) = tmp; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 201 | } |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 202 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 203 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
| 204 | |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 205 | int perf_proc_update_handler(struct ctl_table *table, int write, |
| 206 | void __user *buffer, size_t *lenp, |
| 207 | loff_t *ppos) |
| 208 | { |
Knut Petersen | 723478c | 2013-09-25 14:29:37 +0200 | [diff] [blame] | 209 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 210 | |
| 211 | if (ret || !write) |
| 212 | return ret; |
| 213 | |
| 214 | 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] | 215 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 216 | update_perf_cpu_limits(); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 217 | |
| 218 | return 0; |
| 219 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 220 | |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 221 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; |
| 222 | |
| 223 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, |
| 224 | void __user *buffer, size_t *lenp, |
| 225 | loff_t *ppos) |
| 226 | { |
| 227 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 228 | |
| 229 | if (ret || !write) |
| 230 | return ret; |
| 231 | |
| 232 | update_perf_cpu_limits(); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * perf samples are done in some very critical code paths (NMIs). |
| 239 | * If they take too much CPU time, the system can lock up and not |
| 240 | * get any real work done. This will drop the sample rate when |
| 241 | * we detect that events are taking too long. |
| 242 | */ |
| 243 | #define NR_ACCUMULATED_SAMPLES 128 |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 244 | static DEFINE_PER_CPU(u64, running_sample_length); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 245 | |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 246 | static void perf_duration_warn(struct irq_work *w) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 247 | { |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 248 | u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 249 | u64 avg_local_sample_len; |
Stephane Eranian | e530292 | 2013-07-05 00:30:11 +0200 | [diff] [blame] | 250 | u64 local_samples_len; |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 251 | |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 252 | local_samples_len = __this_cpu_read(running_sample_length); |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 253 | avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES; |
| 254 | |
| 255 | printk_ratelimited(KERN_WARNING |
| 256 | "perf interrupt took too long (%lld > %lld), lowering " |
| 257 | "kernel.perf_event_max_sample_rate to %d\n", |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 258 | avg_local_sample_len, allowed_ns >> 1, |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 259 | sysctl_perf_event_sample_rate); |
| 260 | } |
| 261 | |
| 262 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); |
| 263 | |
| 264 | void perf_sample_event_took(u64 sample_len_ns) |
| 265 | { |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 266 | u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns); |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 267 | u64 avg_local_sample_len; |
| 268 | u64 local_samples_len; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 269 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 270 | if (allowed_ns == 0) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 271 | return; |
| 272 | |
| 273 | /* decay the counter by 1 average sample */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 274 | local_samples_len = __this_cpu_read(running_sample_length); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 275 | local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES; |
| 276 | local_samples_len += sample_len_ns; |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 277 | __this_cpu_write(running_sample_length, local_samples_len); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * note: this will be biased artifically low until we have |
| 281 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us |
| 282 | * from having to maintain a count. |
| 283 | */ |
| 284 | avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES; |
| 285 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 286 | if (avg_local_sample_len <= allowed_ns) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 287 | return; |
| 288 | |
| 289 | if (max_samples_per_tick <= 1) |
| 290 | return; |
| 291 | |
| 292 | max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2); |
| 293 | sysctl_perf_event_sample_rate = max_samples_per_tick * HZ; |
| 294 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 295 | |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 296 | update_perf_cpu_limits(); |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 297 | |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 298 | if (!irq_work_queue(&perf_duration_work)) { |
| 299 | early_printk("perf interrupt took too long (%lld > %lld), lowering " |
| 300 | "kernel.perf_event_max_sample_rate to %d\n", |
| 301 | avg_local_sample_len, allowed_ns >> 1, |
| 302 | sysctl_perf_event_sample_rate); |
| 303 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 306 | static atomic64_t perf_event_id; |
| 307 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 308 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 309 | enum event_type_t event_type); |
| 310 | |
| 311 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 312 | enum event_type_t event_type, |
| 313 | struct task_struct *task); |
| 314 | |
| 315 | static void update_context_time(struct perf_event_context *ctx); |
| 316 | static u64 perf_event_time(struct perf_event *event); |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 317 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 318 | void __weak perf_event_print_debug(void) { } |
| 319 | |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 320 | extern __weak const char *perf_pmu_name(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 321 | { |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 322 | return "pmu"; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 325 | static inline u64 perf_clock(void) |
| 326 | { |
| 327 | return local_clock(); |
| 328 | } |
| 329 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 330 | static inline struct perf_cpu_context * |
| 331 | __get_cpu_context(struct perf_event_context *ctx) |
| 332 | { |
| 333 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); |
| 334 | } |
| 335 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 336 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, |
| 337 | struct perf_event_context *ctx) |
| 338 | { |
| 339 | raw_spin_lock(&cpuctx->ctx.lock); |
| 340 | if (ctx) |
| 341 | raw_spin_lock(&ctx->lock); |
| 342 | } |
| 343 | |
| 344 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, |
| 345 | struct perf_event_context *ctx) |
| 346 | { |
| 347 | if (ctx) |
| 348 | raw_spin_unlock(&ctx->lock); |
| 349 | raw_spin_unlock(&cpuctx->ctx.lock); |
| 350 | } |
| 351 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 352 | #ifdef CONFIG_CGROUP_PERF |
| 353 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 354 | static inline bool |
| 355 | perf_cgroup_match(struct perf_event *event) |
| 356 | { |
| 357 | struct perf_event_context *ctx = event->ctx; |
| 358 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 359 | |
Tejun Heo | ef824fa | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 360 | /* @event doesn't care about cgroup */ |
| 361 | if (!event->cgrp) |
| 362 | return true; |
| 363 | |
| 364 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ |
| 365 | if (!cpuctx->cgrp) |
| 366 | return false; |
| 367 | |
| 368 | /* |
| 369 | * Cgroup scoping is recursive. An event enabled for a cgroup is |
| 370 | * also enabled for all its descendant cgroups. If @cpuctx's |
| 371 | * cgroup is a descendant of @event's (the test covers identity |
| 372 | * case), it's a match. |
| 373 | */ |
| 374 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, |
| 375 | event->cgrp->css.cgroup); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 378 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 379 | { |
Zefan Li | 4e2ba65 | 2014-09-19 16:53:14 +0800 | [diff] [blame] | 380 | css_put(&event->cgrp->css); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 381 | event->cgrp = NULL; |
| 382 | } |
| 383 | |
| 384 | static inline int is_cgroup_event(struct perf_event *event) |
| 385 | { |
| 386 | return event->cgrp != NULL; |
| 387 | } |
| 388 | |
| 389 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 390 | { |
| 391 | struct perf_cgroup_info *t; |
| 392 | |
| 393 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 394 | return t->time; |
| 395 | } |
| 396 | |
| 397 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) |
| 398 | { |
| 399 | struct perf_cgroup_info *info; |
| 400 | u64 now; |
| 401 | |
| 402 | now = perf_clock(); |
| 403 | |
| 404 | info = this_cpu_ptr(cgrp->info); |
| 405 | |
| 406 | info->time += now - info->timestamp; |
| 407 | info->timestamp = now; |
| 408 | } |
| 409 | |
| 410 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 411 | { |
| 412 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; |
| 413 | if (cgrp_out) |
| 414 | __update_cgrp_time(cgrp_out); |
| 415 | } |
| 416 | |
| 417 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 418 | { |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 419 | struct perf_cgroup *cgrp; |
| 420 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 421 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 422 | * ensure we access cgroup data only when needed and |
| 423 | * when we know the cgroup is pinned (css_get) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 424 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 425 | if (!is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 426 | return; |
| 427 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 428 | cgrp = perf_cgroup_from_task(current); |
| 429 | /* |
| 430 | * Do not update time when cgroup is not active |
| 431 | */ |
| 432 | if (cgrp == event->cgrp) |
| 433 | __update_cgrp_time(event->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 437 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 438 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 439 | { |
| 440 | struct perf_cgroup *cgrp; |
| 441 | struct perf_cgroup_info *info; |
| 442 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 443 | /* |
| 444 | * ctx->lock held by caller |
| 445 | * ensure we do not access cgroup data |
| 446 | * unless we have the cgroup pinned (css_get) |
| 447 | */ |
| 448 | if (!task || !ctx->nr_cgroups) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 449 | return; |
| 450 | |
| 451 | cgrp = perf_cgroup_from_task(task); |
| 452 | info = this_cpu_ptr(cgrp->info); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 453 | info->timestamp = ctx->timestamp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
| 457 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ |
| 458 | |
| 459 | /* |
| 460 | * reschedule events based on the cgroup constraint of task. |
| 461 | * |
| 462 | * mode SWOUT : schedule out everything |
| 463 | * mode SWIN : schedule in based on cgroup for next |
| 464 | */ |
| 465 | void perf_cgroup_switch(struct task_struct *task, int mode) |
| 466 | { |
| 467 | struct perf_cpu_context *cpuctx; |
| 468 | struct pmu *pmu; |
| 469 | unsigned long flags; |
| 470 | |
| 471 | /* |
| 472 | * disable interrupts to avoid geting nr_cgroup |
| 473 | * changes via __perf_event_disable(). Also |
| 474 | * avoids preemption. |
| 475 | */ |
| 476 | local_irq_save(flags); |
| 477 | |
| 478 | /* |
| 479 | * we reschedule only in the presence of cgroup |
| 480 | * constrained events. |
| 481 | */ |
| 482 | rcu_read_lock(); |
| 483 | |
| 484 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 485 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 486 | if (cpuctx->unique_pmu != pmu) |
| 487 | continue; /* ensure we process each cpuctx once */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 488 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 489 | /* |
| 490 | * perf_cgroup_events says at least one |
| 491 | * context on this CPU has cgroup events. |
| 492 | * |
| 493 | * ctx->nr_cgroups reports the number of cgroup |
| 494 | * events for a context. |
| 495 | */ |
| 496 | if (cpuctx->ctx.nr_cgroups > 0) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 497 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 498 | perf_pmu_disable(cpuctx->ctx.pmu); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 499 | |
| 500 | if (mode & PERF_CGROUP_SWOUT) { |
| 501 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 502 | /* |
| 503 | * must not be done before ctxswout due |
| 504 | * to event_filter_match() in event_sched_out() |
| 505 | */ |
| 506 | cpuctx->cgrp = NULL; |
| 507 | } |
| 508 | |
| 509 | if (mode & PERF_CGROUP_SWIN) { |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 510 | WARN_ON_ONCE(cpuctx->cgrp); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 511 | /* |
| 512 | * set cgrp before ctxsw in to allow |
| 513 | * event_filter_match() to not have to pass |
| 514 | * task around |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 515 | */ |
| 516 | cpuctx->cgrp = perf_cgroup_from_task(task); |
| 517 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
| 518 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 519 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 520 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 521 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | rcu_read_unlock(); |
| 525 | |
| 526 | local_irq_restore(flags); |
| 527 | } |
| 528 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 529 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 530 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 531 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 532 | struct perf_cgroup *cgrp1; |
| 533 | struct perf_cgroup *cgrp2 = NULL; |
| 534 | |
| 535 | /* |
| 536 | * we come here when we know perf_cgroup_events > 0 |
| 537 | */ |
| 538 | cgrp1 = perf_cgroup_from_task(task); |
| 539 | |
| 540 | /* |
| 541 | * next is NULL when called from perf_event_enable_on_exec() |
| 542 | * that will systematically cause a cgroup_switch() |
| 543 | */ |
| 544 | if (next) |
| 545 | cgrp2 = perf_cgroup_from_task(next); |
| 546 | |
| 547 | /* |
| 548 | * only schedule out current cgroup events if we know |
| 549 | * that we are switching to a different cgroup. Otherwise, |
| 550 | * do no touch the cgroup events. |
| 551 | */ |
| 552 | if (cgrp1 != cgrp2) |
| 553 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 556 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 557 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 558 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 559 | struct perf_cgroup *cgrp1; |
| 560 | struct perf_cgroup *cgrp2 = NULL; |
| 561 | |
| 562 | /* |
| 563 | * we come here when we know perf_cgroup_events > 0 |
| 564 | */ |
| 565 | cgrp1 = perf_cgroup_from_task(task); |
| 566 | |
| 567 | /* prev can never be NULL */ |
| 568 | cgrp2 = perf_cgroup_from_task(prev); |
| 569 | |
| 570 | /* |
| 571 | * only need to schedule in cgroup events if we are changing |
| 572 | * cgroup during ctxsw. Cgroup events were not scheduled |
| 573 | * out of ctxsw out if that was not the case. |
| 574 | */ |
| 575 | if (cgrp1 != cgrp2) |
| 576 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
| 580 | struct perf_event_attr *attr, |
| 581 | struct perf_event *group_leader) |
| 582 | { |
| 583 | struct perf_cgroup *cgrp; |
| 584 | struct cgroup_subsys_state *css; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 585 | struct fd f = fdget(fd); |
| 586 | int ret = 0; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 587 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 588 | if (!f.file) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 589 | return -EBADF; |
| 590 | |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 591 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 592 | &perf_event_cgrp_subsys); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 593 | if (IS_ERR(css)) { |
| 594 | ret = PTR_ERR(css); |
| 595 | goto out; |
| 596 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 597 | |
| 598 | cgrp = container_of(css, struct perf_cgroup, css); |
| 599 | event->cgrp = cgrp; |
| 600 | |
| 601 | /* |
| 602 | * all events in a group must monitor |
| 603 | * the same cgroup because a task belongs |
| 604 | * to only one perf cgroup at a time |
| 605 | */ |
| 606 | if (group_leader && group_leader->cgrp != cgrp) { |
| 607 | perf_detach_cgroup(event); |
| 608 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 609 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 610 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 611 | fdput(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 612 | return ret; |
| 613 | } |
| 614 | |
| 615 | static inline void |
| 616 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 617 | { |
| 618 | struct perf_cgroup_info *t; |
| 619 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 620 | event->shadow_ctx_time = now - t->timestamp; |
| 621 | } |
| 622 | |
| 623 | static inline void |
| 624 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 625 | { |
| 626 | /* |
| 627 | * when the current task's perf cgroup does not match |
| 628 | * the event's, we need to remember to call the |
| 629 | * perf_mark_enable() function the first time a task with |
| 630 | * a matching perf cgroup is scheduled in. |
| 631 | */ |
| 632 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 633 | event->cgrp_defer_enabled = 1; |
| 634 | } |
| 635 | |
| 636 | static inline void |
| 637 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 638 | struct perf_event_context *ctx) |
| 639 | { |
| 640 | struct perf_event *sub; |
| 641 | u64 tstamp = perf_event_time(event); |
| 642 | |
| 643 | if (!event->cgrp_defer_enabled) |
| 644 | return; |
| 645 | |
| 646 | event->cgrp_defer_enabled = 0; |
| 647 | |
| 648 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 649 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 650 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 651 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 652 | sub->cgrp_defer_enabled = 0; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | #else /* !CONFIG_CGROUP_PERF */ |
| 657 | |
| 658 | static inline bool |
| 659 | perf_cgroup_match(struct perf_event *event) |
| 660 | { |
| 661 | return true; |
| 662 | } |
| 663 | |
| 664 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 665 | {} |
| 666 | |
| 667 | static inline int is_cgroup_event(struct perf_event *event) |
| 668 | { |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 673 | { |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 678 | { |
| 679 | } |
| 680 | |
| 681 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 682 | { |
| 683 | } |
| 684 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 685 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 686 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 687 | { |
| 688 | } |
| 689 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 690 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 691 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 692 | { |
| 693 | } |
| 694 | |
| 695 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 696 | struct perf_event_attr *attr, |
| 697 | struct perf_event *group_leader) |
| 698 | { |
| 699 | return -EINVAL; |
| 700 | } |
| 701 | |
| 702 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 703 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 704 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 705 | { |
| 706 | } |
| 707 | |
| 708 | void |
| 709 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 710 | { |
| 711 | } |
| 712 | |
| 713 | static inline void |
| 714 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 715 | { |
| 716 | } |
| 717 | |
| 718 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 719 | { |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static inline void |
| 724 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 725 | { |
| 726 | } |
| 727 | |
| 728 | static inline void |
| 729 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 730 | struct perf_event_context *ctx) |
| 731 | { |
| 732 | } |
| 733 | #endif |
| 734 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 735 | /* |
| 736 | * set default to be dependent on timer tick just |
| 737 | * like original code |
| 738 | */ |
| 739 | #define PERF_CPU_HRTIMER (1000 / HZ) |
| 740 | /* |
| 741 | * function must be called with interrupts disbled |
| 742 | */ |
| 743 | static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr) |
| 744 | { |
| 745 | struct perf_cpu_context *cpuctx; |
| 746 | enum hrtimer_restart ret = HRTIMER_NORESTART; |
| 747 | int rotations = 0; |
| 748 | |
| 749 | WARN_ON(!irqs_disabled()); |
| 750 | |
| 751 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); |
| 752 | |
| 753 | rotations = perf_rotate_context(cpuctx); |
| 754 | |
| 755 | /* |
| 756 | * arm timer if needed |
| 757 | */ |
| 758 | if (rotations) { |
| 759 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
| 760 | ret = HRTIMER_RESTART; |
| 761 | } |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | /* CPU is going down */ |
| 767 | void perf_cpu_hrtimer_cancel(int cpu) |
| 768 | { |
| 769 | struct perf_cpu_context *cpuctx; |
| 770 | struct pmu *pmu; |
| 771 | unsigned long flags; |
| 772 | |
| 773 | if (WARN_ON(cpu != smp_processor_id())) |
| 774 | return; |
| 775 | |
| 776 | local_irq_save(flags); |
| 777 | |
| 778 | rcu_read_lock(); |
| 779 | |
| 780 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 781 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 782 | |
| 783 | if (pmu->task_ctx_nr == perf_sw_context) |
| 784 | continue; |
| 785 | |
| 786 | hrtimer_cancel(&cpuctx->hrtimer); |
| 787 | } |
| 788 | |
| 789 | rcu_read_unlock(); |
| 790 | |
| 791 | local_irq_restore(flags); |
| 792 | } |
| 793 | |
| 794 | static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
| 795 | { |
| 796 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 797 | struct pmu *pmu = cpuctx->ctx.pmu; |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 798 | int timer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 799 | |
| 800 | /* no multiplexing needed for SW PMU */ |
| 801 | if (pmu->task_ctx_nr == perf_sw_context) |
| 802 | return; |
| 803 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 804 | /* |
| 805 | * check default is sane, if not set then force to |
| 806 | * default interval (1/tick) |
| 807 | */ |
| 808 | timer = pmu->hrtimer_interval_ms; |
| 809 | if (timer < 1) |
| 810 | timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; |
| 811 | |
| 812 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 813 | |
| 814 | hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
| 815 | hr->function = perf_cpu_hrtimer_handler; |
| 816 | } |
| 817 | |
| 818 | static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx) |
| 819 | { |
| 820 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 821 | struct pmu *pmu = cpuctx->ctx.pmu; |
| 822 | |
| 823 | /* not for SW PMU */ |
| 824 | if (pmu->task_ctx_nr == perf_sw_context) |
| 825 | return; |
| 826 | |
| 827 | if (hrtimer_active(hr)) |
| 828 | return; |
| 829 | |
| 830 | if (!hrtimer_callback_running(hr)) |
| 831 | __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval, |
| 832 | 0, HRTIMER_MODE_REL_PINNED, 0); |
| 833 | } |
| 834 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 835 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 836 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 837 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 838 | if (!(*count)++) |
| 839 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 840 | } |
| 841 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 842 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 843 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 844 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 845 | if (!--(*count)) |
| 846 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 849 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 850 | |
| 851 | /* |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 852 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
| 853 | * perf_event_task_tick() are fully serialized because they're strictly cpu |
| 854 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs |
| 855 | * disabled, while perf_event_task_tick is called from IRQ context. |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 856 | */ |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 857 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 858 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 859 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 860 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 861 | WARN_ON(!irqs_disabled()); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 862 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 863 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
| 864 | |
| 865 | list_add(&ctx->active_ctx_list, head); |
| 866 | } |
| 867 | |
| 868 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) |
| 869 | { |
| 870 | WARN_ON(!irqs_disabled()); |
| 871 | |
| 872 | WARN_ON(list_empty(&ctx->active_ctx_list)); |
| 873 | |
| 874 | list_del_init(&ctx->active_ctx_list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static void get_ctx(struct perf_event_context *ctx) |
| 878 | { |
| 879 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 880 | } |
| 881 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 882 | static void free_ctx(struct rcu_head *head) |
| 883 | { |
| 884 | struct perf_event_context *ctx; |
| 885 | |
| 886 | ctx = container_of(head, struct perf_event_context, rcu_head); |
| 887 | kfree(ctx->task_ctx_data); |
| 888 | kfree(ctx); |
| 889 | } |
| 890 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 891 | static void put_ctx(struct perf_event_context *ctx) |
| 892 | { |
| 893 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 894 | if (ctx->parent_ctx) |
| 895 | put_ctx(ctx->parent_ctx); |
| 896 | if (ctx->task) |
| 897 | put_task_struct(ctx->task); |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 898 | call_rcu(&ctx->rcu_head, free_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 902 | /* |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 903 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and |
| 904 | * perf_pmu_migrate_context() we need some magic. |
| 905 | * |
| 906 | * Those places that change perf_event::ctx will hold both |
| 907 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. |
| 908 | * |
| 909 | * Lock ordering is by mutex address. There is one other site where |
| 910 | * perf_event_context::mutex nests and that is put_event(). But remember that |
| 911 | * that is a parent<->child context relation, and migration does not affect |
| 912 | * children, therefore these two orderings should not interact. |
| 913 | * |
| 914 | * The change in perf_event::ctx does not affect children (as claimed above) |
| 915 | * because the sys_perf_event_open() case will install a new event and break |
| 916 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only |
| 917 | * concerned with cpuctx and that doesn't have children. |
| 918 | * |
| 919 | * The places that change perf_event::ctx will issue: |
| 920 | * |
| 921 | * perf_remove_from_context(); |
| 922 | * synchronize_rcu(); |
| 923 | * perf_install_in_context(); |
| 924 | * |
| 925 | * to affect the change. The remove_from_context() + synchronize_rcu() should |
| 926 | * quiesce the event, after which we can install it in the new location. This |
| 927 | * means that only external vectors (perf_fops, prctl) can perturb the event |
| 928 | * while in transit. Therefore all such accessors should also acquire |
| 929 | * perf_event_context::mutex to serialize against this. |
| 930 | * |
| 931 | * However; because event->ctx can change while we're waiting to acquire |
| 932 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() |
| 933 | * function. |
| 934 | * |
| 935 | * Lock order: |
| 936 | * task_struct::perf_event_mutex |
| 937 | * perf_event_context::mutex |
| 938 | * perf_event_context::lock |
| 939 | * perf_event::child_mutex; |
| 940 | * perf_event::mmap_mutex |
| 941 | * mmap_sem |
| 942 | */ |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 943 | static struct perf_event_context * |
| 944 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 945 | { |
| 946 | struct perf_event_context *ctx; |
| 947 | |
| 948 | again: |
| 949 | rcu_read_lock(); |
| 950 | ctx = ACCESS_ONCE(event->ctx); |
| 951 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
| 952 | rcu_read_unlock(); |
| 953 | goto again; |
| 954 | } |
| 955 | rcu_read_unlock(); |
| 956 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 957 | mutex_lock_nested(&ctx->mutex, nesting); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 958 | if (event->ctx != ctx) { |
| 959 | mutex_unlock(&ctx->mutex); |
| 960 | put_ctx(ctx); |
| 961 | goto again; |
| 962 | } |
| 963 | |
| 964 | return ctx; |
| 965 | } |
| 966 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 967 | static inline struct perf_event_context * |
| 968 | perf_event_ctx_lock(struct perf_event *event) |
| 969 | { |
| 970 | return perf_event_ctx_lock_nested(event, 0); |
| 971 | } |
| 972 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 973 | static void perf_event_ctx_unlock(struct perf_event *event, |
| 974 | struct perf_event_context *ctx) |
| 975 | { |
| 976 | mutex_unlock(&ctx->mutex); |
| 977 | put_ctx(ctx); |
| 978 | } |
| 979 | |
| 980 | /* |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 981 | * This must be done under the ctx->lock, such as to serialize against |
| 982 | * context_equiv(), therefore we cannot call put_ctx() since that might end up |
| 983 | * calling scheduler related locks and ctx->lock nests inside those. |
| 984 | */ |
| 985 | static __must_check struct perf_event_context * |
| 986 | unclone_ctx(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 987 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 988 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
| 989 | |
| 990 | lockdep_assert_held(&ctx->lock); |
| 991 | |
| 992 | if (parent_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 993 | ctx->parent_ctx = NULL; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 994 | ctx->generation++; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 995 | |
| 996 | return parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 997 | } |
| 998 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 999 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 1000 | { |
| 1001 | /* |
| 1002 | * only top level events have the pid namespace they were created in |
| 1003 | */ |
| 1004 | if (event->parent) |
| 1005 | event = event->parent; |
| 1006 | |
| 1007 | return task_tgid_nr_ns(p, event->ns); |
| 1008 | } |
| 1009 | |
| 1010 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 1011 | { |
| 1012 | /* |
| 1013 | * only top level events have the pid namespace they were created in |
| 1014 | */ |
| 1015 | if (event->parent) |
| 1016 | event = event->parent; |
| 1017 | |
| 1018 | return task_pid_nr_ns(p, event->ns); |
| 1019 | } |
| 1020 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1021 | /* |
| 1022 | * If we inherit events we want to return the parent event id |
| 1023 | * to userspace. |
| 1024 | */ |
| 1025 | static u64 primary_event_id(struct perf_event *event) |
| 1026 | { |
| 1027 | u64 id = event->id; |
| 1028 | |
| 1029 | if (event->parent) |
| 1030 | id = event->parent->id; |
| 1031 | |
| 1032 | return id; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Get the perf_event_context for a task and lock it. |
| 1037 | * This has to cope with with the fact that until it is locked, |
| 1038 | * the context could get moved to another task. |
| 1039 | */ |
| 1040 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1041 | 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] | 1042 | { |
| 1043 | struct perf_event_context *ctx; |
| 1044 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1045 | retry: |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1046 | /* |
| 1047 | * One of the few rules of preemptible RCU is that one cannot do |
| 1048 | * rcu_read_unlock() while holding a scheduler (or nested) lock when |
| 1049 | * part of the read side critical section was preemptible -- see |
| 1050 | * rcu_read_unlock_special(). |
| 1051 | * |
| 1052 | * Since ctx->lock nests under rq->lock we must ensure the entire read |
| 1053 | * side critical section is non-preemptible. |
| 1054 | */ |
| 1055 | preempt_disable(); |
| 1056 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1057 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1058 | if (ctx) { |
| 1059 | /* |
| 1060 | * If this context is a clone of another, it might |
| 1061 | * get swapped for another underneath us by |
| 1062 | * perf_event_task_sched_out, though the |
| 1063 | * rcu_read_lock() protects us from any context |
| 1064 | * getting freed. Lock the context and check if it |
| 1065 | * got swapped before we could get the lock, and retry |
| 1066 | * if so. If we locked the right context, then it |
| 1067 | * can't get swapped on us any more. |
| 1068 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1069 | raw_spin_lock_irqsave(&ctx->lock, *flags); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1070 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1071 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1072 | rcu_read_unlock(); |
| 1073 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1074 | goto retry; |
| 1075 | } |
| 1076 | |
| 1077 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1078 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1079 | ctx = NULL; |
| 1080 | } |
| 1081 | } |
| 1082 | rcu_read_unlock(); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1083 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1084 | return ctx; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * Get the context for a task and increment its pin_count so it |
| 1089 | * can't get swapped to another task. This also increments its |
| 1090 | * reference count so that the context can't get freed. |
| 1091 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1092 | static struct perf_event_context * |
| 1093 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1094 | { |
| 1095 | struct perf_event_context *ctx; |
| 1096 | unsigned long flags; |
| 1097 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1098 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1099 | if (ctx) { |
| 1100 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1101 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1102 | } |
| 1103 | return ctx; |
| 1104 | } |
| 1105 | |
| 1106 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 1107 | { |
| 1108 | unsigned long flags; |
| 1109 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1110 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1111 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1112 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1115 | /* |
| 1116 | * Update the record of the current time in a context. |
| 1117 | */ |
| 1118 | static void update_context_time(struct perf_event_context *ctx) |
| 1119 | { |
| 1120 | u64 now = perf_clock(); |
| 1121 | |
| 1122 | ctx->time += now - ctx->timestamp; |
| 1123 | ctx->timestamp = now; |
| 1124 | } |
| 1125 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1126 | static u64 perf_event_time(struct perf_event *event) |
| 1127 | { |
| 1128 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1129 | |
| 1130 | if (is_cgroup_event(event)) |
| 1131 | return perf_cgroup_event_time(event); |
| 1132 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1133 | return ctx ? ctx->time : 0; |
| 1134 | } |
| 1135 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1136 | /* |
| 1137 | * 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] | 1138 | * The caller of this function needs to hold the ctx->lock. |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1139 | */ |
| 1140 | static void update_event_times(struct perf_event *event) |
| 1141 | { |
| 1142 | struct perf_event_context *ctx = event->ctx; |
| 1143 | u64 run_end; |
| 1144 | |
| 1145 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1146 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1147 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1148 | /* |
| 1149 | * in cgroup mode, time_enabled represents |
| 1150 | * the time the event was enabled AND active |
| 1151 | * tasks were in the monitored cgroup. This is |
| 1152 | * independent of the activity of the context as |
| 1153 | * there may be a mix of cgroup and non-cgroup events. |
| 1154 | * |
| 1155 | * That is why we treat cgroup events differently |
| 1156 | * here. |
| 1157 | */ |
| 1158 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1159 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1160 | else if (ctx->is_active) |
| 1161 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1162 | else |
| 1163 | run_end = event->tstamp_stopped; |
| 1164 | |
| 1165 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1166 | |
| 1167 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1168 | run_end = event->tstamp_stopped; |
| 1169 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1170 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1171 | |
| 1172 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1173 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1174 | } |
| 1175 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1176 | /* |
| 1177 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1178 | */ |
| 1179 | static void update_group_times(struct perf_event *leader) |
| 1180 | { |
| 1181 | struct perf_event *event; |
| 1182 | |
| 1183 | update_event_times(leader); |
| 1184 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1185 | update_event_times(event); |
| 1186 | } |
| 1187 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1188 | static struct list_head * |
| 1189 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1190 | { |
| 1191 | if (event->attr.pinned) |
| 1192 | return &ctx->pinned_groups; |
| 1193 | else |
| 1194 | return &ctx->flexible_groups; |
| 1195 | } |
| 1196 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1197 | /* |
| 1198 | * Add a event from the lists for its context. |
| 1199 | * Must be called with ctx->mutex and ctx->lock held. |
| 1200 | */ |
| 1201 | static void |
| 1202 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1203 | { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1204 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1205 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1206 | |
| 1207 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1208 | * If we're a stand alone event or group leader, we go to the context |
| 1209 | * list, group events are kept attached to the group so that |
| 1210 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1211 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1212 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1213 | struct list_head *list; |
| 1214 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1215 | if (is_software_event(event)) |
| 1216 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 1217 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1218 | list = ctx_group_list(event, ctx); |
| 1219 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1220 | } |
| 1221 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 1222 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1223 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1224 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1225 | list_add_rcu(&event->event_entry, &ctx->event_list); |
| 1226 | ctx->nr_events++; |
| 1227 | if (event->attr.inherit_stat) |
| 1228 | ctx->nr_stat++; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1229 | |
| 1230 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1231 | } |
| 1232 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1233 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1234 | * Initialize event state based on the perf_event_attr::disabled. |
| 1235 | */ |
| 1236 | static inline void perf_event__state_init(struct perf_event *event) |
| 1237 | { |
| 1238 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1239 | PERF_EVENT_STATE_INACTIVE; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1243 | * Called at perf_event creation and when events are attached/detached from a |
| 1244 | * group. |
| 1245 | */ |
| 1246 | static void perf_event__read_size(struct perf_event *event) |
| 1247 | { |
| 1248 | int entry = sizeof(u64); /* value */ |
| 1249 | int size = 0; |
| 1250 | int nr = 1; |
| 1251 | |
| 1252 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1253 | size += sizeof(u64); |
| 1254 | |
| 1255 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1256 | size += sizeof(u64); |
| 1257 | |
| 1258 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1259 | entry += sizeof(u64); |
| 1260 | |
| 1261 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
| 1262 | nr += event->group_leader->nr_siblings; |
| 1263 | size += sizeof(u64); |
| 1264 | } |
| 1265 | |
| 1266 | size += entry * nr; |
| 1267 | event->read_size = size; |
| 1268 | } |
| 1269 | |
| 1270 | static void perf_event__header_size(struct perf_event *event) |
| 1271 | { |
| 1272 | struct perf_sample_data *data; |
| 1273 | u64 sample_type = event->attr.sample_type; |
| 1274 | u16 size = 0; |
| 1275 | |
| 1276 | perf_event__read_size(event); |
| 1277 | |
| 1278 | if (sample_type & PERF_SAMPLE_IP) |
| 1279 | size += sizeof(data->ip); |
| 1280 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1281 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1282 | size += sizeof(data->addr); |
| 1283 | |
| 1284 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1285 | size += sizeof(data->period); |
| 1286 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1287 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1288 | size += sizeof(data->weight); |
| 1289 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1290 | if (sample_type & PERF_SAMPLE_READ) |
| 1291 | size += event->read_size; |
| 1292 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1293 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1294 | size += sizeof(data->data_src.val); |
| 1295 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 1296 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1297 | size += sizeof(data->txn); |
| 1298 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1299 | event->header_size = size; |
| 1300 | } |
| 1301 | |
| 1302 | static void perf_event__id_header_size(struct perf_event *event) |
| 1303 | { |
| 1304 | struct perf_sample_data *data; |
| 1305 | u64 sample_type = event->attr.sample_type; |
| 1306 | u16 size = 0; |
| 1307 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1308 | if (sample_type & PERF_SAMPLE_TID) |
| 1309 | size += sizeof(data->tid_entry); |
| 1310 | |
| 1311 | if (sample_type & PERF_SAMPLE_TIME) |
| 1312 | size += sizeof(data->time); |
| 1313 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 1314 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1315 | size += sizeof(data->id); |
| 1316 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1317 | if (sample_type & PERF_SAMPLE_ID) |
| 1318 | size += sizeof(data->id); |
| 1319 | |
| 1320 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1321 | size += sizeof(data->stream_id); |
| 1322 | |
| 1323 | if (sample_type & PERF_SAMPLE_CPU) |
| 1324 | size += sizeof(data->cpu_entry); |
| 1325 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1326 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1329 | static void perf_group_attach(struct perf_event *event) |
| 1330 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1331 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1332 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1333 | /* |
| 1334 | * We can have double attach due to group movement in perf_event_open. |
| 1335 | */ |
| 1336 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1337 | return; |
| 1338 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1339 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1340 | |
| 1341 | if (group_leader == event) |
| 1342 | return; |
| 1343 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1344 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
| 1345 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1346 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 1347 | !is_software_event(event)) |
| 1348 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 1349 | |
| 1350 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1351 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1352 | |
| 1353 | perf_event__header_size(group_leader); |
| 1354 | |
| 1355 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1356 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1357 | } |
| 1358 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1359 | /* |
| 1360 | * Remove a event from the lists for its context. |
| 1361 | * Must be called with ctx->mutex and ctx->lock held. |
| 1362 | */ |
| 1363 | static void |
| 1364 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1365 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1366 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1367 | |
| 1368 | WARN_ON_ONCE(event->ctx != ctx); |
| 1369 | lockdep_assert_held(&ctx->lock); |
| 1370 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1371 | /* |
| 1372 | * We can have double detach due to exit/hot-unplug + close. |
| 1373 | */ |
| 1374 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1375 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1376 | |
| 1377 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1378 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1379 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1380 | ctx->nr_cgroups--; |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1381 | cpuctx = __get_cpu_context(ctx); |
| 1382 | /* |
| 1383 | * if there are no more cgroup events |
| 1384 | * then cler cgrp to avoid stale pointer |
| 1385 | * in update_cgrp_time_from_cpuctx() |
| 1386 | */ |
| 1387 | if (!ctx->nr_cgroups) |
| 1388 | cpuctx->cgrp = NULL; |
| 1389 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1390 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1391 | ctx->nr_events--; |
| 1392 | if (event->attr.inherit_stat) |
| 1393 | ctx->nr_stat--; |
| 1394 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1395 | list_del_rcu(&event->event_entry); |
| 1396 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1397 | if (event->group_leader == event) |
| 1398 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1399 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1400 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1401 | |
| 1402 | /* |
| 1403 | * If event was in error state, then keep it |
| 1404 | * that way, otherwise bogus counts will be |
| 1405 | * returned on read(). The only way to get out |
| 1406 | * of error state is by explicit re-enabling |
| 1407 | * of the event |
| 1408 | */ |
| 1409 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1410 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1411 | |
| 1412 | ctx->generation++; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1413 | } |
| 1414 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1415 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1416 | { |
| 1417 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1418 | struct list_head *list = NULL; |
| 1419 | |
| 1420 | /* |
| 1421 | * We can have double detach due to exit/hot-unplug + close. |
| 1422 | */ |
| 1423 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1424 | return; |
| 1425 | |
| 1426 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1427 | |
| 1428 | /* |
| 1429 | * If this is a sibling, remove it from its group. |
| 1430 | */ |
| 1431 | if (event->group_leader != event) { |
| 1432 | list_del_init(&event->group_entry); |
| 1433 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1434 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | if (!list_empty(&event->group_entry)) |
| 1438 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1439 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1440 | /* |
| 1441 | * If this was a group event with sibling events then |
| 1442 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1443 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1444 | */ |
| 1445 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1446 | if (list) |
| 1447 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1448 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1449 | |
| 1450 | /* Inherit group flags from the previous leader */ |
| 1451 | sibling->group_flags = event->group_flags; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1452 | |
| 1453 | WARN_ON_ONCE(sibling->ctx != event->ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1454 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1455 | |
| 1456 | out: |
| 1457 | perf_event__header_size(event->group_leader); |
| 1458 | |
| 1459 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1460 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1463 | /* |
| 1464 | * User event without the task. |
| 1465 | */ |
| 1466 | static bool is_orphaned_event(struct perf_event *event) |
| 1467 | { |
| 1468 | return event && !is_kernel_event(event) && !event->owner; |
| 1469 | } |
| 1470 | |
| 1471 | /* |
| 1472 | * Event has a parent but parent's task finished and it's |
| 1473 | * alive only because of children holding refference. |
| 1474 | */ |
| 1475 | static bool is_orphaned_child(struct perf_event *event) |
| 1476 | { |
| 1477 | return is_orphaned_event(event->parent); |
| 1478 | } |
| 1479 | |
| 1480 | static void orphans_remove_work(struct work_struct *work); |
| 1481 | |
| 1482 | static void schedule_orphans_remove(struct perf_event_context *ctx) |
| 1483 | { |
| 1484 | if (!ctx->task || ctx->orphans_remove_sched || !perf_wq) |
| 1485 | return; |
| 1486 | |
| 1487 | if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) { |
| 1488 | get_ctx(ctx); |
| 1489 | ctx->orphans_remove_sched = true; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | static int __init perf_workqueue_init(void) |
| 1494 | { |
| 1495 | perf_wq = create_singlethread_workqueue("perf"); |
| 1496 | WARN(!perf_wq, "failed to create perf workqueue\n"); |
| 1497 | return perf_wq ? 0 : -1; |
| 1498 | } |
| 1499 | |
| 1500 | core_initcall(perf_workqueue_init); |
| 1501 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1502 | static inline int |
| 1503 | event_filter_match(struct perf_event *event) |
| 1504 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1505 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
| 1506 | && perf_cgroup_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1509 | static void |
| 1510 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1511 | struct perf_cpu_context *cpuctx, |
| 1512 | struct perf_event_context *ctx) |
| 1513 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1514 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1515 | u64 delta; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1516 | |
| 1517 | WARN_ON_ONCE(event->ctx != ctx); |
| 1518 | lockdep_assert_held(&ctx->lock); |
| 1519 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1520 | /* |
| 1521 | * An event which could not be activated because of |
| 1522 | * filter mismatch still needs to have its timings |
| 1523 | * maintained, otherwise bogus information is return |
| 1524 | * via read() for time_enabled, time_running: |
| 1525 | */ |
| 1526 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1527 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1528 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1529 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1530 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1533 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1534 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1535 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1536 | perf_pmu_disable(event->pmu); |
| 1537 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1538 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1539 | if (event->pending_disable) { |
| 1540 | event->pending_disable = 0; |
| 1541 | event->state = PERF_EVENT_STATE_OFF; |
| 1542 | } |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1543 | event->tstamp_stopped = tstamp; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1544 | event->pmu->del(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1545 | event->oncpu = -1; |
| 1546 | |
| 1547 | if (!is_software_event(event)) |
| 1548 | cpuctx->active_oncpu--; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1549 | if (!--ctx->nr_active) |
| 1550 | perf_event_ctx_deactivate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1551 | if (event->attr.freq && event->attr.sample_freq) |
| 1552 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1553 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1554 | cpuctx->exclusive = 0; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1555 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1556 | if (is_orphaned_child(event)) |
| 1557 | schedule_orphans_remove(ctx); |
| 1558 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1559 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | static void |
| 1563 | group_sched_out(struct perf_event *group_event, |
| 1564 | struct perf_cpu_context *cpuctx, |
| 1565 | struct perf_event_context *ctx) |
| 1566 | { |
| 1567 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1568 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1569 | |
| 1570 | event_sched_out(group_event, cpuctx, ctx); |
| 1571 | |
| 1572 | /* |
| 1573 | * Schedule out siblings (if any): |
| 1574 | */ |
| 1575 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1576 | event_sched_out(event, cpuctx, ctx); |
| 1577 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1578 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1579 | cpuctx->exclusive = 0; |
| 1580 | } |
| 1581 | |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1582 | struct remove_event { |
| 1583 | struct perf_event *event; |
| 1584 | bool detach_group; |
| 1585 | }; |
| 1586 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1587 | /* |
| 1588 | * Cross CPU call to remove a performance event |
| 1589 | * |
| 1590 | * We disable the event on the hardware level first. After that we |
| 1591 | * remove it from the context list. |
| 1592 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1593 | static int __perf_remove_from_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1594 | { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1595 | struct remove_event *re = info; |
| 1596 | struct perf_event *event = re->event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1597 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1598 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1599 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1600 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1601 | event_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1602 | if (re->detach_group) |
| 1603 | perf_group_detach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1604 | list_del_event(event, ctx); |
Peter Zijlstra | 64ce312 | 2011-04-09 21:17:48 +0200 | [diff] [blame] | 1605 | if (!ctx->nr_events && cpuctx->task_ctx == ctx) { |
| 1606 | ctx->is_active = 0; |
| 1607 | cpuctx->task_ctx = NULL; |
| 1608 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1609 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1610 | |
| 1611 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | |
| 1615 | /* |
| 1616 | * Remove the event from a task's (or a CPU's) list of events. |
| 1617 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1618 | * CPU events are removed with a smp call. For task events we only |
| 1619 | * call when the task is on a CPU. |
| 1620 | * |
| 1621 | * If event->ctx is a cloned context, callers must make sure that |
| 1622 | * every task struct that event->ctx->task could possibly point to |
| 1623 | * remains valid. This is OK when called from perf_release since |
| 1624 | * that only calls us on the top-level context, which can't be a clone. |
| 1625 | * When called from perf_event_exit_task, it's OK because the |
| 1626 | * context has been detached from its task. |
| 1627 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1628 | 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] | 1629 | { |
| 1630 | struct perf_event_context *ctx = event->ctx; |
| 1631 | struct task_struct *task = ctx->task; |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1632 | struct remove_event re = { |
| 1633 | .event = event, |
| 1634 | .detach_group = detach_group, |
| 1635 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1636 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1637 | lockdep_assert_held(&ctx->mutex); |
| 1638 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1639 | if (!task) { |
| 1640 | /* |
Mark Rutland | 226424e | 2014-11-05 16:11:44 +0000 | [diff] [blame] | 1641 | * Per cpu events are removed via an smp call. The removal can |
| 1642 | * fail if the CPU is currently offline, but in that case we |
| 1643 | * already called __perf_remove_from_context from |
| 1644 | * perf_event_exit_cpu. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1645 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1646 | cpu_function_call(event->cpu, __perf_remove_from_context, &re); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1647 | return; |
| 1648 | } |
| 1649 | |
| 1650 | retry: |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1651 | if (!task_function_call(task, __perf_remove_from_context, &re)) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1652 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1653 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1654 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1655 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1656 | * If we failed to find a running task, but find the context active now |
| 1657 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1658 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1659 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1660 | raw_spin_unlock_irq(&ctx->lock); |
Cong Wang | 3577af7 | 2014-09-02 15:27:20 -0700 | [diff] [blame] | 1661 | /* |
| 1662 | * Reload the task pointer, it might have been changed by |
| 1663 | * a concurrent perf_event_context_sched_out(). |
| 1664 | */ |
| 1665 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1666 | goto retry; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1670 | * Since the task isn't running, its safe to remove the event, us |
| 1671 | * holding the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1672 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1673 | if (detach_group) |
| 1674 | perf_group_detach(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1675 | list_del_event(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1676 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1677 | } |
| 1678 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1679 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1680 | * Cross CPU call to disable a performance event |
| 1681 | */ |
K.Prasad | 500ad2d | 2012-08-02 13:46:35 +0530 | [diff] [blame] | 1682 | int __perf_event_disable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1683 | { |
| 1684 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1685 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1686 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1687 | |
| 1688 | /* |
| 1689 | * If this is a per-task event, need to check whether this |
| 1690 | * event's task is the current task on this cpu. |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1691 | * |
| 1692 | * Can trigger due to concurrent perf_event_context_sched_out() |
| 1693 | * flipping contexts around. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1694 | */ |
| 1695 | if (ctx->task && cpuctx->task_ctx != ctx) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1696 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1697 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1698 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1699 | |
| 1700 | /* |
| 1701 | * If the event is on, turn it off. |
| 1702 | * If it is in error state, leave it in error state. |
| 1703 | */ |
| 1704 | if (event->state >= PERF_EVENT_STATE_INACTIVE) { |
| 1705 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1706 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1707 | update_group_times(event); |
| 1708 | if (event == event->group_leader) |
| 1709 | group_sched_out(event, cpuctx, ctx); |
| 1710 | else |
| 1711 | event_sched_out(event, cpuctx, ctx); |
| 1712 | event->state = PERF_EVENT_STATE_OFF; |
| 1713 | } |
| 1714 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1715 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1716 | |
| 1717 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * Disable a event. |
| 1722 | * |
| 1723 | * If event->ctx is a cloned context, callers must make sure that |
| 1724 | * every task struct that event->ctx->task could possibly point to |
| 1725 | * remains valid. This condition is satisifed when called through |
| 1726 | * perf_event_for_each_child or perf_event_for_each because they |
| 1727 | * hold the top-level event's child_mutex, so any descendant that |
| 1728 | * goes to exit will block in sync_child_event. |
| 1729 | * When called from perf_pending_event it's OK because event->ctx |
| 1730 | * is the current context on this CPU and preemption is disabled, |
| 1731 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1732 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1733 | static void _perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1734 | { |
| 1735 | struct perf_event_context *ctx = event->ctx; |
| 1736 | struct task_struct *task = ctx->task; |
| 1737 | |
| 1738 | if (!task) { |
| 1739 | /* |
| 1740 | * Disable the event on the cpu that it's on |
| 1741 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1742 | cpu_function_call(event->cpu, __perf_event_disable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1743 | return; |
| 1744 | } |
| 1745 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1746 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1747 | if (!task_function_call(task, __perf_event_disable, event)) |
| 1748 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1749 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1750 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1751 | /* |
| 1752 | * If the event is still active, we need to retry the cross-call. |
| 1753 | */ |
| 1754 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1755 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1756 | /* |
| 1757 | * Reload the task pointer, it might have been changed by |
| 1758 | * a concurrent perf_event_context_sched_out(). |
| 1759 | */ |
| 1760 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1761 | goto retry; |
| 1762 | } |
| 1763 | |
| 1764 | /* |
| 1765 | * Since we have the lock this context can't be scheduled |
| 1766 | * in, so we can change the state safely. |
| 1767 | */ |
| 1768 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 1769 | update_group_times(event); |
| 1770 | event->state = PERF_EVENT_STATE_OFF; |
| 1771 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1772 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1773 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1774 | |
| 1775 | /* |
| 1776 | * Strictly speaking kernel users cannot create groups and therefore this |
| 1777 | * interface does not need the perf_event_ctx_lock() magic. |
| 1778 | */ |
| 1779 | void perf_event_disable(struct perf_event *event) |
| 1780 | { |
| 1781 | struct perf_event_context *ctx; |
| 1782 | |
| 1783 | ctx = perf_event_ctx_lock(event); |
| 1784 | _perf_event_disable(event); |
| 1785 | perf_event_ctx_unlock(event, ctx); |
| 1786 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 1787 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1788 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1789 | static void perf_set_shadow_time(struct perf_event *event, |
| 1790 | struct perf_event_context *ctx, |
| 1791 | u64 tstamp) |
| 1792 | { |
| 1793 | /* |
| 1794 | * use the correct time source for the time snapshot |
| 1795 | * |
| 1796 | * We could get by without this by leveraging the |
| 1797 | * fact that to get to this function, the caller |
| 1798 | * has most likely already called update_context_time() |
| 1799 | * and update_cgrp_time_xx() and thus both timestamp |
| 1800 | * are identical (or very close). Given that tstamp is, |
| 1801 | * already adjusted for cgroup, we could say that: |
| 1802 | * tstamp - ctx->timestamp |
| 1803 | * is equivalent to |
| 1804 | * tstamp - cgrp->timestamp. |
| 1805 | * |
| 1806 | * Then, in perf_output_read(), the calculation would |
| 1807 | * work with no changes because: |
| 1808 | * - event is guaranteed scheduled in |
| 1809 | * - no scheduled out in between |
| 1810 | * - thus the timestamp would be the same |
| 1811 | * |
| 1812 | * But this is a bit hairy. |
| 1813 | * |
| 1814 | * So instead, we have an explicit cgroup call to remain |
| 1815 | * within the time time source all along. We believe it |
| 1816 | * is cleaner and simpler to understand. |
| 1817 | */ |
| 1818 | if (is_cgroup_event(event)) |
| 1819 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1820 | else |
| 1821 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1822 | } |
| 1823 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1824 | #define MAX_INTERRUPTS (~0ULL) |
| 1825 | |
| 1826 | static void perf_log_throttle(struct perf_event *event, int enable); |
| 1827 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1828 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1829 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1830 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1831 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1832 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1833 | u64 tstamp = perf_event_time(event); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1834 | int ret = 0; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1835 | |
Peter Zijlstra | 6334241 | 2014-05-05 11:49:16 +0200 | [diff] [blame] | 1836 | lockdep_assert_held(&ctx->lock); |
| 1837 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1838 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1839 | return 0; |
| 1840 | |
| 1841 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1842 | event->oncpu = smp_processor_id(); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1843 | |
| 1844 | /* |
| 1845 | * Unthrottle events, since we scheduled we might have missed several |
| 1846 | * ticks already, also for a heavily scheduling task there is little |
| 1847 | * guarantee it'll get a tick in a timely manner. |
| 1848 | */ |
| 1849 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1850 | perf_log_throttle(event, 1); |
| 1851 | event->hw.interrupts = 0; |
| 1852 | } |
| 1853 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1854 | /* |
| 1855 | * The new state must be visible before we turn it on in the hardware: |
| 1856 | */ |
| 1857 | smp_wmb(); |
| 1858 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1859 | perf_pmu_disable(event->pmu); |
| 1860 | |
Shaohua Li | 72f669c | 2015-02-05 15:55:31 -0800 | [diff] [blame] | 1861 | event->tstamp_running += tstamp - event->tstamp_stopped; |
| 1862 | |
| 1863 | perf_set_shadow_time(event, ctx, tstamp); |
| 1864 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1865 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1866 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1867 | event->oncpu = -1; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1868 | ret = -EAGAIN; |
| 1869 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1872 | if (!is_software_event(event)) |
| 1873 | cpuctx->active_oncpu++; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1874 | if (!ctx->nr_active++) |
| 1875 | perf_event_ctx_activate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1876 | if (event->attr.freq && event->attr.sample_freq) |
| 1877 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1878 | |
| 1879 | if (event->attr.exclusive) |
| 1880 | cpuctx->exclusive = 1; |
| 1881 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1882 | if (is_orphaned_child(event)) |
| 1883 | schedule_orphans_remove(ctx); |
| 1884 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1885 | out: |
| 1886 | perf_pmu_enable(event->pmu); |
| 1887 | |
| 1888 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | static int |
| 1892 | group_sched_in(struct perf_event *group_event, |
| 1893 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1894 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1895 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1896 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 4a23459 | 2014-02-24 12:43:31 +0100 | [diff] [blame] | 1897 | struct pmu *pmu = ctx->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1898 | u64 now = ctx->time; |
| 1899 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1900 | |
| 1901 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1902 | return 0; |
| 1903 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1904 | pmu->start_txn(pmu); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1905 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1906 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1907 | pmu->cancel_txn(pmu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1908 | perf_cpu_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1909 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1910 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1911 | |
| 1912 | /* |
| 1913 | * Schedule in siblings as one group (if any): |
| 1914 | */ |
| 1915 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1916 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1917 | partial_group = event; |
| 1918 | goto group_error; |
| 1919 | } |
| 1920 | } |
| 1921 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1922 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 1923 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1924 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1925 | group_error: |
| 1926 | /* |
| 1927 | * Groups can be scheduled in as one unit only, so undo any |
| 1928 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1929 | * The events up to the failed event are scheduled out normally, |
| 1930 | * tstamp_stopped will be updated. |
| 1931 | * |
| 1932 | * The failed events and the remaining siblings need to have |
| 1933 | * their timings updated as if they had gone thru event_sched_in() |
| 1934 | * and event_sched_out(). This is required to get consistent timings |
| 1935 | * across the group. This also takes care of the case where the group |
| 1936 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 1937 | * the time the event was actually stopped, such that time delta |
| 1938 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1939 | */ |
| 1940 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 1941 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1942 | simulate = true; |
| 1943 | |
| 1944 | if (simulate) { |
| 1945 | event->tstamp_running += now - event->tstamp_stopped; |
| 1946 | event->tstamp_stopped = now; |
| 1947 | } else { |
| 1948 | event_sched_out(event, cpuctx, ctx); |
| 1949 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1950 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1951 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1952 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1953 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1954 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1955 | perf_cpu_hrtimer_restart(cpuctx); |
| 1956 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1957 | return -EAGAIN; |
| 1958 | } |
| 1959 | |
| 1960 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1961 | * Work out whether we can put this event group on the CPU now. |
| 1962 | */ |
| 1963 | static int group_can_go_on(struct perf_event *event, |
| 1964 | struct perf_cpu_context *cpuctx, |
| 1965 | int can_add_hw) |
| 1966 | { |
| 1967 | /* |
| 1968 | * Groups consisting entirely of software events can always go on. |
| 1969 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1970 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1971 | return 1; |
| 1972 | /* |
| 1973 | * If an exclusive group is already on, no other hardware |
| 1974 | * events can go on. |
| 1975 | */ |
| 1976 | if (cpuctx->exclusive) |
| 1977 | return 0; |
| 1978 | /* |
| 1979 | * If this group is exclusive and there are already |
| 1980 | * events on the CPU, it can't go on. |
| 1981 | */ |
| 1982 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 1983 | return 0; |
| 1984 | /* |
| 1985 | * Otherwise, try to add it if all previous groups were able |
| 1986 | * to go on. |
| 1987 | */ |
| 1988 | return can_add_hw; |
| 1989 | } |
| 1990 | |
| 1991 | static void add_event_to_ctx(struct perf_event *event, |
| 1992 | struct perf_event_context *ctx) |
| 1993 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1994 | u64 tstamp = perf_event_time(event); |
| 1995 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1996 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1997 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1998 | event->tstamp_enabled = tstamp; |
| 1999 | event->tstamp_running = tstamp; |
| 2000 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2001 | } |
| 2002 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2003 | static void task_ctx_sched_out(struct perf_event_context *ctx); |
| 2004 | static void |
| 2005 | ctx_sched_in(struct perf_event_context *ctx, |
| 2006 | struct perf_cpu_context *cpuctx, |
| 2007 | enum event_type_t event_type, |
| 2008 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2009 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 2010 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 2011 | struct perf_event_context *ctx, |
| 2012 | struct task_struct *task) |
| 2013 | { |
| 2014 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 2015 | if (ctx) |
| 2016 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 2017 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 2018 | if (ctx) |
| 2019 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 2020 | } |
| 2021 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2022 | /* |
| 2023 | * Cross CPU call to install and enable a performance event |
| 2024 | * |
| 2025 | * Must be called with ctx->mutex held |
| 2026 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2027 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2028 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2029 | struct perf_event *event = info; |
| 2030 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2031 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2032 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
| 2033 | struct task_struct *task = current; |
| 2034 | |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2035 | perf_ctx_lock(cpuctx, task_ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2036 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2037 | |
| 2038 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2039 | * If there was an active task_ctx schedule it out. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2040 | */ |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2041 | if (task_ctx) |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2042 | task_ctx_sched_out(task_ctx); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2043 | |
| 2044 | /* |
| 2045 | * If the context we're installing events in is not the |
| 2046 | * active task_ctx, flip them. |
| 2047 | */ |
| 2048 | if (ctx->task && task_ctx != ctx) { |
| 2049 | if (task_ctx) |
| 2050 | raw_spin_unlock(&task_ctx->lock); |
| 2051 | raw_spin_lock(&ctx->lock); |
| 2052 | task_ctx = ctx; |
| 2053 | } |
| 2054 | |
| 2055 | if (task_ctx) { |
| 2056 | cpuctx->task_ctx = task_ctx; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2057 | task = task_ctx->task; |
| 2058 | } |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2059 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2060 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2061 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2062 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2063 | /* |
| 2064 | * update cgrp time only if current cgrp |
| 2065 | * matches event->cgrp. Must be done before |
| 2066 | * calling add_event_to_ctx() |
| 2067 | */ |
| 2068 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2069 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2070 | add_event_to_ctx(event, ctx); |
| 2071 | |
| 2072 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2073 | * Schedule everything back in |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2074 | */ |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 2075 | perf_event_sched_in(cpuctx, task_ctx, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2076 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2077 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 2078 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2079 | |
| 2080 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | /* |
| 2084 | * Attach a performance event to a context |
| 2085 | * |
| 2086 | * First we add the event to the list with the hardware enable bit |
| 2087 | * in event->hw_config cleared. |
| 2088 | * |
| 2089 | * If the event is attached to a task which is on a CPU we use a smp |
| 2090 | * call to enable it in the task context. The task might have been |
| 2091 | * scheduled away, but we check this in the smp call again. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2092 | */ |
| 2093 | static void |
| 2094 | perf_install_in_context(struct perf_event_context *ctx, |
| 2095 | struct perf_event *event, |
| 2096 | int cpu) |
| 2097 | { |
| 2098 | struct task_struct *task = ctx->task; |
| 2099 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2100 | lockdep_assert_held(&ctx->mutex); |
| 2101 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2102 | event->ctx = ctx; |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 2103 | if (event->cpu != -1) |
| 2104 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2105 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2106 | if (!task) { |
| 2107 | /* |
| 2108 | * Per cpu events are installed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2109 | * the install is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2110 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2111 | cpu_function_call(cpu, __perf_install_in_context, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2112 | return; |
| 2113 | } |
| 2114 | |
| 2115 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2116 | if (!task_function_call(task, __perf_install_in_context, event)) |
| 2117 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2118 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2119 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2120 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2121 | * If we failed to find a running task, but find the context active now |
| 2122 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2123 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2124 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2125 | raw_spin_unlock_irq(&ctx->lock); |
Cong Wang | 3577af7 | 2014-09-02 15:27:20 -0700 | [diff] [blame] | 2126 | /* |
| 2127 | * Reload the task pointer, it might have been changed by |
| 2128 | * a concurrent perf_event_context_sched_out(). |
| 2129 | */ |
| 2130 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2131 | goto retry; |
| 2132 | } |
| 2133 | |
| 2134 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2135 | * Since the task isn't running, its safe to add the event, us holding |
| 2136 | * the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2137 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2138 | add_event_to_ctx(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2139 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | /* |
| 2143 | * Put a event into inactive state and update time fields. |
| 2144 | * Enabling the leader of a group effectively enables all |
| 2145 | * the group members that aren't explicitly disabled, so we |
| 2146 | * have to update their ->tstamp_enabled also. |
| 2147 | * Note: this works for group members as well as group leaders |
| 2148 | * since the non-leader members' sibling_lists will be empty. |
| 2149 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2150 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2151 | { |
| 2152 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2153 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2154 | |
| 2155 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2156 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2157 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2158 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 2159 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2160 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | /* |
| 2164 | * Cross CPU call to enable a performance event |
| 2165 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2166 | static int __perf_event_enable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2167 | { |
| 2168 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2169 | struct perf_event_context *ctx = event->ctx; |
| 2170 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2171 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2172 | int err; |
| 2173 | |
Jiri Olsa | 06f4179 | 2013-07-09 17:44:11 +0200 | [diff] [blame] | 2174 | /* |
| 2175 | * There's a time window between 'ctx->is_active' check |
| 2176 | * in perf_event_enable function and this place having: |
| 2177 | * - IRQs on |
| 2178 | * - ctx->lock unlocked |
| 2179 | * |
| 2180 | * where the task could be killed and 'ctx' deactivated |
| 2181 | * by perf_event_exit_task. |
| 2182 | */ |
| 2183 | if (!ctx->is_active) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2184 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2185 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2186 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2187 | update_context_time(ctx); |
| 2188 | |
| 2189 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2190 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2191 | |
| 2192 | /* |
| 2193 | * set current task's cgroup time reference point |
| 2194 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2195 | perf_cgroup_set_timestamp(current, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2196 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2197 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2198 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2199 | if (!event_filter_match(event)) { |
| 2200 | if (is_cgroup_event(event)) |
| 2201 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2202 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2203 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2204 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2205 | /* |
| 2206 | * If the event is in a group and isn't the group leader, |
| 2207 | * then don't put it on unless the group is on. |
| 2208 | */ |
| 2209 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) |
| 2210 | goto unlock; |
| 2211 | |
| 2212 | if (!group_can_go_on(event, cpuctx, 1)) { |
| 2213 | err = -EEXIST; |
| 2214 | } else { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2215 | if (event == leader) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2216 | err = group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2217 | else |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2218 | err = event_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | if (err) { |
| 2222 | /* |
| 2223 | * If this event can't go on and it's part of a |
| 2224 | * group, then the whole group has to come off. |
| 2225 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2226 | if (leader != event) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2227 | group_sched_out(leader, cpuctx, ctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2228 | perf_cpu_hrtimer_restart(cpuctx); |
| 2229 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2230 | if (leader->attr.pinned) { |
| 2231 | update_group_times(leader); |
| 2232 | leader->state = PERF_EVENT_STATE_ERROR; |
| 2233 | } |
| 2234 | } |
| 2235 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2236 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2237 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2238 | |
| 2239 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | /* |
| 2243 | * Enable a event. |
| 2244 | * |
| 2245 | * If event->ctx is a cloned context, callers must make sure that |
| 2246 | * every task struct that event->ctx->task could possibly point to |
| 2247 | * remains valid. This condition is satisfied when called through |
| 2248 | * perf_event_for_each_child or perf_event_for_each as described |
| 2249 | * for perf_event_disable. |
| 2250 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2251 | static void _perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2252 | { |
| 2253 | struct perf_event_context *ctx = event->ctx; |
| 2254 | struct task_struct *task = ctx->task; |
| 2255 | |
| 2256 | if (!task) { |
| 2257 | /* |
| 2258 | * Enable the event on the cpu that it's on |
| 2259 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2260 | cpu_function_call(event->cpu, __perf_event_enable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2261 | return; |
| 2262 | } |
| 2263 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2264 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2265 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2266 | goto out; |
| 2267 | |
| 2268 | /* |
| 2269 | * If the event is in error state, clear that first. |
| 2270 | * That way, if we see the event in error state below, we |
| 2271 | * know that it has gone back into error state, as distinct |
| 2272 | * from the task having been scheduled away before the |
| 2273 | * cross-call arrived. |
| 2274 | */ |
| 2275 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2276 | event->state = PERF_EVENT_STATE_OFF; |
| 2277 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2278 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2279 | if (!ctx->is_active) { |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2280 | __perf_event_mark_enabled(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2281 | goto out; |
| 2282 | } |
| 2283 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2284 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2285 | |
| 2286 | if (!task_function_call(task, __perf_event_enable, event)) |
| 2287 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2288 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2289 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2290 | |
| 2291 | /* |
| 2292 | * If the context is active and the event is still off, |
| 2293 | * we need to retry the cross-call. |
| 2294 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2295 | if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) { |
| 2296 | /* |
| 2297 | * task could have been flipped by a concurrent |
| 2298 | * perf_event_context_sched_out() |
| 2299 | */ |
| 2300 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2301 | goto retry; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2302 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2303 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2304 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2305 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2306 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2307 | |
| 2308 | /* |
| 2309 | * See perf_event_disable(); |
| 2310 | */ |
| 2311 | void perf_event_enable(struct perf_event *event) |
| 2312 | { |
| 2313 | struct perf_event_context *ctx; |
| 2314 | |
| 2315 | ctx = perf_event_ctx_lock(event); |
| 2316 | _perf_event_enable(event); |
| 2317 | perf_event_ctx_unlock(event, ctx); |
| 2318 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2319 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2320 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2321 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2322 | { |
| 2323 | /* |
| 2324 | * not supported on inherited events |
| 2325 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2326 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2327 | return -EINVAL; |
| 2328 | |
| 2329 | atomic_add(refresh, &event->event_limit); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2330 | _perf_event_enable(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2331 | |
| 2332 | return 0; |
| 2333 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2334 | |
| 2335 | /* |
| 2336 | * See perf_event_disable() |
| 2337 | */ |
| 2338 | int perf_event_refresh(struct perf_event *event, int refresh) |
| 2339 | { |
| 2340 | struct perf_event_context *ctx; |
| 2341 | int ret; |
| 2342 | |
| 2343 | ctx = perf_event_ctx_lock(event); |
| 2344 | ret = _perf_event_refresh(event, refresh); |
| 2345 | perf_event_ctx_unlock(event, ctx); |
| 2346 | |
| 2347 | return ret; |
| 2348 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2349 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2350 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2351 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2352 | struct perf_cpu_context *cpuctx, |
| 2353 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2354 | { |
| 2355 | struct perf_event *event; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2356 | int is_active = ctx->is_active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2357 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2358 | ctx->is_active &= ~event_type; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2359 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2360 | return; |
| 2361 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2362 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2363 | update_cgrp_time_from_cpuctx(cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2364 | if (!ctx->nr_active) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2365 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2366 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2367 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2368 | if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2369 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2370 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2371 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2372 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2373 | if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2374 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2375 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2376 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2377 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | /* |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2381 | * Test whether two contexts are equivalent, i.e. whether they have both been |
| 2382 | * cloned from the same version of the same context. |
| 2383 | * |
| 2384 | * Equivalence is measured using a generation number in the context that is |
| 2385 | * incremented on each modification to it; see unclone_ctx(), list_add_event() |
| 2386 | * and list_del_event(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2387 | */ |
| 2388 | static int context_equiv(struct perf_event_context *ctx1, |
| 2389 | struct perf_event_context *ctx2) |
| 2390 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 2391 | lockdep_assert_held(&ctx1->lock); |
| 2392 | lockdep_assert_held(&ctx2->lock); |
| 2393 | |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2394 | /* Pinning disables the swap optimization */ |
| 2395 | if (ctx1->pin_count || ctx2->pin_count) |
| 2396 | return 0; |
| 2397 | |
| 2398 | /* If ctx1 is the parent of ctx2 */ |
| 2399 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) |
| 2400 | return 1; |
| 2401 | |
| 2402 | /* If ctx2 is the parent of ctx1 */ |
| 2403 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) |
| 2404 | return 1; |
| 2405 | |
| 2406 | /* |
| 2407 | * If ctx1 and ctx2 have the same parent; we flatten the parent |
| 2408 | * hierarchy, see perf_event_init_context(). |
| 2409 | */ |
| 2410 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && |
| 2411 | ctx1->parent_gen == ctx2->parent_gen) |
| 2412 | return 1; |
| 2413 | |
| 2414 | /* Unmatched */ |
| 2415 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2416 | } |
| 2417 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2418 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2419 | struct perf_event *next_event) |
| 2420 | { |
| 2421 | u64 value; |
| 2422 | |
| 2423 | if (!event->attr.inherit_stat) |
| 2424 | return; |
| 2425 | |
| 2426 | /* |
| 2427 | * Update the event value, we cannot use perf_event_read() |
| 2428 | * because we're in the middle of a context switch and have IRQs |
| 2429 | * disabled, which upsets smp_call_function_single(), however |
| 2430 | * we know the event must be on the current CPU, therefore we |
| 2431 | * don't need to use it. |
| 2432 | */ |
| 2433 | switch (event->state) { |
| 2434 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2435 | event->pmu->read(event); |
| 2436 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2437 | |
| 2438 | case PERF_EVENT_STATE_INACTIVE: |
| 2439 | update_event_times(event); |
| 2440 | break; |
| 2441 | |
| 2442 | default: |
| 2443 | break; |
| 2444 | } |
| 2445 | |
| 2446 | /* |
| 2447 | * In order to keep per-task stats reliable we need to flip the event |
| 2448 | * values when we flip the contexts. |
| 2449 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2450 | value = local64_read(&next_event->count); |
| 2451 | value = local64_xchg(&event->count, value); |
| 2452 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2453 | |
| 2454 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2455 | swap(event->total_time_running, next_event->total_time_running); |
| 2456 | |
| 2457 | /* |
| 2458 | * Since we swizzled the values, update the user visible data too. |
| 2459 | */ |
| 2460 | perf_event_update_userpage(event); |
| 2461 | perf_event_update_userpage(next_event); |
| 2462 | } |
| 2463 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2464 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2465 | struct perf_event_context *next_ctx) |
| 2466 | { |
| 2467 | struct perf_event *event, *next_event; |
| 2468 | |
| 2469 | if (!ctx->nr_stat) |
| 2470 | return; |
| 2471 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2472 | update_context_time(ctx); |
| 2473 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2474 | event = list_first_entry(&ctx->event_list, |
| 2475 | struct perf_event, event_entry); |
| 2476 | |
| 2477 | next_event = list_first_entry(&next_ctx->event_list, |
| 2478 | struct perf_event, event_entry); |
| 2479 | |
| 2480 | while (&event->event_entry != &ctx->event_list && |
| 2481 | &next_event->event_entry != &next_ctx->event_list) { |
| 2482 | |
| 2483 | __perf_event_sync_stat(event, next_event); |
| 2484 | |
| 2485 | event = list_next_entry(event, event_entry); |
| 2486 | next_event = list_next_entry(next_event, event_entry); |
| 2487 | } |
| 2488 | } |
| 2489 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2490 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2491 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2492 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2493 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2494 | struct perf_event_context *next_ctx; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2495 | struct perf_event_context *parent, *next_parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2496 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2497 | int do_switch = 1; |
| 2498 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2499 | if (likely(!ctx)) |
| 2500 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2501 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2502 | cpuctx = __get_cpu_context(ctx); |
| 2503 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2504 | return; |
| 2505 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2506 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2507 | next_ctx = next->perf_event_ctxp[ctxn]; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2508 | if (!next_ctx) |
| 2509 | goto unlock; |
| 2510 | |
| 2511 | parent = rcu_dereference(ctx->parent_ctx); |
| 2512 | next_parent = rcu_dereference(next_ctx->parent_ctx); |
| 2513 | |
| 2514 | /* If neither context have a parent context; they cannot be clones. */ |
Jiri Olsa | 802c8a6 | 2014-09-12 13:18:28 +0200 | [diff] [blame] | 2515 | if (!parent && !next_parent) |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2516 | goto unlock; |
| 2517 | |
| 2518 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2519 | /* |
| 2520 | * Looks like the two contexts are clones, so we might be |
| 2521 | * able to optimize the context switch. We lock both |
| 2522 | * contexts and check that they are clones under the |
| 2523 | * lock (including re-checking that neither has been |
| 2524 | * uncloned in the meantime). It doesn't matter which |
| 2525 | * order we take the locks because no other cpu could |
| 2526 | * be trying to lock both of these tasks. |
| 2527 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2528 | raw_spin_lock(&ctx->lock); |
| 2529 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2530 | if (context_equiv(ctx, next_ctx)) { |
| 2531 | /* |
| 2532 | * XXX do we need a memory barrier of sorts |
| 2533 | * wrt to rcu_dereference() of perf_event_ctxp |
| 2534 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2535 | task->perf_event_ctxp[ctxn] = next_ctx; |
| 2536 | next->perf_event_ctxp[ctxn] = ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2537 | ctx->task = next; |
| 2538 | next_ctx->task = task; |
Yan, Zheng | 5a158c3 | 2014-11-04 21:56:02 -0500 | [diff] [blame] | 2539 | |
| 2540 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); |
| 2541 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2542 | do_switch = 0; |
| 2543 | |
| 2544 | perf_event_sync_stat(ctx, next_ctx); |
| 2545 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2546 | raw_spin_unlock(&next_ctx->lock); |
| 2547 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2548 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2549 | unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2550 | rcu_read_unlock(); |
| 2551 | |
| 2552 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2553 | raw_spin_lock(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2554 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2555 | cpuctx->task_ctx = NULL; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2556 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2560 | void perf_sched_cb_dec(struct pmu *pmu) |
| 2561 | { |
| 2562 | this_cpu_dec(perf_sched_cb_usages); |
| 2563 | } |
| 2564 | |
| 2565 | void perf_sched_cb_inc(struct pmu *pmu) |
| 2566 | { |
| 2567 | this_cpu_inc(perf_sched_cb_usages); |
| 2568 | } |
| 2569 | |
| 2570 | /* |
| 2571 | * This function provides the context switch callback to the lower code |
| 2572 | * layer. It is invoked ONLY when the context switch callback is enabled. |
| 2573 | */ |
| 2574 | static void perf_pmu_sched_task(struct task_struct *prev, |
| 2575 | struct task_struct *next, |
| 2576 | bool sched_in) |
| 2577 | { |
| 2578 | struct perf_cpu_context *cpuctx; |
| 2579 | struct pmu *pmu; |
| 2580 | unsigned long flags; |
| 2581 | |
| 2582 | if (prev == next) |
| 2583 | return; |
| 2584 | |
| 2585 | local_irq_save(flags); |
| 2586 | |
| 2587 | rcu_read_lock(); |
| 2588 | |
| 2589 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 2590 | if (pmu->sched_task) { |
| 2591 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2592 | |
| 2593 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2594 | |
| 2595 | perf_pmu_disable(pmu); |
| 2596 | |
| 2597 | pmu->sched_task(cpuctx->task_ctx, sched_in); |
| 2598 | |
| 2599 | perf_pmu_enable(pmu); |
| 2600 | |
| 2601 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | rcu_read_unlock(); |
| 2606 | |
| 2607 | local_irq_restore(flags); |
| 2608 | } |
| 2609 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2610 | #define for_each_task_context_nr(ctxn) \ |
| 2611 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2612 | |
| 2613 | /* |
| 2614 | * Called from scheduler to remove the events of the current task, |
| 2615 | * with interrupts disabled. |
| 2616 | * |
| 2617 | * We stop each event and update the event value in event->count. |
| 2618 | * |
| 2619 | * This does not protect us against NMI, but disable() |
| 2620 | * sets the disabled bit in the control field of event _before_ |
| 2621 | * accessing the event control register. If a NMI hits, then it will |
| 2622 | * not restart the event. |
| 2623 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2624 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2625 | struct task_struct *next) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2626 | { |
| 2627 | int ctxn; |
| 2628 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2629 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 2630 | perf_pmu_sched_task(task, next, false); |
| 2631 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2632 | for_each_task_context_nr(ctxn) |
| 2633 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2634 | |
| 2635 | /* |
| 2636 | * if cgroup events exist on this CPU, then we need |
| 2637 | * to check if we have to switch out PMU state. |
| 2638 | * cgroup event are system-wide mode only |
| 2639 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2640 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2641 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2642 | } |
| 2643 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2644 | static void task_ctx_sched_out(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2645 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2646 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2647 | |
| 2648 | if (!cpuctx->task_ctx) |
| 2649 | return; |
| 2650 | |
| 2651 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2652 | return; |
| 2653 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2654 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2655 | cpuctx->task_ctx = NULL; |
| 2656 | } |
| 2657 | |
| 2658 | /* |
| 2659 | * Called with IRQs disabled |
| 2660 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2661 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2662 | enum event_type_t event_type) |
| 2663 | { |
| 2664 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2668 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2669 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2670 | { |
| 2671 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2672 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2673 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2674 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2675 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2676 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2677 | continue; |
| 2678 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2679 | /* may need to reset tstamp_enabled */ |
| 2680 | if (is_cgroup_event(event)) |
| 2681 | perf_cgroup_mark_enabled(event, ctx); |
| 2682 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2683 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2684 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2685 | |
| 2686 | /* |
| 2687 | * If this pinned group hasn't been scheduled, |
| 2688 | * put it in error state. |
| 2689 | */ |
| 2690 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2691 | update_group_times(event); |
| 2692 | event->state = PERF_EVENT_STATE_ERROR; |
| 2693 | } |
| 2694 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | static void |
| 2698 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2699 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2700 | { |
| 2701 | struct perf_event *event; |
| 2702 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2703 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2704 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2705 | /* Ignore events in OFF or ERROR state */ |
| 2706 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2707 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2708 | /* |
| 2709 | * Listen to the 'cpu' scheduling filter constraint |
| 2710 | * of events: |
| 2711 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2712 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2713 | continue; |
| 2714 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2715 | /* may need to reset tstamp_enabled */ |
| 2716 | if (is_cgroup_event(event)) |
| 2717 | perf_cgroup_mark_enabled(event, ctx); |
| 2718 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2719 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2720 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2721 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2722 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2723 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | static void |
| 2727 | ctx_sched_in(struct perf_event_context *ctx, |
| 2728 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2729 | enum event_type_t event_type, |
| 2730 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2731 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2732 | u64 now; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2733 | int is_active = ctx->is_active; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2734 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2735 | ctx->is_active |= event_type; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2736 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2737 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2738 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2739 | now = perf_clock(); |
| 2740 | ctx->timestamp = now; |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2741 | perf_cgroup_set_timestamp(task, ctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2742 | /* |
| 2743 | * First go through the list and put on any pinned groups |
| 2744 | * in order to give them the best chance of going on. |
| 2745 | */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2746 | if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2747 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2748 | |
| 2749 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2750 | if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2751 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2752 | } |
| 2753 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2754 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2755 | enum event_type_t event_type, |
| 2756 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2757 | { |
| 2758 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2759 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2760 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2761 | } |
| 2762 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2763 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2764 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2765 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2766 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2767 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2768 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2769 | if (cpuctx->task_ctx == ctx) |
| 2770 | return; |
| 2771 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2772 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2773 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2774 | /* |
| 2775 | * We want to keep the following priority order: |
| 2776 | * cpu pinned (that don't need to move), task pinned, |
| 2777 | * cpu flexible, task flexible. |
| 2778 | */ |
| 2779 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2780 | |
Gleb Natapov | 1d5f003 | 2011-10-23 19:10:33 +0200 | [diff] [blame] | 2781 | if (ctx->nr_events) |
| 2782 | cpuctx->task_ctx = ctx; |
eranian@google.com | 9b33fa6 | 2010-03-10 22:26:05 -0800 | [diff] [blame] | 2783 | |
Gleb Natapov | 86b47c2 | 2011-11-22 16:08:21 +0200 | [diff] [blame] | 2784 | perf_event_sched_in(cpuctx, cpuctx->task_ctx, task); |
| 2785 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2786 | perf_pmu_enable(ctx->pmu); |
| 2787 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2788 | } |
| 2789 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2790 | /* |
| 2791 | * Called from scheduler to add the events of the current task |
| 2792 | * with interrupts disabled. |
| 2793 | * |
| 2794 | * We restore the event value and then enable it. |
| 2795 | * |
| 2796 | * This does not protect us against NMI, but enable() |
| 2797 | * sets the enabled bit in the control field of event _before_ |
| 2798 | * accessing the event control register. If a NMI hits, then it will |
| 2799 | * keep the event running. |
| 2800 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2801 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 2802 | struct task_struct *task) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2803 | { |
| 2804 | struct perf_event_context *ctx; |
| 2805 | int ctxn; |
| 2806 | |
| 2807 | for_each_task_context_nr(ctxn) { |
| 2808 | ctx = task->perf_event_ctxp[ctxn]; |
| 2809 | if (likely(!ctx)) |
| 2810 | continue; |
| 2811 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2812 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2813 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2814 | /* |
| 2815 | * if cgroup events exist on this CPU, then we need |
| 2816 | * to check if we have to switch in PMU state. |
| 2817 | * cgroup event are system-wide mode only |
| 2818 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2819 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2820 | perf_cgroup_sched_in(prev, task); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2821 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2822 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 2823 | perf_pmu_sched_task(prev, task, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2824 | } |
| 2825 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2826 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 2827 | { |
| 2828 | u64 frequency = event->attr.sample_freq; |
| 2829 | u64 sec = NSEC_PER_SEC; |
| 2830 | u64 divisor, dividend; |
| 2831 | |
| 2832 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 2833 | |
| 2834 | count_fls = fls64(count); |
| 2835 | nsec_fls = fls64(nsec); |
| 2836 | frequency_fls = fls64(frequency); |
| 2837 | sec_fls = 30; |
| 2838 | |
| 2839 | /* |
| 2840 | * We got @count in @nsec, with a target of sample_freq HZ |
| 2841 | * the target period becomes: |
| 2842 | * |
| 2843 | * @count * 10^9 |
| 2844 | * period = ------------------- |
| 2845 | * @nsec * sample_freq |
| 2846 | * |
| 2847 | */ |
| 2848 | |
| 2849 | /* |
| 2850 | * Reduce accuracy by one bit such that @a and @b converge |
| 2851 | * to a similar magnitude. |
| 2852 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2853 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2854 | do { \ |
| 2855 | if (a##_fls > b##_fls) { \ |
| 2856 | a >>= 1; \ |
| 2857 | a##_fls--; \ |
| 2858 | } else { \ |
| 2859 | b >>= 1; \ |
| 2860 | b##_fls--; \ |
| 2861 | } \ |
| 2862 | } while (0) |
| 2863 | |
| 2864 | /* |
| 2865 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 2866 | * the other, so that finally we can do a u64/u64 division. |
| 2867 | */ |
| 2868 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 2869 | REDUCE_FLS(nsec, frequency); |
| 2870 | REDUCE_FLS(sec, count); |
| 2871 | } |
| 2872 | |
| 2873 | if (count_fls + sec_fls > 64) { |
| 2874 | divisor = nsec * frequency; |
| 2875 | |
| 2876 | while (count_fls + sec_fls > 64) { |
| 2877 | REDUCE_FLS(count, sec); |
| 2878 | divisor >>= 1; |
| 2879 | } |
| 2880 | |
| 2881 | dividend = count * sec; |
| 2882 | } else { |
| 2883 | dividend = count * sec; |
| 2884 | |
| 2885 | while (nsec_fls + frequency_fls > 64) { |
| 2886 | REDUCE_FLS(nsec, frequency); |
| 2887 | dividend >>= 1; |
| 2888 | } |
| 2889 | |
| 2890 | divisor = nsec * frequency; |
| 2891 | } |
| 2892 | |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2893 | if (!divisor) |
| 2894 | return dividend; |
| 2895 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2896 | return div64_u64(dividend, divisor); |
| 2897 | } |
| 2898 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2899 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 2900 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 2901 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2902 | 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] | 2903 | { |
| 2904 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2905 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2906 | s64 delta; |
| 2907 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2908 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2909 | |
| 2910 | delta = (s64)(period - hwc->sample_period); |
| 2911 | delta = (delta + 7) / 8; /* low pass filter */ |
| 2912 | |
| 2913 | sample_period = hwc->sample_period + delta; |
| 2914 | |
| 2915 | if (!sample_period) |
| 2916 | sample_period = 1; |
| 2917 | |
| 2918 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2919 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2920 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2921 | if (disable) |
| 2922 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2923 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2924 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2925 | |
| 2926 | if (disable) |
| 2927 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2928 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2929 | } |
| 2930 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2931 | /* |
| 2932 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 2933 | * events. At the same time, make sure, having freq events does not change |
| 2934 | * the rate of unthrottling as that would introduce bias. |
| 2935 | */ |
| 2936 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 2937 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2938 | { |
| 2939 | struct perf_event *event; |
| 2940 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2941 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2942 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2943 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2944 | /* |
| 2945 | * only need to iterate over all events iff: |
| 2946 | * - context have events in frequency mode (needs freq adjust) |
| 2947 | * - there are events to unthrottle on this cpu |
| 2948 | */ |
| 2949 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2950 | return; |
| 2951 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2952 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2953 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2954 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 2955 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2956 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 2957 | continue; |
| 2958 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2959 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 2960 | continue; |
| 2961 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2962 | perf_pmu_disable(event->pmu); |
| 2963 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2964 | hwc = &event->hw; |
| 2965 | |
Jiri Olsa | ae23bff | 2013-08-24 16:45:54 +0200 | [diff] [blame] | 2966 | if (hwc->interrupts == MAX_INTERRUPTS) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2967 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2968 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2969 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | if (!event->attr.freq || !event->attr.sample_freq) |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2973 | goto next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2974 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2975 | /* |
| 2976 | * stop the event and update event->count |
| 2977 | */ |
| 2978 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2979 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2980 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2981 | delta = now - hwc->freq_count_stamp; |
| 2982 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2983 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2984 | /* |
| 2985 | * restart the event |
| 2986 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2987 | * we have stopped the event so tell that |
| 2988 | * to perf_adjust_period() to avoid stopping it |
| 2989 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2990 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2991 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2992 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2993 | |
| 2994 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2995 | next: |
| 2996 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2997 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2998 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2999 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3000 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | /* |
| 3004 | * Round-robin a context's events: |
| 3005 | */ |
| 3006 | static void rotate_ctx(struct perf_event_context *ctx) |
| 3007 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 3008 | /* |
| 3009 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 3010 | * disabled by the inheritance code. |
| 3011 | */ |
| 3012 | if (!ctx->rotate_disable) |
| 3013 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3014 | } |
| 3015 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3016 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3017 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3018 | struct perf_event_context *ctx = NULL; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3019 | int rotate = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3020 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3021 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3022 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 3023 | rotate = 1; |
| 3024 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3025 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3026 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3027 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3028 | if (ctx->nr_events != ctx->nr_active) |
| 3029 | rotate = 1; |
| 3030 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3031 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3032 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3033 | goto done; |
| 3034 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 3035 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 3036 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3037 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3038 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 3039 | if (ctx) |
| 3040 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 3041 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3042 | rotate_ctx(&cpuctx->ctx); |
| 3043 | if (ctx) |
| 3044 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3045 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3046 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3047 | |
| 3048 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 3049 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3050 | done: |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3051 | |
| 3052 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3053 | } |
| 3054 | |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 3055 | #ifdef CONFIG_NO_HZ_FULL |
| 3056 | bool perf_event_can_stop_tick(void) |
| 3057 | { |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3058 | if (atomic_read(&nr_freq_events) || |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 3059 | __this_cpu_read(perf_throttled_count)) |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 3060 | return false; |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 3061 | else |
| 3062 | return true; |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 3063 | } |
| 3064 | #endif |
| 3065 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3066 | void perf_event_task_tick(void) |
| 3067 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3068 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
| 3069 | struct perf_event_context *ctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3070 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3071 | |
| 3072 | WARN_ON(!irqs_disabled()); |
| 3073 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3074 | __this_cpu_inc(perf_throttled_seq); |
| 3075 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
| 3076 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3077 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3078 | perf_adjust_freq_unthr_context(ctx, throttled); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3079 | } |
| 3080 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3081 | static int event_enable_on_exec(struct perf_event *event, |
| 3082 | struct perf_event_context *ctx) |
| 3083 | { |
| 3084 | if (!event->attr.enable_on_exec) |
| 3085 | return 0; |
| 3086 | |
| 3087 | event->attr.enable_on_exec = 0; |
| 3088 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 3089 | return 0; |
| 3090 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 3091 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3092 | |
| 3093 | return 1; |
| 3094 | } |
| 3095 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3096 | /* |
| 3097 | * Enable all of a task's events that have been marked enable-on-exec. |
| 3098 | * This expects task == current. |
| 3099 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3100 | static void perf_event_enable_on_exec(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3101 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3102 | struct perf_event_context *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3103 | struct perf_event *event; |
| 3104 | unsigned long flags; |
| 3105 | int enabled = 0; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3106 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3107 | |
| 3108 | local_irq_save(flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3109 | if (!ctx || !ctx->nr_events) |
| 3110 | goto out; |
| 3111 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 3112 | /* |
| 3113 | * We must ctxsw out cgroup events to avoid conflict |
| 3114 | * when invoking perf_task_event_sched_in() later on |
| 3115 | * in this function. Otherwise we end up trying to |
| 3116 | * ctxswin cgroup events which are already scheduled |
| 3117 | * in. |
| 3118 | */ |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 3119 | perf_cgroup_sched_out(current, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3120 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3121 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 3122 | task_ctx_sched_out(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3123 | |
Peter Zijlstra | b79387e | 2011-11-22 11:25:43 +0100 | [diff] [blame] | 3124 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3125 | ret = event_enable_on_exec(event, ctx); |
| 3126 | if (ret) |
| 3127 | enabled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | /* |
| 3131 | * Unclone this context if we enabled any event. |
| 3132 | */ |
| 3133 | if (enabled) |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3134 | clone_ctx = unclone_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3135 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3136 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3137 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 3138 | /* |
| 3139 | * Also calls ctxswin for cgroup events, if any: |
| 3140 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3141 | perf_event_context_sched_in(ctx, ctx->task); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3142 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3143 | local_irq_restore(flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3144 | |
| 3145 | if (clone_ctx) |
| 3146 | put_ctx(clone_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3147 | } |
| 3148 | |
Peter Zijlstra | e041e32 | 2014-05-21 17:32:19 +0200 | [diff] [blame] | 3149 | void perf_event_exec(void) |
| 3150 | { |
| 3151 | struct perf_event_context *ctx; |
| 3152 | int ctxn; |
| 3153 | |
| 3154 | rcu_read_lock(); |
| 3155 | for_each_task_context_nr(ctxn) { |
| 3156 | ctx = current->perf_event_ctxp[ctxn]; |
| 3157 | if (!ctx) |
| 3158 | continue; |
| 3159 | |
| 3160 | perf_event_enable_on_exec(ctx); |
| 3161 | } |
| 3162 | rcu_read_unlock(); |
| 3163 | } |
| 3164 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3165 | /* |
| 3166 | * Cross CPU call to read the hardware event |
| 3167 | */ |
| 3168 | static void __perf_event_read(void *info) |
| 3169 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3170 | struct perf_event *event = info; |
| 3171 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3172 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3173 | |
| 3174 | /* |
| 3175 | * If this is a task context, we need to check whether it is |
| 3176 | * the current task context of this cpu. If not it has been |
| 3177 | * scheduled out before the smp call arrived. In that case |
| 3178 | * event->count would have been updated to a recent sample |
| 3179 | * when the event was scheduled out. |
| 3180 | */ |
| 3181 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 3182 | return; |
| 3183 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3184 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3185 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3186 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3187 | update_cgrp_time_from_event(event); |
| 3188 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3189 | update_event_times(event); |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3190 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
| 3191 | event->pmu->read(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3192 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3193 | } |
| 3194 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3195 | static inline u64 perf_event_count(struct perf_event *event) |
| 3196 | { |
Matt Fleming | eacd3ec | 2015-01-23 18:45:41 +0000 | [diff] [blame] | 3197 | if (event->pmu->count) |
| 3198 | return event->pmu->count(event); |
| 3199 | |
| 3200 | return __perf_event_count(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3201 | } |
| 3202 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3203 | static u64 perf_event_read(struct perf_event *event) |
| 3204 | { |
| 3205 | /* |
| 3206 | * If event is enabled and currently active on a CPU, update the |
| 3207 | * value in the event structure: |
| 3208 | */ |
| 3209 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
| 3210 | smp_call_function_single(event->oncpu, |
| 3211 | __perf_event_read, event, 1); |
| 3212 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 3213 | struct perf_event_context *ctx = event->ctx; |
| 3214 | unsigned long flags; |
| 3215 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3216 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3217 | /* |
| 3218 | * may read while context is not active |
| 3219 | * (e.g., thread is blocked), in that case |
| 3220 | * we cannot update context time |
| 3221 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3222 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3223 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3224 | update_cgrp_time_from_event(event); |
| 3225 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3226 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3227 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3228 | } |
| 3229 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3230 | return perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | /* |
| 3234 | * Initialize the perf_event context in a task_struct: |
| 3235 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3236 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3237 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3238 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3239 | mutex_init(&ctx->mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3240 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3241 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 3242 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3243 | INIT_LIST_HEAD(&ctx->event_list); |
| 3244 | atomic_set(&ctx->refcount, 1); |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 3245 | INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3246 | } |
| 3247 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3248 | static struct perf_event_context * |
| 3249 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3250 | { |
| 3251 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3252 | |
| 3253 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 3254 | if (!ctx) |
| 3255 | return NULL; |
| 3256 | |
| 3257 | __perf_event_init_context(ctx); |
| 3258 | if (task) { |
| 3259 | ctx->task = task; |
| 3260 | get_task_struct(task); |
| 3261 | } |
| 3262 | ctx->pmu = pmu; |
| 3263 | |
| 3264 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3265 | } |
| 3266 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3267 | static struct task_struct * |
| 3268 | find_lively_task_by_vpid(pid_t vpid) |
| 3269 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3270 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3271 | int err; |
| 3272 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3273 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3274 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3275 | task = current; |
| 3276 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3277 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3278 | if (task) |
| 3279 | get_task_struct(task); |
| 3280 | rcu_read_unlock(); |
| 3281 | |
| 3282 | if (!task) |
| 3283 | return ERR_PTR(-ESRCH); |
| 3284 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3285 | /* Reuse ptrace permission checks for now. */ |
| 3286 | err = -EACCES; |
| 3287 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 3288 | goto errout; |
| 3289 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3290 | return task; |
| 3291 | errout: |
| 3292 | put_task_struct(task); |
| 3293 | return ERR_PTR(err); |
| 3294 | |
| 3295 | } |
| 3296 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3297 | /* |
| 3298 | * Returns a matching context with refcount and pincount. |
| 3299 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3300 | static struct perf_event_context * |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3301 | find_get_context(struct pmu *pmu, struct task_struct *task, |
| 3302 | struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3303 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3304 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3305 | struct perf_cpu_context *cpuctx; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3306 | void *task_ctx_data = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3307 | unsigned long flags; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3308 | int ctxn, err; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3309 | int cpu = event->cpu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3310 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3311 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3312 | /* Must be root to operate on a CPU event: */ |
| 3313 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3314 | return ERR_PTR(-EACCES); |
| 3315 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3316 | /* |
| 3317 | * We could be clever and allow to attach a event to an |
| 3318 | * offline CPU and activate it when the CPU comes up, but |
| 3319 | * that's for later. |
| 3320 | */ |
| 3321 | if (!cpu_online(cpu)) |
| 3322 | return ERR_PTR(-ENODEV); |
| 3323 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3324 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3325 | ctx = &cpuctx->ctx; |
| 3326 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3327 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3328 | |
| 3329 | return ctx; |
| 3330 | } |
| 3331 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3332 | err = -EINVAL; |
| 3333 | ctxn = pmu->task_ctx_nr; |
| 3334 | if (ctxn < 0) |
| 3335 | goto errout; |
| 3336 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3337 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
| 3338 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); |
| 3339 | if (!task_ctx_data) { |
| 3340 | err = -ENOMEM; |
| 3341 | goto errout; |
| 3342 | } |
| 3343 | } |
| 3344 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3345 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3346 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3347 | if (ctx) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3348 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3349 | ++ctx->pin_count; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3350 | |
| 3351 | if (task_ctx_data && !ctx->task_ctx_data) { |
| 3352 | ctx->task_ctx_data = task_ctx_data; |
| 3353 | task_ctx_data = NULL; |
| 3354 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3355 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3356 | |
| 3357 | if (clone_ctx) |
| 3358 | put_ctx(clone_ctx); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3359 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3360 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3361 | err = -ENOMEM; |
| 3362 | if (!ctx) |
| 3363 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3364 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3365 | if (task_ctx_data) { |
| 3366 | ctx->task_ctx_data = task_ctx_data; |
| 3367 | task_ctx_data = NULL; |
| 3368 | } |
| 3369 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3370 | err = 0; |
| 3371 | mutex_lock(&task->perf_event_mutex); |
| 3372 | /* |
| 3373 | * If it has already passed perf_event_exit_task(). |
| 3374 | * we must see PF_EXITING, it takes this mutex too. |
| 3375 | */ |
| 3376 | if (task->flags & PF_EXITING) |
| 3377 | err = -ESRCH; |
| 3378 | else if (task->perf_event_ctxp[ctxn]) |
| 3379 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3380 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3381 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3382 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3383 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3384 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3385 | mutex_unlock(&task->perf_event_mutex); |
| 3386 | |
| 3387 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3388 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3389 | |
| 3390 | if (err == -EAGAIN) |
| 3391 | goto retry; |
| 3392 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3393 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3394 | } |
| 3395 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3396 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3397 | return ctx; |
| 3398 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3399 | errout: |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3400 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3401 | return ERR_PTR(err); |
| 3402 | } |
| 3403 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3404 | static void perf_event_free_filter(struct perf_event *event); |
| 3405 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3406 | static void free_event_rcu(struct rcu_head *head) |
| 3407 | { |
| 3408 | struct perf_event *event; |
| 3409 | |
| 3410 | event = container_of(head, struct perf_event, rcu_head); |
| 3411 | if (event->ns) |
| 3412 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3413 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3414 | kfree(event); |
| 3415 | } |
| 3416 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3417 | static void ring_buffer_put(struct ring_buffer *rb); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3418 | static void ring_buffer_attach(struct perf_event *event, |
| 3419 | struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3420 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3421 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
| 3422 | { |
| 3423 | if (event->parent) |
| 3424 | return; |
| 3425 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3426 | if (is_cgroup_event(event)) |
| 3427 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); |
| 3428 | } |
| 3429 | |
| 3430 | static void unaccount_event(struct perf_event *event) |
| 3431 | { |
| 3432 | if (event->parent) |
| 3433 | return; |
| 3434 | |
| 3435 | if (event->attach_state & PERF_ATTACH_TASK) |
| 3436 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3437 | if (event->attr.mmap || event->attr.mmap_data) |
| 3438 | atomic_dec(&nr_mmap_events); |
| 3439 | if (event->attr.comm) |
| 3440 | atomic_dec(&nr_comm_events); |
| 3441 | if (event->attr.task) |
| 3442 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3443 | if (event->attr.freq) |
| 3444 | atomic_dec(&nr_freq_events); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3445 | if (is_cgroup_event(event)) |
| 3446 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3447 | if (has_branch_stack(event)) |
| 3448 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3449 | |
| 3450 | unaccount_event_cpu(event, event->cpu); |
| 3451 | } |
| 3452 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3453 | static void __free_event(struct perf_event *event) |
| 3454 | { |
| 3455 | if (!event->parent) { |
| 3456 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 3457 | put_callchain_buffers(); |
| 3458 | } |
| 3459 | |
| 3460 | if (event->destroy) |
| 3461 | event->destroy(event); |
| 3462 | |
| 3463 | if (event->ctx) |
| 3464 | put_ctx(event->ctx); |
| 3465 | |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 3466 | if (event->pmu) |
| 3467 | module_put(event->pmu->module); |
| 3468 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3469 | call_rcu(&event->rcu_head, free_event_rcu); |
| 3470 | } |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3471 | |
| 3472 | static void _free_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3473 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3474 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3475 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3476 | unaccount_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3477 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3478 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3479 | /* |
| 3480 | * Can happen when we close an event with re-directed output. |
| 3481 | * |
| 3482 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 3483 | * over us; possibly making our ring_buffer_put() the last. |
| 3484 | */ |
| 3485 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3486 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3487 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3488 | } |
| 3489 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3490 | if (is_cgroup_event(event)) |
| 3491 | perf_detach_cgroup(event); |
| 3492 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3493 | __free_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3494 | } |
| 3495 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3496 | /* |
| 3497 | * Used to free events which have a known refcount of 1, such as in error paths |
| 3498 | * where the event isn't exposed yet and inherited events. |
| 3499 | */ |
| 3500 | static void free_event(struct perf_event *event) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3501 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3502 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
| 3503 | "unexpected event refcount: %ld; ptr=%p\n", |
| 3504 | atomic_long_read(&event->refcount), event)) { |
| 3505 | /* leak to avoid use-after-free */ |
| 3506 | return; |
| 3507 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3508 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3509 | _free_event(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3510 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3511 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3512 | /* |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3513 | * Remove user event from the owner task. |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3514 | */ |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3515 | static void perf_remove_from_owner(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3516 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3517 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3518 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3519 | rcu_read_lock(); |
| 3520 | owner = ACCESS_ONCE(event->owner); |
| 3521 | /* |
| 3522 | * Matches the smp_wmb() in perf_event_exit_task(). If we observe |
| 3523 | * !owner it means the list deletion is complete and we can indeed |
| 3524 | * free this event, otherwise we need to serialize on |
| 3525 | * owner->perf_event_mutex. |
| 3526 | */ |
| 3527 | smp_read_barrier_depends(); |
| 3528 | if (owner) { |
| 3529 | /* |
| 3530 | * Since delayed_put_task_struct() also drops the last |
| 3531 | * task reference we can safely take a new reference |
| 3532 | * while holding the rcu_read_lock(). |
| 3533 | */ |
| 3534 | get_task_struct(owner); |
| 3535 | } |
| 3536 | rcu_read_unlock(); |
| 3537 | |
| 3538 | if (owner) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3539 | /* |
| 3540 | * If we're here through perf_event_exit_task() we're already |
| 3541 | * holding ctx->mutex which would be an inversion wrt. the |
| 3542 | * normal lock order. |
| 3543 | * |
| 3544 | * However we can safely take this lock because its the child |
| 3545 | * ctx->mutex. |
| 3546 | */ |
| 3547 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); |
| 3548 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3549 | /* |
| 3550 | * We have to re-check the event->owner field, if it is cleared |
| 3551 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 3552 | * ensured they're done, and we can proceed with freeing the |
| 3553 | * event. |
| 3554 | */ |
| 3555 | if (event->owner) |
| 3556 | list_del_init(&event->owner_entry); |
| 3557 | mutex_unlock(&owner->perf_event_mutex); |
| 3558 | put_task_struct(owner); |
| 3559 | } |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | /* |
| 3563 | * Called when the last reference to the file is gone. |
| 3564 | */ |
| 3565 | static void put_event(struct perf_event *event) |
| 3566 | { |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 3567 | struct perf_event_context *ctx; |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3568 | |
| 3569 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 3570 | return; |
| 3571 | |
| 3572 | if (!is_kernel_event(event)) |
| 3573 | perf_remove_from_owner(event); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3574 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3575 | /* |
| 3576 | * There are two ways this annotation is useful: |
| 3577 | * |
| 3578 | * 1) there is a lock recursion from perf_event_exit_task |
| 3579 | * see the comment there. |
| 3580 | * |
| 3581 | * 2) there is a lock-inversion with mmap_sem through |
| 3582 | * perf_event_read_group(), which takes faults while |
| 3583 | * holding ctx->mutex, however this is called after |
| 3584 | * the last filedesc died, so there is no possibility |
| 3585 | * to trigger the AB-BA case. |
| 3586 | */ |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 3587 | ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING); |
| 3588 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3589 | perf_remove_from_context(event, true); |
| 3590 | mutex_unlock(&ctx->mutex); |
| 3591 | |
| 3592 | _free_event(event); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3593 | } |
| 3594 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3595 | int perf_event_release_kernel(struct perf_event *event) |
| 3596 | { |
| 3597 | put_event(event); |
| 3598 | return 0; |
| 3599 | } |
| 3600 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 3601 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3602 | static int perf_release(struct inode *inode, struct file *file) |
| 3603 | { |
| 3604 | put_event(file->private_data); |
| 3605 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3606 | } |
| 3607 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 3608 | /* |
| 3609 | * Remove all orphanes events from the context. |
| 3610 | */ |
| 3611 | static void orphans_remove_work(struct work_struct *work) |
| 3612 | { |
| 3613 | struct perf_event_context *ctx; |
| 3614 | struct perf_event *event, *tmp; |
| 3615 | |
| 3616 | ctx = container_of(work, struct perf_event_context, |
| 3617 | orphans_remove.work); |
| 3618 | |
| 3619 | mutex_lock(&ctx->mutex); |
| 3620 | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) { |
| 3621 | struct perf_event *parent_event = event->parent; |
| 3622 | |
| 3623 | if (!is_orphaned_child(event)) |
| 3624 | continue; |
| 3625 | |
| 3626 | perf_remove_from_context(event, true); |
| 3627 | |
| 3628 | mutex_lock(&parent_event->child_mutex); |
| 3629 | list_del_init(&event->child_list); |
| 3630 | mutex_unlock(&parent_event->child_mutex); |
| 3631 | |
| 3632 | free_event(event); |
| 3633 | put_event(parent_event); |
| 3634 | } |
| 3635 | |
| 3636 | raw_spin_lock_irq(&ctx->lock); |
| 3637 | ctx->orphans_remove_sched = false; |
| 3638 | raw_spin_unlock_irq(&ctx->lock); |
| 3639 | mutex_unlock(&ctx->mutex); |
| 3640 | |
| 3641 | put_ctx(ctx); |
| 3642 | } |
| 3643 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3644 | 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] | 3645 | { |
| 3646 | struct perf_event *child; |
| 3647 | u64 total = 0; |
| 3648 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3649 | *enabled = 0; |
| 3650 | *running = 0; |
| 3651 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3652 | mutex_lock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3653 | total += perf_event_read(event); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3654 | *enabled += event->total_time_enabled + |
| 3655 | atomic64_read(&event->child_total_time_enabled); |
| 3656 | *running += event->total_time_running + |
| 3657 | atomic64_read(&event->child_total_time_running); |
| 3658 | |
| 3659 | list_for_each_entry(child, &event->child_list, child_list) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3660 | total += perf_event_read(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3661 | *enabled += child->total_time_enabled; |
| 3662 | *running += child->total_time_running; |
| 3663 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3664 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3665 | |
| 3666 | return total; |
| 3667 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3668 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3669 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3670 | static int perf_event_read_group(struct perf_event *event, |
| 3671 | u64 read_format, char __user *buf) |
| 3672 | { |
| 3673 | struct perf_event *leader = event->group_leader, *sub; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3674 | struct perf_event_context *ctx = leader->ctx; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3675 | int n = 0, size = 0, ret; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3676 | u64 count, enabled, running; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3677 | u64 values[5]; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3678 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3679 | lockdep_assert_held(&ctx->mutex); |
| 3680 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3681 | count = perf_event_read_value(leader, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3682 | |
| 3683 | values[n++] = 1 + leader->nr_siblings; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3684 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3685 | values[n++] = enabled; |
| 3686 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3687 | values[n++] = running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3688 | values[n++] = count; |
| 3689 | if (read_format & PERF_FORMAT_ID) |
| 3690 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3691 | |
| 3692 | size = n * sizeof(u64); |
| 3693 | |
| 3694 | if (copy_to_user(buf, values, size)) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3695 | return -EFAULT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3696 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3697 | ret = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3698 | |
| 3699 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3700 | n = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3701 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3702 | values[n++] = perf_event_read_value(sub, &enabled, &running); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3703 | if (read_format & PERF_FORMAT_ID) |
| 3704 | values[n++] = primary_event_id(sub); |
| 3705 | |
| 3706 | size = n * sizeof(u64); |
| 3707 | |
Stephane Eranian | 184d3da | 2009-11-23 21:40:49 -0800 | [diff] [blame] | 3708 | if (copy_to_user(buf + ret, values, size)) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3709 | return -EFAULT; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3710 | } |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3711 | |
| 3712 | ret += size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3713 | } |
| 3714 | |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3715 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | static int perf_event_read_one(struct perf_event *event, |
| 3719 | u64 read_format, char __user *buf) |
| 3720 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3721 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3722 | u64 values[4]; |
| 3723 | int n = 0; |
| 3724 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3725 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 3726 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3727 | values[n++] = enabled; |
| 3728 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3729 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3730 | if (read_format & PERF_FORMAT_ID) |
| 3731 | values[n++] = primary_event_id(event); |
| 3732 | |
| 3733 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 3734 | return -EFAULT; |
| 3735 | |
| 3736 | return n * sizeof(u64); |
| 3737 | } |
| 3738 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 3739 | static bool is_event_hup(struct perf_event *event) |
| 3740 | { |
| 3741 | bool no_children; |
| 3742 | |
| 3743 | if (event->state != PERF_EVENT_STATE_EXIT) |
| 3744 | return false; |
| 3745 | |
| 3746 | mutex_lock(&event->child_mutex); |
| 3747 | no_children = list_empty(&event->child_list); |
| 3748 | mutex_unlock(&event->child_mutex); |
| 3749 | return no_children; |
| 3750 | } |
| 3751 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3752 | /* |
| 3753 | * Read the performance event - simple non blocking version for now |
| 3754 | */ |
| 3755 | static ssize_t |
| 3756 | perf_read_hw(struct perf_event *event, char __user *buf, size_t count) |
| 3757 | { |
| 3758 | u64 read_format = event->attr.read_format; |
| 3759 | int ret; |
| 3760 | |
| 3761 | /* |
| 3762 | * Return end-of-file for a read on a event that is in |
| 3763 | * error state (i.e. because it was pinned but it couldn't be |
| 3764 | * scheduled on to the CPU at some point). |
| 3765 | */ |
| 3766 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 3767 | return 0; |
| 3768 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 3769 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3770 | return -ENOSPC; |
| 3771 | |
| 3772 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3773 | if (read_format & PERF_FORMAT_GROUP) |
| 3774 | ret = perf_event_read_group(event, read_format, buf); |
| 3775 | else |
| 3776 | ret = perf_event_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3777 | |
| 3778 | return ret; |
| 3779 | } |
| 3780 | |
| 3781 | static ssize_t |
| 3782 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 3783 | { |
| 3784 | struct perf_event *event = file->private_data; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3785 | struct perf_event_context *ctx; |
| 3786 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3787 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3788 | ctx = perf_event_ctx_lock(event); |
| 3789 | ret = perf_read_hw(event, buf, count); |
| 3790 | perf_event_ctx_unlock(event, ctx); |
| 3791 | |
| 3792 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 3796 | { |
| 3797 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3798 | struct ring_buffer *rb; |
Jiri Olsa | 61b6768 | 2014-08-13 19:39:56 +0200 | [diff] [blame] | 3799 | unsigned int events = POLLHUP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3800 | |
Sebastian Andrzej Siewior | e708d7a | 2014-08-04 15:31:08 +0200 | [diff] [blame] | 3801 | poll_wait(file, &event->waitq, wait); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 3802 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 3803 | if (is_event_hup(event)) |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 3804 | return events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3805 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3806 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3807 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 3808 | * 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] | 3809 | */ |
| 3810 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3811 | rb = event->rb; |
| 3812 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3813 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3814 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3815 | return events; |
| 3816 | } |
| 3817 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3818 | static void _perf_event_reset(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3819 | { |
| 3820 | (void)perf_event_read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3821 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3822 | perf_event_update_userpage(event); |
| 3823 | } |
| 3824 | |
| 3825 | /* |
| 3826 | * Holding the top-level event's child_mutex means that any |
| 3827 | * descendant process that has inherited this event will block |
| 3828 | * in sync_child_event if it goes to exit, thus satisfying the |
| 3829 | * task existence requirements of perf_event_enable/disable. |
| 3830 | */ |
| 3831 | static void perf_event_for_each_child(struct perf_event *event, |
| 3832 | void (*func)(struct perf_event *)) |
| 3833 | { |
| 3834 | struct perf_event *child; |
| 3835 | |
| 3836 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3837 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3838 | mutex_lock(&event->child_mutex); |
| 3839 | func(event); |
| 3840 | list_for_each_entry(child, &event->child_list, child_list) |
| 3841 | func(child); |
| 3842 | mutex_unlock(&event->child_mutex); |
| 3843 | } |
| 3844 | |
| 3845 | static void perf_event_for_each(struct perf_event *event, |
| 3846 | void (*func)(struct perf_event *)) |
| 3847 | { |
| 3848 | struct perf_event_context *ctx = event->ctx; |
| 3849 | struct perf_event *sibling; |
| 3850 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3851 | lockdep_assert_held(&ctx->mutex); |
| 3852 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3853 | event = event->group_leader; |
| 3854 | |
| 3855 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3856 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 3857 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 3861 | { |
| 3862 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3863 | int ret = 0, active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3864 | u64 value; |
| 3865 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 3866 | if (!is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3867 | return -EINVAL; |
| 3868 | |
John Blackwood | ad0cf34 | 2010-09-28 18:03:11 -0400 | [diff] [blame] | 3869 | if (copy_from_user(&value, arg, sizeof(value))) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3870 | return -EFAULT; |
| 3871 | |
| 3872 | if (!value) |
| 3873 | return -EINVAL; |
| 3874 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3875 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3876 | if (event->attr.freq) { |
| 3877 | if (value > sysctl_perf_event_sample_rate) { |
| 3878 | ret = -EINVAL; |
| 3879 | goto unlock; |
| 3880 | } |
| 3881 | |
| 3882 | event->attr.sample_freq = value; |
| 3883 | } else { |
| 3884 | event->attr.sample_period = value; |
| 3885 | event->hw.sample_period = value; |
| 3886 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3887 | |
| 3888 | active = (event->state == PERF_EVENT_STATE_ACTIVE); |
| 3889 | if (active) { |
| 3890 | perf_pmu_disable(ctx->pmu); |
| 3891 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3892 | } |
| 3893 | |
| 3894 | local64_set(&event->hw.period_left, 0); |
| 3895 | |
| 3896 | if (active) { |
| 3897 | event->pmu->start(event, PERF_EF_RELOAD); |
| 3898 | perf_pmu_enable(ctx->pmu); |
| 3899 | } |
| 3900 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3901 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3902 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3903 | |
| 3904 | return ret; |
| 3905 | } |
| 3906 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3907 | static const struct file_operations perf_fops; |
| 3908 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3909 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3910 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3911 | struct fd f = fdget(fd); |
| 3912 | if (!f.file) |
| 3913 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3914 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3915 | if (f.file->f_op != &perf_fops) { |
| 3916 | fdput(f); |
| 3917 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3918 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3919 | *p = f; |
| 3920 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | static int perf_event_set_output(struct perf_event *event, |
| 3924 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3925 | 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] | 3926 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3927 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3928 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3929 | void (*func)(struct perf_event *); |
| 3930 | u32 flags = arg; |
| 3931 | |
| 3932 | switch (cmd) { |
| 3933 | case PERF_EVENT_IOC_ENABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3934 | func = _perf_event_enable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3935 | break; |
| 3936 | case PERF_EVENT_IOC_DISABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3937 | func = _perf_event_disable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3938 | break; |
| 3939 | case PERF_EVENT_IOC_RESET: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3940 | func = _perf_event_reset; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3941 | break; |
| 3942 | |
| 3943 | case PERF_EVENT_IOC_REFRESH: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3944 | return _perf_event_refresh(event, arg); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3945 | |
| 3946 | case PERF_EVENT_IOC_PERIOD: |
| 3947 | return perf_event_period(event, (u64 __user *)arg); |
| 3948 | |
Jiri Olsa | cf4957f | 2012-10-24 13:37:58 +0200 | [diff] [blame] | 3949 | case PERF_EVENT_IOC_ID: |
| 3950 | { |
| 3951 | u64 id = primary_event_id(event); |
| 3952 | |
| 3953 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) |
| 3954 | return -EFAULT; |
| 3955 | return 0; |
| 3956 | } |
| 3957 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3958 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3959 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3960 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3961 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3962 | struct perf_event *output_event; |
| 3963 | struct fd output; |
| 3964 | ret = perf_fget_light(arg, &output); |
| 3965 | if (ret) |
| 3966 | return ret; |
| 3967 | output_event = output.file->private_data; |
| 3968 | ret = perf_event_set_output(event, output_event); |
| 3969 | fdput(output); |
| 3970 | } else { |
| 3971 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3972 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3973 | return ret; |
| 3974 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3975 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3976 | case PERF_EVENT_IOC_SET_FILTER: |
| 3977 | return perf_event_set_filter(event, (void __user *)arg); |
| 3978 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3979 | default: |
| 3980 | return -ENOTTY; |
| 3981 | } |
| 3982 | |
| 3983 | if (flags & PERF_IOC_FLAG_GROUP) |
| 3984 | perf_event_for_each(event, func); |
| 3985 | else |
| 3986 | perf_event_for_each_child(event, func); |
| 3987 | |
| 3988 | return 0; |
| 3989 | } |
| 3990 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3991 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 3992 | { |
| 3993 | struct perf_event *event = file->private_data; |
| 3994 | struct perf_event_context *ctx; |
| 3995 | long ret; |
| 3996 | |
| 3997 | ctx = perf_event_ctx_lock(event); |
| 3998 | ret = _perf_ioctl(event, cmd, arg); |
| 3999 | perf_event_ctx_unlock(event, ctx); |
| 4000 | |
| 4001 | return ret; |
| 4002 | } |
| 4003 | |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4004 | #ifdef CONFIG_COMPAT |
| 4005 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, |
| 4006 | unsigned long arg) |
| 4007 | { |
| 4008 | switch (_IOC_NR(cmd)) { |
| 4009 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): |
| 4010 | case _IOC_NR(PERF_EVENT_IOC_ID): |
| 4011 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
| 4012 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { |
| 4013 | cmd &= ~IOCSIZE_MASK; |
| 4014 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; |
| 4015 | } |
| 4016 | break; |
| 4017 | } |
| 4018 | return perf_ioctl(file, cmd, arg); |
| 4019 | } |
| 4020 | #else |
| 4021 | # define perf_compat_ioctl NULL |
| 4022 | #endif |
| 4023 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4024 | int perf_event_task_enable(void) |
| 4025 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4026 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4027 | struct perf_event *event; |
| 4028 | |
| 4029 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4030 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4031 | ctx = perf_event_ctx_lock(event); |
| 4032 | perf_event_for_each_child(event, _perf_event_enable); |
| 4033 | perf_event_ctx_unlock(event, ctx); |
| 4034 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4035 | mutex_unlock(¤t->perf_event_mutex); |
| 4036 | |
| 4037 | return 0; |
| 4038 | } |
| 4039 | |
| 4040 | int perf_event_task_disable(void) |
| 4041 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4042 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4043 | struct perf_event *event; |
| 4044 | |
| 4045 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4046 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4047 | ctx = perf_event_ctx_lock(event); |
| 4048 | perf_event_for_each_child(event, _perf_event_disable); |
| 4049 | perf_event_ctx_unlock(event, ctx); |
| 4050 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4051 | mutex_unlock(¤t->perf_event_mutex); |
| 4052 | |
| 4053 | return 0; |
| 4054 | } |
| 4055 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4056 | static int perf_event_index(struct perf_event *event) |
| 4057 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 4058 | if (event->hw.state & PERF_HES_STOPPED) |
| 4059 | return 0; |
| 4060 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4061 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 4062 | return 0; |
| 4063 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 4064 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4065 | } |
| 4066 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4067 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4068 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4069 | u64 *enabled, |
| 4070 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4071 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4072 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4073 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4074 | *now = perf_clock(); |
| 4075 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4076 | *enabled = ctx_time - event->tstamp_enabled; |
| 4077 | *running = ctx_time - event->tstamp_running; |
| 4078 | } |
| 4079 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4080 | static void perf_event_init_userpage(struct perf_event *event) |
| 4081 | { |
| 4082 | struct perf_event_mmap_page *userpg; |
| 4083 | struct ring_buffer *rb; |
| 4084 | |
| 4085 | rcu_read_lock(); |
| 4086 | rb = rcu_dereference(event->rb); |
| 4087 | if (!rb) |
| 4088 | goto unlock; |
| 4089 | |
| 4090 | userpg = rb->user_page; |
| 4091 | |
| 4092 | /* Allow new userspace to detect that bit 0 is deprecated */ |
| 4093 | userpg->cap_bit0_is_deprecated = 1; |
| 4094 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); |
| 4095 | |
| 4096 | unlock: |
| 4097 | rcu_read_unlock(); |
| 4098 | } |
| 4099 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4100 | void __weak arch_perf_update_userpage( |
| 4101 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4102 | { |
| 4103 | } |
| 4104 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4105 | /* |
| 4106 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 4107 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 4108 | * code calls this from NMI context. |
| 4109 | */ |
| 4110 | void perf_event_update_userpage(struct perf_event *event) |
| 4111 | { |
| 4112 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4113 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4114 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4115 | |
| 4116 | rcu_read_lock(); |
Peter Zijlstra | 5ec4c59 | 2013-08-02 21:16:30 +0200 | [diff] [blame] | 4117 | rb = rcu_dereference(event->rb); |
| 4118 | if (!rb) |
| 4119 | goto unlock; |
| 4120 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4121 | /* |
| 4122 | * compute total_time_enabled, total_time_running |
| 4123 | * based on snapshot values taken when the event |
| 4124 | * was last scheduled in. |
| 4125 | * |
| 4126 | * we cannot simply called update_context_time() |
| 4127 | * because of locking issue as we can be called in |
| 4128 | * NMI context |
| 4129 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4130 | calc_timer_values(event, &now, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4131 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4132 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4133 | /* |
| 4134 | * Disable preemption so as to not let the corresponding user-space |
| 4135 | * spin too long if we get preempted. |
| 4136 | */ |
| 4137 | preempt_disable(); |
| 4138 | ++userpg->lock; |
| 4139 | barrier(); |
| 4140 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4141 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 4142 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4143 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4144 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4145 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4146 | atomic64_read(&event->child_total_time_enabled); |
| 4147 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4148 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4149 | atomic64_read(&event->child_total_time_running); |
| 4150 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4151 | arch_perf_update_userpage(event, userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4152 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4153 | barrier(); |
| 4154 | ++userpg->lock; |
| 4155 | preempt_enable(); |
| 4156 | unlock: |
| 4157 | rcu_read_unlock(); |
| 4158 | } |
| 4159 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4160 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 4161 | { |
| 4162 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4163 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4164 | int ret = VM_FAULT_SIGBUS; |
| 4165 | |
| 4166 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 4167 | if (vmf->pgoff == 0) |
| 4168 | ret = 0; |
| 4169 | return ret; |
| 4170 | } |
| 4171 | |
| 4172 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4173 | rb = rcu_dereference(event->rb); |
| 4174 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4175 | goto unlock; |
| 4176 | |
| 4177 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 4178 | goto unlock; |
| 4179 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4180 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4181 | if (!vmf->page) |
| 4182 | goto unlock; |
| 4183 | |
| 4184 | get_page(vmf->page); |
| 4185 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 4186 | vmf->page->index = vmf->pgoff; |
| 4187 | |
| 4188 | ret = 0; |
| 4189 | unlock: |
| 4190 | rcu_read_unlock(); |
| 4191 | |
| 4192 | return ret; |
| 4193 | } |
| 4194 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4195 | static void ring_buffer_attach(struct perf_event *event, |
| 4196 | struct ring_buffer *rb) |
| 4197 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4198 | struct ring_buffer *old_rb = NULL; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4199 | unsigned long flags; |
| 4200 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4201 | if (event->rb) { |
| 4202 | /* |
| 4203 | * Should be impossible, we set this when removing |
| 4204 | * event->rb_entry and wait/clear when adding event->rb_entry. |
| 4205 | */ |
| 4206 | WARN_ON_ONCE(event->rcu_pending); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4207 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4208 | old_rb = event->rb; |
| 4209 | event->rcu_batches = get_state_synchronize_rcu(); |
| 4210 | event->rcu_pending = 1; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4211 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4212 | spin_lock_irqsave(&old_rb->event_lock, flags); |
| 4213 | list_del_rcu(&event->rb_entry); |
| 4214 | spin_unlock_irqrestore(&old_rb->event_lock, flags); |
| 4215 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4216 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4217 | if (event->rcu_pending && rb) { |
| 4218 | cond_synchronize_rcu(event->rcu_batches); |
| 4219 | event->rcu_pending = 0; |
| 4220 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4221 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4222 | if (rb) { |
| 4223 | spin_lock_irqsave(&rb->event_lock, flags); |
| 4224 | list_add_rcu(&event->rb_entry, &rb->event_list); |
| 4225 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 4226 | } |
| 4227 | |
| 4228 | rcu_assign_pointer(event->rb, rb); |
| 4229 | |
| 4230 | if (old_rb) { |
| 4231 | ring_buffer_put(old_rb); |
| 4232 | /* |
| 4233 | * Since we detached before setting the new rb, so that we |
| 4234 | * could attach the new rb, we could have missed a wakeup. |
| 4235 | * Provide it now. |
| 4236 | */ |
| 4237 | wake_up_all(&event->waitq); |
| 4238 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | static void ring_buffer_wakeup(struct perf_event *event) |
| 4242 | { |
| 4243 | struct ring_buffer *rb; |
| 4244 | |
| 4245 | rcu_read_lock(); |
| 4246 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4247 | if (rb) { |
| 4248 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 4249 | wake_up_all(&event->waitq); |
| 4250 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4251 | rcu_read_unlock(); |
| 4252 | } |
| 4253 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4254 | static void rb_free_rcu(struct rcu_head *rcu_head) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4255 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4256 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4257 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4258 | rb = container_of(rcu_head, struct ring_buffer, rcu_head); |
| 4259 | rb_free(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4260 | } |
| 4261 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4262 | static struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4263 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4264 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4265 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4266 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4267 | rb = rcu_dereference(event->rb); |
| 4268 | if (rb) { |
| 4269 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 4270 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4271 | } |
| 4272 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4273 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4274 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4275 | } |
| 4276 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4277 | static void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4278 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4279 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4280 | return; |
| 4281 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4282 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4283 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4284 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 4288 | { |
| 4289 | struct perf_event *event = vma->vm_file->private_data; |
| 4290 | |
| 4291 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4292 | atomic_inc(&event->rb->mmap_count); |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4293 | |
| 4294 | if (event->pmu->event_mapped) |
| 4295 | event->pmu->event_mapped(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4296 | } |
| 4297 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4298 | /* |
| 4299 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 4300 | * event, or through other events by use of perf_event_set_output(). |
| 4301 | * |
| 4302 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 4303 | * the buffer here, where we still have a VM context. This means we need |
| 4304 | * to detach all events redirecting to us. |
| 4305 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4306 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 4307 | { |
| 4308 | struct perf_event *event = vma->vm_file->private_data; |
| 4309 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4310 | struct ring_buffer *rb = ring_buffer_get(event); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4311 | struct user_struct *mmap_user = rb->mmap_user; |
| 4312 | int mmap_locked = rb->mmap_locked; |
| 4313 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4314 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4315 | if (event->pmu->event_unmapped) |
| 4316 | event->pmu->event_unmapped(event); |
| 4317 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4318 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4319 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4320 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4321 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4322 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4323 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4324 | mutex_unlock(&event->mmap_mutex); |
| 4325 | |
| 4326 | /* 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] | 4327 | if (atomic_read(&rb->mmap_count)) |
| 4328 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4329 | |
| 4330 | /* |
| 4331 | * No other mmap()s, detach from all other events that might redirect |
| 4332 | * into the now unreachable buffer. Somewhat complicated by the |
| 4333 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 4334 | */ |
| 4335 | again: |
| 4336 | rcu_read_lock(); |
| 4337 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 4338 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 4339 | /* |
| 4340 | * This event is en-route to free_event() which will |
| 4341 | * detach it and remove it from the list. |
| 4342 | */ |
| 4343 | continue; |
| 4344 | } |
| 4345 | rcu_read_unlock(); |
| 4346 | |
| 4347 | mutex_lock(&event->mmap_mutex); |
| 4348 | /* |
| 4349 | * Check we didn't race with perf_event_set_output() which can |
| 4350 | * swizzle the rb from under us while we were waiting to |
| 4351 | * acquire mmap_mutex. |
| 4352 | * |
| 4353 | * If we find a different rb; ignore this event, a next |
| 4354 | * iteration will no longer find it on the list. We have to |
| 4355 | * still restart the iteration to make sure we're not now |
| 4356 | * iterating the wrong list. |
| 4357 | */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4358 | if (event->rb == rb) |
| 4359 | ring_buffer_attach(event, NULL); |
| 4360 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4361 | mutex_unlock(&event->mmap_mutex); |
| 4362 | put_event(event); |
| 4363 | |
| 4364 | /* |
| 4365 | * Restart the iteration; either we're on the wrong list or |
| 4366 | * destroyed its integrity by doing a deletion. |
| 4367 | */ |
| 4368 | goto again; |
| 4369 | } |
| 4370 | rcu_read_unlock(); |
| 4371 | |
| 4372 | /* |
| 4373 | * It could be there's still a few 0-ref events on the list; they'll |
| 4374 | * get cleaned up by free_event() -- they'll also still have their |
| 4375 | * ref on the rb and will free it whenever they are done with it. |
| 4376 | * |
| 4377 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 4378 | * undo the VM accounting. |
| 4379 | */ |
| 4380 | |
| 4381 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 4382 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 4383 | free_uid(mmap_user); |
| 4384 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4385 | out_put: |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4386 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4387 | } |
| 4388 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 4389 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4390 | .open = perf_mmap_open, |
| 4391 | .close = perf_mmap_close, |
| 4392 | .fault = perf_mmap_fault, |
| 4393 | .page_mkwrite = perf_mmap_fault, |
| 4394 | }; |
| 4395 | |
| 4396 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 4397 | { |
| 4398 | struct perf_event *event = file->private_data; |
| 4399 | unsigned long user_locked, user_lock_limit; |
| 4400 | struct user_struct *user = current_user(); |
| 4401 | unsigned long locked, lock_limit; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4402 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4403 | unsigned long vma_size; |
| 4404 | unsigned long nr_pages; |
| 4405 | long user_extra, extra; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4406 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4407 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4408 | /* |
| 4409 | * Don't allow mmap() of inherited per-task counters. This would |
| 4410 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4411 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4412 | */ |
| 4413 | if (event->cpu == -1 && event->attr.inherit) |
| 4414 | return -EINVAL; |
| 4415 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4416 | if (!(vma->vm_flags & VM_SHARED)) |
| 4417 | return -EINVAL; |
| 4418 | |
| 4419 | vma_size = vma->vm_end - vma->vm_start; |
| 4420 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 4421 | |
| 4422 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4423 | * 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] | 4424 | * can do bitmasks instead of modulo. |
| 4425 | */ |
Kaixu Xia | 74390aa | 2015-01-27 17:55:12 +0800 | [diff] [blame] | 4426 | if (!is_power_of_2(nr_pages)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4427 | return -EINVAL; |
| 4428 | |
| 4429 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 4430 | return -EINVAL; |
| 4431 | |
| 4432 | if (vma->vm_pgoff != 0) |
| 4433 | return -EINVAL; |
| 4434 | |
| 4435 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4436 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4437 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4438 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4439 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4440 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4441 | goto unlock; |
| 4442 | } |
| 4443 | |
| 4444 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 4445 | /* |
| 4446 | * Raced against perf_mmap_close() through |
| 4447 | * perf_event_set_output(). Try again, hope for better |
| 4448 | * luck. |
| 4449 | */ |
| 4450 | mutex_unlock(&event->mmap_mutex); |
| 4451 | goto again; |
| 4452 | } |
| 4453 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4454 | goto unlock; |
| 4455 | } |
| 4456 | |
| 4457 | user_extra = nr_pages + 1; |
| 4458 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 4459 | |
| 4460 | /* |
| 4461 | * Increase the limit linearly with more CPUs: |
| 4462 | */ |
| 4463 | user_lock_limit *= num_online_cpus(); |
| 4464 | |
| 4465 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 4466 | |
| 4467 | extra = 0; |
| 4468 | if (user_locked > user_lock_limit) |
| 4469 | extra = user_locked - user_lock_limit; |
| 4470 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 4471 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4472 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 4473 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4474 | |
| 4475 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 4476 | !capable(CAP_IPC_LOCK)) { |
| 4477 | ret = -EPERM; |
| 4478 | goto unlock; |
| 4479 | } |
| 4480 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4481 | WARN_ON(event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4482 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4483 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4484 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4485 | |
Vince Weaver | 4ec8363 | 2011-06-01 15:15:36 -0400 | [diff] [blame] | 4486 | rb = rb_alloc(nr_pages, |
| 4487 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 4488 | event->cpu, flags); |
| 4489 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4490 | if (!rb) { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4491 | ret = -ENOMEM; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4492 | goto unlock; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4493 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4494 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4495 | atomic_set(&rb->mmap_count, 1); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4496 | rb->mmap_locked = extra; |
| 4497 | rb->mmap_user = get_current_user(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4498 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4499 | atomic_long_add(user_extra, &user->locked_vm); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4500 | vma->vm_mm->pinned_vm += extra; |
| 4501 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4502 | ring_buffer_attach(event, rb); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4503 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4504 | perf_event_init_userpage(event); |
Peter Zijlstra | 9a0f05c | 2011-11-21 15:13:29 +0100 | [diff] [blame] | 4505 | perf_event_update_userpage(event); |
| 4506 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4507 | unlock: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4508 | if (!ret) |
| 4509 | atomic_inc(&event->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4510 | mutex_unlock(&event->mmap_mutex); |
| 4511 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4512 | /* |
| 4513 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 4514 | * vma. |
| 4515 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4516 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4517 | vma->vm_ops = &perf_mmap_vmops; |
| 4518 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4519 | if (event->pmu->event_mapped) |
| 4520 | event->pmu->event_mapped(event); |
| 4521 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4522 | return ret; |
| 4523 | } |
| 4524 | |
| 4525 | static int perf_fasync(int fd, struct file *filp, int on) |
| 4526 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4527 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4528 | struct perf_event *event = filp->private_data; |
| 4529 | int retval; |
| 4530 | |
| 4531 | mutex_lock(&inode->i_mutex); |
| 4532 | retval = fasync_helper(fd, filp, on, &event->fasync); |
| 4533 | mutex_unlock(&inode->i_mutex); |
| 4534 | |
| 4535 | if (retval < 0) |
| 4536 | return retval; |
| 4537 | |
| 4538 | return 0; |
| 4539 | } |
| 4540 | |
| 4541 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 4542 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4543 | .release = perf_release, |
| 4544 | .read = perf_read, |
| 4545 | .poll = perf_poll, |
| 4546 | .unlocked_ioctl = perf_ioctl, |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4547 | .compat_ioctl = perf_compat_ioctl, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4548 | .mmap = perf_mmap, |
| 4549 | .fasync = perf_fasync, |
| 4550 | }; |
| 4551 | |
| 4552 | /* |
| 4553 | * Perf event wakeup |
| 4554 | * |
| 4555 | * If there's data, ensure we set the poll() state and publish everything |
| 4556 | * to user-space before waking everybody up. |
| 4557 | */ |
| 4558 | |
| 4559 | void perf_event_wakeup(struct perf_event *event) |
| 4560 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4561 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4562 | |
| 4563 | if (event->pending_kill) { |
| 4564 | kill_fasync(&event->fasync, SIGIO, event->pending_kill); |
| 4565 | event->pending_kill = 0; |
| 4566 | } |
| 4567 | } |
| 4568 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 4569 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4570 | { |
| 4571 | struct perf_event *event = container_of(entry, |
| 4572 | struct perf_event, pending); |
| 4573 | |
| 4574 | if (event->pending_disable) { |
| 4575 | event->pending_disable = 0; |
| 4576 | __perf_event_disable(event); |
| 4577 | } |
| 4578 | |
| 4579 | if (event->pending_wakeup) { |
| 4580 | event->pending_wakeup = 0; |
| 4581 | perf_event_wakeup(event); |
| 4582 | } |
| 4583 | } |
| 4584 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4585 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 4586 | * We assume there is only KVM supporting the callbacks. |
| 4587 | * Later on, we might change it to a list if there is |
| 4588 | * another virtualization implementation supporting the callbacks. |
| 4589 | */ |
| 4590 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 4591 | |
| 4592 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4593 | { |
| 4594 | perf_guest_cbs = cbs; |
| 4595 | return 0; |
| 4596 | } |
| 4597 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 4598 | |
| 4599 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4600 | { |
| 4601 | perf_guest_cbs = NULL; |
| 4602 | return 0; |
| 4603 | } |
| 4604 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 4605 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4606 | static void |
| 4607 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 4608 | struct pt_regs *regs, u64 mask) |
| 4609 | { |
| 4610 | int bit; |
| 4611 | |
| 4612 | for_each_set_bit(bit, (const unsigned long *) &mask, |
| 4613 | sizeof(mask) * BITS_PER_BYTE) { |
| 4614 | u64 val; |
| 4615 | |
| 4616 | val = perf_reg_value(regs, bit); |
| 4617 | perf_output_put(handle, val); |
| 4618 | } |
| 4619 | } |
| 4620 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 4621 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 4622 | struct pt_regs *regs, |
| 4623 | struct pt_regs *regs_user_copy) |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4624 | { |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 4625 | if (user_mode(regs)) { |
| 4626 | regs_user->abi = perf_reg_abi(current); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 4627 | regs_user->regs = regs; |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 4628 | } else if (current->mm) { |
| 4629 | perf_get_regs_user(regs_user, regs, regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 4630 | } else { |
| 4631 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; |
| 4632 | regs_user->regs = NULL; |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4633 | } |
| 4634 | } |
| 4635 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 4636 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
| 4637 | struct pt_regs *regs) |
| 4638 | { |
| 4639 | regs_intr->regs = regs; |
| 4640 | regs_intr->abi = perf_reg_abi(current); |
| 4641 | } |
| 4642 | |
| 4643 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4644 | /* |
| 4645 | * Get remaining task size from user stack pointer. |
| 4646 | * |
| 4647 | * It'd be better to take stack vma map and limit this more |
| 4648 | * precisly, but there's no way to get it safely under interrupt, |
| 4649 | * so using TASK_SIZE as limit. |
| 4650 | */ |
| 4651 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 4652 | { |
| 4653 | unsigned long addr = perf_user_stack_pointer(regs); |
| 4654 | |
| 4655 | if (!addr || addr >= TASK_SIZE) |
| 4656 | return 0; |
| 4657 | |
| 4658 | return TASK_SIZE - addr; |
| 4659 | } |
| 4660 | |
| 4661 | static u16 |
| 4662 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 4663 | struct pt_regs *regs) |
| 4664 | { |
| 4665 | u64 task_size; |
| 4666 | |
| 4667 | /* No regs, no stack pointer, no dump. */ |
| 4668 | if (!regs) |
| 4669 | return 0; |
| 4670 | |
| 4671 | /* |
| 4672 | * Check if we fit in with the requested stack size into the: |
| 4673 | * - TASK_SIZE |
| 4674 | * If we don't, we limit the size to the TASK_SIZE. |
| 4675 | * |
| 4676 | * - remaining sample size |
| 4677 | * If we don't, we customize the stack size to |
| 4678 | * fit in to the remaining sample size. |
| 4679 | */ |
| 4680 | |
| 4681 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 4682 | stack_size = min(stack_size, (u16) task_size); |
| 4683 | |
| 4684 | /* Current header size plus static size and dynamic size. */ |
| 4685 | header_size += 2 * sizeof(u64); |
| 4686 | |
| 4687 | /* Do we fit in with the current stack dump size? */ |
| 4688 | if ((u16) (header_size + stack_size) < header_size) { |
| 4689 | /* |
| 4690 | * If we overflow the maximum size for the sample, |
| 4691 | * we customize the stack dump size to fit in. |
| 4692 | */ |
| 4693 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 4694 | stack_size = round_up(stack_size, sizeof(u64)); |
| 4695 | } |
| 4696 | |
| 4697 | return stack_size; |
| 4698 | } |
| 4699 | |
| 4700 | static void |
| 4701 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 4702 | struct pt_regs *regs) |
| 4703 | { |
| 4704 | /* Case of a kernel thread, nothing to dump */ |
| 4705 | if (!regs) { |
| 4706 | u64 size = 0; |
| 4707 | perf_output_put(handle, size); |
| 4708 | } else { |
| 4709 | unsigned long sp; |
| 4710 | unsigned int rem; |
| 4711 | u64 dyn_size; |
| 4712 | |
| 4713 | /* |
| 4714 | * We dump: |
| 4715 | * static size |
| 4716 | * - the size requested by user or the best one we can fit |
| 4717 | * in to the sample max size |
| 4718 | * data |
| 4719 | * - user stack dump data |
| 4720 | * dynamic size |
| 4721 | * - the actual dumped size |
| 4722 | */ |
| 4723 | |
| 4724 | /* Static size. */ |
| 4725 | perf_output_put(handle, dump_size); |
| 4726 | |
| 4727 | /* Data. */ |
| 4728 | sp = perf_user_stack_pointer(regs); |
| 4729 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 4730 | dyn_size = dump_size - rem; |
| 4731 | |
| 4732 | perf_output_skip(handle, rem); |
| 4733 | |
| 4734 | /* Dynamic size. */ |
| 4735 | perf_output_put(handle, dyn_size); |
| 4736 | } |
| 4737 | } |
| 4738 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4739 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 4740 | struct perf_sample_data *data, |
| 4741 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4742 | { |
| 4743 | u64 sample_type = event->attr.sample_type; |
| 4744 | |
| 4745 | data->type = sample_type; |
| 4746 | header->size += event->id_header_size; |
| 4747 | |
| 4748 | if (sample_type & PERF_SAMPLE_TID) { |
| 4749 | /* namespace issues */ |
| 4750 | data->tid_entry.pid = perf_event_pid(event, current); |
| 4751 | data->tid_entry.tid = perf_event_tid(event, current); |
| 4752 | } |
| 4753 | |
| 4754 | if (sample_type & PERF_SAMPLE_TIME) |
| 4755 | data->time = perf_clock(); |
| 4756 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4757 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4758 | data->id = primary_event_id(event); |
| 4759 | |
| 4760 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4761 | data->stream_id = event->id; |
| 4762 | |
| 4763 | if (sample_type & PERF_SAMPLE_CPU) { |
| 4764 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 4765 | data->cpu_entry.reserved = 0; |
| 4766 | } |
| 4767 | } |
| 4768 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4769 | void perf_event_header__init_id(struct perf_event_header *header, |
| 4770 | struct perf_sample_data *data, |
| 4771 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4772 | { |
| 4773 | if (event->attr.sample_id_all) |
| 4774 | __perf_event_header__init_id(header, data, event); |
| 4775 | } |
| 4776 | |
| 4777 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 4778 | struct perf_sample_data *data) |
| 4779 | { |
| 4780 | u64 sample_type = data->type; |
| 4781 | |
| 4782 | if (sample_type & PERF_SAMPLE_TID) |
| 4783 | perf_output_put(handle, data->tid_entry); |
| 4784 | |
| 4785 | if (sample_type & PERF_SAMPLE_TIME) |
| 4786 | perf_output_put(handle, data->time); |
| 4787 | |
| 4788 | if (sample_type & PERF_SAMPLE_ID) |
| 4789 | perf_output_put(handle, data->id); |
| 4790 | |
| 4791 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4792 | perf_output_put(handle, data->stream_id); |
| 4793 | |
| 4794 | if (sample_type & PERF_SAMPLE_CPU) |
| 4795 | perf_output_put(handle, data->cpu_entry); |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4796 | |
| 4797 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4798 | perf_output_put(handle, data->id); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4799 | } |
| 4800 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4801 | void perf_event__output_id_sample(struct perf_event *event, |
| 4802 | struct perf_output_handle *handle, |
| 4803 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4804 | { |
| 4805 | if (event->attr.sample_id_all) |
| 4806 | __perf_event__output_id_sample(handle, sample); |
| 4807 | } |
| 4808 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4809 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4810 | struct perf_event *event, |
| 4811 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4812 | { |
| 4813 | u64 read_format = event->attr.read_format; |
| 4814 | u64 values[4]; |
| 4815 | int n = 0; |
| 4816 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4817 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4818 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4819 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4820 | atomic64_read(&event->child_total_time_enabled); |
| 4821 | } |
| 4822 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4823 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4824 | atomic64_read(&event->child_total_time_running); |
| 4825 | } |
| 4826 | if (read_format & PERF_FORMAT_ID) |
| 4827 | values[n++] = primary_event_id(event); |
| 4828 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4829 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | /* |
| 4833 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 4834 | */ |
| 4835 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4836 | struct perf_event *event, |
| 4837 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4838 | { |
| 4839 | struct perf_event *leader = event->group_leader, *sub; |
| 4840 | u64 read_format = event->attr.read_format; |
| 4841 | u64 values[5]; |
| 4842 | int n = 0; |
| 4843 | |
| 4844 | values[n++] = 1 + leader->nr_siblings; |
| 4845 | |
| 4846 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4847 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4848 | |
| 4849 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4850 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4851 | |
| 4852 | if (leader != event) |
| 4853 | leader->pmu->read(leader); |
| 4854 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4855 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4856 | if (read_format & PERF_FORMAT_ID) |
| 4857 | values[n++] = primary_event_id(leader); |
| 4858 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4859 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4860 | |
| 4861 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 4862 | n = 0; |
| 4863 | |
Jiri Olsa | 6f5ab00 | 2012-10-15 20:13:45 +0200 | [diff] [blame] | 4864 | if ((sub != event) && |
| 4865 | (sub->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4866 | sub->pmu->read(sub); |
| 4867 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4868 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4869 | if (read_format & PERF_FORMAT_ID) |
| 4870 | values[n++] = primary_event_id(sub); |
| 4871 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4872 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4873 | } |
| 4874 | } |
| 4875 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4876 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 4877 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4878 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4879 | static void perf_output_read(struct perf_output_handle *handle, |
| 4880 | struct perf_event *event) |
| 4881 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4882 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4883 | u64 read_format = event->attr.read_format; |
| 4884 | |
| 4885 | /* |
| 4886 | * compute total_time_enabled, total_time_running |
| 4887 | * based on snapshot values taken when the event |
| 4888 | * was last scheduled in. |
| 4889 | * |
| 4890 | * we cannot simply called update_context_time() |
| 4891 | * because of locking issue as we are called in |
| 4892 | * NMI context |
| 4893 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4894 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4895 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4896 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4897 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4898 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4899 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4900 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | void perf_output_sample(struct perf_output_handle *handle, |
| 4904 | struct perf_event_header *header, |
| 4905 | struct perf_sample_data *data, |
| 4906 | struct perf_event *event) |
| 4907 | { |
| 4908 | u64 sample_type = data->type; |
| 4909 | |
| 4910 | perf_output_put(handle, *header); |
| 4911 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4912 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4913 | perf_output_put(handle, data->id); |
| 4914 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4915 | if (sample_type & PERF_SAMPLE_IP) |
| 4916 | perf_output_put(handle, data->ip); |
| 4917 | |
| 4918 | if (sample_type & PERF_SAMPLE_TID) |
| 4919 | perf_output_put(handle, data->tid_entry); |
| 4920 | |
| 4921 | if (sample_type & PERF_SAMPLE_TIME) |
| 4922 | perf_output_put(handle, data->time); |
| 4923 | |
| 4924 | if (sample_type & PERF_SAMPLE_ADDR) |
| 4925 | perf_output_put(handle, data->addr); |
| 4926 | |
| 4927 | if (sample_type & PERF_SAMPLE_ID) |
| 4928 | perf_output_put(handle, data->id); |
| 4929 | |
| 4930 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4931 | perf_output_put(handle, data->stream_id); |
| 4932 | |
| 4933 | if (sample_type & PERF_SAMPLE_CPU) |
| 4934 | perf_output_put(handle, data->cpu_entry); |
| 4935 | |
| 4936 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 4937 | perf_output_put(handle, data->period); |
| 4938 | |
| 4939 | if (sample_type & PERF_SAMPLE_READ) |
| 4940 | perf_output_read(handle, event); |
| 4941 | |
| 4942 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4943 | if (data->callchain) { |
| 4944 | int size = 1; |
| 4945 | |
| 4946 | if (data->callchain) |
| 4947 | size += data->callchain->nr; |
| 4948 | |
| 4949 | size *= sizeof(u64); |
| 4950 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4951 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4952 | } else { |
| 4953 | u64 nr = 0; |
| 4954 | perf_output_put(handle, nr); |
| 4955 | } |
| 4956 | } |
| 4957 | |
| 4958 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4959 | if (data->raw) { |
| 4960 | perf_output_put(handle, data->raw->size); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4961 | __output_copy(handle, data->raw->data, |
| 4962 | data->raw->size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4963 | } else { |
| 4964 | struct { |
| 4965 | u32 size; |
| 4966 | u32 data; |
| 4967 | } raw = { |
| 4968 | .size = sizeof(u32), |
| 4969 | .data = 0, |
| 4970 | }; |
| 4971 | perf_output_put(handle, raw); |
| 4972 | } |
| 4973 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4974 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4975 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4976 | if (data->br_stack) { |
| 4977 | size_t size; |
| 4978 | |
| 4979 | size = data->br_stack->nr |
| 4980 | * sizeof(struct perf_branch_entry); |
| 4981 | |
| 4982 | perf_output_put(handle, data->br_stack->nr); |
| 4983 | perf_output_copy(handle, data->br_stack->entries, size); |
| 4984 | } else { |
| 4985 | /* |
| 4986 | * we always store at least the value of nr |
| 4987 | */ |
| 4988 | u64 nr = 0; |
| 4989 | perf_output_put(handle, nr); |
| 4990 | } |
| 4991 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4992 | |
| 4993 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 4994 | u64 abi = data->regs_user.abi; |
| 4995 | |
| 4996 | /* |
| 4997 | * If there are no regs to dump, notice it through |
| 4998 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 4999 | */ |
| 5000 | perf_output_put(handle, abi); |
| 5001 | |
| 5002 | if (abi) { |
| 5003 | u64 mask = event->attr.sample_regs_user; |
| 5004 | perf_output_sample_regs(handle, |
| 5005 | data->regs_user.regs, |
| 5006 | mask); |
| 5007 | } |
| 5008 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5009 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5010 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5011 | perf_output_sample_ustack(handle, |
| 5012 | data->stack_user_size, |
| 5013 | data->regs_user.regs); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5014 | } |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 5015 | |
| 5016 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 5017 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 5018 | |
| 5019 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 5020 | perf_output_put(handle, data->data_src.val); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5021 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 5022 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 5023 | perf_output_put(handle, data->txn); |
| 5024 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5025 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5026 | u64 abi = data->regs_intr.abi; |
| 5027 | /* |
| 5028 | * If there are no regs to dump, notice it through |
| 5029 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5030 | */ |
| 5031 | perf_output_put(handle, abi); |
| 5032 | |
| 5033 | if (abi) { |
| 5034 | u64 mask = event->attr.sample_regs_intr; |
| 5035 | |
| 5036 | perf_output_sample_regs(handle, |
| 5037 | data->regs_intr.regs, |
| 5038 | mask); |
| 5039 | } |
| 5040 | } |
| 5041 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5042 | if (!event->attr.watermark) { |
| 5043 | int wakeup_events = event->attr.wakeup_events; |
| 5044 | |
| 5045 | if (wakeup_events) { |
| 5046 | struct ring_buffer *rb = handle->rb; |
| 5047 | int events = local_inc_return(&rb->events); |
| 5048 | |
| 5049 | if (events >= wakeup_events) { |
| 5050 | local_sub(wakeup_events, &rb->events); |
| 5051 | local_inc(&rb->wakeup); |
| 5052 | } |
| 5053 | } |
| 5054 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5055 | } |
| 5056 | |
| 5057 | void perf_prepare_sample(struct perf_event_header *header, |
| 5058 | struct perf_sample_data *data, |
| 5059 | struct perf_event *event, |
| 5060 | struct pt_regs *regs) |
| 5061 | { |
| 5062 | u64 sample_type = event->attr.sample_type; |
| 5063 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5064 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5065 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5066 | |
| 5067 | header->misc = 0; |
| 5068 | header->misc |= perf_misc_flags(regs); |
| 5069 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5070 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5071 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5072 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5073 | data->ip = perf_instruction_pointer(regs); |
| 5074 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5075 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5076 | int size = 1; |
| 5077 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5078 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5079 | |
| 5080 | if (data->callchain) |
| 5081 | size += data->callchain->nr; |
| 5082 | |
| 5083 | header->size += size * sizeof(u64); |
| 5084 | } |
| 5085 | |
| 5086 | if (sample_type & PERF_SAMPLE_RAW) { |
| 5087 | int size = sizeof(u32); |
| 5088 | |
| 5089 | if (data->raw) |
| 5090 | size += data->raw->size; |
| 5091 | else |
| 5092 | size += sizeof(u32); |
| 5093 | |
| 5094 | WARN_ON_ONCE(size & (sizeof(u64)-1)); |
| 5095 | header->size += size; |
| 5096 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5097 | |
| 5098 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5099 | int size = sizeof(u64); /* nr */ |
| 5100 | if (data->br_stack) { |
| 5101 | size += data->br_stack->nr |
| 5102 | * sizeof(struct perf_branch_entry); |
| 5103 | } |
| 5104 | header->size += size; |
| 5105 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5106 | |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5107 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5108 | perf_sample_regs_user(&data->regs_user, regs, |
| 5109 | &data->regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5110 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5111 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5112 | /* regs dump ABI info */ |
| 5113 | int size = sizeof(u64); |
| 5114 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5115 | if (data->regs_user.regs) { |
| 5116 | u64 mask = event->attr.sample_regs_user; |
| 5117 | size += hweight64(mask) * sizeof(u64); |
| 5118 | } |
| 5119 | |
| 5120 | header->size += size; |
| 5121 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5122 | |
| 5123 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 5124 | /* |
| 5125 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 5126 | * processed as the last one or have additional check added |
| 5127 | * in case new sample type is added, because we could eat |
| 5128 | * up the rest of the sample size. |
| 5129 | */ |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5130 | u16 stack_size = event->attr.sample_stack_user; |
| 5131 | u16 size = sizeof(u64); |
| 5132 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5133 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5134 | data->regs_user.regs); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5135 | |
| 5136 | /* |
| 5137 | * If there is something to dump, add space for the dump |
| 5138 | * itself and for the field that tells the dynamic size, |
| 5139 | * which is how many have been actually dumped. |
| 5140 | */ |
| 5141 | if (stack_size) |
| 5142 | size += sizeof(u64) + stack_size; |
| 5143 | |
| 5144 | data->stack_user_size = stack_size; |
| 5145 | header->size += size; |
| 5146 | } |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5147 | |
| 5148 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5149 | /* regs dump ABI info */ |
| 5150 | int size = sizeof(u64); |
| 5151 | |
| 5152 | perf_sample_regs_intr(&data->regs_intr, regs); |
| 5153 | |
| 5154 | if (data->regs_intr.regs) { |
| 5155 | u64 mask = event->attr.sample_regs_intr; |
| 5156 | |
| 5157 | size += hweight64(mask) * sizeof(u64); |
| 5158 | } |
| 5159 | |
| 5160 | header->size += size; |
| 5161 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5162 | } |
| 5163 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5164 | static void perf_event_output(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5165 | struct perf_sample_data *data, |
| 5166 | struct pt_regs *regs) |
| 5167 | { |
| 5168 | struct perf_output_handle handle; |
| 5169 | struct perf_event_header header; |
| 5170 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5171 | /* protect the callchain buffers */ |
| 5172 | rcu_read_lock(); |
| 5173 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5174 | perf_prepare_sample(&header, data, event, regs); |
| 5175 | |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5176 | if (perf_output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5177 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5178 | |
| 5179 | perf_output_sample(&handle, &header, data, event); |
| 5180 | |
| 5181 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5182 | |
| 5183 | exit: |
| 5184 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | /* |
| 5188 | * read event_id |
| 5189 | */ |
| 5190 | |
| 5191 | struct perf_read_event { |
| 5192 | struct perf_event_header header; |
| 5193 | |
| 5194 | u32 pid; |
| 5195 | u32 tid; |
| 5196 | }; |
| 5197 | |
| 5198 | static void |
| 5199 | perf_event_read_event(struct perf_event *event, |
| 5200 | struct task_struct *task) |
| 5201 | { |
| 5202 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5203 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5204 | struct perf_read_event read_event = { |
| 5205 | .header = { |
| 5206 | .type = PERF_RECORD_READ, |
| 5207 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5208 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5209 | }, |
| 5210 | .pid = perf_event_pid(event, task), |
| 5211 | .tid = perf_event_tid(event, task), |
| 5212 | }; |
| 5213 | int ret; |
| 5214 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5215 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5216 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5217 | if (ret) |
| 5218 | return; |
| 5219 | |
| 5220 | perf_output_put(&handle, read_event); |
| 5221 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5222 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5223 | |
| 5224 | perf_output_end(&handle); |
| 5225 | } |
| 5226 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5227 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
| 5228 | |
| 5229 | static void |
| 5230 | perf_event_aux_ctx(struct perf_event_context *ctx, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5231 | perf_event_aux_output_cb output, |
| 5232 | void *data) |
| 5233 | { |
| 5234 | struct perf_event *event; |
| 5235 | |
| 5236 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 5237 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 5238 | continue; |
| 5239 | if (!event_filter_match(event)) |
| 5240 | continue; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5241 | output(event, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5242 | } |
| 5243 | } |
| 5244 | |
| 5245 | static void |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5246 | perf_event_aux(perf_event_aux_output_cb output, void *data, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5247 | struct perf_event_context *task_ctx) |
| 5248 | { |
| 5249 | struct perf_cpu_context *cpuctx; |
| 5250 | struct perf_event_context *ctx; |
| 5251 | struct pmu *pmu; |
| 5252 | int ctxn; |
| 5253 | |
| 5254 | rcu_read_lock(); |
| 5255 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 5256 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 5257 | if (cpuctx->unique_pmu != pmu) |
| 5258 | goto next; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5259 | perf_event_aux_ctx(&cpuctx->ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5260 | if (task_ctx) |
| 5261 | goto next; |
| 5262 | ctxn = pmu->task_ctx_nr; |
| 5263 | if (ctxn < 0) |
| 5264 | goto next; |
| 5265 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 5266 | if (ctx) |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5267 | perf_event_aux_ctx(ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5268 | next: |
| 5269 | put_cpu_ptr(pmu->pmu_cpu_context); |
| 5270 | } |
| 5271 | |
| 5272 | if (task_ctx) { |
| 5273 | preempt_disable(); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5274 | perf_event_aux_ctx(task_ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5275 | preempt_enable(); |
| 5276 | } |
| 5277 | rcu_read_unlock(); |
| 5278 | } |
| 5279 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5280 | /* |
| 5281 | * task tracking -- fork/exit |
| 5282 | * |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5283 | * 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] | 5284 | */ |
| 5285 | |
| 5286 | struct perf_task_event { |
| 5287 | struct task_struct *task; |
| 5288 | struct perf_event_context *task_ctx; |
| 5289 | |
| 5290 | struct { |
| 5291 | struct perf_event_header header; |
| 5292 | |
| 5293 | u32 pid; |
| 5294 | u32 ppid; |
| 5295 | u32 tid; |
| 5296 | u32 ptid; |
| 5297 | u64 time; |
| 5298 | } event_id; |
| 5299 | }; |
| 5300 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5301 | static int perf_event_task_match(struct perf_event *event) |
| 5302 | { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5303 | return event->attr.comm || event->attr.mmap || |
| 5304 | event->attr.mmap2 || event->attr.mmap_data || |
| 5305 | event->attr.task; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5306 | } |
| 5307 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5308 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5309 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5310 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5311 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5312 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5313 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5314 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5315 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 5316 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5317 | if (!perf_event_task_match(event)) |
| 5318 | return; |
| 5319 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5320 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5321 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5322 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5323 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 5324 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5325 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5326 | |
| 5327 | task_event->event_id.pid = perf_event_pid(event, task); |
| 5328 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 5329 | |
| 5330 | task_event->event_id.tid = perf_event_tid(event, task); |
| 5331 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 5332 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5333 | perf_output_put(&handle, task_event->event_id); |
| 5334 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5335 | perf_event__output_id_sample(event, &handle, &sample); |
| 5336 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5337 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5338 | out: |
| 5339 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5340 | } |
| 5341 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5342 | static void perf_event_task(struct task_struct *task, |
| 5343 | struct perf_event_context *task_ctx, |
| 5344 | int new) |
| 5345 | { |
| 5346 | struct perf_task_event task_event; |
| 5347 | |
| 5348 | if (!atomic_read(&nr_comm_events) && |
| 5349 | !atomic_read(&nr_mmap_events) && |
| 5350 | !atomic_read(&nr_task_events)) |
| 5351 | return; |
| 5352 | |
| 5353 | task_event = (struct perf_task_event){ |
| 5354 | .task = task, |
| 5355 | .task_ctx = task_ctx, |
| 5356 | .event_id = { |
| 5357 | .header = { |
| 5358 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 5359 | .misc = 0, |
| 5360 | .size = sizeof(task_event.event_id), |
| 5361 | }, |
| 5362 | /* .pid */ |
| 5363 | /* .ppid */ |
| 5364 | /* .tid */ |
| 5365 | /* .ptid */ |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 5366 | .time = perf_clock(), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5367 | }, |
| 5368 | }; |
| 5369 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5370 | perf_event_aux(perf_event_task_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5371 | &task_event, |
| 5372 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | void perf_event_fork(struct task_struct *task) |
| 5376 | { |
| 5377 | perf_event_task(task, NULL, 1); |
| 5378 | } |
| 5379 | |
| 5380 | /* |
| 5381 | * comm tracking |
| 5382 | */ |
| 5383 | |
| 5384 | struct perf_comm_event { |
| 5385 | struct task_struct *task; |
| 5386 | char *comm; |
| 5387 | int comm_size; |
| 5388 | |
| 5389 | struct { |
| 5390 | struct perf_event_header header; |
| 5391 | |
| 5392 | u32 pid; |
| 5393 | u32 tid; |
| 5394 | } event_id; |
| 5395 | }; |
| 5396 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5397 | static int perf_event_comm_match(struct perf_event *event) |
| 5398 | { |
| 5399 | return event->attr.comm; |
| 5400 | } |
| 5401 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5402 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5403 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5404 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5405 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5406 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5407 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5408 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5409 | int ret; |
| 5410 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5411 | if (!perf_event_comm_match(event)) |
| 5412 | return; |
| 5413 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5414 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 5415 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5416 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5417 | |
| 5418 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5419 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5420 | |
| 5421 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 5422 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 5423 | |
| 5424 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5425 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5426 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5427 | |
| 5428 | perf_event__output_id_sample(event, &handle, &sample); |
| 5429 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5430 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5431 | out: |
| 5432 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5433 | } |
| 5434 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5435 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 5436 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5437 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5438 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5439 | |
| 5440 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 5441 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5442 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 5443 | |
| 5444 | comm_event->comm = comm; |
| 5445 | comm_event->comm_size = size; |
| 5446 | |
| 5447 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5448 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5449 | perf_event_aux(perf_event_comm_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5450 | comm_event, |
| 5451 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5452 | } |
| 5453 | |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5454 | void perf_event_comm(struct task_struct *task, bool exec) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5455 | { |
| 5456 | struct perf_comm_event comm_event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5457 | |
| 5458 | if (!atomic_read(&nr_comm_events)) |
| 5459 | return; |
| 5460 | |
| 5461 | comm_event = (struct perf_comm_event){ |
| 5462 | .task = task, |
| 5463 | /* .comm */ |
| 5464 | /* .comm_size */ |
| 5465 | .event_id = { |
| 5466 | .header = { |
| 5467 | .type = PERF_RECORD_COMM, |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5468 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5469 | /* .size */ |
| 5470 | }, |
| 5471 | /* .pid */ |
| 5472 | /* .tid */ |
| 5473 | }, |
| 5474 | }; |
| 5475 | |
| 5476 | perf_event_comm_event(&comm_event); |
| 5477 | } |
| 5478 | |
| 5479 | /* |
| 5480 | * mmap tracking |
| 5481 | */ |
| 5482 | |
| 5483 | struct perf_mmap_event { |
| 5484 | struct vm_area_struct *vma; |
| 5485 | |
| 5486 | const char *file_name; |
| 5487 | int file_size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5488 | int maj, min; |
| 5489 | u64 ino; |
| 5490 | u64 ino_generation; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5491 | u32 prot, flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5492 | |
| 5493 | struct { |
| 5494 | struct perf_event_header header; |
| 5495 | |
| 5496 | u32 pid; |
| 5497 | u32 tid; |
| 5498 | u64 start; |
| 5499 | u64 len; |
| 5500 | u64 pgoff; |
| 5501 | } event_id; |
| 5502 | }; |
| 5503 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5504 | static int perf_event_mmap_match(struct perf_event *event, |
| 5505 | void *data) |
| 5506 | { |
| 5507 | struct perf_mmap_event *mmap_event = data; |
| 5508 | struct vm_area_struct *vma = mmap_event->vma; |
| 5509 | int executable = vma->vm_flags & VM_EXEC; |
| 5510 | |
| 5511 | return (!executable && event->attr.mmap_data) || |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5512 | (executable && (event->attr.mmap || event->attr.mmap2)); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5513 | } |
| 5514 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5515 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5516 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5517 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5518 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5519 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5520 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5521 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5522 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5523 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5524 | if (!perf_event_mmap_match(event, data)) |
| 5525 | return; |
| 5526 | |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5527 | if (event->attr.mmap2) { |
| 5528 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; |
| 5529 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); |
| 5530 | mmap_event->event_id.header.size += sizeof(mmap_event->min); |
| 5531 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); |
Arnaldo Carvalho de Melo | d008d52 | 2013-09-10 10:24:05 -0300 | [diff] [blame] | 5532 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5533 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
| 5534 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5535 | } |
| 5536 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5537 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 5538 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5539 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5540 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5541 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5542 | |
| 5543 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 5544 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 5545 | |
| 5546 | perf_output_put(&handle, mmap_event->event_id); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5547 | |
| 5548 | if (event->attr.mmap2) { |
| 5549 | perf_output_put(&handle, mmap_event->maj); |
| 5550 | perf_output_put(&handle, mmap_event->min); |
| 5551 | perf_output_put(&handle, mmap_event->ino); |
| 5552 | perf_output_put(&handle, mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5553 | perf_output_put(&handle, mmap_event->prot); |
| 5554 | perf_output_put(&handle, mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5555 | } |
| 5556 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5557 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5558 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5559 | |
| 5560 | perf_event__output_id_sample(event, &handle, &sample); |
| 5561 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5562 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5563 | out: |
| 5564 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5565 | } |
| 5566 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5567 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 5568 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5569 | struct vm_area_struct *vma = mmap_event->vma; |
| 5570 | struct file *file = vma->vm_file; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5571 | int maj = 0, min = 0; |
| 5572 | u64 ino = 0, gen = 0; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5573 | u32 prot = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5574 | unsigned int size; |
| 5575 | char tmp[16]; |
| 5576 | char *buf = NULL; |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5577 | char *name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5578 | |
| 5579 | if (file) { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5580 | struct inode *inode; |
| 5581 | dev_t dev; |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5582 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5583 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5584 | if (!buf) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5585 | name = "//enomem"; |
| 5586 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5587 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5588 | /* |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5589 | * d_path() works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5590 | * need to add enough zero bytes after the string to handle |
| 5591 | * the 64bit alignment we do later. |
| 5592 | */ |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5593 | name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5594 | if (IS_ERR(name)) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5595 | name = "//toolong"; |
| 5596 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5597 | } |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5598 | inode = file_inode(vma->vm_file); |
| 5599 | dev = inode->i_sb->s_dev; |
| 5600 | ino = inode->i_ino; |
| 5601 | gen = inode->i_generation; |
| 5602 | maj = MAJOR(dev); |
| 5603 | min = MINOR(dev); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5604 | |
| 5605 | if (vma->vm_flags & VM_READ) |
| 5606 | prot |= PROT_READ; |
| 5607 | if (vma->vm_flags & VM_WRITE) |
| 5608 | prot |= PROT_WRITE; |
| 5609 | if (vma->vm_flags & VM_EXEC) |
| 5610 | prot |= PROT_EXEC; |
| 5611 | |
| 5612 | if (vma->vm_flags & VM_MAYSHARE) |
| 5613 | flags = MAP_SHARED; |
| 5614 | else |
| 5615 | flags = MAP_PRIVATE; |
| 5616 | |
| 5617 | if (vma->vm_flags & VM_DENYWRITE) |
| 5618 | flags |= MAP_DENYWRITE; |
| 5619 | if (vma->vm_flags & VM_MAYEXEC) |
| 5620 | flags |= MAP_EXECUTABLE; |
| 5621 | if (vma->vm_flags & VM_LOCKED) |
| 5622 | flags |= MAP_LOCKED; |
| 5623 | if (vma->vm_flags & VM_HUGETLB) |
| 5624 | flags |= MAP_HUGETLB; |
| 5625 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5626 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5627 | } else { |
Jiri Olsa | fbe26ab | 2014-07-14 17:57:19 +0200 | [diff] [blame] | 5628 | if (vma->vm_ops && vma->vm_ops->name) { |
| 5629 | name = (char *) vma->vm_ops->name(vma); |
| 5630 | if (name) |
| 5631 | goto cpy_name; |
| 5632 | } |
| 5633 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5634 | name = (char *)arch_vma_name(vma); |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5635 | if (name) |
| 5636 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5637 | |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5638 | if (vma->vm_start <= vma->vm_mm->start_brk && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5639 | vma->vm_end >= vma->vm_mm->brk) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5640 | name = "[heap]"; |
| 5641 | goto cpy_name; |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5642 | } |
| 5643 | if (vma->vm_start <= vma->vm_mm->start_stack && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5644 | vma->vm_end >= vma->vm_mm->start_stack) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5645 | name = "[stack]"; |
| 5646 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5647 | } |
| 5648 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5649 | name = "//anon"; |
| 5650 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5651 | } |
| 5652 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5653 | cpy_name: |
| 5654 | strlcpy(tmp, name, sizeof(tmp)); |
| 5655 | name = tmp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5656 | got_name: |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5657 | /* |
| 5658 | * Since our buffer works in 8 byte units we need to align our string |
| 5659 | * size to a multiple of 8. However, we must guarantee the tail end is |
| 5660 | * zero'd out to avoid leaking random bits to userspace. |
| 5661 | */ |
| 5662 | size = strlen(name)+1; |
| 5663 | while (!IS_ALIGNED(size, sizeof(u64))) |
| 5664 | name[size++] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5665 | |
| 5666 | mmap_event->file_name = name; |
| 5667 | mmap_event->file_size = size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5668 | mmap_event->maj = maj; |
| 5669 | mmap_event->min = min; |
| 5670 | mmap_event->ino = ino; |
| 5671 | mmap_event->ino_generation = gen; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5672 | mmap_event->prot = prot; |
| 5673 | mmap_event->flags = flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5674 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 5675 | if (!(vma->vm_flags & VM_EXEC)) |
| 5676 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 5677 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5678 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 5679 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5680 | perf_event_aux(perf_event_mmap_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5681 | mmap_event, |
| 5682 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5683 | |
| 5684 | kfree(buf); |
| 5685 | } |
| 5686 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 5687 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5688 | { |
| 5689 | struct perf_mmap_event mmap_event; |
| 5690 | |
| 5691 | if (!atomic_read(&nr_mmap_events)) |
| 5692 | return; |
| 5693 | |
| 5694 | mmap_event = (struct perf_mmap_event){ |
| 5695 | .vma = vma, |
| 5696 | /* .file_name */ |
| 5697 | /* .file_size */ |
| 5698 | .event_id = { |
| 5699 | .header = { |
| 5700 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5701 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5702 | /* .size */ |
| 5703 | }, |
| 5704 | /* .pid */ |
| 5705 | /* .tid */ |
| 5706 | .start = vma->vm_start, |
| 5707 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 5708 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5709 | }, |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5710 | /* .maj (attr_mmap2 only) */ |
| 5711 | /* .min (attr_mmap2 only) */ |
| 5712 | /* .ino (attr_mmap2 only) */ |
| 5713 | /* .ino_generation (attr_mmap2 only) */ |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5714 | /* .prot (attr_mmap2 only) */ |
| 5715 | /* .flags (attr_mmap2 only) */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5716 | }; |
| 5717 | |
| 5718 | perf_event_mmap_event(&mmap_event); |
| 5719 | } |
| 5720 | |
| 5721 | /* |
| 5722 | * IRQ throttle logging |
| 5723 | */ |
| 5724 | |
| 5725 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 5726 | { |
| 5727 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5728 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5729 | int ret; |
| 5730 | |
| 5731 | struct { |
| 5732 | struct perf_event_header header; |
| 5733 | u64 time; |
| 5734 | u64 id; |
| 5735 | u64 stream_id; |
| 5736 | } throttle_event = { |
| 5737 | .header = { |
| 5738 | .type = PERF_RECORD_THROTTLE, |
| 5739 | .misc = 0, |
| 5740 | .size = sizeof(throttle_event), |
| 5741 | }, |
| 5742 | .time = perf_clock(), |
| 5743 | .id = primary_event_id(event), |
| 5744 | .stream_id = event->id, |
| 5745 | }; |
| 5746 | |
| 5747 | if (enable) |
| 5748 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 5749 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5750 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 5751 | |
| 5752 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5753 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5754 | if (ret) |
| 5755 | return; |
| 5756 | |
| 5757 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5758 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5759 | perf_output_end(&handle); |
| 5760 | } |
| 5761 | |
| 5762 | /* |
| 5763 | * Generic event overflow handling, sampling. |
| 5764 | */ |
| 5765 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5766 | static int __perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5767 | int throttle, struct perf_sample_data *data, |
| 5768 | struct pt_regs *regs) |
| 5769 | { |
| 5770 | int events = atomic_read(&event->event_limit); |
| 5771 | struct hw_perf_event *hwc = &event->hw; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5772 | u64 seq; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5773 | int ret = 0; |
| 5774 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 5775 | /* |
| 5776 | * Non-sampling counters might still use the PMI to fold short |
| 5777 | * hardware counters, ignore those. |
| 5778 | */ |
| 5779 | if (unlikely(!is_sampling_event(event))) |
| 5780 | return 0; |
| 5781 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5782 | seq = __this_cpu_read(perf_throttled_seq); |
| 5783 | if (seq != hwc->interrupts_seq) { |
| 5784 | hwc->interrupts_seq = seq; |
| 5785 | hwc->interrupts = 1; |
| 5786 | } else { |
| 5787 | hwc->interrupts++; |
| 5788 | if (unlikely(throttle |
| 5789 | && hwc->interrupts >= max_samples_per_tick)) { |
| 5790 | __this_cpu_inc(perf_throttled_count); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 5791 | hwc->interrupts = MAX_INTERRUPTS; |
| 5792 | perf_log_throttle(event, 0); |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 5793 | tick_nohz_full_kick(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5794 | ret = 1; |
| 5795 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5796 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5797 | |
| 5798 | if (event->attr.freq) { |
| 5799 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5800 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5801 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5802 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5803 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5804 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 5805 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5806 | } |
| 5807 | |
| 5808 | /* |
| 5809 | * XXX event_limit might not quite work as expected on inherited |
| 5810 | * events |
| 5811 | */ |
| 5812 | |
| 5813 | event->pending_kill = POLL_IN; |
| 5814 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 5815 | ret = 1; |
| 5816 | event->pending_kill = POLL_HUP; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5817 | event->pending_disable = 1; |
| 5818 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5819 | } |
| 5820 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5821 | if (event->overflow_handler) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5822 | event->overflow_handler(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5823 | else |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5824 | perf_event_output(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5825 | |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5826 | if (event->fasync && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5827 | event->pending_wakeup = 1; |
| 5828 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5829 | } |
| 5830 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5831 | return ret; |
| 5832 | } |
| 5833 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5834 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5835 | struct perf_sample_data *data, |
| 5836 | struct pt_regs *regs) |
| 5837 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5838 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | /* |
| 5842 | * Generic software event infrastructure |
| 5843 | */ |
| 5844 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5845 | struct swevent_htable { |
| 5846 | struct swevent_hlist *swevent_hlist; |
| 5847 | struct mutex hlist_mutex; |
| 5848 | int hlist_refcount; |
| 5849 | |
| 5850 | /* Recursion avoidance in each contexts */ |
| 5851 | int recursion[PERF_NR_CONTEXTS]; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5852 | |
| 5853 | /* Keeps track of cpu being initialized/exited */ |
| 5854 | bool online; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5855 | }; |
| 5856 | |
| 5857 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 5858 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5859 | /* |
| 5860 | * We directly increment event->count and keep a second value in |
| 5861 | * event->hw.period_left to count intervals. This period event |
| 5862 | * is kept in the range [-sample_period, 0] so that we can use the |
| 5863 | * sign as trigger. |
| 5864 | */ |
| 5865 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 5866 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5867 | { |
| 5868 | struct hw_perf_event *hwc = &event->hw; |
| 5869 | u64 period = hwc->last_period; |
| 5870 | u64 nr, offset; |
| 5871 | s64 old, val; |
| 5872 | |
| 5873 | hwc->last_period = hwc->sample_period; |
| 5874 | |
| 5875 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5876 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5877 | if (val < 0) |
| 5878 | return 0; |
| 5879 | |
| 5880 | nr = div64_u64(period + val, period); |
| 5881 | offset = nr * period; |
| 5882 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5883 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5884 | goto again; |
| 5885 | |
| 5886 | return nr; |
| 5887 | } |
| 5888 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5889 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5890 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5891 | struct pt_regs *regs) |
| 5892 | { |
| 5893 | struct hw_perf_event *hwc = &event->hw; |
| 5894 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5895 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5896 | if (!overflow) |
| 5897 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5898 | |
| 5899 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 5900 | return; |
| 5901 | |
| 5902 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5903 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5904 | data, regs)) { |
| 5905 | /* |
| 5906 | * We inhibit the overflow from happening when |
| 5907 | * hwc->interrupts == MAX_INTERRUPTS. |
| 5908 | */ |
| 5909 | break; |
| 5910 | } |
| 5911 | throttle = 1; |
| 5912 | } |
| 5913 | } |
| 5914 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5915 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5916 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5917 | struct pt_regs *regs) |
| 5918 | { |
| 5919 | struct hw_perf_event *hwc = &event->hw; |
| 5920 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5921 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5922 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5923 | if (!regs) |
| 5924 | return; |
| 5925 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5926 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5927 | return; |
| 5928 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 5929 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 5930 | data->period = nr; |
| 5931 | return perf_swevent_overflow(event, 1, data, regs); |
| 5932 | } else |
| 5933 | data->period = event->hw.last_period; |
| 5934 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5935 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5936 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5937 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5938 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5939 | return; |
| 5940 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5941 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5942 | } |
| 5943 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5944 | static int perf_exclude_event(struct perf_event *event, |
| 5945 | struct pt_regs *regs) |
| 5946 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5947 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 5948 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5949 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5950 | if (regs) { |
| 5951 | if (event->attr.exclude_user && user_mode(regs)) |
| 5952 | return 1; |
| 5953 | |
| 5954 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 5955 | return 1; |
| 5956 | } |
| 5957 | |
| 5958 | return 0; |
| 5959 | } |
| 5960 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5961 | static int perf_swevent_match(struct perf_event *event, |
| 5962 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5963 | u32 event_id, |
| 5964 | struct perf_sample_data *data, |
| 5965 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5966 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5967 | if (event->attr.type != type) |
| 5968 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5969 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5970 | if (event->attr.config != event_id) |
| 5971 | return 0; |
| 5972 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5973 | if (perf_exclude_event(event, regs)) |
| 5974 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5975 | |
| 5976 | return 1; |
| 5977 | } |
| 5978 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5979 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5980 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5981 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5982 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5983 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 5984 | } |
| 5985 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5986 | static inline struct hlist_head * |
| 5987 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5988 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5989 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5990 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5991 | return &hlist->heads[hash]; |
| 5992 | } |
| 5993 | |
| 5994 | /* For the read side: events when they trigger */ |
| 5995 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5996 | 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] | 5997 | { |
| 5998 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5999 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6000 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6001 | if (!hlist) |
| 6002 | return NULL; |
| 6003 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6004 | return __find_swevent_head(hlist, type, event_id); |
| 6005 | } |
| 6006 | |
| 6007 | /* For the event head insertion and removal in the hlist */ |
| 6008 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6009 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6010 | { |
| 6011 | struct swevent_hlist *hlist; |
| 6012 | u32 event_id = event->attr.config; |
| 6013 | u64 type = event->attr.type; |
| 6014 | |
| 6015 | /* |
| 6016 | * Event scheduling is always serialized against hlist allocation |
| 6017 | * and release. Which makes the protected version suitable here. |
| 6018 | * The context lock guarantees that. |
| 6019 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6020 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6021 | lockdep_is_held(&event->ctx->lock)); |
| 6022 | if (!hlist) |
| 6023 | return NULL; |
| 6024 | |
| 6025 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6026 | } |
| 6027 | |
| 6028 | 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] | 6029 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6030 | struct perf_sample_data *data, |
| 6031 | struct pt_regs *regs) |
| 6032 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6033 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6034 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6035 | struct hlist_head *head; |
| 6036 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6037 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6038 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6039 | if (!head) |
| 6040 | goto end; |
| 6041 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6042 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6043 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6044 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6045 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6046 | end: |
| 6047 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6048 | } |
| 6049 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6050 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
| 6051 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6052 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6053 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6054 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 6055 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6056 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6057 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 6058 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6059 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 6060 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6061 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6062 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6063 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6064 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 6065 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6066 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6067 | 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] | 6068 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 6069 | struct perf_sample_data data; |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6070 | |
| 6071 | if (WARN_ON_ONCE(!regs)) |
| 6072 | return; |
| 6073 | |
| 6074 | perf_sample_data_init(&data, addr, 0); |
| 6075 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
| 6076 | } |
| 6077 | |
| 6078 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
| 6079 | { |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6080 | int rctx; |
| 6081 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6082 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6083 | rctx = perf_swevent_get_recursion_context(); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6084 | if (unlikely(rctx < 0)) |
| 6085 | goto fail; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6086 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6087 | ___perf_sw_event(event_id, nr, regs, addr); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6088 | |
| 6089 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6090 | fail: |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6091 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | static void perf_swevent_read(struct perf_event *event) |
| 6095 | { |
| 6096 | } |
| 6097 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6098 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6099 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6100 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6101 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6102 | struct hlist_head *head; |
| 6103 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6104 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6105 | hwc->last_period = hwc->sample_period; |
| 6106 | perf_swevent_set_period(event); |
| 6107 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6108 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6109 | hwc->state = !(flags & PERF_EF_START); |
| 6110 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6111 | head = find_swevent_head(swhash, event); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 6112 | if (!head) { |
| 6113 | /* |
| 6114 | * We can race with cpu hotplug code. Do not |
| 6115 | * WARN if the cpu just got unplugged. |
| 6116 | */ |
| 6117 | WARN_ON_ONCE(swhash->online); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6118 | return -EINVAL; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 6119 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6120 | |
| 6121 | hlist_add_head_rcu(&event->hlist_entry, head); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 6122 | perf_event_update_userpage(event); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6123 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6124 | return 0; |
| 6125 | } |
| 6126 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6127 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6128 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6129 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6130 | } |
| 6131 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6132 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6133 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6134 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6135 | } |
| 6136 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6137 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6138 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6139 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6140 | } |
| 6141 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6142 | /* Deref the hlist from the update side */ |
| 6143 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6144 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6145 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6146 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 6147 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6148 | } |
| 6149 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6150 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6151 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6152 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6153 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6154 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6155 | return; |
| 6156 | |
Andreea-Cristina Bernat | 70691d4 | 2014-08-22 16:26:05 +0300 | [diff] [blame] | 6157 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 6158 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6159 | } |
| 6160 | |
| 6161 | static void swevent_hlist_put_cpu(struct perf_event *event, int cpu) |
| 6162 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6163 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6164 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6165 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6166 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6167 | if (!--swhash->hlist_refcount) |
| 6168 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6169 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6170 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6171 | } |
| 6172 | |
| 6173 | static void swevent_hlist_put(struct perf_event *event) |
| 6174 | { |
| 6175 | int cpu; |
| 6176 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6177 | for_each_possible_cpu(cpu) |
| 6178 | swevent_hlist_put_cpu(event, cpu); |
| 6179 | } |
| 6180 | |
| 6181 | static int swevent_hlist_get_cpu(struct perf_event *event, int cpu) |
| 6182 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6183 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6184 | int err = 0; |
| 6185 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6186 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6187 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6188 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6189 | struct swevent_hlist *hlist; |
| 6190 | |
| 6191 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 6192 | if (!hlist) { |
| 6193 | err = -ENOMEM; |
| 6194 | goto exit; |
| 6195 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6196 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6197 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6198 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 6199 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6200 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6201 | |
| 6202 | return err; |
| 6203 | } |
| 6204 | |
| 6205 | static int swevent_hlist_get(struct perf_event *event) |
| 6206 | { |
| 6207 | int err; |
| 6208 | int cpu, failed_cpu; |
| 6209 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6210 | get_online_cpus(); |
| 6211 | for_each_possible_cpu(cpu) { |
| 6212 | err = swevent_hlist_get_cpu(event, cpu); |
| 6213 | if (err) { |
| 6214 | failed_cpu = cpu; |
| 6215 | goto fail; |
| 6216 | } |
| 6217 | } |
| 6218 | put_online_cpus(); |
| 6219 | |
| 6220 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 6221 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6222 | for_each_possible_cpu(cpu) { |
| 6223 | if (cpu == failed_cpu) |
| 6224 | break; |
| 6225 | swevent_hlist_put_cpu(event, cpu); |
| 6226 | } |
| 6227 | |
| 6228 | put_online_cpus(); |
| 6229 | return err; |
| 6230 | } |
| 6231 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6232 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6233 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6234 | static void sw_perf_event_destroy(struct perf_event *event) |
| 6235 | { |
| 6236 | u64 event_id = event->attr.config; |
| 6237 | |
| 6238 | WARN_ON(event->parent); |
| 6239 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6240 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6241 | swevent_hlist_put(event); |
| 6242 | } |
| 6243 | |
| 6244 | static int perf_swevent_init(struct perf_event *event) |
| 6245 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 6246 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6247 | |
| 6248 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6249 | return -ENOENT; |
| 6250 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6251 | /* |
| 6252 | * no branch sampling for software events |
| 6253 | */ |
| 6254 | if (has_branch_stack(event)) |
| 6255 | return -EOPNOTSUPP; |
| 6256 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6257 | switch (event_id) { |
| 6258 | case PERF_COUNT_SW_CPU_CLOCK: |
| 6259 | case PERF_COUNT_SW_TASK_CLOCK: |
| 6260 | return -ENOENT; |
| 6261 | |
| 6262 | default: |
| 6263 | break; |
| 6264 | } |
| 6265 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 6266 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6267 | return -ENOENT; |
| 6268 | |
| 6269 | if (!event->parent) { |
| 6270 | int err; |
| 6271 | |
| 6272 | err = swevent_hlist_get(event); |
| 6273 | if (err) |
| 6274 | return err; |
| 6275 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6276 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6277 | event->destroy = sw_perf_event_destroy; |
| 6278 | } |
| 6279 | |
| 6280 | return 0; |
| 6281 | } |
| 6282 | |
| 6283 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6284 | .task_ctx_nr = perf_sw_context, |
| 6285 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6286 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6287 | .add = perf_swevent_add, |
| 6288 | .del = perf_swevent_del, |
| 6289 | .start = perf_swevent_start, |
| 6290 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6291 | .read = perf_swevent_read, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6292 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6293 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6294 | #ifdef CONFIG_EVENT_TRACING |
| 6295 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6296 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6297 | struct perf_sample_data *data) |
| 6298 | { |
| 6299 | void *record = data->raw->data; |
| 6300 | |
| 6301 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 6302 | return 1; |
| 6303 | return 0; |
| 6304 | } |
| 6305 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6306 | static int perf_tp_event_match(struct perf_event *event, |
| 6307 | struct perf_sample_data *data, |
| 6308 | struct pt_regs *regs) |
| 6309 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 6310 | if (event->hw.state & PERF_HES_STOPPED) |
| 6311 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 6312 | /* |
| 6313 | * All tracepoints are from kernel-space. |
| 6314 | */ |
| 6315 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6316 | return 0; |
| 6317 | |
| 6318 | if (!perf_tp_filter_match(event, data)) |
| 6319 | return 0; |
| 6320 | |
| 6321 | return 1; |
| 6322 | } |
| 6323 | |
| 6324 | 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] | 6325 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 6326 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6327 | { |
| 6328 | struct perf_sample_data data; |
| 6329 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6330 | |
| 6331 | struct perf_raw_record raw = { |
| 6332 | .size = entry_size, |
| 6333 | .data = record, |
| 6334 | }; |
| 6335 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6336 | perf_sample_data_init(&data, addr, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6337 | data.raw = &raw; |
| 6338 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6339 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6340 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6341 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6342 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 6343 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 6344 | /* |
| 6345 | * If we got specified a target task, also iterate its context and |
| 6346 | * deliver this event there too. |
| 6347 | */ |
| 6348 | if (task && task != current) { |
| 6349 | struct perf_event_context *ctx; |
| 6350 | struct trace_entry *entry = record; |
| 6351 | |
| 6352 | rcu_read_lock(); |
| 6353 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 6354 | if (!ctx) |
| 6355 | goto unlock; |
| 6356 | |
| 6357 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 6358 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6359 | continue; |
| 6360 | if (event->attr.config != entry->type) |
| 6361 | continue; |
| 6362 | if (perf_tp_event_match(event, &data, regs)) |
| 6363 | perf_swevent_event(event, count, &data, regs); |
| 6364 | } |
| 6365 | unlock: |
| 6366 | rcu_read_unlock(); |
| 6367 | } |
| 6368 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 6369 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6370 | } |
| 6371 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 6372 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6373 | static void tp_perf_event_destroy(struct perf_event *event) |
| 6374 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6375 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6376 | } |
| 6377 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6378 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6379 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6380 | int err; |
| 6381 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6382 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6383 | return -ENOENT; |
| 6384 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6385 | /* |
| 6386 | * no branch sampling for tracepoint events |
| 6387 | */ |
| 6388 | if (has_branch_stack(event)) |
| 6389 | return -EOPNOTSUPP; |
| 6390 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6391 | err = perf_trace_init(event); |
| 6392 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6393 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6394 | |
| 6395 | event->destroy = tp_perf_event_destroy; |
| 6396 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6397 | return 0; |
| 6398 | } |
| 6399 | |
| 6400 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6401 | .task_ctx_nr = perf_sw_context, |
| 6402 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6403 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6404 | .add = perf_trace_add, |
| 6405 | .del = perf_trace_del, |
| 6406 | .start = perf_swevent_start, |
| 6407 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6408 | .read = perf_swevent_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6409 | }; |
| 6410 | |
| 6411 | static inline void perf_tp_register(void) |
| 6412 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6413 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6414 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6415 | |
| 6416 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6417 | { |
| 6418 | char *filter_str; |
| 6419 | int ret; |
| 6420 | |
| 6421 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6422 | return -EINVAL; |
| 6423 | |
| 6424 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 6425 | if (IS_ERR(filter_str)) |
| 6426 | return PTR_ERR(filter_str); |
| 6427 | |
| 6428 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); |
| 6429 | |
| 6430 | kfree(filter_str); |
| 6431 | return ret; |
| 6432 | } |
| 6433 | |
| 6434 | static void perf_event_free_filter(struct perf_event *event) |
| 6435 | { |
| 6436 | ftrace_profile_free_filter(event); |
| 6437 | } |
| 6438 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6439 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6440 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6441 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6442 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6443 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6444 | |
| 6445 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6446 | { |
| 6447 | return -ENOENT; |
| 6448 | } |
| 6449 | |
| 6450 | static void perf_event_free_filter(struct perf_event *event) |
| 6451 | { |
| 6452 | } |
| 6453 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 6454 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6455 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6456 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6457 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6458 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6459 | struct perf_sample_data sample; |
| 6460 | struct pt_regs *regs = data; |
| 6461 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6462 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6463 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6464 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6465 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6466 | } |
| 6467 | #endif |
| 6468 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6469 | /* |
| 6470 | * hrtimer based swevent callback |
| 6471 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6472 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6473 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6474 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6475 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 6476 | struct perf_sample_data data; |
| 6477 | struct pt_regs *regs; |
| 6478 | struct perf_event *event; |
| 6479 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6480 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6481 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6482 | |
| 6483 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 6484 | return HRTIMER_NORESTART; |
| 6485 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6486 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6487 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6488 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6489 | regs = get_irq_regs(); |
| 6490 | |
| 6491 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 6492 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 6493 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6494 | ret = HRTIMER_NORESTART; |
| 6495 | } |
| 6496 | |
| 6497 | period = max_t(u64, 10000, event->hw.sample_period); |
| 6498 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 6499 | |
| 6500 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6501 | } |
| 6502 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6503 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6504 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6505 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6506 | s64 period; |
| 6507 | |
| 6508 | if (!is_sampling_event(event)) |
| 6509 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6510 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6511 | period = local64_read(&hwc->period_left); |
| 6512 | if (period) { |
| 6513 | if (period < 0) |
| 6514 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6515 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6516 | local64_set(&hwc->period_left, 0); |
| 6517 | } else { |
| 6518 | period = max_t(u64, 10000, hwc->sample_period); |
| 6519 | } |
| 6520 | __hrtimer_start_range_ns(&hwc->hrtimer, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6521 | ns_to_ktime(period), 0, |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 6522 | HRTIMER_MODE_REL_PINNED, 0); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6523 | } |
| 6524 | |
| 6525 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 6526 | { |
| 6527 | struct hw_perf_event *hwc = &event->hw; |
| 6528 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6529 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6530 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6531 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6532 | |
| 6533 | hrtimer_cancel(&hwc->hrtimer); |
| 6534 | } |
| 6535 | } |
| 6536 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6537 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 6538 | { |
| 6539 | struct hw_perf_event *hwc = &event->hw; |
| 6540 | |
| 6541 | if (!is_sampling_event(event)) |
| 6542 | return; |
| 6543 | |
| 6544 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 6545 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 6546 | |
| 6547 | /* |
| 6548 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 6549 | * mapping and avoid the whole period adjust feedback stuff. |
| 6550 | */ |
| 6551 | if (event->attr.freq) { |
| 6552 | long freq = event->attr.sample_freq; |
| 6553 | |
| 6554 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 6555 | hwc->sample_period = event->attr.sample_period; |
| 6556 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 6557 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6558 | event->attr.freq = 0; |
| 6559 | } |
| 6560 | } |
| 6561 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6562 | /* |
| 6563 | * Software event: cpu wall time clock |
| 6564 | */ |
| 6565 | |
| 6566 | static void cpu_clock_event_update(struct perf_event *event) |
| 6567 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6568 | s64 prev; |
| 6569 | u64 now; |
| 6570 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6571 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6572 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6573 | local64_add(now - prev, &event->count); |
| 6574 | } |
| 6575 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6576 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6577 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6578 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6579 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6580 | } |
| 6581 | |
| 6582 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 6583 | { |
| 6584 | perf_swevent_cancel_hrtimer(event); |
| 6585 | cpu_clock_event_update(event); |
| 6586 | } |
| 6587 | |
| 6588 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 6589 | { |
| 6590 | if (flags & PERF_EF_START) |
| 6591 | cpu_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 6592 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6593 | |
| 6594 | return 0; |
| 6595 | } |
| 6596 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6597 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6598 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6599 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6600 | } |
| 6601 | |
| 6602 | static void cpu_clock_event_read(struct perf_event *event) |
| 6603 | { |
| 6604 | cpu_clock_event_update(event); |
| 6605 | } |
| 6606 | |
| 6607 | static int cpu_clock_event_init(struct perf_event *event) |
| 6608 | { |
| 6609 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6610 | return -ENOENT; |
| 6611 | |
| 6612 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 6613 | return -ENOENT; |
| 6614 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6615 | /* |
| 6616 | * no branch sampling for software events |
| 6617 | */ |
| 6618 | if (has_branch_stack(event)) |
| 6619 | return -EOPNOTSUPP; |
| 6620 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6621 | perf_swevent_init_hrtimer(event); |
| 6622 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6623 | return 0; |
| 6624 | } |
| 6625 | |
| 6626 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6627 | .task_ctx_nr = perf_sw_context, |
| 6628 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6629 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6630 | .add = cpu_clock_event_add, |
| 6631 | .del = cpu_clock_event_del, |
| 6632 | .start = cpu_clock_event_start, |
| 6633 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6634 | .read = cpu_clock_event_read, |
| 6635 | }; |
| 6636 | |
| 6637 | /* |
| 6638 | * Software event: task time clock |
| 6639 | */ |
| 6640 | |
| 6641 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 6642 | { |
| 6643 | u64 prev; |
| 6644 | s64 delta; |
| 6645 | |
| 6646 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6647 | delta = now - prev; |
| 6648 | local64_add(delta, &event->count); |
| 6649 | } |
| 6650 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6651 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6652 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6653 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6654 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 6658 | { |
| 6659 | perf_swevent_cancel_hrtimer(event); |
| 6660 | task_clock_event_update(event, event->ctx->time); |
| 6661 | } |
| 6662 | |
| 6663 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 6664 | { |
| 6665 | if (flags & PERF_EF_START) |
| 6666 | task_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 6667 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6668 | |
| 6669 | return 0; |
| 6670 | } |
| 6671 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6672 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6673 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6674 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6675 | } |
| 6676 | |
| 6677 | static void task_clock_event_read(struct perf_event *event) |
| 6678 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 6679 | u64 now = perf_clock(); |
| 6680 | u64 delta = now - event->ctx->timestamp; |
| 6681 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6682 | |
| 6683 | task_clock_event_update(event, time); |
| 6684 | } |
| 6685 | |
| 6686 | static int task_clock_event_init(struct perf_event *event) |
| 6687 | { |
| 6688 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6689 | return -ENOENT; |
| 6690 | |
| 6691 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 6692 | return -ENOENT; |
| 6693 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6694 | /* |
| 6695 | * no branch sampling for software events |
| 6696 | */ |
| 6697 | if (has_branch_stack(event)) |
| 6698 | return -EOPNOTSUPP; |
| 6699 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6700 | perf_swevent_init_hrtimer(event); |
| 6701 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6702 | return 0; |
| 6703 | } |
| 6704 | |
| 6705 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6706 | .task_ctx_nr = perf_sw_context, |
| 6707 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6708 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6709 | .add = task_clock_event_add, |
| 6710 | .del = task_clock_event_del, |
| 6711 | .start = task_clock_event_start, |
| 6712 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6713 | .read = task_clock_event_read, |
| 6714 | }; |
| 6715 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6716 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 6717 | { |
| 6718 | } |
| 6719 | |
| 6720 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 6721 | { |
| 6722 | return 0; |
| 6723 | } |
| 6724 | |
| 6725 | static void perf_pmu_start_txn(struct pmu *pmu) |
| 6726 | { |
| 6727 | perf_pmu_disable(pmu); |
| 6728 | } |
| 6729 | |
| 6730 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 6731 | { |
| 6732 | perf_pmu_enable(pmu); |
| 6733 | return 0; |
| 6734 | } |
| 6735 | |
| 6736 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 6737 | { |
| 6738 | perf_pmu_enable(pmu); |
| 6739 | } |
| 6740 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6741 | static int perf_event_idx_default(struct perf_event *event) |
| 6742 | { |
Peter Zijlstra | c719f56 | 2014-10-21 11:10:21 +0200 | [diff] [blame] | 6743 | return 0; |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6744 | } |
| 6745 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6746 | /* |
| 6747 | * Ensures all contexts with the same task_ctx_nr have the same |
| 6748 | * pmu_cpu_context too. |
| 6749 | */ |
Mark Rutland | 9e31704 | 2014-02-10 17:44:18 +0000 | [diff] [blame] | 6750 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6751 | { |
| 6752 | struct pmu *pmu; |
| 6753 | |
| 6754 | if (ctxn < 0) |
| 6755 | return NULL; |
| 6756 | |
| 6757 | list_for_each_entry(pmu, &pmus, entry) { |
| 6758 | if (pmu->task_ctx_nr == ctxn) |
| 6759 | return pmu->pmu_cpu_context; |
| 6760 | } |
| 6761 | |
| 6762 | return NULL; |
| 6763 | } |
| 6764 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6765 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6766 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6767 | int cpu; |
| 6768 | |
| 6769 | for_each_possible_cpu(cpu) { |
| 6770 | struct perf_cpu_context *cpuctx; |
| 6771 | |
| 6772 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6773 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6774 | if (cpuctx->unique_pmu == old_pmu) |
| 6775 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6776 | } |
| 6777 | } |
| 6778 | |
| 6779 | static void free_pmu_context(struct pmu *pmu) |
| 6780 | { |
| 6781 | struct pmu *i; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6782 | |
| 6783 | mutex_lock(&pmus_lock); |
| 6784 | /* |
| 6785 | * Like a real lame refcount. |
| 6786 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6787 | list_for_each_entry(i, &pmus, entry) { |
| 6788 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 6789 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6790 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6791 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6792 | } |
| 6793 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6794 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6795 | out: |
| 6796 | mutex_unlock(&pmus_lock); |
| 6797 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6798 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6799 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6800 | static ssize_t |
| 6801 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 6802 | { |
| 6803 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6804 | |
| 6805 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 6806 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6807 | static DEVICE_ATTR_RO(type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6808 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6809 | static ssize_t |
| 6810 | perf_event_mux_interval_ms_show(struct device *dev, |
| 6811 | struct device_attribute *attr, |
| 6812 | char *page) |
| 6813 | { |
| 6814 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6815 | |
| 6816 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 6817 | } |
| 6818 | |
| 6819 | static ssize_t |
| 6820 | perf_event_mux_interval_ms_store(struct device *dev, |
| 6821 | struct device_attribute *attr, |
| 6822 | const char *buf, size_t count) |
| 6823 | { |
| 6824 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6825 | int timer, cpu, ret; |
| 6826 | |
| 6827 | ret = kstrtoint(buf, 0, &timer); |
| 6828 | if (ret) |
| 6829 | return ret; |
| 6830 | |
| 6831 | if (timer < 1) |
| 6832 | return -EINVAL; |
| 6833 | |
| 6834 | /* same value, noting to do */ |
| 6835 | if (timer == pmu->hrtimer_interval_ms) |
| 6836 | return count; |
| 6837 | |
| 6838 | pmu->hrtimer_interval_ms = timer; |
| 6839 | |
| 6840 | /* update all cpuctx for this PMU */ |
| 6841 | for_each_possible_cpu(cpu) { |
| 6842 | struct perf_cpu_context *cpuctx; |
| 6843 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6844 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 6845 | |
| 6846 | if (hrtimer_active(&cpuctx->hrtimer)) |
| 6847 | hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval); |
| 6848 | } |
| 6849 | |
| 6850 | return count; |
| 6851 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6852 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6853 | |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6854 | static struct attribute *pmu_dev_attrs[] = { |
| 6855 | &dev_attr_type.attr, |
| 6856 | &dev_attr_perf_event_mux_interval_ms.attr, |
| 6857 | NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6858 | }; |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6859 | ATTRIBUTE_GROUPS(pmu_dev); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6860 | |
| 6861 | static int pmu_bus_running; |
| 6862 | static struct bus_type pmu_bus = { |
| 6863 | .name = "event_source", |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6864 | .dev_groups = pmu_dev_groups, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6865 | }; |
| 6866 | |
| 6867 | static void pmu_dev_release(struct device *dev) |
| 6868 | { |
| 6869 | kfree(dev); |
| 6870 | } |
| 6871 | |
| 6872 | static int pmu_dev_alloc(struct pmu *pmu) |
| 6873 | { |
| 6874 | int ret = -ENOMEM; |
| 6875 | |
| 6876 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 6877 | if (!pmu->dev) |
| 6878 | goto out; |
| 6879 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 6880 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6881 | device_initialize(pmu->dev); |
| 6882 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 6883 | if (ret) |
| 6884 | goto free_dev; |
| 6885 | |
| 6886 | dev_set_drvdata(pmu->dev, pmu); |
| 6887 | pmu->dev->bus = &pmu_bus; |
| 6888 | pmu->dev->release = pmu_dev_release; |
| 6889 | ret = device_add(pmu->dev); |
| 6890 | if (ret) |
| 6891 | goto free_dev; |
| 6892 | |
| 6893 | out: |
| 6894 | return ret; |
| 6895 | |
| 6896 | free_dev: |
| 6897 | put_device(pmu->dev); |
| 6898 | goto out; |
| 6899 | } |
| 6900 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6901 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6902 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6903 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 6904 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6905 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6906 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6907 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6908 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6909 | ret = -ENOMEM; |
| 6910 | pmu->pmu_disable_count = alloc_percpu(int); |
| 6911 | if (!pmu->pmu_disable_count) |
| 6912 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6913 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6914 | pmu->type = -1; |
| 6915 | if (!name) |
| 6916 | goto skip_type; |
| 6917 | pmu->name = name; |
| 6918 | |
| 6919 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 6920 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 6921 | if (type < 0) { |
| 6922 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6923 | goto free_pdc; |
| 6924 | } |
| 6925 | } |
| 6926 | pmu->type = type; |
| 6927 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6928 | if (pmu_bus_running) { |
| 6929 | ret = pmu_dev_alloc(pmu); |
| 6930 | if (ret) |
| 6931 | goto free_idr; |
| 6932 | } |
| 6933 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6934 | skip_type: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6935 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 6936 | if (pmu->pmu_cpu_context) |
| 6937 | goto got_cpu_context; |
| 6938 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 6939 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6940 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 6941 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6942 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6943 | |
| 6944 | for_each_possible_cpu(cpu) { |
| 6945 | struct perf_cpu_context *cpuctx; |
| 6946 | |
| 6947 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 6948 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6949 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6950 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6951 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 6952 | |
| 6953 | __perf_cpu_hrtimer_init(cpuctx, cpu); |
| 6954 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6955 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6956 | } |
| 6957 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6958 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6959 | if (!pmu->start_txn) { |
| 6960 | if (pmu->pmu_enable) { |
| 6961 | /* |
| 6962 | * If we have pmu_enable/pmu_disable calls, install |
| 6963 | * transaction stubs that use that to try and batch |
| 6964 | * hardware accesses. |
| 6965 | */ |
| 6966 | pmu->start_txn = perf_pmu_start_txn; |
| 6967 | pmu->commit_txn = perf_pmu_commit_txn; |
| 6968 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 6969 | } else { |
| 6970 | pmu->start_txn = perf_pmu_nop_void; |
| 6971 | pmu->commit_txn = perf_pmu_nop_int; |
| 6972 | pmu->cancel_txn = perf_pmu_nop_void; |
| 6973 | } |
| 6974 | } |
| 6975 | |
| 6976 | if (!pmu->pmu_enable) { |
| 6977 | pmu->pmu_enable = perf_pmu_nop_void; |
| 6978 | pmu->pmu_disable = perf_pmu_nop_void; |
| 6979 | } |
| 6980 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6981 | if (!pmu->event_idx) |
| 6982 | pmu->event_idx = perf_event_idx_default; |
| 6983 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6984 | list_add_rcu(&pmu->entry, &pmus); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6985 | ret = 0; |
| 6986 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6987 | mutex_unlock(&pmus_lock); |
| 6988 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6989 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6990 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6991 | free_dev: |
| 6992 | device_del(pmu->dev); |
| 6993 | put_device(pmu->dev); |
| 6994 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6995 | free_idr: |
| 6996 | if (pmu->type >= PERF_TYPE_MAX) |
| 6997 | idr_remove(&pmu_idr, pmu->type); |
| 6998 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6999 | free_pdc: |
| 7000 | free_percpu(pmu->pmu_disable_count); |
| 7001 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7002 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 7003 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7004 | |
| 7005 | void perf_pmu_unregister(struct pmu *pmu) |
| 7006 | { |
| 7007 | mutex_lock(&pmus_lock); |
| 7008 | list_del_rcu(&pmu->entry); |
| 7009 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7010 | |
| 7011 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 7012 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 7013 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7014 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7015 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 7016 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7017 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 7018 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7019 | if (pmu->type >= PERF_TYPE_MAX) |
| 7020 | idr_remove(&pmu_idr, pmu->type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7021 | device_del(pmu->dev); |
| 7022 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 7023 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7024 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 7025 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7026 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7027 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
| 7028 | { |
| 7029 | int ret; |
| 7030 | |
| 7031 | if (!try_module_get(pmu->module)) |
| 7032 | return -ENODEV; |
| 7033 | event->pmu = pmu; |
| 7034 | ret = pmu->event_init(event); |
| 7035 | if (ret) |
| 7036 | module_put(pmu->module); |
| 7037 | |
| 7038 | return ret; |
| 7039 | } |
| 7040 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7041 | struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7042 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 7043 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7044 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7045 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7046 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7047 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7048 | |
| 7049 | rcu_read_lock(); |
| 7050 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 7051 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7052 | if (pmu) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7053 | ret = perf_try_init_event(pmu, event); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7054 | if (ret) |
| 7055 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7056 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7057 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7058 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7059 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7060 | ret = perf_try_init_event(pmu, event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7061 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 7062 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7063 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7064 | if (ret != -ENOENT) { |
| 7065 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 7066 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7067 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7068 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 7069 | pmu = ERR_PTR(-ENOENT); |
| 7070 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7071 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7072 | |
| 7073 | return pmu; |
| 7074 | } |
| 7075 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7076 | static void account_event_cpu(struct perf_event *event, int cpu) |
| 7077 | { |
| 7078 | if (event->parent) |
| 7079 | return; |
| 7080 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7081 | if (is_cgroup_event(event)) |
| 7082 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); |
| 7083 | } |
| 7084 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7085 | static void account_event(struct perf_event *event) |
| 7086 | { |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7087 | if (event->parent) |
| 7088 | return; |
| 7089 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7090 | if (event->attach_state & PERF_ATTACH_TASK) |
| 7091 | static_key_slow_inc(&perf_sched_events.key); |
| 7092 | if (event->attr.mmap || event->attr.mmap_data) |
| 7093 | atomic_inc(&nr_mmap_events); |
| 7094 | if (event->attr.comm) |
| 7095 | atomic_inc(&nr_comm_events); |
| 7096 | if (event->attr.task) |
| 7097 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 7098 | if (event->attr.freq) { |
| 7099 | if (atomic_inc_return(&nr_freq_events) == 1) |
| 7100 | tick_nohz_full_kick_all(); |
| 7101 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7102 | if (has_branch_stack(event)) |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7103 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7104 | if (is_cgroup_event(event)) |
| 7105 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7106 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7107 | account_event_cpu(event, event->cpu); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7108 | } |
| 7109 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7110 | /* |
| 7111 | * Allocate and initialize a event structure |
| 7112 | */ |
| 7113 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7114 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7115 | struct task_struct *task, |
| 7116 | struct perf_event *group_leader, |
| 7117 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7118 | perf_overflow_handler_t overflow_handler, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame^] | 7119 | void *context, int cgroup_fd) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7120 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 7121 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7122 | struct perf_event *event; |
| 7123 | struct hw_perf_event *hwc; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7124 | long err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7125 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 7126 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 7127 | if (!task || cpu != -1) |
| 7128 | return ERR_PTR(-EINVAL); |
| 7129 | } |
| 7130 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7131 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7132 | if (!event) |
| 7133 | return ERR_PTR(-ENOMEM); |
| 7134 | |
| 7135 | /* |
| 7136 | * Single events are their own group leaders, with an |
| 7137 | * empty sibling list: |
| 7138 | */ |
| 7139 | if (!group_leader) |
| 7140 | group_leader = event; |
| 7141 | |
| 7142 | mutex_init(&event->child_mutex); |
| 7143 | INIT_LIST_HEAD(&event->child_list); |
| 7144 | |
| 7145 | INIT_LIST_HEAD(&event->group_entry); |
| 7146 | INIT_LIST_HEAD(&event->event_entry); |
| 7147 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 7148 | INIT_LIST_HEAD(&event->rb_entry); |
Stephane Eranian | 71ad88e | 2013-11-12 17:58:48 +0100 | [diff] [blame] | 7149 | INIT_LIST_HEAD(&event->active_entry); |
Stephane Eranian | f3ae75d | 2014-01-08 11:15:52 +0100 | [diff] [blame] | 7150 | INIT_HLIST_NODE(&event->hlist_entry); |
| 7151 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 7152 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7153 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 7154 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7155 | |
| 7156 | mutex_init(&event->mmap_mutex); |
| 7157 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7158 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7159 | event->cpu = cpu; |
| 7160 | event->attr = *attr; |
| 7161 | event->group_leader = group_leader; |
| 7162 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7163 | event->oncpu = -1; |
| 7164 | |
| 7165 | event->parent = parent_event; |
| 7166 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 7167 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7168 | event->id = atomic64_inc_return(&perf_event_id); |
| 7169 | |
| 7170 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 7171 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7172 | if (task) { |
| 7173 | event->attach_state = PERF_ATTACH_TASK; |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 7174 | |
| 7175 | if (attr->type == PERF_TYPE_TRACEPOINT) |
| 7176 | event->hw.tp_target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7177 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
| 7178 | /* |
| 7179 | * hw_breakpoint is a bit difficult here.. |
| 7180 | */ |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 7181 | else if (attr->type == PERF_TYPE_BREAKPOINT) |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7182 | event->hw.bp_target = task; |
| 7183 | #endif |
| 7184 | } |
| 7185 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7186 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 7187 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7188 | context = parent_event->overflow_handler_context; |
| 7189 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 7190 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 7191 | event->overflow_handler = overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7192 | event->overflow_handler_context = context; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 7193 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7194 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7195 | |
| 7196 | pmu = NULL; |
| 7197 | |
| 7198 | hwc = &event->hw; |
| 7199 | hwc->sample_period = attr->sample_period; |
| 7200 | if (attr->freq && attr->sample_freq) |
| 7201 | hwc->sample_period = 1; |
| 7202 | hwc->last_period = hwc->sample_period; |
| 7203 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 7204 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7205 | |
| 7206 | /* |
| 7207 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 7208 | */ |
| 7209 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7210 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7211 | |
Yan, Zheng | a46a230 | 2014-11-04 21:56:06 -0500 | [diff] [blame] | 7212 | if (!has_branch_stack(event)) |
| 7213 | event->attr.branch_sample_type = 0; |
| 7214 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame^] | 7215 | if (cgroup_fd != -1) { |
| 7216 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); |
| 7217 | if (err) |
| 7218 | goto err_ns; |
| 7219 | } |
| 7220 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7221 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7222 | if (!pmu) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7223 | goto err_ns; |
| 7224 | else if (IS_ERR(pmu)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7225 | err = PTR_ERR(pmu); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7226 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7227 | } |
| 7228 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7229 | if (!event->parent) { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 7230 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 7231 | err = get_callchain_buffers(); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7232 | if (err) |
| 7233 | goto err_pmu; |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 7234 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7235 | } |
| 7236 | |
| 7237 | return event; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7238 | |
| 7239 | err_pmu: |
| 7240 | if (event->destroy) |
| 7241 | event->destroy(event); |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 7242 | module_put(pmu->module); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7243 | err_ns: |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame^] | 7244 | if (is_cgroup_event(event)) |
| 7245 | perf_detach_cgroup(event); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7246 | if (event->ns) |
| 7247 | put_pid_ns(event->ns); |
| 7248 | kfree(event); |
| 7249 | |
| 7250 | return ERR_PTR(err); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7251 | } |
| 7252 | |
| 7253 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 7254 | struct perf_event_attr *attr) |
| 7255 | { |
| 7256 | u32 size; |
| 7257 | int ret; |
| 7258 | |
| 7259 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 7260 | return -EFAULT; |
| 7261 | |
| 7262 | /* |
| 7263 | * zero the full structure, so that a short copy will be nice. |
| 7264 | */ |
| 7265 | memset(attr, 0, sizeof(*attr)); |
| 7266 | |
| 7267 | ret = get_user(size, &uattr->size); |
| 7268 | if (ret) |
| 7269 | return ret; |
| 7270 | |
| 7271 | if (size > PAGE_SIZE) /* silly large */ |
| 7272 | goto err_size; |
| 7273 | |
| 7274 | if (!size) /* abi compat */ |
| 7275 | size = PERF_ATTR_SIZE_VER0; |
| 7276 | |
| 7277 | if (size < PERF_ATTR_SIZE_VER0) |
| 7278 | goto err_size; |
| 7279 | |
| 7280 | /* |
| 7281 | * If we're handed a bigger struct than we know of, |
| 7282 | * ensure all the unknown bits are 0 - i.e. new |
| 7283 | * user-space does not rely on any kernel feature |
| 7284 | * extensions we dont know about yet. |
| 7285 | */ |
| 7286 | if (size > sizeof(*attr)) { |
| 7287 | unsigned char __user *addr; |
| 7288 | unsigned char __user *end; |
| 7289 | unsigned char val; |
| 7290 | |
| 7291 | addr = (void __user *)uattr + sizeof(*attr); |
| 7292 | end = (void __user *)uattr + size; |
| 7293 | |
| 7294 | for (; addr < end; addr++) { |
| 7295 | ret = get_user(val, addr); |
| 7296 | if (ret) |
| 7297 | return ret; |
| 7298 | if (val) |
| 7299 | goto err_size; |
| 7300 | } |
| 7301 | size = sizeof(*attr); |
| 7302 | } |
| 7303 | |
| 7304 | ret = copy_from_user(attr, uattr, size); |
| 7305 | if (ret) |
| 7306 | return -EFAULT; |
| 7307 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 7308 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7309 | return -EINVAL; |
| 7310 | |
| 7311 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 7312 | return -EINVAL; |
| 7313 | |
| 7314 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 7315 | return -EINVAL; |
| 7316 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 7317 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 7318 | u64 mask = attr->branch_sample_type; |
| 7319 | |
| 7320 | /* only using defined bits */ |
| 7321 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 7322 | return -EINVAL; |
| 7323 | |
| 7324 | /* at least one branch bit must be set */ |
| 7325 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 7326 | return -EINVAL; |
| 7327 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 7328 | /* propagate priv level, when not set for branch */ |
| 7329 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 7330 | |
| 7331 | /* exclude_kernel checked on syscall entry */ |
| 7332 | if (!attr->exclude_kernel) |
| 7333 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 7334 | |
| 7335 | if (!attr->exclude_user) |
| 7336 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 7337 | |
| 7338 | if (!attr->exclude_hv) |
| 7339 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 7340 | /* |
| 7341 | * adjust user setting (for HW filter setup) |
| 7342 | */ |
| 7343 | attr->branch_sample_type = mask; |
| 7344 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 7345 | /* privileged levels capture (kernel, hv): check permissions */ |
| 7346 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 7347 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 7348 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 7349 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7350 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 7351 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7352 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 7353 | if (ret) |
| 7354 | return ret; |
| 7355 | } |
| 7356 | |
| 7357 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 7358 | if (!arch_perf_have_user_stack_dump()) |
| 7359 | return -ENOSYS; |
| 7360 | |
| 7361 | /* |
| 7362 | * We have __u32 type for the size, but so far |
| 7363 | * we can only use __u16 as maximum due to the |
| 7364 | * __u16 sample size limit. |
| 7365 | */ |
| 7366 | if (attr->sample_stack_user >= USHRT_MAX) |
| 7367 | ret = -EINVAL; |
| 7368 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
| 7369 | ret = -EINVAL; |
| 7370 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7371 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 7372 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
| 7373 | ret = perf_reg_validate(attr->sample_regs_intr); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7374 | out: |
| 7375 | return ret; |
| 7376 | |
| 7377 | err_size: |
| 7378 | put_user(sizeof(*attr), &uattr->size); |
| 7379 | ret = -E2BIG; |
| 7380 | goto out; |
| 7381 | } |
| 7382 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7383 | static int |
| 7384 | 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] | 7385 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7386 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7387 | int ret = -EINVAL; |
| 7388 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7389 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7390 | goto set; |
| 7391 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7392 | /* don't allow circular references */ |
| 7393 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7394 | goto out; |
| 7395 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 7396 | /* |
| 7397 | * Don't allow cross-cpu buffers |
| 7398 | */ |
| 7399 | if (output_event->cpu != event->cpu) |
| 7400 | goto out; |
| 7401 | |
| 7402 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7403 | * 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] | 7404 | */ |
| 7405 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 7406 | goto out; |
| 7407 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7408 | set: |
| 7409 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7410 | /* Can't redirect output if we've got an active mmap() */ |
| 7411 | if (atomic_read(&event->mmap_count)) |
| 7412 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7413 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7414 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7415 | /* get the rb we want to redirect to */ |
| 7416 | rb = ring_buffer_get(output_event); |
| 7417 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7418 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7419 | } |
| 7420 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7421 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 7422 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7423 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7424 | unlock: |
| 7425 | mutex_unlock(&event->mmap_mutex); |
| 7426 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7427 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7428 | return ret; |
| 7429 | } |
| 7430 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7431 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
| 7432 | { |
| 7433 | if (b < a) |
| 7434 | swap(a, b); |
| 7435 | |
| 7436 | mutex_lock(a); |
| 7437 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); |
| 7438 | } |
| 7439 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7440 | /** |
| 7441 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 7442 | * |
| 7443 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 7444 | * @pid: target pid |
| 7445 | * @cpu: target cpu |
| 7446 | * @group_fd: group leader event fd |
| 7447 | */ |
| 7448 | SYSCALL_DEFINE5(perf_event_open, |
| 7449 | struct perf_event_attr __user *, attr_uptr, |
| 7450 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 7451 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7452 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 7453 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7454 | struct perf_event_attr attr; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7455 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7456 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7457 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7458 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7459 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7460 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7461 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7462 | int err; |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7463 | int f_flags = O_RDWR; |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame^] | 7464 | int cgroup_fd = -1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7465 | |
| 7466 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7467 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7468 | return -EINVAL; |
| 7469 | |
| 7470 | err = perf_copy_attr(attr_uptr, &attr); |
| 7471 | if (err) |
| 7472 | return err; |
| 7473 | |
| 7474 | if (!attr.exclude_kernel) { |
| 7475 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 7476 | return -EACCES; |
| 7477 | } |
| 7478 | |
| 7479 | if (attr.freq) { |
| 7480 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 7481 | return -EINVAL; |
Peter Zijlstra | 0819b2e | 2014-05-15 20:23:48 +0200 | [diff] [blame] | 7482 | } else { |
| 7483 | if (attr.sample_period & (1ULL << 63)) |
| 7484 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7485 | } |
| 7486 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7487 | /* |
| 7488 | * In cgroup mode, the pid argument is used to pass the fd |
| 7489 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 7490 | * designates the cpu on which to monitor threads from that |
| 7491 | * cgroup. |
| 7492 | */ |
| 7493 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 7494 | return -EINVAL; |
| 7495 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7496 | if (flags & PERF_FLAG_FD_CLOEXEC) |
| 7497 | f_flags |= O_CLOEXEC; |
| 7498 | |
| 7499 | event_fd = get_unused_fd_flags(f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7500 | if (event_fd < 0) |
| 7501 | return event_fd; |
| 7502 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7503 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7504 | err = perf_fget_light(group_fd, &group); |
| 7505 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7506 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7507 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7508 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 7509 | output_event = group_leader; |
| 7510 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 7511 | group_leader = NULL; |
| 7512 | } |
| 7513 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7514 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7515 | task = find_lively_task_by_vpid(pid); |
| 7516 | if (IS_ERR(task)) { |
| 7517 | err = PTR_ERR(task); |
| 7518 | goto err_group_fd; |
| 7519 | } |
| 7520 | } |
| 7521 | |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7522 | if (task && group_leader && |
| 7523 | group_leader->attr.inherit != attr.inherit) { |
| 7524 | err = -EINVAL; |
| 7525 | goto err_task; |
| 7526 | } |
| 7527 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7528 | get_online_cpus(); |
| 7529 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame^] | 7530 | if (flags & PERF_FLAG_PID_CGROUP) |
| 7531 | cgroup_fd = pid; |
| 7532 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7533 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame^] | 7534 | NULL, NULL, cgroup_fd); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7535 | if (IS_ERR(event)) { |
| 7536 | err = PTR_ERR(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7537 | goto err_cpus; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7538 | } |
| 7539 | |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 7540 | if (is_sampling_event(event)) { |
| 7541 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { |
| 7542 | err = -ENOTSUPP; |
| 7543 | goto err_alloc; |
| 7544 | } |
| 7545 | } |
| 7546 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7547 | account_event(event); |
| 7548 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7549 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7550 | * Special case software events and allow them to be part of |
| 7551 | * any hardware group. |
| 7552 | */ |
| 7553 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7554 | |
| 7555 | if (group_leader && |
| 7556 | (is_software_event(event) != is_software_event(group_leader))) { |
| 7557 | if (is_software_event(event)) { |
| 7558 | /* |
| 7559 | * If event and group_leader are not both a software |
| 7560 | * event, and event is, then group leader is not. |
| 7561 | * |
| 7562 | * Allow the addition of software events to !software |
| 7563 | * groups, this is safe because software events never |
| 7564 | * fail to schedule. |
| 7565 | */ |
| 7566 | pmu = group_leader->pmu; |
| 7567 | } else if (is_software_event(group_leader) && |
| 7568 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 7569 | /* |
| 7570 | * In case the group is a pure software group, and we |
| 7571 | * try to add a hardware event, move the whole group to |
| 7572 | * the hardware context. |
| 7573 | */ |
| 7574 | move_group = 1; |
| 7575 | } |
| 7576 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7577 | |
| 7578 | /* |
| 7579 | * Get the target context (task or percpu): |
| 7580 | */ |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 7581 | ctx = find_get_context(pmu, task, event); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7582 | if (IS_ERR(ctx)) { |
| 7583 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7584 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7585 | } |
| 7586 | |
Peter Zijlstra | fd1edb3 | 2011-03-28 13:13:56 +0200 | [diff] [blame] | 7587 | if (task) { |
| 7588 | put_task_struct(task); |
| 7589 | task = NULL; |
| 7590 | } |
| 7591 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7592 | /* |
| 7593 | * Look up the group leader (we will attach this event to it): |
| 7594 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7595 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7596 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7597 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7598 | /* |
| 7599 | * Do not allow a recursive hierarchy (this new sibling |
| 7600 | * becoming part of another group-sibling): |
| 7601 | */ |
| 7602 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7603 | goto err_context; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7604 | /* |
| 7605 | * Do not allow to attach to a group in a different |
| 7606 | * task or CPU context: |
| 7607 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7608 | if (move_group) { |
Peter Zijlstra | c3c87e7 | 2015-01-23 11:19:48 +0100 | [diff] [blame] | 7609 | /* |
| 7610 | * Make sure we're both on the same task, or both |
| 7611 | * per-cpu events. |
| 7612 | */ |
| 7613 | if (group_leader->ctx->task != ctx->task) |
| 7614 | goto err_context; |
| 7615 | |
| 7616 | /* |
| 7617 | * Make sure we're both events for the same CPU; |
| 7618 | * grouping events for different CPUs is broken; since |
| 7619 | * you can never concurrently schedule them anyhow. |
| 7620 | */ |
| 7621 | if (group_leader->cpu != event->cpu) |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7622 | goto err_context; |
| 7623 | } else { |
| 7624 | if (group_leader->ctx != ctx) |
| 7625 | goto err_context; |
| 7626 | } |
| 7627 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7628 | /* |
| 7629 | * Only a group leader can be exclusive or pinned |
| 7630 | */ |
| 7631 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7632 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7633 | } |
| 7634 | |
| 7635 | if (output_event) { |
| 7636 | err = perf_event_set_output(event, output_event); |
| 7637 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7638 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7639 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7640 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7641 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
| 7642 | f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7643 | if (IS_ERR(event_file)) { |
| 7644 | err = PTR_ERR(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7645 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7646 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7647 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7648 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7649 | gctx = group_leader->ctx; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7650 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7651 | /* |
| 7652 | * See perf_event_ctx_lock() for comments on the details |
| 7653 | * of swizzling perf_event::ctx. |
| 7654 | */ |
| 7655 | mutex_lock_double(&gctx->mutex, &ctx->mutex); |
| 7656 | |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7657 | perf_remove_from_context(group_leader, false); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7658 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7659 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7660 | group_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7661 | perf_remove_from_context(sibling, false); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7662 | put_ctx(gctx); |
| 7663 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7664 | } else { |
| 7665 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7666 | } |
| 7667 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7668 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7669 | |
| 7670 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7671 | /* |
| 7672 | * Wait for everybody to stop referencing the events through |
| 7673 | * the old lists, before installing it on new lists. |
| 7674 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7675 | synchronize_rcu(); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7676 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7677 | /* |
| 7678 | * Install the group siblings before the group leader. |
| 7679 | * |
| 7680 | * Because a group leader will try and install the entire group |
| 7681 | * (through the sibling list, which is still in-tact), we can |
| 7682 | * end up with siblings installed in the wrong context. |
| 7683 | * |
| 7684 | * By installing siblings first we NO-OP because they're not |
| 7685 | * reachable through the group lists. |
| 7686 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7687 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7688 | group_entry) { |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7689 | perf_event__state_init(sibling); |
Jiri Olsa | 9fc81d8 | 2014-12-10 21:23:51 +0100 | [diff] [blame] | 7690 | perf_install_in_context(ctx, sibling, sibling->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7691 | get_ctx(ctx); |
| 7692 | } |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7693 | |
| 7694 | /* |
| 7695 | * Removing from the context ends up with disabled |
| 7696 | * event. What we want here is event in the initial |
| 7697 | * startup state, ready to be add into new context. |
| 7698 | */ |
| 7699 | perf_event__state_init(group_leader); |
| 7700 | perf_install_in_context(ctx, group_leader, group_leader->cpu); |
| 7701 | get_ctx(ctx); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7702 | } |
| 7703 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7704 | perf_install_in_context(ctx, event, event->cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7705 | perf_unpin_context(ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7706 | |
| 7707 | if (move_group) { |
| 7708 | mutex_unlock(&gctx->mutex); |
| 7709 | put_ctx(gctx); |
| 7710 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7711 | mutex_unlock(&ctx->mutex); |
| 7712 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7713 | put_online_cpus(); |
| 7714 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7715 | event->owner = current; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7716 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7717 | mutex_lock(¤t->perf_event_mutex); |
| 7718 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 7719 | mutex_unlock(¤t->perf_event_mutex); |
| 7720 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7721 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7722 | * Precalculate sample_data sizes |
| 7723 | */ |
| 7724 | perf_event__header_size(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7725 | perf_event__id_header_size(event); |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7726 | |
| 7727 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7728 | * Drop the reference on the group_event after placing the |
| 7729 | * new event on the sibling_list. This ensures destruction |
| 7730 | * of the group leader will find the pointer to itself in |
| 7731 | * perf_group_detach(). |
| 7732 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7733 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7734 | fd_install(event_fd, event_file); |
| 7735 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7736 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7737 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7738 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7739 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7740 | err_alloc: |
| 7741 | free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7742 | err_cpus: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7743 | put_online_cpus(); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7744 | err_task: |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 7745 | if (task) |
| 7746 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7747 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7748 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7749 | err_fd: |
| 7750 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7751 | return err; |
| 7752 | } |
| 7753 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7754 | /** |
| 7755 | * perf_event_create_kernel_counter |
| 7756 | * |
| 7757 | * @attr: attributes of the counter to create |
| 7758 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7759 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7760 | */ |
| 7761 | struct perf_event * |
| 7762 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7763 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7764 | perf_overflow_handler_t overflow_handler, |
| 7765 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7766 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7767 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7768 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7769 | int err; |
| 7770 | |
| 7771 | /* |
| 7772 | * Get the target context (task or percpu): |
| 7773 | */ |
| 7774 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7775 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame^] | 7776 | overflow_handler, context, -1); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7777 | if (IS_ERR(event)) { |
| 7778 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7779 | goto err; |
| 7780 | } |
| 7781 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 7782 | /* Mark owner so we could distinguish it from user events. */ |
| 7783 | event->owner = EVENT_OWNER_KERNEL; |
| 7784 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7785 | account_event(event); |
| 7786 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 7787 | ctx = find_get_context(event->pmu, task, event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7788 | if (IS_ERR(ctx)) { |
| 7789 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7790 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7791 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7792 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7793 | WARN_ON_ONCE(ctx->parent_ctx); |
| 7794 | mutex_lock(&ctx->mutex); |
| 7795 | perf_install_in_context(ctx, event, cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7796 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7797 | mutex_unlock(&ctx->mutex); |
| 7798 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7799 | return event; |
| 7800 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7801 | err_free: |
| 7802 | free_event(event); |
| 7803 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7804 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7805 | } |
| 7806 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 7807 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7808 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 7809 | { |
| 7810 | struct perf_event_context *src_ctx; |
| 7811 | struct perf_event_context *dst_ctx; |
| 7812 | struct perf_event *event, *tmp; |
| 7813 | LIST_HEAD(events); |
| 7814 | |
| 7815 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 7816 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 7817 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7818 | /* |
| 7819 | * See perf_event_ctx_lock() for comments on the details |
| 7820 | * of swizzling perf_event::ctx. |
| 7821 | */ |
| 7822 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7823 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 7824 | event_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7825 | perf_remove_from_context(event, false); |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7826 | unaccount_event_cpu(event, src_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7827 | put_ctx(src_ctx); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7828 | list_add(&event->migrate_entry, &events); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7829 | } |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7830 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7831 | /* |
| 7832 | * Wait for the events to quiesce before re-instating them. |
| 7833 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7834 | synchronize_rcu(); |
| 7835 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7836 | /* |
| 7837 | * Re-instate events in 2 passes. |
| 7838 | * |
| 7839 | * Skip over group leaders and only install siblings on this first |
| 7840 | * pass, siblings will not get enabled without a leader, however a |
| 7841 | * leader will enable its siblings, even if those are still on the old |
| 7842 | * context. |
| 7843 | */ |
| 7844 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 7845 | if (event->group_leader == event) |
| 7846 | continue; |
| 7847 | |
| 7848 | list_del(&event->migrate_entry); |
| 7849 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 7850 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 7851 | account_event_cpu(event, dst_cpu); |
| 7852 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 7853 | get_ctx(dst_ctx); |
| 7854 | } |
| 7855 | |
| 7856 | /* |
| 7857 | * Once all the siblings are setup properly, install the group leaders |
| 7858 | * to make it go. |
| 7859 | */ |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7860 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 7861 | list_del(&event->migrate_entry); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7862 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 7863 | event->state = PERF_EVENT_STATE_INACTIVE; |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7864 | account_event_cpu(event, dst_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7865 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 7866 | get_ctx(dst_ctx); |
| 7867 | } |
| 7868 | mutex_unlock(&dst_ctx->mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7869 | mutex_unlock(&src_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7870 | } |
| 7871 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 7872 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7873 | static void sync_child_event(struct perf_event *child_event, |
| 7874 | struct task_struct *child) |
| 7875 | { |
| 7876 | struct perf_event *parent_event = child_event->parent; |
| 7877 | u64 child_val; |
| 7878 | |
| 7879 | if (child_event->attr.inherit_stat) |
| 7880 | perf_event_read_event(child_event, child); |
| 7881 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 7882 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7883 | |
| 7884 | /* |
| 7885 | * Add back the child's count to the parent's count: |
| 7886 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 7887 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7888 | atomic64_add(child_event->total_time_enabled, |
| 7889 | &parent_event->child_total_time_enabled); |
| 7890 | atomic64_add(child_event->total_time_running, |
| 7891 | &parent_event->child_total_time_running); |
| 7892 | |
| 7893 | /* |
| 7894 | * Remove this event from the parent's list |
| 7895 | */ |
| 7896 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7897 | mutex_lock(&parent_event->child_mutex); |
| 7898 | list_del_init(&child_event->child_list); |
| 7899 | mutex_unlock(&parent_event->child_mutex); |
| 7900 | |
| 7901 | /* |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 7902 | * Make sure user/parent get notified, that we just |
| 7903 | * lost one event. |
| 7904 | */ |
| 7905 | perf_event_wakeup(parent_event); |
| 7906 | |
| 7907 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7908 | * Release the parent event, if this was the last |
| 7909 | * reference to it. |
| 7910 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7911 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7912 | } |
| 7913 | |
| 7914 | static void |
| 7915 | __perf_event_exit_task(struct perf_event *child_event, |
| 7916 | struct perf_event_context *child_ctx, |
| 7917 | struct task_struct *child) |
| 7918 | { |
Peter Zijlstra | 1903d50 | 2014-07-15 17:27:27 +0200 | [diff] [blame] | 7919 | /* |
| 7920 | * Do not destroy the 'original' grouping; because of the context |
| 7921 | * switch optimization the original events could've ended up in a |
| 7922 | * random child task. |
| 7923 | * |
| 7924 | * If we were to destroy the original group, all group related |
| 7925 | * operations would cease to function properly after this random |
| 7926 | * child dies. |
| 7927 | * |
| 7928 | * Do destroy all inherited groups, we don't care about those |
| 7929 | * and being thorough is better. |
| 7930 | */ |
| 7931 | perf_remove_from_context(child_event, !!child_event->parent); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7932 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7933 | /* |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7934 | * It can happen that the parent exits first, and has events |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7935 | * that are still around due to the child reference. These |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7936 | * events need to be zapped. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7937 | */ |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7938 | if (child_event->parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7939 | sync_child_event(child_event, child); |
| 7940 | free_event(child_event); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 7941 | } else { |
| 7942 | child_event->state = PERF_EVENT_STATE_EXIT; |
| 7943 | perf_event_wakeup(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7944 | } |
| 7945 | } |
| 7946 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7947 | 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] | 7948 | { |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 7949 | struct perf_event *child_event, *next; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 7950 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7951 | unsigned long flags; |
| 7952 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7953 | if (likely(!child->perf_event_ctxp[ctxn])) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7954 | perf_event_task(child, NULL, 0); |
| 7955 | return; |
| 7956 | } |
| 7957 | |
| 7958 | local_irq_save(flags); |
| 7959 | /* |
| 7960 | * We can't reschedule here because interrupts are disabled, |
| 7961 | * and either child is current or it is a task that can't be |
| 7962 | * scheduled, so we are now safe from rescheduling changing |
| 7963 | * our context. |
| 7964 | */ |
Oleg Nesterov | 806839b | 2011-01-21 18:45:47 +0100 | [diff] [blame] | 7965 | child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7966 | |
| 7967 | /* |
| 7968 | * Take the context lock here so that if find_get_context is |
| 7969 | * reading child->perf_event_ctxp, we wait until it has |
| 7970 | * incremented the context's refcount before we do put_ctx below. |
| 7971 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7972 | raw_spin_lock(&child_ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 7973 | task_ctx_sched_out(child_ctx); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7974 | child->perf_event_ctxp[ctxn] = NULL; |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 7975 | |
| 7976 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7977 | * If this context is a clone; unclone it so it can't get |
| 7978 | * swapped to another process while we're removing all |
| 7979 | * the events from it. |
| 7980 | */ |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 7981 | clone_ctx = unclone_ctx(child_ctx); |
Peter Zijlstra | 5e942bb | 2009-11-23 11:37:26 +0100 | [diff] [blame] | 7982 | update_context_time(child_ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7983 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7984 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 7985 | if (clone_ctx) |
| 7986 | put_ctx(clone_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 7987 | |
| 7988 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7989 | * Report the task dead after unscheduling the events so that we |
| 7990 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 7991 | * get a few PERF_RECORD_READ events. |
| 7992 | */ |
| 7993 | perf_event_task(child, child_ctx, 0); |
| 7994 | |
| 7995 | /* |
| 7996 | * We can recurse on the same lock type through: |
| 7997 | * |
| 7998 | * __perf_event_exit_task() |
| 7999 | * sync_child_event() |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8000 | * put_event() |
| 8001 | * mutex_lock(&ctx->mutex) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8002 | * |
| 8003 | * But since its the parent context it won't be the same instance. |
| 8004 | */ |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 8005 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8006 | |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 8007 | 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] | 8008 | __perf_event_exit_task(child_event, child_ctx, child); |
| 8009 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8010 | mutex_unlock(&child_ctx->mutex); |
| 8011 | |
| 8012 | put_ctx(child_ctx); |
| 8013 | } |
| 8014 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8015 | /* |
| 8016 | * When a child task exits, feed back event values to parent events. |
| 8017 | */ |
| 8018 | void perf_event_exit_task(struct task_struct *child) |
| 8019 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 8020 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8021 | int ctxn; |
| 8022 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 8023 | mutex_lock(&child->perf_event_mutex); |
| 8024 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 8025 | owner_entry) { |
| 8026 | list_del_init(&event->owner_entry); |
| 8027 | |
| 8028 | /* |
| 8029 | * Ensure the list deletion is visible before we clear |
| 8030 | * the owner, closes a race against perf_release() where |
| 8031 | * we need to serialize on the owner->perf_event_mutex. |
| 8032 | */ |
| 8033 | smp_wmb(); |
| 8034 | event->owner = NULL; |
| 8035 | } |
| 8036 | mutex_unlock(&child->perf_event_mutex); |
| 8037 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8038 | for_each_task_context_nr(ctxn) |
| 8039 | perf_event_exit_task_context(child, ctxn); |
| 8040 | } |
| 8041 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8042 | static void perf_free_event(struct perf_event *event, |
| 8043 | struct perf_event_context *ctx) |
| 8044 | { |
| 8045 | struct perf_event *parent = event->parent; |
| 8046 | |
| 8047 | if (WARN_ON_ONCE(!parent)) |
| 8048 | return; |
| 8049 | |
| 8050 | mutex_lock(&parent->child_mutex); |
| 8051 | list_del_init(&event->child_list); |
| 8052 | mutex_unlock(&parent->child_mutex); |
| 8053 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8054 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8055 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 8056 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 8057 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8058 | list_del_event(event, ctx); |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 8059 | raw_spin_unlock_irq(&ctx->lock); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8060 | free_event(event); |
| 8061 | } |
| 8062 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8063 | /* |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 8064 | * Free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8065 | * perf_event_init_task below, used by fork() in case of fail. |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 8066 | * |
| 8067 | * Not all locks are strictly required, but take them anyway to be nice and |
| 8068 | * help out with the lockdep assertions. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8069 | */ |
| 8070 | void perf_event_free_task(struct task_struct *task) |
| 8071 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8072 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8073 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8074 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8075 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8076 | for_each_task_context_nr(ctxn) { |
| 8077 | ctx = task->perf_event_ctxp[ctxn]; |
| 8078 | if (!ctx) |
| 8079 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8080 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8081 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8082 | again: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8083 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 8084 | group_entry) |
| 8085 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8086 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8087 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 8088 | group_entry) |
| 8089 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8090 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8091 | if (!list_empty(&ctx->pinned_groups) || |
| 8092 | !list_empty(&ctx->flexible_groups)) |
| 8093 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8094 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8095 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8096 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8097 | put_ctx(ctx); |
| 8098 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8099 | } |
| 8100 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 8101 | void perf_event_delayed_put(struct task_struct *task) |
| 8102 | { |
| 8103 | int ctxn; |
| 8104 | |
| 8105 | for_each_task_context_nr(ctxn) |
| 8106 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 8107 | } |
| 8108 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8109 | /* |
| 8110 | * inherit a event from parent task to child task: |
| 8111 | */ |
| 8112 | static struct perf_event * |
| 8113 | inherit_event(struct perf_event *parent_event, |
| 8114 | struct task_struct *parent, |
| 8115 | struct perf_event_context *parent_ctx, |
| 8116 | struct task_struct *child, |
| 8117 | struct perf_event *group_leader, |
| 8118 | struct perf_event_context *child_ctx) |
| 8119 | { |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 8120 | enum perf_event_active_state parent_state = parent_event->state; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8121 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 8122 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8123 | |
| 8124 | /* |
| 8125 | * Instead of creating recursive hierarchies of events, |
| 8126 | * we link inherited events back to the original parent, |
| 8127 | * which has a filp for sure, which we use as the reference |
| 8128 | * count: |
| 8129 | */ |
| 8130 | if (parent_event->parent) |
| 8131 | parent_event = parent_event->parent; |
| 8132 | |
| 8133 | child_event = perf_event_alloc(&parent_event->attr, |
| 8134 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8135 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8136 | group_leader, parent_event, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame^] | 8137 | NULL, NULL, -1); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8138 | if (IS_ERR(child_event)) |
| 8139 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8140 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 8141 | if (is_orphaned_event(parent_event) || |
| 8142 | !atomic_long_inc_not_zero(&parent_event->refcount)) { |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8143 | free_event(child_event); |
| 8144 | return NULL; |
| 8145 | } |
| 8146 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8147 | get_ctx(child_ctx); |
| 8148 | |
| 8149 | /* |
| 8150 | * Make the child state follow the state of the parent event, |
| 8151 | * not its attr.disabled bit. We hold the parent's mutex, |
| 8152 | * so we won't race with perf_event_{en, dis}able_family. |
| 8153 | */ |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 8154 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8155 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 8156 | else |
| 8157 | child_event->state = PERF_EVENT_STATE_OFF; |
| 8158 | |
| 8159 | if (parent_event->attr.freq) { |
| 8160 | u64 sample_period = parent_event->hw.sample_period; |
| 8161 | struct hw_perf_event *hwc = &child_event->hw; |
| 8162 | |
| 8163 | hwc->sample_period = sample_period; |
| 8164 | hwc->last_period = sample_period; |
| 8165 | |
| 8166 | local64_set(&hwc->period_left, sample_period); |
| 8167 | } |
| 8168 | |
| 8169 | child_event->ctx = child_ctx; |
| 8170 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8171 | child_event->overflow_handler_context |
| 8172 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8173 | |
| 8174 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 8175 | * Precalculate sample_data sizes |
| 8176 | */ |
| 8177 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 8178 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 8179 | |
| 8180 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8181 | * Link it up in the child's context: |
| 8182 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 8183 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8184 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 8185 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8186 | |
| 8187 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8188 | * Link this into the parent event's child list |
| 8189 | */ |
| 8190 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 8191 | mutex_lock(&parent_event->child_mutex); |
| 8192 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 8193 | mutex_unlock(&parent_event->child_mutex); |
| 8194 | |
| 8195 | return child_event; |
| 8196 | } |
| 8197 | |
| 8198 | static int inherit_group(struct perf_event *parent_event, |
| 8199 | struct task_struct *parent, |
| 8200 | struct perf_event_context *parent_ctx, |
| 8201 | struct task_struct *child, |
| 8202 | struct perf_event_context *child_ctx) |
| 8203 | { |
| 8204 | struct perf_event *leader; |
| 8205 | struct perf_event *sub; |
| 8206 | struct perf_event *child_ctr; |
| 8207 | |
| 8208 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 8209 | child, NULL, child_ctx); |
| 8210 | if (IS_ERR(leader)) |
| 8211 | return PTR_ERR(leader); |
| 8212 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 8213 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 8214 | child, leader, child_ctx); |
| 8215 | if (IS_ERR(child_ctr)) |
| 8216 | return PTR_ERR(child_ctr); |
| 8217 | } |
| 8218 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8219 | } |
| 8220 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8221 | static int |
| 8222 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 8223 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8224 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8225 | int *inherited_all) |
| 8226 | { |
| 8227 | int ret; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8228 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8229 | |
| 8230 | if (!event->attr.inherit) { |
| 8231 | *inherited_all = 0; |
| 8232 | return 0; |
| 8233 | } |
| 8234 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 8235 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8236 | if (!child_ctx) { |
| 8237 | /* |
| 8238 | * This is executed from the parent task context, so |
| 8239 | * inherit events that have been marked for cloning. |
| 8240 | * First allocate and initialize a context for the |
| 8241 | * child. |
| 8242 | */ |
| 8243 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 8244 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8245 | if (!child_ctx) |
| 8246 | return -ENOMEM; |
| 8247 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8248 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8249 | } |
| 8250 | |
| 8251 | ret = inherit_group(event, parent, parent_ctx, |
| 8252 | child, child_ctx); |
| 8253 | |
| 8254 | if (ret) |
| 8255 | *inherited_all = 0; |
| 8256 | |
| 8257 | return ret; |
| 8258 | } |
| 8259 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8260 | /* |
| 8261 | * Initialize the perf_event context in task_struct |
| 8262 | */ |
Jiri Olsa | 985c8dc | 2014-06-24 10:20:24 +0200 | [diff] [blame] | 8263 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8264 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8265 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8266 | struct perf_event_context *cloned_ctx; |
| 8267 | struct perf_event *event; |
| 8268 | struct task_struct *parent = current; |
| 8269 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8270 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8271 | int ret = 0; |
| 8272 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8273 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8274 | return 0; |
| 8275 | |
| 8276 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8277 | * If the parent's context is a clone, pin it so it won't get |
| 8278 | * swapped under us. |
| 8279 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8280 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Peter Zijlstra | ffb4ef2 | 2014-05-05 19:12:20 +0200 | [diff] [blame] | 8281 | if (!parent_ctx) |
| 8282 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8283 | |
| 8284 | /* |
| 8285 | * No need to check if parent_ctx != NULL here; since we saw |
| 8286 | * it non-NULL earlier, the only reason for it to become NULL |
| 8287 | * is if we exit, and since we're currently in the middle of |
| 8288 | * a fork we can't be exiting at the same time. |
| 8289 | */ |
| 8290 | |
| 8291 | /* |
| 8292 | * Lock the parent list. No need to lock the child - not PID |
| 8293 | * hashed yet and not running, so nobody can access it. |
| 8294 | */ |
| 8295 | mutex_lock(&parent_ctx->mutex); |
| 8296 | |
| 8297 | /* |
| 8298 | * We dont have to disable NMIs - we are only looking at |
| 8299 | * the list, not manipulating it: |
| 8300 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8301 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8302 | ret = inherit_task_group(event, parent, parent_ctx, |
| 8303 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8304 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8305 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8306 | } |
| 8307 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8308 | /* |
| 8309 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 8310 | * to allocations, but we need to prevent rotation because |
| 8311 | * rotate_ctx() will change the list from interrupt context. |
| 8312 | */ |
| 8313 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 8314 | parent_ctx->rotate_disable = 1; |
| 8315 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 8316 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8317 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8318 | ret = inherit_task_group(event, parent, parent_ctx, |
| 8319 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8320 | if (ret) |
| 8321 | break; |
| 8322 | } |
| 8323 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8324 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 8325 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8326 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8327 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8328 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 8329 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8330 | /* |
| 8331 | * Mark the child context as a clone of the parent |
| 8332 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 8333 | * |
| 8334 | * Note that if the parent is a clone, the holding of |
| 8335 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8336 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 8337 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8338 | if (cloned_ctx) { |
| 8339 | child_ctx->parent_ctx = cloned_ctx; |
| 8340 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 8341 | } else { |
| 8342 | child_ctx->parent_ctx = parent_ctx; |
| 8343 | child_ctx->parent_gen = parent_ctx->generation; |
| 8344 | } |
| 8345 | get_ctx(child_ctx->parent_ctx); |
| 8346 | } |
| 8347 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 8348 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8349 | mutex_unlock(&parent_ctx->mutex); |
| 8350 | |
| 8351 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 8352 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8353 | |
| 8354 | return ret; |
| 8355 | } |
| 8356 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8357 | /* |
| 8358 | * Initialize the perf_event context in task_struct |
| 8359 | */ |
| 8360 | int perf_event_init_task(struct task_struct *child) |
| 8361 | { |
| 8362 | int ctxn, ret; |
| 8363 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 8364 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 8365 | mutex_init(&child->perf_event_mutex); |
| 8366 | INIT_LIST_HEAD(&child->perf_event_list); |
| 8367 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8368 | for_each_task_context_nr(ctxn) { |
| 8369 | ret = perf_event_init_context(child, ctxn); |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 8370 | if (ret) { |
| 8371 | perf_event_free_task(child); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8372 | return ret; |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 8373 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8374 | } |
| 8375 | |
| 8376 | return 0; |
| 8377 | } |
| 8378 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8379 | static void __init perf_event_init_all_cpus(void) |
| 8380 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8381 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8382 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8383 | |
| 8384 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8385 | swhash = &per_cpu(swevent_htable, cpu); |
| 8386 | mutex_init(&swhash->hlist_mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 8387 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8388 | } |
| 8389 | } |
| 8390 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 8391 | static void perf_event_init_cpu(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8392 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8393 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8394 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8395 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 8396 | swhash->online = true; |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 8397 | if (swhash->hlist_refcount > 0) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8398 | struct swevent_hlist *hlist; |
| 8399 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8400 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 8401 | WARN_ON(!hlist); |
| 8402 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8403 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8404 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8405 | } |
| 8406 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8407 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8408 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8409 | { |
Mark Rutland | 226424e | 2014-11-05 16:11:44 +0000 | [diff] [blame] | 8410 | struct remove_event re = { .detach_group = true }; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8411 | struct perf_event_context *ctx = __info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8412 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 8413 | rcu_read_lock(); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 8414 | list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry) |
| 8415 | __perf_remove_from_context(&re); |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 8416 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8417 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8418 | |
| 8419 | static void perf_event_exit_cpu_context(int cpu) |
| 8420 | { |
| 8421 | struct perf_event_context *ctx; |
| 8422 | struct pmu *pmu; |
| 8423 | int idx; |
| 8424 | |
| 8425 | idx = srcu_read_lock(&pmus_srcu); |
| 8426 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 8427 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8428 | |
| 8429 | mutex_lock(&ctx->mutex); |
| 8430 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 8431 | mutex_unlock(&ctx->mutex); |
| 8432 | } |
| 8433 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8434 | } |
| 8435 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8436 | static void perf_event_exit_cpu(int cpu) |
| 8437 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8438 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8439 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 8440 | perf_event_exit_cpu_context(cpu); |
| 8441 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8442 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 8443 | swhash->online = false; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8444 | swevent_hlist_release(swhash); |
| 8445 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8446 | } |
| 8447 | #else |
| 8448 | static inline void perf_event_exit_cpu(int cpu) { } |
| 8449 | #endif |
| 8450 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8451 | static int |
| 8452 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 8453 | { |
| 8454 | int cpu; |
| 8455 | |
| 8456 | for_each_online_cpu(cpu) |
| 8457 | perf_event_exit_cpu(cpu); |
| 8458 | |
| 8459 | return NOTIFY_OK; |
| 8460 | } |
| 8461 | |
| 8462 | /* |
| 8463 | * Run the perf reboot notifier at the very last possible moment so that |
| 8464 | * the generic watchdog code runs as long as possible. |
| 8465 | */ |
| 8466 | static struct notifier_block perf_reboot_notifier = { |
| 8467 | .notifier_call = perf_reboot, |
| 8468 | .priority = INT_MIN, |
| 8469 | }; |
| 8470 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 8471 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8472 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 8473 | { |
| 8474 | unsigned int cpu = (long)hcpu; |
| 8475 | |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 8476 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8477 | |
| 8478 | case CPU_UP_PREPARE: |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8479 | case CPU_DOWN_FAILED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8480 | perf_event_init_cpu(cpu); |
| 8481 | break; |
| 8482 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8483 | case CPU_UP_CANCELED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8484 | case CPU_DOWN_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8485 | perf_event_exit_cpu(cpu); |
| 8486 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8487 | default: |
| 8488 | break; |
| 8489 | } |
| 8490 | |
| 8491 | return NOTIFY_OK; |
| 8492 | } |
| 8493 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8494 | void __init perf_event_init(void) |
| 8495 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8496 | int ret; |
| 8497 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8498 | idr_init(&pmu_idr); |
| 8499 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8500 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8501 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8502 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 8503 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 8504 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8505 | perf_tp_register(); |
| 8506 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8507 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8508 | |
| 8509 | ret = init_hw_breakpoint(); |
| 8510 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 8511 | |
| 8512 | /* do not patch jump label more than once per second */ |
| 8513 | jump_label_rate_limit(&perf_sched_events, HZ); |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 8514 | |
| 8515 | /* |
| 8516 | * Build time assertion that we keep the data_head at the intended |
| 8517 | * location. IOW, validation we got the __reserved[] size right. |
| 8518 | */ |
| 8519 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 8520 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8521 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8522 | |
| 8523 | static int __init perf_event_sysfs_init(void) |
| 8524 | { |
| 8525 | struct pmu *pmu; |
| 8526 | int ret; |
| 8527 | |
| 8528 | mutex_lock(&pmus_lock); |
| 8529 | |
| 8530 | ret = bus_register(&pmu_bus); |
| 8531 | if (ret) |
| 8532 | goto unlock; |
| 8533 | |
| 8534 | list_for_each_entry(pmu, &pmus, entry) { |
| 8535 | if (!pmu->name || pmu->type < 0) |
| 8536 | continue; |
| 8537 | |
| 8538 | ret = pmu_dev_alloc(pmu); |
| 8539 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 8540 | } |
| 8541 | pmu_bus_running = 1; |
| 8542 | ret = 0; |
| 8543 | |
| 8544 | unlock: |
| 8545 | mutex_unlock(&pmus_lock); |
| 8546 | |
| 8547 | return ret; |
| 8548 | } |
| 8549 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8550 | |
| 8551 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8552 | static struct cgroup_subsys_state * |
| 8553 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8554 | { |
| 8555 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8556 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 8557 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8558 | if (!jc) |
| 8559 | return ERR_PTR(-ENOMEM); |
| 8560 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8561 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 8562 | if (!jc->info) { |
| 8563 | kfree(jc); |
| 8564 | return ERR_PTR(-ENOMEM); |
| 8565 | } |
| 8566 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8567 | return &jc->css; |
| 8568 | } |
| 8569 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8570 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8571 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8572 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
| 8573 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8574 | free_percpu(jc->info); |
| 8575 | kfree(jc); |
| 8576 | } |
| 8577 | |
| 8578 | static int __perf_cgroup_move(void *info) |
| 8579 | { |
| 8580 | struct task_struct *task = info; |
| 8581 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
| 8582 | return 0; |
| 8583 | } |
| 8584 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8585 | static void perf_cgroup_attach(struct cgroup_subsys_state *css, |
| 8586 | struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8587 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8588 | struct task_struct *task; |
| 8589 | |
Tejun Heo | 924f0d9a | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 8590 | cgroup_taskset_for_each(task, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8591 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8592 | } |
| 8593 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8594 | static void perf_cgroup_exit(struct cgroup_subsys_state *css, |
| 8595 | struct cgroup_subsys_state *old_css, |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 8596 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8597 | { |
| 8598 | /* |
| 8599 | * cgroup_exit() is called in the copy_process() failure path. |
| 8600 | * Ignore this case since the task hasn't ran yet, this avoids |
| 8601 | * trying to poke a half freed task state from generic code. |
| 8602 | */ |
| 8603 | if (!(task->flags & PF_EXITING)) |
| 8604 | return; |
| 8605 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8606 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8607 | } |
| 8608 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 8609 | struct cgroup_subsys perf_event_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 8610 | .css_alloc = perf_cgroup_css_alloc, |
| 8611 | .css_free = perf_cgroup_css_free, |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 8612 | .exit = perf_cgroup_exit, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8613 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8614 | }; |
| 8615 | #endif /* CONFIG_CGROUP_PERF */ |