Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 2 | * Performance events core code: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 5 | * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar |
| 6 | * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
Al Viro | d36b691 | 2011-12-29 17:09:01 -0500 | [diff] [blame] | 7 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8 | * |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 9 | * For licensing details see kernel-base/COPYING |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/cpu.h> |
| 15 | #include <linux/smp.h> |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 16 | #include <linux/idr.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 17 | #include <linux/file.h> |
| 18 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 20 | #include <linux/hash.h> |
Frederic Weisbecker | 12351ef | 2013-04-20 15:48:22 +0200 | [diff] [blame] | 21 | #include <linux/tick.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 22 | #include <linux/sysfs.h> |
| 23 | #include <linux/dcache.h> |
| 24 | #include <linux/percpu.h> |
| 25 | #include <linux/ptrace.h> |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 26 | #include <linux/reboot.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 27 | #include <linux/vmstat.h> |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 28 | #include <linux/device.h> |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 31 | #include <linux/hardirq.h> |
| 32 | #include <linux/rculist.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/syscalls.h> |
| 35 | #include <linux/anon_inodes.h> |
| 36 | #include <linux/kernel_stat.h> |
| 37 | #include <linux/perf_event.h> |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 38 | #include <linux/ftrace_event.h> |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 39 | #include <linux/hw_breakpoint.h> |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 40 | #include <linux/mm_types.h> |
Li Zefan | 877c685 | 2013-03-05 11:38:08 +0800 | [diff] [blame] | 41 | #include <linux/cgroup.h> |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 42 | #include <linux/module.h> |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 43 | #include <linux/mman.h> |
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); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 156 | static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events); |
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 | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 354 | /* |
Li Zefan | 877c685 | 2013-03-05 11:38:08 +0800 | [diff] [blame] | 355 | * perf_cgroup_info keeps track of time_enabled for a cgroup. |
| 356 | * This is a per-cpu dynamically allocated data structure. |
| 357 | */ |
| 358 | struct perf_cgroup_info { |
| 359 | u64 time; |
| 360 | u64 timestamp; |
| 361 | }; |
| 362 | |
| 363 | struct perf_cgroup { |
| 364 | struct cgroup_subsys_state css; |
Namhyung Kim | 86e213e | 2013-03-18 18:56:34 +0900 | [diff] [blame] | 365 | struct perf_cgroup_info __percpu *info; |
Li Zefan | 877c685 | 2013-03-05 11:38:08 +0800 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 369 | * Must ensure cgroup is pinned (css_get) before calling |
| 370 | * this function. In other words, we cannot call this function |
| 371 | * if there is no cgroup event for the current CPU context. |
| 372 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 373 | static inline struct perf_cgroup * |
| 374 | perf_cgroup_from_task(struct task_struct *task) |
| 375 | { |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 376 | return container_of(task_css(task, perf_event_cgrp_id), |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 377 | struct perf_cgroup, css); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static inline bool |
| 381 | perf_cgroup_match(struct perf_event *event) |
| 382 | { |
| 383 | struct perf_event_context *ctx = event->ctx; |
| 384 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 385 | |
Tejun Heo | ef824fa | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 386 | /* @event doesn't care about cgroup */ |
| 387 | if (!event->cgrp) |
| 388 | return true; |
| 389 | |
| 390 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ |
| 391 | if (!cpuctx->cgrp) |
| 392 | return false; |
| 393 | |
| 394 | /* |
| 395 | * Cgroup scoping is recursive. An event enabled for a cgroup is |
| 396 | * also enabled for all its descendant cgroups. If @cpuctx's |
| 397 | * cgroup is a descendant of @event's (the test covers identity |
| 398 | * case), it's a match. |
| 399 | */ |
| 400 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, |
| 401 | event->cgrp->css.cgroup); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 404 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 405 | { |
Zefan Li | 4e2ba65 | 2014-09-19 16:53:14 +0800 | [diff] [blame] | 406 | css_put(&event->cgrp->css); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 407 | event->cgrp = NULL; |
| 408 | } |
| 409 | |
| 410 | static inline int is_cgroup_event(struct perf_event *event) |
| 411 | { |
| 412 | return event->cgrp != NULL; |
| 413 | } |
| 414 | |
| 415 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 416 | { |
| 417 | struct perf_cgroup_info *t; |
| 418 | |
| 419 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 420 | return t->time; |
| 421 | } |
| 422 | |
| 423 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) |
| 424 | { |
| 425 | struct perf_cgroup_info *info; |
| 426 | u64 now; |
| 427 | |
| 428 | now = perf_clock(); |
| 429 | |
| 430 | info = this_cpu_ptr(cgrp->info); |
| 431 | |
| 432 | info->time += now - info->timestamp; |
| 433 | info->timestamp = now; |
| 434 | } |
| 435 | |
| 436 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 437 | { |
| 438 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; |
| 439 | if (cgrp_out) |
| 440 | __update_cgrp_time(cgrp_out); |
| 441 | } |
| 442 | |
| 443 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 444 | { |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 445 | struct perf_cgroup *cgrp; |
| 446 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 447 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 448 | * ensure we access cgroup data only when needed and |
| 449 | * when we know the cgroup is pinned (css_get) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 450 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 451 | if (!is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 452 | return; |
| 453 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 454 | cgrp = perf_cgroup_from_task(current); |
| 455 | /* |
| 456 | * Do not update time when cgroup is not active |
| 457 | */ |
| 458 | if (cgrp == event->cgrp) |
| 459 | __update_cgrp_time(event->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 463 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 464 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 465 | { |
| 466 | struct perf_cgroup *cgrp; |
| 467 | struct perf_cgroup_info *info; |
| 468 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 469 | /* |
| 470 | * ctx->lock held by caller |
| 471 | * ensure we do not access cgroup data |
| 472 | * unless we have the cgroup pinned (css_get) |
| 473 | */ |
| 474 | if (!task || !ctx->nr_cgroups) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 475 | return; |
| 476 | |
| 477 | cgrp = perf_cgroup_from_task(task); |
| 478 | info = this_cpu_ptr(cgrp->info); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 479 | info->timestamp = ctx->timestamp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
| 483 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ |
| 484 | |
| 485 | /* |
| 486 | * reschedule events based on the cgroup constraint of task. |
| 487 | * |
| 488 | * mode SWOUT : schedule out everything |
| 489 | * mode SWIN : schedule in based on cgroup for next |
| 490 | */ |
| 491 | void perf_cgroup_switch(struct task_struct *task, int mode) |
| 492 | { |
| 493 | struct perf_cpu_context *cpuctx; |
| 494 | struct pmu *pmu; |
| 495 | unsigned long flags; |
| 496 | |
| 497 | /* |
| 498 | * disable interrupts to avoid geting nr_cgroup |
| 499 | * changes via __perf_event_disable(). Also |
| 500 | * avoids preemption. |
| 501 | */ |
| 502 | local_irq_save(flags); |
| 503 | |
| 504 | /* |
| 505 | * we reschedule only in the presence of cgroup |
| 506 | * constrained events. |
| 507 | */ |
| 508 | rcu_read_lock(); |
| 509 | |
| 510 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 511 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 512 | if (cpuctx->unique_pmu != pmu) |
| 513 | continue; /* ensure we process each cpuctx once */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 514 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 515 | /* |
| 516 | * perf_cgroup_events says at least one |
| 517 | * context on this CPU has cgroup events. |
| 518 | * |
| 519 | * ctx->nr_cgroups reports the number of cgroup |
| 520 | * events for a context. |
| 521 | */ |
| 522 | if (cpuctx->ctx.nr_cgroups > 0) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 523 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 524 | perf_pmu_disable(cpuctx->ctx.pmu); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 525 | |
| 526 | if (mode & PERF_CGROUP_SWOUT) { |
| 527 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 528 | /* |
| 529 | * must not be done before ctxswout due |
| 530 | * to event_filter_match() in event_sched_out() |
| 531 | */ |
| 532 | cpuctx->cgrp = NULL; |
| 533 | } |
| 534 | |
| 535 | if (mode & PERF_CGROUP_SWIN) { |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 536 | WARN_ON_ONCE(cpuctx->cgrp); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 537 | /* |
| 538 | * set cgrp before ctxsw in to allow |
| 539 | * event_filter_match() to not have to pass |
| 540 | * task around |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 541 | */ |
| 542 | cpuctx->cgrp = perf_cgroup_from_task(task); |
| 543 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
| 544 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 545 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 546 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 547 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | rcu_read_unlock(); |
| 551 | |
| 552 | local_irq_restore(flags); |
| 553 | } |
| 554 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 555 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 556 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 557 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 558 | struct perf_cgroup *cgrp1; |
| 559 | struct perf_cgroup *cgrp2 = NULL; |
| 560 | |
| 561 | /* |
| 562 | * we come here when we know perf_cgroup_events > 0 |
| 563 | */ |
| 564 | cgrp1 = perf_cgroup_from_task(task); |
| 565 | |
| 566 | /* |
| 567 | * next is NULL when called from perf_event_enable_on_exec() |
| 568 | * that will systematically cause a cgroup_switch() |
| 569 | */ |
| 570 | if (next) |
| 571 | cgrp2 = perf_cgroup_from_task(next); |
| 572 | |
| 573 | /* |
| 574 | * only schedule out current cgroup events if we know |
| 575 | * that we are switching to a different cgroup. Otherwise, |
| 576 | * do no touch the cgroup events. |
| 577 | */ |
| 578 | if (cgrp1 != cgrp2) |
| 579 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 580 | } |
| 581 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 582 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 583 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 584 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 585 | struct perf_cgroup *cgrp1; |
| 586 | struct perf_cgroup *cgrp2 = NULL; |
| 587 | |
| 588 | /* |
| 589 | * we come here when we know perf_cgroup_events > 0 |
| 590 | */ |
| 591 | cgrp1 = perf_cgroup_from_task(task); |
| 592 | |
| 593 | /* prev can never be NULL */ |
| 594 | cgrp2 = perf_cgroup_from_task(prev); |
| 595 | |
| 596 | /* |
| 597 | * only need to schedule in cgroup events if we are changing |
| 598 | * cgroup during ctxsw. Cgroup events were not scheduled |
| 599 | * out of ctxsw out if that was not the case. |
| 600 | */ |
| 601 | if (cgrp1 != cgrp2) |
| 602 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
| 606 | struct perf_event_attr *attr, |
| 607 | struct perf_event *group_leader) |
| 608 | { |
| 609 | struct perf_cgroup *cgrp; |
| 610 | struct cgroup_subsys_state *css; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 611 | struct fd f = fdget(fd); |
| 612 | int ret = 0; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 613 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 614 | if (!f.file) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 615 | return -EBADF; |
| 616 | |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 617 | css = css_tryget_online_from_dir(f.file->f_dentry, |
| 618 | &perf_event_cgrp_subsys); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 619 | if (IS_ERR(css)) { |
| 620 | ret = PTR_ERR(css); |
| 621 | goto out; |
| 622 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 623 | |
| 624 | cgrp = container_of(css, struct perf_cgroup, css); |
| 625 | event->cgrp = cgrp; |
| 626 | |
| 627 | /* |
| 628 | * all events in a group must monitor |
| 629 | * the same cgroup because a task belongs |
| 630 | * to only one perf cgroup at a time |
| 631 | */ |
| 632 | if (group_leader && group_leader->cgrp != cgrp) { |
| 633 | perf_detach_cgroup(event); |
| 634 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 635 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 636 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 637 | fdput(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | static inline void |
| 642 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 643 | { |
| 644 | struct perf_cgroup_info *t; |
| 645 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 646 | event->shadow_ctx_time = now - t->timestamp; |
| 647 | } |
| 648 | |
| 649 | static inline void |
| 650 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 651 | { |
| 652 | /* |
| 653 | * when the current task's perf cgroup does not match |
| 654 | * the event's, we need to remember to call the |
| 655 | * perf_mark_enable() function the first time a task with |
| 656 | * a matching perf cgroup is scheduled in. |
| 657 | */ |
| 658 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 659 | event->cgrp_defer_enabled = 1; |
| 660 | } |
| 661 | |
| 662 | static inline void |
| 663 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 664 | struct perf_event_context *ctx) |
| 665 | { |
| 666 | struct perf_event *sub; |
| 667 | u64 tstamp = perf_event_time(event); |
| 668 | |
| 669 | if (!event->cgrp_defer_enabled) |
| 670 | return; |
| 671 | |
| 672 | event->cgrp_defer_enabled = 0; |
| 673 | |
| 674 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 675 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 676 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 677 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 678 | sub->cgrp_defer_enabled = 0; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | #else /* !CONFIG_CGROUP_PERF */ |
| 683 | |
| 684 | static inline bool |
| 685 | perf_cgroup_match(struct perf_event *event) |
| 686 | { |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 691 | {} |
| 692 | |
| 693 | static inline int is_cgroup_event(struct perf_event *event) |
| 694 | { |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 699 | { |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 704 | { |
| 705 | } |
| 706 | |
| 707 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 708 | { |
| 709 | } |
| 710 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 711 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 712 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 713 | { |
| 714 | } |
| 715 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 716 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 717 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 718 | { |
| 719 | } |
| 720 | |
| 721 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 722 | struct perf_event_attr *attr, |
| 723 | struct perf_event *group_leader) |
| 724 | { |
| 725 | return -EINVAL; |
| 726 | } |
| 727 | |
| 728 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 729 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 730 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 731 | { |
| 732 | } |
| 733 | |
| 734 | void |
| 735 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 736 | { |
| 737 | } |
| 738 | |
| 739 | static inline void |
| 740 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 741 | { |
| 742 | } |
| 743 | |
| 744 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 745 | { |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static inline void |
| 750 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 751 | { |
| 752 | } |
| 753 | |
| 754 | static inline void |
| 755 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 756 | struct perf_event_context *ctx) |
| 757 | { |
| 758 | } |
| 759 | #endif |
| 760 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 761 | /* |
| 762 | * set default to be dependent on timer tick just |
| 763 | * like original code |
| 764 | */ |
| 765 | #define PERF_CPU_HRTIMER (1000 / HZ) |
| 766 | /* |
| 767 | * function must be called with interrupts disbled |
| 768 | */ |
| 769 | static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr) |
| 770 | { |
| 771 | struct perf_cpu_context *cpuctx; |
| 772 | enum hrtimer_restart ret = HRTIMER_NORESTART; |
| 773 | int rotations = 0; |
| 774 | |
| 775 | WARN_ON(!irqs_disabled()); |
| 776 | |
| 777 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); |
| 778 | |
| 779 | rotations = perf_rotate_context(cpuctx); |
| 780 | |
| 781 | /* |
| 782 | * arm timer if needed |
| 783 | */ |
| 784 | if (rotations) { |
| 785 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
| 786 | ret = HRTIMER_RESTART; |
| 787 | } |
| 788 | |
| 789 | return ret; |
| 790 | } |
| 791 | |
| 792 | /* CPU is going down */ |
| 793 | void perf_cpu_hrtimer_cancel(int cpu) |
| 794 | { |
| 795 | struct perf_cpu_context *cpuctx; |
| 796 | struct pmu *pmu; |
| 797 | unsigned long flags; |
| 798 | |
| 799 | if (WARN_ON(cpu != smp_processor_id())) |
| 800 | return; |
| 801 | |
| 802 | local_irq_save(flags); |
| 803 | |
| 804 | rcu_read_lock(); |
| 805 | |
| 806 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 807 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 808 | |
| 809 | if (pmu->task_ctx_nr == perf_sw_context) |
| 810 | continue; |
| 811 | |
| 812 | hrtimer_cancel(&cpuctx->hrtimer); |
| 813 | } |
| 814 | |
| 815 | rcu_read_unlock(); |
| 816 | |
| 817 | local_irq_restore(flags); |
| 818 | } |
| 819 | |
| 820 | static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
| 821 | { |
| 822 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 823 | struct pmu *pmu = cpuctx->ctx.pmu; |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 824 | int timer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 825 | |
| 826 | /* no multiplexing needed for SW PMU */ |
| 827 | if (pmu->task_ctx_nr == perf_sw_context) |
| 828 | return; |
| 829 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 830 | /* |
| 831 | * check default is sane, if not set then force to |
| 832 | * default interval (1/tick) |
| 833 | */ |
| 834 | timer = pmu->hrtimer_interval_ms; |
| 835 | if (timer < 1) |
| 836 | timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; |
| 837 | |
| 838 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 839 | |
| 840 | hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
| 841 | hr->function = perf_cpu_hrtimer_handler; |
| 842 | } |
| 843 | |
| 844 | static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx) |
| 845 | { |
| 846 | struct hrtimer *hr = &cpuctx->hrtimer; |
| 847 | struct pmu *pmu = cpuctx->ctx.pmu; |
| 848 | |
| 849 | /* not for SW PMU */ |
| 850 | if (pmu->task_ctx_nr == perf_sw_context) |
| 851 | return; |
| 852 | |
| 853 | if (hrtimer_active(hr)) |
| 854 | return; |
| 855 | |
| 856 | if (!hrtimer_callback_running(hr)) |
| 857 | __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval, |
| 858 | 0, HRTIMER_MODE_REL_PINNED, 0); |
| 859 | } |
| 860 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 861 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 862 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 863 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 864 | if (!(*count)++) |
| 865 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 866 | } |
| 867 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 868 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 869 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 870 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 871 | if (!--(*count)) |
| 872 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 873 | } |
| 874 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 875 | static DEFINE_PER_CPU(struct list_head, rotation_list); |
| 876 | |
| 877 | /* |
| 878 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 879 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 880 | * disabled, while rotate_context is called from IRQ context. |
| 881 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 882 | static void perf_pmu_rotate_start(struct pmu *pmu) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 883 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 884 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 885 | struct list_head *head = this_cpu_ptr(&rotation_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 886 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 887 | WARN_ON(!irqs_disabled()); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 888 | |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 889 | if (list_empty(&cpuctx->rotation_list)) |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 890 | list_add(&cpuctx->rotation_list, head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static void get_ctx(struct perf_event_context *ctx) |
| 894 | { |
| 895 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 896 | } |
| 897 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 898 | static void put_ctx(struct perf_event_context *ctx) |
| 899 | { |
| 900 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 901 | if (ctx->parent_ctx) |
| 902 | put_ctx(ctx->parent_ctx); |
| 903 | if (ctx->task) |
| 904 | put_task_struct(ctx->task); |
Lai Jiangshan | cb796ff | 2011-03-18 12:07:41 +0800 | [diff] [blame] | 905 | kfree_rcu(ctx, rcu_head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 909 | /* |
| 910 | * This must be done under the ctx->lock, such as to serialize against |
| 911 | * context_equiv(), therefore we cannot call put_ctx() since that might end up |
| 912 | * calling scheduler related locks and ctx->lock nests inside those. |
| 913 | */ |
| 914 | static __must_check struct perf_event_context * |
| 915 | unclone_ctx(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 916 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 917 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
| 918 | |
| 919 | lockdep_assert_held(&ctx->lock); |
| 920 | |
| 921 | if (parent_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 922 | ctx->parent_ctx = NULL; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 923 | ctx->generation++; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 924 | |
| 925 | return parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 928 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 929 | { |
| 930 | /* |
| 931 | * only top level events have the pid namespace they were created in |
| 932 | */ |
| 933 | if (event->parent) |
| 934 | event = event->parent; |
| 935 | |
| 936 | return task_tgid_nr_ns(p, event->ns); |
| 937 | } |
| 938 | |
| 939 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 940 | { |
| 941 | /* |
| 942 | * only top level events have the pid namespace they were created in |
| 943 | */ |
| 944 | if (event->parent) |
| 945 | event = event->parent; |
| 946 | |
| 947 | return task_pid_nr_ns(p, event->ns); |
| 948 | } |
| 949 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 950 | /* |
| 951 | * If we inherit events we want to return the parent event id |
| 952 | * to userspace. |
| 953 | */ |
| 954 | static u64 primary_event_id(struct perf_event *event) |
| 955 | { |
| 956 | u64 id = event->id; |
| 957 | |
| 958 | if (event->parent) |
| 959 | id = event->parent->id; |
| 960 | |
| 961 | return id; |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | * Get the perf_event_context for a task and lock it. |
| 966 | * This has to cope with with the fact that until it is locked, |
| 967 | * the context could get moved to another task. |
| 968 | */ |
| 969 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 970 | 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] | 971 | { |
| 972 | struct perf_event_context *ctx; |
| 973 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 974 | retry: |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 975 | /* |
| 976 | * One of the few rules of preemptible RCU is that one cannot do |
| 977 | * rcu_read_unlock() while holding a scheduler (or nested) lock when |
| 978 | * part of the read side critical section was preemptible -- see |
| 979 | * rcu_read_unlock_special(). |
| 980 | * |
| 981 | * Since ctx->lock nests under rq->lock we must ensure the entire read |
| 982 | * side critical section is non-preemptible. |
| 983 | */ |
| 984 | preempt_disable(); |
| 985 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 986 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 987 | if (ctx) { |
| 988 | /* |
| 989 | * If this context is a clone of another, it might |
| 990 | * get swapped for another underneath us by |
| 991 | * perf_event_task_sched_out, though the |
| 992 | * rcu_read_lock() protects us from any context |
| 993 | * getting freed. Lock the context and check if it |
| 994 | * got swapped before we could get the lock, and retry |
| 995 | * if so. If we locked the right context, then it |
| 996 | * can't get swapped on us any more. |
| 997 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 998 | raw_spin_lock_irqsave(&ctx->lock, *flags); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 999 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1000 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1001 | rcu_read_unlock(); |
| 1002 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1003 | goto retry; |
| 1004 | } |
| 1005 | |
| 1006 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1007 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1008 | ctx = NULL; |
| 1009 | } |
| 1010 | } |
| 1011 | rcu_read_unlock(); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1012 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1013 | return ctx; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Get the context for a task and increment its pin_count so it |
| 1018 | * can't get swapped to another task. This also increments its |
| 1019 | * reference count so that the context can't get freed. |
| 1020 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1021 | static struct perf_event_context * |
| 1022 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1023 | { |
| 1024 | struct perf_event_context *ctx; |
| 1025 | unsigned long flags; |
| 1026 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1027 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1028 | if (ctx) { |
| 1029 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1030 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1031 | } |
| 1032 | return ctx; |
| 1033 | } |
| 1034 | |
| 1035 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 1036 | { |
| 1037 | unsigned long flags; |
| 1038 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1039 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1040 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1041 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1044 | /* |
| 1045 | * Update the record of the current time in a context. |
| 1046 | */ |
| 1047 | static void update_context_time(struct perf_event_context *ctx) |
| 1048 | { |
| 1049 | u64 now = perf_clock(); |
| 1050 | |
| 1051 | ctx->time += now - ctx->timestamp; |
| 1052 | ctx->timestamp = now; |
| 1053 | } |
| 1054 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1055 | static u64 perf_event_time(struct perf_event *event) |
| 1056 | { |
| 1057 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1058 | |
| 1059 | if (is_cgroup_event(event)) |
| 1060 | return perf_cgroup_event_time(event); |
| 1061 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1062 | return ctx ? ctx->time : 0; |
| 1063 | } |
| 1064 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1065 | /* |
| 1066 | * 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] | 1067 | * The caller of this function needs to hold the ctx->lock. |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1068 | */ |
| 1069 | static void update_event_times(struct perf_event *event) |
| 1070 | { |
| 1071 | struct perf_event_context *ctx = event->ctx; |
| 1072 | u64 run_end; |
| 1073 | |
| 1074 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1075 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1076 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1077 | /* |
| 1078 | * in cgroup mode, time_enabled represents |
| 1079 | * the time the event was enabled AND active |
| 1080 | * tasks were in the monitored cgroup. This is |
| 1081 | * independent of the activity of the context as |
| 1082 | * there may be a mix of cgroup and non-cgroup events. |
| 1083 | * |
| 1084 | * That is why we treat cgroup events differently |
| 1085 | * here. |
| 1086 | */ |
| 1087 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1088 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1089 | else if (ctx->is_active) |
| 1090 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1091 | else |
| 1092 | run_end = event->tstamp_stopped; |
| 1093 | |
| 1094 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1095 | |
| 1096 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1097 | run_end = event->tstamp_stopped; |
| 1098 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1099 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1100 | |
| 1101 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1102 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1103 | } |
| 1104 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1105 | /* |
| 1106 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1107 | */ |
| 1108 | static void update_group_times(struct perf_event *leader) |
| 1109 | { |
| 1110 | struct perf_event *event; |
| 1111 | |
| 1112 | update_event_times(leader); |
| 1113 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1114 | update_event_times(event); |
| 1115 | } |
| 1116 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1117 | static struct list_head * |
| 1118 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1119 | { |
| 1120 | if (event->attr.pinned) |
| 1121 | return &ctx->pinned_groups; |
| 1122 | else |
| 1123 | return &ctx->flexible_groups; |
| 1124 | } |
| 1125 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1126 | /* |
| 1127 | * Add a event from the lists for its context. |
| 1128 | * Must be called with ctx->mutex and ctx->lock held. |
| 1129 | */ |
| 1130 | static void |
| 1131 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1132 | { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1133 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1134 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1135 | |
| 1136 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1137 | * If we're a stand alone event or group leader, we go to the context |
| 1138 | * list, group events are kept attached to the group so that |
| 1139 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1140 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1141 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1142 | struct list_head *list; |
| 1143 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1144 | if (is_software_event(event)) |
| 1145 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 1146 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1147 | list = ctx_group_list(event, ctx); |
| 1148 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 1151 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1152 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1153 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1154 | if (has_branch_stack(event)) |
| 1155 | ctx->nr_branch_stack++; |
| 1156 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1157 | list_add_rcu(&event->event_entry, &ctx->event_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1158 | if (!ctx->nr_events) |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1159 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1160 | ctx->nr_events++; |
| 1161 | if (event->attr.inherit_stat) |
| 1162 | ctx->nr_stat++; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1163 | |
| 1164 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1167 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1168 | * Initialize event state based on the perf_event_attr::disabled. |
| 1169 | */ |
| 1170 | static inline void perf_event__state_init(struct perf_event *event) |
| 1171 | { |
| 1172 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1173 | PERF_EVENT_STATE_INACTIVE; |
| 1174 | } |
| 1175 | |
| 1176 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1177 | * Called at perf_event creation and when events are attached/detached from a |
| 1178 | * group. |
| 1179 | */ |
| 1180 | static void perf_event__read_size(struct perf_event *event) |
| 1181 | { |
| 1182 | int entry = sizeof(u64); /* value */ |
| 1183 | int size = 0; |
| 1184 | int nr = 1; |
| 1185 | |
| 1186 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1187 | size += sizeof(u64); |
| 1188 | |
| 1189 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1190 | size += sizeof(u64); |
| 1191 | |
| 1192 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1193 | entry += sizeof(u64); |
| 1194 | |
| 1195 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
| 1196 | nr += event->group_leader->nr_siblings; |
| 1197 | size += sizeof(u64); |
| 1198 | } |
| 1199 | |
| 1200 | size += entry * nr; |
| 1201 | event->read_size = size; |
| 1202 | } |
| 1203 | |
| 1204 | static void perf_event__header_size(struct perf_event *event) |
| 1205 | { |
| 1206 | struct perf_sample_data *data; |
| 1207 | u64 sample_type = event->attr.sample_type; |
| 1208 | u16 size = 0; |
| 1209 | |
| 1210 | perf_event__read_size(event); |
| 1211 | |
| 1212 | if (sample_type & PERF_SAMPLE_IP) |
| 1213 | size += sizeof(data->ip); |
| 1214 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1215 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1216 | size += sizeof(data->addr); |
| 1217 | |
| 1218 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1219 | size += sizeof(data->period); |
| 1220 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1221 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1222 | size += sizeof(data->weight); |
| 1223 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1224 | if (sample_type & PERF_SAMPLE_READ) |
| 1225 | size += event->read_size; |
| 1226 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1227 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1228 | size += sizeof(data->data_src.val); |
| 1229 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 1230 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1231 | size += sizeof(data->txn); |
| 1232 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1233 | event->header_size = size; |
| 1234 | } |
| 1235 | |
| 1236 | static void perf_event__id_header_size(struct perf_event *event) |
| 1237 | { |
| 1238 | struct perf_sample_data *data; |
| 1239 | u64 sample_type = event->attr.sample_type; |
| 1240 | u16 size = 0; |
| 1241 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1242 | if (sample_type & PERF_SAMPLE_TID) |
| 1243 | size += sizeof(data->tid_entry); |
| 1244 | |
| 1245 | if (sample_type & PERF_SAMPLE_TIME) |
| 1246 | size += sizeof(data->time); |
| 1247 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 1248 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1249 | size += sizeof(data->id); |
| 1250 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1251 | if (sample_type & PERF_SAMPLE_ID) |
| 1252 | size += sizeof(data->id); |
| 1253 | |
| 1254 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1255 | size += sizeof(data->stream_id); |
| 1256 | |
| 1257 | if (sample_type & PERF_SAMPLE_CPU) |
| 1258 | size += sizeof(data->cpu_entry); |
| 1259 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1260 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1261 | } |
| 1262 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1263 | static void perf_group_attach(struct perf_event *event) |
| 1264 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1265 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1266 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1267 | /* |
| 1268 | * We can have double attach due to group movement in perf_event_open. |
| 1269 | */ |
| 1270 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1271 | return; |
| 1272 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1273 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1274 | |
| 1275 | if (group_leader == event) |
| 1276 | return; |
| 1277 | |
| 1278 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 1279 | !is_software_event(event)) |
| 1280 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 1281 | |
| 1282 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1283 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1284 | |
| 1285 | perf_event__header_size(group_leader); |
| 1286 | |
| 1287 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1288 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1289 | } |
| 1290 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1291 | /* |
| 1292 | * Remove a event from the lists for its context. |
| 1293 | * Must be called with ctx->mutex and ctx->lock held. |
| 1294 | */ |
| 1295 | static void |
| 1296 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1297 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1298 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1299 | /* |
| 1300 | * We can have double detach due to exit/hot-unplug + close. |
| 1301 | */ |
| 1302 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1303 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1304 | |
| 1305 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1306 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1307 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1308 | ctx->nr_cgroups--; |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1309 | cpuctx = __get_cpu_context(ctx); |
| 1310 | /* |
| 1311 | * if there are no more cgroup events |
| 1312 | * then cler cgrp to avoid stale pointer |
| 1313 | * in update_cgrp_time_from_cpuctx() |
| 1314 | */ |
| 1315 | if (!ctx->nr_cgroups) |
| 1316 | cpuctx->cgrp = NULL; |
| 1317 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1318 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1319 | if (has_branch_stack(event)) |
| 1320 | ctx->nr_branch_stack--; |
| 1321 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1322 | ctx->nr_events--; |
| 1323 | if (event->attr.inherit_stat) |
| 1324 | ctx->nr_stat--; |
| 1325 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1326 | list_del_rcu(&event->event_entry); |
| 1327 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1328 | if (event->group_leader == event) |
| 1329 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1330 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1331 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1332 | |
| 1333 | /* |
| 1334 | * If event was in error state, then keep it |
| 1335 | * that way, otherwise bogus counts will be |
| 1336 | * returned on read(). The only way to get out |
| 1337 | * of error state is by explicit re-enabling |
| 1338 | * of the event |
| 1339 | */ |
| 1340 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1341 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1342 | |
| 1343 | ctx->generation++; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1344 | } |
| 1345 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1346 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1347 | { |
| 1348 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1349 | struct list_head *list = NULL; |
| 1350 | |
| 1351 | /* |
| 1352 | * We can have double detach due to exit/hot-unplug + close. |
| 1353 | */ |
| 1354 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1355 | return; |
| 1356 | |
| 1357 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1358 | |
| 1359 | /* |
| 1360 | * If this is a sibling, remove it from its group. |
| 1361 | */ |
| 1362 | if (event->group_leader != event) { |
| 1363 | list_del_init(&event->group_entry); |
| 1364 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1365 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | if (!list_empty(&event->group_entry)) |
| 1369 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1370 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1371 | /* |
| 1372 | * If this was a group event with sibling events then |
| 1373 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1374 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1375 | */ |
| 1376 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1377 | if (list) |
| 1378 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1379 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1380 | |
| 1381 | /* Inherit group flags from the previous leader */ |
| 1382 | sibling->group_flags = event->group_flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1383 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1384 | |
| 1385 | out: |
| 1386 | perf_event__header_size(event->group_leader); |
| 1387 | |
| 1388 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1389 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1392 | /* |
| 1393 | * User event without the task. |
| 1394 | */ |
| 1395 | static bool is_orphaned_event(struct perf_event *event) |
| 1396 | { |
| 1397 | return event && !is_kernel_event(event) && !event->owner; |
| 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * Event has a parent but parent's task finished and it's |
| 1402 | * alive only because of children holding refference. |
| 1403 | */ |
| 1404 | static bool is_orphaned_child(struct perf_event *event) |
| 1405 | { |
| 1406 | return is_orphaned_event(event->parent); |
| 1407 | } |
| 1408 | |
| 1409 | static void orphans_remove_work(struct work_struct *work); |
| 1410 | |
| 1411 | static void schedule_orphans_remove(struct perf_event_context *ctx) |
| 1412 | { |
| 1413 | if (!ctx->task || ctx->orphans_remove_sched || !perf_wq) |
| 1414 | return; |
| 1415 | |
| 1416 | if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) { |
| 1417 | get_ctx(ctx); |
| 1418 | ctx->orphans_remove_sched = true; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | static int __init perf_workqueue_init(void) |
| 1423 | { |
| 1424 | perf_wq = create_singlethread_workqueue("perf"); |
| 1425 | WARN(!perf_wq, "failed to create perf workqueue\n"); |
| 1426 | return perf_wq ? 0 : -1; |
| 1427 | } |
| 1428 | |
| 1429 | core_initcall(perf_workqueue_init); |
| 1430 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1431 | static inline int |
| 1432 | event_filter_match(struct perf_event *event) |
| 1433 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1434 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
| 1435 | && perf_cgroup_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1436 | } |
| 1437 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1438 | static void |
| 1439 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1440 | struct perf_cpu_context *cpuctx, |
| 1441 | struct perf_event_context *ctx) |
| 1442 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1443 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1444 | u64 delta; |
| 1445 | /* |
| 1446 | * An event which could not be activated because of |
| 1447 | * filter mismatch still needs to have its timings |
| 1448 | * maintained, otherwise bogus information is return |
| 1449 | * via read() for time_enabled, time_running: |
| 1450 | */ |
| 1451 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1452 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1453 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1454 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1455 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1456 | } |
| 1457 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1458 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1459 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1460 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1461 | perf_pmu_disable(event->pmu); |
| 1462 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1463 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1464 | if (event->pending_disable) { |
| 1465 | event->pending_disable = 0; |
| 1466 | event->state = PERF_EVENT_STATE_OFF; |
| 1467 | } |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1468 | event->tstamp_stopped = tstamp; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1469 | event->pmu->del(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1470 | event->oncpu = -1; |
| 1471 | |
| 1472 | if (!is_software_event(event)) |
| 1473 | cpuctx->active_oncpu--; |
| 1474 | ctx->nr_active--; |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1475 | if (event->attr.freq && event->attr.sample_freq) |
| 1476 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1477 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1478 | cpuctx->exclusive = 0; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1479 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1480 | if (is_orphaned_child(event)) |
| 1481 | schedule_orphans_remove(ctx); |
| 1482 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1483 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | static void |
| 1487 | group_sched_out(struct perf_event *group_event, |
| 1488 | struct perf_cpu_context *cpuctx, |
| 1489 | struct perf_event_context *ctx) |
| 1490 | { |
| 1491 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1492 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1493 | |
| 1494 | event_sched_out(group_event, cpuctx, ctx); |
| 1495 | |
| 1496 | /* |
| 1497 | * Schedule out siblings (if any): |
| 1498 | */ |
| 1499 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1500 | event_sched_out(event, cpuctx, ctx); |
| 1501 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1502 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1503 | cpuctx->exclusive = 0; |
| 1504 | } |
| 1505 | |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1506 | struct remove_event { |
| 1507 | struct perf_event *event; |
| 1508 | bool detach_group; |
| 1509 | }; |
| 1510 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1511 | /* |
| 1512 | * Cross CPU call to remove a performance event |
| 1513 | * |
| 1514 | * We disable the event on the hardware level first. After that we |
| 1515 | * remove it from the context list. |
| 1516 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1517 | static int __perf_remove_from_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1518 | { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1519 | struct remove_event *re = info; |
| 1520 | struct perf_event *event = re->event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1521 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1522 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1523 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1524 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1525 | event_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1526 | if (re->detach_group) |
| 1527 | perf_group_detach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1528 | list_del_event(event, ctx); |
Peter Zijlstra | 64ce312 | 2011-04-09 21:17:48 +0200 | [diff] [blame] | 1529 | if (!ctx->nr_events && cpuctx->task_ctx == ctx) { |
| 1530 | ctx->is_active = 0; |
| 1531 | cpuctx->task_ctx = NULL; |
| 1532 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1533 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1534 | |
| 1535 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | /* |
| 1540 | * Remove the event from a task's (or a CPU's) list of events. |
| 1541 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1542 | * CPU events are removed with a smp call. For task events we only |
| 1543 | * call when the task is on a CPU. |
| 1544 | * |
| 1545 | * If event->ctx is a cloned context, callers must make sure that |
| 1546 | * every task struct that event->ctx->task could possibly point to |
| 1547 | * remains valid. This is OK when called from perf_release since |
| 1548 | * that only calls us on the top-level context, which can't be a clone. |
| 1549 | * When called from perf_event_exit_task, it's OK because the |
| 1550 | * context has been detached from its task. |
| 1551 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1552 | 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] | 1553 | { |
| 1554 | struct perf_event_context *ctx = event->ctx; |
| 1555 | struct task_struct *task = ctx->task; |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1556 | struct remove_event re = { |
| 1557 | .event = event, |
| 1558 | .detach_group = detach_group, |
| 1559 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1560 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1561 | lockdep_assert_held(&ctx->mutex); |
| 1562 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1563 | if (!task) { |
| 1564 | /* |
Mark Rutland | 226424e | 2014-11-05 16:11:44 +0000 | [diff] [blame] | 1565 | * Per cpu events are removed via an smp call. The removal can |
| 1566 | * fail if the CPU is currently offline, but in that case we |
| 1567 | * already called __perf_remove_from_context from |
| 1568 | * perf_event_exit_cpu. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1569 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1570 | cpu_function_call(event->cpu, __perf_remove_from_context, &re); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1571 | return; |
| 1572 | } |
| 1573 | |
| 1574 | retry: |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1575 | if (!task_function_call(task, __perf_remove_from_context, &re)) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1576 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1577 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1578 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1579 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1580 | * If we failed to find a running task, but find the context active now |
| 1581 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1582 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1583 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1584 | raw_spin_unlock_irq(&ctx->lock); |
Cong Wang | 3577af7 | 2014-09-02 15:27:20 -0700 | [diff] [blame] | 1585 | /* |
| 1586 | * Reload the task pointer, it might have been changed by |
| 1587 | * a concurrent perf_event_context_sched_out(). |
| 1588 | */ |
| 1589 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1590 | goto retry; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1594 | * Since the task isn't running, its safe to remove the event, us |
| 1595 | * holding the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1596 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1597 | if (detach_group) |
| 1598 | perf_group_detach(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1599 | list_del_event(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1600 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1601 | } |
| 1602 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1603 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1604 | * Cross CPU call to disable a performance event |
| 1605 | */ |
K.Prasad | 500ad2d | 2012-08-02 13:46:35 +0530 | [diff] [blame] | 1606 | int __perf_event_disable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1607 | { |
| 1608 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1609 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1610 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1611 | |
| 1612 | /* |
| 1613 | * If this is a per-task event, need to check whether this |
| 1614 | * event's task is the current task on this cpu. |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1615 | * |
| 1616 | * Can trigger due to concurrent perf_event_context_sched_out() |
| 1617 | * flipping contexts around. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1618 | */ |
| 1619 | if (ctx->task && cpuctx->task_ctx != ctx) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1620 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1621 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1622 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1623 | |
| 1624 | /* |
| 1625 | * If the event is on, turn it off. |
| 1626 | * If it is in error state, leave it in error state. |
| 1627 | */ |
| 1628 | if (event->state >= PERF_EVENT_STATE_INACTIVE) { |
| 1629 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1630 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1631 | update_group_times(event); |
| 1632 | if (event == event->group_leader) |
| 1633 | group_sched_out(event, cpuctx, ctx); |
| 1634 | else |
| 1635 | event_sched_out(event, cpuctx, ctx); |
| 1636 | event->state = PERF_EVENT_STATE_OFF; |
| 1637 | } |
| 1638 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1639 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1640 | |
| 1641 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * Disable a event. |
| 1646 | * |
| 1647 | * If event->ctx is a cloned context, callers must make sure that |
| 1648 | * every task struct that event->ctx->task could possibly point to |
| 1649 | * remains valid. This condition is satisifed when called through |
| 1650 | * perf_event_for_each_child or perf_event_for_each because they |
| 1651 | * hold the top-level event's child_mutex, so any descendant that |
| 1652 | * goes to exit will block in sync_child_event. |
| 1653 | * When called from perf_pending_event it's OK because event->ctx |
| 1654 | * is the current context on this CPU and preemption is disabled, |
| 1655 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1656 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 1657 | void perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1658 | { |
| 1659 | struct perf_event_context *ctx = event->ctx; |
| 1660 | struct task_struct *task = ctx->task; |
| 1661 | |
| 1662 | if (!task) { |
| 1663 | /* |
| 1664 | * Disable the event on the cpu that it's on |
| 1665 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1666 | cpu_function_call(event->cpu, __perf_event_disable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1667 | return; |
| 1668 | } |
| 1669 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1670 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1671 | if (!task_function_call(task, __perf_event_disable, event)) |
| 1672 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1673 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1674 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1675 | /* |
| 1676 | * If the event is still active, we need to retry the cross-call. |
| 1677 | */ |
| 1678 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1679 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1680 | /* |
| 1681 | * Reload the task pointer, it might have been changed by |
| 1682 | * a concurrent perf_event_context_sched_out(). |
| 1683 | */ |
| 1684 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1685 | goto retry; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * Since we have the lock this context can't be scheduled |
| 1690 | * in, so we can change the state safely. |
| 1691 | */ |
| 1692 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 1693 | update_group_times(event); |
| 1694 | event->state = PERF_EVENT_STATE_OFF; |
| 1695 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1696 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1697 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 1698 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1699 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1700 | static void perf_set_shadow_time(struct perf_event *event, |
| 1701 | struct perf_event_context *ctx, |
| 1702 | u64 tstamp) |
| 1703 | { |
| 1704 | /* |
| 1705 | * use the correct time source for the time snapshot |
| 1706 | * |
| 1707 | * We could get by without this by leveraging the |
| 1708 | * fact that to get to this function, the caller |
| 1709 | * has most likely already called update_context_time() |
| 1710 | * and update_cgrp_time_xx() and thus both timestamp |
| 1711 | * are identical (or very close). Given that tstamp is, |
| 1712 | * already adjusted for cgroup, we could say that: |
| 1713 | * tstamp - ctx->timestamp |
| 1714 | * is equivalent to |
| 1715 | * tstamp - cgrp->timestamp. |
| 1716 | * |
| 1717 | * Then, in perf_output_read(), the calculation would |
| 1718 | * work with no changes because: |
| 1719 | * - event is guaranteed scheduled in |
| 1720 | * - no scheduled out in between |
| 1721 | * - thus the timestamp would be the same |
| 1722 | * |
| 1723 | * But this is a bit hairy. |
| 1724 | * |
| 1725 | * So instead, we have an explicit cgroup call to remain |
| 1726 | * within the time time source all along. We believe it |
| 1727 | * is cleaner and simpler to understand. |
| 1728 | */ |
| 1729 | if (is_cgroup_event(event)) |
| 1730 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1731 | else |
| 1732 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1733 | } |
| 1734 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1735 | #define MAX_INTERRUPTS (~0ULL) |
| 1736 | |
| 1737 | static void perf_log_throttle(struct perf_event *event, int enable); |
| 1738 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1739 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1740 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1741 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1742 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1743 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1744 | u64 tstamp = perf_event_time(event); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1745 | int ret = 0; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1746 | |
Peter Zijlstra | 6334241 | 2014-05-05 11:49:16 +0200 | [diff] [blame] | 1747 | lockdep_assert_held(&ctx->lock); |
| 1748 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1749 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1750 | return 0; |
| 1751 | |
| 1752 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1753 | event->oncpu = smp_processor_id(); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1754 | |
| 1755 | /* |
| 1756 | * Unthrottle events, since we scheduled we might have missed several |
| 1757 | * ticks already, also for a heavily scheduling task there is little |
| 1758 | * guarantee it'll get a tick in a timely manner. |
| 1759 | */ |
| 1760 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1761 | perf_log_throttle(event, 1); |
| 1762 | event->hw.interrupts = 0; |
| 1763 | } |
| 1764 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1765 | /* |
| 1766 | * The new state must be visible before we turn it on in the hardware: |
| 1767 | */ |
| 1768 | smp_wmb(); |
| 1769 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1770 | perf_pmu_disable(event->pmu); |
| 1771 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1772 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1773 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1774 | event->oncpu = -1; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1775 | ret = -EAGAIN; |
| 1776 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1777 | } |
| 1778 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1779 | event->tstamp_running += tstamp - event->tstamp_stopped; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1780 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1781 | perf_set_shadow_time(event, ctx, tstamp); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 1782 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1783 | if (!is_software_event(event)) |
| 1784 | cpuctx->active_oncpu++; |
| 1785 | ctx->nr_active++; |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1786 | if (event->attr.freq && event->attr.sample_freq) |
| 1787 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1788 | |
| 1789 | if (event->attr.exclusive) |
| 1790 | cpuctx->exclusive = 1; |
| 1791 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1792 | if (is_orphaned_child(event)) |
| 1793 | schedule_orphans_remove(ctx); |
| 1794 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1795 | out: |
| 1796 | perf_pmu_enable(event->pmu); |
| 1797 | |
| 1798 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | static int |
| 1802 | group_sched_in(struct perf_event *group_event, |
| 1803 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1804 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1805 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1806 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 4a23459 | 2014-02-24 12:43:31 +0100 | [diff] [blame] | 1807 | struct pmu *pmu = ctx->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1808 | u64 now = ctx->time; |
| 1809 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1810 | |
| 1811 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1812 | return 0; |
| 1813 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1814 | pmu->start_txn(pmu); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1815 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1816 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1817 | pmu->cancel_txn(pmu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1818 | perf_cpu_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1819 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1820 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1821 | |
| 1822 | /* |
| 1823 | * Schedule in siblings as one group (if any): |
| 1824 | */ |
| 1825 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1826 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1827 | partial_group = event; |
| 1828 | goto group_error; |
| 1829 | } |
| 1830 | } |
| 1831 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1832 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 1833 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1834 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1835 | group_error: |
| 1836 | /* |
| 1837 | * Groups can be scheduled in as one unit only, so undo any |
| 1838 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1839 | * The events up to the failed event are scheduled out normally, |
| 1840 | * tstamp_stopped will be updated. |
| 1841 | * |
| 1842 | * The failed events and the remaining siblings need to have |
| 1843 | * their timings updated as if they had gone thru event_sched_in() |
| 1844 | * and event_sched_out(). This is required to get consistent timings |
| 1845 | * across the group. This also takes care of the case where the group |
| 1846 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 1847 | * the time the event was actually stopped, such that time delta |
| 1848 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1849 | */ |
| 1850 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 1851 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1852 | simulate = true; |
| 1853 | |
| 1854 | if (simulate) { |
| 1855 | event->tstamp_running += now - event->tstamp_stopped; |
| 1856 | event->tstamp_stopped = now; |
| 1857 | } else { |
| 1858 | event_sched_out(event, cpuctx, ctx); |
| 1859 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1860 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1861 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1862 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1863 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1864 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1865 | perf_cpu_hrtimer_restart(cpuctx); |
| 1866 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1867 | return -EAGAIN; |
| 1868 | } |
| 1869 | |
| 1870 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1871 | * Work out whether we can put this event group on the CPU now. |
| 1872 | */ |
| 1873 | static int group_can_go_on(struct perf_event *event, |
| 1874 | struct perf_cpu_context *cpuctx, |
| 1875 | int can_add_hw) |
| 1876 | { |
| 1877 | /* |
| 1878 | * Groups consisting entirely of software events can always go on. |
| 1879 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1880 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1881 | return 1; |
| 1882 | /* |
| 1883 | * If an exclusive group is already on, no other hardware |
| 1884 | * events can go on. |
| 1885 | */ |
| 1886 | if (cpuctx->exclusive) |
| 1887 | return 0; |
| 1888 | /* |
| 1889 | * If this group is exclusive and there are already |
| 1890 | * events on the CPU, it can't go on. |
| 1891 | */ |
| 1892 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 1893 | return 0; |
| 1894 | /* |
| 1895 | * Otherwise, try to add it if all previous groups were able |
| 1896 | * to go on. |
| 1897 | */ |
| 1898 | return can_add_hw; |
| 1899 | } |
| 1900 | |
| 1901 | static void add_event_to_ctx(struct perf_event *event, |
| 1902 | struct perf_event_context *ctx) |
| 1903 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1904 | u64 tstamp = perf_event_time(event); |
| 1905 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1906 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1907 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1908 | event->tstamp_enabled = tstamp; |
| 1909 | event->tstamp_running = tstamp; |
| 1910 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1911 | } |
| 1912 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1913 | static void task_ctx_sched_out(struct perf_event_context *ctx); |
| 1914 | static void |
| 1915 | ctx_sched_in(struct perf_event_context *ctx, |
| 1916 | struct perf_cpu_context *cpuctx, |
| 1917 | enum event_type_t event_type, |
| 1918 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1919 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 1920 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 1921 | struct perf_event_context *ctx, |
| 1922 | struct task_struct *task) |
| 1923 | { |
| 1924 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 1925 | if (ctx) |
| 1926 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 1927 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 1928 | if (ctx) |
| 1929 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 1930 | } |
| 1931 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1932 | /* |
| 1933 | * Cross CPU call to install and enable a performance event |
| 1934 | * |
| 1935 | * Must be called with ctx->mutex held |
| 1936 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1937 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1938 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1939 | struct perf_event *event = info; |
| 1940 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1941 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1942 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
| 1943 | struct task_struct *task = current; |
| 1944 | |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1945 | perf_ctx_lock(cpuctx, task_ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1946 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1947 | |
| 1948 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1949 | * If there was an active task_ctx schedule it out. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1950 | */ |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1951 | if (task_ctx) |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1952 | task_ctx_sched_out(task_ctx); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1953 | |
| 1954 | /* |
| 1955 | * If the context we're installing events in is not the |
| 1956 | * active task_ctx, flip them. |
| 1957 | */ |
| 1958 | if (ctx->task && task_ctx != ctx) { |
| 1959 | if (task_ctx) |
| 1960 | raw_spin_unlock(&task_ctx->lock); |
| 1961 | raw_spin_lock(&ctx->lock); |
| 1962 | task_ctx = ctx; |
| 1963 | } |
| 1964 | |
| 1965 | if (task_ctx) { |
| 1966 | cpuctx->task_ctx = task_ctx; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1967 | task = task_ctx->task; |
| 1968 | } |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 1969 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1970 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1971 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1972 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1973 | /* |
| 1974 | * update cgrp time only if current cgrp |
| 1975 | * matches event->cgrp. Must be done before |
| 1976 | * calling add_event_to_ctx() |
| 1977 | */ |
| 1978 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1979 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1980 | add_event_to_ctx(event, ctx); |
| 1981 | |
| 1982 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1983 | * Schedule everything back in |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1984 | */ |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 1985 | perf_event_sched_in(cpuctx, task_ctx, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1986 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 1987 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 1988 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1989 | |
| 1990 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * Attach a performance event to a context |
| 1995 | * |
| 1996 | * First we add the event to the list with the hardware enable bit |
| 1997 | * in event->hw_config cleared. |
| 1998 | * |
| 1999 | * If the event is attached to a task which is on a CPU we use a smp |
| 2000 | * call to enable it in the task context. The task might have been |
| 2001 | * scheduled away, but we check this in the smp call again. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2002 | */ |
| 2003 | static void |
| 2004 | perf_install_in_context(struct perf_event_context *ctx, |
| 2005 | struct perf_event *event, |
| 2006 | int cpu) |
| 2007 | { |
| 2008 | struct task_struct *task = ctx->task; |
| 2009 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2010 | lockdep_assert_held(&ctx->mutex); |
| 2011 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2012 | event->ctx = ctx; |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 2013 | if (event->cpu != -1) |
| 2014 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2015 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2016 | if (!task) { |
| 2017 | /* |
| 2018 | * Per cpu events are installed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2019 | * the install is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2020 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2021 | cpu_function_call(cpu, __perf_install_in_context, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2022 | return; |
| 2023 | } |
| 2024 | |
| 2025 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2026 | if (!task_function_call(task, __perf_install_in_context, event)) |
| 2027 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2028 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2029 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2030 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2031 | * If we failed to find a running task, but find the context active now |
| 2032 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2033 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2034 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2035 | raw_spin_unlock_irq(&ctx->lock); |
Cong Wang | 3577af7 | 2014-09-02 15:27:20 -0700 | [diff] [blame] | 2036 | /* |
| 2037 | * Reload the task pointer, it might have been changed by |
| 2038 | * a concurrent perf_event_context_sched_out(). |
| 2039 | */ |
| 2040 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2041 | goto retry; |
| 2042 | } |
| 2043 | |
| 2044 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2045 | * Since the task isn't running, its safe to add the event, us holding |
| 2046 | * the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2047 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2048 | add_event_to_ctx(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2049 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /* |
| 2053 | * Put a event into inactive state and update time fields. |
| 2054 | * Enabling the leader of a group effectively enables all |
| 2055 | * the group members that aren't explicitly disabled, so we |
| 2056 | * have to update their ->tstamp_enabled also. |
| 2057 | * Note: this works for group members as well as group leaders |
| 2058 | * since the non-leader members' sibling_lists will be empty. |
| 2059 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2060 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2061 | { |
| 2062 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2063 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2064 | |
| 2065 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2066 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2067 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2068 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 2069 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2070 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | /* |
| 2074 | * Cross CPU call to enable a performance event |
| 2075 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2076 | static int __perf_event_enable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2077 | { |
| 2078 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2079 | struct perf_event_context *ctx = event->ctx; |
| 2080 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2081 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2082 | int err; |
| 2083 | |
Jiri Olsa | 06f4179 | 2013-07-09 17:44:11 +0200 | [diff] [blame] | 2084 | /* |
| 2085 | * There's a time window between 'ctx->is_active' check |
| 2086 | * in perf_event_enable function and this place having: |
| 2087 | * - IRQs on |
| 2088 | * - ctx->lock unlocked |
| 2089 | * |
| 2090 | * where the task could be killed and 'ctx' deactivated |
| 2091 | * by perf_event_exit_task. |
| 2092 | */ |
| 2093 | if (!ctx->is_active) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2094 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2095 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2096 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2097 | update_context_time(ctx); |
| 2098 | |
| 2099 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2100 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2101 | |
| 2102 | /* |
| 2103 | * set current task's cgroup time reference point |
| 2104 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2105 | perf_cgroup_set_timestamp(current, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2106 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2107 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2108 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2109 | if (!event_filter_match(event)) { |
| 2110 | if (is_cgroup_event(event)) |
| 2111 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2112 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2113 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2114 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2115 | /* |
| 2116 | * If the event is in a group and isn't the group leader, |
| 2117 | * then don't put it on unless the group is on. |
| 2118 | */ |
| 2119 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) |
| 2120 | goto unlock; |
| 2121 | |
| 2122 | if (!group_can_go_on(event, cpuctx, 1)) { |
| 2123 | err = -EEXIST; |
| 2124 | } else { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2125 | if (event == leader) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2126 | err = group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2127 | else |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2128 | err = event_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | if (err) { |
| 2132 | /* |
| 2133 | * If this event can't go on and it's part of a |
| 2134 | * group, then the whole group has to come off. |
| 2135 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2136 | if (leader != event) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2137 | group_sched_out(leader, cpuctx, ctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2138 | perf_cpu_hrtimer_restart(cpuctx); |
| 2139 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2140 | if (leader->attr.pinned) { |
| 2141 | update_group_times(leader); |
| 2142 | leader->state = PERF_EVENT_STATE_ERROR; |
| 2143 | } |
| 2144 | } |
| 2145 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2146 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2147 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2148 | |
| 2149 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /* |
| 2153 | * Enable a event. |
| 2154 | * |
| 2155 | * If event->ctx is a cloned context, callers must make sure that |
| 2156 | * every task struct that event->ctx->task could possibly point to |
| 2157 | * remains valid. This condition is satisfied when called through |
| 2158 | * perf_event_for_each_child or perf_event_for_each as described |
| 2159 | * for perf_event_disable. |
| 2160 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 2161 | void perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2162 | { |
| 2163 | struct perf_event_context *ctx = event->ctx; |
| 2164 | struct task_struct *task = ctx->task; |
| 2165 | |
| 2166 | if (!task) { |
| 2167 | /* |
| 2168 | * Enable the event on the cpu that it's on |
| 2169 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2170 | cpu_function_call(event->cpu, __perf_event_enable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2171 | return; |
| 2172 | } |
| 2173 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2174 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2175 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2176 | goto out; |
| 2177 | |
| 2178 | /* |
| 2179 | * If the event is in error state, clear that first. |
| 2180 | * That way, if we see the event in error state below, we |
| 2181 | * know that it has gone back into error state, as distinct |
| 2182 | * from the task having been scheduled away before the |
| 2183 | * cross-call arrived. |
| 2184 | */ |
| 2185 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2186 | event->state = PERF_EVENT_STATE_OFF; |
| 2187 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2188 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2189 | if (!ctx->is_active) { |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2190 | __perf_event_mark_enabled(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2191 | goto out; |
| 2192 | } |
| 2193 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2194 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2195 | |
| 2196 | if (!task_function_call(task, __perf_event_enable, event)) |
| 2197 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2198 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2199 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2200 | |
| 2201 | /* |
| 2202 | * If the context is active and the event is still off, |
| 2203 | * we need to retry the cross-call. |
| 2204 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2205 | if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) { |
| 2206 | /* |
| 2207 | * task could have been flipped by a concurrent |
| 2208 | * perf_event_context_sched_out() |
| 2209 | */ |
| 2210 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2211 | goto retry; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2212 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2213 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2214 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2215 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2216 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2217 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2218 | |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2219 | int perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2220 | { |
| 2221 | /* |
| 2222 | * not supported on inherited events |
| 2223 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2224 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2225 | return -EINVAL; |
| 2226 | |
| 2227 | atomic_add(refresh, &event->event_limit); |
| 2228 | perf_event_enable(event); |
| 2229 | |
| 2230 | return 0; |
| 2231 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2232 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2233 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2234 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2235 | struct perf_cpu_context *cpuctx, |
| 2236 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2237 | { |
| 2238 | struct perf_event *event; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2239 | int is_active = ctx->is_active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2240 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2241 | ctx->is_active &= ~event_type; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2242 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2243 | return; |
| 2244 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2245 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2246 | update_cgrp_time_from_cpuctx(cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2247 | if (!ctx->nr_active) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2248 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2249 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2250 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2251 | if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2252 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2253 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2254 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2255 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2256 | if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2257 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2258 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2259 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2260 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | /* |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2264 | * Test whether two contexts are equivalent, i.e. whether they have both been |
| 2265 | * cloned from the same version of the same context. |
| 2266 | * |
| 2267 | * Equivalence is measured using a generation number in the context that is |
| 2268 | * incremented on each modification to it; see unclone_ctx(), list_add_event() |
| 2269 | * and list_del_event(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2270 | */ |
| 2271 | static int context_equiv(struct perf_event_context *ctx1, |
| 2272 | struct perf_event_context *ctx2) |
| 2273 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 2274 | lockdep_assert_held(&ctx1->lock); |
| 2275 | lockdep_assert_held(&ctx2->lock); |
| 2276 | |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2277 | /* Pinning disables the swap optimization */ |
| 2278 | if (ctx1->pin_count || ctx2->pin_count) |
| 2279 | return 0; |
| 2280 | |
| 2281 | /* If ctx1 is the parent of ctx2 */ |
| 2282 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) |
| 2283 | return 1; |
| 2284 | |
| 2285 | /* If ctx2 is the parent of ctx1 */ |
| 2286 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) |
| 2287 | return 1; |
| 2288 | |
| 2289 | /* |
| 2290 | * If ctx1 and ctx2 have the same parent; we flatten the parent |
| 2291 | * hierarchy, see perf_event_init_context(). |
| 2292 | */ |
| 2293 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && |
| 2294 | ctx1->parent_gen == ctx2->parent_gen) |
| 2295 | return 1; |
| 2296 | |
| 2297 | /* Unmatched */ |
| 2298 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2299 | } |
| 2300 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2301 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2302 | struct perf_event *next_event) |
| 2303 | { |
| 2304 | u64 value; |
| 2305 | |
| 2306 | if (!event->attr.inherit_stat) |
| 2307 | return; |
| 2308 | |
| 2309 | /* |
| 2310 | * Update the event value, we cannot use perf_event_read() |
| 2311 | * because we're in the middle of a context switch and have IRQs |
| 2312 | * disabled, which upsets smp_call_function_single(), however |
| 2313 | * we know the event must be on the current CPU, therefore we |
| 2314 | * don't need to use it. |
| 2315 | */ |
| 2316 | switch (event->state) { |
| 2317 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2318 | event->pmu->read(event); |
| 2319 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2320 | |
| 2321 | case PERF_EVENT_STATE_INACTIVE: |
| 2322 | update_event_times(event); |
| 2323 | break; |
| 2324 | |
| 2325 | default: |
| 2326 | break; |
| 2327 | } |
| 2328 | |
| 2329 | /* |
| 2330 | * In order to keep per-task stats reliable we need to flip the event |
| 2331 | * values when we flip the contexts. |
| 2332 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2333 | value = local64_read(&next_event->count); |
| 2334 | value = local64_xchg(&event->count, value); |
| 2335 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2336 | |
| 2337 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2338 | swap(event->total_time_running, next_event->total_time_running); |
| 2339 | |
| 2340 | /* |
| 2341 | * Since we swizzled the values, update the user visible data too. |
| 2342 | */ |
| 2343 | perf_event_update_userpage(event); |
| 2344 | perf_event_update_userpage(next_event); |
| 2345 | } |
| 2346 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2347 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2348 | struct perf_event_context *next_ctx) |
| 2349 | { |
| 2350 | struct perf_event *event, *next_event; |
| 2351 | |
| 2352 | if (!ctx->nr_stat) |
| 2353 | return; |
| 2354 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2355 | update_context_time(ctx); |
| 2356 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2357 | event = list_first_entry(&ctx->event_list, |
| 2358 | struct perf_event, event_entry); |
| 2359 | |
| 2360 | next_event = list_first_entry(&next_ctx->event_list, |
| 2361 | struct perf_event, event_entry); |
| 2362 | |
| 2363 | while (&event->event_entry != &ctx->event_list && |
| 2364 | &next_event->event_entry != &next_ctx->event_list) { |
| 2365 | |
| 2366 | __perf_event_sync_stat(event, next_event); |
| 2367 | |
| 2368 | event = list_next_entry(event, event_entry); |
| 2369 | next_event = list_next_entry(next_event, event_entry); |
| 2370 | } |
| 2371 | } |
| 2372 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2373 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2374 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2375 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2376 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2377 | struct perf_event_context *next_ctx; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2378 | struct perf_event_context *parent, *next_parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2379 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2380 | int do_switch = 1; |
| 2381 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2382 | if (likely(!ctx)) |
| 2383 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2384 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2385 | cpuctx = __get_cpu_context(ctx); |
| 2386 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2387 | return; |
| 2388 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2389 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2390 | next_ctx = next->perf_event_ctxp[ctxn]; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2391 | if (!next_ctx) |
| 2392 | goto unlock; |
| 2393 | |
| 2394 | parent = rcu_dereference(ctx->parent_ctx); |
| 2395 | next_parent = rcu_dereference(next_ctx->parent_ctx); |
| 2396 | |
| 2397 | /* If neither context have a parent context; they cannot be clones. */ |
Jiri Olsa | 802c8a6 | 2014-09-12 13:18:28 +0200 | [diff] [blame] | 2398 | if (!parent && !next_parent) |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2399 | goto unlock; |
| 2400 | |
| 2401 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2402 | /* |
| 2403 | * Looks like the two contexts are clones, so we might be |
| 2404 | * able to optimize the context switch. We lock both |
| 2405 | * contexts and check that they are clones under the |
| 2406 | * lock (including re-checking that neither has been |
| 2407 | * uncloned in the meantime). It doesn't matter which |
| 2408 | * order we take the locks because no other cpu could |
| 2409 | * be trying to lock both of these tasks. |
| 2410 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2411 | raw_spin_lock(&ctx->lock); |
| 2412 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2413 | if (context_equiv(ctx, next_ctx)) { |
| 2414 | /* |
| 2415 | * XXX do we need a memory barrier of sorts |
| 2416 | * wrt to rcu_dereference() of perf_event_ctxp |
| 2417 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2418 | task->perf_event_ctxp[ctxn] = next_ctx; |
| 2419 | next->perf_event_ctxp[ctxn] = ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2420 | ctx->task = next; |
| 2421 | next_ctx->task = task; |
| 2422 | do_switch = 0; |
| 2423 | |
| 2424 | perf_event_sync_stat(ctx, next_ctx); |
| 2425 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2426 | raw_spin_unlock(&next_ctx->lock); |
| 2427 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2428 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2429 | unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2430 | rcu_read_unlock(); |
| 2431 | |
| 2432 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2433 | raw_spin_lock(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2434 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2435 | cpuctx->task_ctx = NULL; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2436 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2440 | #define for_each_task_context_nr(ctxn) \ |
| 2441 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2442 | |
| 2443 | /* |
| 2444 | * Called from scheduler to remove the events of the current task, |
| 2445 | * with interrupts disabled. |
| 2446 | * |
| 2447 | * We stop each event and update the event value in event->count. |
| 2448 | * |
| 2449 | * This does not protect us against NMI, but disable() |
| 2450 | * sets the disabled bit in the control field of event _before_ |
| 2451 | * accessing the event control register. If a NMI hits, then it will |
| 2452 | * not restart the event. |
| 2453 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2454 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2455 | struct task_struct *next) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2456 | { |
| 2457 | int ctxn; |
| 2458 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2459 | for_each_task_context_nr(ctxn) |
| 2460 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2461 | |
| 2462 | /* |
| 2463 | * if cgroup events exist on this CPU, then we need |
| 2464 | * to check if we have to switch out PMU state. |
| 2465 | * cgroup event are system-wide mode only |
| 2466 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2467 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2468 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2469 | } |
| 2470 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2471 | static void task_ctx_sched_out(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2472 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2473 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2474 | |
| 2475 | if (!cpuctx->task_ctx) |
| 2476 | return; |
| 2477 | |
| 2478 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2479 | return; |
| 2480 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2481 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2482 | cpuctx->task_ctx = NULL; |
| 2483 | } |
| 2484 | |
| 2485 | /* |
| 2486 | * Called with IRQs disabled |
| 2487 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2488 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2489 | enum event_type_t event_type) |
| 2490 | { |
| 2491 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2495 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2496 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2497 | { |
| 2498 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2499 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2500 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2501 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2502 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2503 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2504 | continue; |
| 2505 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2506 | /* may need to reset tstamp_enabled */ |
| 2507 | if (is_cgroup_event(event)) |
| 2508 | perf_cgroup_mark_enabled(event, ctx); |
| 2509 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2510 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2511 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2512 | |
| 2513 | /* |
| 2514 | * If this pinned group hasn't been scheduled, |
| 2515 | * put it in error state. |
| 2516 | */ |
| 2517 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2518 | update_group_times(event); |
| 2519 | event->state = PERF_EVENT_STATE_ERROR; |
| 2520 | } |
| 2521 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | static void |
| 2525 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2526 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2527 | { |
| 2528 | struct perf_event *event; |
| 2529 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2530 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2531 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2532 | /* Ignore events in OFF or ERROR state */ |
| 2533 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2534 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2535 | /* |
| 2536 | * Listen to the 'cpu' scheduling filter constraint |
| 2537 | * of events: |
| 2538 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2539 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2540 | continue; |
| 2541 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2542 | /* may need to reset tstamp_enabled */ |
| 2543 | if (is_cgroup_event(event)) |
| 2544 | perf_cgroup_mark_enabled(event, ctx); |
| 2545 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2546 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2547 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2548 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2549 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2550 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | static void |
| 2554 | ctx_sched_in(struct perf_event_context *ctx, |
| 2555 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2556 | enum event_type_t event_type, |
| 2557 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2558 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2559 | u64 now; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2560 | int is_active = ctx->is_active; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2561 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2562 | ctx->is_active |= event_type; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2563 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2564 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2565 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2566 | now = perf_clock(); |
| 2567 | ctx->timestamp = now; |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2568 | perf_cgroup_set_timestamp(task, ctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2569 | /* |
| 2570 | * First go through the list and put on any pinned groups |
| 2571 | * in order to give them the best chance of going on. |
| 2572 | */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2573 | if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2574 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2575 | |
| 2576 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2577 | if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2578 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2579 | } |
| 2580 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2581 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2582 | enum event_type_t event_type, |
| 2583 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2584 | { |
| 2585 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2586 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2587 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2588 | } |
| 2589 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2590 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2591 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2592 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2593 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2594 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2595 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2596 | if (cpuctx->task_ctx == ctx) |
| 2597 | return; |
| 2598 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2599 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2600 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2601 | /* |
| 2602 | * We want to keep the following priority order: |
| 2603 | * cpu pinned (that don't need to move), task pinned, |
| 2604 | * cpu flexible, task flexible. |
| 2605 | */ |
| 2606 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2607 | |
Gleb Natapov | 1d5f003 | 2011-10-23 19:10:33 +0200 | [diff] [blame] | 2608 | if (ctx->nr_events) |
| 2609 | cpuctx->task_ctx = ctx; |
eranian@google.com | 9b33fa6 | 2010-03-10 22:26:05 -0800 | [diff] [blame] | 2610 | |
Gleb Natapov | 86b47c2 | 2011-11-22 16:08:21 +0200 | [diff] [blame] | 2611 | perf_event_sched_in(cpuctx, cpuctx->task_ctx, task); |
| 2612 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2613 | perf_pmu_enable(ctx->pmu); |
| 2614 | perf_ctx_unlock(cpuctx, ctx); |
| 2615 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2616 | /* |
| 2617 | * Since these rotations are per-cpu, we need to ensure the |
| 2618 | * cpu-context we got scheduled on is actually rotating. |
| 2619 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2620 | perf_pmu_rotate_start(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2621 | } |
| 2622 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2623 | /* |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2624 | * When sampling the branck stack in system-wide, it may be necessary |
| 2625 | * to flush the stack on context switch. This happens when the branch |
| 2626 | * stack does not tag its entries with the pid of the current task. |
| 2627 | * Otherwise it becomes impossible to associate a branch entry with a |
| 2628 | * task. This ambiguity is more likely to appear when the branch stack |
| 2629 | * supports priv level filtering and the user sets it to monitor only |
| 2630 | * at the user level (which could be a useful measurement in system-wide |
| 2631 | * mode). In that case, the risk is high of having a branch stack with |
| 2632 | * branch from multiple tasks. Flushing may mean dropping the existing |
| 2633 | * entries or stashing them somewhere in the PMU specific code layer. |
| 2634 | * |
| 2635 | * This function provides the context switch callback to the lower code |
| 2636 | * layer. It is invoked ONLY when there is at least one system-wide context |
| 2637 | * with at least one active event using taken branch sampling. |
| 2638 | */ |
| 2639 | static void perf_branch_stack_sched_in(struct task_struct *prev, |
| 2640 | struct task_struct *task) |
| 2641 | { |
| 2642 | struct perf_cpu_context *cpuctx; |
| 2643 | struct pmu *pmu; |
| 2644 | unsigned long flags; |
| 2645 | |
| 2646 | /* no need to flush branch stack if not changing task */ |
| 2647 | if (prev == task) |
| 2648 | return; |
| 2649 | |
| 2650 | local_irq_save(flags); |
| 2651 | |
| 2652 | rcu_read_lock(); |
| 2653 | |
| 2654 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 2655 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2656 | |
| 2657 | /* |
| 2658 | * check if the context has at least one |
| 2659 | * event using PERF_SAMPLE_BRANCH_STACK |
| 2660 | */ |
| 2661 | if (cpuctx->ctx.nr_branch_stack > 0 |
| 2662 | && pmu->flush_branch_stack) { |
| 2663 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2664 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2665 | |
| 2666 | perf_pmu_disable(pmu); |
| 2667 | |
| 2668 | pmu->flush_branch_stack(); |
| 2669 | |
| 2670 | perf_pmu_enable(pmu); |
| 2671 | |
| 2672 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | rcu_read_unlock(); |
| 2677 | |
| 2678 | local_irq_restore(flags); |
| 2679 | } |
| 2680 | |
| 2681 | /* |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2682 | * Called from scheduler to add the events of the current task |
| 2683 | * with interrupts disabled. |
| 2684 | * |
| 2685 | * We restore the event value and then enable it. |
| 2686 | * |
| 2687 | * This does not protect us against NMI, but enable() |
| 2688 | * sets the enabled bit in the control field of event _before_ |
| 2689 | * accessing the event control register. If a NMI hits, then it will |
| 2690 | * keep the event running. |
| 2691 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2692 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 2693 | struct task_struct *task) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2694 | { |
| 2695 | struct perf_event_context *ctx; |
| 2696 | int ctxn; |
| 2697 | |
| 2698 | for_each_task_context_nr(ctxn) { |
| 2699 | ctx = task->perf_event_ctxp[ctxn]; |
| 2700 | if (likely(!ctx)) |
| 2701 | continue; |
| 2702 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2703 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2704 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2705 | /* |
| 2706 | * if cgroup events exist on this CPU, then we need |
| 2707 | * to check if we have to switch in PMU state. |
| 2708 | * cgroup event are system-wide mode only |
| 2709 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2710 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2711 | perf_cgroup_sched_in(prev, task); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2712 | |
| 2713 | /* check for system-wide branch_stack events */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2714 | if (atomic_read(this_cpu_ptr(&perf_branch_stack_events))) |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2715 | perf_branch_stack_sched_in(prev, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2718 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 2719 | { |
| 2720 | u64 frequency = event->attr.sample_freq; |
| 2721 | u64 sec = NSEC_PER_SEC; |
| 2722 | u64 divisor, dividend; |
| 2723 | |
| 2724 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 2725 | |
| 2726 | count_fls = fls64(count); |
| 2727 | nsec_fls = fls64(nsec); |
| 2728 | frequency_fls = fls64(frequency); |
| 2729 | sec_fls = 30; |
| 2730 | |
| 2731 | /* |
| 2732 | * We got @count in @nsec, with a target of sample_freq HZ |
| 2733 | * the target period becomes: |
| 2734 | * |
| 2735 | * @count * 10^9 |
| 2736 | * period = ------------------- |
| 2737 | * @nsec * sample_freq |
| 2738 | * |
| 2739 | */ |
| 2740 | |
| 2741 | /* |
| 2742 | * Reduce accuracy by one bit such that @a and @b converge |
| 2743 | * to a similar magnitude. |
| 2744 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2745 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2746 | do { \ |
| 2747 | if (a##_fls > b##_fls) { \ |
| 2748 | a >>= 1; \ |
| 2749 | a##_fls--; \ |
| 2750 | } else { \ |
| 2751 | b >>= 1; \ |
| 2752 | b##_fls--; \ |
| 2753 | } \ |
| 2754 | } while (0) |
| 2755 | |
| 2756 | /* |
| 2757 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 2758 | * the other, so that finally we can do a u64/u64 division. |
| 2759 | */ |
| 2760 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 2761 | REDUCE_FLS(nsec, frequency); |
| 2762 | REDUCE_FLS(sec, count); |
| 2763 | } |
| 2764 | |
| 2765 | if (count_fls + sec_fls > 64) { |
| 2766 | divisor = nsec * frequency; |
| 2767 | |
| 2768 | while (count_fls + sec_fls > 64) { |
| 2769 | REDUCE_FLS(count, sec); |
| 2770 | divisor >>= 1; |
| 2771 | } |
| 2772 | |
| 2773 | dividend = count * sec; |
| 2774 | } else { |
| 2775 | dividend = count * sec; |
| 2776 | |
| 2777 | while (nsec_fls + frequency_fls > 64) { |
| 2778 | REDUCE_FLS(nsec, frequency); |
| 2779 | dividend >>= 1; |
| 2780 | } |
| 2781 | |
| 2782 | divisor = nsec * frequency; |
| 2783 | } |
| 2784 | |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2785 | if (!divisor) |
| 2786 | return dividend; |
| 2787 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2788 | return div64_u64(dividend, divisor); |
| 2789 | } |
| 2790 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2791 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 2792 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 2793 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2794 | 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] | 2795 | { |
| 2796 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2797 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2798 | s64 delta; |
| 2799 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2800 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2801 | |
| 2802 | delta = (s64)(period - hwc->sample_period); |
| 2803 | delta = (delta + 7) / 8; /* low pass filter */ |
| 2804 | |
| 2805 | sample_period = hwc->sample_period + delta; |
| 2806 | |
| 2807 | if (!sample_period) |
| 2808 | sample_period = 1; |
| 2809 | |
| 2810 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2811 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2812 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2813 | if (disable) |
| 2814 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2815 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2816 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2817 | |
| 2818 | if (disable) |
| 2819 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2820 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2821 | } |
| 2822 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2823 | /* |
| 2824 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 2825 | * events. At the same time, make sure, having freq events does not change |
| 2826 | * the rate of unthrottling as that would introduce bias. |
| 2827 | */ |
| 2828 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 2829 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2830 | { |
| 2831 | struct perf_event *event; |
| 2832 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2833 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2834 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2835 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2836 | /* |
| 2837 | * only need to iterate over all events iff: |
| 2838 | * - context have events in frequency mode (needs freq adjust) |
| 2839 | * - there are events to unthrottle on this cpu |
| 2840 | */ |
| 2841 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2842 | return; |
| 2843 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2844 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2845 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2846 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 2847 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2848 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 2849 | continue; |
| 2850 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2851 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 2852 | continue; |
| 2853 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2854 | perf_pmu_disable(event->pmu); |
| 2855 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2856 | hwc = &event->hw; |
| 2857 | |
Jiri Olsa | ae23bff | 2013-08-24 16:45:54 +0200 | [diff] [blame] | 2858 | if (hwc->interrupts == MAX_INTERRUPTS) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2859 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2860 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2861 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | if (!event->attr.freq || !event->attr.sample_freq) |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2865 | goto next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2866 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2867 | /* |
| 2868 | * stop the event and update event->count |
| 2869 | */ |
| 2870 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2871 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2872 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2873 | delta = now - hwc->freq_count_stamp; |
| 2874 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2875 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2876 | /* |
| 2877 | * restart the event |
| 2878 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2879 | * we have stopped the event so tell that |
| 2880 | * to perf_adjust_period() to avoid stopping it |
| 2881 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2882 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2883 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2884 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2885 | |
| 2886 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2887 | next: |
| 2888 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2889 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2890 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2891 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2892 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | /* |
| 2896 | * Round-robin a context's events: |
| 2897 | */ |
| 2898 | static void rotate_ctx(struct perf_event_context *ctx) |
| 2899 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 2900 | /* |
| 2901 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 2902 | * disabled by the inheritance code. |
| 2903 | */ |
| 2904 | if (!ctx->rotate_disable) |
| 2905 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2906 | } |
| 2907 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2908 | /* |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2909 | * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized |
| 2910 | * because they're strictly cpu affine and rotate_start is called with IRQs |
| 2911 | * disabled, while rotate_context is called from IRQ context. |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2912 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2913 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2914 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2915 | struct perf_event_context *ctx = NULL; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2916 | int rotate = 0, remove = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2917 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2918 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2919 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2920 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 2921 | rotate = 1; |
| 2922 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2923 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2924 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2925 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2926 | remove = 0; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2927 | if (ctx->nr_events != ctx->nr_active) |
| 2928 | rotate = 1; |
| 2929 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2930 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2931 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2932 | goto done; |
| 2933 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2934 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2935 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2936 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2937 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2938 | if (ctx) |
| 2939 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 2940 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2941 | rotate_ctx(&cpuctx->ctx); |
| 2942 | if (ctx) |
| 2943 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2944 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2945 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2946 | |
| 2947 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 2948 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 2949 | done: |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2950 | if (remove) |
| 2951 | list_del_init(&cpuctx->rotation_list); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2952 | |
| 2953 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2954 | } |
| 2955 | |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2956 | #ifdef CONFIG_NO_HZ_FULL |
| 2957 | bool perf_event_can_stop_tick(void) |
| 2958 | { |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 2959 | if (atomic_read(&nr_freq_events) || |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 2960 | __this_cpu_read(perf_throttled_count)) |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2961 | return false; |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 2962 | else |
| 2963 | return true; |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 2964 | } |
| 2965 | #endif |
| 2966 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2967 | void perf_event_task_tick(void) |
| 2968 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2969 | struct list_head *head = this_cpu_ptr(&rotation_list); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2970 | struct perf_cpu_context *cpuctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2971 | struct perf_event_context *ctx; |
| 2972 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2973 | |
| 2974 | WARN_ON(!irqs_disabled()); |
| 2975 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2976 | __this_cpu_inc(perf_throttled_seq); |
| 2977 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
| 2978 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2979 | list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2980 | ctx = &cpuctx->ctx; |
| 2981 | perf_adjust_freq_unthr_context(ctx, throttled); |
| 2982 | |
| 2983 | ctx = cpuctx->task_ctx; |
| 2984 | if (ctx) |
| 2985 | perf_adjust_freq_unthr_context(ctx, throttled); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 2986 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2987 | } |
| 2988 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2989 | static int event_enable_on_exec(struct perf_event *event, |
| 2990 | struct perf_event_context *ctx) |
| 2991 | { |
| 2992 | if (!event->attr.enable_on_exec) |
| 2993 | return 0; |
| 2994 | |
| 2995 | event->attr.enable_on_exec = 0; |
| 2996 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2997 | return 0; |
| 2998 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2999 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3000 | |
| 3001 | return 1; |
| 3002 | } |
| 3003 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3004 | /* |
| 3005 | * Enable all of a task's events that have been marked enable-on-exec. |
| 3006 | * This expects task == current. |
| 3007 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3008 | static void perf_event_enable_on_exec(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3009 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3010 | struct perf_event_context *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3011 | struct perf_event *event; |
| 3012 | unsigned long flags; |
| 3013 | int enabled = 0; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3014 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3015 | |
| 3016 | local_irq_save(flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3017 | if (!ctx || !ctx->nr_events) |
| 3018 | goto out; |
| 3019 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 3020 | /* |
| 3021 | * We must ctxsw out cgroup events to avoid conflict |
| 3022 | * when invoking perf_task_event_sched_in() later on |
| 3023 | * in this function. Otherwise we end up trying to |
| 3024 | * ctxswin cgroup events which are already scheduled |
| 3025 | * in. |
| 3026 | */ |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 3027 | perf_cgroup_sched_out(current, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3028 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3029 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 3030 | task_ctx_sched_out(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3031 | |
Peter Zijlstra | b79387e | 2011-11-22 11:25:43 +0100 | [diff] [blame] | 3032 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3033 | ret = event_enable_on_exec(event, ctx); |
| 3034 | if (ret) |
| 3035 | enabled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | /* |
| 3039 | * Unclone this context if we enabled any event. |
| 3040 | */ |
| 3041 | if (enabled) |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3042 | clone_ctx = unclone_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3043 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3044 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3045 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 3046 | /* |
| 3047 | * Also calls ctxswin for cgroup events, if any: |
| 3048 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3049 | perf_event_context_sched_in(ctx, ctx->task); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3050 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3051 | local_irq_restore(flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3052 | |
| 3053 | if (clone_ctx) |
| 3054 | put_ctx(clone_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3055 | } |
| 3056 | |
Peter Zijlstra | e041e32 | 2014-05-21 17:32:19 +0200 | [diff] [blame] | 3057 | void perf_event_exec(void) |
| 3058 | { |
| 3059 | struct perf_event_context *ctx; |
| 3060 | int ctxn; |
| 3061 | |
| 3062 | rcu_read_lock(); |
| 3063 | for_each_task_context_nr(ctxn) { |
| 3064 | ctx = current->perf_event_ctxp[ctxn]; |
| 3065 | if (!ctx) |
| 3066 | continue; |
| 3067 | |
| 3068 | perf_event_enable_on_exec(ctx); |
| 3069 | } |
| 3070 | rcu_read_unlock(); |
| 3071 | } |
| 3072 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3073 | /* |
| 3074 | * Cross CPU call to read the hardware event |
| 3075 | */ |
| 3076 | static void __perf_event_read(void *info) |
| 3077 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3078 | struct perf_event *event = info; |
| 3079 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3080 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3081 | |
| 3082 | /* |
| 3083 | * If this is a task context, we need to check whether it is |
| 3084 | * the current task context of this cpu. If not it has been |
| 3085 | * scheduled out before the smp call arrived. In that case |
| 3086 | * event->count would have been updated to a recent sample |
| 3087 | * when the event was scheduled out. |
| 3088 | */ |
| 3089 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 3090 | return; |
| 3091 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3092 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3093 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3094 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3095 | update_cgrp_time_from_event(event); |
| 3096 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3097 | update_event_times(event); |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3098 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
| 3099 | event->pmu->read(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3100 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3101 | } |
| 3102 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3103 | static inline u64 perf_event_count(struct perf_event *event) |
| 3104 | { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3105 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3106 | } |
| 3107 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3108 | static u64 perf_event_read(struct perf_event *event) |
| 3109 | { |
| 3110 | /* |
| 3111 | * If event is enabled and currently active on a CPU, update the |
| 3112 | * value in the event structure: |
| 3113 | */ |
| 3114 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
| 3115 | smp_call_function_single(event->oncpu, |
| 3116 | __perf_event_read, event, 1); |
| 3117 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 3118 | struct perf_event_context *ctx = event->ctx; |
| 3119 | unsigned long flags; |
| 3120 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3121 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3122 | /* |
| 3123 | * may read while context is not active |
| 3124 | * (e.g., thread is blocked), in that case |
| 3125 | * we cannot update context time |
| 3126 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3127 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3128 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3129 | update_cgrp_time_from_event(event); |
| 3130 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3131 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3132 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3133 | } |
| 3134 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3135 | return perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | /* |
| 3139 | * Initialize the perf_event context in a task_struct: |
| 3140 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3141 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3142 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3143 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3144 | mutex_init(&ctx->mutex); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3145 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 3146 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3147 | INIT_LIST_HEAD(&ctx->event_list); |
| 3148 | atomic_set(&ctx->refcount, 1); |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 3149 | INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3150 | } |
| 3151 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3152 | static struct perf_event_context * |
| 3153 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3154 | { |
| 3155 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3156 | |
| 3157 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 3158 | if (!ctx) |
| 3159 | return NULL; |
| 3160 | |
| 3161 | __perf_event_init_context(ctx); |
| 3162 | if (task) { |
| 3163 | ctx->task = task; |
| 3164 | get_task_struct(task); |
| 3165 | } |
| 3166 | ctx->pmu = pmu; |
| 3167 | |
| 3168 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3169 | } |
| 3170 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3171 | static struct task_struct * |
| 3172 | find_lively_task_by_vpid(pid_t vpid) |
| 3173 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3174 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3175 | int err; |
| 3176 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3177 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3178 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3179 | task = current; |
| 3180 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3181 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3182 | if (task) |
| 3183 | get_task_struct(task); |
| 3184 | rcu_read_unlock(); |
| 3185 | |
| 3186 | if (!task) |
| 3187 | return ERR_PTR(-ESRCH); |
| 3188 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3189 | /* Reuse ptrace permission checks for now. */ |
| 3190 | err = -EACCES; |
| 3191 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 3192 | goto errout; |
| 3193 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3194 | return task; |
| 3195 | errout: |
| 3196 | put_task_struct(task); |
| 3197 | return ERR_PTR(err); |
| 3198 | |
| 3199 | } |
| 3200 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3201 | /* |
| 3202 | * Returns a matching context with refcount and pincount. |
| 3203 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3204 | static struct perf_event_context * |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 3205 | find_get_context(struct pmu *pmu, struct task_struct *task, int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3206 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3207 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3208 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3209 | unsigned long flags; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3210 | int ctxn, err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3211 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3212 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3213 | /* Must be root to operate on a CPU event: */ |
| 3214 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3215 | return ERR_PTR(-EACCES); |
| 3216 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3217 | /* |
| 3218 | * We could be clever and allow to attach a event to an |
| 3219 | * offline CPU and activate it when the CPU comes up, but |
| 3220 | * that's for later. |
| 3221 | */ |
| 3222 | if (!cpu_online(cpu)) |
| 3223 | return ERR_PTR(-ENODEV); |
| 3224 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3225 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3226 | ctx = &cpuctx->ctx; |
| 3227 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3228 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3229 | |
| 3230 | return ctx; |
| 3231 | } |
| 3232 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3233 | err = -EINVAL; |
| 3234 | ctxn = pmu->task_ctx_nr; |
| 3235 | if (ctxn < 0) |
| 3236 | goto errout; |
| 3237 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3238 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3239 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3240 | if (ctx) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3241 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3242 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3243 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3244 | |
| 3245 | if (clone_ctx) |
| 3246 | put_ctx(clone_ctx); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3247 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3248 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3249 | err = -ENOMEM; |
| 3250 | if (!ctx) |
| 3251 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3252 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3253 | err = 0; |
| 3254 | mutex_lock(&task->perf_event_mutex); |
| 3255 | /* |
| 3256 | * If it has already passed perf_event_exit_task(). |
| 3257 | * we must see PF_EXITING, it takes this mutex too. |
| 3258 | */ |
| 3259 | if (task->flags & PF_EXITING) |
| 3260 | err = -ESRCH; |
| 3261 | else if (task->perf_event_ctxp[ctxn]) |
| 3262 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3263 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3264 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3265 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3266 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3267 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3268 | mutex_unlock(&task->perf_event_mutex); |
| 3269 | |
| 3270 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3271 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3272 | |
| 3273 | if (err == -EAGAIN) |
| 3274 | goto retry; |
| 3275 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3276 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3277 | } |
| 3278 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3279 | return ctx; |
| 3280 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3281 | errout: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3282 | return ERR_PTR(err); |
| 3283 | } |
| 3284 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3285 | static void perf_event_free_filter(struct perf_event *event); |
| 3286 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3287 | static void free_event_rcu(struct rcu_head *head) |
| 3288 | { |
| 3289 | struct perf_event *event; |
| 3290 | |
| 3291 | event = container_of(head, struct perf_event, rcu_head); |
| 3292 | if (event->ns) |
| 3293 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3294 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3295 | kfree(event); |
| 3296 | } |
| 3297 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3298 | static void ring_buffer_put(struct ring_buffer *rb); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3299 | static void ring_buffer_attach(struct perf_event *event, |
| 3300 | struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3301 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3302 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
| 3303 | { |
| 3304 | if (event->parent) |
| 3305 | return; |
| 3306 | |
| 3307 | if (has_branch_stack(event)) { |
| 3308 | if (!(event->attach_state & PERF_ATTACH_TASK)) |
| 3309 | atomic_dec(&per_cpu(perf_branch_stack_events, cpu)); |
| 3310 | } |
| 3311 | if (is_cgroup_event(event)) |
| 3312 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); |
| 3313 | } |
| 3314 | |
| 3315 | static void unaccount_event(struct perf_event *event) |
| 3316 | { |
| 3317 | if (event->parent) |
| 3318 | return; |
| 3319 | |
| 3320 | if (event->attach_state & PERF_ATTACH_TASK) |
| 3321 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3322 | if (event->attr.mmap || event->attr.mmap_data) |
| 3323 | atomic_dec(&nr_mmap_events); |
| 3324 | if (event->attr.comm) |
| 3325 | atomic_dec(&nr_comm_events); |
| 3326 | if (event->attr.task) |
| 3327 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3328 | if (event->attr.freq) |
| 3329 | atomic_dec(&nr_freq_events); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3330 | if (is_cgroup_event(event)) |
| 3331 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3332 | if (has_branch_stack(event)) |
| 3333 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3334 | |
| 3335 | unaccount_event_cpu(event, event->cpu); |
| 3336 | } |
| 3337 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3338 | static void __free_event(struct perf_event *event) |
| 3339 | { |
| 3340 | if (!event->parent) { |
| 3341 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 3342 | put_callchain_buffers(); |
| 3343 | } |
| 3344 | |
| 3345 | if (event->destroy) |
| 3346 | event->destroy(event); |
| 3347 | |
| 3348 | if (event->ctx) |
| 3349 | put_ctx(event->ctx); |
| 3350 | |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 3351 | if (event->pmu) |
| 3352 | module_put(event->pmu->module); |
| 3353 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3354 | call_rcu(&event->rcu_head, free_event_rcu); |
| 3355 | } |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3356 | |
| 3357 | static void _free_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3358 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3359 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3360 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3361 | unaccount_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3362 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3363 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3364 | /* |
| 3365 | * Can happen when we close an event with re-directed output. |
| 3366 | * |
| 3367 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 3368 | * over us; possibly making our ring_buffer_put() the last. |
| 3369 | */ |
| 3370 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3371 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3372 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3373 | } |
| 3374 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3375 | if (is_cgroup_event(event)) |
| 3376 | perf_detach_cgroup(event); |
| 3377 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3378 | __free_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3379 | } |
| 3380 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3381 | /* |
| 3382 | * Used to free events which have a known refcount of 1, such as in error paths |
| 3383 | * where the event isn't exposed yet and inherited events. |
| 3384 | */ |
| 3385 | static void free_event(struct perf_event *event) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3386 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3387 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
| 3388 | "unexpected event refcount: %ld; ptr=%p\n", |
| 3389 | atomic_long_read(&event->refcount), event)) { |
| 3390 | /* leak to avoid use-after-free */ |
| 3391 | return; |
| 3392 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3393 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3394 | _free_event(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3395 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3396 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3397 | /* |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3398 | * Remove user event from the owner task. |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3399 | */ |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3400 | static void perf_remove_from_owner(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3401 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3402 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3403 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3404 | rcu_read_lock(); |
| 3405 | owner = ACCESS_ONCE(event->owner); |
| 3406 | /* |
| 3407 | * Matches the smp_wmb() in perf_event_exit_task(). If we observe |
| 3408 | * !owner it means the list deletion is complete and we can indeed |
| 3409 | * free this event, otherwise we need to serialize on |
| 3410 | * owner->perf_event_mutex. |
| 3411 | */ |
| 3412 | smp_read_barrier_depends(); |
| 3413 | if (owner) { |
| 3414 | /* |
| 3415 | * Since delayed_put_task_struct() also drops the last |
| 3416 | * task reference we can safely take a new reference |
| 3417 | * while holding the rcu_read_lock(). |
| 3418 | */ |
| 3419 | get_task_struct(owner); |
| 3420 | } |
| 3421 | rcu_read_unlock(); |
| 3422 | |
| 3423 | if (owner) { |
| 3424 | mutex_lock(&owner->perf_event_mutex); |
| 3425 | /* |
| 3426 | * We have to re-check the event->owner field, if it is cleared |
| 3427 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 3428 | * ensured they're done, and we can proceed with freeing the |
| 3429 | * event. |
| 3430 | */ |
| 3431 | if (event->owner) |
| 3432 | list_del_init(&event->owner_entry); |
| 3433 | mutex_unlock(&owner->perf_event_mutex); |
| 3434 | put_task_struct(owner); |
| 3435 | } |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* |
| 3439 | * Called when the last reference to the file is gone. |
| 3440 | */ |
| 3441 | static void put_event(struct perf_event *event) |
| 3442 | { |
| 3443 | struct perf_event_context *ctx = event->ctx; |
| 3444 | |
| 3445 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 3446 | return; |
| 3447 | |
| 3448 | if (!is_kernel_event(event)) |
| 3449 | perf_remove_from_owner(event); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3450 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3451 | WARN_ON_ONCE(ctx->parent_ctx); |
| 3452 | /* |
| 3453 | * There are two ways this annotation is useful: |
| 3454 | * |
| 3455 | * 1) there is a lock recursion from perf_event_exit_task |
| 3456 | * see the comment there. |
| 3457 | * |
| 3458 | * 2) there is a lock-inversion with mmap_sem through |
| 3459 | * perf_event_read_group(), which takes faults while |
| 3460 | * holding ctx->mutex, however this is called after |
| 3461 | * the last filedesc died, so there is no possibility |
| 3462 | * to trigger the AB-BA case. |
| 3463 | */ |
| 3464 | mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING); |
| 3465 | perf_remove_from_context(event, true); |
| 3466 | mutex_unlock(&ctx->mutex); |
| 3467 | |
| 3468 | _free_event(event); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3469 | } |
| 3470 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3471 | int perf_event_release_kernel(struct perf_event *event) |
| 3472 | { |
| 3473 | put_event(event); |
| 3474 | return 0; |
| 3475 | } |
| 3476 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 3477 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3478 | static int perf_release(struct inode *inode, struct file *file) |
| 3479 | { |
| 3480 | put_event(file->private_data); |
| 3481 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3482 | } |
| 3483 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 3484 | /* |
| 3485 | * Remove all orphanes events from the context. |
| 3486 | */ |
| 3487 | static void orphans_remove_work(struct work_struct *work) |
| 3488 | { |
| 3489 | struct perf_event_context *ctx; |
| 3490 | struct perf_event *event, *tmp; |
| 3491 | |
| 3492 | ctx = container_of(work, struct perf_event_context, |
| 3493 | orphans_remove.work); |
| 3494 | |
| 3495 | mutex_lock(&ctx->mutex); |
| 3496 | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) { |
| 3497 | struct perf_event *parent_event = event->parent; |
| 3498 | |
| 3499 | if (!is_orphaned_child(event)) |
| 3500 | continue; |
| 3501 | |
| 3502 | perf_remove_from_context(event, true); |
| 3503 | |
| 3504 | mutex_lock(&parent_event->child_mutex); |
| 3505 | list_del_init(&event->child_list); |
| 3506 | mutex_unlock(&parent_event->child_mutex); |
| 3507 | |
| 3508 | free_event(event); |
| 3509 | put_event(parent_event); |
| 3510 | } |
| 3511 | |
| 3512 | raw_spin_lock_irq(&ctx->lock); |
| 3513 | ctx->orphans_remove_sched = false; |
| 3514 | raw_spin_unlock_irq(&ctx->lock); |
| 3515 | mutex_unlock(&ctx->mutex); |
| 3516 | |
| 3517 | put_ctx(ctx); |
| 3518 | } |
| 3519 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3520 | 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] | 3521 | { |
| 3522 | struct perf_event *child; |
| 3523 | u64 total = 0; |
| 3524 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3525 | *enabled = 0; |
| 3526 | *running = 0; |
| 3527 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3528 | mutex_lock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3529 | total += perf_event_read(event); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3530 | *enabled += event->total_time_enabled + |
| 3531 | atomic64_read(&event->child_total_time_enabled); |
| 3532 | *running += event->total_time_running + |
| 3533 | atomic64_read(&event->child_total_time_running); |
| 3534 | |
| 3535 | list_for_each_entry(child, &event->child_list, child_list) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3536 | total += perf_event_read(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3537 | *enabled += child->total_time_enabled; |
| 3538 | *running += child->total_time_running; |
| 3539 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3540 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3541 | |
| 3542 | return total; |
| 3543 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3544 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3545 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3546 | static int perf_event_read_group(struct perf_event *event, |
| 3547 | u64 read_format, char __user *buf) |
| 3548 | { |
| 3549 | struct perf_event *leader = event->group_leader, *sub; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3550 | int n = 0, size = 0, ret = -EFAULT; |
| 3551 | struct perf_event_context *ctx = leader->ctx; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3552 | u64 values[5]; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3553 | u64 count, enabled, running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3554 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3555 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3556 | count = perf_event_read_value(leader, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3557 | |
| 3558 | values[n++] = 1 + leader->nr_siblings; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3559 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3560 | values[n++] = enabled; |
| 3561 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3562 | values[n++] = running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3563 | values[n++] = count; |
| 3564 | if (read_format & PERF_FORMAT_ID) |
| 3565 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3566 | |
| 3567 | size = n * sizeof(u64); |
| 3568 | |
| 3569 | if (copy_to_user(buf, values, size)) |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3570 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3571 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3572 | ret = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3573 | |
| 3574 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3575 | n = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3576 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3577 | values[n++] = perf_event_read_value(sub, &enabled, &running); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3578 | if (read_format & PERF_FORMAT_ID) |
| 3579 | values[n++] = primary_event_id(sub); |
| 3580 | |
| 3581 | size = n * sizeof(u64); |
| 3582 | |
Stephane Eranian | 184d3da | 2009-11-23 21:40:49 -0800 | [diff] [blame] | 3583 | if (copy_to_user(buf + ret, values, size)) { |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3584 | ret = -EFAULT; |
| 3585 | goto unlock; |
| 3586 | } |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3587 | |
| 3588 | ret += size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3589 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3590 | unlock: |
| 3591 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3592 | |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3593 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | static int perf_event_read_one(struct perf_event *event, |
| 3597 | u64 read_format, char __user *buf) |
| 3598 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3599 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3600 | u64 values[4]; |
| 3601 | int n = 0; |
| 3602 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3603 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 3604 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3605 | values[n++] = enabled; |
| 3606 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3607 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3608 | if (read_format & PERF_FORMAT_ID) |
| 3609 | values[n++] = primary_event_id(event); |
| 3610 | |
| 3611 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 3612 | return -EFAULT; |
| 3613 | |
| 3614 | return n * sizeof(u64); |
| 3615 | } |
| 3616 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 3617 | static bool is_event_hup(struct perf_event *event) |
| 3618 | { |
| 3619 | bool no_children; |
| 3620 | |
| 3621 | if (event->state != PERF_EVENT_STATE_EXIT) |
| 3622 | return false; |
| 3623 | |
| 3624 | mutex_lock(&event->child_mutex); |
| 3625 | no_children = list_empty(&event->child_list); |
| 3626 | mutex_unlock(&event->child_mutex); |
| 3627 | return no_children; |
| 3628 | } |
| 3629 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3630 | /* |
| 3631 | * Read the performance event - simple non blocking version for now |
| 3632 | */ |
| 3633 | static ssize_t |
| 3634 | perf_read_hw(struct perf_event *event, char __user *buf, size_t count) |
| 3635 | { |
| 3636 | u64 read_format = event->attr.read_format; |
| 3637 | int ret; |
| 3638 | |
| 3639 | /* |
| 3640 | * Return end-of-file for a read on a event that is in |
| 3641 | * error state (i.e. because it was pinned but it couldn't be |
| 3642 | * scheduled on to the CPU at some point). |
| 3643 | */ |
| 3644 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 3645 | return 0; |
| 3646 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 3647 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3648 | return -ENOSPC; |
| 3649 | |
| 3650 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3651 | if (read_format & PERF_FORMAT_GROUP) |
| 3652 | ret = perf_event_read_group(event, read_format, buf); |
| 3653 | else |
| 3654 | ret = perf_event_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3655 | |
| 3656 | return ret; |
| 3657 | } |
| 3658 | |
| 3659 | static ssize_t |
| 3660 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 3661 | { |
| 3662 | struct perf_event *event = file->private_data; |
| 3663 | |
| 3664 | return perf_read_hw(event, buf, count); |
| 3665 | } |
| 3666 | |
| 3667 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 3668 | { |
| 3669 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3670 | struct ring_buffer *rb; |
Jiri Olsa | 61b6768 | 2014-08-13 19:39:56 +0200 | [diff] [blame] | 3671 | unsigned int events = POLLHUP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3672 | |
Sebastian Andrzej Siewior | e708d7a | 2014-08-04 15:31:08 +0200 | [diff] [blame] | 3673 | poll_wait(file, &event->waitq, wait); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 3674 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 3675 | if (is_event_hup(event)) |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 3676 | return events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3677 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3678 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3679 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 3680 | * 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] | 3681 | */ |
| 3682 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3683 | rb = event->rb; |
| 3684 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3685 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3686 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3687 | return events; |
| 3688 | } |
| 3689 | |
| 3690 | static void perf_event_reset(struct perf_event *event) |
| 3691 | { |
| 3692 | (void)perf_event_read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3693 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3694 | perf_event_update_userpage(event); |
| 3695 | } |
| 3696 | |
| 3697 | /* |
| 3698 | * Holding the top-level event's child_mutex means that any |
| 3699 | * descendant process that has inherited this event will block |
| 3700 | * in sync_child_event if it goes to exit, thus satisfying the |
| 3701 | * task existence requirements of perf_event_enable/disable. |
| 3702 | */ |
| 3703 | static void perf_event_for_each_child(struct perf_event *event, |
| 3704 | void (*func)(struct perf_event *)) |
| 3705 | { |
| 3706 | struct perf_event *child; |
| 3707 | |
| 3708 | WARN_ON_ONCE(event->ctx->parent_ctx); |
| 3709 | mutex_lock(&event->child_mutex); |
| 3710 | func(event); |
| 3711 | list_for_each_entry(child, &event->child_list, child_list) |
| 3712 | func(child); |
| 3713 | mutex_unlock(&event->child_mutex); |
| 3714 | } |
| 3715 | |
| 3716 | static void perf_event_for_each(struct perf_event *event, |
| 3717 | void (*func)(struct perf_event *)) |
| 3718 | { |
| 3719 | struct perf_event_context *ctx = event->ctx; |
| 3720 | struct perf_event *sibling; |
| 3721 | |
| 3722 | WARN_ON_ONCE(ctx->parent_ctx); |
| 3723 | mutex_lock(&ctx->mutex); |
| 3724 | event = event->group_leader; |
| 3725 | |
| 3726 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3727 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 3728 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3729 | mutex_unlock(&ctx->mutex); |
| 3730 | } |
| 3731 | |
| 3732 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 3733 | { |
| 3734 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3735 | int ret = 0, active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3736 | u64 value; |
| 3737 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 3738 | if (!is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3739 | return -EINVAL; |
| 3740 | |
John Blackwood | ad0cf34 | 2010-09-28 18:03:11 -0400 | [diff] [blame] | 3741 | if (copy_from_user(&value, arg, sizeof(value))) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3742 | return -EFAULT; |
| 3743 | |
| 3744 | if (!value) |
| 3745 | return -EINVAL; |
| 3746 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3747 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3748 | if (event->attr.freq) { |
| 3749 | if (value > sysctl_perf_event_sample_rate) { |
| 3750 | ret = -EINVAL; |
| 3751 | goto unlock; |
| 3752 | } |
| 3753 | |
| 3754 | event->attr.sample_freq = value; |
| 3755 | } else { |
| 3756 | event->attr.sample_period = value; |
| 3757 | event->hw.sample_period = value; |
| 3758 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3759 | |
| 3760 | active = (event->state == PERF_EVENT_STATE_ACTIVE); |
| 3761 | if (active) { |
| 3762 | perf_pmu_disable(ctx->pmu); |
| 3763 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3764 | } |
| 3765 | |
| 3766 | local64_set(&event->hw.period_left, 0); |
| 3767 | |
| 3768 | if (active) { |
| 3769 | event->pmu->start(event, PERF_EF_RELOAD); |
| 3770 | perf_pmu_enable(ctx->pmu); |
| 3771 | } |
| 3772 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3773 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3774 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3775 | |
| 3776 | return ret; |
| 3777 | } |
| 3778 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3779 | static const struct file_operations perf_fops; |
| 3780 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3781 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3782 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3783 | struct fd f = fdget(fd); |
| 3784 | if (!f.file) |
| 3785 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3786 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3787 | if (f.file->f_op != &perf_fops) { |
| 3788 | fdput(f); |
| 3789 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3790 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3791 | *p = f; |
| 3792 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | static int perf_event_set_output(struct perf_event *event, |
| 3796 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3797 | 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] | 3798 | |
| 3799 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 3800 | { |
| 3801 | struct perf_event *event = file->private_data; |
| 3802 | void (*func)(struct perf_event *); |
| 3803 | u32 flags = arg; |
| 3804 | |
| 3805 | switch (cmd) { |
| 3806 | case PERF_EVENT_IOC_ENABLE: |
| 3807 | func = perf_event_enable; |
| 3808 | break; |
| 3809 | case PERF_EVENT_IOC_DISABLE: |
| 3810 | func = perf_event_disable; |
| 3811 | break; |
| 3812 | case PERF_EVENT_IOC_RESET: |
| 3813 | func = perf_event_reset; |
| 3814 | break; |
| 3815 | |
| 3816 | case PERF_EVENT_IOC_REFRESH: |
| 3817 | return perf_event_refresh(event, arg); |
| 3818 | |
| 3819 | case PERF_EVENT_IOC_PERIOD: |
| 3820 | return perf_event_period(event, (u64 __user *)arg); |
| 3821 | |
Jiri Olsa | cf4957f | 2012-10-24 13:37:58 +0200 | [diff] [blame] | 3822 | case PERF_EVENT_IOC_ID: |
| 3823 | { |
| 3824 | u64 id = primary_event_id(event); |
| 3825 | |
| 3826 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) |
| 3827 | return -EFAULT; |
| 3828 | return 0; |
| 3829 | } |
| 3830 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3831 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3832 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3833 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3834 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3835 | struct perf_event *output_event; |
| 3836 | struct fd output; |
| 3837 | ret = perf_fget_light(arg, &output); |
| 3838 | if (ret) |
| 3839 | return ret; |
| 3840 | output_event = output.file->private_data; |
| 3841 | ret = perf_event_set_output(event, output_event); |
| 3842 | fdput(output); |
| 3843 | } else { |
| 3844 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3845 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3846 | return ret; |
| 3847 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3848 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3849 | case PERF_EVENT_IOC_SET_FILTER: |
| 3850 | return perf_event_set_filter(event, (void __user *)arg); |
| 3851 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3852 | default: |
| 3853 | return -ENOTTY; |
| 3854 | } |
| 3855 | |
| 3856 | if (flags & PERF_IOC_FLAG_GROUP) |
| 3857 | perf_event_for_each(event, func); |
| 3858 | else |
| 3859 | perf_event_for_each_child(event, func); |
| 3860 | |
| 3861 | return 0; |
| 3862 | } |
| 3863 | |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 3864 | #ifdef CONFIG_COMPAT |
| 3865 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, |
| 3866 | unsigned long arg) |
| 3867 | { |
| 3868 | switch (_IOC_NR(cmd)) { |
| 3869 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): |
| 3870 | case _IOC_NR(PERF_EVENT_IOC_ID): |
| 3871 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
| 3872 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { |
| 3873 | cmd &= ~IOCSIZE_MASK; |
| 3874 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; |
| 3875 | } |
| 3876 | break; |
| 3877 | } |
| 3878 | return perf_ioctl(file, cmd, arg); |
| 3879 | } |
| 3880 | #else |
| 3881 | # define perf_compat_ioctl NULL |
| 3882 | #endif |
| 3883 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3884 | int perf_event_task_enable(void) |
| 3885 | { |
| 3886 | struct perf_event *event; |
| 3887 | |
| 3888 | mutex_lock(¤t->perf_event_mutex); |
| 3889 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3890 | perf_event_for_each_child(event, perf_event_enable); |
| 3891 | mutex_unlock(¤t->perf_event_mutex); |
| 3892 | |
| 3893 | return 0; |
| 3894 | } |
| 3895 | |
| 3896 | int perf_event_task_disable(void) |
| 3897 | { |
| 3898 | struct perf_event *event; |
| 3899 | |
| 3900 | mutex_lock(¤t->perf_event_mutex); |
| 3901 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 3902 | perf_event_for_each_child(event, perf_event_disable); |
| 3903 | mutex_unlock(¤t->perf_event_mutex); |
| 3904 | |
| 3905 | return 0; |
| 3906 | } |
| 3907 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3908 | static int perf_event_index(struct perf_event *event) |
| 3909 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 3910 | if (event->hw.state & PERF_HES_STOPPED) |
| 3911 | return 0; |
| 3912 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3913 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 3914 | return 0; |
| 3915 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 3916 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3917 | } |
| 3918 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3919 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3920 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3921 | u64 *enabled, |
| 3922 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3923 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3924 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3925 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3926 | *now = perf_clock(); |
| 3927 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 3928 | *enabled = ctx_time - event->tstamp_enabled; |
| 3929 | *running = ctx_time - event->tstamp_running; |
| 3930 | } |
| 3931 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 3932 | static void perf_event_init_userpage(struct perf_event *event) |
| 3933 | { |
| 3934 | struct perf_event_mmap_page *userpg; |
| 3935 | struct ring_buffer *rb; |
| 3936 | |
| 3937 | rcu_read_lock(); |
| 3938 | rb = rcu_dereference(event->rb); |
| 3939 | if (!rb) |
| 3940 | goto unlock; |
| 3941 | |
| 3942 | userpg = rb->user_page; |
| 3943 | |
| 3944 | /* Allow new userspace to detect that bit 0 is deprecated */ |
| 3945 | userpg->cap_bit0_is_deprecated = 1; |
| 3946 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); |
| 3947 | |
| 3948 | unlock: |
| 3949 | rcu_read_unlock(); |
| 3950 | } |
| 3951 | |
Peter Zijlstra | c720620 | 2012-03-22 17:26:36 +0100 | [diff] [blame] | 3952 | void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3953 | { |
| 3954 | } |
| 3955 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3956 | /* |
| 3957 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 3958 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 3959 | * code calls this from NMI context. |
| 3960 | */ |
| 3961 | void perf_event_update_userpage(struct perf_event *event) |
| 3962 | { |
| 3963 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3964 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3965 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3966 | |
| 3967 | rcu_read_lock(); |
Peter Zijlstra | 5ec4c59 | 2013-08-02 21:16:30 +0200 | [diff] [blame] | 3968 | rb = rcu_dereference(event->rb); |
| 3969 | if (!rb) |
| 3970 | goto unlock; |
| 3971 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3972 | /* |
| 3973 | * compute total_time_enabled, total_time_running |
| 3974 | * based on snapshot values taken when the event |
| 3975 | * was last scheduled in. |
| 3976 | * |
| 3977 | * we cannot simply called update_context_time() |
| 3978 | * because of locking issue as we can be called in |
| 3979 | * NMI context |
| 3980 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 3981 | calc_timer_values(event, &now, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3982 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3983 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3984 | /* |
| 3985 | * Disable preemption so as to not let the corresponding user-space |
| 3986 | * spin too long if we get preempted. |
| 3987 | */ |
| 3988 | preempt_disable(); |
| 3989 | ++userpg->lock; |
| 3990 | barrier(); |
| 3991 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3992 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 3993 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3994 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3995 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3996 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3997 | atomic64_read(&event->child_total_time_enabled); |
| 3998 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 3999 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4000 | atomic64_read(&event->child_total_time_running); |
| 4001 | |
Peter Zijlstra | c720620 | 2012-03-22 17:26:36 +0100 | [diff] [blame] | 4002 | arch_perf_update_userpage(userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4003 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4004 | barrier(); |
| 4005 | ++userpg->lock; |
| 4006 | preempt_enable(); |
| 4007 | unlock: |
| 4008 | rcu_read_unlock(); |
| 4009 | } |
| 4010 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4011 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 4012 | { |
| 4013 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4014 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4015 | int ret = VM_FAULT_SIGBUS; |
| 4016 | |
| 4017 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 4018 | if (vmf->pgoff == 0) |
| 4019 | ret = 0; |
| 4020 | return ret; |
| 4021 | } |
| 4022 | |
| 4023 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4024 | rb = rcu_dereference(event->rb); |
| 4025 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4026 | goto unlock; |
| 4027 | |
| 4028 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 4029 | goto unlock; |
| 4030 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4031 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4032 | if (!vmf->page) |
| 4033 | goto unlock; |
| 4034 | |
| 4035 | get_page(vmf->page); |
| 4036 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 4037 | vmf->page->index = vmf->pgoff; |
| 4038 | |
| 4039 | ret = 0; |
| 4040 | unlock: |
| 4041 | rcu_read_unlock(); |
| 4042 | |
| 4043 | return ret; |
| 4044 | } |
| 4045 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4046 | static void ring_buffer_attach(struct perf_event *event, |
| 4047 | struct ring_buffer *rb) |
| 4048 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4049 | struct ring_buffer *old_rb = NULL; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4050 | unsigned long flags; |
| 4051 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4052 | if (event->rb) { |
| 4053 | /* |
| 4054 | * Should be impossible, we set this when removing |
| 4055 | * event->rb_entry and wait/clear when adding event->rb_entry. |
| 4056 | */ |
| 4057 | WARN_ON_ONCE(event->rcu_pending); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4058 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4059 | old_rb = event->rb; |
| 4060 | event->rcu_batches = get_state_synchronize_rcu(); |
| 4061 | event->rcu_pending = 1; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4062 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4063 | spin_lock_irqsave(&old_rb->event_lock, flags); |
| 4064 | list_del_rcu(&event->rb_entry); |
| 4065 | spin_unlock_irqrestore(&old_rb->event_lock, flags); |
| 4066 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4067 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4068 | if (event->rcu_pending && rb) { |
| 4069 | cond_synchronize_rcu(event->rcu_batches); |
| 4070 | event->rcu_pending = 0; |
| 4071 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4072 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4073 | if (rb) { |
| 4074 | spin_lock_irqsave(&rb->event_lock, flags); |
| 4075 | list_add_rcu(&event->rb_entry, &rb->event_list); |
| 4076 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 4077 | } |
| 4078 | |
| 4079 | rcu_assign_pointer(event->rb, rb); |
| 4080 | |
| 4081 | if (old_rb) { |
| 4082 | ring_buffer_put(old_rb); |
| 4083 | /* |
| 4084 | * Since we detached before setting the new rb, so that we |
| 4085 | * could attach the new rb, we could have missed a wakeup. |
| 4086 | * Provide it now. |
| 4087 | */ |
| 4088 | wake_up_all(&event->waitq); |
| 4089 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | static void ring_buffer_wakeup(struct perf_event *event) |
| 4093 | { |
| 4094 | struct ring_buffer *rb; |
| 4095 | |
| 4096 | rcu_read_lock(); |
| 4097 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4098 | if (rb) { |
| 4099 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 4100 | wake_up_all(&event->waitq); |
| 4101 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4102 | rcu_read_unlock(); |
| 4103 | } |
| 4104 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4105 | static void rb_free_rcu(struct rcu_head *rcu_head) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4106 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4107 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4108 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4109 | rb = container_of(rcu_head, struct ring_buffer, rcu_head); |
| 4110 | rb_free(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4111 | } |
| 4112 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4113 | static struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4114 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4115 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4116 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4117 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4118 | rb = rcu_dereference(event->rb); |
| 4119 | if (rb) { |
| 4120 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 4121 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4122 | } |
| 4123 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4124 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4125 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4126 | } |
| 4127 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4128 | static void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4129 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4130 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4131 | return; |
| 4132 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4133 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4134 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4135 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 4139 | { |
| 4140 | struct perf_event *event = vma->vm_file->private_data; |
| 4141 | |
| 4142 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4143 | atomic_inc(&event->rb->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4144 | } |
| 4145 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4146 | /* |
| 4147 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 4148 | * event, or through other events by use of perf_event_set_output(). |
| 4149 | * |
| 4150 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 4151 | * the buffer here, where we still have a VM context. This means we need |
| 4152 | * to detach all events redirecting to us. |
| 4153 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4154 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 4155 | { |
| 4156 | struct perf_event *event = vma->vm_file->private_data; |
| 4157 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4158 | struct ring_buffer *rb = ring_buffer_get(event); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4159 | struct user_struct *mmap_user = rb->mmap_user; |
| 4160 | int mmap_locked = rb->mmap_locked; |
| 4161 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4162 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4163 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4164 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4165 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4166 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4167 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4168 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4169 | mutex_unlock(&event->mmap_mutex); |
| 4170 | |
| 4171 | /* 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] | 4172 | if (atomic_read(&rb->mmap_count)) |
| 4173 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4174 | |
| 4175 | /* |
| 4176 | * No other mmap()s, detach from all other events that might redirect |
| 4177 | * into the now unreachable buffer. Somewhat complicated by the |
| 4178 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 4179 | */ |
| 4180 | again: |
| 4181 | rcu_read_lock(); |
| 4182 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 4183 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 4184 | /* |
| 4185 | * This event is en-route to free_event() which will |
| 4186 | * detach it and remove it from the list. |
| 4187 | */ |
| 4188 | continue; |
| 4189 | } |
| 4190 | rcu_read_unlock(); |
| 4191 | |
| 4192 | mutex_lock(&event->mmap_mutex); |
| 4193 | /* |
| 4194 | * Check we didn't race with perf_event_set_output() which can |
| 4195 | * swizzle the rb from under us while we were waiting to |
| 4196 | * acquire mmap_mutex. |
| 4197 | * |
| 4198 | * If we find a different rb; ignore this event, a next |
| 4199 | * iteration will no longer find it on the list. We have to |
| 4200 | * still restart the iteration to make sure we're not now |
| 4201 | * iterating the wrong list. |
| 4202 | */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4203 | if (event->rb == rb) |
| 4204 | ring_buffer_attach(event, NULL); |
| 4205 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4206 | mutex_unlock(&event->mmap_mutex); |
| 4207 | put_event(event); |
| 4208 | |
| 4209 | /* |
| 4210 | * Restart the iteration; either we're on the wrong list or |
| 4211 | * destroyed its integrity by doing a deletion. |
| 4212 | */ |
| 4213 | goto again; |
| 4214 | } |
| 4215 | rcu_read_unlock(); |
| 4216 | |
| 4217 | /* |
| 4218 | * It could be there's still a few 0-ref events on the list; they'll |
| 4219 | * get cleaned up by free_event() -- they'll also still have their |
| 4220 | * ref on the rb and will free it whenever they are done with it. |
| 4221 | * |
| 4222 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 4223 | * undo the VM accounting. |
| 4224 | */ |
| 4225 | |
| 4226 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 4227 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 4228 | free_uid(mmap_user); |
| 4229 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4230 | out_put: |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4231 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4232 | } |
| 4233 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 4234 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4235 | .open = perf_mmap_open, |
| 4236 | .close = perf_mmap_close, |
| 4237 | .fault = perf_mmap_fault, |
| 4238 | .page_mkwrite = perf_mmap_fault, |
| 4239 | }; |
| 4240 | |
| 4241 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 4242 | { |
| 4243 | struct perf_event *event = file->private_data; |
| 4244 | unsigned long user_locked, user_lock_limit; |
| 4245 | struct user_struct *user = current_user(); |
| 4246 | unsigned long locked, lock_limit; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4247 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4248 | unsigned long vma_size; |
| 4249 | unsigned long nr_pages; |
| 4250 | long user_extra, extra; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4251 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4252 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4253 | /* |
| 4254 | * Don't allow mmap() of inherited per-task counters. This would |
| 4255 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4256 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4257 | */ |
| 4258 | if (event->cpu == -1 && event->attr.inherit) |
| 4259 | return -EINVAL; |
| 4260 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4261 | if (!(vma->vm_flags & VM_SHARED)) |
| 4262 | return -EINVAL; |
| 4263 | |
| 4264 | vma_size = vma->vm_end - vma->vm_start; |
| 4265 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 4266 | |
| 4267 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4268 | * 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] | 4269 | * can do bitmasks instead of modulo. |
| 4270 | */ |
| 4271 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
| 4272 | return -EINVAL; |
| 4273 | |
| 4274 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 4275 | return -EINVAL; |
| 4276 | |
| 4277 | if (vma->vm_pgoff != 0) |
| 4278 | return -EINVAL; |
| 4279 | |
| 4280 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4281 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4282 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4283 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4284 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4285 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4286 | goto unlock; |
| 4287 | } |
| 4288 | |
| 4289 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 4290 | /* |
| 4291 | * Raced against perf_mmap_close() through |
| 4292 | * perf_event_set_output(). Try again, hope for better |
| 4293 | * luck. |
| 4294 | */ |
| 4295 | mutex_unlock(&event->mmap_mutex); |
| 4296 | goto again; |
| 4297 | } |
| 4298 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4299 | goto unlock; |
| 4300 | } |
| 4301 | |
| 4302 | user_extra = nr_pages + 1; |
| 4303 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 4304 | |
| 4305 | /* |
| 4306 | * Increase the limit linearly with more CPUs: |
| 4307 | */ |
| 4308 | user_lock_limit *= num_online_cpus(); |
| 4309 | |
| 4310 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 4311 | |
| 4312 | extra = 0; |
| 4313 | if (user_locked > user_lock_limit) |
| 4314 | extra = user_locked - user_lock_limit; |
| 4315 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 4316 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4317 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 4318 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4319 | |
| 4320 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 4321 | !capable(CAP_IPC_LOCK)) { |
| 4322 | ret = -EPERM; |
| 4323 | goto unlock; |
| 4324 | } |
| 4325 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4326 | WARN_ON(event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4327 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4328 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4329 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4330 | |
Vince Weaver | 4ec8363 | 2011-06-01 15:15:36 -0400 | [diff] [blame] | 4331 | rb = rb_alloc(nr_pages, |
| 4332 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 4333 | event->cpu, flags); |
| 4334 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4335 | if (!rb) { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4336 | ret = -ENOMEM; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4337 | goto unlock; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4338 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4339 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4340 | atomic_set(&rb->mmap_count, 1); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4341 | rb->mmap_locked = extra; |
| 4342 | rb->mmap_user = get_current_user(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4343 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4344 | atomic_long_add(user_extra, &user->locked_vm); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4345 | vma->vm_mm->pinned_vm += extra; |
| 4346 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4347 | ring_buffer_attach(event, rb); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4348 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4349 | perf_event_init_userpage(event); |
Peter Zijlstra | 9a0f05c | 2011-11-21 15:13:29 +0100 | [diff] [blame] | 4350 | perf_event_update_userpage(event); |
| 4351 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4352 | unlock: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4353 | if (!ret) |
| 4354 | atomic_inc(&event->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4355 | mutex_unlock(&event->mmap_mutex); |
| 4356 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4357 | /* |
| 4358 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 4359 | * vma. |
| 4360 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4361 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4362 | vma->vm_ops = &perf_mmap_vmops; |
| 4363 | |
| 4364 | return ret; |
| 4365 | } |
| 4366 | |
| 4367 | static int perf_fasync(int fd, struct file *filp, int on) |
| 4368 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4369 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4370 | struct perf_event *event = filp->private_data; |
| 4371 | int retval; |
| 4372 | |
| 4373 | mutex_lock(&inode->i_mutex); |
| 4374 | retval = fasync_helper(fd, filp, on, &event->fasync); |
| 4375 | mutex_unlock(&inode->i_mutex); |
| 4376 | |
| 4377 | if (retval < 0) |
| 4378 | return retval; |
| 4379 | |
| 4380 | return 0; |
| 4381 | } |
| 4382 | |
| 4383 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 4384 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4385 | .release = perf_release, |
| 4386 | .read = perf_read, |
| 4387 | .poll = perf_poll, |
| 4388 | .unlocked_ioctl = perf_ioctl, |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4389 | .compat_ioctl = perf_compat_ioctl, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4390 | .mmap = perf_mmap, |
| 4391 | .fasync = perf_fasync, |
| 4392 | }; |
| 4393 | |
| 4394 | /* |
| 4395 | * Perf event wakeup |
| 4396 | * |
| 4397 | * If there's data, ensure we set the poll() state and publish everything |
| 4398 | * to user-space before waking everybody up. |
| 4399 | */ |
| 4400 | |
| 4401 | void perf_event_wakeup(struct perf_event *event) |
| 4402 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4403 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4404 | |
| 4405 | if (event->pending_kill) { |
| 4406 | kill_fasync(&event->fasync, SIGIO, event->pending_kill); |
| 4407 | event->pending_kill = 0; |
| 4408 | } |
| 4409 | } |
| 4410 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 4411 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4412 | { |
| 4413 | struct perf_event *event = container_of(entry, |
| 4414 | struct perf_event, pending); |
| 4415 | |
| 4416 | if (event->pending_disable) { |
| 4417 | event->pending_disable = 0; |
| 4418 | __perf_event_disable(event); |
| 4419 | } |
| 4420 | |
| 4421 | if (event->pending_wakeup) { |
| 4422 | event->pending_wakeup = 0; |
| 4423 | perf_event_wakeup(event); |
| 4424 | } |
| 4425 | } |
| 4426 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4427 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 4428 | * We assume there is only KVM supporting the callbacks. |
| 4429 | * Later on, we might change it to a list if there is |
| 4430 | * another virtualization implementation supporting the callbacks. |
| 4431 | */ |
| 4432 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 4433 | |
| 4434 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4435 | { |
| 4436 | perf_guest_cbs = cbs; |
| 4437 | return 0; |
| 4438 | } |
| 4439 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 4440 | |
| 4441 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4442 | { |
| 4443 | perf_guest_cbs = NULL; |
| 4444 | return 0; |
| 4445 | } |
| 4446 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 4447 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4448 | static void |
| 4449 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 4450 | struct pt_regs *regs, u64 mask) |
| 4451 | { |
| 4452 | int bit; |
| 4453 | |
| 4454 | for_each_set_bit(bit, (const unsigned long *) &mask, |
| 4455 | sizeof(mask) * BITS_PER_BYTE) { |
| 4456 | u64 val; |
| 4457 | |
| 4458 | val = perf_reg_value(regs, bit); |
| 4459 | perf_output_put(handle, val); |
| 4460 | } |
| 4461 | } |
| 4462 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 4463 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4464 | struct pt_regs *regs) |
| 4465 | { |
| 4466 | if (!user_mode(regs)) { |
| 4467 | if (current->mm) |
| 4468 | regs = task_pt_regs(current); |
| 4469 | else |
| 4470 | regs = NULL; |
| 4471 | } |
| 4472 | |
| 4473 | if (regs) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4474 | regs_user->abi = perf_reg_abi(current); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame^] | 4475 | regs_user->regs = regs; |
| 4476 | } else { |
| 4477 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; |
| 4478 | regs_user->regs = NULL; |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4479 | } |
| 4480 | } |
| 4481 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 4482 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
| 4483 | struct pt_regs *regs) |
| 4484 | { |
| 4485 | regs_intr->regs = regs; |
| 4486 | regs_intr->abi = perf_reg_abi(current); |
| 4487 | } |
| 4488 | |
| 4489 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4490 | /* |
| 4491 | * Get remaining task size from user stack pointer. |
| 4492 | * |
| 4493 | * It'd be better to take stack vma map and limit this more |
| 4494 | * precisly, but there's no way to get it safely under interrupt, |
| 4495 | * so using TASK_SIZE as limit. |
| 4496 | */ |
| 4497 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 4498 | { |
| 4499 | unsigned long addr = perf_user_stack_pointer(regs); |
| 4500 | |
| 4501 | if (!addr || addr >= TASK_SIZE) |
| 4502 | return 0; |
| 4503 | |
| 4504 | return TASK_SIZE - addr; |
| 4505 | } |
| 4506 | |
| 4507 | static u16 |
| 4508 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 4509 | struct pt_regs *regs) |
| 4510 | { |
| 4511 | u64 task_size; |
| 4512 | |
| 4513 | /* No regs, no stack pointer, no dump. */ |
| 4514 | if (!regs) |
| 4515 | return 0; |
| 4516 | |
| 4517 | /* |
| 4518 | * Check if we fit in with the requested stack size into the: |
| 4519 | * - TASK_SIZE |
| 4520 | * If we don't, we limit the size to the TASK_SIZE. |
| 4521 | * |
| 4522 | * - remaining sample size |
| 4523 | * If we don't, we customize the stack size to |
| 4524 | * fit in to the remaining sample size. |
| 4525 | */ |
| 4526 | |
| 4527 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 4528 | stack_size = min(stack_size, (u16) task_size); |
| 4529 | |
| 4530 | /* Current header size plus static size and dynamic size. */ |
| 4531 | header_size += 2 * sizeof(u64); |
| 4532 | |
| 4533 | /* Do we fit in with the current stack dump size? */ |
| 4534 | if ((u16) (header_size + stack_size) < header_size) { |
| 4535 | /* |
| 4536 | * If we overflow the maximum size for the sample, |
| 4537 | * we customize the stack dump size to fit in. |
| 4538 | */ |
| 4539 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 4540 | stack_size = round_up(stack_size, sizeof(u64)); |
| 4541 | } |
| 4542 | |
| 4543 | return stack_size; |
| 4544 | } |
| 4545 | |
| 4546 | static void |
| 4547 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 4548 | struct pt_regs *regs) |
| 4549 | { |
| 4550 | /* Case of a kernel thread, nothing to dump */ |
| 4551 | if (!regs) { |
| 4552 | u64 size = 0; |
| 4553 | perf_output_put(handle, size); |
| 4554 | } else { |
| 4555 | unsigned long sp; |
| 4556 | unsigned int rem; |
| 4557 | u64 dyn_size; |
| 4558 | |
| 4559 | /* |
| 4560 | * We dump: |
| 4561 | * static size |
| 4562 | * - the size requested by user or the best one we can fit |
| 4563 | * in to the sample max size |
| 4564 | * data |
| 4565 | * - user stack dump data |
| 4566 | * dynamic size |
| 4567 | * - the actual dumped size |
| 4568 | */ |
| 4569 | |
| 4570 | /* Static size. */ |
| 4571 | perf_output_put(handle, dump_size); |
| 4572 | |
| 4573 | /* Data. */ |
| 4574 | sp = perf_user_stack_pointer(regs); |
| 4575 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 4576 | dyn_size = dump_size - rem; |
| 4577 | |
| 4578 | perf_output_skip(handle, rem); |
| 4579 | |
| 4580 | /* Dynamic size. */ |
| 4581 | perf_output_put(handle, dyn_size); |
| 4582 | } |
| 4583 | } |
| 4584 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4585 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 4586 | struct perf_sample_data *data, |
| 4587 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4588 | { |
| 4589 | u64 sample_type = event->attr.sample_type; |
| 4590 | |
| 4591 | data->type = sample_type; |
| 4592 | header->size += event->id_header_size; |
| 4593 | |
| 4594 | if (sample_type & PERF_SAMPLE_TID) { |
| 4595 | /* namespace issues */ |
| 4596 | data->tid_entry.pid = perf_event_pid(event, current); |
| 4597 | data->tid_entry.tid = perf_event_tid(event, current); |
| 4598 | } |
| 4599 | |
| 4600 | if (sample_type & PERF_SAMPLE_TIME) |
| 4601 | data->time = perf_clock(); |
| 4602 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4603 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4604 | data->id = primary_event_id(event); |
| 4605 | |
| 4606 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4607 | data->stream_id = event->id; |
| 4608 | |
| 4609 | if (sample_type & PERF_SAMPLE_CPU) { |
| 4610 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 4611 | data->cpu_entry.reserved = 0; |
| 4612 | } |
| 4613 | } |
| 4614 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4615 | void perf_event_header__init_id(struct perf_event_header *header, |
| 4616 | struct perf_sample_data *data, |
| 4617 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4618 | { |
| 4619 | if (event->attr.sample_id_all) |
| 4620 | __perf_event_header__init_id(header, data, event); |
| 4621 | } |
| 4622 | |
| 4623 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 4624 | struct perf_sample_data *data) |
| 4625 | { |
| 4626 | u64 sample_type = data->type; |
| 4627 | |
| 4628 | if (sample_type & PERF_SAMPLE_TID) |
| 4629 | perf_output_put(handle, data->tid_entry); |
| 4630 | |
| 4631 | if (sample_type & PERF_SAMPLE_TIME) |
| 4632 | perf_output_put(handle, data->time); |
| 4633 | |
| 4634 | if (sample_type & PERF_SAMPLE_ID) |
| 4635 | perf_output_put(handle, data->id); |
| 4636 | |
| 4637 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4638 | perf_output_put(handle, data->stream_id); |
| 4639 | |
| 4640 | if (sample_type & PERF_SAMPLE_CPU) |
| 4641 | perf_output_put(handle, data->cpu_entry); |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4642 | |
| 4643 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4644 | perf_output_put(handle, data->id); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4645 | } |
| 4646 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4647 | void perf_event__output_id_sample(struct perf_event *event, |
| 4648 | struct perf_output_handle *handle, |
| 4649 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4650 | { |
| 4651 | if (event->attr.sample_id_all) |
| 4652 | __perf_event__output_id_sample(handle, sample); |
| 4653 | } |
| 4654 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4655 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4656 | struct perf_event *event, |
| 4657 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4658 | { |
| 4659 | u64 read_format = event->attr.read_format; |
| 4660 | u64 values[4]; |
| 4661 | int n = 0; |
| 4662 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4663 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4664 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4665 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4666 | atomic64_read(&event->child_total_time_enabled); |
| 4667 | } |
| 4668 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4669 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4670 | atomic64_read(&event->child_total_time_running); |
| 4671 | } |
| 4672 | if (read_format & PERF_FORMAT_ID) |
| 4673 | values[n++] = primary_event_id(event); |
| 4674 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4675 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | /* |
| 4679 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 4680 | */ |
| 4681 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4682 | struct perf_event *event, |
| 4683 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4684 | { |
| 4685 | struct perf_event *leader = event->group_leader, *sub; |
| 4686 | u64 read_format = event->attr.read_format; |
| 4687 | u64 values[5]; |
| 4688 | int n = 0; |
| 4689 | |
| 4690 | values[n++] = 1 + leader->nr_siblings; |
| 4691 | |
| 4692 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4693 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4694 | |
| 4695 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4696 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4697 | |
| 4698 | if (leader != event) |
| 4699 | leader->pmu->read(leader); |
| 4700 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4701 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4702 | if (read_format & PERF_FORMAT_ID) |
| 4703 | values[n++] = primary_event_id(leader); |
| 4704 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4705 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4706 | |
| 4707 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 4708 | n = 0; |
| 4709 | |
Jiri Olsa | 6f5ab00 | 2012-10-15 20:13:45 +0200 | [diff] [blame] | 4710 | if ((sub != event) && |
| 4711 | (sub->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4712 | sub->pmu->read(sub); |
| 4713 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4714 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4715 | if (read_format & PERF_FORMAT_ID) |
| 4716 | values[n++] = primary_event_id(sub); |
| 4717 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4718 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4719 | } |
| 4720 | } |
| 4721 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4722 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 4723 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4724 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4725 | static void perf_output_read(struct perf_output_handle *handle, |
| 4726 | struct perf_event *event) |
| 4727 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4728 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4729 | u64 read_format = event->attr.read_format; |
| 4730 | |
| 4731 | /* |
| 4732 | * compute total_time_enabled, total_time_running |
| 4733 | * based on snapshot values taken when the event |
| 4734 | * was last scheduled in. |
| 4735 | * |
| 4736 | * we cannot simply called update_context_time() |
| 4737 | * because of locking issue as we are called in |
| 4738 | * NMI context |
| 4739 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4740 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4741 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4742 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4743 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4744 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4745 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4746 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | void perf_output_sample(struct perf_output_handle *handle, |
| 4750 | struct perf_event_header *header, |
| 4751 | struct perf_sample_data *data, |
| 4752 | struct perf_event *event) |
| 4753 | { |
| 4754 | u64 sample_type = data->type; |
| 4755 | |
| 4756 | perf_output_put(handle, *header); |
| 4757 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4758 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4759 | perf_output_put(handle, data->id); |
| 4760 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4761 | if (sample_type & PERF_SAMPLE_IP) |
| 4762 | perf_output_put(handle, data->ip); |
| 4763 | |
| 4764 | if (sample_type & PERF_SAMPLE_TID) |
| 4765 | perf_output_put(handle, data->tid_entry); |
| 4766 | |
| 4767 | if (sample_type & PERF_SAMPLE_TIME) |
| 4768 | perf_output_put(handle, data->time); |
| 4769 | |
| 4770 | if (sample_type & PERF_SAMPLE_ADDR) |
| 4771 | perf_output_put(handle, data->addr); |
| 4772 | |
| 4773 | if (sample_type & PERF_SAMPLE_ID) |
| 4774 | perf_output_put(handle, data->id); |
| 4775 | |
| 4776 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4777 | perf_output_put(handle, data->stream_id); |
| 4778 | |
| 4779 | if (sample_type & PERF_SAMPLE_CPU) |
| 4780 | perf_output_put(handle, data->cpu_entry); |
| 4781 | |
| 4782 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 4783 | perf_output_put(handle, data->period); |
| 4784 | |
| 4785 | if (sample_type & PERF_SAMPLE_READ) |
| 4786 | perf_output_read(handle, event); |
| 4787 | |
| 4788 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4789 | if (data->callchain) { |
| 4790 | int size = 1; |
| 4791 | |
| 4792 | if (data->callchain) |
| 4793 | size += data->callchain->nr; |
| 4794 | |
| 4795 | size *= sizeof(u64); |
| 4796 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4797 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4798 | } else { |
| 4799 | u64 nr = 0; |
| 4800 | perf_output_put(handle, nr); |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4805 | if (data->raw) { |
| 4806 | perf_output_put(handle, data->raw->size); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4807 | __output_copy(handle, data->raw->data, |
| 4808 | data->raw->size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4809 | } else { |
| 4810 | struct { |
| 4811 | u32 size; |
| 4812 | u32 data; |
| 4813 | } raw = { |
| 4814 | .size = sizeof(u32), |
| 4815 | .data = 0, |
| 4816 | }; |
| 4817 | perf_output_put(handle, raw); |
| 4818 | } |
| 4819 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4820 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4821 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4822 | if (data->br_stack) { |
| 4823 | size_t size; |
| 4824 | |
| 4825 | size = data->br_stack->nr |
| 4826 | * sizeof(struct perf_branch_entry); |
| 4827 | |
| 4828 | perf_output_put(handle, data->br_stack->nr); |
| 4829 | perf_output_copy(handle, data->br_stack->entries, size); |
| 4830 | } else { |
| 4831 | /* |
| 4832 | * we always store at least the value of nr |
| 4833 | */ |
| 4834 | u64 nr = 0; |
| 4835 | perf_output_put(handle, nr); |
| 4836 | } |
| 4837 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4838 | |
| 4839 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 4840 | u64 abi = data->regs_user.abi; |
| 4841 | |
| 4842 | /* |
| 4843 | * If there are no regs to dump, notice it through |
| 4844 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 4845 | */ |
| 4846 | perf_output_put(handle, abi); |
| 4847 | |
| 4848 | if (abi) { |
| 4849 | u64 mask = event->attr.sample_regs_user; |
| 4850 | perf_output_sample_regs(handle, |
| 4851 | data->regs_user.regs, |
| 4852 | mask); |
| 4853 | } |
| 4854 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4855 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4856 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4857 | perf_output_sample_ustack(handle, |
| 4858 | data->stack_user_size, |
| 4859 | data->regs_user.regs); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4860 | } |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 4861 | |
| 4862 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 4863 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 4864 | |
| 4865 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 4866 | perf_output_put(handle, data->data_src.val); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4867 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 4868 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 4869 | perf_output_put(handle, data->txn); |
| 4870 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 4871 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 4872 | u64 abi = data->regs_intr.abi; |
| 4873 | /* |
| 4874 | * If there are no regs to dump, notice it through |
| 4875 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 4876 | */ |
| 4877 | perf_output_put(handle, abi); |
| 4878 | |
| 4879 | if (abi) { |
| 4880 | u64 mask = event->attr.sample_regs_intr; |
| 4881 | |
| 4882 | perf_output_sample_regs(handle, |
| 4883 | data->regs_intr.regs, |
| 4884 | mask); |
| 4885 | } |
| 4886 | } |
| 4887 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 4888 | if (!event->attr.watermark) { |
| 4889 | int wakeup_events = event->attr.wakeup_events; |
| 4890 | |
| 4891 | if (wakeup_events) { |
| 4892 | struct ring_buffer *rb = handle->rb; |
| 4893 | int events = local_inc_return(&rb->events); |
| 4894 | |
| 4895 | if (events >= wakeup_events) { |
| 4896 | local_sub(wakeup_events, &rb->events); |
| 4897 | local_inc(&rb->wakeup); |
| 4898 | } |
| 4899 | } |
| 4900 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | void perf_prepare_sample(struct perf_event_header *header, |
| 4904 | struct perf_sample_data *data, |
| 4905 | struct perf_event *event, |
| 4906 | struct pt_regs *regs) |
| 4907 | { |
| 4908 | u64 sample_type = event->attr.sample_type; |
| 4909 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4910 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4911 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4912 | |
| 4913 | header->misc = 0; |
| 4914 | header->misc |= perf_misc_flags(regs); |
| 4915 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4916 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4917 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4918 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4919 | data->ip = perf_instruction_pointer(regs); |
| 4920 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4921 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4922 | int size = 1; |
| 4923 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 4924 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4925 | |
| 4926 | if (data->callchain) |
| 4927 | size += data->callchain->nr; |
| 4928 | |
| 4929 | header->size += size * sizeof(u64); |
| 4930 | } |
| 4931 | |
| 4932 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4933 | int size = sizeof(u32); |
| 4934 | |
| 4935 | if (data->raw) |
| 4936 | size += data->raw->size; |
| 4937 | else |
| 4938 | size += sizeof(u32); |
| 4939 | |
| 4940 | WARN_ON_ONCE(size & (sizeof(u64)-1)); |
| 4941 | header->size += size; |
| 4942 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4943 | |
| 4944 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4945 | int size = sizeof(u64); /* nr */ |
| 4946 | if (data->br_stack) { |
| 4947 | size += data->br_stack->nr |
| 4948 | * sizeof(struct perf_branch_entry); |
| 4949 | } |
| 4950 | header->size += size; |
| 4951 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4952 | |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame^] | 4953 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
| 4954 | perf_sample_regs_user(&data->regs_user, regs); |
| 4955 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4956 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 4957 | /* regs dump ABI info */ |
| 4958 | int size = sizeof(u64); |
| 4959 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4960 | if (data->regs_user.regs) { |
| 4961 | u64 mask = event->attr.sample_regs_user; |
| 4962 | size += hweight64(mask) * sizeof(u64); |
| 4963 | } |
| 4964 | |
| 4965 | header->size += size; |
| 4966 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4967 | |
| 4968 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 4969 | /* |
| 4970 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 4971 | * processed as the last one or have additional check added |
| 4972 | * in case new sample type is added, because we could eat |
| 4973 | * up the rest of the sample size. |
| 4974 | */ |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4975 | u16 stack_size = event->attr.sample_stack_user; |
| 4976 | u16 size = sizeof(u64); |
| 4977 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4978 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame^] | 4979 | data->regs_user.regs); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4980 | |
| 4981 | /* |
| 4982 | * If there is something to dump, add space for the dump |
| 4983 | * itself and for the field that tells the dynamic size, |
| 4984 | * which is how many have been actually dumped. |
| 4985 | */ |
| 4986 | if (stack_size) |
| 4987 | size += sizeof(u64) + stack_size; |
| 4988 | |
| 4989 | data->stack_user_size = stack_size; |
| 4990 | header->size += size; |
| 4991 | } |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 4992 | |
| 4993 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 4994 | /* regs dump ABI info */ |
| 4995 | int size = sizeof(u64); |
| 4996 | |
| 4997 | perf_sample_regs_intr(&data->regs_intr, regs); |
| 4998 | |
| 4999 | if (data->regs_intr.regs) { |
| 5000 | u64 mask = event->attr.sample_regs_intr; |
| 5001 | |
| 5002 | size += hweight64(mask) * sizeof(u64); |
| 5003 | } |
| 5004 | |
| 5005 | header->size += size; |
| 5006 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5007 | } |
| 5008 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5009 | static void perf_event_output(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5010 | struct perf_sample_data *data, |
| 5011 | struct pt_regs *regs) |
| 5012 | { |
| 5013 | struct perf_output_handle handle; |
| 5014 | struct perf_event_header header; |
| 5015 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5016 | /* protect the callchain buffers */ |
| 5017 | rcu_read_lock(); |
| 5018 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5019 | perf_prepare_sample(&header, data, event, regs); |
| 5020 | |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5021 | if (perf_output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5022 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5023 | |
| 5024 | perf_output_sample(&handle, &header, data, event); |
| 5025 | |
| 5026 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5027 | |
| 5028 | exit: |
| 5029 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5030 | } |
| 5031 | |
| 5032 | /* |
| 5033 | * read event_id |
| 5034 | */ |
| 5035 | |
| 5036 | struct perf_read_event { |
| 5037 | struct perf_event_header header; |
| 5038 | |
| 5039 | u32 pid; |
| 5040 | u32 tid; |
| 5041 | }; |
| 5042 | |
| 5043 | static void |
| 5044 | perf_event_read_event(struct perf_event *event, |
| 5045 | struct task_struct *task) |
| 5046 | { |
| 5047 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5048 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5049 | struct perf_read_event read_event = { |
| 5050 | .header = { |
| 5051 | .type = PERF_RECORD_READ, |
| 5052 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5053 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5054 | }, |
| 5055 | .pid = perf_event_pid(event, task), |
| 5056 | .tid = perf_event_tid(event, task), |
| 5057 | }; |
| 5058 | int ret; |
| 5059 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5060 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5061 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5062 | if (ret) |
| 5063 | return; |
| 5064 | |
| 5065 | perf_output_put(&handle, read_event); |
| 5066 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5067 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5068 | |
| 5069 | perf_output_end(&handle); |
| 5070 | } |
| 5071 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5072 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
| 5073 | |
| 5074 | static void |
| 5075 | perf_event_aux_ctx(struct perf_event_context *ctx, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5076 | perf_event_aux_output_cb output, |
| 5077 | void *data) |
| 5078 | { |
| 5079 | struct perf_event *event; |
| 5080 | |
| 5081 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 5082 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 5083 | continue; |
| 5084 | if (!event_filter_match(event)) |
| 5085 | continue; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5086 | output(event, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5087 | } |
| 5088 | } |
| 5089 | |
| 5090 | static void |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5091 | perf_event_aux(perf_event_aux_output_cb output, void *data, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5092 | struct perf_event_context *task_ctx) |
| 5093 | { |
| 5094 | struct perf_cpu_context *cpuctx; |
| 5095 | struct perf_event_context *ctx; |
| 5096 | struct pmu *pmu; |
| 5097 | int ctxn; |
| 5098 | |
| 5099 | rcu_read_lock(); |
| 5100 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 5101 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 5102 | if (cpuctx->unique_pmu != pmu) |
| 5103 | goto next; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5104 | perf_event_aux_ctx(&cpuctx->ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5105 | if (task_ctx) |
| 5106 | goto next; |
| 5107 | ctxn = pmu->task_ctx_nr; |
| 5108 | if (ctxn < 0) |
| 5109 | goto next; |
| 5110 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 5111 | if (ctx) |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5112 | perf_event_aux_ctx(ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5113 | next: |
| 5114 | put_cpu_ptr(pmu->pmu_cpu_context); |
| 5115 | } |
| 5116 | |
| 5117 | if (task_ctx) { |
| 5118 | preempt_disable(); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5119 | perf_event_aux_ctx(task_ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5120 | preempt_enable(); |
| 5121 | } |
| 5122 | rcu_read_unlock(); |
| 5123 | } |
| 5124 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5125 | /* |
| 5126 | * task tracking -- fork/exit |
| 5127 | * |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5128 | * 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] | 5129 | */ |
| 5130 | |
| 5131 | struct perf_task_event { |
| 5132 | struct task_struct *task; |
| 5133 | struct perf_event_context *task_ctx; |
| 5134 | |
| 5135 | struct { |
| 5136 | struct perf_event_header header; |
| 5137 | |
| 5138 | u32 pid; |
| 5139 | u32 ppid; |
| 5140 | u32 tid; |
| 5141 | u32 ptid; |
| 5142 | u64 time; |
| 5143 | } event_id; |
| 5144 | }; |
| 5145 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5146 | static int perf_event_task_match(struct perf_event *event) |
| 5147 | { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5148 | return event->attr.comm || event->attr.mmap || |
| 5149 | event->attr.mmap2 || event->attr.mmap_data || |
| 5150 | event->attr.task; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5151 | } |
| 5152 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5153 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5154 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5155 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5156 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5157 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5158 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5159 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5160 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 5161 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5162 | if (!perf_event_task_match(event)) |
| 5163 | return; |
| 5164 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5165 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5166 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5167 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5168 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 5169 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5170 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5171 | |
| 5172 | task_event->event_id.pid = perf_event_pid(event, task); |
| 5173 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 5174 | |
| 5175 | task_event->event_id.tid = perf_event_tid(event, task); |
| 5176 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 5177 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5178 | perf_output_put(&handle, task_event->event_id); |
| 5179 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5180 | perf_event__output_id_sample(event, &handle, &sample); |
| 5181 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5182 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5183 | out: |
| 5184 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5185 | } |
| 5186 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5187 | static void perf_event_task(struct task_struct *task, |
| 5188 | struct perf_event_context *task_ctx, |
| 5189 | int new) |
| 5190 | { |
| 5191 | struct perf_task_event task_event; |
| 5192 | |
| 5193 | if (!atomic_read(&nr_comm_events) && |
| 5194 | !atomic_read(&nr_mmap_events) && |
| 5195 | !atomic_read(&nr_task_events)) |
| 5196 | return; |
| 5197 | |
| 5198 | task_event = (struct perf_task_event){ |
| 5199 | .task = task, |
| 5200 | .task_ctx = task_ctx, |
| 5201 | .event_id = { |
| 5202 | .header = { |
| 5203 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 5204 | .misc = 0, |
| 5205 | .size = sizeof(task_event.event_id), |
| 5206 | }, |
| 5207 | /* .pid */ |
| 5208 | /* .ppid */ |
| 5209 | /* .tid */ |
| 5210 | /* .ptid */ |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 5211 | .time = perf_clock(), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5212 | }, |
| 5213 | }; |
| 5214 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5215 | perf_event_aux(perf_event_task_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5216 | &task_event, |
| 5217 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | void perf_event_fork(struct task_struct *task) |
| 5221 | { |
| 5222 | perf_event_task(task, NULL, 1); |
| 5223 | } |
| 5224 | |
| 5225 | /* |
| 5226 | * comm tracking |
| 5227 | */ |
| 5228 | |
| 5229 | struct perf_comm_event { |
| 5230 | struct task_struct *task; |
| 5231 | char *comm; |
| 5232 | int comm_size; |
| 5233 | |
| 5234 | struct { |
| 5235 | struct perf_event_header header; |
| 5236 | |
| 5237 | u32 pid; |
| 5238 | u32 tid; |
| 5239 | } event_id; |
| 5240 | }; |
| 5241 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5242 | static int perf_event_comm_match(struct perf_event *event) |
| 5243 | { |
| 5244 | return event->attr.comm; |
| 5245 | } |
| 5246 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5247 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5248 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5249 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5250 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5251 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5252 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5253 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5254 | int ret; |
| 5255 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5256 | if (!perf_event_comm_match(event)) |
| 5257 | return; |
| 5258 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5259 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 5260 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5261 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5262 | |
| 5263 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5264 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5265 | |
| 5266 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 5267 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 5268 | |
| 5269 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5270 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5271 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5272 | |
| 5273 | perf_event__output_id_sample(event, &handle, &sample); |
| 5274 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5275 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5276 | out: |
| 5277 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5278 | } |
| 5279 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5280 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 5281 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5282 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5283 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5284 | |
| 5285 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 5286 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5287 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 5288 | |
| 5289 | comm_event->comm = comm; |
| 5290 | comm_event->comm_size = size; |
| 5291 | |
| 5292 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5293 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5294 | perf_event_aux(perf_event_comm_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5295 | comm_event, |
| 5296 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5297 | } |
| 5298 | |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5299 | void perf_event_comm(struct task_struct *task, bool exec) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5300 | { |
| 5301 | struct perf_comm_event comm_event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5302 | |
| 5303 | if (!atomic_read(&nr_comm_events)) |
| 5304 | return; |
| 5305 | |
| 5306 | comm_event = (struct perf_comm_event){ |
| 5307 | .task = task, |
| 5308 | /* .comm */ |
| 5309 | /* .comm_size */ |
| 5310 | .event_id = { |
| 5311 | .header = { |
| 5312 | .type = PERF_RECORD_COMM, |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5313 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5314 | /* .size */ |
| 5315 | }, |
| 5316 | /* .pid */ |
| 5317 | /* .tid */ |
| 5318 | }, |
| 5319 | }; |
| 5320 | |
| 5321 | perf_event_comm_event(&comm_event); |
| 5322 | } |
| 5323 | |
| 5324 | /* |
| 5325 | * mmap tracking |
| 5326 | */ |
| 5327 | |
| 5328 | struct perf_mmap_event { |
| 5329 | struct vm_area_struct *vma; |
| 5330 | |
| 5331 | const char *file_name; |
| 5332 | int file_size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5333 | int maj, min; |
| 5334 | u64 ino; |
| 5335 | u64 ino_generation; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5336 | u32 prot, flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5337 | |
| 5338 | struct { |
| 5339 | struct perf_event_header header; |
| 5340 | |
| 5341 | u32 pid; |
| 5342 | u32 tid; |
| 5343 | u64 start; |
| 5344 | u64 len; |
| 5345 | u64 pgoff; |
| 5346 | } event_id; |
| 5347 | }; |
| 5348 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5349 | static int perf_event_mmap_match(struct perf_event *event, |
| 5350 | void *data) |
| 5351 | { |
| 5352 | struct perf_mmap_event *mmap_event = data; |
| 5353 | struct vm_area_struct *vma = mmap_event->vma; |
| 5354 | int executable = vma->vm_flags & VM_EXEC; |
| 5355 | |
| 5356 | return (!executable && event->attr.mmap_data) || |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5357 | (executable && (event->attr.mmap || event->attr.mmap2)); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5358 | } |
| 5359 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5360 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5361 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5362 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5363 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5364 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5365 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5366 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5367 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5368 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5369 | if (!perf_event_mmap_match(event, data)) |
| 5370 | return; |
| 5371 | |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5372 | if (event->attr.mmap2) { |
| 5373 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; |
| 5374 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); |
| 5375 | mmap_event->event_id.header.size += sizeof(mmap_event->min); |
| 5376 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); |
Arnaldo Carvalho de Melo | d008d52 | 2013-09-10 10:24:05 -0300 | [diff] [blame] | 5377 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5378 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
| 5379 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5380 | } |
| 5381 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5382 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 5383 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5384 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5385 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5386 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5387 | |
| 5388 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 5389 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 5390 | |
| 5391 | perf_output_put(&handle, mmap_event->event_id); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5392 | |
| 5393 | if (event->attr.mmap2) { |
| 5394 | perf_output_put(&handle, mmap_event->maj); |
| 5395 | perf_output_put(&handle, mmap_event->min); |
| 5396 | perf_output_put(&handle, mmap_event->ino); |
| 5397 | perf_output_put(&handle, mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5398 | perf_output_put(&handle, mmap_event->prot); |
| 5399 | perf_output_put(&handle, mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5400 | } |
| 5401 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5402 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5403 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5404 | |
| 5405 | perf_event__output_id_sample(event, &handle, &sample); |
| 5406 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5407 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5408 | out: |
| 5409 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5410 | } |
| 5411 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5412 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 5413 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5414 | struct vm_area_struct *vma = mmap_event->vma; |
| 5415 | struct file *file = vma->vm_file; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5416 | int maj = 0, min = 0; |
| 5417 | u64 ino = 0, gen = 0; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5418 | u32 prot = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5419 | unsigned int size; |
| 5420 | char tmp[16]; |
| 5421 | char *buf = NULL; |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5422 | char *name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5423 | |
| 5424 | if (file) { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5425 | struct inode *inode; |
| 5426 | dev_t dev; |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5427 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5428 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5429 | if (!buf) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5430 | name = "//enomem"; |
| 5431 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5432 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5433 | /* |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5434 | * d_path() works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5435 | * need to add enough zero bytes after the string to handle |
| 5436 | * the 64bit alignment we do later. |
| 5437 | */ |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5438 | name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5439 | if (IS_ERR(name)) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5440 | name = "//toolong"; |
| 5441 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5442 | } |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5443 | inode = file_inode(vma->vm_file); |
| 5444 | dev = inode->i_sb->s_dev; |
| 5445 | ino = inode->i_ino; |
| 5446 | gen = inode->i_generation; |
| 5447 | maj = MAJOR(dev); |
| 5448 | min = MINOR(dev); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5449 | |
| 5450 | if (vma->vm_flags & VM_READ) |
| 5451 | prot |= PROT_READ; |
| 5452 | if (vma->vm_flags & VM_WRITE) |
| 5453 | prot |= PROT_WRITE; |
| 5454 | if (vma->vm_flags & VM_EXEC) |
| 5455 | prot |= PROT_EXEC; |
| 5456 | |
| 5457 | if (vma->vm_flags & VM_MAYSHARE) |
| 5458 | flags = MAP_SHARED; |
| 5459 | else |
| 5460 | flags = MAP_PRIVATE; |
| 5461 | |
| 5462 | if (vma->vm_flags & VM_DENYWRITE) |
| 5463 | flags |= MAP_DENYWRITE; |
| 5464 | if (vma->vm_flags & VM_MAYEXEC) |
| 5465 | flags |= MAP_EXECUTABLE; |
| 5466 | if (vma->vm_flags & VM_LOCKED) |
| 5467 | flags |= MAP_LOCKED; |
| 5468 | if (vma->vm_flags & VM_HUGETLB) |
| 5469 | flags |= MAP_HUGETLB; |
| 5470 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5471 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5472 | } else { |
Jiri Olsa | fbe26ab | 2014-07-14 17:57:19 +0200 | [diff] [blame] | 5473 | if (vma->vm_ops && vma->vm_ops->name) { |
| 5474 | name = (char *) vma->vm_ops->name(vma); |
| 5475 | if (name) |
| 5476 | goto cpy_name; |
| 5477 | } |
| 5478 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5479 | name = (char *)arch_vma_name(vma); |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5480 | if (name) |
| 5481 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5482 | |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5483 | if (vma->vm_start <= vma->vm_mm->start_brk && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5484 | vma->vm_end >= vma->vm_mm->brk) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5485 | name = "[heap]"; |
| 5486 | goto cpy_name; |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5487 | } |
| 5488 | if (vma->vm_start <= vma->vm_mm->start_stack && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5489 | vma->vm_end >= vma->vm_mm->start_stack) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5490 | name = "[stack]"; |
| 5491 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5492 | } |
| 5493 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5494 | name = "//anon"; |
| 5495 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5496 | } |
| 5497 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5498 | cpy_name: |
| 5499 | strlcpy(tmp, name, sizeof(tmp)); |
| 5500 | name = tmp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5501 | got_name: |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5502 | /* |
| 5503 | * Since our buffer works in 8 byte units we need to align our string |
| 5504 | * size to a multiple of 8. However, we must guarantee the tail end is |
| 5505 | * zero'd out to avoid leaking random bits to userspace. |
| 5506 | */ |
| 5507 | size = strlen(name)+1; |
| 5508 | while (!IS_ALIGNED(size, sizeof(u64))) |
| 5509 | name[size++] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5510 | |
| 5511 | mmap_event->file_name = name; |
| 5512 | mmap_event->file_size = size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5513 | mmap_event->maj = maj; |
| 5514 | mmap_event->min = min; |
| 5515 | mmap_event->ino = ino; |
| 5516 | mmap_event->ino_generation = gen; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5517 | mmap_event->prot = prot; |
| 5518 | mmap_event->flags = flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5519 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 5520 | if (!(vma->vm_flags & VM_EXEC)) |
| 5521 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 5522 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5523 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 5524 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5525 | perf_event_aux(perf_event_mmap_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5526 | mmap_event, |
| 5527 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5528 | |
| 5529 | kfree(buf); |
| 5530 | } |
| 5531 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 5532 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5533 | { |
| 5534 | struct perf_mmap_event mmap_event; |
| 5535 | |
| 5536 | if (!atomic_read(&nr_mmap_events)) |
| 5537 | return; |
| 5538 | |
| 5539 | mmap_event = (struct perf_mmap_event){ |
| 5540 | .vma = vma, |
| 5541 | /* .file_name */ |
| 5542 | /* .file_size */ |
| 5543 | .event_id = { |
| 5544 | .header = { |
| 5545 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5546 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5547 | /* .size */ |
| 5548 | }, |
| 5549 | /* .pid */ |
| 5550 | /* .tid */ |
| 5551 | .start = vma->vm_start, |
| 5552 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 5553 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5554 | }, |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5555 | /* .maj (attr_mmap2 only) */ |
| 5556 | /* .min (attr_mmap2 only) */ |
| 5557 | /* .ino (attr_mmap2 only) */ |
| 5558 | /* .ino_generation (attr_mmap2 only) */ |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5559 | /* .prot (attr_mmap2 only) */ |
| 5560 | /* .flags (attr_mmap2 only) */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5561 | }; |
| 5562 | |
| 5563 | perf_event_mmap_event(&mmap_event); |
| 5564 | } |
| 5565 | |
| 5566 | /* |
| 5567 | * IRQ throttle logging |
| 5568 | */ |
| 5569 | |
| 5570 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 5571 | { |
| 5572 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5573 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5574 | int ret; |
| 5575 | |
| 5576 | struct { |
| 5577 | struct perf_event_header header; |
| 5578 | u64 time; |
| 5579 | u64 id; |
| 5580 | u64 stream_id; |
| 5581 | } throttle_event = { |
| 5582 | .header = { |
| 5583 | .type = PERF_RECORD_THROTTLE, |
| 5584 | .misc = 0, |
| 5585 | .size = sizeof(throttle_event), |
| 5586 | }, |
| 5587 | .time = perf_clock(), |
| 5588 | .id = primary_event_id(event), |
| 5589 | .stream_id = event->id, |
| 5590 | }; |
| 5591 | |
| 5592 | if (enable) |
| 5593 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 5594 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5595 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 5596 | |
| 5597 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5598 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5599 | if (ret) |
| 5600 | return; |
| 5601 | |
| 5602 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5603 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5604 | perf_output_end(&handle); |
| 5605 | } |
| 5606 | |
| 5607 | /* |
| 5608 | * Generic event overflow handling, sampling. |
| 5609 | */ |
| 5610 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5611 | static int __perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5612 | int throttle, struct perf_sample_data *data, |
| 5613 | struct pt_regs *regs) |
| 5614 | { |
| 5615 | int events = atomic_read(&event->event_limit); |
| 5616 | struct hw_perf_event *hwc = &event->hw; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5617 | u64 seq; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5618 | int ret = 0; |
| 5619 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 5620 | /* |
| 5621 | * Non-sampling counters might still use the PMI to fold short |
| 5622 | * hardware counters, ignore those. |
| 5623 | */ |
| 5624 | if (unlikely(!is_sampling_event(event))) |
| 5625 | return 0; |
| 5626 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5627 | seq = __this_cpu_read(perf_throttled_seq); |
| 5628 | if (seq != hwc->interrupts_seq) { |
| 5629 | hwc->interrupts_seq = seq; |
| 5630 | hwc->interrupts = 1; |
| 5631 | } else { |
| 5632 | hwc->interrupts++; |
| 5633 | if (unlikely(throttle |
| 5634 | && hwc->interrupts >= max_samples_per_tick)) { |
| 5635 | __this_cpu_inc(perf_throttled_count); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 5636 | hwc->interrupts = MAX_INTERRUPTS; |
| 5637 | perf_log_throttle(event, 0); |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 5638 | tick_nohz_full_kick(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5639 | ret = 1; |
| 5640 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5641 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5642 | |
| 5643 | if (event->attr.freq) { |
| 5644 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5645 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5646 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5647 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5648 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5649 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 5650 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5651 | } |
| 5652 | |
| 5653 | /* |
| 5654 | * XXX event_limit might not quite work as expected on inherited |
| 5655 | * events |
| 5656 | */ |
| 5657 | |
| 5658 | event->pending_kill = POLL_IN; |
| 5659 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 5660 | ret = 1; |
| 5661 | event->pending_kill = POLL_HUP; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5662 | event->pending_disable = 1; |
| 5663 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5664 | } |
| 5665 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5666 | if (event->overflow_handler) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5667 | event->overflow_handler(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5668 | else |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5669 | perf_event_output(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5670 | |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5671 | if (event->fasync && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5672 | event->pending_wakeup = 1; |
| 5673 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5674 | } |
| 5675 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5676 | return ret; |
| 5677 | } |
| 5678 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5679 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5680 | struct perf_sample_data *data, |
| 5681 | struct pt_regs *regs) |
| 5682 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5683 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5684 | } |
| 5685 | |
| 5686 | /* |
| 5687 | * Generic software event infrastructure |
| 5688 | */ |
| 5689 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5690 | struct swevent_htable { |
| 5691 | struct swevent_hlist *swevent_hlist; |
| 5692 | struct mutex hlist_mutex; |
| 5693 | int hlist_refcount; |
| 5694 | |
| 5695 | /* Recursion avoidance in each contexts */ |
| 5696 | int recursion[PERF_NR_CONTEXTS]; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5697 | |
| 5698 | /* Keeps track of cpu being initialized/exited */ |
| 5699 | bool online; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5700 | }; |
| 5701 | |
| 5702 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 5703 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5704 | /* |
| 5705 | * We directly increment event->count and keep a second value in |
| 5706 | * event->hw.period_left to count intervals. This period event |
| 5707 | * is kept in the range [-sample_period, 0] so that we can use the |
| 5708 | * sign as trigger. |
| 5709 | */ |
| 5710 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 5711 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5712 | { |
| 5713 | struct hw_perf_event *hwc = &event->hw; |
| 5714 | u64 period = hwc->last_period; |
| 5715 | u64 nr, offset; |
| 5716 | s64 old, val; |
| 5717 | |
| 5718 | hwc->last_period = hwc->sample_period; |
| 5719 | |
| 5720 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5721 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5722 | if (val < 0) |
| 5723 | return 0; |
| 5724 | |
| 5725 | nr = div64_u64(period + val, period); |
| 5726 | offset = nr * period; |
| 5727 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5728 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5729 | goto again; |
| 5730 | |
| 5731 | return nr; |
| 5732 | } |
| 5733 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5734 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5735 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5736 | struct pt_regs *regs) |
| 5737 | { |
| 5738 | struct hw_perf_event *hwc = &event->hw; |
| 5739 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5740 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5741 | if (!overflow) |
| 5742 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5743 | |
| 5744 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 5745 | return; |
| 5746 | |
| 5747 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5748 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5749 | data, regs)) { |
| 5750 | /* |
| 5751 | * We inhibit the overflow from happening when |
| 5752 | * hwc->interrupts == MAX_INTERRUPTS. |
| 5753 | */ |
| 5754 | break; |
| 5755 | } |
| 5756 | throttle = 1; |
| 5757 | } |
| 5758 | } |
| 5759 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5760 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5761 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5762 | struct pt_regs *regs) |
| 5763 | { |
| 5764 | struct hw_perf_event *hwc = &event->hw; |
| 5765 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5766 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5767 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5768 | if (!regs) |
| 5769 | return; |
| 5770 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5771 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5772 | return; |
| 5773 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 5774 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 5775 | data->period = nr; |
| 5776 | return perf_swevent_overflow(event, 1, data, regs); |
| 5777 | } else |
| 5778 | data->period = event->hw.last_period; |
| 5779 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5780 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5781 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5782 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5783 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5784 | return; |
| 5785 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5786 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5787 | } |
| 5788 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5789 | static int perf_exclude_event(struct perf_event *event, |
| 5790 | struct pt_regs *regs) |
| 5791 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5792 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 5793 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5794 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5795 | if (regs) { |
| 5796 | if (event->attr.exclude_user && user_mode(regs)) |
| 5797 | return 1; |
| 5798 | |
| 5799 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 5800 | return 1; |
| 5801 | } |
| 5802 | |
| 5803 | return 0; |
| 5804 | } |
| 5805 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5806 | static int perf_swevent_match(struct perf_event *event, |
| 5807 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5808 | u32 event_id, |
| 5809 | struct perf_sample_data *data, |
| 5810 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5811 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5812 | if (event->attr.type != type) |
| 5813 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5814 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5815 | if (event->attr.config != event_id) |
| 5816 | return 0; |
| 5817 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5818 | if (perf_exclude_event(event, regs)) |
| 5819 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5820 | |
| 5821 | return 1; |
| 5822 | } |
| 5823 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5824 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5825 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5826 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5827 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5828 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 5829 | } |
| 5830 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5831 | static inline struct hlist_head * |
| 5832 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5833 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5834 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5835 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5836 | return &hlist->heads[hash]; |
| 5837 | } |
| 5838 | |
| 5839 | /* For the read side: events when they trigger */ |
| 5840 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5841 | 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] | 5842 | { |
| 5843 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5844 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5845 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5846 | if (!hlist) |
| 5847 | return NULL; |
| 5848 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5849 | return __find_swevent_head(hlist, type, event_id); |
| 5850 | } |
| 5851 | |
| 5852 | /* For the event head insertion and removal in the hlist */ |
| 5853 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5854 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5855 | { |
| 5856 | struct swevent_hlist *hlist; |
| 5857 | u32 event_id = event->attr.config; |
| 5858 | u64 type = event->attr.type; |
| 5859 | |
| 5860 | /* |
| 5861 | * Event scheduling is always serialized against hlist allocation |
| 5862 | * and release. Which makes the protected version suitable here. |
| 5863 | * The context lock guarantees that. |
| 5864 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5865 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5866 | lockdep_is_held(&event->ctx->lock)); |
| 5867 | if (!hlist) |
| 5868 | return NULL; |
| 5869 | |
| 5870 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5871 | } |
| 5872 | |
| 5873 | 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] | 5874 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5875 | struct perf_sample_data *data, |
| 5876 | struct pt_regs *regs) |
| 5877 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 5878 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5879 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5880 | struct hlist_head *head; |
| 5881 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5882 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5883 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5884 | if (!head) |
| 5885 | goto end; |
| 5886 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5887 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5888 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5889 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5890 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5891 | end: |
| 5892 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5893 | } |
| 5894 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5895 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5896 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 5897 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5898 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5899 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5900 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 5901 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5902 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 5903 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5904 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 5905 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5906 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5907 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 5908 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5909 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5910 | 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] | 5911 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5912 | struct perf_sample_data data; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5913 | int rctx; |
| 5914 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5915 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5916 | rctx = perf_swevent_get_recursion_context(); |
| 5917 | if (rctx < 0) |
| 5918 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5919 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 5920 | perf_sample_data_init(&data, addr, 0); |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 5921 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5922 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 5923 | |
| 5924 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 5925 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5926 | } |
| 5927 | |
| 5928 | static void perf_swevent_read(struct perf_event *event) |
| 5929 | { |
| 5930 | } |
| 5931 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5932 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5933 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 5934 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5935 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5936 | struct hlist_head *head; |
| 5937 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5938 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5939 | hwc->last_period = hwc->sample_period; |
| 5940 | perf_swevent_set_period(event); |
| 5941 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5942 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5943 | hwc->state = !(flags & PERF_EF_START); |
| 5944 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5945 | head = find_swevent_head(swhash, event); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5946 | if (!head) { |
| 5947 | /* |
| 5948 | * We can race with cpu hotplug code. Do not |
| 5949 | * WARN if the cpu just got unplugged. |
| 5950 | */ |
| 5951 | WARN_ON_ONCE(swhash->online); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5952 | return -EINVAL; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5953 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5954 | |
| 5955 | hlist_add_head_rcu(&event->hlist_entry, head); |
| 5956 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5957 | return 0; |
| 5958 | } |
| 5959 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5960 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5961 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5962 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5963 | } |
| 5964 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5965 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5966 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5967 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5968 | } |
| 5969 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5970 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5971 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5972 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 5973 | } |
| 5974 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5975 | /* Deref the hlist from the update side */ |
| 5976 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5977 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5978 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5979 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 5980 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5981 | } |
| 5982 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5983 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5984 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5985 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5986 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 5987 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5988 | return; |
| 5989 | |
Andreea-Cristina Bernat | 70691d4 | 2014-08-22 16:26:05 +0300 | [diff] [blame] | 5990 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 5991 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | static void swevent_hlist_put_cpu(struct perf_event *event, int cpu) |
| 5995 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5996 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5997 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5998 | mutex_lock(&swhash->hlist_mutex); |
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 | if (!--swhash->hlist_refcount) |
| 6001 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6002 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6003 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6004 | } |
| 6005 | |
| 6006 | static void swevent_hlist_put(struct perf_event *event) |
| 6007 | { |
| 6008 | int cpu; |
| 6009 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6010 | for_each_possible_cpu(cpu) |
| 6011 | swevent_hlist_put_cpu(event, cpu); |
| 6012 | } |
| 6013 | |
| 6014 | static int swevent_hlist_get_cpu(struct perf_event *event, int cpu) |
| 6015 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6016 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6017 | int err = 0; |
| 6018 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6019 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6020 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6021 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6022 | struct swevent_hlist *hlist; |
| 6023 | |
| 6024 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 6025 | if (!hlist) { |
| 6026 | err = -ENOMEM; |
| 6027 | goto exit; |
| 6028 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6029 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6030 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6031 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 6032 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6033 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6034 | |
| 6035 | return err; |
| 6036 | } |
| 6037 | |
| 6038 | static int swevent_hlist_get(struct perf_event *event) |
| 6039 | { |
| 6040 | int err; |
| 6041 | int cpu, failed_cpu; |
| 6042 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6043 | get_online_cpus(); |
| 6044 | for_each_possible_cpu(cpu) { |
| 6045 | err = swevent_hlist_get_cpu(event, cpu); |
| 6046 | if (err) { |
| 6047 | failed_cpu = cpu; |
| 6048 | goto fail; |
| 6049 | } |
| 6050 | } |
| 6051 | put_online_cpus(); |
| 6052 | |
| 6053 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 6054 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6055 | for_each_possible_cpu(cpu) { |
| 6056 | if (cpu == failed_cpu) |
| 6057 | break; |
| 6058 | swevent_hlist_put_cpu(event, cpu); |
| 6059 | } |
| 6060 | |
| 6061 | put_online_cpus(); |
| 6062 | return err; |
| 6063 | } |
| 6064 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6065 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6066 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6067 | static void sw_perf_event_destroy(struct perf_event *event) |
| 6068 | { |
| 6069 | u64 event_id = event->attr.config; |
| 6070 | |
| 6071 | WARN_ON(event->parent); |
| 6072 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6073 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6074 | swevent_hlist_put(event); |
| 6075 | } |
| 6076 | |
| 6077 | static int perf_swevent_init(struct perf_event *event) |
| 6078 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 6079 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6080 | |
| 6081 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6082 | return -ENOENT; |
| 6083 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6084 | /* |
| 6085 | * no branch sampling for software events |
| 6086 | */ |
| 6087 | if (has_branch_stack(event)) |
| 6088 | return -EOPNOTSUPP; |
| 6089 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6090 | switch (event_id) { |
| 6091 | case PERF_COUNT_SW_CPU_CLOCK: |
| 6092 | case PERF_COUNT_SW_TASK_CLOCK: |
| 6093 | return -ENOENT; |
| 6094 | |
| 6095 | default: |
| 6096 | break; |
| 6097 | } |
| 6098 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 6099 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6100 | return -ENOENT; |
| 6101 | |
| 6102 | if (!event->parent) { |
| 6103 | int err; |
| 6104 | |
| 6105 | err = swevent_hlist_get(event); |
| 6106 | if (err) |
| 6107 | return err; |
| 6108 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6109 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6110 | event->destroy = sw_perf_event_destroy; |
| 6111 | } |
| 6112 | |
| 6113 | return 0; |
| 6114 | } |
| 6115 | |
| 6116 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6117 | .task_ctx_nr = perf_sw_context, |
| 6118 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6119 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6120 | .add = perf_swevent_add, |
| 6121 | .del = perf_swevent_del, |
| 6122 | .start = perf_swevent_start, |
| 6123 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6124 | .read = perf_swevent_read, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6125 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6126 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6127 | #ifdef CONFIG_EVENT_TRACING |
| 6128 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6129 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6130 | struct perf_sample_data *data) |
| 6131 | { |
| 6132 | void *record = data->raw->data; |
| 6133 | |
| 6134 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 6135 | return 1; |
| 6136 | return 0; |
| 6137 | } |
| 6138 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6139 | static int perf_tp_event_match(struct perf_event *event, |
| 6140 | struct perf_sample_data *data, |
| 6141 | struct pt_regs *regs) |
| 6142 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 6143 | if (event->hw.state & PERF_HES_STOPPED) |
| 6144 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 6145 | /* |
| 6146 | * All tracepoints are from kernel-space. |
| 6147 | */ |
| 6148 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6149 | return 0; |
| 6150 | |
| 6151 | if (!perf_tp_filter_match(event, data)) |
| 6152 | return 0; |
| 6153 | |
| 6154 | return 1; |
| 6155 | } |
| 6156 | |
| 6157 | 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] | 6158 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 6159 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6160 | { |
| 6161 | struct perf_sample_data data; |
| 6162 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6163 | |
| 6164 | struct perf_raw_record raw = { |
| 6165 | .size = entry_size, |
| 6166 | .data = record, |
| 6167 | }; |
| 6168 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6169 | perf_sample_data_init(&data, addr, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6170 | data.raw = &raw; |
| 6171 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6172 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6173 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6174 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6175 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 6176 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 6177 | /* |
| 6178 | * If we got specified a target task, also iterate its context and |
| 6179 | * deliver this event there too. |
| 6180 | */ |
| 6181 | if (task && task != current) { |
| 6182 | struct perf_event_context *ctx; |
| 6183 | struct trace_entry *entry = record; |
| 6184 | |
| 6185 | rcu_read_lock(); |
| 6186 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 6187 | if (!ctx) |
| 6188 | goto unlock; |
| 6189 | |
| 6190 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 6191 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6192 | continue; |
| 6193 | if (event->attr.config != entry->type) |
| 6194 | continue; |
| 6195 | if (perf_tp_event_match(event, &data, regs)) |
| 6196 | perf_swevent_event(event, count, &data, regs); |
| 6197 | } |
| 6198 | unlock: |
| 6199 | rcu_read_unlock(); |
| 6200 | } |
| 6201 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 6202 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6203 | } |
| 6204 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 6205 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6206 | static void tp_perf_event_destroy(struct perf_event *event) |
| 6207 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6208 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6209 | } |
| 6210 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6211 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6212 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6213 | int err; |
| 6214 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6215 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6216 | return -ENOENT; |
| 6217 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6218 | /* |
| 6219 | * no branch sampling for tracepoint events |
| 6220 | */ |
| 6221 | if (has_branch_stack(event)) |
| 6222 | return -EOPNOTSUPP; |
| 6223 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6224 | err = perf_trace_init(event); |
| 6225 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6226 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6227 | |
| 6228 | event->destroy = tp_perf_event_destroy; |
| 6229 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6230 | return 0; |
| 6231 | } |
| 6232 | |
| 6233 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6234 | .task_ctx_nr = perf_sw_context, |
| 6235 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6236 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6237 | .add = perf_trace_add, |
| 6238 | .del = perf_trace_del, |
| 6239 | .start = perf_swevent_start, |
| 6240 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6241 | .read = perf_swevent_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6242 | }; |
| 6243 | |
| 6244 | static inline void perf_tp_register(void) |
| 6245 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6246 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6247 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6248 | |
| 6249 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6250 | { |
| 6251 | char *filter_str; |
| 6252 | int ret; |
| 6253 | |
| 6254 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6255 | return -EINVAL; |
| 6256 | |
| 6257 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 6258 | if (IS_ERR(filter_str)) |
| 6259 | return PTR_ERR(filter_str); |
| 6260 | |
| 6261 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); |
| 6262 | |
| 6263 | kfree(filter_str); |
| 6264 | return ret; |
| 6265 | } |
| 6266 | |
| 6267 | static void perf_event_free_filter(struct perf_event *event) |
| 6268 | { |
| 6269 | ftrace_profile_free_filter(event); |
| 6270 | } |
| 6271 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6272 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6273 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6274 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6275 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6276 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6277 | |
| 6278 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6279 | { |
| 6280 | return -ENOENT; |
| 6281 | } |
| 6282 | |
| 6283 | static void perf_event_free_filter(struct perf_event *event) |
| 6284 | { |
| 6285 | } |
| 6286 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 6287 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6288 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6289 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6290 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6291 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6292 | struct perf_sample_data sample; |
| 6293 | struct pt_regs *regs = data; |
| 6294 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6295 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6296 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6297 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6298 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6299 | } |
| 6300 | #endif |
| 6301 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6302 | /* |
| 6303 | * hrtimer based swevent callback |
| 6304 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6305 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6306 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6307 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6308 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 6309 | struct perf_sample_data data; |
| 6310 | struct pt_regs *regs; |
| 6311 | struct perf_event *event; |
| 6312 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6313 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6314 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6315 | |
| 6316 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 6317 | return HRTIMER_NORESTART; |
| 6318 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6319 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6320 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6321 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6322 | regs = get_irq_regs(); |
| 6323 | |
| 6324 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 6325 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 6326 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6327 | ret = HRTIMER_NORESTART; |
| 6328 | } |
| 6329 | |
| 6330 | period = max_t(u64, 10000, event->hw.sample_period); |
| 6331 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 6332 | |
| 6333 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6334 | } |
| 6335 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6336 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6337 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6338 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6339 | s64 period; |
| 6340 | |
| 6341 | if (!is_sampling_event(event)) |
| 6342 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6343 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6344 | period = local64_read(&hwc->period_left); |
| 6345 | if (period) { |
| 6346 | if (period < 0) |
| 6347 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6348 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6349 | local64_set(&hwc->period_left, 0); |
| 6350 | } else { |
| 6351 | period = max_t(u64, 10000, hwc->sample_period); |
| 6352 | } |
| 6353 | __hrtimer_start_range_ns(&hwc->hrtimer, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6354 | ns_to_ktime(period), 0, |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 6355 | HRTIMER_MODE_REL_PINNED, 0); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6356 | } |
| 6357 | |
| 6358 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 6359 | { |
| 6360 | struct hw_perf_event *hwc = &event->hw; |
| 6361 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6362 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6363 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6364 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6365 | |
| 6366 | hrtimer_cancel(&hwc->hrtimer); |
| 6367 | } |
| 6368 | } |
| 6369 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6370 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 6371 | { |
| 6372 | struct hw_perf_event *hwc = &event->hw; |
| 6373 | |
| 6374 | if (!is_sampling_event(event)) |
| 6375 | return; |
| 6376 | |
| 6377 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 6378 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 6379 | |
| 6380 | /* |
| 6381 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 6382 | * mapping and avoid the whole period adjust feedback stuff. |
| 6383 | */ |
| 6384 | if (event->attr.freq) { |
| 6385 | long freq = event->attr.sample_freq; |
| 6386 | |
| 6387 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 6388 | hwc->sample_period = event->attr.sample_period; |
| 6389 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 6390 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6391 | event->attr.freq = 0; |
| 6392 | } |
| 6393 | } |
| 6394 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6395 | /* |
| 6396 | * Software event: cpu wall time clock |
| 6397 | */ |
| 6398 | |
| 6399 | static void cpu_clock_event_update(struct perf_event *event) |
| 6400 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6401 | s64 prev; |
| 6402 | u64 now; |
| 6403 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6404 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6405 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6406 | local64_add(now - prev, &event->count); |
| 6407 | } |
| 6408 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6409 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6410 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6411 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6412 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6413 | } |
| 6414 | |
| 6415 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 6416 | { |
| 6417 | perf_swevent_cancel_hrtimer(event); |
| 6418 | cpu_clock_event_update(event); |
| 6419 | } |
| 6420 | |
| 6421 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 6422 | { |
| 6423 | if (flags & PERF_EF_START) |
| 6424 | cpu_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6425 | |
| 6426 | return 0; |
| 6427 | } |
| 6428 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6429 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6430 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6431 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | static void cpu_clock_event_read(struct perf_event *event) |
| 6435 | { |
| 6436 | cpu_clock_event_update(event); |
| 6437 | } |
| 6438 | |
| 6439 | static int cpu_clock_event_init(struct perf_event *event) |
| 6440 | { |
| 6441 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6442 | return -ENOENT; |
| 6443 | |
| 6444 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 6445 | return -ENOENT; |
| 6446 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6447 | /* |
| 6448 | * no branch sampling for software events |
| 6449 | */ |
| 6450 | if (has_branch_stack(event)) |
| 6451 | return -EOPNOTSUPP; |
| 6452 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6453 | perf_swevent_init_hrtimer(event); |
| 6454 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6455 | return 0; |
| 6456 | } |
| 6457 | |
| 6458 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6459 | .task_ctx_nr = perf_sw_context, |
| 6460 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6461 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6462 | .add = cpu_clock_event_add, |
| 6463 | .del = cpu_clock_event_del, |
| 6464 | .start = cpu_clock_event_start, |
| 6465 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6466 | .read = cpu_clock_event_read, |
| 6467 | }; |
| 6468 | |
| 6469 | /* |
| 6470 | * Software event: task time clock |
| 6471 | */ |
| 6472 | |
| 6473 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 6474 | { |
| 6475 | u64 prev; |
| 6476 | s64 delta; |
| 6477 | |
| 6478 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6479 | delta = now - prev; |
| 6480 | local64_add(delta, &event->count); |
| 6481 | } |
| 6482 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6483 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6484 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6485 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6486 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6487 | } |
| 6488 | |
| 6489 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 6490 | { |
| 6491 | perf_swevent_cancel_hrtimer(event); |
| 6492 | task_clock_event_update(event, event->ctx->time); |
| 6493 | } |
| 6494 | |
| 6495 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 6496 | { |
| 6497 | if (flags & PERF_EF_START) |
| 6498 | task_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6499 | |
| 6500 | return 0; |
| 6501 | } |
| 6502 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6503 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6504 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6505 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6506 | } |
| 6507 | |
| 6508 | static void task_clock_event_read(struct perf_event *event) |
| 6509 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 6510 | u64 now = perf_clock(); |
| 6511 | u64 delta = now - event->ctx->timestamp; |
| 6512 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6513 | |
| 6514 | task_clock_event_update(event, time); |
| 6515 | } |
| 6516 | |
| 6517 | static int task_clock_event_init(struct perf_event *event) |
| 6518 | { |
| 6519 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6520 | return -ENOENT; |
| 6521 | |
| 6522 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 6523 | return -ENOENT; |
| 6524 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6525 | /* |
| 6526 | * no branch sampling for software events |
| 6527 | */ |
| 6528 | if (has_branch_stack(event)) |
| 6529 | return -EOPNOTSUPP; |
| 6530 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6531 | perf_swevent_init_hrtimer(event); |
| 6532 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6533 | return 0; |
| 6534 | } |
| 6535 | |
| 6536 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6537 | .task_ctx_nr = perf_sw_context, |
| 6538 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6539 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6540 | .add = task_clock_event_add, |
| 6541 | .del = task_clock_event_del, |
| 6542 | .start = task_clock_event_start, |
| 6543 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6544 | .read = task_clock_event_read, |
| 6545 | }; |
| 6546 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6547 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 6548 | { |
| 6549 | } |
| 6550 | |
| 6551 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 6552 | { |
| 6553 | return 0; |
| 6554 | } |
| 6555 | |
| 6556 | static void perf_pmu_start_txn(struct pmu *pmu) |
| 6557 | { |
| 6558 | perf_pmu_disable(pmu); |
| 6559 | } |
| 6560 | |
| 6561 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 6562 | { |
| 6563 | perf_pmu_enable(pmu); |
| 6564 | return 0; |
| 6565 | } |
| 6566 | |
| 6567 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 6568 | { |
| 6569 | perf_pmu_enable(pmu); |
| 6570 | } |
| 6571 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6572 | static int perf_event_idx_default(struct perf_event *event) |
| 6573 | { |
Peter Zijlstra | c719f56 | 2014-10-21 11:10:21 +0200 | [diff] [blame] | 6574 | return 0; |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6575 | } |
| 6576 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6577 | /* |
| 6578 | * Ensures all contexts with the same task_ctx_nr have the same |
| 6579 | * pmu_cpu_context too. |
| 6580 | */ |
Mark Rutland | 9e31704 | 2014-02-10 17:44:18 +0000 | [diff] [blame] | 6581 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6582 | { |
| 6583 | struct pmu *pmu; |
| 6584 | |
| 6585 | if (ctxn < 0) |
| 6586 | return NULL; |
| 6587 | |
| 6588 | list_for_each_entry(pmu, &pmus, entry) { |
| 6589 | if (pmu->task_ctx_nr == ctxn) |
| 6590 | return pmu->pmu_cpu_context; |
| 6591 | } |
| 6592 | |
| 6593 | return NULL; |
| 6594 | } |
| 6595 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6596 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6597 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6598 | int cpu; |
| 6599 | |
| 6600 | for_each_possible_cpu(cpu) { |
| 6601 | struct perf_cpu_context *cpuctx; |
| 6602 | |
| 6603 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6604 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6605 | if (cpuctx->unique_pmu == old_pmu) |
| 6606 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6607 | } |
| 6608 | } |
| 6609 | |
| 6610 | static void free_pmu_context(struct pmu *pmu) |
| 6611 | { |
| 6612 | struct pmu *i; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6613 | |
| 6614 | mutex_lock(&pmus_lock); |
| 6615 | /* |
| 6616 | * Like a real lame refcount. |
| 6617 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6618 | list_for_each_entry(i, &pmus, entry) { |
| 6619 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 6620 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6621 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6622 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6623 | } |
| 6624 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6625 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6626 | out: |
| 6627 | mutex_unlock(&pmus_lock); |
| 6628 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6629 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6630 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6631 | static ssize_t |
| 6632 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 6633 | { |
| 6634 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6635 | |
| 6636 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 6637 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6638 | static DEVICE_ATTR_RO(type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6639 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6640 | static ssize_t |
| 6641 | perf_event_mux_interval_ms_show(struct device *dev, |
| 6642 | struct device_attribute *attr, |
| 6643 | char *page) |
| 6644 | { |
| 6645 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6646 | |
| 6647 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 6648 | } |
| 6649 | |
| 6650 | static ssize_t |
| 6651 | perf_event_mux_interval_ms_store(struct device *dev, |
| 6652 | struct device_attribute *attr, |
| 6653 | const char *buf, size_t count) |
| 6654 | { |
| 6655 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6656 | int timer, cpu, ret; |
| 6657 | |
| 6658 | ret = kstrtoint(buf, 0, &timer); |
| 6659 | if (ret) |
| 6660 | return ret; |
| 6661 | |
| 6662 | if (timer < 1) |
| 6663 | return -EINVAL; |
| 6664 | |
| 6665 | /* same value, noting to do */ |
| 6666 | if (timer == pmu->hrtimer_interval_ms) |
| 6667 | return count; |
| 6668 | |
| 6669 | pmu->hrtimer_interval_ms = timer; |
| 6670 | |
| 6671 | /* update all cpuctx for this PMU */ |
| 6672 | for_each_possible_cpu(cpu) { |
| 6673 | struct perf_cpu_context *cpuctx; |
| 6674 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6675 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 6676 | |
| 6677 | if (hrtimer_active(&cpuctx->hrtimer)) |
| 6678 | hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval); |
| 6679 | } |
| 6680 | |
| 6681 | return count; |
| 6682 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6683 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6684 | |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6685 | static struct attribute *pmu_dev_attrs[] = { |
| 6686 | &dev_attr_type.attr, |
| 6687 | &dev_attr_perf_event_mux_interval_ms.attr, |
| 6688 | NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6689 | }; |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6690 | ATTRIBUTE_GROUPS(pmu_dev); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6691 | |
| 6692 | static int pmu_bus_running; |
| 6693 | static struct bus_type pmu_bus = { |
| 6694 | .name = "event_source", |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6695 | .dev_groups = pmu_dev_groups, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6696 | }; |
| 6697 | |
| 6698 | static void pmu_dev_release(struct device *dev) |
| 6699 | { |
| 6700 | kfree(dev); |
| 6701 | } |
| 6702 | |
| 6703 | static int pmu_dev_alloc(struct pmu *pmu) |
| 6704 | { |
| 6705 | int ret = -ENOMEM; |
| 6706 | |
| 6707 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 6708 | if (!pmu->dev) |
| 6709 | goto out; |
| 6710 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 6711 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6712 | device_initialize(pmu->dev); |
| 6713 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 6714 | if (ret) |
| 6715 | goto free_dev; |
| 6716 | |
| 6717 | dev_set_drvdata(pmu->dev, pmu); |
| 6718 | pmu->dev->bus = &pmu_bus; |
| 6719 | pmu->dev->release = pmu_dev_release; |
| 6720 | ret = device_add(pmu->dev); |
| 6721 | if (ret) |
| 6722 | goto free_dev; |
| 6723 | |
| 6724 | out: |
| 6725 | return ret; |
| 6726 | |
| 6727 | free_dev: |
| 6728 | put_device(pmu->dev); |
| 6729 | goto out; |
| 6730 | } |
| 6731 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6732 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6733 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6734 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 6735 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6736 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6737 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6738 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6739 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6740 | ret = -ENOMEM; |
| 6741 | pmu->pmu_disable_count = alloc_percpu(int); |
| 6742 | if (!pmu->pmu_disable_count) |
| 6743 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6744 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6745 | pmu->type = -1; |
| 6746 | if (!name) |
| 6747 | goto skip_type; |
| 6748 | pmu->name = name; |
| 6749 | |
| 6750 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 6751 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 6752 | if (type < 0) { |
| 6753 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6754 | goto free_pdc; |
| 6755 | } |
| 6756 | } |
| 6757 | pmu->type = type; |
| 6758 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6759 | if (pmu_bus_running) { |
| 6760 | ret = pmu_dev_alloc(pmu); |
| 6761 | if (ret) |
| 6762 | goto free_idr; |
| 6763 | } |
| 6764 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6765 | skip_type: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6766 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 6767 | if (pmu->pmu_cpu_context) |
| 6768 | goto got_cpu_context; |
| 6769 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 6770 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6771 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 6772 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6773 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6774 | |
| 6775 | for_each_possible_cpu(cpu) { |
| 6776 | struct perf_cpu_context *cpuctx; |
| 6777 | |
| 6778 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 6779 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6780 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6781 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 6782 | cpuctx->ctx.type = cpu_context; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6783 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 6784 | |
| 6785 | __perf_cpu_hrtimer_init(cpuctx, cpu); |
| 6786 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 6787 | INIT_LIST_HEAD(&cpuctx->rotation_list); |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6788 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6789 | } |
| 6790 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6791 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6792 | if (!pmu->start_txn) { |
| 6793 | if (pmu->pmu_enable) { |
| 6794 | /* |
| 6795 | * If we have pmu_enable/pmu_disable calls, install |
| 6796 | * transaction stubs that use that to try and batch |
| 6797 | * hardware accesses. |
| 6798 | */ |
| 6799 | pmu->start_txn = perf_pmu_start_txn; |
| 6800 | pmu->commit_txn = perf_pmu_commit_txn; |
| 6801 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 6802 | } else { |
| 6803 | pmu->start_txn = perf_pmu_nop_void; |
| 6804 | pmu->commit_txn = perf_pmu_nop_int; |
| 6805 | pmu->cancel_txn = perf_pmu_nop_void; |
| 6806 | } |
| 6807 | } |
| 6808 | |
| 6809 | if (!pmu->pmu_enable) { |
| 6810 | pmu->pmu_enable = perf_pmu_nop_void; |
| 6811 | pmu->pmu_disable = perf_pmu_nop_void; |
| 6812 | } |
| 6813 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6814 | if (!pmu->event_idx) |
| 6815 | pmu->event_idx = perf_event_idx_default; |
| 6816 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6817 | list_add_rcu(&pmu->entry, &pmus); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6818 | ret = 0; |
| 6819 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6820 | mutex_unlock(&pmus_lock); |
| 6821 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6822 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6823 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6824 | free_dev: |
| 6825 | device_del(pmu->dev); |
| 6826 | put_device(pmu->dev); |
| 6827 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6828 | free_idr: |
| 6829 | if (pmu->type >= PERF_TYPE_MAX) |
| 6830 | idr_remove(&pmu_idr, pmu->type); |
| 6831 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6832 | free_pdc: |
| 6833 | free_percpu(pmu->pmu_disable_count); |
| 6834 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6835 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6836 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6837 | |
| 6838 | void perf_pmu_unregister(struct pmu *pmu) |
| 6839 | { |
| 6840 | mutex_lock(&pmus_lock); |
| 6841 | list_del_rcu(&pmu->entry); |
| 6842 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6843 | |
| 6844 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6845 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 6846 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6847 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6848 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 6849 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6850 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6851 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6852 | if (pmu->type >= PERF_TYPE_MAX) |
| 6853 | idr_remove(&pmu_idr, pmu->type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6854 | device_del(pmu->dev); |
| 6855 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6856 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6857 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6858 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6859 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6860 | struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6861 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6862 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6863 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6864 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6865 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6866 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6867 | |
| 6868 | rcu_read_lock(); |
| 6869 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 6870 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6871 | if (pmu) { |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6872 | if (!try_module_get(pmu->module)) { |
| 6873 | pmu = ERR_PTR(-ENODEV); |
| 6874 | goto unlock; |
| 6875 | } |
Mark Rutland | 7e5b2a0 | 2011-08-11 12:31:20 +0100 | [diff] [blame] | 6876 | event->pmu = pmu; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6877 | ret = pmu->event_init(event); |
| 6878 | if (ret) |
| 6879 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6880 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6881 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6882 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6883 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 6884 | if (!try_module_get(pmu->module)) { |
| 6885 | pmu = ERR_PTR(-ENODEV); |
| 6886 | goto unlock; |
| 6887 | } |
Mark Rutland | 7e5b2a0 | 2011-08-11 12:31:20 +0100 | [diff] [blame] | 6888 | event->pmu = pmu; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 6889 | ret = pmu->event_init(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6890 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6891 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6892 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6893 | if (ret != -ENOENT) { |
| 6894 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6895 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6896 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6897 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 6898 | pmu = ERR_PTR(-ENOENT); |
| 6899 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6900 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6901 | |
| 6902 | return pmu; |
| 6903 | } |
| 6904 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6905 | static void account_event_cpu(struct perf_event *event, int cpu) |
| 6906 | { |
| 6907 | if (event->parent) |
| 6908 | return; |
| 6909 | |
| 6910 | if (has_branch_stack(event)) { |
| 6911 | if (!(event->attach_state & PERF_ATTACH_TASK)) |
| 6912 | atomic_inc(&per_cpu(perf_branch_stack_events, cpu)); |
| 6913 | } |
| 6914 | if (is_cgroup_event(event)) |
| 6915 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); |
| 6916 | } |
| 6917 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6918 | static void account_event(struct perf_event *event) |
| 6919 | { |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6920 | if (event->parent) |
| 6921 | return; |
| 6922 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6923 | if (event->attach_state & PERF_ATTACH_TASK) |
| 6924 | static_key_slow_inc(&perf_sched_events.key); |
| 6925 | if (event->attr.mmap || event->attr.mmap_data) |
| 6926 | atomic_inc(&nr_mmap_events); |
| 6927 | if (event->attr.comm) |
| 6928 | atomic_inc(&nr_comm_events); |
| 6929 | if (event->attr.task) |
| 6930 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 6931 | if (event->attr.freq) { |
| 6932 | if (atomic_inc_return(&nr_freq_events) == 1) |
| 6933 | tick_nohz_full_kick_all(); |
| 6934 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6935 | if (has_branch_stack(event)) |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6936 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6937 | if (is_cgroup_event(event)) |
| 6938 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6939 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 6940 | account_event_cpu(event, event->cpu); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 6941 | } |
| 6942 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6943 | /* |
| 6944 | * Allocate and initialize a event structure |
| 6945 | */ |
| 6946 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6947 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 6948 | struct task_struct *task, |
| 6949 | struct perf_event *group_leader, |
| 6950 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 6951 | perf_overflow_handler_t overflow_handler, |
| 6952 | void *context) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6953 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 6954 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6955 | struct perf_event *event; |
| 6956 | struct hw_perf_event *hwc; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 6957 | long err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6958 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 6959 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 6960 | if (!task || cpu != -1) |
| 6961 | return ERR_PTR(-EINVAL); |
| 6962 | } |
| 6963 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 6964 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6965 | if (!event) |
| 6966 | return ERR_PTR(-ENOMEM); |
| 6967 | |
| 6968 | /* |
| 6969 | * Single events are their own group leaders, with an |
| 6970 | * empty sibling list: |
| 6971 | */ |
| 6972 | if (!group_leader) |
| 6973 | group_leader = event; |
| 6974 | |
| 6975 | mutex_init(&event->child_mutex); |
| 6976 | INIT_LIST_HEAD(&event->child_list); |
| 6977 | |
| 6978 | INIT_LIST_HEAD(&event->group_entry); |
| 6979 | INIT_LIST_HEAD(&event->event_entry); |
| 6980 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 6981 | INIT_LIST_HEAD(&event->rb_entry); |
Stephane Eranian | 71ad88e | 2013-11-12 17:58:48 +0100 | [diff] [blame] | 6982 | INIT_LIST_HEAD(&event->active_entry); |
Stephane Eranian | f3ae75d | 2014-01-08 11:15:52 +0100 | [diff] [blame] | 6983 | INIT_HLIST_NODE(&event->hlist_entry); |
| 6984 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 6985 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6986 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 6987 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6988 | |
| 6989 | mutex_init(&event->mmap_mutex); |
| 6990 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 6991 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6992 | event->cpu = cpu; |
| 6993 | event->attr = *attr; |
| 6994 | event->group_leader = group_leader; |
| 6995 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6996 | event->oncpu = -1; |
| 6997 | |
| 6998 | event->parent = parent_event; |
| 6999 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 7000 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7001 | event->id = atomic64_inc_return(&perf_event_id); |
| 7002 | |
| 7003 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 7004 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7005 | if (task) { |
| 7006 | event->attach_state = PERF_ATTACH_TASK; |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 7007 | |
| 7008 | if (attr->type == PERF_TYPE_TRACEPOINT) |
| 7009 | event->hw.tp_target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7010 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
| 7011 | /* |
| 7012 | * hw_breakpoint is a bit difficult here.. |
| 7013 | */ |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 7014 | else if (attr->type == PERF_TYPE_BREAKPOINT) |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7015 | event->hw.bp_target = task; |
| 7016 | #endif |
| 7017 | } |
| 7018 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7019 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 7020 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7021 | context = parent_event->overflow_handler_context; |
| 7022 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 7023 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 7024 | event->overflow_handler = overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7025 | event->overflow_handler_context = context; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 7026 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7027 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7028 | |
| 7029 | pmu = NULL; |
| 7030 | |
| 7031 | hwc = &event->hw; |
| 7032 | hwc->sample_period = attr->sample_period; |
| 7033 | if (attr->freq && attr->sample_freq) |
| 7034 | hwc->sample_period = 1; |
| 7035 | hwc->last_period = hwc->sample_period; |
| 7036 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 7037 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7038 | |
| 7039 | /* |
| 7040 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 7041 | */ |
| 7042 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7043 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7044 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7045 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7046 | if (!pmu) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7047 | goto err_ns; |
| 7048 | else if (IS_ERR(pmu)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7049 | err = PTR_ERR(pmu); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7050 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7051 | } |
| 7052 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7053 | if (!event->parent) { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 7054 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 7055 | err = get_callchain_buffers(); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7056 | if (err) |
| 7057 | goto err_pmu; |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 7058 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7059 | } |
| 7060 | |
| 7061 | return event; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7062 | |
| 7063 | err_pmu: |
| 7064 | if (event->destroy) |
| 7065 | event->destroy(event); |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 7066 | module_put(pmu->module); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7067 | err_ns: |
| 7068 | if (event->ns) |
| 7069 | put_pid_ns(event->ns); |
| 7070 | kfree(event); |
| 7071 | |
| 7072 | return ERR_PTR(err); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7073 | } |
| 7074 | |
| 7075 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 7076 | struct perf_event_attr *attr) |
| 7077 | { |
| 7078 | u32 size; |
| 7079 | int ret; |
| 7080 | |
| 7081 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 7082 | return -EFAULT; |
| 7083 | |
| 7084 | /* |
| 7085 | * zero the full structure, so that a short copy will be nice. |
| 7086 | */ |
| 7087 | memset(attr, 0, sizeof(*attr)); |
| 7088 | |
| 7089 | ret = get_user(size, &uattr->size); |
| 7090 | if (ret) |
| 7091 | return ret; |
| 7092 | |
| 7093 | if (size > PAGE_SIZE) /* silly large */ |
| 7094 | goto err_size; |
| 7095 | |
| 7096 | if (!size) /* abi compat */ |
| 7097 | size = PERF_ATTR_SIZE_VER0; |
| 7098 | |
| 7099 | if (size < PERF_ATTR_SIZE_VER0) |
| 7100 | goto err_size; |
| 7101 | |
| 7102 | /* |
| 7103 | * If we're handed a bigger struct than we know of, |
| 7104 | * ensure all the unknown bits are 0 - i.e. new |
| 7105 | * user-space does not rely on any kernel feature |
| 7106 | * extensions we dont know about yet. |
| 7107 | */ |
| 7108 | if (size > sizeof(*attr)) { |
| 7109 | unsigned char __user *addr; |
| 7110 | unsigned char __user *end; |
| 7111 | unsigned char val; |
| 7112 | |
| 7113 | addr = (void __user *)uattr + sizeof(*attr); |
| 7114 | end = (void __user *)uattr + size; |
| 7115 | |
| 7116 | for (; addr < end; addr++) { |
| 7117 | ret = get_user(val, addr); |
| 7118 | if (ret) |
| 7119 | return ret; |
| 7120 | if (val) |
| 7121 | goto err_size; |
| 7122 | } |
| 7123 | size = sizeof(*attr); |
| 7124 | } |
| 7125 | |
| 7126 | ret = copy_from_user(attr, uattr, size); |
| 7127 | if (ret) |
| 7128 | return -EFAULT; |
| 7129 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 7130 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7131 | return -EINVAL; |
| 7132 | |
| 7133 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 7134 | return -EINVAL; |
| 7135 | |
| 7136 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 7137 | return -EINVAL; |
| 7138 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 7139 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 7140 | u64 mask = attr->branch_sample_type; |
| 7141 | |
| 7142 | /* only using defined bits */ |
| 7143 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 7144 | return -EINVAL; |
| 7145 | |
| 7146 | /* at least one branch bit must be set */ |
| 7147 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 7148 | return -EINVAL; |
| 7149 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 7150 | /* propagate priv level, when not set for branch */ |
| 7151 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 7152 | |
| 7153 | /* exclude_kernel checked on syscall entry */ |
| 7154 | if (!attr->exclude_kernel) |
| 7155 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 7156 | |
| 7157 | if (!attr->exclude_user) |
| 7158 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 7159 | |
| 7160 | if (!attr->exclude_hv) |
| 7161 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 7162 | /* |
| 7163 | * adjust user setting (for HW filter setup) |
| 7164 | */ |
| 7165 | attr->branch_sample_type = mask; |
| 7166 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 7167 | /* privileged levels capture (kernel, hv): check permissions */ |
| 7168 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 7169 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 7170 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 7171 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7172 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 7173 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7174 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 7175 | if (ret) |
| 7176 | return ret; |
| 7177 | } |
| 7178 | |
| 7179 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 7180 | if (!arch_perf_have_user_stack_dump()) |
| 7181 | return -ENOSYS; |
| 7182 | |
| 7183 | /* |
| 7184 | * We have __u32 type for the size, but so far |
| 7185 | * we can only use __u16 as maximum due to the |
| 7186 | * __u16 sample size limit. |
| 7187 | */ |
| 7188 | if (attr->sample_stack_user >= USHRT_MAX) |
| 7189 | ret = -EINVAL; |
| 7190 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
| 7191 | ret = -EINVAL; |
| 7192 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7193 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 7194 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
| 7195 | ret = perf_reg_validate(attr->sample_regs_intr); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7196 | out: |
| 7197 | return ret; |
| 7198 | |
| 7199 | err_size: |
| 7200 | put_user(sizeof(*attr), &uattr->size); |
| 7201 | ret = -E2BIG; |
| 7202 | goto out; |
| 7203 | } |
| 7204 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7205 | static int |
| 7206 | 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] | 7207 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7208 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7209 | int ret = -EINVAL; |
| 7210 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7211 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7212 | goto set; |
| 7213 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7214 | /* don't allow circular references */ |
| 7215 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7216 | goto out; |
| 7217 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 7218 | /* |
| 7219 | * Don't allow cross-cpu buffers |
| 7220 | */ |
| 7221 | if (output_event->cpu != event->cpu) |
| 7222 | goto out; |
| 7223 | |
| 7224 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7225 | * 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] | 7226 | */ |
| 7227 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 7228 | goto out; |
| 7229 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7230 | set: |
| 7231 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7232 | /* Can't redirect output if we've got an active mmap() */ |
| 7233 | if (atomic_read(&event->mmap_count)) |
| 7234 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7235 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7236 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7237 | /* get the rb we want to redirect to */ |
| 7238 | rb = ring_buffer_get(output_event); |
| 7239 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7240 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7241 | } |
| 7242 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7243 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 7244 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7245 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7246 | unlock: |
| 7247 | mutex_unlock(&event->mmap_mutex); |
| 7248 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7249 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7250 | return ret; |
| 7251 | } |
| 7252 | |
| 7253 | /** |
| 7254 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 7255 | * |
| 7256 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 7257 | * @pid: target pid |
| 7258 | * @cpu: target cpu |
| 7259 | * @group_fd: group leader event fd |
| 7260 | */ |
| 7261 | SYSCALL_DEFINE5(perf_event_open, |
| 7262 | struct perf_event_attr __user *, attr_uptr, |
| 7263 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 7264 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7265 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 7266 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7267 | struct perf_event_attr attr; |
| 7268 | struct perf_event_context *ctx; |
| 7269 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7270 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7271 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7272 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7273 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7274 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7275 | int err; |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7276 | int f_flags = O_RDWR; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7277 | |
| 7278 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7279 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7280 | return -EINVAL; |
| 7281 | |
| 7282 | err = perf_copy_attr(attr_uptr, &attr); |
| 7283 | if (err) |
| 7284 | return err; |
| 7285 | |
| 7286 | if (!attr.exclude_kernel) { |
| 7287 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 7288 | return -EACCES; |
| 7289 | } |
| 7290 | |
| 7291 | if (attr.freq) { |
| 7292 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 7293 | return -EINVAL; |
Peter Zijlstra | 0819b2e | 2014-05-15 20:23:48 +0200 | [diff] [blame] | 7294 | } else { |
| 7295 | if (attr.sample_period & (1ULL << 63)) |
| 7296 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7297 | } |
| 7298 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7299 | /* |
| 7300 | * In cgroup mode, the pid argument is used to pass the fd |
| 7301 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 7302 | * designates the cpu on which to monitor threads from that |
| 7303 | * cgroup. |
| 7304 | */ |
| 7305 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 7306 | return -EINVAL; |
| 7307 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7308 | if (flags & PERF_FLAG_FD_CLOEXEC) |
| 7309 | f_flags |= O_CLOEXEC; |
| 7310 | |
| 7311 | event_fd = get_unused_fd_flags(f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7312 | if (event_fd < 0) |
| 7313 | return event_fd; |
| 7314 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7315 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7316 | err = perf_fget_light(group_fd, &group); |
| 7317 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7318 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7319 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7320 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 7321 | output_event = group_leader; |
| 7322 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 7323 | group_leader = NULL; |
| 7324 | } |
| 7325 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7326 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7327 | task = find_lively_task_by_vpid(pid); |
| 7328 | if (IS_ERR(task)) { |
| 7329 | err = PTR_ERR(task); |
| 7330 | goto err_group_fd; |
| 7331 | } |
| 7332 | } |
| 7333 | |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7334 | if (task && group_leader && |
| 7335 | group_leader->attr.inherit != attr.inherit) { |
| 7336 | err = -EINVAL; |
| 7337 | goto err_task; |
| 7338 | } |
| 7339 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7340 | get_online_cpus(); |
| 7341 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7342 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
| 7343 | NULL, NULL); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7344 | if (IS_ERR(event)) { |
| 7345 | err = PTR_ERR(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7346 | goto err_cpus; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7347 | } |
| 7348 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7349 | if (flags & PERF_FLAG_PID_CGROUP) { |
| 7350 | err = perf_cgroup_connect(pid, event, &attr, group_leader); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7351 | if (err) { |
| 7352 | __free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7353 | goto err_cpus; |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7354 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7355 | } |
| 7356 | |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 7357 | if (is_sampling_event(event)) { |
| 7358 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { |
| 7359 | err = -ENOTSUPP; |
| 7360 | goto err_alloc; |
| 7361 | } |
| 7362 | } |
| 7363 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7364 | account_event(event); |
| 7365 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7366 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7367 | * Special case software events and allow them to be part of |
| 7368 | * any hardware group. |
| 7369 | */ |
| 7370 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7371 | |
| 7372 | if (group_leader && |
| 7373 | (is_software_event(event) != is_software_event(group_leader))) { |
| 7374 | if (is_software_event(event)) { |
| 7375 | /* |
| 7376 | * If event and group_leader are not both a software |
| 7377 | * event, and event is, then group leader is not. |
| 7378 | * |
| 7379 | * Allow the addition of software events to !software |
| 7380 | * groups, this is safe because software events never |
| 7381 | * fail to schedule. |
| 7382 | */ |
| 7383 | pmu = group_leader->pmu; |
| 7384 | } else if (is_software_event(group_leader) && |
| 7385 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 7386 | /* |
| 7387 | * In case the group is a pure software group, and we |
| 7388 | * try to add a hardware event, move the whole group to |
| 7389 | * the hardware context. |
| 7390 | */ |
| 7391 | move_group = 1; |
| 7392 | } |
| 7393 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7394 | |
| 7395 | /* |
| 7396 | * Get the target context (task or percpu): |
| 7397 | */ |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7398 | ctx = find_get_context(pmu, task, event->cpu); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7399 | if (IS_ERR(ctx)) { |
| 7400 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7401 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7402 | } |
| 7403 | |
Peter Zijlstra | fd1edb3 | 2011-03-28 13:13:56 +0200 | [diff] [blame] | 7404 | if (task) { |
| 7405 | put_task_struct(task); |
| 7406 | task = NULL; |
| 7407 | } |
| 7408 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7409 | /* |
| 7410 | * Look up the group leader (we will attach this event to it): |
| 7411 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7412 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7413 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7414 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7415 | /* |
| 7416 | * Do not allow a recursive hierarchy (this new sibling |
| 7417 | * becoming part of another group-sibling): |
| 7418 | */ |
| 7419 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7420 | goto err_context; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7421 | /* |
| 7422 | * Do not allow to attach to a group in a different |
| 7423 | * task or CPU context: |
| 7424 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7425 | if (move_group) { |
| 7426 | if (group_leader->ctx->type != ctx->type) |
| 7427 | goto err_context; |
| 7428 | } else { |
| 7429 | if (group_leader->ctx != ctx) |
| 7430 | goto err_context; |
| 7431 | } |
| 7432 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7433 | /* |
| 7434 | * Only a group leader can be exclusive or pinned |
| 7435 | */ |
| 7436 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7437 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7438 | } |
| 7439 | |
| 7440 | if (output_event) { |
| 7441 | err = perf_event_set_output(event, output_event); |
| 7442 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7443 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7444 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7445 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7446 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
| 7447 | f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7448 | if (IS_ERR(event_file)) { |
| 7449 | err = PTR_ERR(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7450 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7451 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7452 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7453 | if (move_group) { |
| 7454 | struct perf_event_context *gctx = group_leader->ctx; |
| 7455 | |
| 7456 | mutex_lock(&gctx->mutex); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7457 | perf_remove_from_context(group_leader, false); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7458 | |
| 7459 | /* |
| 7460 | * Removing from the context ends up with disabled |
| 7461 | * event. What we want here is event in the initial |
| 7462 | * startup state, ready to be add into new context. |
| 7463 | */ |
| 7464 | perf_event__state_init(group_leader); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7465 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7466 | group_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7467 | perf_remove_from_context(sibling, false); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7468 | perf_event__state_init(sibling); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7469 | put_ctx(gctx); |
| 7470 | } |
| 7471 | mutex_unlock(&gctx->mutex); |
| 7472 | put_ctx(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7473 | } |
| 7474 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7475 | WARN_ON_ONCE(ctx->parent_ctx); |
| 7476 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7477 | |
| 7478 | if (move_group) { |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7479 | synchronize_rcu(); |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7480 | perf_install_in_context(ctx, group_leader, event->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7481 | get_ctx(ctx); |
| 7482 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7483 | group_entry) { |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7484 | perf_install_in_context(ctx, sibling, event->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7485 | get_ctx(ctx); |
| 7486 | } |
| 7487 | } |
| 7488 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7489 | perf_install_in_context(ctx, event, event->cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7490 | perf_unpin_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7491 | mutex_unlock(&ctx->mutex); |
| 7492 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7493 | put_online_cpus(); |
| 7494 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7495 | event->owner = current; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7496 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7497 | mutex_lock(¤t->perf_event_mutex); |
| 7498 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 7499 | mutex_unlock(¤t->perf_event_mutex); |
| 7500 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7501 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7502 | * Precalculate sample_data sizes |
| 7503 | */ |
| 7504 | perf_event__header_size(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7505 | perf_event__id_header_size(event); |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7506 | |
| 7507 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7508 | * Drop the reference on the group_event after placing the |
| 7509 | * new event on the sibling_list. This ensures destruction |
| 7510 | * of the group leader will find the pointer to itself in |
| 7511 | * perf_group_detach(). |
| 7512 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7513 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7514 | fd_install(event_fd, event_file); |
| 7515 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7516 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7517 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7518 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7519 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7520 | err_alloc: |
| 7521 | free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7522 | err_cpus: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7523 | put_online_cpus(); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7524 | err_task: |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 7525 | if (task) |
| 7526 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7527 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7528 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7529 | err_fd: |
| 7530 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7531 | return err; |
| 7532 | } |
| 7533 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7534 | /** |
| 7535 | * perf_event_create_kernel_counter |
| 7536 | * |
| 7537 | * @attr: attributes of the counter to create |
| 7538 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7539 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7540 | */ |
| 7541 | struct perf_event * |
| 7542 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7543 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7544 | perf_overflow_handler_t overflow_handler, |
| 7545 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7546 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7547 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7548 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7549 | int err; |
| 7550 | |
| 7551 | /* |
| 7552 | * Get the target context (task or percpu): |
| 7553 | */ |
| 7554 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7555 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
| 7556 | overflow_handler, context); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7557 | if (IS_ERR(event)) { |
| 7558 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7559 | goto err; |
| 7560 | } |
| 7561 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 7562 | /* Mark owner so we could distinguish it from user events. */ |
| 7563 | event->owner = EVENT_OWNER_KERNEL; |
| 7564 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7565 | account_event(event); |
| 7566 | |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7567 | ctx = find_get_context(event->pmu, task, cpu); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7568 | if (IS_ERR(ctx)) { |
| 7569 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7570 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7571 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7572 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7573 | WARN_ON_ONCE(ctx->parent_ctx); |
| 7574 | mutex_lock(&ctx->mutex); |
| 7575 | perf_install_in_context(ctx, event, cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7576 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7577 | mutex_unlock(&ctx->mutex); |
| 7578 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7579 | return event; |
| 7580 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7581 | err_free: |
| 7582 | free_event(event); |
| 7583 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7584 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7585 | } |
| 7586 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 7587 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7588 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 7589 | { |
| 7590 | struct perf_event_context *src_ctx; |
| 7591 | struct perf_event_context *dst_ctx; |
| 7592 | struct perf_event *event, *tmp; |
| 7593 | LIST_HEAD(events); |
| 7594 | |
| 7595 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 7596 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 7597 | |
| 7598 | mutex_lock(&src_ctx->mutex); |
| 7599 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 7600 | event_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7601 | perf_remove_from_context(event, false); |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7602 | unaccount_event_cpu(event, src_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7603 | put_ctx(src_ctx); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7604 | list_add(&event->migrate_entry, &events); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7605 | } |
| 7606 | mutex_unlock(&src_ctx->mutex); |
| 7607 | |
| 7608 | synchronize_rcu(); |
| 7609 | |
| 7610 | mutex_lock(&dst_ctx->mutex); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7611 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 7612 | list_del(&event->migrate_entry); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7613 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 7614 | event->state = PERF_EVENT_STATE_INACTIVE; |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7615 | account_event_cpu(event, dst_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7616 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 7617 | get_ctx(dst_ctx); |
| 7618 | } |
| 7619 | mutex_unlock(&dst_ctx->mutex); |
| 7620 | } |
| 7621 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 7622 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7623 | static void sync_child_event(struct perf_event *child_event, |
| 7624 | struct task_struct *child) |
| 7625 | { |
| 7626 | struct perf_event *parent_event = child_event->parent; |
| 7627 | u64 child_val; |
| 7628 | |
| 7629 | if (child_event->attr.inherit_stat) |
| 7630 | perf_event_read_event(child_event, child); |
| 7631 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 7632 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7633 | |
| 7634 | /* |
| 7635 | * Add back the child's count to the parent's count: |
| 7636 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 7637 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7638 | atomic64_add(child_event->total_time_enabled, |
| 7639 | &parent_event->child_total_time_enabled); |
| 7640 | atomic64_add(child_event->total_time_running, |
| 7641 | &parent_event->child_total_time_running); |
| 7642 | |
| 7643 | /* |
| 7644 | * Remove this event from the parent's list |
| 7645 | */ |
| 7646 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7647 | mutex_lock(&parent_event->child_mutex); |
| 7648 | list_del_init(&child_event->child_list); |
| 7649 | mutex_unlock(&parent_event->child_mutex); |
| 7650 | |
| 7651 | /* |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 7652 | * Make sure user/parent get notified, that we just |
| 7653 | * lost one event. |
| 7654 | */ |
| 7655 | perf_event_wakeup(parent_event); |
| 7656 | |
| 7657 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7658 | * Release the parent event, if this was the last |
| 7659 | * reference to it. |
| 7660 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7661 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | static void |
| 7665 | __perf_event_exit_task(struct perf_event *child_event, |
| 7666 | struct perf_event_context *child_ctx, |
| 7667 | struct task_struct *child) |
| 7668 | { |
Peter Zijlstra | 1903d50 | 2014-07-15 17:27:27 +0200 | [diff] [blame] | 7669 | /* |
| 7670 | * Do not destroy the 'original' grouping; because of the context |
| 7671 | * switch optimization the original events could've ended up in a |
| 7672 | * random child task. |
| 7673 | * |
| 7674 | * If we were to destroy the original group, all group related |
| 7675 | * operations would cease to function properly after this random |
| 7676 | * child dies. |
| 7677 | * |
| 7678 | * Do destroy all inherited groups, we don't care about those |
| 7679 | * and being thorough is better. |
| 7680 | */ |
| 7681 | perf_remove_from_context(child_event, !!child_event->parent); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7682 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7683 | /* |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7684 | * It can happen that the parent exits first, and has events |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7685 | * that are still around due to the child reference. These |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7686 | * events need to be zapped. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7687 | */ |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7688 | if (child_event->parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7689 | sync_child_event(child_event, child); |
| 7690 | free_event(child_event); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 7691 | } else { |
| 7692 | child_event->state = PERF_EVENT_STATE_EXIT; |
| 7693 | perf_event_wakeup(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7694 | } |
| 7695 | } |
| 7696 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7697 | 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] | 7698 | { |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 7699 | struct perf_event *child_event, *next; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 7700 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7701 | unsigned long flags; |
| 7702 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7703 | if (likely(!child->perf_event_ctxp[ctxn])) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7704 | perf_event_task(child, NULL, 0); |
| 7705 | return; |
| 7706 | } |
| 7707 | |
| 7708 | local_irq_save(flags); |
| 7709 | /* |
| 7710 | * We can't reschedule here because interrupts are disabled, |
| 7711 | * and either child is current or it is a task that can't be |
| 7712 | * scheduled, so we are now safe from rescheduling changing |
| 7713 | * our context. |
| 7714 | */ |
Oleg Nesterov | 806839b | 2011-01-21 18:45:47 +0100 | [diff] [blame] | 7715 | child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7716 | |
| 7717 | /* |
| 7718 | * Take the context lock here so that if find_get_context is |
| 7719 | * reading child->perf_event_ctxp, we wait until it has |
| 7720 | * incremented the context's refcount before we do put_ctx below. |
| 7721 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7722 | raw_spin_lock(&child_ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 7723 | task_ctx_sched_out(child_ctx); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7724 | child->perf_event_ctxp[ctxn] = NULL; |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 7725 | |
| 7726 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7727 | * If this context is a clone; unclone it so it can't get |
| 7728 | * swapped to another process while we're removing all |
| 7729 | * the events from it. |
| 7730 | */ |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 7731 | clone_ctx = unclone_ctx(child_ctx); |
Peter Zijlstra | 5e942bb | 2009-11-23 11:37:26 +0100 | [diff] [blame] | 7732 | update_context_time(child_ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7733 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7734 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 7735 | if (clone_ctx) |
| 7736 | put_ctx(clone_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 7737 | |
| 7738 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7739 | * Report the task dead after unscheduling the events so that we |
| 7740 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 7741 | * get a few PERF_RECORD_READ events. |
| 7742 | */ |
| 7743 | perf_event_task(child, child_ctx, 0); |
| 7744 | |
| 7745 | /* |
| 7746 | * We can recurse on the same lock type through: |
| 7747 | * |
| 7748 | * __perf_event_exit_task() |
| 7749 | * sync_child_event() |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7750 | * put_event() |
| 7751 | * mutex_lock(&ctx->mutex) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7752 | * |
| 7753 | * But since its the parent context it won't be the same instance. |
| 7754 | */ |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 7755 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7756 | |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 7757 | 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] | 7758 | __perf_event_exit_task(child_event, child_ctx, child); |
| 7759 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7760 | mutex_unlock(&child_ctx->mutex); |
| 7761 | |
| 7762 | put_ctx(child_ctx); |
| 7763 | } |
| 7764 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7765 | /* |
| 7766 | * When a child task exits, feed back event values to parent events. |
| 7767 | */ |
| 7768 | void perf_event_exit_task(struct task_struct *child) |
| 7769 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7770 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7771 | int ctxn; |
| 7772 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7773 | mutex_lock(&child->perf_event_mutex); |
| 7774 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 7775 | owner_entry) { |
| 7776 | list_del_init(&event->owner_entry); |
| 7777 | |
| 7778 | /* |
| 7779 | * Ensure the list deletion is visible before we clear |
| 7780 | * the owner, closes a race against perf_release() where |
| 7781 | * we need to serialize on the owner->perf_event_mutex. |
| 7782 | */ |
| 7783 | smp_wmb(); |
| 7784 | event->owner = NULL; |
| 7785 | } |
| 7786 | mutex_unlock(&child->perf_event_mutex); |
| 7787 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7788 | for_each_task_context_nr(ctxn) |
| 7789 | perf_event_exit_task_context(child, ctxn); |
| 7790 | } |
| 7791 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7792 | static void perf_free_event(struct perf_event *event, |
| 7793 | struct perf_event_context *ctx) |
| 7794 | { |
| 7795 | struct perf_event *parent = event->parent; |
| 7796 | |
| 7797 | if (WARN_ON_ONCE(!parent)) |
| 7798 | return; |
| 7799 | |
| 7800 | mutex_lock(&parent->child_mutex); |
| 7801 | list_del_init(&event->child_list); |
| 7802 | mutex_unlock(&parent->child_mutex); |
| 7803 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7804 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7805 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7806 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7807 | list_del_event(event, ctx); |
| 7808 | free_event(event); |
| 7809 | } |
| 7810 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7811 | /* |
| 7812 | * free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7813 | * perf_event_init_task below, used by fork() in case of fail. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7814 | */ |
| 7815 | void perf_event_free_task(struct task_struct *task) |
| 7816 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7817 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7818 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7819 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7820 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7821 | for_each_task_context_nr(ctxn) { |
| 7822 | ctx = task->perf_event_ctxp[ctxn]; |
| 7823 | if (!ctx) |
| 7824 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7825 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7826 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7827 | again: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7828 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 7829 | group_entry) |
| 7830 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7831 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7832 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 7833 | group_entry) |
| 7834 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7835 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7836 | if (!list_empty(&ctx->pinned_groups) || |
| 7837 | !list_empty(&ctx->flexible_groups)) |
| 7838 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7839 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7840 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7841 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7842 | put_ctx(ctx); |
| 7843 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7844 | } |
| 7845 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 7846 | void perf_event_delayed_put(struct task_struct *task) |
| 7847 | { |
| 7848 | int ctxn; |
| 7849 | |
| 7850 | for_each_task_context_nr(ctxn) |
| 7851 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 7852 | } |
| 7853 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7854 | /* |
| 7855 | * inherit a event from parent task to child task: |
| 7856 | */ |
| 7857 | static struct perf_event * |
| 7858 | inherit_event(struct perf_event *parent_event, |
| 7859 | struct task_struct *parent, |
| 7860 | struct perf_event_context *parent_ctx, |
| 7861 | struct task_struct *child, |
| 7862 | struct perf_event *group_leader, |
| 7863 | struct perf_event_context *child_ctx) |
| 7864 | { |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 7865 | enum perf_event_active_state parent_state = parent_event->state; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7866 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7867 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7868 | |
| 7869 | /* |
| 7870 | * Instead of creating recursive hierarchies of events, |
| 7871 | * we link inherited events back to the original parent, |
| 7872 | * which has a filp for sure, which we use as the reference |
| 7873 | * count: |
| 7874 | */ |
| 7875 | if (parent_event->parent) |
| 7876 | parent_event = parent_event->parent; |
| 7877 | |
| 7878 | child_event = perf_event_alloc(&parent_event->attr, |
| 7879 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7880 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7881 | group_leader, parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7882 | NULL, NULL); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7883 | if (IS_ERR(child_event)) |
| 7884 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7885 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 7886 | if (is_orphaned_event(parent_event) || |
| 7887 | !atomic_long_inc_not_zero(&parent_event->refcount)) { |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7888 | free_event(child_event); |
| 7889 | return NULL; |
| 7890 | } |
| 7891 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7892 | get_ctx(child_ctx); |
| 7893 | |
| 7894 | /* |
| 7895 | * Make the child state follow the state of the parent event, |
| 7896 | * not its attr.disabled bit. We hold the parent's mutex, |
| 7897 | * so we won't race with perf_event_{en, dis}able_family. |
| 7898 | */ |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 7899 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7900 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 7901 | else |
| 7902 | child_event->state = PERF_EVENT_STATE_OFF; |
| 7903 | |
| 7904 | if (parent_event->attr.freq) { |
| 7905 | u64 sample_period = parent_event->hw.sample_period; |
| 7906 | struct hw_perf_event *hwc = &child_event->hw; |
| 7907 | |
| 7908 | hwc->sample_period = sample_period; |
| 7909 | hwc->last_period = sample_period; |
| 7910 | |
| 7911 | local64_set(&hwc->period_left, sample_period); |
| 7912 | } |
| 7913 | |
| 7914 | child_event->ctx = child_ctx; |
| 7915 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7916 | child_event->overflow_handler_context |
| 7917 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7918 | |
| 7919 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 7920 | * Precalculate sample_data sizes |
| 7921 | */ |
| 7922 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7923 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 7924 | |
| 7925 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7926 | * Link it up in the child's context: |
| 7927 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7928 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7929 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 7930 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7931 | |
| 7932 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 7933 | * Link this into the parent event's child list |
| 7934 | */ |
| 7935 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7936 | mutex_lock(&parent_event->child_mutex); |
| 7937 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 7938 | mutex_unlock(&parent_event->child_mutex); |
| 7939 | |
| 7940 | return child_event; |
| 7941 | } |
| 7942 | |
| 7943 | static int inherit_group(struct perf_event *parent_event, |
| 7944 | struct task_struct *parent, |
| 7945 | struct perf_event_context *parent_ctx, |
| 7946 | struct task_struct *child, |
| 7947 | struct perf_event_context *child_ctx) |
| 7948 | { |
| 7949 | struct perf_event *leader; |
| 7950 | struct perf_event *sub; |
| 7951 | struct perf_event *child_ctr; |
| 7952 | |
| 7953 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 7954 | child, NULL, child_ctx); |
| 7955 | if (IS_ERR(leader)) |
| 7956 | return PTR_ERR(leader); |
| 7957 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 7958 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 7959 | child, leader, child_ctx); |
| 7960 | if (IS_ERR(child_ctr)) |
| 7961 | return PTR_ERR(child_ctr); |
| 7962 | } |
| 7963 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7964 | } |
| 7965 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7966 | static int |
| 7967 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 7968 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7969 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7970 | int *inherited_all) |
| 7971 | { |
| 7972 | int ret; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7973 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7974 | |
| 7975 | if (!event->attr.inherit) { |
| 7976 | *inherited_all = 0; |
| 7977 | return 0; |
| 7978 | } |
| 7979 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7980 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7981 | if (!child_ctx) { |
| 7982 | /* |
| 7983 | * This is executed from the parent task context, so |
| 7984 | * inherit events that have been marked for cloning. |
| 7985 | * First allocate and initialize a context for the |
| 7986 | * child. |
| 7987 | */ |
| 7988 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 7989 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7990 | if (!child_ctx) |
| 7991 | return -ENOMEM; |
| 7992 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7993 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 7994 | } |
| 7995 | |
| 7996 | ret = inherit_group(event, parent, parent_ctx, |
| 7997 | child, child_ctx); |
| 7998 | |
| 7999 | if (ret) |
| 8000 | *inherited_all = 0; |
| 8001 | |
| 8002 | return ret; |
| 8003 | } |
| 8004 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8005 | /* |
| 8006 | * Initialize the perf_event context in task_struct |
| 8007 | */ |
Jiri Olsa | 985c8dc | 2014-06-24 10:20:24 +0200 | [diff] [blame] | 8008 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8009 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8010 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8011 | struct perf_event_context *cloned_ctx; |
| 8012 | struct perf_event *event; |
| 8013 | struct task_struct *parent = current; |
| 8014 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8015 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8016 | int ret = 0; |
| 8017 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8018 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8019 | return 0; |
| 8020 | |
| 8021 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8022 | * If the parent's context is a clone, pin it so it won't get |
| 8023 | * swapped under us. |
| 8024 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8025 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Peter Zijlstra | ffb4ef2 | 2014-05-05 19:12:20 +0200 | [diff] [blame] | 8026 | if (!parent_ctx) |
| 8027 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8028 | |
| 8029 | /* |
| 8030 | * No need to check if parent_ctx != NULL here; since we saw |
| 8031 | * it non-NULL earlier, the only reason for it to become NULL |
| 8032 | * is if we exit, and since we're currently in the middle of |
| 8033 | * a fork we can't be exiting at the same time. |
| 8034 | */ |
| 8035 | |
| 8036 | /* |
| 8037 | * Lock the parent list. No need to lock the child - not PID |
| 8038 | * hashed yet and not running, so nobody can access it. |
| 8039 | */ |
| 8040 | mutex_lock(&parent_ctx->mutex); |
| 8041 | |
| 8042 | /* |
| 8043 | * We dont have to disable NMIs - we are only looking at |
| 8044 | * the list, not manipulating it: |
| 8045 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8046 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8047 | ret = inherit_task_group(event, parent, parent_ctx, |
| 8048 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8049 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8050 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8051 | } |
| 8052 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8053 | /* |
| 8054 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 8055 | * to allocations, but we need to prevent rotation because |
| 8056 | * rotate_ctx() will change the list from interrupt context. |
| 8057 | */ |
| 8058 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 8059 | parent_ctx->rotate_disable = 1; |
| 8060 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 8061 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8062 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8063 | ret = inherit_task_group(event, parent, parent_ctx, |
| 8064 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8065 | if (ret) |
| 8066 | break; |
| 8067 | } |
| 8068 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8069 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 8070 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8071 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8072 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8073 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 8074 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8075 | /* |
| 8076 | * Mark the child context as a clone of the parent |
| 8077 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 8078 | * |
| 8079 | * Note that if the parent is a clone, the holding of |
| 8080 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8081 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 8082 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8083 | if (cloned_ctx) { |
| 8084 | child_ctx->parent_ctx = cloned_ctx; |
| 8085 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 8086 | } else { |
| 8087 | child_ctx->parent_ctx = parent_ctx; |
| 8088 | child_ctx->parent_gen = parent_ctx->generation; |
| 8089 | } |
| 8090 | get_ctx(child_ctx->parent_ctx); |
| 8091 | } |
| 8092 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 8093 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8094 | mutex_unlock(&parent_ctx->mutex); |
| 8095 | |
| 8096 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 8097 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8098 | |
| 8099 | return ret; |
| 8100 | } |
| 8101 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8102 | /* |
| 8103 | * Initialize the perf_event context in task_struct |
| 8104 | */ |
| 8105 | int perf_event_init_task(struct task_struct *child) |
| 8106 | { |
| 8107 | int ctxn, ret; |
| 8108 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 8109 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 8110 | mutex_init(&child->perf_event_mutex); |
| 8111 | INIT_LIST_HEAD(&child->perf_event_list); |
| 8112 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8113 | for_each_task_context_nr(ctxn) { |
| 8114 | ret = perf_event_init_context(child, ctxn); |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 8115 | if (ret) { |
| 8116 | perf_event_free_task(child); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8117 | return ret; |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 8118 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8119 | } |
| 8120 | |
| 8121 | return 0; |
| 8122 | } |
| 8123 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8124 | static void __init perf_event_init_all_cpus(void) |
| 8125 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8126 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8127 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8128 | |
| 8129 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8130 | swhash = &per_cpu(swevent_htable, cpu); |
| 8131 | mutex_init(&swhash->hlist_mutex); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 8132 | INIT_LIST_HEAD(&per_cpu(rotation_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8133 | } |
| 8134 | } |
| 8135 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 8136 | static void perf_event_init_cpu(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8137 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8138 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8139 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8140 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 8141 | swhash->online = true; |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 8142 | if (swhash->hlist_refcount > 0) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8143 | struct swevent_hlist *hlist; |
| 8144 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8145 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 8146 | WARN_ON(!hlist); |
| 8147 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8148 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8149 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8150 | } |
| 8151 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8152 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 8153 | static void perf_pmu_rotate_stop(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8154 | { |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 8155 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 8156 | |
| 8157 | WARN_ON(!irqs_disabled()); |
| 8158 | |
| 8159 | list_del_init(&cpuctx->rotation_list); |
| 8160 | } |
| 8161 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8162 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8163 | { |
Mark Rutland | 226424e | 2014-11-05 16:11:44 +0000 | [diff] [blame] | 8164 | struct remove_event re = { .detach_group = true }; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8165 | struct perf_event_context *ctx = __info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8166 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8167 | perf_pmu_rotate_stop(ctx->pmu); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 8168 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 8169 | rcu_read_lock(); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 8170 | list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry) |
| 8171 | __perf_remove_from_context(&re); |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 8172 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8173 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8174 | |
| 8175 | static void perf_event_exit_cpu_context(int cpu) |
| 8176 | { |
| 8177 | struct perf_event_context *ctx; |
| 8178 | struct pmu *pmu; |
| 8179 | int idx; |
| 8180 | |
| 8181 | idx = srcu_read_lock(&pmus_srcu); |
| 8182 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 8183 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8184 | |
| 8185 | mutex_lock(&ctx->mutex); |
| 8186 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 8187 | mutex_unlock(&ctx->mutex); |
| 8188 | } |
| 8189 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8190 | } |
| 8191 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8192 | static void perf_event_exit_cpu(int cpu) |
| 8193 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8194 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8195 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 8196 | perf_event_exit_cpu_context(cpu); |
| 8197 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8198 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 8199 | swhash->online = false; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8200 | swevent_hlist_release(swhash); |
| 8201 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8202 | } |
| 8203 | #else |
| 8204 | static inline void perf_event_exit_cpu(int cpu) { } |
| 8205 | #endif |
| 8206 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8207 | static int |
| 8208 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 8209 | { |
| 8210 | int cpu; |
| 8211 | |
| 8212 | for_each_online_cpu(cpu) |
| 8213 | perf_event_exit_cpu(cpu); |
| 8214 | |
| 8215 | return NOTIFY_OK; |
| 8216 | } |
| 8217 | |
| 8218 | /* |
| 8219 | * Run the perf reboot notifier at the very last possible moment so that |
| 8220 | * the generic watchdog code runs as long as possible. |
| 8221 | */ |
| 8222 | static struct notifier_block perf_reboot_notifier = { |
| 8223 | .notifier_call = perf_reboot, |
| 8224 | .priority = INT_MIN, |
| 8225 | }; |
| 8226 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 8227 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8228 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 8229 | { |
| 8230 | unsigned int cpu = (long)hcpu; |
| 8231 | |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 8232 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8233 | |
| 8234 | case CPU_UP_PREPARE: |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8235 | case CPU_DOWN_FAILED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8236 | perf_event_init_cpu(cpu); |
| 8237 | break; |
| 8238 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8239 | case CPU_UP_CANCELED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8240 | case CPU_DOWN_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8241 | perf_event_exit_cpu(cpu); |
| 8242 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8243 | default: |
| 8244 | break; |
| 8245 | } |
| 8246 | |
| 8247 | return NOTIFY_OK; |
| 8248 | } |
| 8249 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8250 | void __init perf_event_init(void) |
| 8251 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8252 | int ret; |
| 8253 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8254 | idr_init(&pmu_idr); |
| 8255 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8256 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8257 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8258 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 8259 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 8260 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8261 | perf_tp_register(); |
| 8262 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8263 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8264 | |
| 8265 | ret = init_hw_breakpoint(); |
| 8266 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 8267 | |
| 8268 | /* do not patch jump label more than once per second */ |
| 8269 | jump_label_rate_limit(&perf_sched_events, HZ); |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 8270 | |
| 8271 | /* |
| 8272 | * Build time assertion that we keep the data_head at the intended |
| 8273 | * location. IOW, validation we got the __reserved[] size right. |
| 8274 | */ |
| 8275 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 8276 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8277 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8278 | |
| 8279 | static int __init perf_event_sysfs_init(void) |
| 8280 | { |
| 8281 | struct pmu *pmu; |
| 8282 | int ret; |
| 8283 | |
| 8284 | mutex_lock(&pmus_lock); |
| 8285 | |
| 8286 | ret = bus_register(&pmu_bus); |
| 8287 | if (ret) |
| 8288 | goto unlock; |
| 8289 | |
| 8290 | list_for_each_entry(pmu, &pmus, entry) { |
| 8291 | if (!pmu->name || pmu->type < 0) |
| 8292 | continue; |
| 8293 | |
| 8294 | ret = pmu_dev_alloc(pmu); |
| 8295 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 8296 | } |
| 8297 | pmu_bus_running = 1; |
| 8298 | ret = 0; |
| 8299 | |
| 8300 | unlock: |
| 8301 | mutex_unlock(&pmus_lock); |
| 8302 | |
| 8303 | return ret; |
| 8304 | } |
| 8305 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8306 | |
| 8307 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8308 | static struct cgroup_subsys_state * |
| 8309 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8310 | { |
| 8311 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8312 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 8313 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8314 | if (!jc) |
| 8315 | return ERR_PTR(-ENOMEM); |
| 8316 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8317 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 8318 | if (!jc->info) { |
| 8319 | kfree(jc); |
| 8320 | return ERR_PTR(-ENOMEM); |
| 8321 | } |
| 8322 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8323 | return &jc->css; |
| 8324 | } |
| 8325 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8326 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8327 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8328 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
| 8329 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8330 | free_percpu(jc->info); |
| 8331 | kfree(jc); |
| 8332 | } |
| 8333 | |
| 8334 | static int __perf_cgroup_move(void *info) |
| 8335 | { |
| 8336 | struct task_struct *task = info; |
| 8337 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
| 8338 | return 0; |
| 8339 | } |
| 8340 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8341 | static void perf_cgroup_attach(struct cgroup_subsys_state *css, |
| 8342 | struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8343 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8344 | struct task_struct *task; |
| 8345 | |
Tejun Heo | 924f0d9a | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 8346 | cgroup_taskset_for_each(task, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8347 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8348 | } |
| 8349 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8350 | static void perf_cgroup_exit(struct cgroup_subsys_state *css, |
| 8351 | struct cgroup_subsys_state *old_css, |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 8352 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8353 | { |
| 8354 | /* |
| 8355 | * cgroup_exit() is called in the copy_process() failure path. |
| 8356 | * Ignore this case since the task hasn't ran yet, this avoids |
| 8357 | * trying to poke a half freed task state from generic code. |
| 8358 | */ |
| 8359 | if (!(task->flags & PF_EXITING)) |
| 8360 | return; |
| 8361 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8362 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8363 | } |
| 8364 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 8365 | struct cgroup_subsys perf_event_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 8366 | .css_alloc = perf_cgroup_css_alloc, |
| 8367 | .css_free = perf_cgroup_css_free, |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 8368 | .exit = perf_cgroup_exit, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8369 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8370 | }; |
| 8371 | #endif /* CONFIG_CGROUP_PERF */ |