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 | |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 617 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 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 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 875 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 876 | |
| 877 | /* |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 878 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
| 879 | * perf_event_task_tick() are fully serialized because they're strictly cpu |
| 880 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs |
| 881 | * disabled, while perf_event_task_tick is called from IRQ context. |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 882 | */ |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 883 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 884 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 885 | struct list_head *head = this_cpu_ptr(&active_ctx_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 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 889 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
| 890 | |
| 891 | list_add(&ctx->active_ctx_list, head); |
| 892 | } |
| 893 | |
| 894 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) |
| 895 | { |
| 896 | WARN_ON(!irqs_disabled()); |
| 897 | |
| 898 | WARN_ON(list_empty(&ctx->active_ctx_list)); |
| 899 | |
| 900 | list_del_init(&ctx->active_ctx_list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static void get_ctx(struct perf_event_context *ctx) |
| 904 | { |
| 905 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 906 | } |
| 907 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 908 | static void put_ctx(struct perf_event_context *ctx) |
| 909 | { |
| 910 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 911 | if (ctx->parent_ctx) |
| 912 | put_ctx(ctx->parent_ctx); |
| 913 | if (ctx->task) |
| 914 | put_task_struct(ctx->task); |
Lai Jiangshan | cb796ff | 2011-03-18 12:07:41 +0800 | [diff] [blame] | 915 | kfree_rcu(ctx, rcu_head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 919 | /* |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 920 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and |
| 921 | * perf_pmu_migrate_context() we need some magic. |
| 922 | * |
| 923 | * Those places that change perf_event::ctx will hold both |
| 924 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. |
| 925 | * |
| 926 | * Lock ordering is by mutex address. There is one other site where |
| 927 | * perf_event_context::mutex nests and that is put_event(). But remember that |
| 928 | * that is a parent<->child context relation, and migration does not affect |
| 929 | * children, therefore these two orderings should not interact. |
| 930 | * |
| 931 | * The change in perf_event::ctx does not affect children (as claimed above) |
| 932 | * because the sys_perf_event_open() case will install a new event and break |
| 933 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only |
| 934 | * concerned with cpuctx and that doesn't have children. |
| 935 | * |
| 936 | * The places that change perf_event::ctx will issue: |
| 937 | * |
| 938 | * perf_remove_from_context(); |
| 939 | * synchronize_rcu(); |
| 940 | * perf_install_in_context(); |
| 941 | * |
| 942 | * to affect the change. The remove_from_context() + synchronize_rcu() should |
| 943 | * quiesce the event, after which we can install it in the new location. This |
| 944 | * means that only external vectors (perf_fops, prctl) can perturb the event |
| 945 | * while in transit. Therefore all such accessors should also acquire |
| 946 | * perf_event_context::mutex to serialize against this. |
| 947 | * |
| 948 | * However; because event->ctx can change while we're waiting to acquire |
| 949 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() |
| 950 | * function. |
| 951 | * |
| 952 | * Lock order: |
| 953 | * task_struct::perf_event_mutex |
| 954 | * perf_event_context::mutex |
| 955 | * perf_event_context::lock |
| 956 | * perf_event::child_mutex; |
| 957 | * perf_event::mmap_mutex |
| 958 | * mmap_sem |
| 959 | */ |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 960 | static struct perf_event_context * |
| 961 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 962 | { |
| 963 | struct perf_event_context *ctx; |
| 964 | |
| 965 | again: |
| 966 | rcu_read_lock(); |
| 967 | ctx = ACCESS_ONCE(event->ctx); |
| 968 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
| 969 | rcu_read_unlock(); |
| 970 | goto again; |
| 971 | } |
| 972 | rcu_read_unlock(); |
| 973 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 974 | mutex_lock_nested(&ctx->mutex, nesting); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 975 | if (event->ctx != ctx) { |
| 976 | mutex_unlock(&ctx->mutex); |
| 977 | put_ctx(ctx); |
| 978 | goto again; |
| 979 | } |
| 980 | |
| 981 | return ctx; |
| 982 | } |
| 983 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 984 | static inline struct perf_event_context * |
| 985 | perf_event_ctx_lock(struct perf_event *event) |
| 986 | { |
| 987 | return perf_event_ctx_lock_nested(event, 0); |
| 988 | } |
| 989 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 990 | static void perf_event_ctx_unlock(struct perf_event *event, |
| 991 | struct perf_event_context *ctx) |
| 992 | { |
| 993 | mutex_unlock(&ctx->mutex); |
| 994 | put_ctx(ctx); |
| 995 | } |
| 996 | |
| 997 | /* |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 998 | * This must be done under the ctx->lock, such as to serialize against |
| 999 | * context_equiv(), therefore we cannot call put_ctx() since that might end up |
| 1000 | * calling scheduler related locks and ctx->lock nests inside those. |
| 1001 | */ |
| 1002 | static __must_check struct perf_event_context * |
| 1003 | unclone_ctx(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1004 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1005 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
| 1006 | |
| 1007 | lockdep_assert_held(&ctx->lock); |
| 1008 | |
| 1009 | if (parent_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1010 | ctx->parent_ctx = NULL; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1011 | ctx->generation++; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1012 | |
| 1013 | return parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1016 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 1017 | { |
| 1018 | /* |
| 1019 | * only top level events have the pid namespace they were created in |
| 1020 | */ |
| 1021 | if (event->parent) |
| 1022 | event = event->parent; |
| 1023 | |
| 1024 | return task_tgid_nr_ns(p, event->ns); |
| 1025 | } |
| 1026 | |
| 1027 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 1028 | { |
| 1029 | /* |
| 1030 | * only top level events have the pid namespace they were created in |
| 1031 | */ |
| 1032 | if (event->parent) |
| 1033 | event = event->parent; |
| 1034 | |
| 1035 | return task_pid_nr_ns(p, event->ns); |
| 1036 | } |
| 1037 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1038 | /* |
| 1039 | * If we inherit events we want to return the parent event id |
| 1040 | * to userspace. |
| 1041 | */ |
| 1042 | static u64 primary_event_id(struct perf_event *event) |
| 1043 | { |
| 1044 | u64 id = event->id; |
| 1045 | |
| 1046 | if (event->parent) |
| 1047 | id = event->parent->id; |
| 1048 | |
| 1049 | return id; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * Get the perf_event_context for a task and lock it. |
| 1054 | * This has to cope with with the fact that until it is locked, |
| 1055 | * the context could get moved to another task. |
| 1056 | */ |
| 1057 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1058 | 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] | 1059 | { |
| 1060 | struct perf_event_context *ctx; |
| 1061 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1062 | retry: |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1063 | /* |
| 1064 | * One of the few rules of preemptible RCU is that one cannot do |
| 1065 | * rcu_read_unlock() while holding a scheduler (or nested) lock when |
| 1066 | * part of the read side critical section was preemptible -- see |
| 1067 | * rcu_read_unlock_special(). |
| 1068 | * |
| 1069 | * Since ctx->lock nests under rq->lock we must ensure the entire read |
| 1070 | * side critical section is non-preemptible. |
| 1071 | */ |
| 1072 | preempt_disable(); |
| 1073 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1074 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1075 | if (ctx) { |
| 1076 | /* |
| 1077 | * If this context is a clone of another, it might |
| 1078 | * get swapped for another underneath us by |
| 1079 | * perf_event_task_sched_out, though the |
| 1080 | * rcu_read_lock() protects us from any context |
| 1081 | * getting freed. Lock the context and check if it |
| 1082 | * got swapped before we could get the lock, and retry |
| 1083 | * if so. If we locked the right context, then it |
| 1084 | * can't get swapped on us any more. |
| 1085 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1086 | raw_spin_lock_irqsave(&ctx->lock, *flags); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1087 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1088 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1089 | rcu_read_unlock(); |
| 1090 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1091 | goto retry; |
| 1092 | } |
| 1093 | |
| 1094 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1095 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1096 | ctx = NULL; |
| 1097 | } |
| 1098 | } |
| 1099 | rcu_read_unlock(); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1100 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1101 | return ctx; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * Get the context for a task and increment its pin_count so it |
| 1106 | * can't get swapped to another task. This also increments its |
| 1107 | * reference count so that the context can't get freed. |
| 1108 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1109 | static struct perf_event_context * |
| 1110 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1111 | { |
| 1112 | struct perf_event_context *ctx; |
| 1113 | unsigned long flags; |
| 1114 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1115 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1116 | if (ctx) { |
| 1117 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1118 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1119 | } |
| 1120 | return ctx; |
| 1121 | } |
| 1122 | |
| 1123 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 1124 | { |
| 1125 | unsigned long flags; |
| 1126 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1127 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1128 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1129 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1132 | /* |
| 1133 | * Update the record of the current time in a context. |
| 1134 | */ |
| 1135 | static void update_context_time(struct perf_event_context *ctx) |
| 1136 | { |
| 1137 | u64 now = perf_clock(); |
| 1138 | |
| 1139 | ctx->time += now - ctx->timestamp; |
| 1140 | ctx->timestamp = now; |
| 1141 | } |
| 1142 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1143 | static u64 perf_event_time(struct perf_event *event) |
| 1144 | { |
| 1145 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1146 | |
| 1147 | if (is_cgroup_event(event)) |
| 1148 | return perf_cgroup_event_time(event); |
| 1149 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1150 | return ctx ? ctx->time : 0; |
| 1151 | } |
| 1152 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1153 | /* |
| 1154 | * 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] | 1155 | * The caller of this function needs to hold the ctx->lock. |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1156 | */ |
| 1157 | static void update_event_times(struct perf_event *event) |
| 1158 | { |
| 1159 | struct perf_event_context *ctx = event->ctx; |
| 1160 | u64 run_end; |
| 1161 | |
| 1162 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1163 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1164 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1165 | /* |
| 1166 | * in cgroup mode, time_enabled represents |
| 1167 | * the time the event was enabled AND active |
| 1168 | * tasks were in the monitored cgroup. This is |
| 1169 | * independent of the activity of the context as |
| 1170 | * there may be a mix of cgroup and non-cgroup events. |
| 1171 | * |
| 1172 | * That is why we treat cgroup events differently |
| 1173 | * here. |
| 1174 | */ |
| 1175 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1176 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1177 | else if (ctx->is_active) |
| 1178 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1179 | else |
| 1180 | run_end = event->tstamp_stopped; |
| 1181 | |
| 1182 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1183 | |
| 1184 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1185 | run_end = event->tstamp_stopped; |
| 1186 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1187 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1188 | |
| 1189 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1190 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1193 | /* |
| 1194 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1195 | */ |
| 1196 | static void update_group_times(struct perf_event *leader) |
| 1197 | { |
| 1198 | struct perf_event *event; |
| 1199 | |
| 1200 | update_event_times(leader); |
| 1201 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1202 | update_event_times(event); |
| 1203 | } |
| 1204 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1205 | static struct list_head * |
| 1206 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1207 | { |
| 1208 | if (event->attr.pinned) |
| 1209 | return &ctx->pinned_groups; |
| 1210 | else |
| 1211 | return &ctx->flexible_groups; |
| 1212 | } |
| 1213 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1214 | /* |
| 1215 | * Add a event from the lists for its context. |
| 1216 | * Must be called with ctx->mutex and ctx->lock held. |
| 1217 | */ |
| 1218 | static void |
| 1219 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1220 | { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1221 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1222 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1223 | |
| 1224 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1225 | * If we're a stand alone event or group leader, we go to the context |
| 1226 | * list, group events are kept attached to the group so that |
| 1227 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1228 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1229 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1230 | struct list_head *list; |
| 1231 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1232 | if (is_software_event(event)) |
| 1233 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 1234 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1235 | list = ctx_group_list(event, ctx); |
| 1236 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1237 | } |
| 1238 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 1239 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1240 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1241 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1242 | if (has_branch_stack(event)) |
| 1243 | ctx->nr_branch_stack++; |
| 1244 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1245 | list_add_rcu(&event->event_entry, &ctx->event_list); |
| 1246 | ctx->nr_events++; |
| 1247 | if (event->attr.inherit_stat) |
| 1248 | ctx->nr_stat++; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1249 | |
| 1250 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1253 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1254 | * Initialize event state based on the perf_event_attr::disabled. |
| 1255 | */ |
| 1256 | static inline void perf_event__state_init(struct perf_event *event) |
| 1257 | { |
| 1258 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1259 | PERF_EVENT_STATE_INACTIVE; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1263 | * Called at perf_event creation and when events are attached/detached from a |
| 1264 | * group. |
| 1265 | */ |
| 1266 | static void perf_event__read_size(struct perf_event *event) |
| 1267 | { |
| 1268 | int entry = sizeof(u64); /* value */ |
| 1269 | int size = 0; |
| 1270 | int nr = 1; |
| 1271 | |
| 1272 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1273 | size += sizeof(u64); |
| 1274 | |
| 1275 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1276 | size += sizeof(u64); |
| 1277 | |
| 1278 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1279 | entry += sizeof(u64); |
| 1280 | |
| 1281 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
| 1282 | nr += event->group_leader->nr_siblings; |
| 1283 | size += sizeof(u64); |
| 1284 | } |
| 1285 | |
| 1286 | size += entry * nr; |
| 1287 | event->read_size = size; |
| 1288 | } |
| 1289 | |
| 1290 | static void perf_event__header_size(struct perf_event *event) |
| 1291 | { |
| 1292 | struct perf_sample_data *data; |
| 1293 | u64 sample_type = event->attr.sample_type; |
| 1294 | u16 size = 0; |
| 1295 | |
| 1296 | perf_event__read_size(event); |
| 1297 | |
| 1298 | if (sample_type & PERF_SAMPLE_IP) |
| 1299 | size += sizeof(data->ip); |
| 1300 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1301 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1302 | size += sizeof(data->addr); |
| 1303 | |
| 1304 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1305 | size += sizeof(data->period); |
| 1306 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1307 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1308 | size += sizeof(data->weight); |
| 1309 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1310 | if (sample_type & PERF_SAMPLE_READ) |
| 1311 | size += event->read_size; |
| 1312 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1313 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1314 | size += sizeof(data->data_src.val); |
| 1315 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 1316 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1317 | size += sizeof(data->txn); |
| 1318 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1319 | event->header_size = size; |
| 1320 | } |
| 1321 | |
| 1322 | static void perf_event__id_header_size(struct perf_event *event) |
| 1323 | { |
| 1324 | struct perf_sample_data *data; |
| 1325 | u64 sample_type = event->attr.sample_type; |
| 1326 | u16 size = 0; |
| 1327 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1328 | if (sample_type & PERF_SAMPLE_TID) |
| 1329 | size += sizeof(data->tid_entry); |
| 1330 | |
| 1331 | if (sample_type & PERF_SAMPLE_TIME) |
| 1332 | size += sizeof(data->time); |
| 1333 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 1334 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1335 | size += sizeof(data->id); |
| 1336 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1337 | if (sample_type & PERF_SAMPLE_ID) |
| 1338 | size += sizeof(data->id); |
| 1339 | |
| 1340 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1341 | size += sizeof(data->stream_id); |
| 1342 | |
| 1343 | if (sample_type & PERF_SAMPLE_CPU) |
| 1344 | size += sizeof(data->cpu_entry); |
| 1345 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1346 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1349 | static void perf_group_attach(struct perf_event *event) |
| 1350 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1351 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1352 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1353 | /* |
| 1354 | * We can have double attach due to group movement in perf_event_open. |
| 1355 | */ |
| 1356 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1357 | return; |
| 1358 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1359 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1360 | |
| 1361 | if (group_leader == event) |
| 1362 | return; |
| 1363 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1364 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
| 1365 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1366 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 1367 | !is_software_event(event)) |
| 1368 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 1369 | |
| 1370 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1371 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1372 | |
| 1373 | perf_event__header_size(group_leader); |
| 1374 | |
| 1375 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1376 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1377 | } |
| 1378 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1379 | /* |
| 1380 | * Remove a event from the lists for its context. |
| 1381 | * Must be called with ctx->mutex and ctx->lock held. |
| 1382 | */ |
| 1383 | static void |
| 1384 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1385 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1386 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1387 | |
| 1388 | WARN_ON_ONCE(event->ctx != ctx); |
| 1389 | lockdep_assert_held(&ctx->lock); |
| 1390 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1391 | /* |
| 1392 | * We can have double detach due to exit/hot-unplug + close. |
| 1393 | */ |
| 1394 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1395 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1396 | |
| 1397 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1398 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1399 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1400 | ctx->nr_cgroups--; |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1401 | cpuctx = __get_cpu_context(ctx); |
| 1402 | /* |
| 1403 | * if there are no more cgroup events |
| 1404 | * then cler cgrp to avoid stale pointer |
| 1405 | * in update_cgrp_time_from_cpuctx() |
| 1406 | */ |
| 1407 | if (!ctx->nr_cgroups) |
| 1408 | cpuctx->cgrp = NULL; |
| 1409 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1410 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 1411 | if (has_branch_stack(event)) |
| 1412 | ctx->nr_branch_stack--; |
| 1413 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1414 | ctx->nr_events--; |
| 1415 | if (event->attr.inherit_stat) |
| 1416 | ctx->nr_stat--; |
| 1417 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1418 | list_del_rcu(&event->event_entry); |
| 1419 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1420 | if (event->group_leader == event) |
| 1421 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1422 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1423 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1424 | |
| 1425 | /* |
| 1426 | * If event was in error state, then keep it |
| 1427 | * that way, otherwise bogus counts will be |
| 1428 | * returned on read(). The only way to get out |
| 1429 | * of error state is by explicit re-enabling |
| 1430 | * of the event |
| 1431 | */ |
| 1432 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1433 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1434 | |
| 1435 | ctx->generation++; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1436 | } |
| 1437 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1438 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1439 | { |
| 1440 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1441 | struct list_head *list = NULL; |
| 1442 | |
| 1443 | /* |
| 1444 | * We can have double detach due to exit/hot-unplug + close. |
| 1445 | */ |
| 1446 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1447 | return; |
| 1448 | |
| 1449 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1450 | |
| 1451 | /* |
| 1452 | * If this is a sibling, remove it from its group. |
| 1453 | */ |
| 1454 | if (event->group_leader != event) { |
| 1455 | list_del_init(&event->group_entry); |
| 1456 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1457 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | if (!list_empty(&event->group_entry)) |
| 1461 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1462 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1463 | /* |
| 1464 | * If this was a group event with sibling events then |
| 1465 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1466 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1467 | */ |
| 1468 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1469 | if (list) |
| 1470 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1471 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1472 | |
| 1473 | /* Inherit group flags from the previous leader */ |
| 1474 | sibling->group_flags = event->group_flags; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1475 | |
| 1476 | WARN_ON_ONCE(sibling->ctx != event->ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1477 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1478 | |
| 1479 | out: |
| 1480 | perf_event__header_size(event->group_leader); |
| 1481 | |
| 1482 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1483 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1484 | } |
| 1485 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1486 | /* |
| 1487 | * User event without the task. |
| 1488 | */ |
| 1489 | static bool is_orphaned_event(struct perf_event *event) |
| 1490 | { |
| 1491 | return event && !is_kernel_event(event) && !event->owner; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | * Event has a parent but parent's task finished and it's |
| 1496 | * alive only because of children holding refference. |
| 1497 | */ |
| 1498 | static bool is_orphaned_child(struct perf_event *event) |
| 1499 | { |
| 1500 | return is_orphaned_event(event->parent); |
| 1501 | } |
| 1502 | |
| 1503 | static void orphans_remove_work(struct work_struct *work); |
| 1504 | |
| 1505 | static void schedule_orphans_remove(struct perf_event_context *ctx) |
| 1506 | { |
| 1507 | if (!ctx->task || ctx->orphans_remove_sched || !perf_wq) |
| 1508 | return; |
| 1509 | |
| 1510 | if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) { |
| 1511 | get_ctx(ctx); |
| 1512 | ctx->orphans_remove_sched = true; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | static int __init perf_workqueue_init(void) |
| 1517 | { |
| 1518 | perf_wq = create_singlethread_workqueue("perf"); |
| 1519 | WARN(!perf_wq, "failed to create perf workqueue\n"); |
| 1520 | return perf_wq ? 0 : -1; |
| 1521 | } |
| 1522 | |
| 1523 | core_initcall(perf_workqueue_init); |
| 1524 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1525 | static inline int |
| 1526 | event_filter_match(struct perf_event *event) |
| 1527 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1528 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
| 1529 | && perf_cgroup_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1530 | } |
| 1531 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1532 | static void |
| 1533 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1534 | struct perf_cpu_context *cpuctx, |
| 1535 | struct perf_event_context *ctx) |
| 1536 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1537 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1538 | u64 delta; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1539 | |
| 1540 | WARN_ON_ONCE(event->ctx != ctx); |
| 1541 | lockdep_assert_held(&ctx->lock); |
| 1542 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1543 | /* |
| 1544 | * An event which could not be activated because of |
| 1545 | * filter mismatch still needs to have its timings |
| 1546 | * maintained, otherwise bogus information is return |
| 1547 | * via read() for time_enabled, time_running: |
| 1548 | */ |
| 1549 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1550 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1551 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1552 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1553 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1554 | } |
| 1555 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1556 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1557 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1558 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1559 | perf_pmu_disable(event->pmu); |
| 1560 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1561 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1562 | if (event->pending_disable) { |
| 1563 | event->pending_disable = 0; |
| 1564 | event->state = PERF_EVENT_STATE_OFF; |
| 1565 | } |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1566 | event->tstamp_stopped = tstamp; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1567 | event->pmu->del(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1568 | event->oncpu = -1; |
| 1569 | |
| 1570 | if (!is_software_event(event)) |
| 1571 | cpuctx->active_oncpu--; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1572 | if (!--ctx->nr_active) |
| 1573 | perf_event_ctx_deactivate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1574 | if (event->attr.freq && event->attr.sample_freq) |
| 1575 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1576 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1577 | cpuctx->exclusive = 0; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1578 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1579 | if (is_orphaned_child(event)) |
| 1580 | schedule_orphans_remove(ctx); |
| 1581 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1582 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | static void |
| 1586 | group_sched_out(struct perf_event *group_event, |
| 1587 | struct perf_cpu_context *cpuctx, |
| 1588 | struct perf_event_context *ctx) |
| 1589 | { |
| 1590 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1591 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1592 | |
| 1593 | event_sched_out(group_event, cpuctx, ctx); |
| 1594 | |
| 1595 | /* |
| 1596 | * Schedule out siblings (if any): |
| 1597 | */ |
| 1598 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1599 | event_sched_out(event, cpuctx, ctx); |
| 1600 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1601 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1602 | cpuctx->exclusive = 0; |
| 1603 | } |
| 1604 | |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1605 | struct remove_event { |
| 1606 | struct perf_event *event; |
| 1607 | bool detach_group; |
| 1608 | }; |
| 1609 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1610 | /* |
| 1611 | * Cross CPU call to remove a performance event |
| 1612 | * |
| 1613 | * We disable the event on the hardware level first. After that we |
| 1614 | * remove it from the context list. |
| 1615 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1616 | static int __perf_remove_from_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1617 | { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1618 | struct remove_event *re = info; |
| 1619 | struct perf_event *event = re->event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1620 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1621 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1622 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1623 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1624 | event_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1625 | if (re->detach_group) |
| 1626 | perf_group_detach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1627 | list_del_event(event, ctx); |
Peter Zijlstra | 64ce312 | 2011-04-09 21:17:48 +0200 | [diff] [blame] | 1628 | if (!ctx->nr_events && cpuctx->task_ctx == ctx) { |
| 1629 | ctx->is_active = 0; |
| 1630 | cpuctx->task_ctx = NULL; |
| 1631 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1632 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1633 | |
| 1634 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | /* |
| 1639 | * Remove the event from a task's (or a CPU's) list of events. |
| 1640 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1641 | * CPU events are removed with a smp call. For task events we only |
| 1642 | * call when the task is on a CPU. |
| 1643 | * |
| 1644 | * If event->ctx is a cloned context, callers must make sure that |
| 1645 | * every task struct that event->ctx->task could possibly point to |
| 1646 | * remains valid. This is OK when called from perf_release since |
| 1647 | * that only calls us on the top-level context, which can't be a clone. |
| 1648 | * When called from perf_event_exit_task, it's OK because the |
| 1649 | * context has been detached from its task. |
| 1650 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1651 | 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] | 1652 | { |
| 1653 | struct perf_event_context *ctx = event->ctx; |
| 1654 | struct task_struct *task = ctx->task; |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1655 | struct remove_event re = { |
| 1656 | .event = event, |
| 1657 | .detach_group = detach_group, |
| 1658 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1659 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1660 | lockdep_assert_held(&ctx->mutex); |
| 1661 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1662 | if (!task) { |
| 1663 | /* |
Mark Rutland | 226424e | 2014-11-05 16:11:44 +0000 | [diff] [blame] | 1664 | * Per cpu events are removed via an smp call. The removal can |
| 1665 | * fail if the CPU is currently offline, but in that case we |
| 1666 | * already called __perf_remove_from_context from |
| 1667 | * perf_event_exit_cpu. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1668 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1669 | cpu_function_call(event->cpu, __perf_remove_from_context, &re); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1670 | return; |
| 1671 | } |
| 1672 | |
| 1673 | retry: |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1674 | if (!task_function_call(task, __perf_remove_from_context, &re)) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1675 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1676 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1677 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1678 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1679 | * If we failed to find a running task, but find the context active now |
| 1680 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1681 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1682 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1683 | raw_spin_unlock_irq(&ctx->lock); |
Cong Wang | 3577af7 | 2014-09-02 15:27:20 -0700 | [diff] [blame] | 1684 | /* |
| 1685 | * Reload the task pointer, it might have been changed by |
| 1686 | * a concurrent perf_event_context_sched_out(). |
| 1687 | */ |
| 1688 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1689 | goto retry; |
| 1690 | } |
| 1691 | |
| 1692 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1693 | * Since the task isn't running, its safe to remove the event, us |
| 1694 | * holding the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1695 | */ |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1696 | if (detach_group) |
| 1697 | perf_group_detach(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1698 | list_del_event(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1699 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1702 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1703 | * Cross CPU call to disable a performance event |
| 1704 | */ |
K.Prasad | 500ad2d | 2012-08-02 13:46:35 +0530 | [diff] [blame] | 1705 | int __perf_event_disable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1706 | { |
| 1707 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1708 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 1709 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1710 | |
| 1711 | /* |
| 1712 | * If this is a per-task event, need to check whether this |
| 1713 | * event's task is the current task on this cpu. |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1714 | * |
| 1715 | * Can trigger due to concurrent perf_event_context_sched_out() |
| 1716 | * flipping contexts around. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1717 | */ |
| 1718 | if (ctx->task && cpuctx->task_ctx != ctx) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1719 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1720 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1721 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1722 | |
| 1723 | /* |
| 1724 | * If the event is on, turn it off. |
| 1725 | * If it is in error state, leave it in error state. |
| 1726 | */ |
| 1727 | if (event->state >= PERF_EVENT_STATE_INACTIVE) { |
| 1728 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1729 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1730 | update_group_times(event); |
| 1731 | if (event == event->group_leader) |
| 1732 | group_sched_out(event, cpuctx, ctx); |
| 1733 | else |
| 1734 | event_sched_out(event, cpuctx, ctx); |
| 1735 | event->state = PERF_EVENT_STATE_OFF; |
| 1736 | } |
| 1737 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1738 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1739 | |
| 1740 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | * Disable a event. |
| 1745 | * |
| 1746 | * If event->ctx is a cloned context, callers must make sure that |
| 1747 | * every task struct that event->ctx->task could possibly point to |
| 1748 | * remains valid. This condition is satisifed when called through |
| 1749 | * perf_event_for_each_child or perf_event_for_each because they |
| 1750 | * hold the top-level event's child_mutex, so any descendant that |
| 1751 | * goes to exit will block in sync_child_event. |
| 1752 | * When called from perf_pending_event it's OK because event->ctx |
| 1753 | * is the current context on this CPU and preemption is disabled, |
| 1754 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1755 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1756 | static void _perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1757 | { |
| 1758 | struct perf_event_context *ctx = event->ctx; |
| 1759 | struct task_struct *task = ctx->task; |
| 1760 | |
| 1761 | if (!task) { |
| 1762 | /* |
| 1763 | * Disable the event on the cpu that it's on |
| 1764 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1765 | cpu_function_call(event->cpu, __perf_event_disable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1766 | return; |
| 1767 | } |
| 1768 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1769 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1770 | if (!task_function_call(task, __perf_event_disable, event)) |
| 1771 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1772 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1773 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1774 | /* |
| 1775 | * If the event is still active, we need to retry the cross-call. |
| 1776 | */ |
| 1777 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1778 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1779 | /* |
| 1780 | * Reload the task pointer, it might have been changed by |
| 1781 | * a concurrent perf_event_context_sched_out(). |
| 1782 | */ |
| 1783 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1784 | goto retry; |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * Since we have the lock this context can't be scheduled |
| 1789 | * in, so we can change the state safely. |
| 1790 | */ |
| 1791 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 1792 | update_group_times(event); |
| 1793 | event->state = PERF_EVENT_STATE_OFF; |
| 1794 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1795 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1796 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1797 | |
| 1798 | /* |
| 1799 | * Strictly speaking kernel users cannot create groups and therefore this |
| 1800 | * interface does not need the perf_event_ctx_lock() magic. |
| 1801 | */ |
| 1802 | void perf_event_disable(struct perf_event *event) |
| 1803 | { |
| 1804 | struct perf_event_context *ctx; |
| 1805 | |
| 1806 | ctx = perf_event_ctx_lock(event); |
| 1807 | _perf_event_disable(event); |
| 1808 | perf_event_ctx_unlock(event, ctx); |
| 1809 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 1810 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1811 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1812 | static void perf_set_shadow_time(struct perf_event *event, |
| 1813 | struct perf_event_context *ctx, |
| 1814 | u64 tstamp) |
| 1815 | { |
| 1816 | /* |
| 1817 | * use the correct time source for the time snapshot |
| 1818 | * |
| 1819 | * We could get by without this by leveraging the |
| 1820 | * fact that to get to this function, the caller |
| 1821 | * has most likely already called update_context_time() |
| 1822 | * and update_cgrp_time_xx() and thus both timestamp |
| 1823 | * are identical (or very close). Given that tstamp is, |
| 1824 | * already adjusted for cgroup, we could say that: |
| 1825 | * tstamp - ctx->timestamp |
| 1826 | * is equivalent to |
| 1827 | * tstamp - cgrp->timestamp. |
| 1828 | * |
| 1829 | * Then, in perf_output_read(), the calculation would |
| 1830 | * work with no changes because: |
| 1831 | * - event is guaranteed scheduled in |
| 1832 | * - no scheduled out in between |
| 1833 | * - thus the timestamp would be the same |
| 1834 | * |
| 1835 | * But this is a bit hairy. |
| 1836 | * |
| 1837 | * So instead, we have an explicit cgroup call to remain |
| 1838 | * within the time time source all along. We believe it |
| 1839 | * is cleaner and simpler to understand. |
| 1840 | */ |
| 1841 | if (is_cgroup_event(event)) |
| 1842 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1843 | else |
| 1844 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1845 | } |
| 1846 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1847 | #define MAX_INTERRUPTS (~0ULL) |
| 1848 | |
| 1849 | static void perf_log_throttle(struct perf_event *event, int enable); |
| 1850 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1851 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1852 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1853 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1854 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1855 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1856 | u64 tstamp = perf_event_time(event); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1857 | int ret = 0; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1858 | |
Peter Zijlstra | 6334241 | 2014-05-05 11:49:16 +0200 | [diff] [blame] | 1859 | lockdep_assert_held(&ctx->lock); |
| 1860 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1861 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1862 | return 0; |
| 1863 | |
| 1864 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1865 | event->oncpu = smp_processor_id(); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1866 | |
| 1867 | /* |
| 1868 | * Unthrottle events, since we scheduled we might have missed several |
| 1869 | * ticks already, also for a heavily scheduling task there is little |
| 1870 | * guarantee it'll get a tick in a timely manner. |
| 1871 | */ |
| 1872 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1873 | perf_log_throttle(event, 1); |
| 1874 | event->hw.interrupts = 0; |
| 1875 | } |
| 1876 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1877 | /* |
| 1878 | * The new state must be visible before we turn it on in the hardware: |
| 1879 | */ |
| 1880 | smp_wmb(); |
| 1881 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1882 | perf_pmu_disable(event->pmu); |
| 1883 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1884 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1885 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1886 | event->oncpu = -1; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1887 | ret = -EAGAIN; |
| 1888 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1889 | } |
| 1890 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1891 | event->tstamp_running += tstamp - event->tstamp_stopped; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1892 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1893 | perf_set_shadow_time(event, ctx, tstamp); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 1894 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1895 | if (!is_software_event(event)) |
| 1896 | cpuctx->active_oncpu++; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1897 | if (!ctx->nr_active++) |
| 1898 | perf_event_ctx_activate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1899 | if (event->attr.freq && event->attr.sample_freq) |
| 1900 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1901 | |
| 1902 | if (event->attr.exclusive) |
| 1903 | cpuctx->exclusive = 1; |
| 1904 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1905 | if (is_orphaned_child(event)) |
| 1906 | schedule_orphans_remove(ctx); |
| 1907 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1908 | out: |
| 1909 | perf_pmu_enable(event->pmu); |
| 1910 | |
| 1911 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | static int |
| 1915 | group_sched_in(struct perf_event *group_event, |
| 1916 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1917 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1918 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1919 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 4a23459 | 2014-02-24 12:43:31 +0100 | [diff] [blame] | 1920 | struct pmu *pmu = ctx->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1921 | u64 now = ctx->time; |
| 1922 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1923 | |
| 1924 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1925 | return 0; |
| 1926 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1927 | pmu->start_txn(pmu); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1928 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1929 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1930 | pmu->cancel_txn(pmu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1931 | perf_cpu_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1932 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1933 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1934 | |
| 1935 | /* |
| 1936 | * Schedule in siblings as one group (if any): |
| 1937 | */ |
| 1938 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1939 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1940 | partial_group = event; |
| 1941 | goto group_error; |
| 1942 | } |
| 1943 | } |
| 1944 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1945 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 1946 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1947 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1948 | group_error: |
| 1949 | /* |
| 1950 | * Groups can be scheduled in as one unit only, so undo any |
| 1951 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1952 | * The events up to the failed event are scheduled out normally, |
| 1953 | * tstamp_stopped will be updated. |
| 1954 | * |
| 1955 | * The failed events and the remaining siblings need to have |
| 1956 | * their timings updated as if they had gone thru event_sched_in() |
| 1957 | * and event_sched_out(). This is required to get consistent timings |
| 1958 | * across the group. This also takes care of the case where the group |
| 1959 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 1960 | * the time the event was actually stopped, such that time delta |
| 1961 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1962 | */ |
| 1963 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 1964 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1965 | simulate = true; |
| 1966 | |
| 1967 | if (simulate) { |
| 1968 | event->tstamp_running += now - event->tstamp_stopped; |
| 1969 | event->tstamp_stopped = now; |
| 1970 | } else { |
| 1971 | event_sched_out(event, cpuctx, ctx); |
| 1972 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1973 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1974 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1975 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1976 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1977 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1978 | perf_cpu_hrtimer_restart(cpuctx); |
| 1979 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1980 | return -EAGAIN; |
| 1981 | } |
| 1982 | |
| 1983 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1984 | * Work out whether we can put this event group on the CPU now. |
| 1985 | */ |
| 1986 | static int group_can_go_on(struct perf_event *event, |
| 1987 | struct perf_cpu_context *cpuctx, |
| 1988 | int can_add_hw) |
| 1989 | { |
| 1990 | /* |
| 1991 | * Groups consisting entirely of software events can always go on. |
| 1992 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1993 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1994 | return 1; |
| 1995 | /* |
| 1996 | * If an exclusive group is already on, no other hardware |
| 1997 | * events can go on. |
| 1998 | */ |
| 1999 | if (cpuctx->exclusive) |
| 2000 | return 0; |
| 2001 | /* |
| 2002 | * If this group is exclusive and there are already |
| 2003 | * events on the CPU, it can't go on. |
| 2004 | */ |
| 2005 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 2006 | return 0; |
| 2007 | /* |
| 2008 | * Otherwise, try to add it if all previous groups were able |
| 2009 | * to go on. |
| 2010 | */ |
| 2011 | return can_add_hw; |
| 2012 | } |
| 2013 | |
| 2014 | static void add_event_to_ctx(struct perf_event *event, |
| 2015 | struct perf_event_context *ctx) |
| 2016 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2017 | u64 tstamp = perf_event_time(event); |
| 2018 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2019 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 2020 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2021 | event->tstamp_enabled = tstamp; |
| 2022 | event->tstamp_running = tstamp; |
| 2023 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2024 | } |
| 2025 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2026 | static void task_ctx_sched_out(struct perf_event_context *ctx); |
| 2027 | static void |
| 2028 | ctx_sched_in(struct perf_event_context *ctx, |
| 2029 | struct perf_cpu_context *cpuctx, |
| 2030 | enum event_type_t event_type, |
| 2031 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2032 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 2033 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 2034 | struct perf_event_context *ctx, |
| 2035 | struct task_struct *task) |
| 2036 | { |
| 2037 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 2038 | if (ctx) |
| 2039 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 2040 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 2041 | if (ctx) |
| 2042 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 2043 | } |
| 2044 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2045 | /* |
| 2046 | * Cross CPU call to install and enable a performance event |
| 2047 | * |
| 2048 | * Must be called with ctx->mutex held |
| 2049 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2050 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2051 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2052 | struct perf_event *event = info; |
| 2053 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2054 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2055 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
| 2056 | struct task_struct *task = current; |
| 2057 | |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2058 | perf_ctx_lock(cpuctx, task_ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2059 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2060 | |
| 2061 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2062 | * If there was an active task_ctx schedule it out. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2063 | */ |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2064 | if (task_ctx) |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2065 | task_ctx_sched_out(task_ctx); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2066 | |
| 2067 | /* |
| 2068 | * If the context we're installing events in is not the |
| 2069 | * active task_ctx, flip them. |
| 2070 | */ |
| 2071 | if (ctx->task && task_ctx != ctx) { |
| 2072 | if (task_ctx) |
| 2073 | raw_spin_unlock(&task_ctx->lock); |
| 2074 | raw_spin_lock(&ctx->lock); |
| 2075 | task_ctx = ctx; |
| 2076 | } |
| 2077 | |
| 2078 | if (task_ctx) { |
| 2079 | cpuctx->task_ctx = task_ctx; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2080 | task = task_ctx->task; |
| 2081 | } |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2082 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2083 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2084 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2085 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2086 | /* |
| 2087 | * update cgrp time only if current cgrp |
| 2088 | * matches event->cgrp. Must be done before |
| 2089 | * calling add_event_to_ctx() |
| 2090 | */ |
| 2091 | update_cgrp_time_from_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2092 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2093 | add_event_to_ctx(event, ctx); |
| 2094 | |
| 2095 | /* |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2096 | * Schedule everything back in |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2097 | */ |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 2098 | perf_event_sched_in(cpuctx, task_ctx, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2099 | |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2100 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 2101 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2102 | |
| 2103 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * Attach a performance event to a context |
| 2108 | * |
| 2109 | * First we add the event to the list with the hardware enable bit |
| 2110 | * in event->hw_config cleared. |
| 2111 | * |
| 2112 | * If the event is attached to a task which is on a CPU we use a smp |
| 2113 | * call to enable it in the task context. The task might have been |
| 2114 | * scheduled away, but we check this in the smp call again. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2115 | */ |
| 2116 | static void |
| 2117 | perf_install_in_context(struct perf_event_context *ctx, |
| 2118 | struct perf_event *event, |
| 2119 | int cpu) |
| 2120 | { |
| 2121 | struct task_struct *task = ctx->task; |
| 2122 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2123 | lockdep_assert_held(&ctx->mutex); |
| 2124 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2125 | event->ctx = ctx; |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 2126 | if (event->cpu != -1) |
| 2127 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2128 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2129 | if (!task) { |
| 2130 | /* |
| 2131 | * Per cpu events are installed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2132 | * the install is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2133 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2134 | cpu_function_call(cpu, __perf_install_in_context, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2135 | return; |
| 2136 | } |
| 2137 | |
| 2138 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2139 | if (!task_function_call(task, __perf_install_in_context, event)) |
| 2140 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2141 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2142 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2143 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2144 | * If we failed to find a running task, but find the context active now |
| 2145 | * that we've acquired the ctx->lock, retry. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2146 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2147 | if (ctx->is_active) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2148 | raw_spin_unlock_irq(&ctx->lock); |
Cong Wang | 3577af7 | 2014-09-02 15:27:20 -0700 | [diff] [blame] | 2149 | /* |
| 2150 | * Reload the task pointer, it might have been changed by |
| 2151 | * a concurrent perf_event_context_sched_out(). |
| 2152 | */ |
| 2153 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2154 | goto retry; |
| 2155 | } |
| 2156 | |
| 2157 | /* |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2158 | * Since the task isn't running, its safe to add the event, us holding |
| 2159 | * the ctx->lock ensures the task won't get scheduled in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2160 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2161 | add_event_to_ctx(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2162 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | /* |
| 2166 | * Put a event into inactive state and update time fields. |
| 2167 | * Enabling the leader of a group effectively enables all |
| 2168 | * the group members that aren't explicitly disabled, so we |
| 2169 | * have to update their ->tstamp_enabled also. |
| 2170 | * Note: this works for group members as well as group leaders |
| 2171 | * since the non-leader members' sibling_lists will be empty. |
| 2172 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2173 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2174 | { |
| 2175 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2176 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2177 | |
| 2178 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2179 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2180 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2181 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 2182 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2183 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | * Cross CPU call to enable a performance event |
| 2188 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2189 | static int __perf_event_enable(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2190 | { |
| 2191 | struct perf_event *event = info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2192 | struct perf_event_context *ctx = event->ctx; |
| 2193 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2194 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2195 | int err; |
| 2196 | |
Jiri Olsa | 06f4179 | 2013-07-09 17:44:11 +0200 | [diff] [blame] | 2197 | /* |
| 2198 | * There's a time window between 'ctx->is_active' check |
| 2199 | * in perf_event_enable function and this place having: |
| 2200 | * - IRQs on |
| 2201 | * - ctx->lock unlocked |
| 2202 | * |
| 2203 | * where the task could be killed and 'ctx' deactivated |
| 2204 | * by perf_event_exit_task. |
| 2205 | */ |
| 2206 | if (!ctx->is_active) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2207 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2208 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2209 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2210 | update_context_time(ctx); |
| 2211 | |
| 2212 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2213 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2214 | |
| 2215 | /* |
| 2216 | * set current task's cgroup time reference point |
| 2217 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2218 | perf_cgroup_set_timestamp(current, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2219 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2220 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2221 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2222 | if (!event_filter_match(event)) { |
| 2223 | if (is_cgroup_event(event)) |
| 2224 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2225 | goto unlock; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2226 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2227 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2228 | /* |
| 2229 | * If the event is in a group and isn't the group leader, |
| 2230 | * then don't put it on unless the group is on. |
| 2231 | */ |
| 2232 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) |
| 2233 | goto unlock; |
| 2234 | |
| 2235 | if (!group_can_go_on(event, cpuctx, 1)) { |
| 2236 | err = -EEXIST; |
| 2237 | } else { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2238 | if (event == leader) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2239 | err = group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2240 | else |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2241 | err = event_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | if (err) { |
| 2245 | /* |
| 2246 | * If this event can't go on and it's part of a |
| 2247 | * group, then the whole group has to come off. |
| 2248 | */ |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2249 | if (leader != event) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2250 | group_sched_out(leader, cpuctx, ctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2251 | perf_cpu_hrtimer_restart(cpuctx); |
| 2252 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2253 | if (leader->attr.pinned) { |
| 2254 | update_group_times(leader); |
| 2255 | leader->state = PERF_EVENT_STATE_ERROR; |
| 2256 | } |
| 2257 | } |
| 2258 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2259 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2260 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2261 | |
| 2262 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | /* |
| 2266 | * Enable a event. |
| 2267 | * |
| 2268 | * If event->ctx is a cloned context, callers must make sure that |
| 2269 | * every task struct that event->ctx->task could possibly point to |
| 2270 | * remains valid. This condition is satisfied when called through |
| 2271 | * perf_event_for_each_child or perf_event_for_each as described |
| 2272 | * for perf_event_disable. |
| 2273 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2274 | static void _perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2275 | { |
| 2276 | struct perf_event_context *ctx = event->ctx; |
| 2277 | struct task_struct *task = ctx->task; |
| 2278 | |
| 2279 | if (!task) { |
| 2280 | /* |
| 2281 | * Enable the event on the cpu that it's on |
| 2282 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2283 | cpu_function_call(event->cpu, __perf_event_enable, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2284 | return; |
| 2285 | } |
| 2286 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2287 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2288 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 2289 | goto out; |
| 2290 | |
| 2291 | /* |
| 2292 | * If the event is in error state, clear that first. |
| 2293 | * That way, if we see the event in error state below, we |
| 2294 | * know that it has gone back into error state, as distinct |
| 2295 | * from the task having been scheduled away before the |
| 2296 | * cross-call arrived. |
| 2297 | */ |
| 2298 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2299 | event->state = PERF_EVENT_STATE_OFF; |
| 2300 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2301 | retry: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2302 | if (!ctx->is_active) { |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2303 | __perf_event_mark_enabled(event); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2304 | goto out; |
| 2305 | } |
| 2306 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2307 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2308 | |
| 2309 | if (!task_function_call(task, __perf_event_enable, event)) |
| 2310 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2311 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2312 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2313 | |
| 2314 | /* |
| 2315 | * If the context is active and the event is still off, |
| 2316 | * we need to retry the cross-call. |
| 2317 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2318 | if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) { |
| 2319 | /* |
| 2320 | * task could have been flipped by a concurrent |
| 2321 | * perf_event_context_sched_out() |
| 2322 | */ |
| 2323 | task = ctx->task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2324 | goto retry; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2325 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2326 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2327 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2328 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2329 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2330 | |
| 2331 | /* |
| 2332 | * See perf_event_disable(); |
| 2333 | */ |
| 2334 | void perf_event_enable(struct perf_event *event) |
| 2335 | { |
| 2336 | struct perf_event_context *ctx; |
| 2337 | |
| 2338 | ctx = perf_event_ctx_lock(event); |
| 2339 | _perf_event_enable(event); |
| 2340 | perf_event_ctx_unlock(event, ctx); |
| 2341 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2342 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2343 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2344 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2345 | { |
| 2346 | /* |
| 2347 | * not supported on inherited events |
| 2348 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2349 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2350 | return -EINVAL; |
| 2351 | |
| 2352 | atomic_add(refresh, &event->event_limit); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2353 | _perf_event_enable(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2354 | |
| 2355 | return 0; |
| 2356 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2357 | |
| 2358 | /* |
| 2359 | * See perf_event_disable() |
| 2360 | */ |
| 2361 | int perf_event_refresh(struct perf_event *event, int refresh) |
| 2362 | { |
| 2363 | struct perf_event_context *ctx; |
| 2364 | int ret; |
| 2365 | |
| 2366 | ctx = perf_event_ctx_lock(event); |
| 2367 | ret = _perf_event_refresh(event, refresh); |
| 2368 | perf_event_ctx_unlock(event, ctx); |
| 2369 | |
| 2370 | return ret; |
| 2371 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2372 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2373 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2374 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2375 | struct perf_cpu_context *cpuctx, |
| 2376 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2377 | { |
| 2378 | struct perf_event *event; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2379 | int is_active = ctx->is_active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2380 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2381 | ctx->is_active &= ~event_type; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2382 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2383 | return; |
| 2384 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2385 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2386 | update_cgrp_time_from_cpuctx(cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2387 | if (!ctx->nr_active) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2388 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2389 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2390 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2391 | if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2392 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2393 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2394 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2395 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2396 | if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2397 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2398 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2399 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2400 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | /* |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2404 | * Test whether two contexts are equivalent, i.e. whether they have both been |
| 2405 | * cloned from the same version of the same context. |
| 2406 | * |
| 2407 | * Equivalence is measured using a generation number in the context that is |
| 2408 | * incremented on each modification to it; see unclone_ctx(), list_add_event() |
| 2409 | * and list_del_event(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2410 | */ |
| 2411 | static int context_equiv(struct perf_event_context *ctx1, |
| 2412 | struct perf_event_context *ctx2) |
| 2413 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 2414 | lockdep_assert_held(&ctx1->lock); |
| 2415 | lockdep_assert_held(&ctx2->lock); |
| 2416 | |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2417 | /* Pinning disables the swap optimization */ |
| 2418 | if (ctx1->pin_count || ctx2->pin_count) |
| 2419 | return 0; |
| 2420 | |
| 2421 | /* If ctx1 is the parent of ctx2 */ |
| 2422 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) |
| 2423 | return 1; |
| 2424 | |
| 2425 | /* If ctx2 is the parent of ctx1 */ |
| 2426 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) |
| 2427 | return 1; |
| 2428 | |
| 2429 | /* |
| 2430 | * If ctx1 and ctx2 have the same parent; we flatten the parent |
| 2431 | * hierarchy, see perf_event_init_context(). |
| 2432 | */ |
| 2433 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && |
| 2434 | ctx1->parent_gen == ctx2->parent_gen) |
| 2435 | return 1; |
| 2436 | |
| 2437 | /* Unmatched */ |
| 2438 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2439 | } |
| 2440 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2441 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2442 | struct perf_event *next_event) |
| 2443 | { |
| 2444 | u64 value; |
| 2445 | |
| 2446 | if (!event->attr.inherit_stat) |
| 2447 | return; |
| 2448 | |
| 2449 | /* |
| 2450 | * Update the event value, we cannot use perf_event_read() |
| 2451 | * because we're in the middle of a context switch and have IRQs |
| 2452 | * disabled, which upsets smp_call_function_single(), however |
| 2453 | * we know the event must be on the current CPU, therefore we |
| 2454 | * don't need to use it. |
| 2455 | */ |
| 2456 | switch (event->state) { |
| 2457 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2458 | event->pmu->read(event); |
| 2459 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2460 | |
| 2461 | case PERF_EVENT_STATE_INACTIVE: |
| 2462 | update_event_times(event); |
| 2463 | break; |
| 2464 | |
| 2465 | default: |
| 2466 | break; |
| 2467 | } |
| 2468 | |
| 2469 | /* |
| 2470 | * In order to keep per-task stats reliable we need to flip the event |
| 2471 | * values when we flip the contexts. |
| 2472 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2473 | value = local64_read(&next_event->count); |
| 2474 | value = local64_xchg(&event->count, value); |
| 2475 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2476 | |
| 2477 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2478 | swap(event->total_time_running, next_event->total_time_running); |
| 2479 | |
| 2480 | /* |
| 2481 | * Since we swizzled the values, update the user visible data too. |
| 2482 | */ |
| 2483 | perf_event_update_userpage(event); |
| 2484 | perf_event_update_userpage(next_event); |
| 2485 | } |
| 2486 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2487 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2488 | struct perf_event_context *next_ctx) |
| 2489 | { |
| 2490 | struct perf_event *event, *next_event; |
| 2491 | |
| 2492 | if (!ctx->nr_stat) |
| 2493 | return; |
| 2494 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2495 | update_context_time(ctx); |
| 2496 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2497 | event = list_first_entry(&ctx->event_list, |
| 2498 | struct perf_event, event_entry); |
| 2499 | |
| 2500 | next_event = list_first_entry(&next_ctx->event_list, |
| 2501 | struct perf_event, event_entry); |
| 2502 | |
| 2503 | while (&event->event_entry != &ctx->event_list && |
| 2504 | &next_event->event_entry != &next_ctx->event_list) { |
| 2505 | |
| 2506 | __perf_event_sync_stat(event, next_event); |
| 2507 | |
| 2508 | event = list_next_entry(event, event_entry); |
| 2509 | next_event = list_next_entry(next_event, event_entry); |
| 2510 | } |
| 2511 | } |
| 2512 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2513 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2514 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2515 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2516 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2517 | struct perf_event_context *next_ctx; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2518 | struct perf_event_context *parent, *next_parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2519 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2520 | int do_switch = 1; |
| 2521 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2522 | if (likely(!ctx)) |
| 2523 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2524 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2525 | cpuctx = __get_cpu_context(ctx); |
| 2526 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2527 | return; |
| 2528 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2529 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2530 | next_ctx = next->perf_event_ctxp[ctxn]; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2531 | if (!next_ctx) |
| 2532 | goto unlock; |
| 2533 | |
| 2534 | parent = rcu_dereference(ctx->parent_ctx); |
| 2535 | next_parent = rcu_dereference(next_ctx->parent_ctx); |
| 2536 | |
| 2537 | /* If neither context have a parent context; they cannot be clones. */ |
Jiri Olsa | 802c8a6 | 2014-09-12 13:18:28 +0200 | [diff] [blame] | 2538 | if (!parent && !next_parent) |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2539 | goto unlock; |
| 2540 | |
| 2541 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2542 | /* |
| 2543 | * Looks like the two contexts are clones, so we might be |
| 2544 | * able to optimize the context switch. We lock both |
| 2545 | * contexts and check that they are clones under the |
| 2546 | * lock (including re-checking that neither has been |
| 2547 | * uncloned in the meantime). It doesn't matter which |
| 2548 | * order we take the locks because no other cpu could |
| 2549 | * be trying to lock both of these tasks. |
| 2550 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2551 | raw_spin_lock(&ctx->lock); |
| 2552 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2553 | if (context_equiv(ctx, next_ctx)) { |
| 2554 | /* |
| 2555 | * XXX do we need a memory barrier of sorts |
| 2556 | * wrt to rcu_dereference() of perf_event_ctxp |
| 2557 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2558 | task->perf_event_ctxp[ctxn] = next_ctx; |
| 2559 | next->perf_event_ctxp[ctxn] = ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2560 | ctx->task = next; |
| 2561 | next_ctx->task = task; |
| 2562 | do_switch = 0; |
| 2563 | |
| 2564 | perf_event_sync_stat(ctx, next_ctx); |
| 2565 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2566 | raw_spin_unlock(&next_ctx->lock); |
| 2567 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2568 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2569 | unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2570 | rcu_read_unlock(); |
| 2571 | |
| 2572 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2573 | raw_spin_lock(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2574 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2575 | cpuctx->task_ctx = NULL; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2576 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2577 | } |
| 2578 | } |
| 2579 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2580 | #define for_each_task_context_nr(ctxn) \ |
| 2581 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2582 | |
| 2583 | /* |
| 2584 | * Called from scheduler to remove the events of the current task, |
| 2585 | * with interrupts disabled. |
| 2586 | * |
| 2587 | * We stop each event and update the event value in event->count. |
| 2588 | * |
| 2589 | * This does not protect us against NMI, but disable() |
| 2590 | * sets the disabled bit in the control field of event _before_ |
| 2591 | * accessing the event control register. If a NMI hits, then it will |
| 2592 | * not restart the event. |
| 2593 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2594 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2595 | struct task_struct *next) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2596 | { |
| 2597 | int ctxn; |
| 2598 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2599 | for_each_task_context_nr(ctxn) |
| 2600 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2601 | |
| 2602 | /* |
| 2603 | * if cgroup events exist on this CPU, then we need |
| 2604 | * to check if we have to switch out PMU state. |
| 2605 | * cgroup event are system-wide mode only |
| 2606 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2607 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2608 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2609 | } |
| 2610 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2611 | static void task_ctx_sched_out(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2612 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2613 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2614 | |
| 2615 | if (!cpuctx->task_ctx) |
| 2616 | return; |
| 2617 | |
| 2618 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2619 | return; |
| 2620 | |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 2621 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2622 | cpuctx->task_ctx = NULL; |
| 2623 | } |
| 2624 | |
| 2625 | /* |
| 2626 | * Called with IRQs disabled |
| 2627 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2628 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2629 | enum event_type_t event_type) |
| 2630 | { |
| 2631 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2635 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2636 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2637 | { |
| 2638 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2639 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2640 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2641 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2642 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2643 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2644 | continue; |
| 2645 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2646 | /* may need to reset tstamp_enabled */ |
| 2647 | if (is_cgroup_event(event)) |
| 2648 | perf_cgroup_mark_enabled(event, ctx); |
| 2649 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2650 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2651 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2652 | |
| 2653 | /* |
| 2654 | * If this pinned group hasn't been scheduled, |
| 2655 | * put it in error state. |
| 2656 | */ |
| 2657 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2658 | update_group_times(event); |
| 2659 | event->state = PERF_EVENT_STATE_ERROR; |
| 2660 | } |
| 2661 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | static void |
| 2665 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2666 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2667 | { |
| 2668 | struct perf_event *event; |
| 2669 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2670 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2671 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2672 | /* Ignore events in OFF or ERROR state */ |
| 2673 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2674 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2675 | /* |
| 2676 | * Listen to the 'cpu' scheduling filter constraint |
| 2677 | * of events: |
| 2678 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2679 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2680 | continue; |
| 2681 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2682 | /* may need to reset tstamp_enabled */ |
| 2683 | if (is_cgroup_event(event)) |
| 2684 | perf_cgroup_mark_enabled(event, ctx); |
| 2685 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2686 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2687 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2688 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2689 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2690 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | static void |
| 2694 | ctx_sched_in(struct perf_event_context *ctx, |
| 2695 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2696 | enum event_type_t event_type, |
| 2697 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2698 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2699 | u64 now; |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2700 | int is_active = ctx->is_active; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2701 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2702 | ctx->is_active |= event_type; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2703 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2704 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2705 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2706 | now = perf_clock(); |
| 2707 | ctx->timestamp = now; |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 2708 | perf_cgroup_set_timestamp(task, ctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2709 | /* |
| 2710 | * First go through the list and put on any pinned groups |
| 2711 | * in order to give them the best chance of going on. |
| 2712 | */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2713 | if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2714 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2715 | |
| 2716 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2717 | if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2718 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2719 | } |
| 2720 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2721 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2722 | enum event_type_t event_type, |
| 2723 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2724 | { |
| 2725 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2726 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2727 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2728 | } |
| 2729 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2730 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2731 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2732 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2733 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2734 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2735 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2736 | if (cpuctx->task_ctx == ctx) |
| 2737 | return; |
| 2738 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2739 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2740 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2741 | /* |
| 2742 | * We want to keep the following priority order: |
| 2743 | * cpu pinned (that don't need to move), task pinned, |
| 2744 | * cpu flexible, task flexible. |
| 2745 | */ |
| 2746 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 2747 | |
Gleb Natapov | 1d5f003 | 2011-10-23 19:10:33 +0200 | [diff] [blame] | 2748 | if (ctx->nr_events) |
| 2749 | cpuctx->task_ctx = ctx; |
eranian@google.com | 9b33fa6 | 2010-03-10 22:26:05 -0800 | [diff] [blame] | 2750 | |
Gleb Natapov | 86b47c2 | 2011-11-22 16:08:21 +0200 | [diff] [blame] | 2751 | perf_event_sched_in(cpuctx, cpuctx->task_ctx, task); |
| 2752 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2753 | perf_pmu_enable(ctx->pmu); |
| 2754 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2755 | } |
| 2756 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2757 | /* |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2758 | * When sampling the branck stack in system-wide, it may be necessary |
| 2759 | * to flush the stack on context switch. This happens when the branch |
| 2760 | * stack does not tag its entries with the pid of the current task. |
| 2761 | * Otherwise it becomes impossible to associate a branch entry with a |
| 2762 | * task. This ambiguity is more likely to appear when the branch stack |
| 2763 | * supports priv level filtering and the user sets it to monitor only |
| 2764 | * at the user level (which could be a useful measurement in system-wide |
| 2765 | * mode). In that case, the risk is high of having a branch stack with |
| 2766 | * branch from multiple tasks. Flushing may mean dropping the existing |
| 2767 | * entries or stashing them somewhere in the PMU specific code layer. |
| 2768 | * |
| 2769 | * This function provides the context switch callback to the lower code |
| 2770 | * layer. It is invoked ONLY when there is at least one system-wide context |
| 2771 | * with at least one active event using taken branch sampling. |
| 2772 | */ |
| 2773 | static void perf_branch_stack_sched_in(struct task_struct *prev, |
| 2774 | struct task_struct *task) |
| 2775 | { |
| 2776 | struct perf_cpu_context *cpuctx; |
| 2777 | struct pmu *pmu; |
| 2778 | unsigned long flags; |
| 2779 | |
| 2780 | /* no need to flush branch stack if not changing task */ |
| 2781 | if (prev == task) |
| 2782 | return; |
| 2783 | |
| 2784 | local_irq_save(flags); |
| 2785 | |
| 2786 | rcu_read_lock(); |
| 2787 | |
| 2788 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 2789 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2790 | |
| 2791 | /* |
| 2792 | * check if the context has at least one |
| 2793 | * event using PERF_SAMPLE_BRANCH_STACK |
| 2794 | */ |
| 2795 | if (cpuctx->ctx.nr_branch_stack > 0 |
| 2796 | && pmu->flush_branch_stack) { |
| 2797 | |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2798 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2799 | |
| 2800 | perf_pmu_disable(pmu); |
| 2801 | |
| 2802 | pmu->flush_branch_stack(); |
| 2803 | |
| 2804 | perf_pmu_enable(pmu); |
| 2805 | |
| 2806 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
| 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | rcu_read_unlock(); |
| 2811 | |
| 2812 | local_irq_restore(flags); |
| 2813 | } |
| 2814 | |
| 2815 | /* |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2816 | * Called from scheduler to add the events of the current task |
| 2817 | * with interrupts disabled. |
| 2818 | * |
| 2819 | * We restore the event value and then enable it. |
| 2820 | * |
| 2821 | * This does not protect us against NMI, but enable() |
| 2822 | * sets the enabled bit in the control field of event _before_ |
| 2823 | * accessing the event control register. If a NMI hits, then it will |
| 2824 | * keep the event running. |
| 2825 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2826 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 2827 | struct task_struct *task) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2828 | { |
| 2829 | struct perf_event_context *ctx; |
| 2830 | int ctxn; |
| 2831 | |
| 2832 | for_each_task_context_nr(ctxn) { |
| 2833 | ctx = task->perf_event_ctxp[ctxn]; |
| 2834 | if (likely(!ctx)) |
| 2835 | continue; |
| 2836 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2837 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2838 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2839 | /* |
| 2840 | * if cgroup events exist on this CPU, then we need |
| 2841 | * to check if we have to switch in PMU state. |
| 2842 | * cgroup event are system-wide mode only |
| 2843 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2844 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2845 | perf_cgroup_sched_in(prev, task); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2846 | |
| 2847 | /* check for system-wide branch_stack events */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2848 | if (atomic_read(this_cpu_ptr(&perf_branch_stack_events))) |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2849 | perf_branch_stack_sched_in(prev, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2850 | } |
| 2851 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2852 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 2853 | { |
| 2854 | u64 frequency = event->attr.sample_freq; |
| 2855 | u64 sec = NSEC_PER_SEC; |
| 2856 | u64 divisor, dividend; |
| 2857 | |
| 2858 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 2859 | |
| 2860 | count_fls = fls64(count); |
| 2861 | nsec_fls = fls64(nsec); |
| 2862 | frequency_fls = fls64(frequency); |
| 2863 | sec_fls = 30; |
| 2864 | |
| 2865 | /* |
| 2866 | * We got @count in @nsec, with a target of sample_freq HZ |
| 2867 | * the target period becomes: |
| 2868 | * |
| 2869 | * @count * 10^9 |
| 2870 | * period = ------------------- |
| 2871 | * @nsec * sample_freq |
| 2872 | * |
| 2873 | */ |
| 2874 | |
| 2875 | /* |
| 2876 | * Reduce accuracy by one bit such that @a and @b converge |
| 2877 | * to a similar magnitude. |
| 2878 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2879 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2880 | do { \ |
| 2881 | if (a##_fls > b##_fls) { \ |
| 2882 | a >>= 1; \ |
| 2883 | a##_fls--; \ |
| 2884 | } else { \ |
| 2885 | b >>= 1; \ |
| 2886 | b##_fls--; \ |
| 2887 | } \ |
| 2888 | } while (0) |
| 2889 | |
| 2890 | /* |
| 2891 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 2892 | * the other, so that finally we can do a u64/u64 division. |
| 2893 | */ |
| 2894 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 2895 | REDUCE_FLS(nsec, frequency); |
| 2896 | REDUCE_FLS(sec, count); |
| 2897 | } |
| 2898 | |
| 2899 | if (count_fls + sec_fls > 64) { |
| 2900 | divisor = nsec * frequency; |
| 2901 | |
| 2902 | while (count_fls + sec_fls > 64) { |
| 2903 | REDUCE_FLS(count, sec); |
| 2904 | divisor >>= 1; |
| 2905 | } |
| 2906 | |
| 2907 | dividend = count * sec; |
| 2908 | } else { |
| 2909 | dividend = count * sec; |
| 2910 | |
| 2911 | while (nsec_fls + frequency_fls > 64) { |
| 2912 | REDUCE_FLS(nsec, frequency); |
| 2913 | dividend >>= 1; |
| 2914 | } |
| 2915 | |
| 2916 | divisor = nsec * frequency; |
| 2917 | } |
| 2918 | |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2919 | if (!divisor) |
| 2920 | return dividend; |
| 2921 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2922 | return div64_u64(dividend, divisor); |
| 2923 | } |
| 2924 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2925 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 2926 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 2927 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2928 | 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] | 2929 | { |
| 2930 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2931 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2932 | s64 delta; |
| 2933 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2934 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2935 | |
| 2936 | delta = (s64)(period - hwc->sample_period); |
| 2937 | delta = (delta + 7) / 8; /* low pass filter */ |
| 2938 | |
| 2939 | sample_period = hwc->sample_period + delta; |
| 2940 | |
| 2941 | if (!sample_period) |
| 2942 | sample_period = 1; |
| 2943 | |
| 2944 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2945 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2946 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2947 | if (disable) |
| 2948 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2949 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2950 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2951 | |
| 2952 | if (disable) |
| 2953 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2954 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2955 | } |
| 2956 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2957 | /* |
| 2958 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 2959 | * events. At the same time, make sure, having freq events does not change |
| 2960 | * the rate of unthrottling as that would introduce bias. |
| 2961 | */ |
| 2962 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 2963 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2964 | { |
| 2965 | struct perf_event *event; |
| 2966 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2967 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2968 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2969 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2970 | /* |
| 2971 | * only need to iterate over all events iff: |
| 2972 | * - context have events in frequency mode (needs freq adjust) |
| 2973 | * - there are events to unthrottle on this cpu |
| 2974 | */ |
| 2975 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2976 | return; |
| 2977 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2978 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2979 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2980 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 2981 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2982 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 2983 | continue; |
| 2984 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2985 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 2986 | continue; |
| 2987 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2988 | perf_pmu_disable(event->pmu); |
| 2989 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2990 | hwc = &event->hw; |
| 2991 | |
Jiri Olsa | ae23bff | 2013-08-24 16:45:54 +0200 | [diff] [blame] | 2992 | if (hwc->interrupts == MAX_INTERRUPTS) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2993 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2994 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2995 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | if (!event->attr.freq || !event->attr.sample_freq) |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2999 | goto next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3000 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3001 | /* |
| 3002 | * stop the event and update event->count |
| 3003 | */ |
| 3004 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3005 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3006 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3007 | delta = now - hwc->freq_count_stamp; |
| 3008 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3009 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3010 | /* |
| 3011 | * restart the event |
| 3012 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3013 | * we have stopped the event so tell that |
| 3014 | * to perf_adjust_period() to avoid stopping it |
| 3015 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3016 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3017 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3018 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3019 | |
| 3020 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3021 | next: |
| 3022 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3023 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3024 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3025 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3026 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | /* |
| 3030 | * Round-robin a context's events: |
| 3031 | */ |
| 3032 | static void rotate_ctx(struct perf_event_context *ctx) |
| 3033 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 3034 | /* |
| 3035 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 3036 | * disabled by the inheritance code. |
| 3037 | */ |
| 3038 | if (!ctx->rotate_disable) |
| 3039 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3040 | } |
| 3041 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3042 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3043 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3044 | struct perf_event_context *ctx = NULL; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3045 | int rotate = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3046 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3047 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3048 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 3049 | rotate = 1; |
| 3050 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3051 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3052 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3053 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3054 | if (ctx->nr_events != ctx->nr_active) |
| 3055 | rotate = 1; |
| 3056 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3057 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3058 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3059 | goto done; |
| 3060 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 3061 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 3062 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3063 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3064 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 3065 | if (ctx) |
| 3066 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 3067 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3068 | rotate_ctx(&cpuctx->ctx); |
| 3069 | if (ctx) |
| 3070 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3071 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3072 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3073 | |
| 3074 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 3075 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3076 | done: |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3077 | |
| 3078 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3079 | } |
| 3080 | |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 3081 | #ifdef CONFIG_NO_HZ_FULL |
| 3082 | bool perf_event_can_stop_tick(void) |
| 3083 | { |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3084 | if (atomic_read(&nr_freq_events) || |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 3085 | __this_cpu_read(perf_throttled_count)) |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 3086 | return false; |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 3087 | else |
| 3088 | return true; |
Frederic Weisbecker | 026249e | 2013-04-20 15:58:34 +0200 | [diff] [blame] | 3089 | } |
| 3090 | #endif |
| 3091 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3092 | void perf_event_task_tick(void) |
| 3093 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3094 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
| 3095 | struct perf_event_context *ctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3096 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3097 | |
| 3098 | WARN_ON(!irqs_disabled()); |
| 3099 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3100 | __this_cpu_inc(perf_throttled_seq); |
| 3101 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
| 3102 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3103 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3104 | perf_adjust_freq_unthr_context(ctx, throttled); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3105 | } |
| 3106 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3107 | static int event_enable_on_exec(struct perf_event *event, |
| 3108 | struct perf_event_context *ctx) |
| 3109 | { |
| 3110 | if (!event->attr.enable_on_exec) |
| 3111 | return 0; |
| 3112 | |
| 3113 | event->attr.enable_on_exec = 0; |
| 3114 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 3115 | return 0; |
| 3116 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 3117 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3118 | |
| 3119 | return 1; |
| 3120 | } |
| 3121 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3122 | /* |
| 3123 | * Enable all of a task's events that have been marked enable-on-exec. |
| 3124 | * This expects task == current. |
| 3125 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3126 | static void perf_event_enable_on_exec(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3127 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3128 | struct perf_event_context *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3129 | struct perf_event *event; |
| 3130 | unsigned long flags; |
| 3131 | int enabled = 0; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3132 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3133 | |
| 3134 | local_irq_save(flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3135 | if (!ctx || !ctx->nr_events) |
| 3136 | goto out; |
| 3137 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 3138 | /* |
| 3139 | * We must ctxsw out cgroup events to avoid conflict |
| 3140 | * when invoking perf_task_event_sched_in() later on |
| 3141 | * in this function. Otherwise we end up trying to |
| 3142 | * ctxswin cgroup events which are already scheduled |
| 3143 | * in. |
| 3144 | */ |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 3145 | perf_cgroup_sched_out(current, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3146 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3147 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 3148 | task_ctx_sched_out(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3149 | |
Peter Zijlstra | b79387e | 2011-11-22 11:25:43 +0100 | [diff] [blame] | 3150 | list_for_each_entry(event, &ctx->event_list, event_entry) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3151 | ret = event_enable_on_exec(event, ctx); |
| 3152 | if (ret) |
| 3153 | enabled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | * Unclone this context if we enabled any event. |
| 3158 | */ |
| 3159 | if (enabled) |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3160 | clone_ctx = unclone_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3161 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3162 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3163 | |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 3164 | /* |
| 3165 | * Also calls ctxswin for cgroup events, if any: |
| 3166 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3167 | perf_event_context_sched_in(ctx, ctx->task); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3168 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3169 | local_irq_restore(flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3170 | |
| 3171 | if (clone_ctx) |
| 3172 | put_ctx(clone_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3173 | } |
| 3174 | |
Peter Zijlstra | e041e32 | 2014-05-21 17:32:19 +0200 | [diff] [blame] | 3175 | void perf_event_exec(void) |
| 3176 | { |
| 3177 | struct perf_event_context *ctx; |
| 3178 | int ctxn; |
| 3179 | |
| 3180 | rcu_read_lock(); |
| 3181 | for_each_task_context_nr(ctxn) { |
| 3182 | ctx = current->perf_event_ctxp[ctxn]; |
| 3183 | if (!ctx) |
| 3184 | continue; |
| 3185 | |
| 3186 | perf_event_enable_on_exec(ctx); |
| 3187 | } |
| 3188 | rcu_read_unlock(); |
| 3189 | } |
| 3190 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3191 | /* |
| 3192 | * Cross CPU call to read the hardware event |
| 3193 | */ |
| 3194 | static void __perf_event_read(void *info) |
| 3195 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3196 | struct perf_event *event = info; |
| 3197 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3198 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3199 | |
| 3200 | /* |
| 3201 | * If this is a task context, we need to check whether it is |
| 3202 | * the current task context of this cpu. If not it has been |
| 3203 | * scheduled out before the smp call arrived. In that case |
| 3204 | * event->count would have been updated to a recent sample |
| 3205 | * when the event was scheduled out. |
| 3206 | */ |
| 3207 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 3208 | return; |
| 3209 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3210 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3211 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3212 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3213 | update_cgrp_time_from_event(event); |
| 3214 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3215 | update_event_times(event); |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3216 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
| 3217 | event->pmu->read(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3218 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3219 | } |
| 3220 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3221 | static inline u64 perf_event_count(struct perf_event *event) |
| 3222 | { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3223 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3224 | } |
| 3225 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3226 | static u64 perf_event_read(struct perf_event *event) |
| 3227 | { |
| 3228 | /* |
| 3229 | * If event is enabled and currently active on a CPU, update the |
| 3230 | * value in the event structure: |
| 3231 | */ |
| 3232 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
| 3233 | smp_call_function_single(event->oncpu, |
| 3234 | __perf_event_read, event, 1); |
| 3235 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 3236 | struct perf_event_context *ctx = event->ctx; |
| 3237 | unsigned long flags; |
| 3238 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3239 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3240 | /* |
| 3241 | * may read while context is not active |
| 3242 | * (e.g., thread is blocked), in that case |
| 3243 | * we cannot update context time |
| 3244 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3245 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3246 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3247 | update_cgrp_time_from_event(event); |
| 3248 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3249 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3250 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3251 | } |
| 3252 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3253 | return perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | /* |
| 3257 | * Initialize the perf_event context in a task_struct: |
| 3258 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3259 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3260 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3261 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3262 | mutex_init(&ctx->mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3263 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3264 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 3265 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3266 | INIT_LIST_HEAD(&ctx->event_list); |
| 3267 | atomic_set(&ctx->refcount, 1); |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 3268 | INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3271 | static struct perf_event_context * |
| 3272 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3273 | { |
| 3274 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3275 | |
| 3276 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 3277 | if (!ctx) |
| 3278 | return NULL; |
| 3279 | |
| 3280 | __perf_event_init_context(ctx); |
| 3281 | if (task) { |
| 3282 | ctx->task = task; |
| 3283 | get_task_struct(task); |
| 3284 | } |
| 3285 | ctx->pmu = pmu; |
| 3286 | |
| 3287 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3288 | } |
| 3289 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3290 | static struct task_struct * |
| 3291 | find_lively_task_by_vpid(pid_t vpid) |
| 3292 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3293 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3294 | int err; |
| 3295 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3296 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3297 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3298 | task = current; |
| 3299 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3300 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3301 | if (task) |
| 3302 | get_task_struct(task); |
| 3303 | rcu_read_unlock(); |
| 3304 | |
| 3305 | if (!task) |
| 3306 | return ERR_PTR(-ESRCH); |
| 3307 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3308 | /* Reuse ptrace permission checks for now. */ |
| 3309 | err = -EACCES; |
| 3310 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 3311 | goto errout; |
| 3312 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3313 | return task; |
| 3314 | errout: |
| 3315 | put_task_struct(task); |
| 3316 | return ERR_PTR(err); |
| 3317 | |
| 3318 | } |
| 3319 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3320 | /* |
| 3321 | * Returns a matching context with refcount and pincount. |
| 3322 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3323 | static struct perf_event_context * |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 3324 | find_get_context(struct pmu *pmu, struct task_struct *task, int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3325 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3326 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3327 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3328 | unsigned long flags; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3329 | int ctxn, err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3330 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3331 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3332 | /* Must be root to operate on a CPU event: */ |
| 3333 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3334 | return ERR_PTR(-EACCES); |
| 3335 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3336 | /* |
| 3337 | * We could be clever and allow to attach a event to an |
| 3338 | * offline CPU and activate it when the CPU comes up, but |
| 3339 | * that's for later. |
| 3340 | */ |
| 3341 | if (!cpu_online(cpu)) |
| 3342 | return ERR_PTR(-ENODEV); |
| 3343 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3344 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3345 | ctx = &cpuctx->ctx; |
| 3346 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3347 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3348 | |
| 3349 | return ctx; |
| 3350 | } |
| 3351 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3352 | err = -EINVAL; |
| 3353 | ctxn = pmu->task_ctx_nr; |
| 3354 | if (ctxn < 0) |
| 3355 | goto errout; |
| 3356 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3357 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3358 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3359 | if (ctx) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3360 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3361 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3362 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3363 | |
| 3364 | if (clone_ctx) |
| 3365 | put_ctx(clone_ctx); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3366 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3367 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3368 | err = -ENOMEM; |
| 3369 | if (!ctx) |
| 3370 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3371 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3372 | err = 0; |
| 3373 | mutex_lock(&task->perf_event_mutex); |
| 3374 | /* |
| 3375 | * If it has already passed perf_event_exit_task(). |
| 3376 | * we must see PF_EXITING, it takes this mutex too. |
| 3377 | */ |
| 3378 | if (task->flags & PF_EXITING) |
| 3379 | err = -ESRCH; |
| 3380 | else if (task->perf_event_ctxp[ctxn]) |
| 3381 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3382 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3383 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3384 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3385 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3386 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3387 | mutex_unlock(&task->perf_event_mutex); |
| 3388 | |
| 3389 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3390 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3391 | |
| 3392 | if (err == -EAGAIN) |
| 3393 | goto retry; |
| 3394 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3395 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3396 | } |
| 3397 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3398 | return ctx; |
| 3399 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3400 | errout: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3401 | return ERR_PTR(err); |
| 3402 | } |
| 3403 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3404 | static void perf_event_free_filter(struct perf_event *event); |
| 3405 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3406 | static void free_event_rcu(struct rcu_head *head) |
| 3407 | { |
| 3408 | struct perf_event *event; |
| 3409 | |
| 3410 | event = container_of(head, struct perf_event, rcu_head); |
| 3411 | if (event->ns) |
| 3412 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3413 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3414 | kfree(event); |
| 3415 | } |
| 3416 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3417 | static void ring_buffer_put(struct ring_buffer *rb); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3418 | static void ring_buffer_attach(struct perf_event *event, |
| 3419 | struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3420 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3421 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
| 3422 | { |
| 3423 | if (event->parent) |
| 3424 | return; |
| 3425 | |
| 3426 | if (has_branch_stack(event)) { |
| 3427 | if (!(event->attach_state & PERF_ATTACH_TASK)) |
| 3428 | atomic_dec(&per_cpu(perf_branch_stack_events, cpu)); |
| 3429 | } |
| 3430 | if (is_cgroup_event(event)) |
| 3431 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); |
| 3432 | } |
| 3433 | |
| 3434 | static void unaccount_event(struct perf_event *event) |
| 3435 | { |
| 3436 | if (event->parent) |
| 3437 | return; |
| 3438 | |
| 3439 | if (event->attach_state & PERF_ATTACH_TASK) |
| 3440 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3441 | if (event->attr.mmap || event->attr.mmap_data) |
| 3442 | atomic_dec(&nr_mmap_events); |
| 3443 | if (event->attr.comm) |
| 3444 | atomic_dec(&nr_comm_events); |
| 3445 | if (event->attr.task) |
| 3446 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3447 | if (event->attr.freq) |
| 3448 | atomic_dec(&nr_freq_events); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3449 | if (is_cgroup_event(event)) |
| 3450 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3451 | if (has_branch_stack(event)) |
| 3452 | static_key_slow_dec_deferred(&perf_sched_events); |
| 3453 | |
| 3454 | unaccount_event_cpu(event, event->cpu); |
| 3455 | } |
| 3456 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3457 | static void __free_event(struct perf_event *event) |
| 3458 | { |
| 3459 | if (!event->parent) { |
| 3460 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 3461 | put_callchain_buffers(); |
| 3462 | } |
| 3463 | |
| 3464 | if (event->destroy) |
| 3465 | event->destroy(event); |
| 3466 | |
| 3467 | if (event->ctx) |
| 3468 | put_ctx(event->ctx); |
| 3469 | |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 3470 | if (event->pmu) |
| 3471 | module_put(event->pmu->module); |
| 3472 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3473 | call_rcu(&event->rcu_head, free_event_rcu); |
| 3474 | } |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3475 | |
| 3476 | static void _free_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3477 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3478 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3479 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3480 | unaccount_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3481 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3482 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3483 | /* |
| 3484 | * Can happen when we close an event with re-directed output. |
| 3485 | * |
| 3486 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 3487 | * over us; possibly making our ring_buffer_put() the last. |
| 3488 | */ |
| 3489 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3490 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3491 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3492 | } |
| 3493 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3494 | if (is_cgroup_event(event)) |
| 3495 | perf_detach_cgroup(event); |
| 3496 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 3497 | __free_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3498 | } |
| 3499 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3500 | /* |
| 3501 | * Used to free events which have a known refcount of 1, such as in error paths |
| 3502 | * where the event isn't exposed yet and inherited events. |
| 3503 | */ |
| 3504 | static void free_event(struct perf_event *event) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3505 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3506 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
| 3507 | "unexpected event refcount: %ld; ptr=%p\n", |
| 3508 | atomic_long_read(&event->refcount), event)) { |
| 3509 | /* leak to avoid use-after-free */ |
| 3510 | return; |
| 3511 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3512 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3513 | _free_event(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3514 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3515 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3516 | /* |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3517 | * Remove user event from the owner task. |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3518 | */ |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3519 | static void perf_remove_from_owner(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3520 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3521 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3522 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3523 | rcu_read_lock(); |
| 3524 | owner = ACCESS_ONCE(event->owner); |
| 3525 | /* |
| 3526 | * Matches the smp_wmb() in perf_event_exit_task(). If we observe |
| 3527 | * !owner it means the list deletion is complete and we can indeed |
| 3528 | * free this event, otherwise we need to serialize on |
| 3529 | * owner->perf_event_mutex. |
| 3530 | */ |
| 3531 | smp_read_barrier_depends(); |
| 3532 | if (owner) { |
| 3533 | /* |
| 3534 | * Since delayed_put_task_struct() also drops the last |
| 3535 | * task reference we can safely take a new reference |
| 3536 | * while holding the rcu_read_lock(). |
| 3537 | */ |
| 3538 | get_task_struct(owner); |
| 3539 | } |
| 3540 | rcu_read_unlock(); |
| 3541 | |
| 3542 | if (owner) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3543 | /* |
| 3544 | * If we're here through perf_event_exit_task() we're already |
| 3545 | * holding ctx->mutex which would be an inversion wrt. the |
| 3546 | * normal lock order. |
| 3547 | * |
| 3548 | * However we can safely take this lock because its the child |
| 3549 | * ctx->mutex. |
| 3550 | */ |
| 3551 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); |
| 3552 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3553 | /* |
| 3554 | * We have to re-check the event->owner field, if it is cleared |
| 3555 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 3556 | * ensured they're done, and we can proceed with freeing the |
| 3557 | * event. |
| 3558 | */ |
| 3559 | if (event->owner) |
| 3560 | list_del_init(&event->owner_entry); |
| 3561 | mutex_unlock(&owner->perf_event_mutex); |
| 3562 | put_task_struct(owner); |
| 3563 | } |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | /* |
| 3567 | * Called when the last reference to the file is gone. |
| 3568 | */ |
| 3569 | static void put_event(struct perf_event *event) |
| 3570 | { |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 3571 | struct perf_event_context *ctx; |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3572 | |
| 3573 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 3574 | return; |
| 3575 | |
| 3576 | if (!is_kernel_event(event)) |
| 3577 | perf_remove_from_owner(event); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3578 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3579 | /* |
| 3580 | * There are two ways this annotation is useful: |
| 3581 | * |
| 3582 | * 1) there is a lock recursion from perf_event_exit_task |
| 3583 | * see the comment there. |
| 3584 | * |
| 3585 | * 2) there is a lock-inversion with mmap_sem through |
| 3586 | * perf_event_read_group(), which takes faults while |
| 3587 | * holding ctx->mutex, however this is called after |
| 3588 | * the last filedesc died, so there is no possibility |
| 3589 | * to trigger the AB-BA case. |
| 3590 | */ |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 3591 | ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING); |
| 3592 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3593 | perf_remove_from_context(event, true); |
Leon Yu | d415a7f | 2015-02-26 20:43:33 +0800 | [diff] [blame] | 3594 | perf_event_ctx_unlock(event, ctx); |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3595 | |
| 3596 | _free_event(event); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3597 | } |
| 3598 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3599 | int perf_event_release_kernel(struct perf_event *event) |
| 3600 | { |
| 3601 | put_event(event); |
| 3602 | return 0; |
| 3603 | } |
| 3604 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 3605 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3606 | static int perf_release(struct inode *inode, struct file *file) |
| 3607 | { |
| 3608 | put_event(file->private_data); |
| 3609 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3610 | } |
| 3611 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 3612 | /* |
| 3613 | * Remove all orphanes events from the context. |
| 3614 | */ |
| 3615 | static void orphans_remove_work(struct work_struct *work) |
| 3616 | { |
| 3617 | struct perf_event_context *ctx; |
| 3618 | struct perf_event *event, *tmp; |
| 3619 | |
| 3620 | ctx = container_of(work, struct perf_event_context, |
| 3621 | orphans_remove.work); |
| 3622 | |
| 3623 | mutex_lock(&ctx->mutex); |
| 3624 | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) { |
| 3625 | struct perf_event *parent_event = event->parent; |
| 3626 | |
| 3627 | if (!is_orphaned_child(event)) |
| 3628 | continue; |
| 3629 | |
| 3630 | perf_remove_from_context(event, true); |
| 3631 | |
| 3632 | mutex_lock(&parent_event->child_mutex); |
| 3633 | list_del_init(&event->child_list); |
| 3634 | mutex_unlock(&parent_event->child_mutex); |
| 3635 | |
| 3636 | free_event(event); |
| 3637 | put_event(parent_event); |
| 3638 | } |
| 3639 | |
| 3640 | raw_spin_lock_irq(&ctx->lock); |
| 3641 | ctx->orphans_remove_sched = false; |
| 3642 | raw_spin_unlock_irq(&ctx->lock); |
| 3643 | mutex_unlock(&ctx->mutex); |
| 3644 | |
| 3645 | put_ctx(ctx); |
| 3646 | } |
| 3647 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3648 | 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] | 3649 | { |
| 3650 | struct perf_event *child; |
| 3651 | u64 total = 0; |
| 3652 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3653 | *enabled = 0; |
| 3654 | *running = 0; |
| 3655 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3656 | mutex_lock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3657 | total += perf_event_read(event); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3658 | *enabled += event->total_time_enabled + |
| 3659 | atomic64_read(&event->child_total_time_enabled); |
| 3660 | *running += event->total_time_running + |
| 3661 | atomic64_read(&event->child_total_time_running); |
| 3662 | |
| 3663 | list_for_each_entry(child, &event->child_list, child_list) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3664 | total += perf_event_read(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3665 | *enabled += child->total_time_enabled; |
| 3666 | *running += child->total_time_running; |
| 3667 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3668 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3669 | |
| 3670 | return total; |
| 3671 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3672 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3673 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3674 | static int perf_event_read_group(struct perf_event *event, |
| 3675 | u64 read_format, char __user *buf) |
| 3676 | { |
| 3677 | struct perf_event *leader = event->group_leader, *sub; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3678 | struct perf_event_context *ctx = leader->ctx; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3679 | int n = 0, size = 0, ret; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3680 | u64 count, enabled, running; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3681 | u64 values[5]; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3682 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3683 | lockdep_assert_held(&ctx->mutex); |
| 3684 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3685 | count = perf_event_read_value(leader, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3686 | |
| 3687 | values[n++] = 1 + leader->nr_siblings; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3688 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3689 | values[n++] = enabled; |
| 3690 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3691 | values[n++] = running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3692 | values[n++] = count; |
| 3693 | if (read_format & PERF_FORMAT_ID) |
| 3694 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3695 | |
| 3696 | size = n * sizeof(u64); |
| 3697 | |
| 3698 | if (copy_to_user(buf, values, size)) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3699 | return -EFAULT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3700 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3701 | ret = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3702 | |
| 3703 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3704 | n = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3705 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3706 | values[n++] = perf_event_read_value(sub, &enabled, &running); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3707 | if (read_format & PERF_FORMAT_ID) |
| 3708 | values[n++] = primary_event_id(sub); |
| 3709 | |
| 3710 | size = n * sizeof(u64); |
| 3711 | |
Stephane Eranian | 184d3da | 2009-11-23 21:40:49 -0800 | [diff] [blame] | 3712 | if (copy_to_user(buf + ret, values, size)) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3713 | return -EFAULT; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3714 | } |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3715 | |
| 3716 | ret += size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3717 | } |
| 3718 | |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3719 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | static int perf_event_read_one(struct perf_event *event, |
| 3723 | u64 read_format, char __user *buf) |
| 3724 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3725 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3726 | u64 values[4]; |
| 3727 | int n = 0; |
| 3728 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3729 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 3730 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3731 | values[n++] = enabled; |
| 3732 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3733 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3734 | if (read_format & PERF_FORMAT_ID) |
| 3735 | values[n++] = primary_event_id(event); |
| 3736 | |
| 3737 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 3738 | return -EFAULT; |
| 3739 | |
| 3740 | return n * sizeof(u64); |
| 3741 | } |
| 3742 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 3743 | static bool is_event_hup(struct perf_event *event) |
| 3744 | { |
| 3745 | bool no_children; |
| 3746 | |
| 3747 | if (event->state != PERF_EVENT_STATE_EXIT) |
| 3748 | return false; |
| 3749 | |
| 3750 | mutex_lock(&event->child_mutex); |
| 3751 | no_children = list_empty(&event->child_list); |
| 3752 | mutex_unlock(&event->child_mutex); |
| 3753 | return no_children; |
| 3754 | } |
| 3755 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3756 | /* |
| 3757 | * Read the performance event - simple non blocking version for now |
| 3758 | */ |
| 3759 | static ssize_t |
| 3760 | perf_read_hw(struct perf_event *event, char __user *buf, size_t count) |
| 3761 | { |
| 3762 | u64 read_format = event->attr.read_format; |
| 3763 | int ret; |
| 3764 | |
| 3765 | /* |
| 3766 | * Return end-of-file for a read on a event that is in |
| 3767 | * error state (i.e. because it was pinned but it couldn't be |
| 3768 | * scheduled on to the CPU at some point). |
| 3769 | */ |
| 3770 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 3771 | return 0; |
| 3772 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 3773 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3774 | return -ENOSPC; |
| 3775 | |
| 3776 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3777 | if (read_format & PERF_FORMAT_GROUP) |
| 3778 | ret = perf_event_read_group(event, read_format, buf); |
| 3779 | else |
| 3780 | ret = perf_event_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3781 | |
| 3782 | return ret; |
| 3783 | } |
| 3784 | |
| 3785 | static ssize_t |
| 3786 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 3787 | { |
| 3788 | struct perf_event *event = file->private_data; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3789 | struct perf_event_context *ctx; |
| 3790 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3791 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3792 | ctx = perf_event_ctx_lock(event); |
| 3793 | ret = perf_read_hw(event, buf, count); |
| 3794 | perf_event_ctx_unlock(event, ctx); |
| 3795 | |
| 3796 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 3800 | { |
| 3801 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3802 | struct ring_buffer *rb; |
Jiri Olsa | 61b6768 | 2014-08-13 19:39:56 +0200 | [diff] [blame] | 3803 | unsigned int events = POLLHUP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3804 | |
Sebastian Andrzej Siewior | e708d7a | 2014-08-04 15:31:08 +0200 | [diff] [blame] | 3805 | poll_wait(file, &event->waitq, wait); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 3806 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 3807 | if (is_event_hup(event)) |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 3808 | return events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3809 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3810 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3811 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 3812 | * 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] | 3813 | */ |
| 3814 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3815 | rb = event->rb; |
| 3816 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3817 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 3818 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3819 | return events; |
| 3820 | } |
| 3821 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3822 | static void _perf_event_reset(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3823 | { |
| 3824 | (void)perf_event_read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3825 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3826 | perf_event_update_userpage(event); |
| 3827 | } |
| 3828 | |
| 3829 | /* |
| 3830 | * Holding the top-level event's child_mutex means that any |
| 3831 | * descendant process that has inherited this event will block |
| 3832 | * in sync_child_event if it goes to exit, thus satisfying the |
| 3833 | * task existence requirements of perf_event_enable/disable. |
| 3834 | */ |
| 3835 | static void perf_event_for_each_child(struct perf_event *event, |
| 3836 | void (*func)(struct perf_event *)) |
| 3837 | { |
| 3838 | struct perf_event *child; |
| 3839 | |
| 3840 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3841 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3842 | mutex_lock(&event->child_mutex); |
| 3843 | func(event); |
| 3844 | list_for_each_entry(child, &event->child_list, child_list) |
| 3845 | func(child); |
| 3846 | mutex_unlock(&event->child_mutex); |
| 3847 | } |
| 3848 | |
| 3849 | static void perf_event_for_each(struct perf_event *event, |
| 3850 | void (*func)(struct perf_event *)) |
| 3851 | { |
| 3852 | struct perf_event_context *ctx = event->ctx; |
| 3853 | struct perf_event *sibling; |
| 3854 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3855 | lockdep_assert_held(&ctx->mutex); |
| 3856 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3857 | event = event->group_leader; |
| 3858 | |
| 3859 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3860 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 3861 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 3865 | { |
| 3866 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3867 | int ret = 0, active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3868 | u64 value; |
| 3869 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 3870 | if (!is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3871 | return -EINVAL; |
| 3872 | |
John Blackwood | ad0cf34 | 2010-09-28 18:03:11 -0400 | [diff] [blame] | 3873 | if (copy_from_user(&value, arg, sizeof(value))) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3874 | return -EFAULT; |
| 3875 | |
| 3876 | if (!value) |
| 3877 | return -EINVAL; |
| 3878 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3879 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3880 | if (event->attr.freq) { |
| 3881 | if (value > sysctl_perf_event_sample_rate) { |
| 3882 | ret = -EINVAL; |
| 3883 | goto unlock; |
| 3884 | } |
| 3885 | |
| 3886 | event->attr.sample_freq = value; |
| 3887 | } else { |
| 3888 | event->attr.sample_period = value; |
| 3889 | event->hw.sample_period = value; |
| 3890 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 3891 | |
| 3892 | active = (event->state == PERF_EVENT_STATE_ACTIVE); |
| 3893 | if (active) { |
| 3894 | perf_pmu_disable(ctx->pmu); |
| 3895 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3896 | } |
| 3897 | |
| 3898 | local64_set(&event->hw.period_left, 0); |
| 3899 | |
| 3900 | if (active) { |
| 3901 | event->pmu->start(event, PERF_EF_RELOAD); |
| 3902 | perf_pmu_enable(ctx->pmu); |
| 3903 | } |
| 3904 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3905 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3906 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3907 | |
| 3908 | return ret; |
| 3909 | } |
| 3910 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3911 | static const struct file_operations perf_fops; |
| 3912 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3913 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3914 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3915 | struct fd f = fdget(fd); |
| 3916 | if (!f.file) |
| 3917 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3918 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3919 | if (f.file->f_op != &perf_fops) { |
| 3920 | fdput(f); |
| 3921 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3922 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3923 | *p = f; |
| 3924 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | static int perf_event_set_output(struct perf_event *event, |
| 3928 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3929 | 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] | 3930 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3931 | static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3932 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3933 | void (*func)(struct perf_event *); |
| 3934 | u32 flags = arg; |
| 3935 | |
| 3936 | switch (cmd) { |
| 3937 | case PERF_EVENT_IOC_ENABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3938 | func = _perf_event_enable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3939 | break; |
| 3940 | case PERF_EVENT_IOC_DISABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3941 | func = _perf_event_disable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3942 | break; |
| 3943 | case PERF_EVENT_IOC_RESET: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3944 | func = _perf_event_reset; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3945 | break; |
| 3946 | |
| 3947 | case PERF_EVENT_IOC_REFRESH: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3948 | return _perf_event_refresh(event, arg); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3949 | |
| 3950 | case PERF_EVENT_IOC_PERIOD: |
| 3951 | return perf_event_period(event, (u64 __user *)arg); |
| 3952 | |
Jiri Olsa | cf4957f | 2012-10-24 13:37:58 +0200 | [diff] [blame] | 3953 | case PERF_EVENT_IOC_ID: |
| 3954 | { |
| 3955 | u64 id = primary_event_id(event); |
| 3956 | |
| 3957 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) |
| 3958 | return -EFAULT; |
| 3959 | return 0; |
| 3960 | } |
| 3961 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3962 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3963 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3964 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3965 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3966 | struct perf_event *output_event; |
| 3967 | struct fd output; |
| 3968 | ret = perf_fget_light(arg, &output); |
| 3969 | if (ret) |
| 3970 | return ret; |
| 3971 | output_event = output.file->private_data; |
| 3972 | ret = perf_event_set_output(event, output_event); |
| 3973 | fdput(output); |
| 3974 | } else { |
| 3975 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3976 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 3977 | return ret; |
| 3978 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3979 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3980 | case PERF_EVENT_IOC_SET_FILTER: |
| 3981 | return perf_event_set_filter(event, (void __user *)arg); |
| 3982 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3983 | default: |
| 3984 | return -ENOTTY; |
| 3985 | } |
| 3986 | |
| 3987 | if (flags & PERF_IOC_FLAG_GROUP) |
| 3988 | perf_event_for_each(event, func); |
| 3989 | else |
| 3990 | perf_event_for_each_child(event, func); |
| 3991 | |
| 3992 | return 0; |
| 3993 | } |
| 3994 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3995 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 3996 | { |
| 3997 | struct perf_event *event = file->private_data; |
| 3998 | struct perf_event_context *ctx; |
| 3999 | long ret; |
| 4000 | |
| 4001 | ctx = perf_event_ctx_lock(event); |
| 4002 | ret = _perf_ioctl(event, cmd, arg); |
| 4003 | perf_event_ctx_unlock(event, ctx); |
| 4004 | |
| 4005 | return ret; |
| 4006 | } |
| 4007 | |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4008 | #ifdef CONFIG_COMPAT |
| 4009 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, |
| 4010 | unsigned long arg) |
| 4011 | { |
| 4012 | switch (_IOC_NR(cmd)) { |
| 4013 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): |
| 4014 | case _IOC_NR(PERF_EVENT_IOC_ID): |
| 4015 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
| 4016 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { |
| 4017 | cmd &= ~IOCSIZE_MASK; |
| 4018 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; |
| 4019 | } |
| 4020 | break; |
| 4021 | } |
| 4022 | return perf_ioctl(file, cmd, arg); |
| 4023 | } |
| 4024 | #else |
| 4025 | # define perf_compat_ioctl NULL |
| 4026 | #endif |
| 4027 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4028 | int perf_event_task_enable(void) |
| 4029 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4030 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4031 | struct perf_event *event; |
| 4032 | |
| 4033 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4034 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4035 | ctx = perf_event_ctx_lock(event); |
| 4036 | perf_event_for_each_child(event, _perf_event_enable); |
| 4037 | perf_event_ctx_unlock(event, ctx); |
| 4038 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4039 | mutex_unlock(¤t->perf_event_mutex); |
| 4040 | |
| 4041 | return 0; |
| 4042 | } |
| 4043 | |
| 4044 | int perf_event_task_disable(void) |
| 4045 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4046 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4047 | struct perf_event *event; |
| 4048 | |
| 4049 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4050 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4051 | ctx = perf_event_ctx_lock(event); |
| 4052 | perf_event_for_each_child(event, _perf_event_disable); |
| 4053 | perf_event_ctx_unlock(event, ctx); |
| 4054 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4055 | mutex_unlock(¤t->perf_event_mutex); |
| 4056 | |
| 4057 | return 0; |
| 4058 | } |
| 4059 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4060 | static int perf_event_index(struct perf_event *event) |
| 4061 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 4062 | if (event->hw.state & PERF_HES_STOPPED) |
| 4063 | return 0; |
| 4064 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4065 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 4066 | return 0; |
| 4067 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 4068 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4069 | } |
| 4070 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4071 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4072 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4073 | u64 *enabled, |
| 4074 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4075 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4076 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4077 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4078 | *now = perf_clock(); |
| 4079 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4080 | *enabled = ctx_time - event->tstamp_enabled; |
| 4081 | *running = ctx_time - event->tstamp_running; |
| 4082 | } |
| 4083 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4084 | static void perf_event_init_userpage(struct perf_event *event) |
| 4085 | { |
| 4086 | struct perf_event_mmap_page *userpg; |
| 4087 | struct ring_buffer *rb; |
| 4088 | |
| 4089 | rcu_read_lock(); |
| 4090 | rb = rcu_dereference(event->rb); |
| 4091 | if (!rb) |
| 4092 | goto unlock; |
| 4093 | |
| 4094 | userpg = rb->user_page; |
| 4095 | |
| 4096 | /* Allow new userspace to detect that bit 0 is deprecated */ |
| 4097 | userpg->cap_bit0_is_deprecated = 1; |
| 4098 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); |
| 4099 | |
| 4100 | unlock: |
| 4101 | rcu_read_unlock(); |
| 4102 | } |
| 4103 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4104 | void __weak arch_perf_update_userpage( |
| 4105 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4106 | { |
| 4107 | } |
| 4108 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4109 | /* |
| 4110 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 4111 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 4112 | * code calls this from NMI context. |
| 4113 | */ |
| 4114 | void perf_event_update_userpage(struct perf_event *event) |
| 4115 | { |
| 4116 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4117 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4118 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4119 | |
| 4120 | rcu_read_lock(); |
Peter Zijlstra | 5ec4c59 | 2013-08-02 21:16:30 +0200 | [diff] [blame] | 4121 | rb = rcu_dereference(event->rb); |
| 4122 | if (!rb) |
| 4123 | goto unlock; |
| 4124 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4125 | /* |
| 4126 | * compute total_time_enabled, total_time_running |
| 4127 | * based on snapshot values taken when the event |
| 4128 | * was last scheduled in. |
| 4129 | * |
| 4130 | * we cannot simply called update_context_time() |
| 4131 | * because of locking issue as we can be called in |
| 4132 | * NMI context |
| 4133 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4134 | calc_timer_values(event, &now, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4135 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4136 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4137 | /* |
| 4138 | * Disable preemption so as to not let the corresponding user-space |
| 4139 | * spin too long if we get preempted. |
| 4140 | */ |
| 4141 | preempt_disable(); |
| 4142 | ++userpg->lock; |
| 4143 | barrier(); |
| 4144 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4145 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 4146 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4147 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4148 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4149 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4150 | atomic64_read(&event->child_total_time_enabled); |
| 4151 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4152 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4153 | atomic64_read(&event->child_total_time_running); |
| 4154 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4155 | arch_perf_update_userpage(event, userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4156 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4157 | barrier(); |
| 4158 | ++userpg->lock; |
| 4159 | preempt_enable(); |
| 4160 | unlock: |
| 4161 | rcu_read_unlock(); |
| 4162 | } |
| 4163 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4164 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 4165 | { |
| 4166 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4167 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4168 | int ret = VM_FAULT_SIGBUS; |
| 4169 | |
| 4170 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 4171 | if (vmf->pgoff == 0) |
| 4172 | ret = 0; |
| 4173 | return ret; |
| 4174 | } |
| 4175 | |
| 4176 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4177 | rb = rcu_dereference(event->rb); |
| 4178 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4179 | goto unlock; |
| 4180 | |
| 4181 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 4182 | goto unlock; |
| 4183 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4184 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4185 | if (!vmf->page) |
| 4186 | goto unlock; |
| 4187 | |
| 4188 | get_page(vmf->page); |
| 4189 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 4190 | vmf->page->index = vmf->pgoff; |
| 4191 | |
| 4192 | ret = 0; |
| 4193 | unlock: |
| 4194 | rcu_read_unlock(); |
| 4195 | |
| 4196 | return ret; |
| 4197 | } |
| 4198 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4199 | static void ring_buffer_attach(struct perf_event *event, |
| 4200 | struct ring_buffer *rb) |
| 4201 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4202 | struct ring_buffer *old_rb = NULL; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4203 | unsigned long flags; |
| 4204 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4205 | if (event->rb) { |
| 4206 | /* |
| 4207 | * Should be impossible, we set this when removing |
| 4208 | * event->rb_entry and wait/clear when adding event->rb_entry. |
| 4209 | */ |
| 4210 | WARN_ON_ONCE(event->rcu_pending); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4211 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4212 | old_rb = event->rb; |
| 4213 | event->rcu_batches = get_state_synchronize_rcu(); |
| 4214 | event->rcu_pending = 1; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4215 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4216 | spin_lock_irqsave(&old_rb->event_lock, flags); |
| 4217 | list_del_rcu(&event->rb_entry); |
| 4218 | spin_unlock_irqrestore(&old_rb->event_lock, flags); |
| 4219 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4220 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4221 | if (event->rcu_pending && rb) { |
| 4222 | cond_synchronize_rcu(event->rcu_batches); |
| 4223 | event->rcu_pending = 0; |
| 4224 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4225 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4226 | if (rb) { |
| 4227 | spin_lock_irqsave(&rb->event_lock, flags); |
| 4228 | list_add_rcu(&event->rb_entry, &rb->event_list); |
| 4229 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 4230 | } |
| 4231 | |
| 4232 | rcu_assign_pointer(event->rb, rb); |
| 4233 | |
| 4234 | if (old_rb) { |
| 4235 | ring_buffer_put(old_rb); |
| 4236 | /* |
| 4237 | * Since we detached before setting the new rb, so that we |
| 4238 | * could attach the new rb, we could have missed a wakeup. |
| 4239 | * Provide it now. |
| 4240 | */ |
| 4241 | wake_up_all(&event->waitq); |
| 4242 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | static void ring_buffer_wakeup(struct perf_event *event) |
| 4246 | { |
| 4247 | struct ring_buffer *rb; |
| 4248 | |
| 4249 | rcu_read_lock(); |
| 4250 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4251 | if (rb) { |
| 4252 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 4253 | wake_up_all(&event->waitq); |
| 4254 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4255 | rcu_read_unlock(); |
| 4256 | } |
| 4257 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4258 | static void rb_free_rcu(struct rcu_head *rcu_head) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4259 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4260 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4261 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4262 | rb = container_of(rcu_head, struct ring_buffer, rcu_head); |
| 4263 | rb_free(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4264 | } |
| 4265 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4266 | static struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4267 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4268 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4269 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4270 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4271 | rb = rcu_dereference(event->rb); |
| 4272 | if (rb) { |
| 4273 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 4274 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4275 | } |
| 4276 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4277 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4278 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4279 | } |
| 4280 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4281 | static void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4282 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4283 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4284 | return; |
| 4285 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4286 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4287 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4288 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 4292 | { |
| 4293 | struct perf_event *event = vma->vm_file->private_data; |
| 4294 | |
| 4295 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4296 | atomic_inc(&event->rb->mmap_count); |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4297 | |
| 4298 | if (event->pmu->event_mapped) |
| 4299 | event->pmu->event_mapped(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4300 | } |
| 4301 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4302 | /* |
| 4303 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 4304 | * event, or through other events by use of perf_event_set_output(). |
| 4305 | * |
| 4306 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 4307 | * the buffer here, where we still have a VM context. This means we need |
| 4308 | * to detach all events redirecting to us. |
| 4309 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4310 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 4311 | { |
| 4312 | struct perf_event *event = vma->vm_file->private_data; |
| 4313 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4314 | struct ring_buffer *rb = ring_buffer_get(event); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4315 | struct user_struct *mmap_user = rb->mmap_user; |
| 4316 | int mmap_locked = rb->mmap_locked; |
| 4317 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4318 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4319 | if (event->pmu->event_unmapped) |
| 4320 | event->pmu->event_unmapped(event); |
| 4321 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4322 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4323 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4324 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4325 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4326 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4327 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4328 | mutex_unlock(&event->mmap_mutex); |
| 4329 | |
| 4330 | /* 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] | 4331 | if (atomic_read(&rb->mmap_count)) |
| 4332 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4333 | |
| 4334 | /* |
| 4335 | * No other mmap()s, detach from all other events that might redirect |
| 4336 | * into the now unreachable buffer. Somewhat complicated by the |
| 4337 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 4338 | */ |
| 4339 | again: |
| 4340 | rcu_read_lock(); |
| 4341 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 4342 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 4343 | /* |
| 4344 | * This event is en-route to free_event() which will |
| 4345 | * detach it and remove it from the list. |
| 4346 | */ |
| 4347 | continue; |
| 4348 | } |
| 4349 | rcu_read_unlock(); |
| 4350 | |
| 4351 | mutex_lock(&event->mmap_mutex); |
| 4352 | /* |
| 4353 | * Check we didn't race with perf_event_set_output() which can |
| 4354 | * swizzle the rb from under us while we were waiting to |
| 4355 | * acquire mmap_mutex. |
| 4356 | * |
| 4357 | * If we find a different rb; ignore this event, a next |
| 4358 | * iteration will no longer find it on the list. We have to |
| 4359 | * still restart the iteration to make sure we're not now |
| 4360 | * iterating the wrong list. |
| 4361 | */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4362 | if (event->rb == rb) |
| 4363 | ring_buffer_attach(event, NULL); |
| 4364 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4365 | mutex_unlock(&event->mmap_mutex); |
| 4366 | put_event(event); |
| 4367 | |
| 4368 | /* |
| 4369 | * Restart the iteration; either we're on the wrong list or |
| 4370 | * destroyed its integrity by doing a deletion. |
| 4371 | */ |
| 4372 | goto again; |
| 4373 | } |
| 4374 | rcu_read_unlock(); |
| 4375 | |
| 4376 | /* |
| 4377 | * It could be there's still a few 0-ref events on the list; they'll |
| 4378 | * get cleaned up by free_event() -- they'll also still have their |
| 4379 | * ref on the rb and will free it whenever they are done with it. |
| 4380 | * |
| 4381 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 4382 | * undo the VM accounting. |
| 4383 | */ |
| 4384 | |
| 4385 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 4386 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 4387 | free_uid(mmap_user); |
| 4388 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4389 | out_put: |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4390 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4391 | } |
| 4392 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 4393 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4394 | .open = perf_mmap_open, |
| 4395 | .close = perf_mmap_close, |
| 4396 | .fault = perf_mmap_fault, |
| 4397 | .page_mkwrite = perf_mmap_fault, |
| 4398 | }; |
| 4399 | |
| 4400 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 4401 | { |
| 4402 | struct perf_event *event = file->private_data; |
| 4403 | unsigned long user_locked, user_lock_limit; |
| 4404 | struct user_struct *user = current_user(); |
| 4405 | unsigned long locked, lock_limit; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4406 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4407 | unsigned long vma_size; |
| 4408 | unsigned long nr_pages; |
| 4409 | long user_extra, extra; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4410 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4411 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4412 | /* |
| 4413 | * Don't allow mmap() of inherited per-task counters. This would |
| 4414 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4415 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4416 | */ |
| 4417 | if (event->cpu == -1 && event->attr.inherit) |
| 4418 | return -EINVAL; |
| 4419 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4420 | if (!(vma->vm_flags & VM_SHARED)) |
| 4421 | return -EINVAL; |
| 4422 | |
| 4423 | vma_size = vma->vm_end - vma->vm_start; |
| 4424 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 4425 | |
| 4426 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4427 | * 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] | 4428 | * can do bitmasks instead of modulo. |
| 4429 | */ |
| 4430 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
| 4431 | return -EINVAL; |
| 4432 | |
| 4433 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 4434 | return -EINVAL; |
| 4435 | |
| 4436 | if (vma->vm_pgoff != 0) |
| 4437 | return -EINVAL; |
| 4438 | |
| 4439 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4440 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4441 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4442 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4443 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4444 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4445 | goto unlock; |
| 4446 | } |
| 4447 | |
| 4448 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 4449 | /* |
| 4450 | * Raced against perf_mmap_close() through |
| 4451 | * perf_event_set_output(). Try again, hope for better |
| 4452 | * luck. |
| 4453 | */ |
| 4454 | mutex_unlock(&event->mmap_mutex); |
| 4455 | goto again; |
| 4456 | } |
| 4457 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4458 | goto unlock; |
| 4459 | } |
| 4460 | |
| 4461 | user_extra = nr_pages + 1; |
| 4462 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 4463 | |
| 4464 | /* |
| 4465 | * Increase the limit linearly with more CPUs: |
| 4466 | */ |
| 4467 | user_lock_limit *= num_online_cpus(); |
| 4468 | |
| 4469 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 4470 | |
| 4471 | extra = 0; |
| 4472 | if (user_locked > user_lock_limit) |
| 4473 | extra = user_locked - user_lock_limit; |
| 4474 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 4475 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4476 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 4477 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4478 | |
| 4479 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 4480 | !capable(CAP_IPC_LOCK)) { |
| 4481 | ret = -EPERM; |
| 4482 | goto unlock; |
| 4483 | } |
| 4484 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4485 | WARN_ON(event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4486 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4487 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4488 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4489 | |
Vince Weaver | 4ec8363 | 2011-06-01 15:15:36 -0400 | [diff] [blame] | 4490 | rb = rb_alloc(nr_pages, |
| 4491 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 4492 | event->cpu, flags); |
| 4493 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4494 | if (!rb) { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4495 | ret = -ENOMEM; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4496 | goto unlock; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4497 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4498 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4499 | atomic_set(&rb->mmap_count, 1); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4500 | rb->mmap_locked = extra; |
| 4501 | rb->mmap_user = get_current_user(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4502 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4503 | atomic_long_add(user_extra, &user->locked_vm); |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4504 | vma->vm_mm->pinned_vm += extra; |
| 4505 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4506 | ring_buffer_attach(event, rb); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4507 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4508 | perf_event_init_userpage(event); |
Peter Zijlstra | 9a0f05c | 2011-11-21 15:13:29 +0100 | [diff] [blame] | 4509 | perf_event_update_userpage(event); |
| 4510 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4511 | unlock: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4512 | if (!ret) |
| 4513 | atomic_inc(&event->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4514 | mutex_unlock(&event->mmap_mutex); |
| 4515 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4516 | /* |
| 4517 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 4518 | * vma. |
| 4519 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4520 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4521 | vma->vm_ops = &perf_mmap_vmops; |
| 4522 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4523 | if (event->pmu->event_mapped) |
| 4524 | event->pmu->event_mapped(event); |
| 4525 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4526 | return ret; |
| 4527 | } |
| 4528 | |
| 4529 | static int perf_fasync(int fd, struct file *filp, int on) |
| 4530 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4531 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4532 | struct perf_event *event = filp->private_data; |
| 4533 | int retval; |
| 4534 | |
| 4535 | mutex_lock(&inode->i_mutex); |
| 4536 | retval = fasync_helper(fd, filp, on, &event->fasync); |
| 4537 | mutex_unlock(&inode->i_mutex); |
| 4538 | |
| 4539 | if (retval < 0) |
| 4540 | return retval; |
| 4541 | |
| 4542 | return 0; |
| 4543 | } |
| 4544 | |
| 4545 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 4546 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4547 | .release = perf_release, |
| 4548 | .read = perf_read, |
| 4549 | .poll = perf_poll, |
| 4550 | .unlocked_ioctl = perf_ioctl, |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4551 | .compat_ioctl = perf_compat_ioctl, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4552 | .mmap = perf_mmap, |
| 4553 | .fasync = perf_fasync, |
| 4554 | }; |
| 4555 | |
| 4556 | /* |
| 4557 | * Perf event wakeup |
| 4558 | * |
| 4559 | * If there's data, ensure we set the poll() state and publish everything |
| 4560 | * to user-space before waking everybody up. |
| 4561 | */ |
| 4562 | |
| 4563 | void perf_event_wakeup(struct perf_event *event) |
| 4564 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4565 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4566 | |
| 4567 | if (event->pending_kill) { |
| 4568 | kill_fasync(&event->fasync, SIGIO, event->pending_kill); |
| 4569 | event->pending_kill = 0; |
| 4570 | } |
| 4571 | } |
| 4572 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 4573 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4574 | { |
| 4575 | struct perf_event *event = container_of(entry, |
| 4576 | struct perf_event, pending); |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 4577 | int rctx; |
| 4578 | |
| 4579 | rctx = perf_swevent_get_recursion_context(); |
| 4580 | /* |
| 4581 | * If we 'fail' here, that's OK, it means recursion is already disabled |
| 4582 | * and we won't recurse 'further'. |
| 4583 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4584 | |
| 4585 | if (event->pending_disable) { |
| 4586 | event->pending_disable = 0; |
| 4587 | __perf_event_disable(event); |
| 4588 | } |
| 4589 | |
| 4590 | if (event->pending_wakeup) { |
| 4591 | event->pending_wakeup = 0; |
| 4592 | perf_event_wakeup(event); |
| 4593 | } |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 4594 | |
| 4595 | if (rctx >= 0) |
| 4596 | perf_swevent_put_recursion_context(rctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4597 | } |
| 4598 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4599 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 4600 | * We assume there is only KVM supporting the callbacks. |
| 4601 | * Later on, we might change it to a list if there is |
| 4602 | * another virtualization implementation supporting the callbacks. |
| 4603 | */ |
| 4604 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 4605 | |
| 4606 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4607 | { |
| 4608 | perf_guest_cbs = cbs; |
| 4609 | return 0; |
| 4610 | } |
| 4611 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 4612 | |
| 4613 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 4614 | { |
| 4615 | perf_guest_cbs = NULL; |
| 4616 | return 0; |
| 4617 | } |
| 4618 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 4619 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4620 | static void |
| 4621 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 4622 | struct pt_regs *regs, u64 mask) |
| 4623 | { |
| 4624 | int bit; |
| 4625 | |
| 4626 | for_each_set_bit(bit, (const unsigned long *) &mask, |
| 4627 | sizeof(mask) * BITS_PER_BYTE) { |
| 4628 | u64 val; |
| 4629 | |
| 4630 | val = perf_reg_value(regs, bit); |
| 4631 | perf_output_put(handle, val); |
| 4632 | } |
| 4633 | } |
| 4634 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 4635 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 4636 | struct pt_regs *regs, |
| 4637 | struct pt_regs *regs_user_copy) |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4638 | { |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 4639 | if (user_mode(regs)) { |
| 4640 | regs_user->abi = perf_reg_abi(current); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 4641 | regs_user->regs = regs; |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 4642 | } else if (current->mm) { |
| 4643 | perf_get_regs_user(regs_user, regs, regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 4644 | } else { |
| 4645 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; |
| 4646 | regs_user->regs = NULL; |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 4647 | } |
| 4648 | } |
| 4649 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 4650 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
| 4651 | struct pt_regs *regs) |
| 4652 | { |
| 4653 | regs_intr->regs = regs; |
| 4654 | regs_intr->abi = perf_reg_abi(current); |
| 4655 | } |
| 4656 | |
| 4657 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 4658 | /* |
| 4659 | * Get remaining task size from user stack pointer. |
| 4660 | * |
| 4661 | * It'd be better to take stack vma map and limit this more |
| 4662 | * precisly, but there's no way to get it safely under interrupt, |
| 4663 | * so using TASK_SIZE as limit. |
| 4664 | */ |
| 4665 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 4666 | { |
| 4667 | unsigned long addr = perf_user_stack_pointer(regs); |
| 4668 | |
| 4669 | if (!addr || addr >= TASK_SIZE) |
| 4670 | return 0; |
| 4671 | |
| 4672 | return TASK_SIZE - addr; |
| 4673 | } |
| 4674 | |
| 4675 | static u16 |
| 4676 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 4677 | struct pt_regs *regs) |
| 4678 | { |
| 4679 | u64 task_size; |
| 4680 | |
| 4681 | /* No regs, no stack pointer, no dump. */ |
| 4682 | if (!regs) |
| 4683 | return 0; |
| 4684 | |
| 4685 | /* |
| 4686 | * Check if we fit in with the requested stack size into the: |
| 4687 | * - TASK_SIZE |
| 4688 | * If we don't, we limit the size to the TASK_SIZE. |
| 4689 | * |
| 4690 | * - remaining sample size |
| 4691 | * If we don't, we customize the stack size to |
| 4692 | * fit in to the remaining sample size. |
| 4693 | */ |
| 4694 | |
| 4695 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 4696 | stack_size = min(stack_size, (u16) task_size); |
| 4697 | |
| 4698 | /* Current header size plus static size and dynamic size. */ |
| 4699 | header_size += 2 * sizeof(u64); |
| 4700 | |
| 4701 | /* Do we fit in with the current stack dump size? */ |
| 4702 | if ((u16) (header_size + stack_size) < header_size) { |
| 4703 | /* |
| 4704 | * If we overflow the maximum size for the sample, |
| 4705 | * we customize the stack dump size to fit in. |
| 4706 | */ |
| 4707 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 4708 | stack_size = round_up(stack_size, sizeof(u64)); |
| 4709 | } |
| 4710 | |
| 4711 | return stack_size; |
| 4712 | } |
| 4713 | |
| 4714 | static void |
| 4715 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 4716 | struct pt_regs *regs) |
| 4717 | { |
| 4718 | /* Case of a kernel thread, nothing to dump */ |
| 4719 | if (!regs) { |
| 4720 | u64 size = 0; |
| 4721 | perf_output_put(handle, size); |
| 4722 | } else { |
| 4723 | unsigned long sp; |
| 4724 | unsigned int rem; |
| 4725 | u64 dyn_size; |
| 4726 | |
| 4727 | /* |
| 4728 | * We dump: |
| 4729 | * static size |
| 4730 | * - the size requested by user or the best one we can fit |
| 4731 | * in to the sample max size |
| 4732 | * data |
| 4733 | * - user stack dump data |
| 4734 | * dynamic size |
| 4735 | * - the actual dumped size |
| 4736 | */ |
| 4737 | |
| 4738 | /* Static size. */ |
| 4739 | perf_output_put(handle, dump_size); |
| 4740 | |
| 4741 | /* Data. */ |
| 4742 | sp = perf_user_stack_pointer(regs); |
| 4743 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 4744 | dyn_size = dump_size - rem; |
| 4745 | |
| 4746 | perf_output_skip(handle, rem); |
| 4747 | |
| 4748 | /* Dynamic size. */ |
| 4749 | perf_output_put(handle, dyn_size); |
| 4750 | } |
| 4751 | } |
| 4752 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4753 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 4754 | struct perf_sample_data *data, |
| 4755 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4756 | { |
| 4757 | u64 sample_type = event->attr.sample_type; |
| 4758 | |
| 4759 | data->type = sample_type; |
| 4760 | header->size += event->id_header_size; |
| 4761 | |
| 4762 | if (sample_type & PERF_SAMPLE_TID) { |
| 4763 | /* namespace issues */ |
| 4764 | data->tid_entry.pid = perf_event_pid(event, current); |
| 4765 | data->tid_entry.tid = perf_event_tid(event, current); |
| 4766 | } |
| 4767 | |
| 4768 | if (sample_type & PERF_SAMPLE_TIME) |
| 4769 | data->time = perf_clock(); |
| 4770 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4771 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 4772 | data->id = primary_event_id(event); |
| 4773 | |
| 4774 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4775 | data->stream_id = event->id; |
| 4776 | |
| 4777 | if (sample_type & PERF_SAMPLE_CPU) { |
| 4778 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 4779 | data->cpu_entry.reserved = 0; |
| 4780 | } |
| 4781 | } |
| 4782 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4783 | void perf_event_header__init_id(struct perf_event_header *header, |
| 4784 | struct perf_sample_data *data, |
| 4785 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4786 | { |
| 4787 | if (event->attr.sample_id_all) |
| 4788 | __perf_event_header__init_id(header, data, event); |
| 4789 | } |
| 4790 | |
| 4791 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 4792 | struct perf_sample_data *data) |
| 4793 | { |
| 4794 | u64 sample_type = data->type; |
| 4795 | |
| 4796 | if (sample_type & PERF_SAMPLE_TID) |
| 4797 | perf_output_put(handle, data->tid_entry); |
| 4798 | |
| 4799 | if (sample_type & PERF_SAMPLE_TIME) |
| 4800 | perf_output_put(handle, data->time); |
| 4801 | |
| 4802 | if (sample_type & PERF_SAMPLE_ID) |
| 4803 | perf_output_put(handle, data->id); |
| 4804 | |
| 4805 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4806 | perf_output_put(handle, data->stream_id); |
| 4807 | |
| 4808 | if (sample_type & PERF_SAMPLE_CPU) |
| 4809 | perf_output_put(handle, data->cpu_entry); |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4810 | |
| 4811 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4812 | perf_output_put(handle, data->id); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4813 | } |
| 4814 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4815 | void perf_event__output_id_sample(struct perf_event *event, |
| 4816 | struct perf_output_handle *handle, |
| 4817 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 4818 | { |
| 4819 | if (event->attr.sample_id_all) |
| 4820 | __perf_event__output_id_sample(handle, sample); |
| 4821 | } |
| 4822 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4823 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4824 | struct perf_event *event, |
| 4825 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4826 | { |
| 4827 | u64 read_format = event->attr.read_format; |
| 4828 | u64 values[4]; |
| 4829 | int n = 0; |
| 4830 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4831 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4832 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4833 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4834 | atomic64_read(&event->child_total_time_enabled); |
| 4835 | } |
| 4836 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4837 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4838 | atomic64_read(&event->child_total_time_running); |
| 4839 | } |
| 4840 | if (read_format & PERF_FORMAT_ID) |
| 4841 | values[n++] = primary_event_id(event); |
| 4842 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4843 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4844 | } |
| 4845 | |
| 4846 | /* |
| 4847 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 4848 | */ |
| 4849 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4850 | struct perf_event *event, |
| 4851 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4852 | { |
| 4853 | struct perf_event *leader = event->group_leader, *sub; |
| 4854 | u64 read_format = event->attr.read_format; |
| 4855 | u64 values[5]; |
| 4856 | int n = 0; |
| 4857 | |
| 4858 | values[n++] = 1 + leader->nr_siblings; |
| 4859 | |
| 4860 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4861 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4862 | |
| 4863 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4864 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4865 | |
| 4866 | if (leader != event) |
| 4867 | leader->pmu->read(leader); |
| 4868 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4869 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4870 | if (read_format & PERF_FORMAT_ID) |
| 4871 | values[n++] = primary_event_id(leader); |
| 4872 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4873 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4874 | |
| 4875 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 4876 | n = 0; |
| 4877 | |
Jiri Olsa | 6f5ab00 | 2012-10-15 20:13:45 +0200 | [diff] [blame] | 4878 | if ((sub != event) && |
| 4879 | (sub->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4880 | sub->pmu->read(sub); |
| 4881 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4882 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4883 | if (read_format & PERF_FORMAT_ID) |
| 4884 | values[n++] = primary_event_id(sub); |
| 4885 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4886 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4887 | } |
| 4888 | } |
| 4889 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4890 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 4891 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4892 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4893 | static void perf_output_read(struct perf_output_handle *handle, |
| 4894 | struct perf_event *event) |
| 4895 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4896 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4897 | u64 read_format = event->attr.read_format; |
| 4898 | |
| 4899 | /* |
| 4900 | * compute total_time_enabled, total_time_running |
| 4901 | * based on snapshot values taken when the event |
| 4902 | * was last scheduled in. |
| 4903 | * |
| 4904 | * we cannot simply called update_context_time() |
| 4905 | * because of locking issue as we are called in |
| 4906 | * NMI context |
| 4907 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4908 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4909 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4910 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4911 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4912 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4913 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 4914 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | void perf_output_sample(struct perf_output_handle *handle, |
| 4918 | struct perf_event_header *header, |
| 4919 | struct perf_sample_data *data, |
| 4920 | struct perf_event *event) |
| 4921 | { |
| 4922 | u64 sample_type = data->type; |
| 4923 | |
| 4924 | perf_output_put(handle, *header); |
| 4925 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 4926 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 4927 | perf_output_put(handle, data->id); |
| 4928 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4929 | if (sample_type & PERF_SAMPLE_IP) |
| 4930 | perf_output_put(handle, data->ip); |
| 4931 | |
| 4932 | if (sample_type & PERF_SAMPLE_TID) |
| 4933 | perf_output_put(handle, data->tid_entry); |
| 4934 | |
| 4935 | if (sample_type & PERF_SAMPLE_TIME) |
| 4936 | perf_output_put(handle, data->time); |
| 4937 | |
| 4938 | if (sample_type & PERF_SAMPLE_ADDR) |
| 4939 | perf_output_put(handle, data->addr); |
| 4940 | |
| 4941 | if (sample_type & PERF_SAMPLE_ID) |
| 4942 | perf_output_put(handle, data->id); |
| 4943 | |
| 4944 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 4945 | perf_output_put(handle, data->stream_id); |
| 4946 | |
| 4947 | if (sample_type & PERF_SAMPLE_CPU) |
| 4948 | perf_output_put(handle, data->cpu_entry); |
| 4949 | |
| 4950 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 4951 | perf_output_put(handle, data->period); |
| 4952 | |
| 4953 | if (sample_type & PERF_SAMPLE_READ) |
| 4954 | perf_output_read(handle, event); |
| 4955 | |
| 4956 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 4957 | if (data->callchain) { |
| 4958 | int size = 1; |
| 4959 | |
| 4960 | if (data->callchain) |
| 4961 | size += data->callchain->nr; |
| 4962 | |
| 4963 | size *= sizeof(u64); |
| 4964 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4965 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4966 | } else { |
| 4967 | u64 nr = 0; |
| 4968 | perf_output_put(handle, nr); |
| 4969 | } |
| 4970 | } |
| 4971 | |
| 4972 | if (sample_type & PERF_SAMPLE_RAW) { |
| 4973 | if (data->raw) { |
| 4974 | perf_output_put(handle, data->raw->size); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4975 | __output_copy(handle, data->raw->data, |
| 4976 | data->raw->size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4977 | } else { |
| 4978 | struct { |
| 4979 | u32 size; |
| 4980 | u32 data; |
| 4981 | } raw = { |
| 4982 | .size = sizeof(u32), |
| 4983 | .data = 0, |
| 4984 | }; |
| 4985 | perf_output_put(handle, raw); |
| 4986 | } |
| 4987 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 4988 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 4989 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 4990 | if (data->br_stack) { |
| 4991 | size_t size; |
| 4992 | |
| 4993 | size = data->br_stack->nr |
| 4994 | * sizeof(struct perf_branch_entry); |
| 4995 | |
| 4996 | perf_output_put(handle, data->br_stack->nr); |
| 4997 | perf_output_copy(handle, data->br_stack->entries, size); |
| 4998 | } else { |
| 4999 | /* |
| 5000 | * we always store at least the value of nr |
| 5001 | */ |
| 5002 | u64 nr = 0; |
| 5003 | perf_output_put(handle, nr); |
| 5004 | } |
| 5005 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5006 | |
| 5007 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5008 | u64 abi = data->regs_user.abi; |
| 5009 | |
| 5010 | /* |
| 5011 | * If there are no regs to dump, notice it through |
| 5012 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5013 | */ |
| 5014 | perf_output_put(handle, abi); |
| 5015 | |
| 5016 | if (abi) { |
| 5017 | u64 mask = event->attr.sample_regs_user; |
| 5018 | perf_output_sample_regs(handle, |
| 5019 | data->regs_user.regs, |
| 5020 | mask); |
| 5021 | } |
| 5022 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5023 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5024 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5025 | perf_output_sample_ustack(handle, |
| 5026 | data->stack_user_size, |
| 5027 | data->regs_user.regs); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5028 | } |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 5029 | |
| 5030 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 5031 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 5032 | |
| 5033 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 5034 | perf_output_put(handle, data->data_src.val); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5035 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 5036 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 5037 | perf_output_put(handle, data->txn); |
| 5038 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5039 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5040 | u64 abi = data->regs_intr.abi; |
| 5041 | /* |
| 5042 | * If there are no regs to dump, notice it through |
| 5043 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5044 | */ |
| 5045 | perf_output_put(handle, abi); |
| 5046 | |
| 5047 | if (abi) { |
| 5048 | u64 mask = event->attr.sample_regs_intr; |
| 5049 | |
| 5050 | perf_output_sample_regs(handle, |
| 5051 | data->regs_intr.regs, |
| 5052 | mask); |
| 5053 | } |
| 5054 | } |
| 5055 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5056 | if (!event->attr.watermark) { |
| 5057 | int wakeup_events = event->attr.wakeup_events; |
| 5058 | |
| 5059 | if (wakeup_events) { |
| 5060 | struct ring_buffer *rb = handle->rb; |
| 5061 | int events = local_inc_return(&rb->events); |
| 5062 | |
| 5063 | if (events >= wakeup_events) { |
| 5064 | local_sub(wakeup_events, &rb->events); |
| 5065 | local_inc(&rb->wakeup); |
| 5066 | } |
| 5067 | } |
| 5068 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5069 | } |
| 5070 | |
| 5071 | void perf_prepare_sample(struct perf_event_header *header, |
| 5072 | struct perf_sample_data *data, |
| 5073 | struct perf_event *event, |
| 5074 | struct pt_regs *regs) |
| 5075 | { |
| 5076 | u64 sample_type = event->attr.sample_type; |
| 5077 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5078 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5079 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5080 | |
| 5081 | header->misc = 0; |
| 5082 | header->misc |= perf_misc_flags(regs); |
| 5083 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5084 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5085 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5086 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5087 | data->ip = perf_instruction_pointer(regs); |
| 5088 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5089 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5090 | int size = 1; |
| 5091 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5092 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5093 | |
| 5094 | if (data->callchain) |
| 5095 | size += data->callchain->nr; |
| 5096 | |
| 5097 | header->size += size * sizeof(u64); |
| 5098 | } |
| 5099 | |
| 5100 | if (sample_type & PERF_SAMPLE_RAW) { |
| 5101 | int size = sizeof(u32); |
| 5102 | |
| 5103 | if (data->raw) |
| 5104 | size += data->raw->size; |
| 5105 | else |
| 5106 | size += sizeof(u32); |
| 5107 | |
| 5108 | WARN_ON_ONCE(size & (sizeof(u64)-1)); |
| 5109 | header->size += size; |
| 5110 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5111 | |
| 5112 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5113 | int size = sizeof(u64); /* nr */ |
| 5114 | if (data->br_stack) { |
| 5115 | size += data->br_stack->nr |
| 5116 | * sizeof(struct perf_branch_entry); |
| 5117 | } |
| 5118 | header->size += size; |
| 5119 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5120 | |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5121 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5122 | perf_sample_regs_user(&data->regs_user, regs, |
| 5123 | &data->regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5124 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5125 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5126 | /* regs dump ABI info */ |
| 5127 | int size = sizeof(u64); |
| 5128 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5129 | if (data->regs_user.regs) { |
| 5130 | u64 mask = event->attr.sample_regs_user; |
| 5131 | size += hweight64(mask) * sizeof(u64); |
| 5132 | } |
| 5133 | |
| 5134 | header->size += size; |
| 5135 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5136 | |
| 5137 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 5138 | /* |
| 5139 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 5140 | * processed as the last one or have additional check added |
| 5141 | * in case new sample type is added, because we could eat |
| 5142 | * up the rest of the sample size. |
| 5143 | */ |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5144 | u16 stack_size = event->attr.sample_stack_user; |
| 5145 | u16 size = sizeof(u64); |
| 5146 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5147 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5148 | data->regs_user.regs); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5149 | |
| 5150 | /* |
| 5151 | * If there is something to dump, add space for the dump |
| 5152 | * itself and for the field that tells the dynamic size, |
| 5153 | * which is how many have been actually dumped. |
| 5154 | */ |
| 5155 | if (stack_size) |
| 5156 | size += sizeof(u64) + stack_size; |
| 5157 | |
| 5158 | data->stack_user_size = stack_size; |
| 5159 | header->size += size; |
| 5160 | } |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5161 | |
| 5162 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5163 | /* regs dump ABI info */ |
| 5164 | int size = sizeof(u64); |
| 5165 | |
| 5166 | perf_sample_regs_intr(&data->regs_intr, regs); |
| 5167 | |
| 5168 | if (data->regs_intr.regs) { |
| 5169 | u64 mask = event->attr.sample_regs_intr; |
| 5170 | |
| 5171 | size += hweight64(mask) * sizeof(u64); |
| 5172 | } |
| 5173 | |
| 5174 | header->size += size; |
| 5175 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5176 | } |
| 5177 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5178 | static void perf_event_output(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5179 | struct perf_sample_data *data, |
| 5180 | struct pt_regs *regs) |
| 5181 | { |
| 5182 | struct perf_output_handle handle; |
| 5183 | struct perf_event_header header; |
| 5184 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5185 | /* protect the callchain buffers */ |
| 5186 | rcu_read_lock(); |
| 5187 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5188 | perf_prepare_sample(&header, data, event, regs); |
| 5189 | |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5190 | if (perf_output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5191 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5192 | |
| 5193 | perf_output_sample(&handle, &header, data, event); |
| 5194 | |
| 5195 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5196 | |
| 5197 | exit: |
| 5198 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | /* |
| 5202 | * read event_id |
| 5203 | */ |
| 5204 | |
| 5205 | struct perf_read_event { |
| 5206 | struct perf_event_header header; |
| 5207 | |
| 5208 | u32 pid; |
| 5209 | u32 tid; |
| 5210 | }; |
| 5211 | |
| 5212 | static void |
| 5213 | perf_event_read_event(struct perf_event *event, |
| 5214 | struct task_struct *task) |
| 5215 | { |
| 5216 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5217 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5218 | struct perf_read_event read_event = { |
| 5219 | .header = { |
| 5220 | .type = PERF_RECORD_READ, |
| 5221 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5222 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5223 | }, |
| 5224 | .pid = perf_event_pid(event, task), |
| 5225 | .tid = perf_event_tid(event, task), |
| 5226 | }; |
| 5227 | int ret; |
| 5228 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5229 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5230 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5231 | if (ret) |
| 5232 | return; |
| 5233 | |
| 5234 | perf_output_put(&handle, read_event); |
| 5235 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5236 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5237 | |
| 5238 | perf_output_end(&handle); |
| 5239 | } |
| 5240 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5241 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
| 5242 | |
| 5243 | static void |
| 5244 | perf_event_aux_ctx(struct perf_event_context *ctx, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5245 | perf_event_aux_output_cb output, |
| 5246 | void *data) |
| 5247 | { |
| 5248 | struct perf_event *event; |
| 5249 | |
| 5250 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 5251 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 5252 | continue; |
| 5253 | if (!event_filter_match(event)) |
| 5254 | continue; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5255 | output(event, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5256 | } |
| 5257 | } |
| 5258 | |
| 5259 | static void |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5260 | perf_event_aux(perf_event_aux_output_cb output, void *data, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5261 | struct perf_event_context *task_ctx) |
| 5262 | { |
| 5263 | struct perf_cpu_context *cpuctx; |
| 5264 | struct perf_event_context *ctx; |
| 5265 | struct pmu *pmu; |
| 5266 | int ctxn; |
| 5267 | |
| 5268 | rcu_read_lock(); |
| 5269 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 5270 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 5271 | if (cpuctx->unique_pmu != pmu) |
| 5272 | goto next; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5273 | perf_event_aux_ctx(&cpuctx->ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5274 | if (task_ctx) |
| 5275 | goto next; |
| 5276 | ctxn = pmu->task_ctx_nr; |
| 5277 | if (ctxn < 0) |
| 5278 | goto next; |
| 5279 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 5280 | if (ctx) |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5281 | perf_event_aux_ctx(ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5282 | next: |
| 5283 | put_cpu_ptr(pmu->pmu_cpu_context); |
| 5284 | } |
| 5285 | |
| 5286 | if (task_ctx) { |
| 5287 | preempt_disable(); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5288 | perf_event_aux_ctx(task_ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5289 | preempt_enable(); |
| 5290 | } |
| 5291 | rcu_read_unlock(); |
| 5292 | } |
| 5293 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5294 | /* |
| 5295 | * task tracking -- fork/exit |
| 5296 | * |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5297 | * 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] | 5298 | */ |
| 5299 | |
| 5300 | struct perf_task_event { |
| 5301 | struct task_struct *task; |
| 5302 | struct perf_event_context *task_ctx; |
| 5303 | |
| 5304 | struct { |
| 5305 | struct perf_event_header header; |
| 5306 | |
| 5307 | u32 pid; |
| 5308 | u32 ppid; |
| 5309 | u32 tid; |
| 5310 | u32 ptid; |
| 5311 | u64 time; |
| 5312 | } event_id; |
| 5313 | }; |
| 5314 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5315 | static int perf_event_task_match(struct perf_event *event) |
| 5316 | { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5317 | return event->attr.comm || event->attr.mmap || |
| 5318 | event->attr.mmap2 || event->attr.mmap_data || |
| 5319 | event->attr.task; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5320 | } |
| 5321 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5322 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5323 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5324 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5325 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5326 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5327 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5328 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5329 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 5330 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5331 | if (!perf_event_task_match(event)) |
| 5332 | return; |
| 5333 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5334 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5335 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5336 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5337 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 5338 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5339 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5340 | |
| 5341 | task_event->event_id.pid = perf_event_pid(event, task); |
| 5342 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 5343 | |
| 5344 | task_event->event_id.tid = perf_event_tid(event, task); |
| 5345 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 5346 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5347 | perf_output_put(&handle, task_event->event_id); |
| 5348 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5349 | perf_event__output_id_sample(event, &handle, &sample); |
| 5350 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5351 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5352 | out: |
| 5353 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5354 | } |
| 5355 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5356 | static void perf_event_task(struct task_struct *task, |
| 5357 | struct perf_event_context *task_ctx, |
| 5358 | int new) |
| 5359 | { |
| 5360 | struct perf_task_event task_event; |
| 5361 | |
| 5362 | if (!atomic_read(&nr_comm_events) && |
| 5363 | !atomic_read(&nr_mmap_events) && |
| 5364 | !atomic_read(&nr_task_events)) |
| 5365 | return; |
| 5366 | |
| 5367 | task_event = (struct perf_task_event){ |
| 5368 | .task = task, |
| 5369 | .task_ctx = task_ctx, |
| 5370 | .event_id = { |
| 5371 | .header = { |
| 5372 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 5373 | .misc = 0, |
| 5374 | .size = sizeof(task_event.event_id), |
| 5375 | }, |
| 5376 | /* .pid */ |
| 5377 | /* .ppid */ |
| 5378 | /* .tid */ |
| 5379 | /* .ptid */ |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 5380 | .time = perf_clock(), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5381 | }, |
| 5382 | }; |
| 5383 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5384 | perf_event_aux(perf_event_task_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5385 | &task_event, |
| 5386 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | void perf_event_fork(struct task_struct *task) |
| 5390 | { |
| 5391 | perf_event_task(task, NULL, 1); |
| 5392 | } |
| 5393 | |
| 5394 | /* |
| 5395 | * comm tracking |
| 5396 | */ |
| 5397 | |
| 5398 | struct perf_comm_event { |
| 5399 | struct task_struct *task; |
| 5400 | char *comm; |
| 5401 | int comm_size; |
| 5402 | |
| 5403 | struct { |
| 5404 | struct perf_event_header header; |
| 5405 | |
| 5406 | u32 pid; |
| 5407 | u32 tid; |
| 5408 | } event_id; |
| 5409 | }; |
| 5410 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5411 | static int perf_event_comm_match(struct perf_event *event) |
| 5412 | { |
| 5413 | return event->attr.comm; |
| 5414 | } |
| 5415 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5416 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5417 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5418 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5419 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5420 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5421 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5422 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5423 | int ret; |
| 5424 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5425 | if (!perf_event_comm_match(event)) |
| 5426 | return; |
| 5427 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5428 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 5429 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5430 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5431 | |
| 5432 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5433 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5434 | |
| 5435 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 5436 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 5437 | |
| 5438 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5439 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5440 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5441 | |
| 5442 | perf_event__output_id_sample(event, &handle, &sample); |
| 5443 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5444 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5445 | out: |
| 5446 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5447 | } |
| 5448 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5449 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 5450 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5451 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5452 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5453 | |
| 5454 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 5455 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5456 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 5457 | |
| 5458 | comm_event->comm = comm; |
| 5459 | comm_event->comm_size = size; |
| 5460 | |
| 5461 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5462 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5463 | perf_event_aux(perf_event_comm_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5464 | comm_event, |
| 5465 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5466 | } |
| 5467 | |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5468 | void perf_event_comm(struct task_struct *task, bool exec) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5469 | { |
| 5470 | struct perf_comm_event comm_event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5471 | |
| 5472 | if (!atomic_read(&nr_comm_events)) |
| 5473 | return; |
| 5474 | |
| 5475 | comm_event = (struct perf_comm_event){ |
| 5476 | .task = task, |
| 5477 | /* .comm */ |
| 5478 | /* .comm_size */ |
| 5479 | .event_id = { |
| 5480 | .header = { |
| 5481 | .type = PERF_RECORD_COMM, |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5482 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5483 | /* .size */ |
| 5484 | }, |
| 5485 | /* .pid */ |
| 5486 | /* .tid */ |
| 5487 | }, |
| 5488 | }; |
| 5489 | |
| 5490 | perf_event_comm_event(&comm_event); |
| 5491 | } |
| 5492 | |
| 5493 | /* |
| 5494 | * mmap tracking |
| 5495 | */ |
| 5496 | |
| 5497 | struct perf_mmap_event { |
| 5498 | struct vm_area_struct *vma; |
| 5499 | |
| 5500 | const char *file_name; |
| 5501 | int file_size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5502 | int maj, min; |
| 5503 | u64 ino; |
| 5504 | u64 ino_generation; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5505 | u32 prot, flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5506 | |
| 5507 | struct { |
| 5508 | struct perf_event_header header; |
| 5509 | |
| 5510 | u32 pid; |
| 5511 | u32 tid; |
| 5512 | u64 start; |
| 5513 | u64 len; |
| 5514 | u64 pgoff; |
| 5515 | } event_id; |
| 5516 | }; |
| 5517 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5518 | static int perf_event_mmap_match(struct perf_event *event, |
| 5519 | void *data) |
| 5520 | { |
| 5521 | struct perf_mmap_event *mmap_event = data; |
| 5522 | struct vm_area_struct *vma = mmap_event->vma; |
| 5523 | int executable = vma->vm_flags & VM_EXEC; |
| 5524 | |
| 5525 | return (!executable && event->attr.mmap_data) || |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5526 | (executable && (event->attr.mmap || event->attr.mmap2)); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5527 | } |
| 5528 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5529 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5530 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5531 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5532 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5533 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5534 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5535 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5536 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5537 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5538 | if (!perf_event_mmap_match(event, data)) |
| 5539 | return; |
| 5540 | |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5541 | if (event->attr.mmap2) { |
| 5542 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; |
| 5543 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); |
| 5544 | mmap_event->event_id.header.size += sizeof(mmap_event->min); |
| 5545 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); |
Arnaldo Carvalho de Melo | d008d52 | 2013-09-10 10:24:05 -0300 | [diff] [blame] | 5546 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5547 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
| 5548 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5549 | } |
| 5550 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5551 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 5552 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5553 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5554 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5555 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5556 | |
| 5557 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 5558 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 5559 | |
| 5560 | perf_output_put(&handle, mmap_event->event_id); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5561 | |
| 5562 | if (event->attr.mmap2) { |
| 5563 | perf_output_put(&handle, mmap_event->maj); |
| 5564 | perf_output_put(&handle, mmap_event->min); |
| 5565 | perf_output_put(&handle, mmap_event->ino); |
| 5566 | perf_output_put(&handle, mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5567 | perf_output_put(&handle, mmap_event->prot); |
| 5568 | perf_output_put(&handle, mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5569 | } |
| 5570 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5571 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5572 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5573 | |
| 5574 | perf_event__output_id_sample(event, &handle, &sample); |
| 5575 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5576 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5577 | out: |
| 5578 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5579 | } |
| 5580 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5581 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 5582 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5583 | struct vm_area_struct *vma = mmap_event->vma; |
| 5584 | struct file *file = vma->vm_file; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5585 | int maj = 0, min = 0; |
| 5586 | u64 ino = 0, gen = 0; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5587 | u32 prot = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5588 | unsigned int size; |
| 5589 | char tmp[16]; |
| 5590 | char *buf = NULL; |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5591 | char *name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5592 | |
| 5593 | if (file) { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5594 | struct inode *inode; |
| 5595 | dev_t dev; |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5596 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5597 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5598 | if (!buf) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5599 | name = "//enomem"; |
| 5600 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5601 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5602 | /* |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5603 | * d_path() works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5604 | * need to add enough zero bytes after the string to handle |
| 5605 | * the 64bit alignment we do later. |
| 5606 | */ |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 5607 | name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5608 | if (IS_ERR(name)) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5609 | name = "//toolong"; |
| 5610 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5611 | } |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5612 | inode = file_inode(vma->vm_file); |
| 5613 | dev = inode->i_sb->s_dev; |
| 5614 | ino = inode->i_ino; |
| 5615 | gen = inode->i_generation; |
| 5616 | maj = MAJOR(dev); |
| 5617 | min = MINOR(dev); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5618 | |
| 5619 | if (vma->vm_flags & VM_READ) |
| 5620 | prot |= PROT_READ; |
| 5621 | if (vma->vm_flags & VM_WRITE) |
| 5622 | prot |= PROT_WRITE; |
| 5623 | if (vma->vm_flags & VM_EXEC) |
| 5624 | prot |= PROT_EXEC; |
| 5625 | |
| 5626 | if (vma->vm_flags & VM_MAYSHARE) |
| 5627 | flags = MAP_SHARED; |
| 5628 | else |
| 5629 | flags = MAP_PRIVATE; |
| 5630 | |
| 5631 | if (vma->vm_flags & VM_DENYWRITE) |
| 5632 | flags |= MAP_DENYWRITE; |
| 5633 | if (vma->vm_flags & VM_MAYEXEC) |
| 5634 | flags |= MAP_EXECUTABLE; |
| 5635 | if (vma->vm_flags & VM_LOCKED) |
| 5636 | flags |= MAP_LOCKED; |
| 5637 | if (vma->vm_flags & VM_HUGETLB) |
| 5638 | flags |= MAP_HUGETLB; |
| 5639 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5640 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5641 | } else { |
Jiri Olsa | fbe26ab | 2014-07-14 17:57:19 +0200 | [diff] [blame] | 5642 | if (vma->vm_ops && vma->vm_ops->name) { |
| 5643 | name = (char *) vma->vm_ops->name(vma); |
| 5644 | if (name) |
| 5645 | goto cpy_name; |
| 5646 | } |
| 5647 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5648 | name = (char *)arch_vma_name(vma); |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5649 | if (name) |
| 5650 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5651 | |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5652 | if (vma->vm_start <= vma->vm_mm->start_brk && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5653 | vma->vm_end >= vma->vm_mm->brk) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5654 | name = "[heap]"; |
| 5655 | goto cpy_name; |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 5656 | } |
| 5657 | if (vma->vm_start <= vma->vm_mm->start_stack && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5658 | vma->vm_end >= vma->vm_mm->start_stack) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5659 | name = "[stack]"; |
| 5660 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5661 | } |
| 5662 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5663 | name = "//anon"; |
| 5664 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5665 | } |
| 5666 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 5667 | cpy_name: |
| 5668 | strlcpy(tmp, name, sizeof(tmp)); |
| 5669 | name = tmp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5670 | got_name: |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 5671 | /* |
| 5672 | * Since our buffer works in 8 byte units we need to align our string |
| 5673 | * size to a multiple of 8. However, we must guarantee the tail end is |
| 5674 | * zero'd out to avoid leaking random bits to userspace. |
| 5675 | */ |
| 5676 | size = strlen(name)+1; |
| 5677 | while (!IS_ALIGNED(size, sizeof(u64))) |
| 5678 | name[size++] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5679 | |
| 5680 | mmap_event->file_name = name; |
| 5681 | mmap_event->file_size = size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5682 | mmap_event->maj = maj; |
| 5683 | mmap_event->min = min; |
| 5684 | mmap_event->ino = ino; |
| 5685 | mmap_event->ino_generation = gen; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5686 | mmap_event->prot = prot; |
| 5687 | mmap_event->flags = flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5688 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 5689 | if (!(vma->vm_flags & VM_EXEC)) |
| 5690 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 5691 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5692 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 5693 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5694 | perf_event_aux(perf_event_mmap_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5695 | mmap_event, |
| 5696 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5697 | |
| 5698 | kfree(buf); |
| 5699 | } |
| 5700 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 5701 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5702 | { |
| 5703 | struct perf_mmap_event mmap_event; |
| 5704 | |
| 5705 | if (!atomic_read(&nr_mmap_events)) |
| 5706 | return; |
| 5707 | |
| 5708 | mmap_event = (struct perf_mmap_event){ |
| 5709 | .vma = vma, |
| 5710 | /* .file_name */ |
| 5711 | /* .file_size */ |
| 5712 | .event_id = { |
| 5713 | .header = { |
| 5714 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5715 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5716 | /* .size */ |
| 5717 | }, |
| 5718 | /* .pid */ |
| 5719 | /* .tid */ |
| 5720 | .start = vma->vm_start, |
| 5721 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 5722 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5723 | }, |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5724 | /* .maj (attr_mmap2 only) */ |
| 5725 | /* .min (attr_mmap2 only) */ |
| 5726 | /* .ino (attr_mmap2 only) */ |
| 5727 | /* .ino_generation (attr_mmap2 only) */ |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5728 | /* .prot (attr_mmap2 only) */ |
| 5729 | /* .flags (attr_mmap2 only) */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5730 | }; |
| 5731 | |
| 5732 | perf_event_mmap_event(&mmap_event); |
| 5733 | } |
| 5734 | |
| 5735 | /* |
| 5736 | * IRQ throttle logging |
| 5737 | */ |
| 5738 | |
| 5739 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 5740 | { |
| 5741 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5742 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5743 | int ret; |
| 5744 | |
| 5745 | struct { |
| 5746 | struct perf_event_header header; |
| 5747 | u64 time; |
| 5748 | u64 id; |
| 5749 | u64 stream_id; |
| 5750 | } throttle_event = { |
| 5751 | .header = { |
| 5752 | .type = PERF_RECORD_THROTTLE, |
| 5753 | .misc = 0, |
| 5754 | .size = sizeof(throttle_event), |
| 5755 | }, |
| 5756 | .time = perf_clock(), |
| 5757 | .id = primary_event_id(event), |
| 5758 | .stream_id = event->id, |
| 5759 | }; |
| 5760 | |
| 5761 | if (enable) |
| 5762 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 5763 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5764 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 5765 | |
| 5766 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5767 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5768 | if (ret) |
| 5769 | return; |
| 5770 | |
| 5771 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5772 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5773 | perf_output_end(&handle); |
| 5774 | } |
| 5775 | |
| 5776 | /* |
| 5777 | * Generic event overflow handling, sampling. |
| 5778 | */ |
| 5779 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5780 | static int __perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5781 | int throttle, struct perf_sample_data *data, |
| 5782 | struct pt_regs *regs) |
| 5783 | { |
| 5784 | int events = atomic_read(&event->event_limit); |
| 5785 | struct hw_perf_event *hwc = &event->hw; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5786 | u64 seq; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5787 | int ret = 0; |
| 5788 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 5789 | /* |
| 5790 | * Non-sampling counters might still use the PMI to fold short |
| 5791 | * hardware counters, ignore those. |
| 5792 | */ |
| 5793 | if (unlikely(!is_sampling_event(event))) |
| 5794 | return 0; |
| 5795 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5796 | seq = __this_cpu_read(perf_throttled_seq); |
| 5797 | if (seq != hwc->interrupts_seq) { |
| 5798 | hwc->interrupts_seq = seq; |
| 5799 | hwc->interrupts = 1; |
| 5800 | } else { |
| 5801 | hwc->interrupts++; |
| 5802 | if (unlikely(throttle |
| 5803 | && hwc->interrupts >= max_samples_per_tick)) { |
| 5804 | __this_cpu_inc(perf_throttled_count); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 5805 | hwc->interrupts = MAX_INTERRUPTS; |
| 5806 | perf_log_throttle(event, 0); |
Frederic Weisbecker | d84153d | 2013-07-23 02:31:05 +0200 | [diff] [blame] | 5807 | tick_nohz_full_kick(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5808 | ret = 1; |
| 5809 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 5810 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5811 | |
| 5812 | if (event->attr.freq) { |
| 5813 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5814 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5815 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5816 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5817 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 5818 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 5819 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5820 | } |
| 5821 | |
| 5822 | /* |
| 5823 | * XXX event_limit might not quite work as expected on inherited |
| 5824 | * events |
| 5825 | */ |
| 5826 | |
| 5827 | event->pending_kill = POLL_IN; |
| 5828 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 5829 | ret = 1; |
| 5830 | event->pending_kill = POLL_HUP; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5831 | event->pending_disable = 1; |
| 5832 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5833 | } |
| 5834 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5835 | if (event->overflow_handler) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5836 | event->overflow_handler(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5837 | else |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5838 | perf_event_output(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5839 | |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5840 | if (event->fasync && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5841 | event->pending_wakeup = 1; |
| 5842 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 5843 | } |
| 5844 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5845 | return ret; |
| 5846 | } |
| 5847 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5848 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5849 | struct perf_sample_data *data, |
| 5850 | struct pt_regs *regs) |
| 5851 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5852 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5853 | } |
| 5854 | |
| 5855 | /* |
| 5856 | * Generic software event infrastructure |
| 5857 | */ |
| 5858 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5859 | struct swevent_htable { |
| 5860 | struct swevent_hlist *swevent_hlist; |
| 5861 | struct mutex hlist_mutex; |
| 5862 | int hlist_refcount; |
| 5863 | |
| 5864 | /* Recursion avoidance in each contexts */ |
| 5865 | int recursion[PERF_NR_CONTEXTS]; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 5866 | |
| 5867 | /* Keeps track of cpu being initialized/exited */ |
| 5868 | bool online; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 5869 | }; |
| 5870 | |
| 5871 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 5872 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5873 | /* |
| 5874 | * We directly increment event->count and keep a second value in |
| 5875 | * event->hw.period_left to count intervals. This period event |
| 5876 | * is kept in the range [-sample_period, 0] so that we can use the |
| 5877 | * sign as trigger. |
| 5878 | */ |
| 5879 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 5880 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5881 | { |
| 5882 | struct hw_perf_event *hwc = &event->hw; |
| 5883 | u64 period = hwc->last_period; |
| 5884 | u64 nr, offset; |
| 5885 | s64 old, val; |
| 5886 | |
| 5887 | hwc->last_period = hwc->sample_period; |
| 5888 | |
| 5889 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5890 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5891 | if (val < 0) |
| 5892 | return 0; |
| 5893 | |
| 5894 | nr = div64_u64(period + val, period); |
| 5895 | offset = nr * period; |
| 5896 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5897 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5898 | goto again; |
| 5899 | |
| 5900 | return nr; |
| 5901 | } |
| 5902 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5903 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5904 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5905 | struct pt_regs *regs) |
| 5906 | { |
| 5907 | struct hw_perf_event *hwc = &event->hw; |
| 5908 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5909 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5910 | if (!overflow) |
| 5911 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5912 | |
| 5913 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 5914 | return; |
| 5915 | |
| 5916 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5917 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5918 | data, regs)) { |
| 5919 | /* |
| 5920 | * We inhibit the overflow from happening when |
| 5921 | * hwc->interrupts == MAX_INTERRUPTS. |
| 5922 | */ |
| 5923 | break; |
| 5924 | } |
| 5925 | throttle = 1; |
| 5926 | } |
| 5927 | } |
| 5928 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5929 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5930 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5931 | struct pt_regs *regs) |
| 5932 | { |
| 5933 | struct hw_perf_event *hwc = &event->hw; |
| 5934 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5935 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5936 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5937 | if (!regs) |
| 5938 | return; |
| 5939 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 5940 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5941 | return; |
| 5942 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 5943 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 5944 | data->period = nr; |
| 5945 | return perf_swevent_overflow(event, 1, data, regs); |
| 5946 | } else |
| 5947 | data->period = event->hw.last_period; |
| 5948 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5949 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5950 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5951 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5952 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 5953 | return; |
| 5954 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 5955 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5956 | } |
| 5957 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5958 | static int perf_exclude_event(struct perf_event *event, |
| 5959 | struct pt_regs *regs) |
| 5960 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5961 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 5962 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 5963 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5964 | if (regs) { |
| 5965 | if (event->attr.exclude_user && user_mode(regs)) |
| 5966 | return 1; |
| 5967 | |
| 5968 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 5969 | return 1; |
| 5970 | } |
| 5971 | |
| 5972 | return 0; |
| 5973 | } |
| 5974 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5975 | static int perf_swevent_match(struct perf_event *event, |
| 5976 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 5977 | u32 event_id, |
| 5978 | struct perf_sample_data *data, |
| 5979 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5980 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5981 | if (event->attr.type != type) |
| 5982 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5983 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5984 | if (event->attr.config != event_id) |
| 5985 | return 0; |
| 5986 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 5987 | if (perf_exclude_event(event, regs)) |
| 5988 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5989 | |
| 5990 | return 1; |
| 5991 | } |
| 5992 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5993 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5994 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5995 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5996 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5997 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 5998 | } |
| 5999 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6000 | static inline struct hlist_head * |
| 6001 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6002 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6003 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6004 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6005 | return &hlist->heads[hash]; |
| 6006 | } |
| 6007 | |
| 6008 | /* For the read side: events when they trigger */ |
| 6009 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6010 | 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] | 6011 | { |
| 6012 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6013 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6014 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6015 | if (!hlist) |
| 6016 | return NULL; |
| 6017 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6018 | return __find_swevent_head(hlist, type, event_id); |
| 6019 | } |
| 6020 | |
| 6021 | /* For the event head insertion and removal in the hlist */ |
| 6022 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6023 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6024 | { |
| 6025 | struct swevent_hlist *hlist; |
| 6026 | u32 event_id = event->attr.config; |
| 6027 | u64 type = event->attr.type; |
| 6028 | |
| 6029 | /* |
| 6030 | * Event scheduling is always serialized against hlist allocation |
| 6031 | * and release. Which makes the protected version suitable here. |
| 6032 | * The context lock guarantees that. |
| 6033 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6034 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6035 | lockdep_is_held(&event->ctx->lock)); |
| 6036 | if (!hlist) |
| 6037 | return NULL; |
| 6038 | |
| 6039 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6040 | } |
| 6041 | |
| 6042 | 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] | 6043 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6044 | struct perf_sample_data *data, |
| 6045 | struct pt_regs *regs) |
| 6046 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6047 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6048 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6049 | struct hlist_head *head; |
| 6050 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6051 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6052 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6053 | if (!head) |
| 6054 | goto end; |
| 6055 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6056 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6057 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6058 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6059 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6060 | end: |
| 6061 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6062 | } |
| 6063 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6064 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
| 6065 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6066 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6067 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6068 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 6069 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6070 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6071 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 6072 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6073 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 6074 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6075 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6076 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6077 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6078 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 6079 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6080 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6081 | 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] | 6082 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 6083 | struct perf_sample_data data; |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6084 | |
| 6085 | if (WARN_ON_ONCE(!regs)) |
| 6086 | return; |
| 6087 | |
| 6088 | perf_sample_data_init(&data, addr, 0); |
| 6089 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
| 6090 | } |
| 6091 | |
| 6092 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
| 6093 | { |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6094 | int rctx; |
| 6095 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6096 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6097 | rctx = perf_swevent_get_recursion_context(); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6098 | if (unlikely(rctx < 0)) |
| 6099 | goto fail; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6100 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6101 | ___perf_sw_event(event_id, nr, regs, addr); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6102 | |
| 6103 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6104 | fail: |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6105 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6106 | } |
| 6107 | |
| 6108 | static void perf_swevent_read(struct perf_event *event) |
| 6109 | { |
| 6110 | } |
| 6111 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6112 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6113 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6114 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6115 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6116 | struct hlist_head *head; |
| 6117 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6118 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6119 | hwc->last_period = hwc->sample_period; |
| 6120 | perf_swevent_set_period(event); |
| 6121 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6122 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6123 | hwc->state = !(flags & PERF_EF_START); |
| 6124 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6125 | head = find_swevent_head(swhash, event); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 6126 | if (!head) { |
| 6127 | /* |
| 6128 | * We can race with cpu hotplug code. Do not |
| 6129 | * WARN if the cpu just got unplugged. |
| 6130 | */ |
| 6131 | WARN_ON_ONCE(swhash->online); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6132 | return -EINVAL; |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 6133 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6134 | |
| 6135 | hlist_add_head_rcu(&event->hlist_entry, head); |
| 6136 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6137 | return 0; |
| 6138 | } |
| 6139 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6140 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6141 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6142 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6143 | } |
| 6144 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6145 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6146 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6147 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6148 | } |
| 6149 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6150 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6151 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6152 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6153 | } |
| 6154 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6155 | /* Deref the hlist from the update side */ |
| 6156 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6157 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6158 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6159 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 6160 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6161 | } |
| 6162 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6163 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6164 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6165 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6166 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6167 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6168 | return; |
| 6169 | |
Andreea-Cristina Bernat | 70691d4 | 2014-08-22 16:26:05 +0300 | [diff] [blame] | 6170 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 6171 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | static void swevent_hlist_put_cpu(struct perf_event *event, int cpu) |
| 6175 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6176 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6177 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6178 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6179 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6180 | if (!--swhash->hlist_refcount) |
| 6181 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6182 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6183 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | static void swevent_hlist_put(struct perf_event *event) |
| 6187 | { |
| 6188 | int cpu; |
| 6189 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6190 | for_each_possible_cpu(cpu) |
| 6191 | swevent_hlist_put_cpu(event, cpu); |
| 6192 | } |
| 6193 | |
| 6194 | static int swevent_hlist_get_cpu(struct perf_event *event, int cpu) |
| 6195 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6196 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6197 | int err = 0; |
| 6198 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6199 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6200 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6201 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6202 | struct swevent_hlist *hlist; |
| 6203 | |
| 6204 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 6205 | if (!hlist) { |
| 6206 | err = -ENOMEM; |
| 6207 | goto exit; |
| 6208 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6209 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6210 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6211 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 6212 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6213 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6214 | |
| 6215 | return err; |
| 6216 | } |
| 6217 | |
| 6218 | static int swevent_hlist_get(struct perf_event *event) |
| 6219 | { |
| 6220 | int err; |
| 6221 | int cpu, failed_cpu; |
| 6222 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6223 | get_online_cpus(); |
| 6224 | for_each_possible_cpu(cpu) { |
| 6225 | err = swevent_hlist_get_cpu(event, cpu); |
| 6226 | if (err) { |
| 6227 | failed_cpu = cpu; |
| 6228 | goto fail; |
| 6229 | } |
| 6230 | } |
| 6231 | put_online_cpus(); |
| 6232 | |
| 6233 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 6234 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6235 | for_each_possible_cpu(cpu) { |
| 6236 | if (cpu == failed_cpu) |
| 6237 | break; |
| 6238 | swevent_hlist_put_cpu(event, cpu); |
| 6239 | } |
| 6240 | |
| 6241 | put_online_cpus(); |
| 6242 | return err; |
| 6243 | } |
| 6244 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6245 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6246 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6247 | static void sw_perf_event_destroy(struct perf_event *event) |
| 6248 | { |
| 6249 | u64 event_id = event->attr.config; |
| 6250 | |
| 6251 | WARN_ON(event->parent); |
| 6252 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6253 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6254 | swevent_hlist_put(event); |
| 6255 | } |
| 6256 | |
| 6257 | static int perf_swevent_init(struct perf_event *event) |
| 6258 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 6259 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6260 | |
| 6261 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6262 | return -ENOENT; |
| 6263 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6264 | /* |
| 6265 | * no branch sampling for software events |
| 6266 | */ |
| 6267 | if (has_branch_stack(event)) |
| 6268 | return -EOPNOTSUPP; |
| 6269 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6270 | switch (event_id) { |
| 6271 | case PERF_COUNT_SW_CPU_CLOCK: |
| 6272 | case PERF_COUNT_SW_TASK_CLOCK: |
| 6273 | return -ENOENT; |
| 6274 | |
| 6275 | default: |
| 6276 | break; |
| 6277 | } |
| 6278 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 6279 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6280 | return -ENOENT; |
| 6281 | |
| 6282 | if (!event->parent) { |
| 6283 | int err; |
| 6284 | |
| 6285 | err = swevent_hlist_get(event); |
| 6286 | if (err) |
| 6287 | return err; |
| 6288 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6289 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6290 | event->destroy = sw_perf_event_destroy; |
| 6291 | } |
| 6292 | |
| 6293 | return 0; |
| 6294 | } |
| 6295 | |
| 6296 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6297 | .task_ctx_nr = perf_sw_context, |
| 6298 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6299 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6300 | .add = perf_swevent_add, |
| 6301 | .del = perf_swevent_del, |
| 6302 | .start = perf_swevent_start, |
| 6303 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6304 | .read = perf_swevent_read, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6305 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6306 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6307 | #ifdef CONFIG_EVENT_TRACING |
| 6308 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6309 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6310 | struct perf_sample_data *data) |
| 6311 | { |
| 6312 | void *record = data->raw->data; |
| 6313 | |
| 6314 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 6315 | return 1; |
| 6316 | return 0; |
| 6317 | } |
| 6318 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6319 | static int perf_tp_event_match(struct perf_event *event, |
| 6320 | struct perf_sample_data *data, |
| 6321 | struct pt_regs *regs) |
| 6322 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 6323 | if (event->hw.state & PERF_HES_STOPPED) |
| 6324 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 6325 | /* |
| 6326 | * All tracepoints are from kernel-space. |
| 6327 | */ |
| 6328 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6329 | return 0; |
| 6330 | |
| 6331 | if (!perf_tp_filter_match(event, data)) |
| 6332 | return 0; |
| 6333 | |
| 6334 | return 1; |
| 6335 | } |
| 6336 | |
| 6337 | 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] | 6338 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 6339 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6340 | { |
| 6341 | struct perf_sample_data data; |
| 6342 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6343 | |
| 6344 | struct perf_raw_record raw = { |
| 6345 | .size = entry_size, |
| 6346 | .data = record, |
| 6347 | }; |
| 6348 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6349 | perf_sample_data_init(&data, addr, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6350 | data.raw = &raw; |
| 6351 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6352 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6353 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6354 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6355 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 6356 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 6357 | /* |
| 6358 | * If we got specified a target task, also iterate its context and |
| 6359 | * deliver this event there too. |
| 6360 | */ |
| 6361 | if (task && task != current) { |
| 6362 | struct perf_event_context *ctx; |
| 6363 | struct trace_entry *entry = record; |
| 6364 | |
| 6365 | rcu_read_lock(); |
| 6366 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 6367 | if (!ctx) |
| 6368 | goto unlock; |
| 6369 | |
| 6370 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 6371 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6372 | continue; |
| 6373 | if (event->attr.config != entry->type) |
| 6374 | continue; |
| 6375 | if (perf_tp_event_match(event, &data, regs)) |
| 6376 | perf_swevent_event(event, count, &data, regs); |
| 6377 | } |
| 6378 | unlock: |
| 6379 | rcu_read_unlock(); |
| 6380 | } |
| 6381 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 6382 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6383 | } |
| 6384 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 6385 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6386 | static void tp_perf_event_destroy(struct perf_event *event) |
| 6387 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6388 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6389 | } |
| 6390 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6391 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6392 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6393 | int err; |
| 6394 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6395 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6396 | return -ENOENT; |
| 6397 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6398 | /* |
| 6399 | * no branch sampling for tracepoint events |
| 6400 | */ |
| 6401 | if (has_branch_stack(event)) |
| 6402 | return -EOPNOTSUPP; |
| 6403 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6404 | err = perf_trace_init(event); |
| 6405 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6406 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6407 | |
| 6408 | event->destroy = tp_perf_event_destroy; |
| 6409 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6410 | return 0; |
| 6411 | } |
| 6412 | |
| 6413 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6414 | .task_ctx_nr = perf_sw_context, |
| 6415 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6416 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6417 | .add = perf_trace_add, |
| 6418 | .del = perf_trace_del, |
| 6419 | .start = perf_swevent_start, |
| 6420 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6421 | .read = perf_swevent_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6422 | }; |
| 6423 | |
| 6424 | static inline void perf_tp_register(void) |
| 6425 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6426 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6427 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6428 | |
| 6429 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6430 | { |
| 6431 | char *filter_str; |
| 6432 | int ret; |
| 6433 | |
| 6434 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 6435 | return -EINVAL; |
| 6436 | |
| 6437 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 6438 | if (IS_ERR(filter_str)) |
| 6439 | return PTR_ERR(filter_str); |
| 6440 | |
| 6441 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); |
| 6442 | |
| 6443 | kfree(filter_str); |
| 6444 | return ret; |
| 6445 | } |
| 6446 | |
| 6447 | static void perf_event_free_filter(struct perf_event *event) |
| 6448 | { |
| 6449 | ftrace_profile_free_filter(event); |
| 6450 | } |
| 6451 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6452 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6453 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6454 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6455 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6456 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6457 | |
| 6458 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 6459 | { |
| 6460 | return -ENOENT; |
| 6461 | } |
| 6462 | |
| 6463 | static void perf_event_free_filter(struct perf_event *event) |
| 6464 | { |
| 6465 | } |
| 6466 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 6467 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6468 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6469 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6470 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6471 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6472 | struct perf_sample_data sample; |
| 6473 | struct pt_regs *regs = data; |
| 6474 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6475 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6476 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6477 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6478 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 6479 | } |
| 6480 | #endif |
| 6481 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6482 | /* |
| 6483 | * hrtimer based swevent callback |
| 6484 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6485 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6486 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6487 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6488 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 6489 | struct perf_sample_data data; |
| 6490 | struct pt_regs *regs; |
| 6491 | struct perf_event *event; |
| 6492 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6493 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6494 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6495 | |
| 6496 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 6497 | return HRTIMER_NORESTART; |
| 6498 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6499 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6500 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 6501 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6502 | regs = get_irq_regs(); |
| 6503 | |
| 6504 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 6505 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 6506 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6507 | ret = HRTIMER_NORESTART; |
| 6508 | } |
| 6509 | |
| 6510 | period = max_t(u64, 10000, event->hw.sample_period); |
| 6511 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 6512 | |
| 6513 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6514 | } |
| 6515 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6516 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6517 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6518 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6519 | s64 period; |
| 6520 | |
| 6521 | if (!is_sampling_event(event)) |
| 6522 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6523 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6524 | period = local64_read(&hwc->period_left); |
| 6525 | if (period) { |
| 6526 | if (period < 0) |
| 6527 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6528 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 6529 | local64_set(&hwc->period_left, 0); |
| 6530 | } else { |
| 6531 | period = max_t(u64, 10000, hwc->sample_period); |
| 6532 | } |
| 6533 | __hrtimer_start_range_ns(&hwc->hrtimer, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6534 | ns_to_ktime(period), 0, |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 6535 | HRTIMER_MODE_REL_PINNED, 0); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6536 | } |
| 6537 | |
| 6538 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 6539 | { |
| 6540 | struct hw_perf_event *hwc = &event->hw; |
| 6541 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6542 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6543 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 6544 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6545 | |
| 6546 | hrtimer_cancel(&hwc->hrtimer); |
| 6547 | } |
| 6548 | } |
| 6549 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6550 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 6551 | { |
| 6552 | struct hw_perf_event *hwc = &event->hw; |
| 6553 | |
| 6554 | if (!is_sampling_event(event)) |
| 6555 | return; |
| 6556 | |
| 6557 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 6558 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 6559 | |
| 6560 | /* |
| 6561 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 6562 | * mapping and avoid the whole period adjust feedback stuff. |
| 6563 | */ |
| 6564 | if (event->attr.freq) { |
| 6565 | long freq = event->attr.sample_freq; |
| 6566 | |
| 6567 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 6568 | hwc->sample_period = event->attr.sample_period; |
| 6569 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 6570 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6571 | event->attr.freq = 0; |
| 6572 | } |
| 6573 | } |
| 6574 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6575 | /* |
| 6576 | * Software event: cpu wall time clock |
| 6577 | */ |
| 6578 | |
| 6579 | static void cpu_clock_event_update(struct perf_event *event) |
| 6580 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6581 | s64 prev; |
| 6582 | u64 now; |
| 6583 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6584 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6585 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6586 | local64_add(now - prev, &event->count); |
| 6587 | } |
| 6588 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6589 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6590 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6591 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6592 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6593 | } |
| 6594 | |
| 6595 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 6596 | { |
| 6597 | perf_swevent_cancel_hrtimer(event); |
| 6598 | cpu_clock_event_update(event); |
| 6599 | } |
| 6600 | |
| 6601 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 6602 | { |
| 6603 | if (flags & PERF_EF_START) |
| 6604 | cpu_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6605 | |
| 6606 | return 0; |
| 6607 | } |
| 6608 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6609 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6610 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6611 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6612 | } |
| 6613 | |
| 6614 | static void cpu_clock_event_read(struct perf_event *event) |
| 6615 | { |
| 6616 | cpu_clock_event_update(event); |
| 6617 | } |
| 6618 | |
| 6619 | static int cpu_clock_event_init(struct perf_event *event) |
| 6620 | { |
| 6621 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6622 | return -ENOENT; |
| 6623 | |
| 6624 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 6625 | return -ENOENT; |
| 6626 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6627 | /* |
| 6628 | * no branch sampling for software events |
| 6629 | */ |
| 6630 | if (has_branch_stack(event)) |
| 6631 | return -EOPNOTSUPP; |
| 6632 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6633 | perf_swevent_init_hrtimer(event); |
| 6634 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6635 | return 0; |
| 6636 | } |
| 6637 | |
| 6638 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6639 | .task_ctx_nr = perf_sw_context, |
| 6640 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6641 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6642 | .add = cpu_clock_event_add, |
| 6643 | .del = cpu_clock_event_del, |
| 6644 | .start = cpu_clock_event_start, |
| 6645 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6646 | .read = cpu_clock_event_read, |
| 6647 | }; |
| 6648 | |
| 6649 | /* |
| 6650 | * Software event: task time clock |
| 6651 | */ |
| 6652 | |
| 6653 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 6654 | { |
| 6655 | u64 prev; |
| 6656 | s64 delta; |
| 6657 | |
| 6658 | prev = local64_xchg(&event->hw.prev_count, now); |
| 6659 | delta = now - prev; |
| 6660 | local64_add(delta, &event->count); |
| 6661 | } |
| 6662 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6663 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6664 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6665 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6666 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6667 | } |
| 6668 | |
| 6669 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 6670 | { |
| 6671 | perf_swevent_cancel_hrtimer(event); |
| 6672 | task_clock_event_update(event, event->ctx->time); |
| 6673 | } |
| 6674 | |
| 6675 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 6676 | { |
| 6677 | if (flags & PERF_EF_START) |
| 6678 | task_clock_event_start(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6679 | |
| 6680 | return 0; |
| 6681 | } |
| 6682 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6683 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6684 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6685 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6686 | } |
| 6687 | |
| 6688 | static void task_clock_event_read(struct perf_event *event) |
| 6689 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 6690 | u64 now = perf_clock(); |
| 6691 | u64 delta = now - event->ctx->timestamp; |
| 6692 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6693 | |
| 6694 | task_clock_event_update(event, time); |
| 6695 | } |
| 6696 | |
| 6697 | static int task_clock_event_init(struct perf_event *event) |
| 6698 | { |
| 6699 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6700 | return -ENOENT; |
| 6701 | |
| 6702 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 6703 | return -ENOENT; |
| 6704 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6705 | /* |
| 6706 | * no branch sampling for software events |
| 6707 | */ |
| 6708 | if (has_branch_stack(event)) |
| 6709 | return -EOPNOTSUPP; |
| 6710 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 6711 | perf_swevent_init_hrtimer(event); |
| 6712 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6713 | return 0; |
| 6714 | } |
| 6715 | |
| 6716 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6717 | .task_ctx_nr = perf_sw_context, |
| 6718 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6719 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6720 | .add = task_clock_event_add, |
| 6721 | .del = task_clock_event_del, |
| 6722 | .start = task_clock_event_start, |
| 6723 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6724 | .read = task_clock_event_read, |
| 6725 | }; |
| 6726 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6727 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 6728 | { |
| 6729 | } |
| 6730 | |
| 6731 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 6732 | { |
| 6733 | return 0; |
| 6734 | } |
| 6735 | |
| 6736 | static void perf_pmu_start_txn(struct pmu *pmu) |
| 6737 | { |
| 6738 | perf_pmu_disable(pmu); |
| 6739 | } |
| 6740 | |
| 6741 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 6742 | { |
| 6743 | perf_pmu_enable(pmu); |
| 6744 | return 0; |
| 6745 | } |
| 6746 | |
| 6747 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 6748 | { |
| 6749 | perf_pmu_enable(pmu); |
| 6750 | } |
| 6751 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6752 | static int perf_event_idx_default(struct perf_event *event) |
| 6753 | { |
Peter Zijlstra | c719f56 | 2014-10-21 11:10:21 +0200 | [diff] [blame] | 6754 | return 0; |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6755 | } |
| 6756 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6757 | /* |
| 6758 | * Ensures all contexts with the same task_ctx_nr have the same |
| 6759 | * pmu_cpu_context too. |
| 6760 | */ |
Mark Rutland | 9e31704 | 2014-02-10 17:44:18 +0000 | [diff] [blame] | 6761 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6762 | { |
| 6763 | struct pmu *pmu; |
| 6764 | |
| 6765 | if (ctxn < 0) |
| 6766 | return NULL; |
| 6767 | |
| 6768 | list_for_each_entry(pmu, &pmus, entry) { |
| 6769 | if (pmu->task_ctx_nr == ctxn) |
| 6770 | return pmu->pmu_cpu_context; |
| 6771 | } |
| 6772 | |
| 6773 | return NULL; |
| 6774 | } |
| 6775 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6776 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6777 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6778 | int cpu; |
| 6779 | |
| 6780 | for_each_possible_cpu(cpu) { |
| 6781 | struct perf_cpu_context *cpuctx; |
| 6782 | |
| 6783 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6784 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6785 | if (cpuctx->unique_pmu == old_pmu) |
| 6786 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6787 | } |
| 6788 | } |
| 6789 | |
| 6790 | static void free_pmu_context(struct pmu *pmu) |
| 6791 | { |
| 6792 | struct pmu *i; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6793 | |
| 6794 | mutex_lock(&pmus_lock); |
| 6795 | /* |
| 6796 | * Like a real lame refcount. |
| 6797 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6798 | list_for_each_entry(i, &pmus, entry) { |
| 6799 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 6800 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6801 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6802 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6803 | } |
| 6804 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 6805 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6806 | out: |
| 6807 | mutex_unlock(&pmus_lock); |
| 6808 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6809 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6810 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6811 | static ssize_t |
| 6812 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 6813 | { |
| 6814 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6815 | |
| 6816 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 6817 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6818 | static DEVICE_ATTR_RO(type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6819 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6820 | static ssize_t |
| 6821 | perf_event_mux_interval_ms_show(struct device *dev, |
| 6822 | struct device_attribute *attr, |
| 6823 | char *page) |
| 6824 | { |
| 6825 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6826 | |
| 6827 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 6828 | } |
| 6829 | |
| 6830 | static ssize_t |
| 6831 | perf_event_mux_interval_ms_store(struct device *dev, |
| 6832 | struct device_attribute *attr, |
| 6833 | const char *buf, size_t count) |
| 6834 | { |
| 6835 | struct pmu *pmu = dev_get_drvdata(dev); |
| 6836 | int timer, cpu, ret; |
| 6837 | |
| 6838 | ret = kstrtoint(buf, 0, &timer); |
| 6839 | if (ret) |
| 6840 | return ret; |
| 6841 | |
| 6842 | if (timer < 1) |
| 6843 | return -EINVAL; |
| 6844 | |
| 6845 | /* same value, noting to do */ |
| 6846 | if (timer == pmu->hrtimer_interval_ms) |
| 6847 | return count; |
| 6848 | |
| 6849 | pmu->hrtimer_interval_ms = timer; |
| 6850 | |
| 6851 | /* update all cpuctx for this PMU */ |
| 6852 | for_each_possible_cpu(cpu) { |
| 6853 | struct perf_cpu_context *cpuctx; |
| 6854 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 6855 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 6856 | |
| 6857 | if (hrtimer_active(&cpuctx->hrtimer)) |
| 6858 | hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval); |
| 6859 | } |
| 6860 | |
| 6861 | return count; |
| 6862 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6863 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 6864 | |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6865 | static struct attribute *pmu_dev_attrs[] = { |
| 6866 | &dev_attr_type.attr, |
| 6867 | &dev_attr_perf_event_mux_interval_ms.attr, |
| 6868 | NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6869 | }; |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6870 | ATTRIBUTE_GROUPS(pmu_dev); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6871 | |
| 6872 | static int pmu_bus_running; |
| 6873 | static struct bus_type pmu_bus = { |
| 6874 | .name = "event_source", |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 6875 | .dev_groups = pmu_dev_groups, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6876 | }; |
| 6877 | |
| 6878 | static void pmu_dev_release(struct device *dev) |
| 6879 | { |
| 6880 | kfree(dev); |
| 6881 | } |
| 6882 | |
| 6883 | static int pmu_dev_alloc(struct pmu *pmu) |
| 6884 | { |
| 6885 | int ret = -ENOMEM; |
| 6886 | |
| 6887 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 6888 | if (!pmu->dev) |
| 6889 | goto out; |
| 6890 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 6891 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6892 | device_initialize(pmu->dev); |
| 6893 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 6894 | if (ret) |
| 6895 | goto free_dev; |
| 6896 | |
| 6897 | dev_set_drvdata(pmu->dev, pmu); |
| 6898 | pmu->dev->bus = &pmu_bus; |
| 6899 | pmu->dev->release = pmu_dev_release; |
| 6900 | ret = device_add(pmu->dev); |
| 6901 | if (ret) |
| 6902 | goto free_dev; |
| 6903 | |
| 6904 | out: |
| 6905 | return ret; |
| 6906 | |
| 6907 | free_dev: |
| 6908 | put_device(pmu->dev); |
| 6909 | goto out; |
| 6910 | } |
| 6911 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6912 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6913 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6914 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 6915 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6916 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6917 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6918 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6919 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6920 | ret = -ENOMEM; |
| 6921 | pmu->pmu_disable_count = alloc_percpu(int); |
| 6922 | if (!pmu->pmu_disable_count) |
| 6923 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6924 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6925 | pmu->type = -1; |
| 6926 | if (!name) |
| 6927 | goto skip_type; |
| 6928 | pmu->name = name; |
| 6929 | |
| 6930 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 6931 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 6932 | if (type < 0) { |
| 6933 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6934 | goto free_pdc; |
| 6935 | } |
| 6936 | } |
| 6937 | pmu->type = type; |
| 6938 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6939 | if (pmu_bus_running) { |
| 6940 | ret = pmu_dev_alloc(pmu); |
| 6941 | if (ret) |
| 6942 | goto free_idr; |
| 6943 | } |
| 6944 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 6945 | skip_type: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6946 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 6947 | if (pmu->pmu_cpu_context) |
| 6948 | goto got_cpu_context; |
| 6949 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 6950 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6951 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 6952 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 6953 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6954 | |
| 6955 | for_each_possible_cpu(cpu) { |
| 6956 | struct perf_cpu_context *cpuctx; |
| 6957 | |
| 6958 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 6959 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 6960 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 6961 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6962 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 6963 | |
| 6964 | __perf_cpu_hrtimer_init(cpuctx, cpu); |
| 6965 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 6966 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 6967 | } |
| 6968 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6969 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 6970 | if (!pmu->start_txn) { |
| 6971 | if (pmu->pmu_enable) { |
| 6972 | /* |
| 6973 | * If we have pmu_enable/pmu_disable calls, install |
| 6974 | * transaction stubs that use that to try and batch |
| 6975 | * hardware accesses. |
| 6976 | */ |
| 6977 | pmu->start_txn = perf_pmu_start_txn; |
| 6978 | pmu->commit_txn = perf_pmu_commit_txn; |
| 6979 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 6980 | } else { |
| 6981 | pmu->start_txn = perf_pmu_nop_void; |
| 6982 | pmu->commit_txn = perf_pmu_nop_int; |
| 6983 | pmu->cancel_txn = perf_pmu_nop_void; |
| 6984 | } |
| 6985 | } |
| 6986 | |
| 6987 | if (!pmu->pmu_enable) { |
| 6988 | pmu->pmu_enable = perf_pmu_nop_void; |
| 6989 | pmu->pmu_disable = perf_pmu_nop_void; |
| 6990 | } |
| 6991 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 6992 | if (!pmu->event_idx) |
| 6993 | pmu->event_idx = perf_event_idx_default; |
| 6994 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6995 | list_add_rcu(&pmu->entry, &pmus); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 6996 | ret = 0; |
| 6997 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6998 | mutex_unlock(&pmus_lock); |
| 6999 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 7000 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7001 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7002 | free_dev: |
| 7003 | device_del(pmu->dev); |
| 7004 | put_device(pmu->dev); |
| 7005 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7006 | free_idr: |
| 7007 | if (pmu->type >= PERF_TYPE_MAX) |
| 7008 | idr_remove(&pmu_idr, pmu->type); |
| 7009 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7010 | free_pdc: |
| 7011 | free_percpu(pmu->pmu_disable_count); |
| 7012 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7013 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 7014 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7015 | |
| 7016 | void perf_pmu_unregister(struct pmu *pmu) |
| 7017 | { |
| 7018 | mutex_lock(&pmus_lock); |
| 7019 | list_del_rcu(&pmu->entry); |
| 7020 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7021 | |
| 7022 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 7023 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 7024 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7025 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7026 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 7027 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7028 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 7029 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7030 | if (pmu->type >= PERF_TYPE_MAX) |
| 7031 | idr_remove(&pmu_idr, pmu->type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7032 | device_del(pmu->dev); |
| 7033 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 7034 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7035 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 7036 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7037 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7038 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
| 7039 | { |
| 7040 | int ret; |
| 7041 | |
| 7042 | if (!try_module_get(pmu->module)) |
| 7043 | return -ENODEV; |
| 7044 | event->pmu = pmu; |
| 7045 | ret = pmu->event_init(event); |
| 7046 | if (ret) |
| 7047 | module_put(pmu->module); |
| 7048 | |
| 7049 | return ret; |
| 7050 | } |
| 7051 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7052 | struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7053 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 7054 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7055 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7056 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7057 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7058 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7059 | |
| 7060 | rcu_read_lock(); |
| 7061 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 7062 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7063 | if (pmu) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7064 | ret = perf_try_init_event(pmu, event); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7065 | if (ret) |
| 7066 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7067 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7068 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7069 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7070 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7071 | ret = perf_try_init_event(pmu, event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7072 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 7073 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7074 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7075 | if (ret != -ENOENT) { |
| 7076 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 7077 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7078 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7079 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 7080 | pmu = ERR_PTR(-ENOENT); |
| 7081 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7082 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7083 | |
| 7084 | return pmu; |
| 7085 | } |
| 7086 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7087 | static void account_event_cpu(struct perf_event *event, int cpu) |
| 7088 | { |
| 7089 | if (event->parent) |
| 7090 | return; |
| 7091 | |
| 7092 | if (has_branch_stack(event)) { |
| 7093 | if (!(event->attach_state & PERF_ATTACH_TASK)) |
| 7094 | atomic_inc(&per_cpu(perf_branch_stack_events, cpu)); |
| 7095 | } |
| 7096 | if (is_cgroup_event(event)) |
| 7097 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); |
| 7098 | } |
| 7099 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7100 | static void account_event(struct perf_event *event) |
| 7101 | { |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7102 | if (event->parent) |
| 7103 | return; |
| 7104 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7105 | if (event->attach_state & PERF_ATTACH_TASK) |
| 7106 | static_key_slow_inc(&perf_sched_events.key); |
| 7107 | if (event->attr.mmap || event->attr.mmap_data) |
| 7108 | atomic_inc(&nr_mmap_events); |
| 7109 | if (event->attr.comm) |
| 7110 | atomic_inc(&nr_comm_events); |
| 7111 | if (event->attr.task) |
| 7112 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 7113 | if (event->attr.freq) { |
| 7114 | if (atomic_inc_return(&nr_freq_events) == 1) |
| 7115 | tick_nohz_full_kick_all(); |
| 7116 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7117 | if (has_branch_stack(event)) |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7118 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7119 | if (is_cgroup_event(event)) |
| 7120 | static_key_slow_inc(&perf_sched_events.key); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7121 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7122 | account_event_cpu(event, event->cpu); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7123 | } |
| 7124 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7125 | /* |
| 7126 | * Allocate and initialize a event structure |
| 7127 | */ |
| 7128 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7129 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7130 | struct task_struct *task, |
| 7131 | struct perf_event *group_leader, |
| 7132 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7133 | perf_overflow_handler_t overflow_handler, |
| 7134 | void *context) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7135 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 7136 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7137 | struct perf_event *event; |
| 7138 | struct hw_perf_event *hwc; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7139 | long err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7140 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 7141 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 7142 | if (!task || cpu != -1) |
| 7143 | return ERR_PTR(-EINVAL); |
| 7144 | } |
| 7145 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7146 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7147 | if (!event) |
| 7148 | return ERR_PTR(-ENOMEM); |
| 7149 | |
| 7150 | /* |
| 7151 | * Single events are their own group leaders, with an |
| 7152 | * empty sibling list: |
| 7153 | */ |
| 7154 | if (!group_leader) |
| 7155 | group_leader = event; |
| 7156 | |
| 7157 | mutex_init(&event->child_mutex); |
| 7158 | INIT_LIST_HEAD(&event->child_list); |
| 7159 | |
| 7160 | INIT_LIST_HEAD(&event->group_entry); |
| 7161 | INIT_LIST_HEAD(&event->event_entry); |
| 7162 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 7163 | INIT_LIST_HEAD(&event->rb_entry); |
Stephane Eranian | 71ad88e | 2013-11-12 17:58:48 +0100 | [diff] [blame] | 7164 | INIT_LIST_HEAD(&event->active_entry); |
Stephane Eranian | f3ae75d | 2014-01-08 11:15:52 +0100 | [diff] [blame] | 7165 | INIT_HLIST_NODE(&event->hlist_entry); |
| 7166 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 7167 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7168 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 7169 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7170 | |
| 7171 | mutex_init(&event->mmap_mutex); |
| 7172 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7173 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7174 | event->cpu = cpu; |
| 7175 | event->attr = *attr; |
| 7176 | event->group_leader = group_leader; |
| 7177 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7178 | event->oncpu = -1; |
| 7179 | |
| 7180 | event->parent = parent_event; |
| 7181 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 7182 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7183 | event->id = atomic64_inc_return(&perf_event_id); |
| 7184 | |
| 7185 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 7186 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7187 | if (task) { |
| 7188 | event->attach_state = PERF_ATTACH_TASK; |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 7189 | |
| 7190 | if (attr->type == PERF_TYPE_TRACEPOINT) |
| 7191 | event->hw.tp_target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7192 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
| 7193 | /* |
| 7194 | * hw_breakpoint is a bit difficult here.. |
| 7195 | */ |
Oleg Nesterov | f22c1bb | 2013-02-02 16:27:52 +0100 | [diff] [blame] | 7196 | else if (attr->type == PERF_TYPE_BREAKPOINT) |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7197 | event->hw.bp_target = task; |
| 7198 | #endif |
| 7199 | } |
| 7200 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7201 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 7202 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7203 | context = parent_event->overflow_handler_context; |
| 7204 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 7205 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 7206 | event->overflow_handler = overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7207 | event->overflow_handler_context = context; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 7208 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7209 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7210 | |
| 7211 | pmu = NULL; |
| 7212 | |
| 7213 | hwc = &event->hw; |
| 7214 | hwc->sample_period = attr->sample_period; |
| 7215 | if (attr->freq && attr->sample_freq) |
| 7216 | hwc->sample_period = 1; |
| 7217 | hwc->last_period = hwc->sample_period; |
| 7218 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 7219 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7220 | |
| 7221 | /* |
| 7222 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 7223 | */ |
| 7224 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7225 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7226 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7227 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7228 | if (!pmu) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7229 | goto err_ns; |
| 7230 | else if (IS_ERR(pmu)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7231 | err = PTR_ERR(pmu); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7232 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7233 | } |
| 7234 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7235 | if (!event->parent) { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 7236 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 7237 | err = get_callchain_buffers(); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7238 | if (err) |
| 7239 | goto err_pmu; |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 7240 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7241 | } |
| 7242 | |
| 7243 | return event; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7244 | |
| 7245 | err_pmu: |
| 7246 | if (event->destroy) |
| 7247 | event->destroy(event); |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 7248 | module_put(pmu->module); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7249 | err_ns: |
| 7250 | if (event->ns) |
| 7251 | put_pid_ns(event->ns); |
| 7252 | kfree(event); |
| 7253 | |
| 7254 | return ERR_PTR(err); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7255 | } |
| 7256 | |
| 7257 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 7258 | struct perf_event_attr *attr) |
| 7259 | { |
| 7260 | u32 size; |
| 7261 | int ret; |
| 7262 | |
| 7263 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 7264 | return -EFAULT; |
| 7265 | |
| 7266 | /* |
| 7267 | * zero the full structure, so that a short copy will be nice. |
| 7268 | */ |
| 7269 | memset(attr, 0, sizeof(*attr)); |
| 7270 | |
| 7271 | ret = get_user(size, &uattr->size); |
| 7272 | if (ret) |
| 7273 | return ret; |
| 7274 | |
| 7275 | if (size > PAGE_SIZE) /* silly large */ |
| 7276 | goto err_size; |
| 7277 | |
| 7278 | if (!size) /* abi compat */ |
| 7279 | size = PERF_ATTR_SIZE_VER0; |
| 7280 | |
| 7281 | if (size < PERF_ATTR_SIZE_VER0) |
| 7282 | goto err_size; |
| 7283 | |
| 7284 | /* |
| 7285 | * If we're handed a bigger struct than we know of, |
| 7286 | * ensure all the unknown bits are 0 - i.e. new |
| 7287 | * user-space does not rely on any kernel feature |
| 7288 | * extensions we dont know about yet. |
| 7289 | */ |
| 7290 | if (size > sizeof(*attr)) { |
| 7291 | unsigned char __user *addr; |
| 7292 | unsigned char __user *end; |
| 7293 | unsigned char val; |
| 7294 | |
| 7295 | addr = (void __user *)uattr + sizeof(*attr); |
| 7296 | end = (void __user *)uattr + size; |
| 7297 | |
| 7298 | for (; addr < end; addr++) { |
| 7299 | ret = get_user(val, addr); |
| 7300 | if (ret) |
| 7301 | return ret; |
| 7302 | if (val) |
| 7303 | goto err_size; |
| 7304 | } |
| 7305 | size = sizeof(*attr); |
| 7306 | } |
| 7307 | |
| 7308 | ret = copy_from_user(attr, uattr, size); |
| 7309 | if (ret) |
| 7310 | return -EFAULT; |
| 7311 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 7312 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7313 | return -EINVAL; |
| 7314 | |
| 7315 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 7316 | return -EINVAL; |
| 7317 | |
| 7318 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 7319 | return -EINVAL; |
| 7320 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 7321 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 7322 | u64 mask = attr->branch_sample_type; |
| 7323 | |
| 7324 | /* only using defined bits */ |
| 7325 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 7326 | return -EINVAL; |
| 7327 | |
| 7328 | /* at least one branch bit must be set */ |
| 7329 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 7330 | return -EINVAL; |
| 7331 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 7332 | /* propagate priv level, when not set for branch */ |
| 7333 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 7334 | |
| 7335 | /* exclude_kernel checked on syscall entry */ |
| 7336 | if (!attr->exclude_kernel) |
| 7337 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 7338 | |
| 7339 | if (!attr->exclude_user) |
| 7340 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 7341 | |
| 7342 | if (!attr->exclude_hv) |
| 7343 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 7344 | /* |
| 7345 | * adjust user setting (for HW filter setup) |
| 7346 | */ |
| 7347 | attr->branch_sample_type = mask; |
| 7348 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 7349 | /* privileged levels capture (kernel, hv): check permissions */ |
| 7350 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 7351 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 7352 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 7353 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7354 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 7355 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7356 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 7357 | if (ret) |
| 7358 | return ret; |
| 7359 | } |
| 7360 | |
| 7361 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 7362 | if (!arch_perf_have_user_stack_dump()) |
| 7363 | return -ENOSYS; |
| 7364 | |
| 7365 | /* |
| 7366 | * We have __u32 type for the size, but so far |
| 7367 | * we can only use __u16 as maximum due to the |
| 7368 | * __u16 sample size limit. |
| 7369 | */ |
| 7370 | if (attr->sample_stack_user >= USHRT_MAX) |
| 7371 | ret = -EINVAL; |
| 7372 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
| 7373 | ret = -EINVAL; |
| 7374 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 7375 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 7376 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
| 7377 | ret = perf_reg_validate(attr->sample_regs_intr); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7378 | out: |
| 7379 | return ret; |
| 7380 | |
| 7381 | err_size: |
| 7382 | put_user(sizeof(*attr), &uattr->size); |
| 7383 | ret = -E2BIG; |
| 7384 | goto out; |
| 7385 | } |
| 7386 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7387 | static int |
| 7388 | 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] | 7389 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7390 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7391 | int ret = -EINVAL; |
| 7392 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7393 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7394 | goto set; |
| 7395 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7396 | /* don't allow circular references */ |
| 7397 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7398 | goto out; |
| 7399 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 7400 | /* |
| 7401 | * Don't allow cross-cpu buffers |
| 7402 | */ |
| 7403 | if (output_event->cpu != event->cpu) |
| 7404 | goto out; |
| 7405 | |
| 7406 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7407 | * 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] | 7408 | */ |
| 7409 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 7410 | goto out; |
| 7411 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7412 | set: |
| 7413 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7414 | /* Can't redirect output if we've got an active mmap() */ |
| 7415 | if (atomic_read(&event->mmap_count)) |
| 7416 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7417 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7418 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 7419 | /* get the rb we want to redirect to */ |
| 7420 | rb = ring_buffer_get(output_event); |
| 7421 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7422 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7423 | } |
| 7424 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 7425 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 7426 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7427 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7428 | unlock: |
| 7429 | mutex_unlock(&event->mmap_mutex); |
| 7430 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7431 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7432 | return ret; |
| 7433 | } |
| 7434 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7435 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
| 7436 | { |
| 7437 | if (b < a) |
| 7438 | swap(a, b); |
| 7439 | |
| 7440 | mutex_lock(a); |
| 7441 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); |
| 7442 | } |
| 7443 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7444 | /** |
| 7445 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 7446 | * |
| 7447 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 7448 | * @pid: target pid |
| 7449 | * @cpu: target cpu |
| 7450 | * @group_fd: group leader event fd |
| 7451 | */ |
| 7452 | SYSCALL_DEFINE5(perf_event_open, |
| 7453 | struct perf_event_attr __user *, attr_uptr, |
| 7454 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 7455 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7456 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 7457 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7458 | struct perf_event_attr attr; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7459 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7460 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7461 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7462 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7463 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7464 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7465 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7466 | int err; |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7467 | int f_flags = O_RDWR; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7468 | |
| 7469 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7470 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7471 | return -EINVAL; |
| 7472 | |
| 7473 | err = perf_copy_attr(attr_uptr, &attr); |
| 7474 | if (err) |
| 7475 | return err; |
| 7476 | |
| 7477 | if (!attr.exclude_kernel) { |
| 7478 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 7479 | return -EACCES; |
| 7480 | } |
| 7481 | |
| 7482 | if (attr.freq) { |
| 7483 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 7484 | return -EINVAL; |
Peter Zijlstra | 0819b2e | 2014-05-15 20:23:48 +0200 | [diff] [blame] | 7485 | } else { |
| 7486 | if (attr.sample_period & (1ULL << 63)) |
| 7487 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7488 | } |
| 7489 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7490 | /* |
| 7491 | * In cgroup mode, the pid argument is used to pass the fd |
| 7492 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 7493 | * designates the cpu on which to monitor threads from that |
| 7494 | * cgroup. |
| 7495 | */ |
| 7496 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 7497 | return -EINVAL; |
| 7498 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7499 | if (flags & PERF_FLAG_FD_CLOEXEC) |
| 7500 | f_flags |= O_CLOEXEC; |
| 7501 | |
| 7502 | event_fd = get_unused_fd_flags(f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7503 | if (event_fd < 0) |
| 7504 | return event_fd; |
| 7505 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7506 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7507 | err = perf_fget_light(group_fd, &group); |
| 7508 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7509 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7510 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7511 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 7512 | output_event = group_leader; |
| 7513 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 7514 | group_leader = NULL; |
| 7515 | } |
| 7516 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7517 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7518 | task = find_lively_task_by_vpid(pid); |
| 7519 | if (IS_ERR(task)) { |
| 7520 | err = PTR_ERR(task); |
| 7521 | goto err_group_fd; |
| 7522 | } |
| 7523 | } |
| 7524 | |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7525 | if (task && group_leader && |
| 7526 | group_leader->attr.inherit != attr.inherit) { |
| 7527 | err = -EINVAL; |
| 7528 | goto err_task; |
| 7529 | } |
| 7530 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7531 | get_online_cpus(); |
| 7532 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7533 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
| 7534 | NULL, NULL); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7535 | if (IS_ERR(event)) { |
| 7536 | err = PTR_ERR(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7537 | goto err_cpus; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 7538 | } |
| 7539 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7540 | if (flags & PERF_FLAG_PID_CGROUP) { |
| 7541 | err = perf_cgroup_connect(pid, event, &attr, group_leader); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7542 | if (err) { |
| 7543 | __free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7544 | goto err_cpus; |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7545 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 7546 | } |
| 7547 | |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 7548 | if (is_sampling_event(event)) { |
| 7549 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { |
| 7550 | err = -ENOTSUPP; |
| 7551 | goto err_alloc; |
| 7552 | } |
| 7553 | } |
| 7554 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7555 | account_event(event); |
| 7556 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7557 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7558 | * Special case software events and allow them to be part of |
| 7559 | * any hardware group. |
| 7560 | */ |
| 7561 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7562 | |
| 7563 | if (group_leader && |
| 7564 | (is_software_event(event) != is_software_event(group_leader))) { |
| 7565 | if (is_software_event(event)) { |
| 7566 | /* |
| 7567 | * If event and group_leader are not both a software |
| 7568 | * event, and event is, then group leader is not. |
| 7569 | * |
| 7570 | * Allow the addition of software events to !software |
| 7571 | * groups, this is safe because software events never |
| 7572 | * fail to schedule. |
| 7573 | */ |
| 7574 | pmu = group_leader->pmu; |
| 7575 | } else if (is_software_event(group_leader) && |
| 7576 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 7577 | /* |
| 7578 | * In case the group is a pure software group, and we |
| 7579 | * try to add a hardware event, move the whole group to |
| 7580 | * the hardware context. |
| 7581 | */ |
| 7582 | move_group = 1; |
| 7583 | } |
| 7584 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7585 | |
| 7586 | /* |
| 7587 | * Get the target context (task or percpu): |
| 7588 | */ |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7589 | ctx = find_get_context(pmu, task, event->cpu); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7590 | if (IS_ERR(ctx)) { |
| 7591 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7592 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7593 | } |
| 7594 | |
Peter Zijlstra | fd1edb3 | 2011-03-28 13:13:56 +0200 | [diff] [blame] | 7595 | if (task) { |
| 7596 | put_task_struct(task); |
| 7597 | task = NULL; |
| 7598 | } |
| 7599 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7600 | /* |
| 7601 | * Look up the group leader (we will attach this event to it): |
| 7602 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7603 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7604 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7605 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7606 | /* |
| 7607 | * Do not allow a recursive hierarchy (this new sibling |
| 7608 | * becoming part of another group-sibling): |
| 7609 | */ |
| 7610 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7611 | goto err_context; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7612 | /* |
| 7613 | * Do not allow to attach to a group in a different |
| 7614 | * task or CPU context: |
| 7615 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7616 | if (move_group) { |
Peter Zijlstra | c3c87e7 | 2015-01-23 11:19:48 +0100 | [diff] [blame] | 7617 | /* |
| 7618 | * Make sure we're both on the same task, or both |
| 7619 | * per-cpu events. |
| 7620 | */ |
| 7621 | if (group_leader->ctx->task != ctx->task) |
| 7622 | goto err_context; |
| 7623 | |
| 7624 | /* |
| 7625 | * Make sure we're both events for the same CPU; |
| 7626 | * grouping events for different CPUs is broken; since |
| 7627 | * you can never concurrently schedule them anyhow. |
| 7628 | */ |
| 7629 | if (group_leader->cpu != event->cpu) |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7630 | goto err_context; |
| 7631 | } else { |
| 7632 | if (group_leader->ctx != ctx) |
| 7633 | goto err_context; |
| 7634 | } |
| 7635 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7636 | /* |
| 7637 | * Only a group leader can be exclusive or pinned |
| 7638 | */ |
| 7639 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7640 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7641 | } |
| 7642 | |
| 7643 | if (output_event) { |
| 7644 | err = perf_event_set_output(event, output_event); |
| 7645 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7646 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 7647 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7648 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 7649 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
| 7650 | f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7651 | if (IS_ERR(event_file)) { |
| 7652 | err = PTR_ERR(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7653 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7654 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7655 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7656 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7657 | gctx = group_leader->ctx; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7658 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7659 | /* |
| 7660 | * See perf_event_ctx_lock() for comments on the details |
| 7661 | * of swizzling perf_event::ctx. |
| 7662 | */ |
| 7663 | mutex_lock_double(&gctx->mutex, &ctx->mutex); |
| 7664 | |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7665 | perf_remove_from_context(group_leader, false); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 7666 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7667 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7668 | group_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7669 | perf_remove_from_context(sibling, false); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7670 | put_ctx(gctx); |
| 7671 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7672 | } else { |
| 7673 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7674 | } |
| 7675 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7676 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7677 | |
| 7678 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7679 | /* |
| 7680 | * Wait for everybody to stop referencing the events through |
| 7681 | * the old lists, before installing it on new lists. |
| 7682 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7683 | synchronize_rcu(); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7684 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7685 | /* |
| 7686 | * Install the group siblings before the group leader. |
| 7687 | * |
| 7688 | * Because a group leader will try and install the entire group |
| 7689 | * (through the sibling list, which is still in-tact), we can |
| 7690 | * end up with siblings installed in the wrong context. |
| 7691 | * |
| 7692 | * By installing siblings first we NO-OP because they're not |
| 7693 | * reachable through the group lists. |
| 7694 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7695 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 7696 | group_entry) { |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7697 | perf_event__state_init(sibling); |
Jiri Olsa | 9fc81d8 | 2014-12-10 21:23:51 +0100 | [diff] [blame] | 7698 | perf_install_in_context(ctx, sibling, sibling->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7699 | get_ctx(ctx); |
| 7700 | } |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7701 | |
| 7702 | /* |
| 7703 | * Removing from the context ends up with disabled |
| 7704 | * event. What we want here is event in the initial |
| 7705 | * startup state, ready to be add into new context. |
| 7706 | */ |
| 7707 | perf_event__state_init(group_leader); |
| 7708 | perf_install_in_context(ctx, group_leader, group_leader->cpu); |
| 7709 | get_ctx(ctx); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 7710 | } |
| 7711 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 7712 | perf_install_in_context(ctx, event, event->cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7713 | perf_unpin_context(ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7714 | |
| 7715 | if (move_group) { |
| 7716 | mutex_unlock(&gctx->mutex); |
| 7717 | put_ctx(gctx); |
| 7718 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7719 | mutex_unlock(&ctx->mutex); |
| 7720 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7721 | put_online_cpus(); |
| 7722 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7723 | event->owner = current; |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 7724 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7725 | mutex_lock(¤t->perf_event_mutex); |
| 7726 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 7727 | mutex_unlock(¤t->perf_event_mutex); |
| 7728 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7729 | /* |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7730 | * Precalculate sample_data sizes |
| 7731 | */ |
| 7732 | perf_event__header_size(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 7733 | perf_event__id_header_size(event); |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 7734 | |
| 7735 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 7736 | * Drop the reference on the group_event after placing the |
| 7737 | * new event on the sibling_list. This ensures destruction |
| 7738 | * of the group leader will find the pointer to itself in |
| 7739 | * perf_group_detach(). |
| 7740 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7741 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7742 | fd_install(event_fd, event_file); |
| 7743 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7744 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7745 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7746 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7747 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 7748 | err_alloc: |
| 7749 | free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7750 | err_cpus: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 7751 | put_online_cpus(); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 7752 | err_task: |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 7753 | if (task) |
| 7754 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7755 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 7756 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 7757 | err_fd: |
| 7758 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7759 | return err; |
| 7760 | } |
| 7761 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7762 | /** |
| 7763 | * perf_event_create_kernel_counter |
| 7764 | * |
| 7765 | * @attr: attributes of the counter to create |
| 7766 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7767 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7768 | */ |
| 7769 | struct perf_event * |
| 7770 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7771 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7772 | perf_overflow_handler_t overflow_handler, |
| 7773 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7774 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7775 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7776 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7777 | int err; |
| 7778 | |
| 7779 | /* |
| 7780 | * Get the target context (task or percpu): |
| 7781 | */ |
| 7782 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7783 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
| 7784 | overflow_handler, context); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7785 | if (IS_ERR(event)) { |
| 7786 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7787 | goto err; |
| 7788 | } |
| 7789 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 7790 | /* Mark owner so we could distinguish it from user events. */ |
| 7791 | event->owner = EVENT_OWNER_KERNEL; |
| 7792 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7793 | account_event(event); |
| 7794 | |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 7795 | ctx = find_get_context(event->pmu, task, cpu); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7796 | if (IS_ERR(ctx)) { |
| 7797 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7798 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7799 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7800 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7801 | WARN_ON_ONCE(ctx->parent_ctx); |
| 7802 | mutex_lock(&ctx->mutex); |
| 7803 | perf_install_in_context(ctx, event, cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 7804 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7805 | mutex_unlock(&ctx->mutex); |
| 7806 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7807 | return event; |
| 7808 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7809 | err_free: |
| 7810 | free_event(event); |
| 7811 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 7812 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 7813 | } |
| 7814 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 7815 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7816 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 7817 | { |
| 7818 | struct perf_event_context *src_ctx; |
| 7819 | struct perf_event_context *dst_ctx; |
| 7820 | struct perf_event *event, *tmp; |
| 7821 | LIST_HEAD(events); |
| 7822 | |
| 7823 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 7824 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 7825 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7826 | /* |
| 7827 | * See perf_event_ctx_lock() for comments on the details |
| 7828 | * of swizzling perf_event::ctx. |
| 7829 | */ |
| 7830 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7831 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 7832 | event_entry) { |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 7833 | perf_remove_from_context(event, false); |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7834 | unaccount_event_cpu(event, src_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7835 | put_ctx(src_ctx); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7836 | list_add(&event->migrate_entry, &events); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7837 | } |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7838 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7839 | /* |
| 7840 | * Wait for the events to quiesce before re-instating them. |
| 7841 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7842 | synchronize_rcu(); |
| 7843 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 7844 | /* |
| 7845 | * Re-instate events in 2 passes. |
| 7846 | * |
| 7847 | * Skip over group leaders and only install siblings on this first |
| 7848 | * pass, siblings will not get enabled without a leader, however a |
| 7849 | * leader will enable its siblings, even if those are still on the old |
| 7850 | * context. |
| 7851 | */ |
| 7852 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 7853 | if (event->group_leader == event) |
| 7854 | continue; |
| 7855 | |
| 7856 | list_del(&event->migrate_entry); |
| 7857 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 7858 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 7859 | account_event_cpu(event, dst_cpu); |
| 7860 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 7861 | get_ctx(dst_ctx); |
| 7862 | } |
| 7863 | |
| 7864 | /* |
| 7865 | * Once all the siblings are setup properly, install the group leaders |
| 7866 | * to make it go. |
| 7867 | */ |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 7868 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 7869 | list_del(&event->migrate_entry); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7870 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 7871 | event->state = PERF_EVENT_STATE_INACTIVE; |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 7872 | account_event_cpu(event, dst_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7873 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 7874 | get_ctx(dst_ctx); |
| 7875 | } |
| 7876 | mutex_unlock(&dst_ctx->mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 7877 | mutex_unlock(&src_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 7878 | } |
| 7879 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 7880 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7881 | static void sync_child_event(struct perf_event *child_event, |
| 7882 | struct task_struct *child) |
| 7883 | { |
| 7884 | struct perf_event *parent_event = child_event->parent; |
| 7885 | u64 child_val; |
| 7886 | |
| 7887 | if (child_event->attr.inherit_stat) |
| 7888 | perf_event_read_event(child_event, child); |
| 7889 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 7890 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7891 | |
| 7892 | /* |
| 7893 | * Add back the child's count to the parent's count: |
| 7894 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 7895 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7896 | atomic64_add(child_event->total_time_enabled, |
| 7897 | &parent_event->child_total_time_enabled); |
| 7898 | atomic64_add(child_event->total_time_running, |
| 7899 | &parent_event->child_total_time_running); |
| 7900 | |
| 7901 | /* |
| 7902 | * Remove this event from the parent's list |
| 7903 | */ |
| 7904 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 7905 | mutex_lock(&parent_event->child_mutex); |
| 7906 | list_del_init(&child_event->child_list); |
| 7907 | mutex_unlock(&parent_event->child_mutex); |
| 7908 | |
| 7909 | /* |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 7910 | * Make sure user/parent get notified, that we just |
| 7911 | * lost one event. |
| 7912 | */ |
| 7913 | perf_event_wakeup(parent_event); |
| 7914 | |
| 7915 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7916 | * Release the parent event, if this was the last |
| 7917 | * reference to it. |
| 7918 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7919 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7920 | } |
| 7921 | |
| 7922 | static void |
| 7923 | __perf_event_exit_task(struct perf_event *child_event, |
| 7924 | struct perf_event_context *child_ctx, |
| 7925 | struct task_struct *child) |
| 7926 | { |
Peter Zijlstra | 1903d50 | 2014-07-15 17:27:27 +0200 | [diff] [blame] | 7927 | /* |
| 7928 | * Do not destroy the 'original' grouping; because of the context |
| 7929 | * switch optimization the original events could've ended up in a |
| 7930 | * random child task. |
| 7931 | * |
| 7932 | * If we were to destroy the original group, all group related |
| 7933 | * operations would cease to function properly after this random |
| 7934 | * child dies. |
| 7935 | * |
| 7936 | * Do destroy all inherited groups, we don't care about those |
| 7937 | * and being thorough is better. |
| 7938 | */ |
| 7939 | perf_remove_from_context(child_event, !!child_event->parent); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7940 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7941 | /* |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7942 | * It can happen that the parent exits first, and has events |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7943 | * that are still around due to the child reference. These |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7944 | * events need to be zapped. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7945 | */ |
Peter Zijlstra | 38b435b | 2011-03-15 14:37:10 +0100 | [diff] [blame] | 7946 | if (child_event->parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7947 | sync_child_event(child_event, child); |
| 7948 | free_event(child_event); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 7949 | } else { |
| 7950 | child_event->state = PERF_EVENT_STATE_EXIT; |
| 7951 | perf_event_wakeup(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7952 | } |
| 7953 | } |
| 7954 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7955 | 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] | 7956 | { |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 7957 | struct perf_event *child_event, *next; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 7958 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7959 | unsigned long flags; |
| 7960 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7961 | if (likely(!child->perf_event_ctxp[ctxn])) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7962 | perf_event_task(child, NULL, 0); |
| 7963 | return; |
| 7964 | } |
| 7965 | |
| 7966 | local_irq_save(flags); |
| 7967 | /* |
| 7968 | * We can't reschedule here because interrupts are disabled, |
| 7969 | * and either child is current or it is a task that can't be |
| 7970 | * scheduled, so we are now safe from rescheduling changing |
| 7971 | * our context. |
| 7972 | */ |
Oleg Nesterov | 806839b | 2011-01-21 18:45:47 +0100 | [diff] [blame] | 7973 | child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7974 | |
| 7975 | /* |
| 7976 | * Take the context lock here so that if find_get_context is |
| 7977 | * reading child->perf_event_ctxp, we wait until it has |
| 7978 | * incremented the context's refcount before we do put_ctx below. |
| 7979 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7980 | raw_spin_lock(&child_ctx->lock); |
Peter Zijlstra | 04dc2db | 2011-04-09 21:17:43 +0200 | [diff] [blame] | 7981 | task_ctx_sched_out(child_ctx); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7982 | child->perf_event_ctxp[ctxn] = NULL; |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 7983 | |
| 7984 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7985 | * If this context is a clone; unclone it so it can't get |
| 7986 | * swapped to another process while we're removing all |
| 7987 | * the events from it. |
| 7988 | */ |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 7989 | clone_ctx = unclone_ctx(child_ctx); |
Peter Zijlstra | 5e942bb | 2009-11-23 11:37:26 +0100 | [diff] [blame] | 7990 | update_context_time(child_ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 7991 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7992 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 7993 | if (clone_ctx) |
| 7994 | put_ctx(clone_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 7995 | |
| 7996 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7997 | * Report the task dead after unscheduling the events so that we |
| 7998 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 7999 | * get a few PERF_RECORD_READ events. |
| 8000 | */ |
| 8001 | perf_event_task(child, child_ctx, 0); |
| 8002 | |
| 8003 | /* |
| 8004 | * We can recurse on the same lock type through: |
| 8005 | * |
| 8006 | * __perf_event_exit_task() |
| 8007 | * sync_child_event() |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8008 | * put_event() |
| 8009 | * mutex_lock(&ctx->mutex) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8010 | * |
| 8011 | * But since its the parent context it won't be the same instance. |
| 8012 | */ |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 8013 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8014 | |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 8015 | 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] | 8016 | __perf_event_exit_task(child_event, child_ctx, child); |
| 8017 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8018 | mutex_unlock(&child_ctx->mutex); |
| 8019 | |
| 8020 | put_ctx(child_ctx); |
| 8021 | } |
| 8022 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8023 | /* |
| 8024 | * When a child task exits, feed back event values to parent events. |
| 8025 | */ |
| 8026 | void perf_event_exit_task(struct task_struct *child) |
| 8027 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 8028 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8029 | int ctxn; |
| 8030 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 8031 | mutex_lock(&child->perf_event_mutex); |
| 8032 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 8033 | owner_entry) { |
| 8034 | list_del_init(&event->owner_entry); |
| 8035 | |
| 8036 | /* |
| 8037 | * Ensure the list deletion is visible before we clear |
| 8038 | * the owner, closes a race against perf_release() where |
| 8039 | * we need to serialize on the owner->perf_event_mutex. |
| 8040 | */ |
| 8041 | smp_wmb(); |
| 8042 | event->owner = NULL; |
| 8043 | } |
| 8044 | mutex_unlock(&child->perf_event_mutex); |
| 8045 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8046 | for_each_task_context_nr(ctxn) |
| 8047 | perf_event_exit_task_context(child, ctxn); |
| 8048 | } |
| 8049 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8050 | static void perf_free_event(struct perf_event *event, |
| 8051 | struct perf_event_context *ctx) |
| 8052 | { |
| 8053 | struct perf_event *parent = event->parent; |
| 8054 | |
| 8055 | if (WARN_ON_ONCE(!parent)) |
| 8056 | return; |
| 8057 | |
| 8058 | mutex_lock(&parent->child_mutex); |
| 8059 | list_del_init(&event->child_list); |
| 8060 | mutex_unlock(&parent->child_mutex); |
| 8061 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8062 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8063 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 8064 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 8065 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8066 | list_del_event(event, ctx); |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 8067 | raw_spin_unlock_irq(&ctx->lock); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8068 | free_event(event); |
| 8069 | } |
| 8070 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8071 | /* |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 8072 | * Free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8073 | * perf_event_init_task below, used by fork() in case of fail. |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 8074 | * |
| 8075 | * Not all locks are strictly required, but take them anyway to be nice and |
| 8076 | * help out with the lockdep assertions. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8077 | */ |
| 8078 | void perf_event_free_task(struct task_struct *task) |
| 8079 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8080 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8081 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8082 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8083 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8084 | for_each_task_context_nr(ctxn) { |
| 8085 | ctx = task->perf_event_ctxp[ctxn]; |
| 8086 | if (!ctx) |
| 8087 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8088 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8089 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8090 | again: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8091 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 8092 | group_entry) |
| 8093 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8094 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8095 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 8096 | group_entry) |
| 8097 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8098 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8099 | if (!list_empty(&ctx->pinned_groups) || |
| 8100 | !list_empty(&ctx->flexible_groups)) |
| 8101 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8102 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8103 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8104 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8105 | put_ctx(ctx); |
| 8106 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8107 | } |
| 8108 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 8109 | void perf_event_delayed_put(struct task_struct *task) |
| 8110 | { |
| 8111 | int ctxn; |
| 8112 | |
| 8113 | for_each_task_context_nr(ctxn) |
| 8114 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 8115 | } |
| 8116 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8117 | /* |
| 8118 | * inherit a event from parent task to child task: |
| 8119 | */ |
| 8120 | static struct perf_event * |
| 8121 | inherit_event(struct perf_event *parent_event, |
| 8122 | struct task_struct *parent, |
| 8123 | struct perf_event_context *parent_ctx, |
| 8124 | struct task_struct *child, |
| 8125 | struct perf_event *group_leader, |
| 8126 | struct perf_event_context *child_ctx) |
| 8127 | { |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 8128 | enum perf_event_active_state parent_state = parent_event->state; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8129 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 8130 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8131 | |
| 8132 | /* |
| 8133 | * Instead of creating recursive hierarchies of events, |
| 8134 | * we link inherited events back to the original parent, |
| 8135 | * which has a filp for sure, which we use as the reference |
| 8136 | * count: |
| 8137 | */ |
| 8138 | if (parent_event->parent) |
| 8139 | parent_event = parent_event->parent; |
| 8140 | |
| 8141 | child_event = perf_event_alloc(&parent_event->attr, |
| 8142 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8143 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8144 | group_leader, parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8145 | NULL, NULL); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8146 | if (IS_ERR(child_event)) |
| 8147 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8148 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 8149 | if (is_orphaned_event(parent_event) || |
| 8150 | !atomic_long_inc_not_zero(&parent_event->refcount)) { |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8151 | free_event(child_event); |
| 8152 | return NULL; |
| 8153 | } |
| 8154 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8155 | get_ctx(child_ctx); |
| 8156 | |
| 8157 | /* |
| 8158 | * Make the child state follow the state of the parent event, |
| 8159 | * not its attr.disabled bit. We hold the parent's mutex, |
| 8160 | * so we won't race with perf_event_{en, dis}able_family. |
| 8161 | */ |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 8162 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8163 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 8164 | else |
| 8165 | child_event->state = PERF_EVENT_STATE_OFF; |
| 8166 | |
| 8167 | if (parent_event->attr.freq) { |
| 8168 | u64 sample_period = parent_event->hw.sample_period; |
| 8169 | struct hw_perf_event *hwc = &child_event->hw; |
| 8170 | |
| 8171 | hwc->sample_period = sample_period; |
| 8172 | hwc->last_period = sample_period; |
| 8173 | |
| 8174 | local64_set(&hwc->period_left, sample_period); |
| 8175 | } |
| 8176 | |
| 8177 | child_event->ctx = child_ctx; |
| 8178 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8179 | child_event->overflow_handler_context |
| 8180 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8181 | |
| 8182 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 8183 | * Precalculate sample_data sizes |
| 8184 | */ |
| 8185 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 8186 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 8187 | |
| 8188 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8189 | * Link it up in the child's context: |
| 8190 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 8191 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8192 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 8193 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8194 | |
| 8195 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 8196 | * Link this into the parent event's child list |
| 8197 | */ |
| 8198 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 8199 | mutex_lock(&parent_event->child_mutex); |
| 8200 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 8201 | mutex_unlock(&parent_event->child_mutex); |
| 8202 | |
| 8203 | return child_event; |
| 8204 | } |
| 8205 | |
| 8206 | static int inherit_group(struct perf_event *parent_event, |
| 8207 | struct task_struct *parent, |
| 8208 | struct perf_event_context *parent_ctx, |
| 8209 | struct task_struct *child, |
| 8210 | struct perf_event_context *child_ctx) |
| 8211 | { |
| 8212 | struct perf_event *leader; |
| 8213 | struct perf_event *sub; |
| 8214 | struct perf_event *child_ctr; |
| 8215 | |
| 8216 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 8217 | child, NULL, child_ctx); |
| 8218 | if (IS_ERR(leader)) |
| 8219 | return PTR_ERR(leader); |
| 8220 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 8221 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 8222 | child, leader, child_ctx); |
| 8223 | if (IS_ERR(child_ctr)) |
| 8224 | return PTR_ERR(child_ctr); |
| 8225 | } |
| 8226 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8227 | } |
| 8228 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8229 | static int |
| 8230 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 8231 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8232 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8233 | int *inherited_all) |
| 8234 | { |
| 8235 | int ret; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8236 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8237 | |
| 8238 | if (!event->attr.inherit) { |
| 8239 | *inherited_all = 0; |
| 8240 | return 0; |
| 8241 | } |
| 8242 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 8243 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8244 | if (!child_ctx) { |
| 8245 | /* |
| 8246 | * This is executed from the parent task context, so |
| 8247 | * inherit events that have been marked for cloning. |
| 8248 | * First allocate and initialize a context for the |
| 8249 | * child. |
| 8250 | */ |
| 8251 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 8252 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8253 | if (!child_ctx) |
| 8254 | return -ENOMEM; |
| 8255 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8256 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8257 | } |
| 8258 | |
| 8259 | ret = inherit_group(event, parent, parent_ctx, |
| 8260 | child, child_ctx); |
| 8261 | |
| 8262 | if (ret) |
| 8263 | *inherited_all = 0; |
| 8264 | |
| 8265 | return ret; |
| 8266 | } |
| 8267 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8268 | /* |
| 8269 | * Initialize the perf_event context in task_struct |
| 8270 | */ |
Jiri Olsa | 985c8dc | 2014-06-24 10:20:24 +0200 | [diff] [blame] | 8271 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8272 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8273 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8274 | struct perf_event_context *cloned_ctx; |
| 8275 | struct perf_event *event; |
| 8276 | struct task_struct *parent = current; |
| 8277 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8278 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8279 | int ret = 0; |
| 8280 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8281 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8282 | return 0; |
| 8283 | |
| 8284 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8285 | * If the parent's context is a clone, pin it so it won't get |
| 8286 | * swapped under us. |
| 8287 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8288 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Peter Zijlstra | ffb4ef2 | 2014-05-05 19:12:20 +0200 | [diff] [blame] | 8289 | if (!parent_ctx) |
| 8290 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8291 | |
| 8292 | /* |
| 8293 | * No need to check if parent_ctx != NULL here; since we saw |
| 8294 | * it non-NULL earlier, the only reason for it to become NULL |
| 8295 | * is if we exit, and since we're currently in the middle of |
| 8296 | * a fork we can't be exiting at the same time. |
| 8297 | */ |
| 8298 | |
| 8299 | /* |
| 8300 | * Lock the parent list. No need to lock the child - not PID |
| 8301 | * hashed yet and not running, so nobody can access it. |
| 8302 | */ |
| 8303 | mutex_lock(&parent_ctx->mutex); |
| 8304 | |
| 8305 | /* |
| 8306 | * We dont have to disable NMIs - we are only looking at |
| 8307 | * the list, not manipulating it: |
| 8308 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8309 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8310 | ret = inherit_task_group(event, parent, parent_ctx, |
| 8311 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8312 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8313 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8314 | } |
| 8315 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8316 | /* |
| 8317 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 8318 | * to allocations, but we need to prevent rotation because |
| 8319 | * rotate_ctx() will change the list from interrupt context. |
| 8320 | */ |
| 8321 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 8322 | parent_ctx->rotate_disable = 1; |
| 8323 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 8324 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8325 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8326 | ret = inherit_task_group(event, parent, parent_ctx, |
| 8327 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8328 | if (ret) |
| 8329 | break; |
| 8330 | } |
| 8331 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8332 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 8333 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 8334 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8335 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8336 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 8337 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8338 | /* |
| 8339 | * Mark the child context as a clone of the parent |
| 8340 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 8341 | * |
| 8342 | * Note that if the parent is a clone, the holding of |
| 8343 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8344 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 8345 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8346 | if (cloned_ctx) { |
| 8347 | child_ctx->parent_ctx = cloned_ctx; |
| 8348 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 8349 | } else { |
| 8350 | child_ctx->parent_ctx = parent_ctx; |
| 8351 | child_ctx->parent_gen = parent_ctx->generation; |
| 8352 | } |
| 8353 | get_ctx(child_ctx->parent_ctx); |
| 8354 | } |
| 8355 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 8356 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8357 | mutex_unlock(&parent_ctx->mutex); |
| 8358 | |
| 8359 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 8360 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8361 | |
| 8362 | return ret; |
| 8363 | } |
| 8364 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8365 | /* |
| 8366 | * Initialize the perf_event context in task_struct |
| 8367 | */ |
| 8368 | int perf_event_init_task(struct task_struct *child) |
| 8369 | { |
| 8370 | int ctxn, ret; |
| 8371 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 8372 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 8373 | mutex_init(&child->perf_event_mutex); |
| 8374 | INIT_LIST_HEAD(&child->perf_event_list); |
| 8375 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8376 | for_each_task_context_nr(ctxn) { |
| 8377 | ret = perf_event_init_context(child, ctxn); |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 8378 | if (ret) { |
| 8379 | perf_event_free_task(child); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8380 | return ret; |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 8381 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8382 | } |
| 8383 | |
| 8384 | return 0; |
| 8385 | } |
| 8386 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8387 | static void __init perf_event_init_all_cpus(void) |
| 8388 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8389 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8390 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8391 | |
| 8392 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8393 | swhash = &per_cpu(swevent_htable, cpu); |
| 8394 | mutex_init(&swhash->hlist_mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 8395 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8396 | } |
| 8397 | } |
| 8398 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 8399 | static void perf_event_init_cpu(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8400 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8401 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8402 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8403 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 8404 | swhash->online = true; |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 8405 | if (swhash->hlist_refcount > 0) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8406 | struct swevent_hlist *hlist; |
| 8407 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8408 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 8409 | WARN_ON(!hlist); |
| 8410 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8411 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8412 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8413 | } |
| 8414 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8415 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8416 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8417 | { |
Mark Rutland | 226424e | 2014-11-05 16:11:44 +0000 | [diff] [blame] | 8418 | struct remove_event re = { .detach_group = true }; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8419 | struct perf_event_context *ctx = __info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8420 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 8421 | rcu_read_lock(); |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 8422 | list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry) |
| 8423 | __perf_remove_from_context(&re); |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 8424 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8425 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8426 | |
| 8427 | static void perf_event_exit_cpu_context(int cpu) |
| 8428 | { |
| 8429 | struct perf_event_context *ctx; |
| 8430 | struct pmu *pmu; |
| 8431 | int idx; |
| 8432 | |
| 8433 | idx = srcu_read_lock(&pmus_srcu); |
| 8434 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 8435 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8436 | |
| 8437 | mutex_lock(&ctx->mutex); |
| 8438 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 8439 | mutex_unlock(&ctx->mutex); |
| 8440 | } |
| 8441 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8442 | } |
| 8443 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8444 | static void perf_event_exit_cpu(int cpu) |
| 8445 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8446 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8447 | |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 8448 | perf_event_exit_cpu_context(cpu); |
| 8449 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8450 | mutex_lock(&swhash->hlist_mutex); |
Jiri Olsa | 39af6b1 | 2014-04-07 11:04:08 +0200 | [diff] [blame] | 8451 | swhash->online = false; |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 8452 | swevent_hlist_release(swhash); |
| 8453 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8454 | } |
| 8455 | #else |
| 8456 | static inline void perf_event_exit_cpu(int cpu) { } |
| 8457 | #endif |
| 8458 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8459 | static int |
| 8460 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 8461 | { |
| 8462 | int cpu; |
| 8463 | |
| 8464 | for_each_online_cpu(cpu) |
| 8465 | perf_event_exit_cpu(cpu); |
| 8466 | |
| 8467 | return NOTIFY_OK; |
| 8468 | } |
| 8469 | |
| 8470 | /* |
| 8471 | * Run the perf reboot notifier at the very last possible moment so that |
| 8472 | * the generic watchdog code runs as long as possible. |
| 8473 | */ |
| 8474 | static struct notifier_block perf_reboot_notifier = { |
| 8475 | .notifier_call = perf_reboot, |
| 8476 | .priority = INT_MIN, |
| 8477 | }; |
| 8478 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 8479 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8480 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 8481 | { |
| 8482 | unsigned int cpu = (long)hcpu; |
| 8483 | |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 8484 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8485 | |
| 8486 | case CPU_UP_PREPARE: |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8487 | case CPU_DOWN_FAILED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8488 | perf_event_init_cpu(cpu); |
| 8489 | break; |
| 8490 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8491 | case CPU_UP_CANCELED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8492 | case CPU_DOWN_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8493 | perf_event_exit_cpu(cpu); |
| 8494 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8495 | default: |
| 8496 | break; |
| 8497 | } |
| 8498 | |
| 8499 | return NOTIFY_OK; |
| 8500 | } |
| 8501 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8502 | void __init perf_event_init(void) |
| 8503 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8504 | int ret; |
| 8505 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8506 | idr_init(&pmu_idr); |
| 8507 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 8508 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8509 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8510 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 8511 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 8512 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8513 | perf_tp_register(); |
| 8514 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 8515 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 8516 | |
| 8517 | ret = init_hw_breakpoint(); |
| 8518 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 8519 | |
| 8520 | /* do not patch jump label more than once per second */ |
| 8521 | jump_label_rate_limit(&perf_sched_events, HZ); |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 8522 | |
| 8523 | /* |
| 8524 | * Build time assertion that we keep the data_head at the intended |
| 8525 | * location. IOW, validation we got the __reserved[] size right. |
| 8526 | */ |
| 8527 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 8528 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8529 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8530 | |
Cody P Schafer | fd979c0 | 2015-01-30 13:45:57 -0800 | [diff] [blame] | 8531 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
| 8532 | char *page) |
| 8533 | { |
| 8534 | struct perf_pmu_events_attr *pmu_attr = |
| 8535 | container_of(attr, struct perf_pmu_events_attr, attr); |
| 8536 | |
| 8537 | if (pmu_attr->event_str) |
| 8538 | return sprintf(page, "%s\n", pmu_attr->event_str); |
| 8539 | |
| 8540 | return 0; |
| 8541 | } |
| 8542 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8543 | static int __init perf_event_sysfs_init(void) |
| 8544 | { |
| 8545 | struct pmu *pmu; |
| 8546 | int ret; |
| 8547 | |
| 8548 | mutex_lock(&pmus_lock); |
| 8549 | |
| 8550 | ret = bus_register(&pmu_bus); |
| 8551 | if (ret) |
| 8552 | goto unlock; |
| 8553 | |
| 8554 | list_for_each_entry(pmu, &pmus, entry) { |
| 8555 | if (!pmu->name || pmu->type < 0) |
| 8556 | continue; |
| 8557 | |
| 8558 | ret = pmu_dev_alloc(pmu); |
| 8559 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 8560 | } |
| 8561 | pmu_bus_running = 1; |
| 8562 | ret = 0; |
| 8563 | |
| 8564 | unlock: |
| 8565 | mutex_unlock(&pmus_lock); |
| 8566 | |
| 8567 | return ret; |
| 8568 | } |
| 8569 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8570 | |
| 8571 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8572 | static struct cgroup_subsys_state * |
| 8573 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8574 | { |
| 8575 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8576 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 8577 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8578 | if (!jc) |
| 8579 | return ERR_PTR(-ENOMEM); |
| 8580 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8581 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 8582 | if (!jc->info) { |
| 8583 | kfree(jc); |
| 8584 | return ERR_PTR(-ENOMEM); |
| 8585 | } |
| 8586 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8587 | return &jc->css; |
| 8588 | } |
| 8589 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8590 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8591 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8592 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
| 8593 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8594 | free_percpu(jc->info); |
| 8595 | kfree(jc); |
| 8596 | } |
| 8597 | |
| 8598 | static int __perf_cgroup_move(void *info) |
| 8599 | { |
| 8600 | struct task_struct *task = info; |
| 8601 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
| 8602 | return 0; |
| 8603 | } |
| 8604 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8605 | static void perf_cgroup_attach(struct cgroup_subsys_state *css, |
| 8606 | struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8607 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8608 | struct task_struct *task; |
| 8609 | |
Tejun Heo | 924f0d9a | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 8610 | cgroup_taskset_for_each(task, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8611 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8612 | } |
| 8613 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 8614 | static void perf_cgroup_exit(struct cgroup_subsys_state *css, |
| 8615 | struct cgroup_subsys_state *old_css, |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 8616 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8617 | { |
| 8618 | /* |
| 8619 | * cgroup_exit() is called in the copy_process() failure path. |
| 8620 | * Ignore this case since the task hasn't ran yet, this avoids |
| 8621 | * trying to poke a half freed task state from generic code. |
| 8622 | */ |
| 8623 | if (!(task->flags & PF_EXITING)) |
| 8624 | return; |
| 8625 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8626 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8627 | } |
| 8628 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 8629 | struct cgroup_subsys perf_event_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 8630 | .css_alloc = perf_cgroup_css_alloc, |
| 8631 | .css_free = perf_cgroup_css_free, |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 8632 | .exit = perf_cgroup_exit, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 8633 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8634 | }; |
| 8635 | #endif /* CONFIG_CGROUP_PERF */ |