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 |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 6 | * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra |
Al Viro | d36b691 | 2011-12-29 17:09:01 -0500 | [diff] [blame] | 7 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8 | * |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 9 | * For licensing details see kernel-base/COPYING |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/cpu.h> |
| 15 | #include <linux/smp.h> |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 16 | #include <linux/idr.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 17 | #include <linux/file.h> |
| 18 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 20 | #include <linux/hash.h> |
Frederic Weisbecker | 12351ef | 2013-04-20 15:48:22 +0200 | [diff] [blame] | 21 | #include <linux/tick.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 22 | #include <linux/sysfs.h> |
| 23 | #include <linux/dcache.h> |
| 24 | #include <linux/percpu.h> |
| 25 | #include <linux/ptrace.h> |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 26 | #include <linux/reboot.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 27 | #include <linux/vmstat.h> |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 28 | #include <linux/device.h> |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 31 | #include <linux/hardirq.h> |
| 32 | #include <linux/rculist.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/syscalls.h> |
| 35 | #include <linux/anon_inodes.h> |
| 36 | #include <linux/kernel_stat.h> |
Matt Fleming | 39bed6c | 2015-01-23 18:45:40 +0000 | [diff] [blame] | 37 | #include <linux/cgroup.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 38 | #include <linux/perf_event.h> |
Steven Rostedt (Red Hat) | af658dc | 2015-04-29 14:36:05 -0400 | [diff] [blame] | 39 | #include <linux/trace_events.h> |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 40 | #include <linux/hw_breakpoint.h> |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 41 | #include <linux/mm_types.h> |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 42 | #include <linux/module.h> |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 43 | #include <linux/mman.h> |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 44 | #include <linux/compat.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 45 | #include <linux/bpf.h> |
| 46 | #include <linux/filter.h> |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 47 | #include <linux/namei.h> |
| 48 | #include <linux/parser.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 49 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 50 | #include "internal.h" |
| 51 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 52 | #include <asm/irq_regs.h> |
| 53 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 54 | typedef int (*remote_function_f)(void *); |
| 55 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 56 | struct remote_function_call { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 57 | struct task_struct *p; |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 58 | remote_function_f func; |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 59 | void *info; |
| 60 | int ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static void remote_function(void *data) |
| 64 | { |
| 65 | struct remote_function_call *tfc = data; |
| 66 | struct task_struct *p = tfc->p; |
| 67 | |
| 68 | if (p) { |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 69 | /* -EAGAIN */ |
| 70 | if (task_cpu(p) != smp_processor_id()) |
| 71 | return; |
| 72 | |
| 73 | /* |
| 74 | * Now that we're on right CPU with IRQs disabled, we can test |
| 75 | * if we hit the right task without races. |
| 76 | */ |
| 77 | |
| 78 | tfc->ret = -ESRCH; /* No such (running) process */ |
| 79 | if (p != current) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
| 83 | tfc->ret = tfc->func(tfc->info); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * task_function_call - call a function on the cpu on which a task runs |
| 88 | * @p: the task to evaluate |
| 89 | * @func: the function to be called |
| 90 | * @info: the function call argument |
| 91 | * |
| 92 | * Calls the function @func when the task is currently running. This might |
| 93 | * be on the current CPU, which just calls the function directly |
| 94 | * |
| 95 | * returns: @func return value, or |
| 96 | * -ESRCH - when the process isn't running |
| 97 | * -EAGAIN - when the process moved away |
| 98 | */ |
| 99 | static int |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 100 | task_function_call(struct task_struct *p, remote_function_f func, void *info) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 101 | { |
| 102 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 103 | .p = p, |
| 104 | .func = func, |
| 105 | .info = info, |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 106 | .ret = -EAGAIN, |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 107 | }; |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 108 | int ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 109 | |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 110 | do { |
| 111 | ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1); |
| 112 | if (!ret) |
| 113 | ret = data.ret; |
| 114 | } while (ret == -EAGAIN); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 115 | |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 116 | return ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * cpu_function_call - call a function on the cpu |
| 121 | * @func: the function to be called |
| 122 | * @info: the function call argument |
| 123 | * |
| 124 | * Calls the function @func on the remote cpu. |
| 125 | * |
| 126 | * returns: @func return value or -ENXIO when the cpu is offline |
| 127 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 128 | static int cpu_function_call(int cpu, remote_function_f func, void *info) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 129 | { |
| 130 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 131 | .p = NULL, |
| 132 | .func = func, |
| 133 | .info = info, |
| 134 | .ret = -ENXIO, /* No such CPU */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | smp_call_function_single(cpu, remote_function, &data, 1); |
| 138 | |
| 139 | return data.ret; |
| 140 | } |
| 141 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 142 | static inline struct perf_cpu_context * |
| 143 | __get_cpu_context(struct perf_event_context *ctx) |
| 144 | { |
| 145 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); |
| 146 | } |
| 147 | |
| 148 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, |
| 149 | struct perf_event_context *ctx) |
| 150 | { |
| 151 | raw_spin_lock(&cpuctx->ctx.lock); |
| 152 | if (ctx) |
| 153 | raw_spin_lock(&ctx->lock); |
| 154 | } |
| 155 | |
| 156 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, |
| 157 | struct perf_event_context *ctx) |
| 158 | { |
| 159 | if (ctx) |
| 160 | raw_spin_unlock(&ctx->lock); |
| 161 | raw_spin_unlock(&cpuctx->ctx.lock); |
| 162 | } |
| 163 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 164 | #define TASK_TOMBSTONE ((void *)-1L) |
| 165 | |
| 166 | static bool is_kernel_event(struct perf_event *event) |
| 167 | { |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 168 | return READ_ONCE(event->owner) == TASK_TOMBSTONE; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 171 | /* |
| 172 | * On task ctx scheduling... |
| 173 | * |
| 174 | * When !ctx->nr_events a task context will not be scheduled. This means |
| 175 | * we can disable the scheduler hooks (for performance) without leaving |
| 176 | * pending task ctx state. |
| 177 | * |
| 178 | * This however results in two special cases: |
| 179 | * |
| 180 | * - removing the last event from a task ctx; this is relatively straight |
| 181 | * forward and is done in __perf_remove_from_context. |
| 182 | * |
| 183 | * - adding the first event to a task ctx; this is tricky because we cannot |
| 184 | * rely on ctx->is_active and therefore cannot use event_function_call(). |
| 185 | * See perf_install_in_context(). |
| 186 | * |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 187 | * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. |
| 188 | */ |
| 189 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 190 | typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, |
| 191 | struct perf_event_context *, void *); |
| 192 | |
| 193 | struct event_function_struct { |
| 194 | struct perf_event *event; |
| 195 | event_f func; |
| 196 | void *data; |
| 197 | }; |
| 198 | |
| 199 | static int event_function(void *info) |
| 200 | { |
| 201 | struct event_function_struct *efs = info; |
| 202 | struct perf_event *event = efs->event; |
| 203 | struct perf_event_context *ctx = event->ctx; |
| 204 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 205 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 206 | int ret = 0; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 207 | |
| 208 | WARN_ON_ONCE(!irqs_disabled()); |
| 209 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 210 | perf_ctx_lock(cpuctx, task_ctx); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 211 | /* |
| 212 | * Since we do the IPI call without holding ctx->lock things can have |
| 213 | * changed, double check we hit the task we set out to hit. |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 214 | */ |
| 215 | if (ctx->task) { |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 216 | if (ctx->task != current) { |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 217 | ret = -ESRCH; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 218 | goto unlock; |
| 219 | } |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 220 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 221 | /* |
| 222 | * We only use event_function_call() on established contexts, |
| 223 | * and event_function() is only ever called when active (or |
| 224 | * rather, we'll have bailed in task_function_call() or the |
| 225 | * above ctx->task != current test), therefore we must have |
| 226 | * ctx->is_active here. |
| 227 | */ |
| 228 | WARN_ON_ONCE(!ctx->is_active); |
| 229 | /* |
| 230 | * And since we have ctx->is_active, cpuctx->task_ctx must |
| 231 | * match. |
| 232 | */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 233 | WARN_ON_ONCE(task_ctx != ctx); |
| 234 | } else { |
| 235 | WARN_ON_ONCE(&cpuctx->ctx != ctx); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 236 | } |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 237 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 238 | efs->func(event, cpuctx, ctx, efs->data); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 239 | unlock: |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 240 | perf_ctx_unlock(cpuctx, task_ctx); |
| 241 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 242 | return ret; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static void event_function_local(struct perf_event *event, event_f func, void *data) |
| 246 | { |
| 247 | struct event_function_struct efs = { |
| 248 | .event = event, |
| 249 | .func = func, |
| 250 | .data = data, |
| 251 | }; |
| 252 | |
| 253 | int ret = event_function(&efs); |
| 254 | WARN_ON_ONCE(ret); |
| 255 | } |
| 256 | |
| 257 | static void event_function_call(struct perf_event *event, event_f func, void *data) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 258 | { |
| 259 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 260 | struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 261 | struct event_function_struct efs = { |
| 262 | .event = event, |
| 263 | .func = func, |
| 264 | .data = data, |
| 265 | }; |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 266 | |
Peter Zijlstra | c97f473 | 2016-01-14 10:51:03 +0100 | [diff] [blame] | 267 | if (!event->parent) { |
| 268 | /* |
| 269 | * If this is a !child event, we must hold ctx::mutex to |
| 270 | * stabilize the the event->ctx relation. See |
| 271 | * perf_event_ctx_lock(). |
| 272 | */ |
| 273 | lockdep_assert_held(&ctx->mutex); |
| 274 | } |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 275 | |
| 276 | if (!task) { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 277 | cpu_function_call(event->cpu, event_function, &efs); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 281 | if (task == TASK_TOMBSTONE) |
| 282 | return; |
| 283 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 284 | again: |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 285 | if (!task_function_call(task, event_function, &efs)) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 286 | return; |
| 287 | |
| 288 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 289 | /* |
| 290 | * Reload the task pointer, it might have been changed by |
| 291 | * a concurrent perf_event_context_sched_out(). |
| 292 | */ |
| 293 | task = ctx->task; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 294 | if (task == TASK_TOMBSTONE) { |
| 295 | raw_spin_unlock_irq(&ctx->lock); |
| 296 | return; |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 297 | } |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 298 | if (ctx->is_active) { |
| 299 | raw_spin_unlock_irq(&ctx->lock); |
| 300 | goto again; |
| 301 | } |
| 302 | func(event, NULL, ctx, data); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 303 | raw_spin_unlock_irq(&ctx->lock); |
| 304 | } |
| 305 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 306 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
| 307 | PERF_FLAG_FD_OUTPUT |\ |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 308 | PERF_FLAG_PID_CGROUP |\ |
| 309 | PERF_FLAG_FD_CLOEXEC) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 310 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 311 | /* |
| 312 | * branch priv levels that need permission checks |
| 313 | */ |
| 314 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ |
| 315 | (PERF_SAMPLE_BRANCH_KERNEL |\ |
| 316 | PERF_SAMPLE_BRANCH_HV) |
| 317 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 318 | enum event_type_t { |
| 319 | EVENT_FLEXIBLE = 0x1, |
| 320 | EVENT_PINNED = 0x2, |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 321 | EVENT_TIME = 0x4, |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 322 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
| 323 | }; |
| 324 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 325 | /* |
| 326 | * perf_sched_events : >0 events exist |
| 327 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu |
| 328 | */ |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 329 | |
| 330 | static void perf_sched_delayed(struct work_struct *work); |
| 331 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); |
| 332 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); |
| 333 | static DEFINE_MUTEX(perf_sched_mutex); |
| 334 | static atomic_t perf_sched_count; |
| 335 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 336 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 337 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 338 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 339 | static atomic_t nr_mmap_events __read_mostly; |
| 340 | static atomic_t nr_comm_events __read_mostly; |
| 341 | static atomic_t nr_task_events __read_mostly; |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 342 | static atomic_t nr_freq_events __read_mostly; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 343 | static atomic_t nr_switch_events __read_mostly; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 344 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 345 | static LIST_HEAD(pmus); |
| 346 | static DEFINE_MUTEX(pmus_lock); |
| 347 | static struct srcu_struct pmus_srcu; |
| 348 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 349 | /* |
| 350 | * perf event paranoia level: |
| 351 | * -1 - not paranoid at all |
| 352 | * 0 - disallow raw tracepoint access for unpriv |
| 353 | * 1 - disallow cpu events for unpriv |
| 354 | * 2 - disallow kernel profiling for unpriv |
| 355 | */ |
Andy Lutomirski | 0161028 | 2016-05-09 15:48:51 -0700 | [diff] [blame] | 356 | int sysctl_perf_event_paranoid __read_mostly = 2; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 357 | |
Frederic Weisbecker | 2044338 | 2011-03-31 03:33:29 +0200 | [diff] [blame] | 358 | /* Minimum for 512 kiB + 1 user control page */ |
| 359 | 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] | 360 | |
| 361 | /* |
| 362 | * max perf event sample rate |
| 363 | */ |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 364 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
| 365 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) |
| 366 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 |
| 367 | |
| 368 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; |
| 369 | |
| 370 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); |
| 371 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; |
| 372 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 373 | static int perf_sample_allowed_ns __read_mostly = |
| 374 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 375 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 376 | static void update_perf_cpu_limits(void) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 377 | { |
| 378 | u64 tmp = perf_sample_period_ns; |
| 379 | |
| 380 | tmp *= sysctl_perf_cpu_time_max_percent; |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 381 | tmp = div_u64(tmp, 100); |
| 382 | if (!tmp) |
| 383 | tmp = 1; |
| 384 | |
| 385 | WRITE_ONCE(perf_sample_allowed_ns, tmp); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 386 | } |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 387 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 388 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
| 389 | |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 390 | int perf_proc_update_handler(struct ctl_table *table, int write, |
| 391 | void __user *buffer, size_t *lenp, |
| 392 | loff_t *ppos) |
| 393 | { |
Knut Petersen | 723478c | 2013-09-25 14:29:37 +0200 | [diff] [blame] | 394 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 395 | |
| 396 | if (ret || !write) |
| 397 | return ret; |
| 398 | |
| 399 | 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] | 400 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 401 | update_perf_cpu_limits(); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 402 | |
| 403 | return 0; |
| 404 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 405 | |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 406 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; |
| 407 | |
| 408 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, |
| 409 | void __user *buffer, size_t *lenp, |
| 410 | loff_t *ppos) |
| 411 | { |
| 412 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 413 | |
| 414 | if (ret || !write) |
| 415 | return ret; |
| 416 | |
Peter Zijlstra | b303e7c | 2016-04-04 09:57:40 +0200 | [diff] [blame] | 417 | if (sysctl_perf_cpu_time_max_percent == 100 || |
| 418 | sysctl_perf_cpu_time_max_percent == 0) { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 419 | printk(KERN_WARNING |
| 420 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); |
| 421 | WRITE_ONCE(perf_sample_allowed_ns, 0); |
| 422 | } else { |
| 423 | update_perf_cpu_limits(); |
| 424 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * perf samples are done in some very critical code paths (NMIs). |
| 431 | * If they take too much CPU time, the system can lock up and not |
| 432 | * get any real work done. This will drop the sample rate when |
| 433 | * we detect that events are taking too long. |
| 434 | */ |
| 435 | #define NR_ACCUMULATED_SAMPLES 128 |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 436 | static DEFINE_PER_CPU(u64, running_sample_length); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 437 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 438 | static u64 __report_avg; |
| 439 | static u64 __report_allowed; |
| 440 | |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 441 | static void perf_duration_warn(struct irq_work *w) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 442 | { |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 443 | printk_ratelimited(KERN_WARNING |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 444 | "perf: interrupt took too long (%lld > %lld), lowering " |
| 445 | "kernel.perf_event_max_sample_rate to %d\n", |
| 446 | __report_avg, __report_allowed, |
| 447 | sysctl_perf_event_sample_rate); |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); |
| 451 | |
| 452 | void perf_sample_event_took(u64 sample_len_ns) |
| 453 | { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 454 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
| 455 | u64 running_len; |
| 456 | u64 avg_len; |
| 457 | u32 max; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 458 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 459 | if (max_len == 0) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 460 | return; |
| 461 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 462 | /* Decay the counter by 1 average sample. */ |
| 463 | running_len = __this_cpu_read(running_sample_length); |
| 464 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; |
| 465 | running_len += sample_len_ns; |
| 466 | __this_cpu_write(running_sample_length, running_len); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 467 | |
| 468 | /* |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 469 | * Note: this will be biased artifically low until we have |
| 470 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 471 | * from having to maintain a count. |
| 472 | */ |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 473 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
| 474 | if (avg_len <= max_len) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 475 | return; |
| 476 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 477 | __report_avg = avg_len; |
| 478 | __report_allowed = max_len; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 479 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 480 | /* |
| 481 | * Compute a throttle threshold 25% below the current duration. |
| 482 | */ |
| 483 | avg_len += avg_len / 4; |
| 484 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; |
| 485 | if (avg_len < max) |
| 486 | max /= (u32)avg_len; |
| 487 | else |
| 488 | max = 1; |
| 489 | |
| 490 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
| 491 | WRITE_ONCE(max_samples_per_tick, max); |
| 492 | |
| 493 | sysctl_perf_event_sample_rate = max * HZ; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 494 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 495 | |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 496 | if (!irq_work_queue(&perf_duration_work)) { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 497 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 498 | "kernel.perf_event_max_sample_rate to %d\n", |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 499 | __report_avg, __report_allowed, |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 500 | sysctl_perf_event_sample_rate); |
| 501 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 504 | static atomic64_t perf_event_id; |
| 505 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 506 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 507 | enum event_type_t event_type); |
| 508 | |
| 509 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 510 | enum event_type_t event_type, |
| 511 | struct task_struct *task); |
| 512 | |
| 513 | static void update_context_time(struct perf_event_context *ctx); |
| 514 | static u64 perf_event_time(struct perf_event *event); |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 515 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 516 | void __weak perf_event_print_debug(void) { } |
| 517 | |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 518 | extern __weak const char *perf_pmu_name(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 519 | { |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 520 | return "pmu"; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 523 | static inline u64 perf_clock(void) |
| 524 | { |
| 525 | return local_clock(); |
| 526 | } |
| 527 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 528 | static inline u64 perf_event_clock(struct perf_event *event) |
| 529 | { |
| 530 | return event->clock(); |
| 531 | } |
| 532 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 533 | #ifdef CONFIG_CGROUP_PERF |
| 534 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 535 | static inline bool |
| 536 | perf_cgroup_match(struct perf_event *event) |
| 537 | { |
| 538 | struct perf_event_context *ctx = event->ctx; |
| 539 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 540 | |
Tejun Heo | ef824fa | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 541 | /* @event doesn't care about cgroup */ |
| 542 | if (!event->cgrp) |
| 543 | return true; |
| 544 | |
| 545 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ |
| 546 | if (!cpuctx->cgrp) |
| 547 | return false; |
| 548 | |
| 549 | /* |
| 550 | * Cgroup scoping is recursive. An event enabled for a cgroup is |
| 551 | * also enabled for all its descendant cgroups. If @cpuctx's |
| 552 | * cgroup is a descendant of @event's (the test covers identity |
| 553 | * case), it's a match. |
| 554 | */ |
| 555 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, |
| 556 | event->cgrp->css.cgroup); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 559 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 560 | { |
Zefan Li | 4e2ba65 | 2014-09-19 16:53:14 +0800 | [diff] [blame] | 561 | css_put(&event->cgrp->css); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 562 | event->cgrp = NULL; |
| 563 | } |
| 564 | |
| 565 | static inline int is_cgroup_event(struct perf_event *event) |
| 566 | { |
| 567 | return event->cgrp != NULL; |
| 568 | } |
| 569 | |
| 570 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 571 | { |
| 572 | struct perf_cgroup_info *t; |
| 573 | |
| 574 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 575 | return t->time; |
| 576 | } |
| 577 | |
| 578 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) |
| 579 | { |
| 580 | struct perf_cgroup_info *info; |
| 581 | u64 now; |
| 582 | |
| 583 | now = perf_clock(); |
| 584 | |
| 585 | info = this_cpu_ptr(cgrp->info); |
| 586 | |
| 587 | info->time += now - info->timestamp; |
| 588 | info->timestamp = now; |
| 589 | } |
| 590 | |
| 591 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 592 | { |
| 593 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; |
| 594 | if (cgrp_out) |
| 595 | __update_cgrp_time(cgrp_out); |
| 596 | } |
| 597 | |
| 598 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 599 | { |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 600 | struct perf_cgroup *cgrp; |
| 601 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 602 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 603 | * ensure we access cgroup data only when needed and |
| 604 | * when we know the cgroup is pinned (css_get) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 605 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 606 | if (!is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 607 | return; |
| 608 | |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 609 | cgrp = perf_cgroup_from_task(current, event->ctx); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 610 | /* |
| 611 | * Do not update time when cgroup is not active |
| 612 | */ |
| 613 | if (cgrp == event->cgrp) |
| 614 | __update_cgrp_time(event->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 618 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 619 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 620 | { |
| 621 | struct perf_cgroup *cgrp; |
| 622 | struct perf_cgroup_info *info; |
| 623 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 624 | /* |
| 625 | * ctx->lock held by caller |
| 626 | * ensure we do not access cgroup data |
| 627 | * unless we have the cgroup pinned (css_get) |
| 628 | */ |
| 629 | if (!task || !ctx->nr_cgroups) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 630 | return; |
| 631 | |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 632 | cgrp = perf_cgroup_from_task(task, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 633 | info = this_cpu_ptr(cgrp->info); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 634 | info->timestamp = ctx->timestamp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
| 638 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ |
| 639 | |
| 640 | /* |
| 641 | * reschedule events based on the cgroup constraint of task. |
| 642 | * |
| 643 | * mode SWOUT : schedule out everything |
| 644 | * mode SWIN : schedule in based on cgroup for next |
| 645 | */ |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 646 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 647 | { |
| 648 | struct perf_cpu_context *cpuctx; |
| 649 | struct pmu *pmu; |
| 650 | unsigned long flags; |
| 651 | |
| 652 | /* |
| 653 | * disable interrupts to avoid geting nr_cgroup |
| 654 | * changes via __perf_event_disable(). Also |
| 655 | * avoids preemption. |
| 656 | */ |
| 657 | local_irq_save(flags); |
| 658 | |
| 659 | /* |
| 660 | * we reschedule only in the presence of cgroup |
| 661 | * constrained events. |
| 662 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 663 | |
| 664 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 665 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 666 | if (cpuctx->unique_pmu != pmu) |
| 667 | continue; /* ensure we process each cpuctx once */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 668 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 669 | /* |
| 670 | * perf_cgroup_events says at least one |
| 671 | * context on this CPU has cgroup events. |
| 672 | * |
| 673 | * ctx->nr_cgroups reports the number of cgroup |
| 674 | * events for a context. |
| 675 | */ |
| 676 | if (cpuctx->ctx.nr_cgroups > 0) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 677 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 678 | perf_pmu_disable(cpuctx->ctx.pmu); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 679 | |
| 680 | if (mode & PERF_CGROUP_SWOUT) { |
| 681 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 682 | /* |
| 683 | * must not be done before ctxswout due |
| 684 | * to event_filter_match() in event_sched_out() |
| 685 | */ |
| 686 | cpuctx->cgrp = NULL; |
| 687 | } |
| 688 | |
| 689 | if (mode & PERF_CGROUP_SWIN) { |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 690 | WARN_ON_ONCE(cpuctx->cgrp); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 691 | /* |
| 692 | * set cgrp before ctxsw in to allow |
| 693 | * event_filter_match() to not have to pass |
| 694 | * task around |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 695 | * we pass the cpuctx->ctx to perf_cgroup_from_task() |
| 696 | * because cgorup events are only per-cpu |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 697 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 698 | cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 699 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
| 700 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 701 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 702 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 703 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 704 | } |
| 705 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 706 | local_irq_restore(flags); |
| 707 | } |
| 708 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 709 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 710 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 711 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 712 | struct perf_cgroup *cgrp1; |
| 713 | struct perf_cgroup *cgrp2 = NULL; |
| 714 | |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 715 | rcu_read_lock(); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 716 | /* |
| 717 | * we come here when we know perf_cgroup_events > 0 |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 718 | * we do not need to pass the ctx here because we know |
| 719 | * we are holding the rcu lock |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 720 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 721 | cgrp1 = perf_cgroup_from_task(task, NULL); |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 722 | cgrp2 = perf_cgroup_from_task(next, NULL); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 723 | |
| 724 | /* |
| 725 | * only schedule out current cgroup events if we know |
| 726 | * that we are switching to a different cgroup. Otherwise, |
| 727 | * do no touch the cgroup events. |
| 728 | */ |
| 729 | if (cgrp1 != cgrp2) |
| 730 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 731 | |
| 732 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 733 | } |
| 734 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 735 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 736 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 737 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 738 | struct perf_cgroup *cgrp1; |
| 739 | struct perf_cgroup *cgrp2 = NULL; |
| 740 | |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 741 | rcu_read_lock(); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 742 | /* |
| 743 | * we come here when we know perf_cgroup_events > 0 |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 744 | * we do not need to pass the ctx here because we know |
| 745 | * we are holding the rcu lock |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 746 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 747 | cgrp1 = perf_cgroup_from_task(task, NULL); |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 748 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 749 | |
| 750 | /* |
| 751 | * only need to schedule in cgroup events if we are changing |
| 752 | * cgroup during ctxsw. Cgroup events were not scheduled |
| 753 | * out of ctxsw out if that was not the case. |
| 754 | */ |
| 755 | if (cgrp1 != cgrp2) |
| 756 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 757 | |
| 758 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
| 762 | struct perf_event_attr *attr, |
| 763 | struct perf_event *group_leader) |
| 764 | { |
| 765 | struct perf_cgroup *cgrp; |
| 766 | struct cgroup_subsys_state *css; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 767 | struct fd f = fdget(fd); |
| 768 | int ret = 0; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 769 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 770 | if (!f.file) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 771 | return -EBADF; |
| 772 | |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 773 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 774 | &perf_event_cgrp_subsys); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 775 | if (IS_ERR(css)) { |
| 776 | ret = PTR_ERR(css); |
| 777 | goto out; |
| 778 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 779 | |
| 780 | cgrp = container_of(css, struct perf_cgroup, css); |
| 781 | event->cgrp = cgrp; |
| 782 | |
| 783 | /* |
| 784 | * all events in a group must monitor |
| 785 | * the same cgroup because a task belongs |
| 786 | * to only one perf cgroup at a time |
| 787 | */ |
| 788 | if (group_leader && group_leader->cgrp != cgrp) { |
| 789 | perf_detach_cgroup(event); |
| 790 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 791 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 792 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 793 | fdput(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | static inline void |
| 798 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 799 | { |
| 800 | struct perf_cgroup_info *t; |
| 801 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 802 | event->shadow_ctx_time = now - t->timestamp; |
| 803 | } |
| 804 | |
| 805 | static inline void |
| 806 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 807 | { |
| 808 | /* |
| 809 | * when the current task's perf cgroup does not match |
| 810 | * the event's, we need to remember to call the |
| 811 | * perf_mark_enable() function the first time a task with |
| 812 | * a matching perf cgroup is scheduled in. |
| 813 | */ |
| 814 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 815 | event->cgrp_defer_enabled = 1; |
| 816 | } |
| 817 | |
| 818 | static inline void |
| 819 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 820 | struct perf_event_context *ctx) |
| 821 | { |
| 822 | struct perf_event *sub; |
| 823 | u64 tstamp = perf_event_time(event); |
| 824 | |
| 825 | if (!event->cgrp_defer_enabled) |
| 826 | return; |
| 827 | |
| 828 | event->cgrp_defer_enabled = 0; |
| 829 | |
| 830 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 831 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 832 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 833 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 834 | sub->cgrp_defer_enabled = 0; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | #else /* !CONFIG_CGROUP_PERF */ |
| 839 | |
| 840 | static inline bool |
| 841 | perf_cgroup_match(struct perf_event *event) |
| 842 | { |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 847 | {} |
| 848 | |
| 849 | static inline int is_cgroup_event(struct perf_event *event) |
| 850 | { |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 855 | { |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 860 | { |
| 861 | } |
| 862 | |
| 863 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 864 | { |
| 865 | } |
| 866 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 867 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 868 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 869 | { |
| 870 | } |
| 871 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 872 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 873 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 874 | { |
| 875 | } |
| 876 | |
| 877 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 878 | struct perf_event_attr *attr, |
| 879 | struct perf_event *group_leader) |
| 880 | { |
| 881 | return -EINVAL; |
| 882 | } |
| 883 | |
| 884 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 885 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 886 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 887 | { |
| 888 | } |
| 889 | |
| 890 | void |
| 891 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 892 | { |
| 893 | } |
| 894 | |
| 895 | static inline void |
| 896 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 897 | { |
| 898 | } |
| 899 | |
| 900 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 901 | { |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | static inline void |
| 906 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 907 | { |
| 908 | } |
| 909 | |
| 910 | static inline void |
| 911 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 912 | struct perf_event_context *ctx) |
| 913 | { |
| 914 | } |
| 915 | #endif |
| 916 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 917 | /* |
| 918 | * set default to be dependent on timer tick just |
| 919 | * like original code |
| 920 | */ |
| 921 | #define PERF_CPU_HRTIMER (1000 / HZ) |
| 922 | /* |
| 923 | * function must be called with interrupts disbled |
| 924 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 925 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 926 | { |
| 927 | struct perf_cpu_context *cpuctx; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 928 | int rotations = 0; |
| 929 | |
| 930 | WARN_ON(!irqs_disabled()); |
| 931 | |
| 932 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 933 | rotations = perf_rotate_context(cpuctx); |
| 934 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 935 | raw_spin_lock(&cpuctx->hrtimer_lock); |
| 936 | if (rotations) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 937 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 938 | else |
| 939 | cpuctx->hrtimer_active = 0; |
| 940 | raw_spin_unlock(&cpuctx->hrtimer_lock); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 941 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 942 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 945 | static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 946 | { |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 947 | struct hrtimer *timer = &cpuctx->hrtimer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 948 | struct pmu *pmu = cpuctx->ctx.pmu; |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 949 | u64 interval; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 950 | |
| 951 | /* no multiplexing needed for SW PMU */ |
| 952 | if (pmu->task_ctx_nr == perf_sw_context) |
| 953 | return; |
| 954 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 955 | /* |
| 956 | * check default is sane, if not set then force to |
| 957 | * default interval (1/tick) |
| 958 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 959 | interval = pmu->hrtimer_interval_ms; |
| 960 | if (interval < 1) |
| 961 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 962 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 963 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 964 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 965 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
| 966 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 967 | timer->function = perf_mux_hrtimer_handler; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 970 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 971 | { |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 972 | struct hrtimer *timer = &cpuctx->hrtimer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 973 | struct pmu *pmu = cpuctx->ctx.pmu; |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 974 | unsigned long flags; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 975 | |
| 976 | /* not for SW PMU */ |
| 977 | if (pmu->task_ctx_nr == perf_sw_context) |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 978 | return 0; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 979 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 980 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
| 981 | if (!cpuctx->hrtimer_active) { |
| 982 | cpuctx->hrtimer_active = 1; |
| 983 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); |
| 984 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); |
| 985 | } |
| 986 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 987 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 988 | return 0; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 991 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 992 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 993 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 994 | if (!(*count)++) |
| 995 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 998 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 999 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1000 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 1001 | if (!--(*count)) |
| 1002 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1005 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1006 | |
| 1007 | /* |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1008 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
| 1009 | * perf_event_task_tick() are fully serialized because they're strictly cpu |
| 1010 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs |
| 1011 | * disabled, while perf_event_task_tick is called from IRQ context. |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1012 | */ |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1013 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1014 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1015 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1016 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1017 | WARN_ON(!irqs_disabled()); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1018 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1019 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
| 1020 | |
| 1021 | list_add(&ctx->active_ctx_list, head); |
| 1022 | } |
| 1023 | |
| 1024 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) |
| 1025 | { |
| 1026 | WARN_ON(!irqs_disabled()); |
| 1027 | |
| 1028 | WARN_ON(list_empty(&ctx->active_ctx_list)); |
| 1029 | |
| 1030 | list_del_init(&ctx->active_ctx_list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static void get_ctx(struct perf_event_context *ctx) |
| 1034 | { |
| 1035 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 1036 | } |
| 1037 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 1038 | static void free_ctx(struct rcu_head *head) |
| 1039 | { |
| 1040 | struct perf_event_context *ctx; |
| 1041 | |
| 1042 | ctx = container_of(head, struct perf_event_context, rcu_head); |
| 1043 | kfree(ctx->task_ctx_data); |
| 1044 | kfree(ctx); |
| 1045 | } |
| 1046 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1047 | static void put_ctx(struct perf_event_context *ctx) |
| 1048 | { |
| 1049 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 1050 | if (ctx->parent_ctx) |
| 1051 | put_ctx(ctx->parent_ctx); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1052 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1053 | put_task_struct(ctx->task); |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 1054 | call_rcu(&ctx->rcu_head, free_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1058 | /* |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1059 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and |
| 1060 | * perf_pmu_migrate_context() we need some magic. |
| 1061 | * |
| 1062 | * Those places that change perf_event::ctx will hold both |
| 1063 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. |
| 1064 | * |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 1065 | * Lock ordering is by mutex address. There are two other sites where |
| 1066 | * perf_event_context::mutex nests and those are: |
| 1067 | * |
| 1068 | * - perf_event_exit_task_context() [ child , 0 ] |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 1069 | * perf_event_exit_event() |
| 1070 | * put_event() [ parent, 1 ] |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 1071 | * |
| 1072 | * - perf_event_init_context() [ parent, 0 ] |
| 1073 | * inherit_task_group() |
| 1074 | * inherit_group() |
| 1075 | * inherit_event() |
| 1076 | * perf_event_alloc() |
| 1077 | * perf_init_event() |
| 1078 | * perf_try_init_event() [ child , 1 ] |
| 1079 | * |
| 1080 | * While it appears there is an obvious deadlock here -- the parent and child |
| 1081 | * nesting levels are inverted between the two. This is in fact safe because |
| 1082 | * life-time rules separate them. That is an exiting task cannot fork, and a |
| 1083 | * spawning task cannot (yet) exit. |
| 1084 | * |
| 1085 | * But remember that that these are parent<->child context relations, and |
| 1086 | * migration does not affect children, therefore these two orderings should not |
| 1087 | * interact. |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1088 | * |
| 1089 | * The change in perf_event::ctx does not affect children (as claimed above) |
| 1090 | * because the sys_perf_event_open() case will install a new event and break |
| 1091 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only |
| 1092 | * concerned with cpuctx and that doesn't have children. |
| 1093 | * |
| 1094 | * The places that change perf_event::ctx will issue: |
| 1095 | * |
| 1096 | * perf_remove_from_context(); |
| 1097 | * synchronize_rcu(); |
| 1098 | * perf_install_in_context(); |
| 1099 | * |
| 1100 | * to affect the change. The remove_from_context() + synchronize_rcu() should |
| 1101 | * quiesce the event, after which we can install it in the new location. This |
| 1102 | * means that only external vectors (perf_fops, prctl) can perturb the event |
| 1103 | * while in transit. Therefore all such accessors should also acquire |
| 1104 | * perf_event_context::mutex to serialize against this. |
| 1105 | * |
| 1106 | * However; because event->ctx can change while we're waiting to acquire |
| 1107 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() |
| 1108 | * function. |
| 1109 | * |
| 1110 | * Lock order: |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 1111 | * cred_guard_mutex |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1112 | * task_struct::perf_event_mutex |
| 1113 | * perf_event_context::mutex |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1114 | * perf_event::child_mutex; |
Peter Zijlstra | 07c4a77 | 2016-01-26 12:15:37 +0100 | [diff] [blame] | 1115 | * perf_event_context::lock |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1116 | * perf_event::mmap_mutex |
| 1117 | * mmap_sem |
| 1118 | */ |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1119 | static struct perf_event_context * |
| 1120 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1121 | { |
| 1122 | struct perf_event_context *ctx; |
| 1123 | |
| 1124 | again: |
| 1125 | rcu_read_lock(); |
| 1126 | ctx = ACCESS_ONCE(event->ctx); |
| 1127 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
| 1128 | rcu_read_unlock(); |
| 1129 | goto again; |
| 1130 | } |
| 1131 | rcu_read_unlock(); |
| 1132 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1133 | mutex_lock_nested(&ctx->mutex, nesting); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1134 | if (event->ctx != ctx) { |
| 1135 | mutex_unlock(&ctx->mutex); |
| 1136 | put_ctx(ctx); |
| 1137 | goto again; |
| 1138 | } |
| 1139 | |
| 1140 | return ctx; |
| 1141 | } |
| 1142 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1143 | static inline struct perf_event_context * |
| 1144 | perf_event_ctx_lock(struct perf_event *event) |
| 1145 | { |
| 1146 | return perf_event_ctx_lock_nested(event, 0); |
| 1147 | } |
| 1148 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1149 | static void perf_event_ctx_unlock(struct perf_event *event, |
| 1150 | struct perf_event_context *ctx) |
| 1151 | { |
| 1152 | mutex_unlock(&ctx->mutex); |
| 1153 | put_ctx(ctx); |
| 1154 | } |
| 1155 | |
| 1156 | /* |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1157 | * This must be done under the ctx->lock, such as to serialize against |
| 1158 | * context_equiv(), therefore we cannot call put_ctx() since that might end up |
| 1159 | * calling scheduler related locks and ctx->lock nests inside those. |
| 1160 | */ |
| 1161 | static __must_check struct perf_event_context * |
| 1162 | unclone_ctx(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1163 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1164 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
| 1165 | |
| 1166 | lockdep_assert_held(&ctx->lock); |
| 1167 | |
| 1168 | if (parent_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1169 | ctx->parent_ctx = NULL; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1170 | ctx->generation++; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1171 | |
| 1172 | return parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1175 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 1176 | { |
| 1177 | /* |
| 1178 | * only top level events have the pid namespace they were created in |
| 1179 | */ |
| 1180 | if (event->parent) |
| 1181 | event = event->parent; |
| 1182 | |
| 1183 | return task_tgid_nr_ns(p, event->ns); |
| 1184 | } |
| 1185 | |
| 1186 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 1187 | { |
| 1188 | /* |
| 1189 | * only top level events have the pid namespace they were created in |
| 1190 | */ |
| 1191 | if (event->parent) |
| 1192 | event = event->parent; |
| 1193 | |
| 1194 | return task_pid_nr_ns(p, event->ns); |
| 1195 | } |
| 1196 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1197 | /* |
| 1198 | * If we inherit events we want to return the parent event id |
| 1199 | * to userspace. |
| 1200 | */ |
| 1201 | static u64 primary_event_id(struct perf_event *event) |
| 1202 | { |
| 1203 | u64 id = event->id; |
| 1204 | |
| 1205 | if (event->parent) |
| 1206 | id = event->parent->id; |
| 1207 | |
| 1208 | return id; |
| 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | * Get the perf_event_context for a task and lock it. |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1213 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1214 | * This has to cope with with the fact that until it is locked, |
| 1215 | * the context could get moved to another task. |
| 1216 | */ |
| 1217 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1218 | 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] | 1219 | { |
| 1220 | struct perf_event_context *ctx; |
| 1221 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1222 | retry: |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1223 | /* |
| 1224 | * One of the few rules of preemptible RCU is that one cannot do |
| 1225 | * rcu_read_unlock() while holding a scheduler (or nested) lock when |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1226 | * part of the read side critical section was irqs-enabled -- see |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1227 | * rcu_read_unlock_special(). |
| 1228 | * |
| 1229 | * Since ctx->lock nests under rq->lock we must ensure the entire read |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1230 | * side critical section has interrupts disabled. |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1231 | */ |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1232 | local_irq_save(*flags); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1233 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1234 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1235 | if (ctx) { |
| 1236 | /* |
| 1237 | * If this context is a clone of another, it might |
| 1238 | * get swapped for another underneath us by |
| 1239 | * perf_event_task_sched_out, though the |
| 1240 | * rcu_read_lock() protects us from any context |
| 1241 | * getting freed. Lock the context and check if it |
| 1242 | * got swapped before we could get the lock, and retry |
| 1243 | * if so. If we locked the right context, then it |
| 1244 | * can't get swapped on us any more. |
| 1245 | */ |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1246 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1247 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1248 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1249 | rcu_read_unlock(); |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1250 | local_irq_restore(*flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1251 | goto retry; |
| 1252 | } |
| 1253 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1254 | if (ctx->task == TASK_TOMBSTONE || |
| 1255 | !atomic_inc_not_zero(&ctx->refcount)) { |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1256 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1257 | ctx = NULL; |
Peter Zijlstra | 828b6f0 | 2016-01-27 21:59:04 +0100 | [diff] [blame] | 1258 | } else { |
| 1259 | WARN_ON_ONCE(ctx->task != task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | rcu_read_unlock(); |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1263 | if (!ctx) |
| 1264 | local_irq_restore(*flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1265 | return ctx; |
| 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | * Get the context for a task and increment its pin_count so it |
| 1270 | * can't get swapped to another task. This also increments its |
| 1271 | * reference count so that the context can't get freed. |
| 1272 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1273 | static struct perf_event_context * |
| 1274 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1275 | { |
| 1276 | struct perf_event_context *ctx; |
| 1277 | unsigned long flags; |
| 1278 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1279 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1280 | if (ctx) { |
| 1281 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1282 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1283 | } |
| 1284 | return ctx; |
| 1285 | } |
| 1286 | |
| 1287 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 1288 | { |
| 1289 | unsigned long flags; |
| 1290 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1291 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1292 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1293 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1294 | } |
| 1295 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1296 | /* |
| 1297 | * Update the record of the current time in a context. |
| 1298 | */ |
| 1299 | static void update_context_time(struct perf_event_context *ctx) |
| 1300 | { |
| 1301 | u64 now = perf_clock(); |
| 1302 | |
| 1303 | ctx->time += now - ctx->timestamp; |
| 1304 | ctx->timestamp = now; |
| 1305 | } |
| 1306 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1307 | static u64 perf_event_time(struct perf_event *event) |
| 1308 | { |
| 1309 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1310 | |
| 1311 | if (is_cgroup_event(event)) |
| 1312 | return perf_cgroup_event_time(event); |
| 1313 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1314 | return ctx ? ctx->time : 0; |
| 1315 | } |
| 1316 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1317 | /* |
| 1318 | * Update the total_time_enabled and total_time_running fields for a event. |
| 1319 | */ |
| 1320 | static void update_event_times(struct perf_event *event) |
| 1321 | { |
| 1322 | struct perf_event_context *ctx = event->ctx; |
| 1323 | u64 run_end; |
| 1324 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 1325 | lockdep_assert_held(&ctx->lock); |
| 1326 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1327 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1328 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1329 | return; |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 1330 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1331 | /* |
| 1332 | * in cgroup mode, time_enabled represents |
| 1333 | * the time the event was enabled AND active |
| 1334 | * tasks were in the monitored cgroup. This is |
| 1335 | * independent of the activity of the context as |
| 1336 | * there may be a mix of cgroup and non-cgroup events. |
| 1337 | * |
| 1338 | * That is why we treat cgroup events differently |
| 1339 | * here. |
| 1340 | */ |
| 1341 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1342 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1343 | else if (ctx->is_active) |
| 1344 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1345 | else |
| 1346 | run_end = event->tstamp_stopped; |
| 1347 | |
| 1348 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1349 | |
| 1350 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1351 | run_end = event->tstamp_stopped; |
| 1352 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1353 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1354 | |
| 1355 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1356 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1359 | /* |
| 1360 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1361 | */ |
| 1362 | static void update_group_times(struct perf_event *leader) |
| 1363 | { |
| 1364 | struct perf_event *event; |
| 1365 | |
| 1366 | update_event_times(leader); |
| 1367 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1368 | update_event_times(event); |
| 1369 | } |
| 1370 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1371 | static struct list_head * |
| 1372 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1373 | { |
| 1374 | if (event->attr.pinned) |
| 1375 | return &ctx->pinned_groups; |
| 1376 | else |
| 1377 | return &ctx->flexible_groups; |
| 1378 | } |
| 1379 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1380 | /* |
| 1381 | * Add a event from the lists for its context. |
| 1382 | * Must be called with ctx->mutex and ctx->lock held. |
| 1383 | */ |
| 1384 | static void |
| 1385 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1386 | { |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 1387 | lockdep_assert_held(&ctx->lock); |
| 1388 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1389 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1390 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1391 | |
| 1392 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1393 | * If we're a stand alone event or group leader, we go to the context |
| 1394 | * list, group events are kept attached to the group so that |
| 1395 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1396 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1397 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1398 | struct list_head *list; |
| 1399 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1400 | if (is_software_event(event)) |
| 1401 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 1402 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1403 | list = ctx_group_list(event, ctx); |
| 1404 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1405 | } |
| 1406 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 1407 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1408 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1409 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1410 | list_add_rcu(&event->event_entry, &ctx->event_list); |
| 1411 | ctx->nr_events++; |
| 1412 | if (event->attr.inherit_stat) |
| 1413 | ctx->nr_stat++; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1414 | |
| 1415 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1416 | } |
| 1417 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1418 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1419 | * Initialize event state based on the perf_event_attr::disabled. |
| 1420 | */ |
| 1421 | static inline void perf_event__state_init(struct perf_event *event) |
| 1422 | { |
| 1423 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1424 | PERF_EVENT_STATE_INACTIVE; |
| 1425 | } |
| 1426 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1427 | static void __perf_event_read_size(struct perf_event *event, int nr_siblings) |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1428 | { |
| 1429 | int entry = sizeof(u64); /* value */ |
| 1430 | int size = 0; |
| 1431 | int nr = 1; |
| 1432 | |
| 1433 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1434 | size += sizeof(u64); |
| 1435 | |
| 1436 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1437 | size += sizeof(u64); |
| 1438 | |
| 1439 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1440 | entry += sizeof(u64); |
| 1441 | |
| 1442 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1443 | nr += nr_siblings; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1444 | size += sizeof(u64); |
| 1445 | } |
| 1446 | |
| 1447 | size += entry * nr; |
| 1448 | event->read_size = size; |
| 1449 | } |
| 1450 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1451 | static void __perf_event_header_size(struct perf_event *event, u64 sample_type) |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1452 | { |
| 1453 | struct perf_sample_data *data; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1454 | u16 size = 0; |
| 1455 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1456 | if (sample_type & PERF_SAMPLE_IP) |
| 1457 | size += sizeof(data->ip); |
| 1458 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1459 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1460 | size += sizeof(data->addr); |
| 1461 | |
| 1462 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1463 | size += sizeof(data->period); |
| 1464 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1465 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1466 | size += sizeof(data->weight); |
| 1467 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1468 | if (sample_type & PERF_SAMPLE_READ) |
| 1469 | size += event->read_size; |
| 1470 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1471 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1472 | size += sizeof(data->data_src.val); |
| 1473 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 1474 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1475 | size += sizeof(data->txn); |
| 1476 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1477 | event->header_size = size; |
| 1478 | } |
| 1479 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1480 | /* |
| 1481 | * Called at perf_event creation and when events are attached/detached from a |
| 1482 | * group. |
| 1483 | */ |
| 1484 | static void perf_event__header_size(struct perf_event *event) |
| 1485 | { |
| 1486 | __perf_event_read_size(event, |
| 1487 | event->group_leader->nr_siblings); |
| 1488 | __perf_event_header_size(event, event->attr.sample_type); |
| 1489 | } |
| 1490 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1491 | static void perf_event__id_header_size(struct perf_event *event) |
| 1492 | { |
| 1493 | struct perf_sample_data *data; |
| 1494 | u64 sample_type = event->attr.sample_type; |
| 1495 | u16 size = 0; |
| 1496 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1497 | if (sample_type & PERF_SAMPLE_TID) |
| 1498 | size += sizeof(data->tid_entry); |
| 1499 | |
| 1500 | if (sample_type & PERF_SAMPLE_TIME) |
| 1501 | size += sizeof(data->time); |
| 1502 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 1503 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1504 | size += sizeof(data->id); |
| 1505 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1506 | if (sample_type & PERF_SAMPLE_ID) |
| 1507 | size += sizeof(data->id); |
| 1508 | |
| 1509 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1510 | size += sizeof(data->stream_id); |
| 1511 | |
| 1512 | if (sample_type & PERF_SAMPLE_CPU) |
| 1513 | size += sizeof(data->cpu_entry); |
| 1514 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1515 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1516 | } |
| 1517 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1518 | static bool perf_event_validate_size(struct perf_event *event) |
| 1519 | { |
| 1520 | /* |
| 1521 | * The values computed here will be over-written when we actually |
| 1522 | * attach the event. |
| 1523 | */ |
| 1524 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); |
| 1525 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); |
| 1526 | perf_event__id_header_size(event); |
| 1527 | |
| 1528 | /* |
| 1529 | * Sum the lot; should not exceed the 64k limit we have on records. |
| 1530 | * Conservative limit to allow for callchains and other variable fields. |
| 1531 | */ |
| 1532 | if (event->read_size + event->header_size + |
| 1533 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) |
| 1534 | return false; |
| 1535 | |
| 1536 | return true; |
| 1537 | } |
| 1538 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1539 | static void perf_group_attach(struct perf_event *event) |
| 1540 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1541 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1542 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1543 | /* |
| 1544 | * We can have double attach due to group movement in perf_event_open. |
| 1545 | */ |
| 1546 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1547 | return; |
| 1548 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1549 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1550 | |
| 1551 | if (group_leader == event) |
| 1552 | return; |
| 1553 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1554 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
| 1555 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1556 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 1557 | !is_software_event(event)) |
| 1558 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 1559 | |
| 1560 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1561 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1562 | |
| 1563 | perf_event__header_size(group_leader); |
| 1564 | |
| 1565 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1566 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1567 | } |
| 1568 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1569 | /* |
| 1570 | * Remove a event from the lists for its context. |
| 1571 | * Must be called with ctx->mutex and ctx->lock held. |
| 1572 | */ |
| 1573 | static void |
| 1574 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1575 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1576 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1577 | |
| 1578 | WARN_ON_ONCE(event->ctx != ctx); |
| 1579 | lockdep_assert_held(&ctx->lock); |
| 1580 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1581 | /* |
| 1582 | * We can have double detach due to exit/hot-unplug + close. |
| 1583 | */ |
| 1584 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1585 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1586 | |
| 1587 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1588 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1589 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1590 | ctx->nr_cgroups--; |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 1591 | /* |
| 1592 | * Because cgroup events are always per-cpu events, this will |
| 1593 | * always be called from the right CPU. |
| 1594 | */ |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1595 | cpuctx = __get_cpu_context(ctx); |
| 1596 | /* |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 1597 | * If there are no more cgroup events then clear cgrp to avoid |
| 1598 | * stale pointer in update_cgrp_time_from_cpuctx(). |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1599 | */ |
| 1600 | if (!ctx->nr_cgroups) |
| 1601 | cpuctx->cgrp = NULL; |
| 1602 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1603 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1604 | ctx->nr_events--; |
| 1605 | if (event->attr.inherit_stat) |
| 1606 | ctx->nr_stat--; |
| 1607 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1608 | list_del_rcu(&event->event_entry); |
| 1609 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1610 | if (event->group_leader == event) |
| 1611 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1612 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1613 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1614 | |
| 1615 | /* |
| 1616 | * If event was in error state, then keep it |
| 1617 | * that way, otherwise bogus counts will be |
| 1618 | * returned on read(). The only way to get out |
| 1619 | * of error state is by explicit re-enabling |
| 1620 | * of the event |
| 1621 | */ |
| 1622 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1623 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1624 | |
| 1625 | ctx->generation++; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1626 | } |
| 1627 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1628 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1629 | { |
| 1630 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1631 | struct list_head *list = NULL; |
| 1632 | |
| 1633 | /* |
| 1634 | * We can have double detach due to exit/hot-unplug + close. |
| 1635 | */ |
| 1636 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1637 | return; |
| 1638 | |
| 1639 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1640 | |
| 1641 | /* |
| 1642 | * If this is a sibling, remove it from its group. |
| 1643 | */ |
| 1644 | if (event->group_leader != event) { |
| 1645 | list_del_init(&event->group_entry); |
| 1646 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1647 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | if (!list_empty(&event->group_entry)) |
| 1651 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1652 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1653 | /* |
| 1654 | * If this was a group event with sibling events then |
| 1655 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1656 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1657 | */ |
| 1658 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1659 | if (list) |
| 1660 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1661 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1662 | |
| 1663 | /* Inherit group flags from the previous leader */ |
| 1664 | sibling->group_flags = event->group_flags; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1665 | |
| 1666 | WARN_ON_ONCE(sibling->ctx != event->ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1667 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1668 | |
| 1669 | out: |
| 1670 | perf_event__header_size(event->group_leader); |
| 1671 | |
| 1672 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1673 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1674 | } |
| 1675 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1676 | static bool is_orphaned_event(struct perf_event *event) |
| 1677 | { |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 1678 | return event->state == PERF_EVENT_STATE_DEAD; |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1679 | } |
| 1680 | |
Mark Rutland | 66eb579 | 2015-05-13 17:12:23 +0100 | [diff] [blame] | 1681 | static inline int pmu_filter_match(struct perf_event *event) |
| 1682 | { |
| 1683 | struct pmu *pmu = event->pmu; |
| 1684 | return pmu->filter_match ? pmu->filter_match(event) : 1; |
| 1685 | } |
| 1686 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1687 | static inline int |
| 1688 | event_filter_match(struct perf_event *event) |
| 1689 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1690 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
Mark Rutland | 66eb579 | 2015-05-13 17:12:23 +0100 | [diff] [blame] | 1691 | && perf_cgroup_match(event) && pmu_filter_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1692 | } |
| 1693 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1694 | static void |
| 1695 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1696 | struct perf_cpu_context *cpuctx, |
| 1697 | struct perf_event_context *ctx) |
| 1698 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1699 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1700 | u64 delta; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1701 | |
| 1702 | WARN_ON_ONCE(event->ctx != ctx); |
| 1703 | lockdep_assert_held(&ctx->lock); |
| 1704 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1705 | /* |
| 1706 | * An event which could not be activated because of |
| 1707 | * filter mismatch still needs to have its timings |
| 1708 | * maintained, otherwise bogus information is return |
| 1709 | * via read() for time_enabled, time_running: |
| 1710 | */ |
| 1711 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1712 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1713 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1714 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1715 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1718 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1719 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1720 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1721 | perf_pmu_disable(event->pmu); |
| 1722 | |
Peter Zijlstra | 28a967c | 2016-02-24 18:45:46 +0100 | [diff] [blame] | 1723 | event->tstamp_stopped = tstamp; |
| 1724 | event->pmu->del(event, 0); |
| 1725 | event->oncpu = -1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1726 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1727 | if (event->pending_disable) { |
| 1728 | event->pending_disable = 0; |
| 1729 | event->state = PERF_EVENT_STATE_OFF; |
| 1730 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1731 | |
| 1732 | if (!is_software_event(event)) |
| 1733 | cpuctx->active_oncpu--; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1734 | if (!--ctx->nr_active) |
| 1735 | perf_event_ctx_deactivate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1736 | if (event->attr.freq && event->attr.sample_freq) |
| 1737 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1738 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1739 | cpuctx->exclusive = 0; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1740 | |
| 1741 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | static void |
| 1745 | group_sched_out(struct perf_event *group_event, |
| 1746 | struct perf_cpu_context *cpuctx, |
| 1747 | struct perf_event_context *ctx) |
| 1748 | { |
| 1749 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1750 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1751 | |
| 1752 | event_sched_out(group_event, cpuctx, ctx); |
| 1753 | |
| 1754 | /* |
| 1755 | * Schedule out siblings (if any): |
| 1756 | */ |
| 1757 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1758 | event_sched_out(event, cpuctx, ctx); |
| 1759 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1760 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1761 | cpuctx->exclusive = 0; |
| 1762 | } |
| 1763 | |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1764 | #define DETACH_GROUP 0x01UL |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 1765 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1766 | /* |
| 1767 | * Cross CPU call to remove a performance event |
| 1768 | * |
| 1769 | * We disable the event on the hardware level first. After that we |
| 1770 | * remove it from the context list. |
| 1771 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1772 | static void |
| 1773 | __perf_remove_from_context(struct perf_event *event, |
| 1774 | struct perf_cpu_context *cpuctx, |
| 1775 | struct perf_event_context *ctx, |
| 1776 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1777 | { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1778 | unsigned long flags = (unsigned long)info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1779 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1780 | event_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1781 | if (flags & DETACH_GROUP) |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1782 | perf_group_detach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1783 | list_del_event(event, ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1784 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 1785 | if (!ctx->nr_events && ctx->is_active) { |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1786 | ctx->is_active = 0; |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 1787 | if (ctx->task) { |
| 1788 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 1789 | cpuctx->task_ctx = NULL; |
| 1790 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1791 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1792 | } |
| 1793 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1794 | /* |
| 1795 | * Remove the event from a task's (or a CPU's) list of events. |
| 1796 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1797 | * If event->ctx is a cloned context, callers must make sure that |
| 1798 | * every task struct that event->ctx->task could possibly point to |
| 1799 | * remains valid. This is OK when called from perf_release since |
| 1800 | * that only calls us on the top-level context, which can't be a clone. |
| 1801 | * When called from perf_event_exit_task, it's OK because the |
| 1802 | * context has been detached from its task. |
| 1803 | */ |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1804 | static void perf_remove_from_context(struct perf_event *event, unsigned long flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1805 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1806 | lockdep_assert_held(&event->ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1807 | |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1808 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1809 | } |
| 1810 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1811 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1812 | * Cross CPU call to disable a performance event |
| 1813 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1814 | static void __perf_event_disable(struct perf_event *event, |
| 1815 | struct perf_cpu_context *cpuctx, |
| 1816 | struct perf_event_context *ctx, |
| 1817 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1818 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1819 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 1820 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1821 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1822 | update_context_time(ctx); |
| 1823 | update_cgrp_time_from_event(event); |
| 1824 | update_group_times(event); |
| 1825 | if (event == event->group_leader) |
| 1826 | group_sched_out(event, cpuctx, ctx); |
| 1827 | else |
| 1828 | event_sched_out(event, cpuctx, ctx); |
| 1829 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1830 | } |
| 1831 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1832 | /* |
| 1833 | * Disable a event. |
| 1834 | * |
| 1835 | * If event->ctx is a cloned context, callers must make sure that |
| 1836 | * every task struct that event->ctx->task could possibly point to |
| 1837 | * remains valid. This condition is satisifed when called through |
| 1838 | * perf_event_for_each_child or perf_event_for_each because they |
| 1839 | * hold the top-level event's child_mutex, so any descendant that |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 1840 | * goes to exit will block in perf_event_exit_event(). |
| 1841 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1842 | * When called from perf_pending_event it's OK because event->ctx |
| 1843 | * is the current context on this CPU and preemption is disabled, |
| 1844 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1845 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1846 | static void _perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1847 | { |
| 1848 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1849 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1850 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1851 | if (event->state <= PERF_EVENT_STATE_OFF) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1852 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1853 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1854 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1855 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1856 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1857 | event_function_call(event, __perf_event_disable, NULL); |
| 1858 | } |
| 1859 | |
| 1860 | void perf_event_disable_local(struct perf_event *event) |
| 1861 | { |
| 1862 | event_function_local(event, __perf_event_disable, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1863 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1864 | |
| 1865 | /* |
| 1866 | * Strictly speaking kernel users cannot create groups and therefore this |
| 1867 | * interface does not need the perf_event_ctx_lock() magic. |
| 1868 | */ |
| 1869 | void perf_event_disable(struct perf_event *event) |
| 1870 | { |
| 1871 | struct perf_event_context *ctx; |
| 1872 | |
| 1873 | ctx = perf_event_ctx_lock(event); |
| 1874 | _perf_event_disable(event); |
| 1875 | perf_event_ctx_unlock(event, ctx); |
| 1876 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 1877 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1878 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1879 | static void perf_set_shadow_time(struct perf_event *event, |
| 1880 | struct perf_event_context *ctx, |
| 1881 | u64 tstamp) |
| 1882 | { |
| 1883 | /* |
| 1884 | * use the correct time source for the time snapshot |
| 1885 | * |
| 1886 | * We could get by without this by leveraging the |
| 1887 | * fact that to get to this function, the caller |
| 1888 | * has most likely already called update_context_time() |
| 1889 | * and update_cgrp_time_xx() and thus both timestamp |
| 1890 | * are identical (or very close). Given that tstamp is, |
| 1891 | * already adjusted for cgroup, we could say that: |
| 1892 | * tstamp - ctx->timestamp |
| 1893 | * is equivalent to |
| 1894 | * tstamp - cgrp->timestamp. |
| 1895 | * |
| 1896 | * Then, in perf_output_read(), the calculation would |
| 1897 | * work with no changes because: |
| 1898 | * - event is guaranteed scheduled in |
| 1899 | * - no scheduled out in between |
| 1900 | * - thus the timestamp would be the same |
| 1901 | * |
| 1902 | * But this is a bit hairy. |
| 1903 | * |
| 1904 | * So instead, we have an explicit cgroup call to remain |
| 1905 | * within the time time source all along. We believe it |
| 1906 | * is cleaner and simpler to understand. |
| 1907 | */ |
| 1908 | if (is_cgroup_event(event)) |
| 1909 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1910 | else |
| 1911 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1912 | } |
| 1913 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1914 | #define MAX_INTERRUPTS (~0ULL) |
| 1915 | |
| 1916 | static void perf_log_throttle(struct perf_event *event, int enable); |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 1917 | static void perf_log_itrace_start(struct perf_event *event); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1918 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1919 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1920 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1921 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1922 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1923 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1924 | u64 tstamp = perf_event_time(event); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1925 | int ret = 0; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1926 | |
Peter Zijlstra | 6334241 | 2014-05-05 11:49:16 +0200 | [diff] [blame] | 1927 | lockdep_assert_held(&ctx->lock); |
| 1928 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1929 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1930 | return 0; |
| 1931 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 1932 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
| 1933 | /* |
| 1934 | * Order event::oncpu write to happen before the ACTIVE state |
| 1935 | * is visible. |
| 1936 | */ |
| 1937 | smp_wmb(); |
| 1938 | WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1939 | |
| 1940 | /* |
| 1941 | * Unthrottle events, since we scheduled we might have missed several |
| 1942 | * ticks already, also for a heavily scheduling task there is little |
| 1943 | * guarantee it'll get a tick in a timely manner. |
| 1944 | */ |
| 1945 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1946 | perf_log_throttle(event, 1); |
| 1947 | event->hw.interrupts = 0; |
| 1948 | } |
| 1949 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1950 | /* |
| 1951 | * The new state must be visible before we turn it on in the hardware: |
| 1952 | */ |
| 1953 | smp_wmb(); |
| 1954 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1955 | perf_pmu_disable(event->pmu); |
| 1956 | |
Shaohua Li | 72f669c | 2015-02-05 15:55:31 -0800 | [diff] [blame] | 1957 | perf_set_shadow_time(event, ctx, tstamp); |
| 1958 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 1959 | perf_log_itrace_start(event); |
| 1960 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1961 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1962 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1963 | event->oncpu = -1; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1964 | ret = -EAGAIN; |
| 1965 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1966 | } |
| 1967 | |
Peter Zijlstra | 00a2916 | 2015-07-27 10:35:07 +0200 | [diff] [blame] | 1968 | event->tstamp_running += tstamp - event->tstamp_stopped; |
| 1969 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1970 | if (!is_software_event(event)) |
| 1971 | cpuctx->active_oncpu++; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1972 | if (!ctx->nr_active++) |
| 1973 | perf_event_ctx_activate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1974 | if (event->attr.freq && event->attr.sample_freq) |
| 1975 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1976 | |
| 1977 | if (event->attr.exclusive) |
| 1978 | cpuctx->exclusive = 1; |
| 1979 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1980 | out: |
| 1981 | perf_pmu_enable(event->pmu); |
| 1982 | |
| 1983 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | static int |
| 1987 | group_sched_in(struct perf_event *group_event, |
| 1988 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1989 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1990 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1991 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 4a23459 | 2014-02-24 12:43:31 +0100 | [diff] [blame] | 1992 | struct pmu *pmu = ctx->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1993 | u64 now = ctx->time; |
| 1994 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1995 | |
| 1996 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1997 | return 0; |
| 1998 | |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 1999 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 2000 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2001 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 2002 | pmu->cancel_txn(pmu); |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 2003 | perf_mux_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2004 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 2005 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2006 | |
| 2007 | /* |
| 2008 | * Schedule in siblings as one group (if any): |
| 2009 | */ |
| 2010 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2011 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2012 | partial_group = event; |
| 2013 | goto group_error; |
| 2014 | } |
| 2015 | } |
| 2016 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2017 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 2018 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2019 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2020 | group_error: |
| 2021 | /* |
| 2022 | * Groups can be scheduled in as one unit only, so undo any |
| 2023 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2024 | * The events up to the failed event are scheduled out normally, |
| 2025 | * tstamp_stopped will be updated. |
| 2026 | * |
| 2027 | * The failed events and the remaining siblings need to have |
| 2028 | * their timings updated as if they had gone thru event_sched_in() |
| 2029 | * and event_sched_out(). This is required to get consistent timings |
| 2030 | * across the group. This also takes care of the case where the group |
| 2031 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 2032 | * the time the event was actually stopped, such that time delta |
| 2033 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2034 | */ |
| 2035 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 2036 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2037 | simulate = true; |
| 2038 | |
| 2039 | if (simulate) { |
| 2040 | event->tstamp_running += now - event->tstamp_stopped; |
| 2041 | event->tstamp_stopped = now; |
| 2042 | } else { |
| 2043 | event_sched_out(event, cpuctx, ctx); |
| 2044 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2045 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2046 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2047 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 2048 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 2049 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 2050 | perf_mux_hrtimer_restart(cpuctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2051 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2052 | return -EAGAIN; |
| 2053 | } |
| 2054 | |
| 2055 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2056 | * Work out whether we can put this event group on the CPU now. |
| 2057 | */ |
| 2058 | static int group_can_go_on(struct perf_event *event, |
| 2059 | struct perf_cpu_context *cpuctx, |
| 2060 | int can_add_hw) |
| 2061 | { |
| 2062 | /* |
| 2063 | * Groups consisting entirely of software events can always go on. |
| 2064 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 2065 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2066 | return 1; |
| 2067 | /* |
| 2068 | * If an exclusive group is already on, no other hardware |
| 2069 | * events can go on. |
| 2070 | */ |
| 2071 | if (cpuctx->exclusive) |
| 2072 | return 0; |
| 2073 | /* |
| 2074 | * If this group is exclusive and there are already |
| 2075 | * events on the CPU, it can't go on. |
| 2076 | */ |
| 2077 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 2078 | return 0; |
| 2079 | /* |
| 2080 | * Otherwise, try to add it if all previous groups were able |
| 2081 | * to go on. |
| 2082 | */ |
| 2083 | return can_add_hw; |
| 2084 | } |
| 2085 | |
| 2086 | static void add_event_to_ctx(struct perf_event *event, |
| 2087 | struct perf_event_context *ctx) |
| 2088 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2089 | u64 tstamp = perf_event_time(event); |
| 2090 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2091 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 2092 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2093 | event->tstamp_enabled = tstamp; |
| 2094 | event->tstamp_running = tstamp; |
| 2095 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2096 | } |
| 2097 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2098 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2099 | struct perf_cpu_context *cpuctx, |
| 2100 | enum event_type_t event_type); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2101 | static void |
| 2102 | ctx_sched_in(struct perf_event_context *ctx, |
| 2103 | struct perf_cpu_context *cpuctx, |
| 2104 | enum event_type_t event_type, |
| 2105 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2106 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2107 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2108 | struct perf_event_context *ctx) |
| 2109 | { |
| 2110 | if (!cpuctx->task_ctx) |
| 2111 | return; |
| 2112 | |
| 2113 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2114 | return; |
| 2115 | |
| 2116 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
| 2117 | } |
| 2118 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 2119 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 2120 | struct perf_event_context *ctx, |
| 2121 | struct task_struct *task) |
| 2122 | { |
| 2123 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 2124 | if (ctx) |
| 2125 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 2126 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 2127 | if (ctx) |
| 2128 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 2129 | } |
| 2130 | |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 2131 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
| 2132 | struct perf_event_context *task_ctx) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 2133 | { |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 2134 | perf_pmu_disable(cpuctx->ctx.pmu); |
| 2135 | if (task_ctx) |
| 2136 | task_ctx_sched_out(cpuctx, task_ctx); |
| 2137 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 2138 | perf_event_sched_in(cpuctx, task_ctx, current); |
| 2139 | perf_pmu_enable(cpuctx->ctx.pmu); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2142 | /* |
| 2143 | * Cross CPU call to install and enable a performance event |
| 2144 | * |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2145 | * Very similar to remote_function() + event_function() but cannot assume that |
| 2146 | * things like ctx->is_active and cpuctx->task_ctx are set. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2147 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2148 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2149 | { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2150 | struct perf_event *event = info; |
| 2151 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2152 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2153 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2154 | bool activate = true; |
| 2155 | int ret = 0; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2156 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2157 | raw_spin_lock(&cpuctx->ctx.lock); |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2158 | if (ctx->task) { |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2159 | raw_spin_lock(&ctx->lock); |
| 2160 | task_ctx = ctx; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2161 | |
| 2162 | /* If we're on the wrong CPU, try again */ |
| 2163 | if (task_cpu(ctx->task) != smp_processor_id()) { |
| 2164 | ret = -ESRCH; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2165 | goto unlock; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2166 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2167 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2168 | /* |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2169 | * If we're on the right CPU, see if the task we target is |
| 2170 | * current, if not we don't have to activate the ctx, a future |
| 2171 | * context switch will do that for us. |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2172 | */ |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2173 | if (ctx->task != current) |
| 2174 | activate = false; |
| 2175 | else |
| 2176 | WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
| 2177 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2178 | } else if (task_ctx) { |
| 2179 | raw_spin_lock(&task_ctx->lock); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2180 | } |
| 2181 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2182 | if (activate) { |
| 2183 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
| 2184 | add_event_to_ctx(event, ctx); |
| 2185 | ctx_resched(cpuctx, task_ctx); |
| 2186 | } else { |
| 2187 | add_event_to_ctx(event, ctx); |
| 2188 | } |
| 2189 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2190 | unlock: |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2191 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2192 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2193 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | /* |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2197 | * Attach a performance event to a context. |
| 2198 | * |
| 2199 | * Very similar to event_function_call, see comment there. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2200 | */ |
| 2201 | static void |
| 2202 | perf_install_in_context(struct perf_event_context *ctx, |
| 2203 | struct perf_event *event, |
| 2204 | int cpu) |
| 2205 | { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2206 | struct task_struct *task = READ_ONCE(ctx->task); |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2207 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2208 | lockdep_assert_held(&ctx->mutex); |
| 2209 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2210 | event->ctx = ctx; |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 2211 | if (event->cpu != -1) |
| 2212 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2213 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2214 | if (!task) { |
| 2215 | cpu_function_call(cpu, __perf_install_in_context, event); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2216 | return; |
| 2217 | } |
Peter Zijlstra | 6f932e5 | 2016-02-24 18:45:43 +0100 | [diff] [blame] | 2218 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2219 | /* |
| 2220 | * Should not happen, we validate the ctx is still alive before calling. |
| 2221 | */ |
| 2222 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) |
| 2223 | return; |
| 2224 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2225 | /* |
| 2226 | * Installing events is tricky because we cannot rely on ctx->is_active |
| 2227 | * to be set in case this is the nr_events 0 -> 1 transition. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2228 | */ |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2229 | again: |
| 2230 | /* |
| 2231 | * Cannot use task_function_call() because we need to run on the task's |
| 2232 | * CPU regardless of whether its current or not. |
| 2233 | */ |
| 2234 | if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event)) |
| 2235 | return; |
| 2236 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2237 | raw_spin_lock_irq(&ctx->lock); |
| 2238 | task = ctx->task; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2239 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
| 2240 | /* |
| 2241 | * Cannot happen because we already checked above (which also |
| 2242 | * cannot happen), and we hold ctx->mutex, which serializes us |
| 2243 | * against perf_event_exit_task_context(). |
| 2244 | */ |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2245 | raw_spin_unlock_irq(&ctx->lock); |
| 2246 | return; |
| 2247 | } |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2248 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2249 | /* |
| 2250 | * Since !ctx->is_active doesn't mean anything, we must IPI |
| 2251 | * unconditionally. |
| 2252 | */ |
| 2253 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | /* |
| 2257 | * Put a event into inactive state and update time fields. |
| 2258 | * Enabling the leader of a group effectively enables all |
| 2259 | * the group members that aren't explicitly disabled, so we |
| 2260 | * have to update their ->tstamp_enabled also. |
| 2261 | * Note: this works for group members as well as group leaders |
| 2262 | * since the non-leader members' sibling_lists will be empty. |
| 2263 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2264 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2265 | { |
| 2266 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2267 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2268 | |
| 2269 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2270 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2271 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2272 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 2273 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2274 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | /* |
| 2278 | * Cross CPU call to enable a performance event |
| 2279 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2280 | static void __perf_event_enable(struct perf_event *event, |
| 2281 | struct perf_cpu_context *cpuctx, |
| 2282 | struct perf_event_context *ctx, |
| 2283 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2284 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2285 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2286 | struct perf_event_context *task_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2287 | |
Peter Zijlstra | 6e801e01 | 2016-01-26 12:17:08 +0100 | [diff] [blame] | 2288 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
| 2289 | event->state <= PERF_EVENT_STATE_ERROR) |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2290 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2291 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2292 | if (ctx->is_active) |
| 2293 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
| 2294 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2295 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2296 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2297 | if (!ctx->is_active) |
| 2298 | return; |
| 2299 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2300 | if (!event_filter_match(event)) { |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2301 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2302 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2303 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2304 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2305 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2306 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2307 | /* |
| 2308 | * If the event is in a group and isn't the group leader, |
| 2309 | * then don't put it on unless the group is on. |
| 2310 | */ |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2311 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
| 2312 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2313 | return; |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2314 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2315 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2316 | task_ctx = cpuctx->task_ctx; |
| 2317 | if (ctx->task) |
| 2318 | WARN_ON_ONCE(task_ctx != ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2319 | |
Peter Zijlstra | aee7dbc | 2016-01-08 10:45:11 +0100 | [diff] [blame] | 2320 | ctx_resched(cpuctx, task_ctx); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2321 | } |
| 2322 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2323 | /* |
| 2324 | * Enable a event. |
| 2325 | * |
| 2326 | * If event->ctx is a cloned context, callers must make sure that |
| 2327 | * every task struct that event->ctx->task could possibly point to |
| 2328 | * remains valid. This condition is satisfied when called through |
| 2329 | * perf_event_for_each_child or perf_event_for_each as described |
| 2330 | * for perf_event_disable. |
| 2331 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2332 | static void _perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2333 | { |
| 2334 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2335 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2336 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 6e801e01 | 2016-01-26 12:17:08 +0100 | [diff] [blame] | 2337 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
| 2338 | event->state < PERF_EVENT_STATE_ERROR) { |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2339 | raw_spin_unlock_irq(&ctx->lock); |
| 2340 | return; |
| 2341 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2342 | |
| 2343 | /* |
| 2344 | * If the event is in error state, clear that first. |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2345 | * |
| 2346 | * That way, if we see the event in error state below, we know that it |
| 2347 | * has gone back into error state, as distinct from the task having |
| 2348 | * been scheduled away before the cross-call arrived. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2349 | */ |
| 2350 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2351 | event->state = PERF_EVENT_STATE_OFF; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2352 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2353 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2354 | event_function_call(event, __perf_event_enable, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2355 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2356 | |
| 2357 | /* |
| 2358 | * See perf_event_disable(); |
| 2359 | */ |
| 2360 | void perf_event_enable(struct perf_event *event) |
| 2361 | { |
| 2362 | struct perf_event_context *ctx; |
| 2363 | |
| 2364 | ctx = perf_event_ctx_lock(event); |
| 2365 | _perf_event_enable(event); |
| 2366 | perf_event_ctx_unlock(event, ctx); |
| 2367 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2368 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2369 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2370 | struct stop_event_data { |
| 2371 | struct perf_event *event; |
| 2372 | unsigned int restart; |
| 2373 | }; |
| 2374 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2375 | static int __perf_event_stop(void *info) |
| 2376 | { |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2377 | struct stop_event_data *sd = info; |
| 2378 | struct perf_event *event = sd->event; |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2379 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2380 | /* if it's already INACTIVE, do nothing */ |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2381 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
| 2382 | return 0; |
| 2383 | |
| 2384 | /* matches smp_wmb() in event_sched_in() */ |
| 2385 | smp_rmb(); |
| 2386 | |
| 2387 | /* |
| 2388 | * There is a window with interrupts enabled before we get here, |
| 2389 | * so we need to check again lest we try to stop another CPU's event. |
| 2390 | */ |
| 2391 | if (READ_ONCE(event->oncpu) != smp_processor_id()) |
| 2392 | return -EAGAIN; |
| 2393 | |
| 2394 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2395 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2396 | /* |
| 2397 | * May race with the actual stop (through perf_pmu_output_stop()), |
| 2398 | * but it is only used for events with AUX ring buffer, and such |
| 2399 | * events will refuse to restart because of rb::aux_mmap_count==0, |
| 2400 | * see comments in perf_aux_output_begin(). |
| 2401 | * |
| 2402 | * Since this is happening on a event-local CPU, no trace is lost |
| 2403 | * while restarting. |
| 2404 | */ |
| 2405 | if (sd->restart) |
| 2406 | event->pmu->start(event, PERF_EF_START); |
| 2407 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2408 | return 0; |
| 2409 | } |
| 2410 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2411 | static int perf_event_restart(struct perf_event *event) |
| 2412 | { |
| 2413 | struct stop_event_data sd = { |
| 2414 | .event = event, |
| 2415 | .restart = 1, |
| 2416 | }; |
| 2417 | int ret = 0; |
| 2418 | |
| 2419 | do { |
| 2420 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
| 2421 | return 0; |
| 2422 | |
| 2423 | /* matches smp_wmb() in event_sched_in() */ |
| 2424 | smp_rmb(); |
| 2425 | |
| 2426 | /* |
| 2427 | * We only want to restart ACTIVE events, so if the event goes |
| 2428 | * inactive here (event->oncpu==-1), there's nothing more to do; |
| 2429 | * fall through with ret==-ENXIO. |
| 2430 | */ |
| 2431 | ret = cpu_function_call(READ_ONCE(event->oncpu), |
| 2432 | __perf_event_stop, &sd); |
| 2433 | } while (ret == -EAGAIN); |
| 2434 | |
| 2435 | return ret; |
| 2436 | } |
| 2437 | |
| 2438 | /* |
| 2439 | * In order to contain the amount of racy and tricky in the address filter |
| 2440 | * configuration management, it is a two part process: |
| 2441 | * |
| 2442 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, |
| 2443 | * we update the addresses of corresponding vmas in |
| 2444 | * event::addr_filters_offs array and bump the event::addr_filters_gen; |
| 2445 | * (p2) when an event is scheduled in (pmu::add), it calls |
| 2446 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() |
| 2447 | * if the generation has changed since the previous call. |
| 2448 | * |
| 2449 | * If (p1) happens while the event is active, we restart it to force (p2). |
| 2450 | * |
| 2451 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on |
| 2452 | * pre-existing mappings, called once when new filters arrive via SET_FILTER |
| 2453 | * ioctl; |
| 2454 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly |
| 2455 | * registered mapping, called for every new mmap(), with mm::mmap_sem down |
| 2456 | * for reading; |
| 2457 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process |
| 2458 | * of exec. |
| 2459 | */ |
| 2460 | void perf_event_addr_filters_sync(struct perf_event *event) |
| 2461 | { |
| 2462 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 2463 | |
| 2464 | if (!has_addr_filter(event)) |
| 2465 | return; |
| 2466 | |
| 2467 | raw_spin_lock(&ifh->lock); |
| 2468 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { |
| 2469 | event->pmu->addr_filters_sync(event); |
| 2470 | event->hw.addr_filters_gen = event->addr_filters_gen; |
| 2471 | } |
| 2472 | raw_spin_unlock(&ifh->lock); |
| 2473 | } |
| 2474 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); |
| 2475 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2476 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2477 | { |
| 2478 | /* |
| 2479 | * not supported on inherited events |
| 2480 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2481 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2482 | return -EINVAL; |
| 2483 | |
| 2484 | atomic_add(refresh, &event->event_limit); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2485 | _perf_event_enable(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2486 | |
| 2487 | return 0; |
| 2488 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2489 | |
| 2490 | /* |
| 2491 | * See perf_event_disable() |
| 2492 | */ |
| 2493 | int perf_event_refresh(struct perf_event *event, int refresh) |
| 2494 | { |
| 2495 | struct perf_event_context *ctx; |
| 2496 | int ret; |
| 2497 | |
| 2498 | ctx = perf_event_ctx_lock(event); |
| 2499 | ret = _perf_event_refresh(event, refresh); |
| 2500 | perf_event_ctx_unlock(event, ctx); |
| 2501 | |
| 2502 | return ret; |
| 2503 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2504 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2505 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2506 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2507 | struct perf_cpu_context *cpuctx, |
| 2508 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2509 | { |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2510 | int is_active = ctx->is_active; |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2511 | struct perf_event *event; |
| 2512 | |
| 2513 | lockdep_assert_held(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2514 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2515 | if (likely(!ctx->nr_events)) { |
| 2516 | /* |
| 2517 | * See __perf_remove_from_context(). |
| 2518 | */ |
| 2519 | WARN_ON_ONCE(ctx->is_active); |
| 2520 | if (ctx->task) |
| 2521 | WARN_ON_ONCE(cpuctx->task_ctx); |
| 2522 | return; |
| 2523 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2524 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2525 | ctx->is_active &= ~event_type; |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2526 | if (!(ctx->is_active & EVENT_ALL)) |
| 2527 | ctx->is_active = 0; |
| 2528 | |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2529 | if (ctx->task) { |
| 2530 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 2531 | if (!ctx->is_active) |
| 2532 | cpuctx->task_ctx = NULL; |
| 2533 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2534 | |
Peter Zijlstra | 8fdc653 | 2016-03-29 09:26:44 +0200 | [diff] [blame] | 2535 | /* |
| 2536 | * Always update time if it was set; not only when it changes. |
| 2537 | * Otherwise we can 'forget' to update time for any but the last |
| 2538 | * context we sched out. For example: |
| 2539 | * |
| 2540 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) |
| 2541 | * ctx_sched_out(.event_type = EVENT_PINNED) |
| 2542 | * |
| 2543 | * would only update time for the pinned events. |
| 2544 | */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2545 | if (is_active & EVENT_TIME) { |
| 2546 | /* update (and stop) ctx time */ |
| 2547 | update_context_time(ctx); |
| 2548 | update_cgrp_time_from_cpuctx(cpuctx); |
| 2549 | } |
| 2550 | |
Peter Zijlstra | 8fdc653 | 2016-03-29 09:26:44 +0200 | [diff] [blame] | 2551 | is_active ^= ctx->is_active; /* changed bits */ |
| 2552 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2553 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2554 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2555 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2556 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2557 | if (is_active & EVENT_PINNED) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2558 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2559 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2560 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2561 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2562 | if (is_active & EVENT_FLEXIBLE) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2563 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2564 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2565 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2566 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | /* |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2570 | * Test whether two contexts are equivalent, i.e. whether they have both been |
| 2571 | * cloned from the same version of the same context. |
| 2572 | * |
| 2573 | * Equivalence is measured using a generation number in the context that is |
| 2574 | * incremented on each modification to it; see unclone_ctx(), list_add_event() |
| 2575 | * and list_del_event(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2576 | */ |
| 2577 | static int context_equiv(struct perf_event_context *ctx1, |
| 2578 | struct perf_event_context *ctx2) |
| 2579 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 2580 | lockdep_assert_held(&ctx1->lock); |
| 2581 | lockdep_assert_held(&ctx2->lock); |
| 2582 | |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2583 | /* Pinning disables the swap optimization */ |
| 2584 | if (ctx1->pin_count || ctx2->pin_count) |
| 2585 | return 0; |
| 2586 | |
| 2587 | /* If ctx1 is the parent of ctx2 */ |
| 2588 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) |
| 2589 | return 1; |
| 2590 | |
| 2591 | /* If ctx2 is the parent of ctx1 */ |
| 2592 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) |
| 2593 | return 1; |
| 2594 | |
| 2595 | /* |
| 2596 | * If ctx1 and ctx2 have the same parent; we flatten the parent |
| 2597 | * hierarchy, see perf_event_init_context(). |
| 2598 | */ |
| 2599 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && |
| 2600 | ctx1->parent_gen == ctx2->parent_gen) |
| 2601 | return 1; |
| 2602 | |
| 2603 | /* Unmatched */ |
| 2604 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2605 | } |
| 2606 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2607 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2608 | struct perf_event *next_event) |
| 2609 | { |
| 2610 | u64 value; |
| 2611 | |
| 2612 | if (!event->attr.inherit_stat) |
| 2613 | return; |
| 2614 | |
| 2615 | /* |
| 2616 | * Update the event value, we cannot use perf_event_read() |
| 2617 | * because we're in the middle of a context switch and have IRQs |
| 2618 | * disabled, which upsets smp_call_function_single(), however |
| 2619 | * we know the event must be on the current CPU, therefore we |
| 2620 | * don't need to use it. |
| 2621 | */ |
| 2622 | switch (event->state) { |
| 2623 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2624 | event->pmu->read(event); |
| 2625 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2626 | |
| 2627 | case PERF_EVENT_STATE_INACTIVE: |
| 2628 | update_event_times(event); |
| 2629 | break; |
| 2630 | |
| 2631 | default: |
| 2632 | break; |
| 2633 | } |
| 2634 | |
| 2635 | /* |
| 2636 | * In order to keep per-task stats reliable we need to flip the event |
| 2637 | * values when we flip the contexts. |
| 2638 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2639 | value = local64_read(&next_event->count); |
| 2640 | value = local64_xchg(&event->count, value); |
| 2641 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2642 | |
| 2643 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2644 | swap(event->total_time_running, next_event->total_time_running); |
| 2645 | |
| 2646 | /* |
| 2647 | * Since we swizzled the values, update the user visible data too. |
| 2648 | */ |
| 2649 | perf_event_update_userpage(event); |
| 2650 | perf_event_update_userpage(next_event); |
| 2651 | } |
| 2652 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2653 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2654 | struct perf_event_context *next_ctx) |
| 2655 | { |
| 2656 | struct perf_event *event, *next_event; |
| 2657 | |
| 2658 | if (!ctx->nr_stat) |
| 2659 | return; |
| 2660 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2661 | update_context_time(ctx); |
| 2662 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2663 | event = list_first_entry(&ctx->event_list, |
| 2664 | struct perf_event, event_entry); |
| 2665 | |
| 2666 | next_event = list_first_entry(&next_ctx->event_list, |
| 2667 | struct perf_event, event_entry); |
| 2668 | |
| 2669 | while (&event->event_entry != &ctx->event_list && |
| 2670 | &next_event->event_entry != &next_ctx->event_list) { |
| 2671 | |
| 2672 | __perf_event_sync_stat(event, next_event); |
| 2673 | |
| 2674 | event = list_next_entry(event, event_entry); |
| 2675 | next_event = list_next_entry(next_event, event_entry); |
| 2676 | } |
| 2677 | } |
| 2678 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2679 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2680 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2681 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2682 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2683 | struct perf_event_context *next_ctx; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2684 | struct perf_event_context *parent, *next_parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2685 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2686 | int do_switch = 1; |
| 2687 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2688 | if (likely(!ctx)) |
| 2689 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2690 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2691 | cpuctx = __get_cpu_context(ctx); |
| 2692 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2693 | return; |
| 2694 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2695 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2696 | next_ctx = next->perf_event_ctxp[ctxn]; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2697 | if (!next_ctx) |
| 2698 | goto unlock; |
| 2699 | |
| 2700 | parent = rcu_dereference(ctx->parent_ctx); |
| 2701 | next_parent = rcu_dereference(next_ctx->parent_ctx); |
| 2702 | |
| 2703 | /* If neither context have a parent context; they cannot be clones. */ |
Jiri Olsa | 802c8a6 | 2014-09-12 13:18:28 +0200 | [diff] [blame] | 2704 | if (!parent && !next_parent) |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2705 | goto unlock; |
| 2706 | |
| 2707 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2708 | /* |
| 2709 | * Looks like the two contexts are clones, so we might be |
| 2710 | * able to optimize the context switch. We lock both |
| 2711 | * contexts and check that they are clones under the |
| 2712 | * lock (including re-checking that neither has been |
| 2713 | * uncloned in the meantime). It doesn't matter which |
| 2714 | * order we take the locks because no other cpu could |
| 2715 | * be trying to lock both of these tasks. |
| 2716 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2717 | raw_spin_lock(&ctx->lock); |
| 2718 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2719 | if (context_equiv(ctx, next_ctx)) { |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2720 | WRITE_ONCE(ctx->task, next); |
| 2721 | WRITE_ONCE(next_ctx->task, task); |
Yan, Zheng | 5a158c3 | 2014-11-04 21:56:02 -0500 | [diff] [blame] | 2722 | |
| 2723 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); |
| 2724 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2725 | /* |
| 2726 | * RCU_INIT_POINTER here is safe because we've not |
| 2727 | * modified the ctx and the above modification of |
| 2728 | * ctx->task and ctx->task_ctx_data are immaterial |
| 2729 | * since those values are always verified under |
| 2730 | * ctx->lock which we're now holding. |
| 2731 | */ |
| 2732 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); |
| 2733 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); |
| 2734 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2735 | do_switch = 0; |
| 2736 | |
| 2737 | perf_event_sync_stat(ctx, next_ctx); |
| 2738 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2739 | raw_spin_unlock(&next_ctx->lock); |
| 2740 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2741 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2742 | unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2743 | rcu_read_unlock(); |
| 2744 | |
| 2745 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2746 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 8833d0e | 2016-01-08 10:02:37 +0100 | [diff] [blame] | 2747 | task_ctx_sched_out(cpuctx, ctx); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2748 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2749 | } |
| 2750 | } |
| 2751 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2752 | void perf_sched_cb_dec(struct pmu *pmu) |
| 2753 | { |
| 2754 | this_cpu_dec(perf_sched_cb_usages); |
| 2755 | } |
| 2756 | |
| 2757 | void perf_sched_cb_inc(struct pmu *pmu) |
| 2758 | { |
| 2759 | this_cpu_inc(perf_sched_cb_usages); |
| 2760 | } |
| 2761 | |
| 2762 | /* |
| 2763 | * This function provides the context switch callback to the lower code |
| 2764 | * layer. It is invoked ONLY when the context switch callback is enabled. |
| 2765 | */ |
| 2766 | static void perf_pmu_sched_task(struct task_struct *prev, |
| 2767 | struct task_struct *next, |
| 2768 | bool sched_in) |
| 2769 | { |
| 2770 | struct perf_cpu_context *cpuctx; |
| 2771 | struct pmu *pmu; |
| 2772 | unsigned long flags; |
| 2773 | |
| 2774 | if (prev == next) |
| 2775 | return; |
| 2776 | |
| 2777 | local_irq_save(flags); |
| 2778 | |
| 2779 | rcu_read_lock(); |
| 2780 | |
| 2781 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 2782 | if (pmu->sched_task) { |
| 2783 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2784 | |
| 2785 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2786 | |
| 2787 | perf_pmu_disable(pmu); |
| 2788 | |
| 2789 | pmu->sched_task(cpuctx->task_ctx, sched_in); |
| 2790 | |
| 2791 | perf_pmu_enable(pmu); |
| 2792 | |
| 2793 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | rcu_read_unlock(); |
| 2798 | |
| 2799 | local_irq_restore(flags); |
| 2800 | } |
| 2801 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 2802 | static void perf_event_switch(struct task_struct *task, |
| 2803 | struct task_struct *next_prev, bool sched_in); |
| 2804 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2805 | #define for_each_task_context_nr(ctxn) \ |
| 2806 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2807 | |
| 2808 | /* |
| 2809 | * Called from scheduler to remove the events of the current task, |
| 2810 | * with interrupts disabled. |
| 2811 | * |
| 2812 | * We stop each event and update the event value in event->count. |
| 2813 | * |
| 2814 | * This does not protect us against NMI, but disable() |
| 2815 | * sets the disabled bit in the control field of event _before_ |
| 2816 | * accessing the event control register. If a NMI hits, then it will |
| 2817 | * not restart the event. |
| 2818 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2819 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2820 | struct task_struct *next) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2821 | { |
| 2822 | int ctxn; |
| 2823 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2824 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 2825 | perf_pmu_sched_task(task, next, false); |
| 2826 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 2827 | if (atomic_read(&nr_switch_events)) |
| 2828 | perf_event_switch(task, next, false); |
| 2829 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2830 | for_each_task_context_nr(ctxn) |
| 2831 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2832 | |
| 2833 | /* |
| 2834 | * if cgroup events exist on this CPU, then we need |
| 2835 | * to check if we have to switch out PMU state. |
| 2836 | * cgroup event are system-wide mode only |
| 2837 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2838 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2839 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2840 | } |
| 2841 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2842 | /* |
| 2843 | * Called with IRQs disabled |
| 2844 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2845 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2846 | enum event_type_t event_type) |
| 2847 | { |
| 2848 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2852 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2853 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2854 | { |
| 2855 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2856 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2857 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2858 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2859 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2860 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2861 | continue; |
| 2862 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2863 | /* may need to reset tstamp_enabled */ |
| 2864 | if (is_cgroup_event(event)) |
| 2865 | perf_cgroup_mark_enabled(event, ctx); |
| 2866 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2867 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2868 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2869 | |
| 2870 | /* |
| 2871 | * If this pinned group hasn't been scheduled, |
| 2872 | * put it in error state. |
| 2873 | */ |
| 2874 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2875 | update_group_times(event); |
| 2876 | event->state = PERF_EVENT_STATE_ERROR; |
| 2877 | } |
| 2878 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | static void |
| 2882 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2883 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2884 | { |
| 2885 | struct perf_event *event; |
| 2886 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2887 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2888 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2889 | /* Ignore events in OFF or ERROR state */ |
| 2890 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2891 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2892 | /* |
| 2893 | * Listen to the 'cpu' scheduling filter constraint |
| 2894 | * of events: |
| 2895 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2896 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2897 | continue; |
| 2898 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2899 | /* may need to reset tstamp_enabled */ |
| 2900 | if (is_cgroup_event(event)) |
| 2901 | perf_cgroup_mark_enabled(event, ctx); |
| 2902 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2903 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2904 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2905 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2906 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2907 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | static void |
| 2911 | ctx_sched_in(struct perf_event_context *ctx, |
| 2912 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2913 | enum event_type_t event_type, |
| 2914 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2915 | { |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2916 | int is_active = ctx->is_active; |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2917 | u64 now; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2918 | |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2919 | lockdep_assert_held(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2920 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2921 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2922 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2923 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2924 | ctx->is_active |= (event_type | EVENT_TIME); |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2925 | if (ctx->task) { |
| 2926 | if (!is_active) |
| 2927 | cpuctx->task_ctx = ctx; |
| 2928 | else |
| 2929 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 2930 | } |
| 2931 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2932 | is_active ^= ctx->is_active; /* changed bits */ |
| 2933 | |
| 2934 | if (is_active & EVENT_TIME) { |
| 2935 | /* start ctx time */ |
| 2936 | now = perf_clock(); |
| 2937 | ctx->timestamp = now; |
| 2938 | perf_cgroup_set_timestamp(task, ctx); |
| 2939 | } |
| 2940 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2941 | /* |
| 2942 | * First go through the list and put on any pinned groups |
| 2943 | * in order to give them the best chance of going on. |
| 2944 | */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2945 | if (is_active & EVENT_PINNED) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2946 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2947 | |
| 2948 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2949 | if (is_active & EVENT_FLEXIBLE) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2950 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2951 | } |
| 2952 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2953 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2954 | enum event_type_t event_type, |
| 2955 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2956 | { |
| 2957 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2958 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2959 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2960 | } |
| 2961 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2962 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2963 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2964 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2965 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2966 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2967 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2968 | if (cpuctx->task_ctx == ctx) |
| 2969 | return; |
| 2970 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2971 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2972 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2973 | /* |
| 2974 | * We want to keep the following priority order: |
| 2975 | * cpu pinned (that don't need to move), task pinned, |
| 2976 | * cpu flexible, task flexible. |
| 2977 | */ |
| 2978 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2979 | perf_event_sched_in(cpuctx, ctx, task); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2980 | perf_pmu_enable(ctx->pmu); |
| 2981 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2982 | } |
| 2983 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2984 | /* |
| 2985 | * Called from scheduler to add the events of the current task |
| 2986 | * with interrupts disabled. |
| 2987 | * |
| 2988 | * We restore the event value and then enable it. |
| 2989 | * |
| 2990 | * This does not protect us against NMI, but enable() |
| 2991 | * sets the enabled bit in the control field of event _before_ |
| 2992 | * accessing the event control register. If a NMI hits, then it will |
| 2993 | * keep the event running. |
| 2994 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2995 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 2996 | struct task_struct *task) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2997 | { |
| 2998 | struct perf_event_context *ctx; |
| 2999 | int ctxn; |
| 3000 | |
Peter Zijlstra | 7e41d17 | 2016-01-08 09:21:40 +0100 | [diff] [blame] | 3001 | /* |
| 3002 | * If cgroup events exist on this CPU, then we need to check if we have |
| 3003 | * to switch in PMU state; cgroup event are system-wide mode only. |
| 3004 | * |
| 3005 | * Since cgroup events are CPU events, we must schedule these in before |
| 3006 | * we schedule in the task events. |
| 3007 | */ |
| 3008 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
| 3009 | perf_cgroup_sched_in(prev, task); |
| 3010 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3011 | for_each_task_context_nr(ctxn) { |
| 3012 | ctx = task->perf_event_ctxp[ctxn]; |
| 3013 | if (likely(!ctx)) |
| 3014 | continue; |
| 3015 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3016 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3017 | } |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 3018 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3019 | if (atomic_read(&nr_switch_events)) |
| 3020 | perf_event_switch(task, prev, true); |
| 3021 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 3022 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 3023 | perf_pmu_sched_task(prev, task, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3024 | } |
| 3025 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3026 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 3027 | { |
| 3028 | u64 frequency = event->attr.sample_freq; |
| 3029 | u64 sec = NSEC_PER_SEC; |
| 3030 | u64 divisor, dividend; |
| 3031 | |
| 3032 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 3033 | |
| 3034 | count_fls = fls64(count); |
| 3035 | nsec_fls = fls64(nsec); |
| 3036 | frequency_fls = fls64(frequency); |
| 3037 | sec_fls = 30; |
| 3038 | |
| 3039 | /* |
| 3040 | * We got @count in @nsec, with a target of sample_freq HZ |
| 3041 | * the target period becomes: |
| 3042 | * |
| 3043 | * @count * 10^9 |
| 3044 | * period = ------------------- |
| 3045 | * @nsec * sample_freq |
| 3046 | * |
| 3047 | */ |
| 3048 | |
| 3049 | /* |
| 3050 | * Reduce accuracy by one bit such that @a and @b converge |
| 3051 | * to a similar magnitude. |
| 3052 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3053 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3054 | do { \ |
| 3055 | if (a##_fls > b##_fls) { \ |
| 3056 | a >>= 1; \ |
| 3057 | a##_fls--; \ |
| 3058 | } else { \ |
| 3059 | b >>= 1; \ |
| 3060 | b##_fls--; \ |
| 3061 | } \ |
| 3062 | } while (0) |
| 3063 | |
| 3064 | /* |
| 3065 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 3066 | * the other, so that finally we can do a u64/u64 division. |
| 3067 | */ |
| 3068 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 3069 | REDUCE_FLS(nsec, frequency); |
| 3070 | REDUCE_FLS(sec, count); |
| 3071 | } |
| 3072 | |
| 3073 | if (count_fls + sec_fls > 64) { |
| 3074 | divisor = nsec * frequency; |
| 3075 | |
| 3076 | while (count_fls + sec_fls > 64) { |
| 3077 | REDUCE_FLS(count, sec); |
| 3078 | divisor >>= 1; |
| 3079 | } |
| 3080 | |
| 3081 | dividend = count * sec; |
| 3082 | } else { |
| 3083 | dividend = count * sec; |
| 3084 | |
| 3085 | while (nsec_fls + frequency_fls > 64) { |
| 3086 | REDUCE_FLS(nsec, frequency); |
| 3087 | dividend >>= 1; |
| 3088 | } |
| 3089 | |
| 3090 | divisor = nsec * frequency; |
| 3091 | } |
| 3092 | |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 3093 | if (!divisor) |
| 3094 | return dividend; |
| 3095 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3096 | return div64_u64(dividend, divisor); |
| 3097 | } |
| 3098 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3099 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 3100 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 3101 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3102 | 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] | 3103 | { |
| 3104 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 3105 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3106 | s64 delta; |
| 3107 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3108 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3109 | |
| 3110 | delta = (s64)(period - hwc->sample_period); |
| 3111 | delta = (delta + 7) / 8; /* low pass filter */ |
| 3112 | |
| 3113 | sample_period = hwc->sample_period + delta; |
| 3114 | |
| 3115 | if (!sample_period) |
| 3116 | sample_period = 1; |
| 3117 | |
| 3118 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3119 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3120 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3121 | if (disable) |
| 3122 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3123 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3124 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3125 | |
| 3126 | if (disable) |
| 3127 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3128 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3129 | } |
| 3130 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3131 | /* |
| 3132 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 3133 | * events. At the same time, make sure, having freq events does not change |
| 3134 | * the rate of unthrottling as that would introduce bias. |
| 3135 | */ |
| 3136 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 3137 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3138 | { |
| 3139 | struct perf_event *event; |
| 3140 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3141 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3142 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3143 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3144 | /* |
| 3145 | * only need to iterate over all events iff: |
| 3146 | * - context have events in frequency mode (needs freq adjust) |
| 3147 | * - there are events to unthrottle on this cpu |
| 3148 | */ |
| 3149 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3150 | return; |
| 3151 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3152 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3153 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3154 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 3155 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3156 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 3157 | continue; |
| 3158 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 3159 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 3160 | continue; |
| 3161 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3162 | perf_pmu_disable(event->pmu); |
| 3163 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3164 | hwc = &event->hw; |
| 3165 | |
Jiri Olsa | ae23bff | 2013-08-24 16:45:54 +0200 | [diff] [blame] | 3166 | if (hwc->interrupts == MAX_INTERRUPTS) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3167 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3168 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 3169 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | if (!event->attr.freq || !event->attr.sample_freq) |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3173 | goto next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3174 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3175 | /* |
| 3176 | * stop the event and update event->count |
| 3177 | */ |
| 3178 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3179 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3180 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3181 | delta = now - hwc->freq_count_stamp; |
| 3182 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3183 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3184 | /* |
| 3185 | * restart the event |
| 3186 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3187 | * we have stopped the event so tell that |
| 3188 | * to perf_adjust_period() to avoid stopping it |
| 3189 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3190 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3191 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3192 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3193 | |
| 3194 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3195 | next: |
| 3196 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3197 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3198 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3199 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3200 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | /* |
| 3204 | * Round-robin a context's events: |
| 3205 | */ |
| 3206 | static void rotate_ctx(struct perf_event_context *ctx) |
| 3207 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 3208 | /* |
| 3209 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 3210 | * disabled by the inheritance code. |
| 3211 | */ |
| 3212 | if (!ctx->rotate_disable) |
| 3213 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3214 | } |
| 3215 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3216 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3217 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3218 | struct perf_event_context *ctx = NULL; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3219 | int rotate = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3220 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3221 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3222 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 3223 | rotate = 1; |
| 3224 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3225 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3226 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3227 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3228 | if (ctx->nr_events != ctx->nr_active) |
| 3229 | rotate = 1; |
| 3230 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3231 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3232 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3233 | goto done; |
| 3234 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 3235 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 3236 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3237 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3238 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 3239 | if (ctx) |
| 3240 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 3241 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3242 | rotate_ctx(&cpuctx->ctx); |
| 3243 | if (ctx) |
| 3244 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3245 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3246 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3247 | |
| 3248 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 3249 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3250 | done: |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3251 | |
| 3252 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | void perf_event_task_tick(void) |
| 3256 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3257 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
| 3258 | struct perf_event_context *ctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3259 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3260 | |
| 3261 | WARN_ON(!irqs_disabled()); |
| 3262 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3263 | __this_cpu_inc(perf_throttled_seq); |
| 3264 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3265 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3266 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3267 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3268 | perf_adjust_freq_unthr_context(ctx, throttled); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3271 | static int event_enable_on_exec(struct perf_event *event, |
| 3272 | struct perf_event_context *ctx) |
| 3273 | { |
| 3274 | if (!event->attr.enable_on_exec) |
| 3275 | return 0; |
| 3276 | |
| 3277 | event->attr.enable_on_exec = 0; |
| 3278 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 3279 | return 0; |
| 3280 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 3281 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3282 | |
| 3283 | return 1; |
| 3284 | } |
| 3285 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3286 | /* |
| 3287 | * Enable all of a task's events that have been marked enable-on-exec. |
| 3288 | * This expects task == current. |
| 3289 | */ |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3290 | static void perf_event_enable_on_exec(int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3291 | { |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3292 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3293 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3294 | struct perf_event *event; |
| 3295 | unsigned long flags; |
| 3296 | int enabled = 0; |
| 3297 | |
| 3298 | local_irq_save(flags); |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3299 | ctx = current->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3300 | if (!ctx || !ctx->nr_events) |
| 3301 | goto out; |
| 3302 | |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3303 | cpuctx = __get_cpu_context(ctx); |
| 3304 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 7fce250 | 2016-02-24 18:45:48 +0100 | [diff] [blame] | 3305 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3306 | list_for_each_entry(event, &ctx->event_list, event_entry) |
| 3307 | enabled |= event_enable_on_exec(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3308 | |
| 3309 | /* |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3310 | * Unclone and reschedule this context if we enabled any event. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3311 | */ |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3312 | if (enabled) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3313 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3314 | ctx_resched(cpuctx, ctx); |
| 3315 | } |
| 3316 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3317 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3318 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3319 | local_irq_restore(flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3320 | |
| 3321 | if (clone_ctx) |
| 3322 | put_ctx(clone_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3323 | } |
| 3324 | |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3325 | struct perf_read_data { |
| 3326 | struct perf_event *event; |
| 3327 | bool group; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3328 | int ret; |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3329 | }; |
| 3330 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3331 | /* |
| 3332 | * Cross CPU call to read the hardware event |
| 3333 | */ |
| 3334 | static void __perf_event_read(void *info) |
| 3335 | { |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3336 | struct perf_read_data *data = info; |
| 3337 | struct perf_event *sub, *event = data->event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3338 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3339 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3340 | struct pmu *pmu = event->pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3341 | |
| 3342 | /* |
| 3343 | * If this is a task context, we need to check whether it is |
| 3344 | * the current task context of this cpu. If not it has been |
| 3345 | * scheduled out before the smp call arrived. In that case |
| 3346 | * event->count would have been updated to a recent sample |
| 3347 | * when the event was scheduled out. |
| 3348 | */ |
| 3349 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 3350 | return; |
| 3351 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3352 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3353 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3354 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3355 | update_cgrp_time_from_event(event); |
| 3356 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3357 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3358 | update_event_times(event); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3359 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3360 | goto unlock; |
| 3361 | |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3362 | if (!data->group) { |
| 3363 | pmu->read(event); |
| 3364 | data->ret = 0; |
| 3365 | goto unlock; |
| 3366 | } |
| 3367 | |
| 3368 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); |
| 3369 | |
| 3370 | pmu->read(event); |
| 3371 | |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3372 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 3373 | update_event_times(sub); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3374 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
| 3375 | /* |
| 3376 | * Use sibling's PMU rather than @event's since |
| 3377 | * sibling could be on different (eg: software) PMU. |
| 3378 | */ |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3379 | sub->pmu->read(sub); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3380 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3381 | } |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3382 | |
| 3383 | data->ret = pmu->commit_txn(pmu); |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3384 | |
| 3385 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3386 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3387 | } |
| 3388 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3389 | static inline u64 perf_event_count(struct perf_event *event) |
| 3390 | { |
Matt Fleming | eacd3ec | 2015-01-23 18:45:41 +0000 | [diff] [blame] | 3391 | if (event->pmu->count) |
| 3392 | return event->pmu->count(event); |
| 3393 | |
| 3394 | return __perf_event_count(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3395 | } |
| 3396 | |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 3397 | /* |
| 3398 | * NMI-safe method to read a local event, that is an event that |
| 3399 | * is: |
| 3400 | * - either for the current task, or for this CPU |
| 3401 | * - does not have inherit set, for inherited task events |
| 3402 | * will not be local and we cannot read them atomically |
| 3403 | * - must not have a pmu::count method |
| 3404 | */ |
| 3405 | u64 perf_event_read_local(struct perf_event *event) |
| 3406 | { |
| 3407 | unsigned long flags; |
| 3408 | u64 val; |
| 3409 | |
| 3410 | /* |
| 3411 | * Disabling interrupts avoids all counter scheduling (context |
| 3412 | * switches, timer based rotation and IPIs). |
| 3413 | */ |
| 3414 | local_irq_save(flags); |
| 3415 | |
| 3416 | /* If this is a per-task event, it must be for current */ |
| 3417 | WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) && |
| 3418 | event->hw.target != current); |
| 3419 | |
| 3420 | /* If this is a per-CPU event, it must be for this CPU */ |
| 3421 | WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) && |
| 3422 | event->cpu != smp_processor_id()); |
| 3423 | |
| 3424 | /* |
| 3425 | * It must not be an event with inherit set, we cannot read |
| 3426 | * all child counters from atomic context. |
| 3427 | */ |
| 3428 | WARN_ON_ONCE(event->attr.inherit); |
| 3429 | |
| 3430 | /* |
| 3431 | * It must not have a pmu::count method, those are not |
| 3432 | * NMI safe. |
| 3433 | */ |
| 3434 | WARN_ON_ONCE(event->pmu->count); |
| 3435 | |
| 3436 | /* |
| 3437 | * If the event is currently on this CPU, its either a per-task event, |
| 3438 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise |
| 3439 | * oncpu == -1). |
| 3440 | */ |
| 3441 | if (event->oncpu == smp_processor_id()) |
| 3442 | event->pmu->read(event); |
| 3443 | |
| 3444 | val = local64_read(&event->count); |
| 3445 | local_irq_restore(flags); |
| 3446 | |
| 3447 | return val; |
| 3448 | } |
| 3449 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3450 | static int perf_event_read(struct perf_event *event, bool group) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3451 | { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3452 | int ret = 0; |
| 3453 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3454 | /* |
| 3455 | * If event is enabled and currently active on a CPU, update the |
| 3456 | * value in the event structure: |
| 3457 | */ |
| 3458 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3459 | struct perf_read_data data = { |
| 3460 | .event = event, |
| 3461 | .group = group, |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3462 | .ret = 0, |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3463 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3464 | smp_call_function_single(event->oncpu, |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3465 | __perf_event_read, &data, 1); |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3466 | ret = data.ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3467 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 3468 | struct perf_event_context *ctx = event->ctx; |
| 3469 | unsigned long flags; |
| 3470 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3471 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3472 | /* |
| 3473 | * may read while context is not active |
| 3474 | * (e.g., thread is blocked), in that case |
| 3475 | * we cannot update context time |
| 3476 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3477 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3478 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3479 | update_cgrp_time_from_event(event); |
| 3480 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3481 | if (group) |
| 3482 | update_group_times(event); |
| 3483 | else |
| 3484 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3485 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3486 | } |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3487 | |
| 3488 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | /* |
| 3492 | * Initialize the perf_event context in a task_struct: |
| 3493 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3494 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3495 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3496 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3497 | mutex_init(&ctx->mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3498 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3499 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 3500 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3501 | INIT_LIST_HEAD(&ctx->event_list); |
| 3502 | atomic_set(&ctx->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3503 | } |
| 3504 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3505 | static struct perf_event_context * |
| 3506 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3507 | { |
| 3508 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3509 | |
| 3510 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 3511 | if (!ctx) |
| 3512 | return NULL; |
| 3513 | |
| 3514 | __perf_event_init_context(ctx); |
| 3515 | if (task) { |
| 3516 | ctx->task = task; |
| 3517 | get_task_struct(task); |
| 3518 | } |
| 3519 | ctx->pmu = pmu; |
| 3520 | |
| 3521 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3522 | } |
| 3523 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3524 | static struct task_struct * |
| 3525 | find_lively_task_by_vpid(pid_t vpid) |
| 3526 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3527 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3528 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3529 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3530 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3531 | task = current; |
| 3532 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3533 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3534 | if (task) |
| 3535 | get_task_struct(task); |
| 3536 | rcu_read_unlock(); |
| 3537 | |
| 3538 | if (!task) |
| 3539 | return ERR_PTR(-ESRCH); |
| 3540 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3541 | return task; |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3542 | } |
| 3543 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3544 | /* |
| 3545 | * Returns a matching context with refcount and pincount. |
| 3546 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3547 | static struct perf_event_context * |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3548 | find_get_context(struct pmu *pmu, struct task_struct *task, |
| 3549 | struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3550 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3551 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3552 | struct perf_cpu_context *cpuctx; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3553 | void *task_ctx_data = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3554 | unsigned long flags; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3555 | int ctxn, err; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3556 | int cpu = event->cpu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3557 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3558 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3559 | /* Must be root to operate on a CPU event: */ |
| 3560 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3561 | return ERR_PTR(-EACCES); |
| 3562 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3563 | /* |
| 3564 | * We could be clever and allow to attach a event to an |
| 3565 | * offline CPU and activate it when the CPU comes up, but |
| 3566 | * that's for later. |
| 3567 | */ |
| 3568 | if (!cpu_online(cpu)) |
| 3569 | return ERR_PTR(-ENODEV); |
| 3570 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3571 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3572 | ctx = &cpuctx->ctx; |
| 3573 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3574 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3575 | |
| 3576 | return ctx; |
| 3577 | } |
| 3578 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3579 | err = -EINVAL; |
| 3580 | ctxn = pmu->task_ctx_nr; |
| 3581 | if (ctxn < 0) |
| 3582 | goto errout; |
| 3583 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3584 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
| 3585 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); |
| 3586 | if (!task_ctx_data) { |
| 3587 | err = -ENOMEM; |
| 3588 | goto errout; |
| 3589 | } |
| 3590 | } |
| 3591 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3592 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3593 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3594 | if (ctx) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3595 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3596 | ++ctx->pin_count; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3597 | |
| 3598 | if (task_ctx_data && !ctx->task_ctx_data) { |
| 3599 | ctx->task_ctx_data = task_ctx_data; |
| 3600 | task_ctx_data = NULL; |
| 3601 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3602 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3603 | |
| 3604 | if (clone_ctx) |
| 3605 | put_ctx(clone_ctx); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3606 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3607 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3608 | err = -ENOMEM; |
| 3609 | if (!ctx) |
| 3610 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3611 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3612 | if (task_ctx_data) { |
| 3613 | ctx->task_ctx_data = task_ctx_data; |
| 3614 | task_ctx_data = NULL; |
| 3615 | } |
| 3616 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3617 | err = 0; |
| 3618 | mutex_lock(&task->perf_event_mutex); |
| 3619 | /* |
| 3620 | * If it has already passed perf_event_exit_task(). |
| 3621 | * we must see PF_EXITING, it takes this mutex too. |
| 3622 | */ |
| 3623 | if (task->flags & PF_EXITING) |
| 3624 | err = -ESRCH; |
| 3625 | else if (task->perf_event_ctxp[ctxn]) |
| 3626 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3627 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3628 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3629 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3630 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3631 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3632 | mutex_unlock(&task->perf_event_mutex); |
| 3633 | |
| 3634 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3635 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3636 | |
| 3637 | if (err == -EAGAIN) |
| 3638 | goto retry; |
| 3639 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3640 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3641 | } |
| 3642 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3643 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3644 | return ctx; |
| 3645 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3646 | errout: |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3647 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3648 | return ERR_PTR(err); |
| 3649 | } |
| 3650 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3651 | static void perf_event_free_filter(struct perf_event *event); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 3652 | static void perf_event_free_bpf_prog(struct perf_event *event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3653 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3654 | static void free_event_rcu(struct rcu_head *head) |
| 3655 | { |
| 3656 | struct perf_event *event; |
| 3657 | |
| 3658 | event = container_of(head, struct perf_event, rcu_head); |
| 3659 | if (event->ns) |
| 3660 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3661 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3662 | kfree(event); |
| 3663 | } |
| 3664 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3665 | static void ring_buffer_attach(struct perf_event *event, |
| 3666 | struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3667 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3668 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
| 3669 | { |
| 3670 | if (event->parent) |
| 3671 | return; |
| 3672 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3673 | if (is_cgroup_event(event)) |
| 3674 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); |
| 3675 | } |
| 3676 | |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3677 | #ifdef CONFIG_NO_HZ_FULL |
| 3678 | static DEFINE_SPINLOCK(nr_freq_lock); |
| 3679 | #endif |
| 3680 | |
| 3681 | static void unaccount_freq_event_nohz(void) |
| 3682 | { |
| 3683 | #ifdef CONFIG_NO_HZ_FULL |
| 3684 | spin_lock(&nr_freq_lock); |
| 3685 | if (atomic_dec_and_test(&nr_freq_events)) |
| 3686 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); |
| 3687 | spin_unlock(&nr_freq_lock); |
| 3688 | #endif |
| 3689 | } |
| 3690 | |
| 3691 | static void unaccount_freq_event(void) |
| 3692 | { |
| 3693 | if (tick_nohz_full_enabled()) |
| 3694 | unaccount_freq_event_nohz(); |
| 3695 | else |
| 3696 | atomic_dec(&nr_freq_events); |
| 3697 | } |
| 3698 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3699 | static void unaccount_event(struct perf_event *event) |
| 3700 | { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3701 | bool dec = false; |
| 3702 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3703 | if (event->parent) |
| 3704 | return; |
| 3705 | |
| 3706 | if (event->attach_state & PERF_ATTACH_TASK) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3707 | dec = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3708 | if (event->attr.mmap || event->attr.mmap_data) |
| 3709 | atomic_dec(&nr_mmap_events); |
| 3710 | if (event->attr.comm) |
| 3711 | atomic_dec(&nr_comm_events); |
| 3712 | if (event->attr.task) |
| 3713 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3714 | if (event->attr.freq) |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3715 | unaccount_freq_event(); |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3716 | if (event->attr.context_switch) { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3717 | dec = true; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3718 | atomic_dec(&nr_switch_events); |
| 3719 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3720 | if (is_cgroup_event(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3721 | dec = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3722 | if (has_branch_stack(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3723 | dec = true; |
| 3724 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 3725 | if (dec) { |
| 3726 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) |
| 3727 | schedule_delayed_work(&perf_sched_work, HZ); |
| 3728 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3729 | |
| 3730 | unaccount_event_cpu(event, event->cpu); |
| 3731 | } |
| 3732 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 3733 | static void perf_sched_delayed(struct work_struct *work) |
| 3734 | { |
| 3735 | mutex_lock(&perf_sched_mutex); |
| 3736 | if (atomic_dec_and_test(&perf_sched_count)) |
| 3737 | static_branch_disable(&perf_sched_events); |
| 3738 | mutex_unlock(&perf_sched_mutex); |
| 3739 | } |
| 3740 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 3741 | /* |
| 3742 | * The following implement mutual exclusion of events on "exclusive" pmus |
| 3743 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled |
| 3744 | * at a time, so we disallow creating events that might conflict, namely: |
| 3745 | * |
| 3746 | * 1) cpu-wide events in the presence of per-task events, |
| 3747 | * 2) per-task events in the presence of cpu-wide events, |
| 3748 | * 3) two matching events on the same context. |
| 3749 | * |
| 3750 | * The former two cases are handled in the allocation path (perf_event_alloc(), |
Peter Zijlstra | a0733e6 | 2016-01-26 12:14:40 +0100 | [diff] [blame] | 3751 | * _free_event()), the latter -- before the first perf_install_in_context(). |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 3752 | */ |
| 3753 | static int exclusive_event_init(struct perf_event *event) |
| 3754 | { |
| 3755 | struct pmu *pmu = event->pmu; |
| 3756 | |
| 3757 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3758 | return 0; |
| 3759 | |
| 3760 | /* |
| 3761 | * Prevent co-existence of per-task and cpu-wide events on the |
| 3762 | * same exclusive pmu. |
| 3763 | * |
| 3764 | * Negative pmu::exclusive_cnt means there are cpu-wide |
| 3765 | * events on this "exclusive" pmu, positive means there are |
| 3766 | * per-task events. |
| 3767 | * |
| 3768 | * Since this is called in perf_event_alloc() path, event::ctx |
| 3769 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK |
| 3770 | * to mean "per-task event", because unlike other attach states it |
| 3771 | * never gets cleared. |
| 3772 | */ |
| 3773 | if (event->attach_state & PERF_ATTACH_TASK) { |
| 3774 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) |
| 3775 | return -EBUSY; |
| 3776 | } else { |
| 3777 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) |
| 3778 | return -EBUSY; |
| 3779 | } |
| 3780 | |
| 3781 | return 0; |
| 3782 | } |
| 3783 | |
| 3784 | static void exclusive_event_destroy(struct perf_event *event) |
| 3785 | { |
| 3786 | struct pmu *pmu = event->pmu; |
| 3787 | |
| 3788 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3789 | return; |
| 3790 | |
| 3791 | /* see comment in exclusive_event_init() */ |
| 3792 | if (event->attach_state & PERF_ATTACH_TASK) |
| 3793 | atomic_dec(&pmu->exclusive_cnt); |
| 3794 | else |
| 3795 | atomic_inc(&pmu->exclusive_cnt); |
| 3796 | } |
| 3797 | |
| 3798 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) |
| 3799 | { |
| 3800 | if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && |
| 3801 | (e1->cpu == e2->cpu || |
| 3802 | e1->cpu == -1 || |
| 3803 | e2->cpu == -1)) |
| 3804 | return true; |
| 3805 | return false; |
| 3806 | } |
| 3807 | |
| 3808 | /* Called under the same ctx::mutex as perf_install_in_context() */ |
| 3809 | static bool exclusive_event_installable(struct perf_event *event, |
| 3810 | struct perf_event_context *ctx) |
| 3811 | { |
| 3812 | struct perf_event *iter_event; |
| 3813 | struct pmu *pmu = event->pmu; |
| 3814 | |
| 3815 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3816 | return true; |
| 3817 | |
| 3818 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { |
| 3819 | if (exclusive_event_match(iter_event, event)) |
| 3820 | return false; |
| 3821 | } |
| 3822 | |
| 3823 | return true; |
| 3824 | } |
| 3825 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 3826 | static void perf_addr_filters_splice(struct perf_event *event, |
| 3827 | struct list_head *head); |
| 3828 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3829 | static void _free_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3830 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3831 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3832 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3833 | unaccount_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3834 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3835 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3836 | /* |
| 3837 | * Can happen when we close an event with re-directed output. |
| 3838 | * |
| 3839 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 3840 | * over us; possibly making our ring_buffer_put() the last. |
| 3841 | */ |
| 3842 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3843 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3844 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3845 | } |
| 3846 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3847 | if (is_cgroup_event(event)) |
| 3848 | perf_detach_cgroup(event); |
| 3849 | |
Peter Zijlstra | a0733e6 | 2016-01-26 12:14:40 +0100 | [diff] [blame] | 3850 | if (!event->parent) { |
| 3851 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 3852 | put_callchain_buffers(); |
| 3853 | } |
| 3854 | |
| 3855 | perf_event_free_bpf_prog(event); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 3856 | perf_addr_filters_splice(event, NULL); |
| 3857 | kfree(event->addr_filters_offs); |
Peter Zijlstra | a0733e6 | 2016-01-26 12:14:40 +0100 | [diff] [blame] | 3858 | |
| 3859 | if (event->destroy) |
| 3860 | event->destroy(event); |
| 3861 | |
| 3862 | if (event->ctx) |
| 3863 | put_ctx(event->ctx); |
| 3864 | |
| 3865 | if (event->pmu) { |
| 3866 | exclusive_event_destroy(event); |
| 3867 | module_put(event->pmu->module); |
| 3868 | } |
| 3869 | |
| 3870 | call_rcu(&event->rcu_head, free_event_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3871 | } |
| 3872 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3873 | /* |
| 3874 | * Used to free events which have a known refcount of 1, such as in error paths |
| 3875 | * where the event isn't exposed yet and inherited events. |
| 3876 | */ |
| 3877 | static void free_event(struct perf_event *event) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3878 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3879 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
| 3880 | "unexpected event refcount: %ld; ptr=%p\n", |
| 3881 | atomic_long_read(&event->refcount), event)) { |
| 3882 | /* leak to avoid use-after-free */ |
| 3883 | return; |
| 3884 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3885 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3886 | _free_event(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3887 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3888 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3889 | /* |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3890 | * Remove user event from the owner task. |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3891 | */ |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3892 | static void perf_remove_from_owner(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3893 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3894 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3895 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3896 | rcu_read_lock(); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3897 | /* |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3898 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
| 3899 | * observe !owner it means the list deletion is complete and we can |
| 3900 | * indeed free this event, otherwise we need to serialize on |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3901 | * owner->perf_event_mutex. |
| 3902 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3903 | owner = lockless_dereference(event->owner); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3904 | if (owner) { |
| 3905 | /* |
| 3906 | * Since delayed_put_task_struct() also drops the last |
| 3907 | * task reference we can safely take a new reference |
| 3908 | * while holding the rcu_read_lock(). |
| 3909 | */ |
| 3910 | get_task_struct(owner); |
| 3911 | } |
| 3912 | rcu_read_unlock(); |
| 3913 | |
| 3914 | if (owner) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3915 | /* |
| 3916 | * If we're here through perf_event_exit_task() we're already |
| 3917 | * holding ctx->mutex which would be an inversion wrt. the |
| 3918 | * normal lock order. |
| 3919 | * |
| 3920 | * However we can safely take this lock because its the child |
| 3921 | * ctx->mutex. |
| 3922 | */ |
| 3923 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); |
| 3924 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3925 | /* |
| 3926 | * We have to re-check the event->owner field, if it is cleared |
| 3927 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 3928 | * ensured they're done, and we can proceed with freeing the |
| 3929 | * event. |
| 3930 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3931 | if (event->owner) { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3932 | list_del_init(&event->owner_entry); |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3933 | smp_store_release(&event->owner, NULL); |
| 3934 | } |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3935 | mutex_unlock(&owner->perf_event_mutex); |
| 3936 | put_task_struct(owner); |
| 3937 | } |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3938 | } |
| 3939 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3940 | static void put_event(struct perf_event *event) |
| 3941 | { |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3942 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 3943 | return; |
| 3944 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3945 | _free_event(event); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3946 | } |
| 3947 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3948 | /* |
| 3949 | * Kill an event dead; while event:refcount will preserve the event |
| 3950 | * object, it will not preserve its functionality. Once the last 'user' |
| 3951 | * gives up the object, we'll destroy the thing. |
| 3952 | */ |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3953 | int perf_event_release_kernel(struct perf_event *event) |
| 3954 | { |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 3955 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3956 | struct perf_event *child, *tmp; |
| 3957 | |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 3958 | /* |
| 3959 | * If we got here through err_file: fput(event_file); we will not have |
| 3960 | * attached to a context yet. |
| 3961 | */ |
| 3962 | if (!ctx) { |
| 3963 | WARN_ON_ONCE(event->attach_state & |
| 3964 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); |
| 3965 | goto no_ctx; |
| 3966 | } |
| 3967 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3968 | if (!is_kernel_event(event)) |
| 3969 | perf_remove_from_owner(event); |
| 3970 | |
Peter Zijlstra | 5fa7c8e | 2016-01-26 15:25:15 +0100 | [diff] [blame] | 3971 | ctx = perf_event_ctx_lock(event); |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3972 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3973 | perf_remove_from_context(event, DETACH_GROUP); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3974 | |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3975 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 3976 | /* |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3977 | * Mark this even as STATE_DEAD, there is no external reference to it |
| 3978 | * anymore. |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3979 | * |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3980 | * Anybody acquiring event->child_mutex after the below loop _must_ |
| 3981 | * also see this, most importantly inherit_event() which will avoid |
| 3982 | * placing more children on the list. |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3983 | * |
| 3984 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
| 3985 | * child events. |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 3986 | */ |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3987 | event->state = PERF_EVENT_STATE_DEAD; |
| 3988 | raw_spin_unlock_irq(&ctx->lock); |
| 3989 | |
| 3990 | perf_event_ctx_unlock(event, ctx); |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 3991 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3992 | again: |
| 3993 | mutex_lock(&event->child_mutex); |
| 3994 | list_for_each_entry(child, &event->child_list, child_list) { |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3995 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3996 | /* |
| 3997 | * Cannot change, child events are not migrated, see the |
| 3998 | * comment with perf_event_ctx_lock_nested(). |
| 3999 | */ |
| 4000 | ctx = lockless_dereference(child->ctx); |
| 4001 | /* |
| 4002 | * Since child_mutex nests inside ctx::mutex, we must jump |
| 4003 | * through hoops. We start by grabbing a reference on the ctx. |
| 4004 | * |
| 4005 | * Since the event cannot get freed while we hold the |
| 4006 | * child_mutex, the context must also exist and have a !0 |
| 4007 | * reference count. |
| 4008 | */ |
| 4009 | get_ctx(ctx); |
| 4010 | |
| 4011 | /* |
| 4012 | * Now that we have a ctx ref, we can drop child_mutex, and |
| 4013 | * acquire ctx::mutex without fear of it going away. Then we |
| 4014 | * can re-acquire child_mutex. |
| 4015 | */ |
| 4016 | mutex_unlock(&event->child_mutex); |
| 4017 | mutex_lock(&ctx->mutex); |
| 4018 | mutex_lock(&event->child_mutex); |
| 4019 | |
| 4020 | /* |
| 4021 | * Now that we hold ctx::mutex and child_mutex, revalidate our |
| 4022 | * state, if child is still the first entry, it didn't get freed |
| 4023 | * and we can continue doing so. |
| 4024 | */ |
| 4025 | tmp = list_first_entry_or_null(&event->child_list, |
| 4026 | struct perf_event, child_list); |
| 4027 | if (tmp == child) { |
| 4028 | perf_remove_from_context(child, DETACH_GROUP); |
| 4029 | list_del(&child->child_list); |
| 4030 | free_event(child); |
| 4031 | /* |
| 4032 | * This matches the refcount bump in inherit_event(); |
| 4033 | * this can't be the last reference. |
| 4034 | */ |
| 4035 | put_event(event); |
| 4036 | } |
| 4037 | |
| 4038 | mutex_unlock(&event->child_mutex); |
| 4039 | mutex_unlock(&ctx->mutex); |
| 4040 | put_ctx(ctx); |
| 4041 | goto again; |
| 4042 | } |
| 4043 | mutex_unlock(&event->child_mutex); |
| 4044 | |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 4045 | no_ctx: |
| 4046 | put_event(event); /* Must be the 'last' reference */ |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4047 | return 0; |
| 4048 | } |
| 4049 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 4050 | |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 4051 | /* |
| 4052 | * Called when the last reference to the file is gone. |
| 4053 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 4054 | static int perf_release(struct inode *inode, struct file *file) |
| 4055 | { |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4056 | perf_event_release_kernel(file->private_data); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 4057 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 4058 | } |
| 4059 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4060 | 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] | 4061 | { |
| 4062 | struct perf_event *child; |
| 4063 | u64 total = 0; |
| 4064 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4065 | *enabled = 0; |
| 4066 | *running = 0; |
| 4067 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 4068 | mutex_lock(&event->child_mutex); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 4069 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4070 | (void)perf_event_read(event, false); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 4071 | total += perf_event_count(event); |
| 4072 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4073 | *enabled += event->total_time_enabled + |
| 4074 | atomic64_read(&event->child_total_time_enabled); |
| 4075 | *running += event->total_time_running + |
| 4076 | atomic64_read(&event->child_total_time_running); |
| 4077 | |
| 4078 | list_for_each_entry(child, &event->child_list, child_list) { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4079 | (void)perf_event_read(child, false); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 4080 | total += perf_event_count(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4081 | *enabled += child->total_time_enabled; |
| 4082 | *running += child->total_time_running; |
| 4083 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 4084 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4085 | |
| 4086 | return total; |
| 4087 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 4088 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4089 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4090 | static int __perf_read_group_add(struct perf_event *leader, |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4091 | u64 read_format, u64 *values) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4092 | { |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4093 | struct perf_event *sub; |
| 4094 | int n = 1; /* skip @nr */ |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4095 | int ret; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4096 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4097 | ret = perf_event_read(leader, true); |
| 4098 | if (ret) |
| 4099 | return ret; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4100 | |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4101 | /* |
| 4102 | * Since we co-schedule groups, {enabled,running} times of siblings |
| 4103 | * will be identical to those of the leader, so we only publish one |
| 4104 | * set. |
| 4105 | */ |
| 4106 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
| 4107 | values[n++] += leader->total_time_enabled + |
| 4108 | atomic64_read(&leader->child_total_time_enabled); |
| 4109 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4110 | |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4111 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
| 4112 | values[n++] += leader->total_time_running + |
| 4113 | atomic64_read(&leader->child_total_time_running); |
| 4114 | } |
| 4115 | |
| 4116 | /* |
| 4117 | * Write {count,id} tuples for every sibling. |
| 4118 | */ |
| 4119 | values[n++] += perf_event_count(leader); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4120 | if (read_format & PERF_FORMAT_ID) |
| 4121 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4122 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4123 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4124 | values[n++] += perf_event_count(sub); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4125 | if (read_format & PERF_FORMAT_ID) |
| 4126 | values[n++] = primary_event_id(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4127 | } |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4128 | |
| 4129 | return 0; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | static int perf_read_group(struct perf_event *event, |
| 4133 | u64 read_format, char __user *buf) |
| 4134 | { |
| 4135 | struct perf_event *leader = event->group_leader, *child; |
| 4136 | struct perf_event_context *ctx = leader->ctx; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4137 | int ret; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4138 | u64 *values; |
| 4139 | |
| 4140 | lockdep_assert_held(&ctx->mutex); |
| 4141 | |
| 4142 | values = kzalloc(event->read_size, GFP_KERNEL); |
| 4143 | if (!values) |
| 4144 | return -ENOMEM; |
| 4145 | |
| 4146 | values[0] = 1 + leader->nr_siblings; |
| 4147 | |
| 4148 | /* |
| 4149 | * By locking the child_mutex of the leader we effectively |
| 4150 | * lock the child list of all siblings.. XXX explain how. |
| 4151 | */ |
| 4152 | mutex_lock(&leader->child_mutex); |
| 4153 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4154 | ret = __perf_read_group_add(leader, read_format, values); |
| 4155 | if (ret) |
| 4156 | goto unlock; |
| 4157 | |
| 4158 | list_for_each_entry(child, &leader->child_list, child_list) { |
| 4159 | ret = __perf_read_group_add(child, read_format, values); |
| 4160 | if (ret) |
| 4161 | goto unlock; |
| 4162 | } |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4163 | |
| 4164 | mutex_unlock(&leader->child_mutex); |
| 4165 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4166 | ret = event->read_size; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4167 | if (copy_to_user(buf, values, event->read_size)) |
| 4168 | ret = -EFAULT; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4169 | goto out; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4170 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4171 | unlock: |
| 4172 | mutex_unlock(&leader->child_mutex); |
| 4173 | out: |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4174 | kfree(values); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4175 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4176 | } |
| 4177 | |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4178 | static int perf_read_one(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4179 | u64 read_format, char __user *buf) |
| 4180 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4181 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4182 | u64 values[4]; |
| 4183 | int n = 0; |
| 4184 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4185 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 4186 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 4187 | values[n++] = enabled; |
| 4188 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4189 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4190 | if (read_format & PERF_FORMAT_ID) |
| 4191 | values[n++] = primary_event_id(event); |
| 4192 | |
| 4193 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 4194 | return -EFAULT; |
| 4195 | |
| 4196 | return n * sizeof(u64); |
| 4197 | } |
| 4198 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4199 | static bool is_event_hup(struct perf_event *event) |
| 4200 | { |
| 4201 | bool no_children; |
| 4202 | |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4203 | if (event->state > PERF_EVENT_STATE_EXIT) |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4204 | return false; |
| 4205 | |
| 4206 | mutex_lock(&event->child_mutex); |
| 4207 | no_children = list_empty(&event->child_list); |
| 4208 | mutex_unlock(&event->child_mutex); |
| 4209 | return no_children; |
| 4210 | } |
| 4211 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4212 | /* |
| 4213 | * Read the performance event - simple non blocking version for now |
| 4214 | */ |
| 4215 | static ssize_t |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4216 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4217 | { |
| 4218 | u64 read_format = event->attr.read_format; |
| 4219 | int ret; |
| 4220 | |
| 4221 | /* |
| 4222 | * Return end-of-file for a read on a event that is in |
| 4223 | * error state (i.e. because it was pinned but it couldn't be |
| 4224 | * scheduled on to the CPU at some point). |
| 4225 | */ |
| 4226 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 4227 | return 0; |
| 4228 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4229 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4230 | return -ENOSPC; |
| 4231 | |
| 4232 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4233 | if (read_format & PERF_FORMAT_GROUP) |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4234 | ret = perf_read_group(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4235 | else |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4236 | ret = perf_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4237 | |
| 4238 | return ret; |
| 4239 | } |
| 4240 | |
| 4241 | static ssize_t |
| 4242 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 4243 | { |
| 4244 | struct perf_event *event = file->private_data; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4245 | struct perf_event_context *ctx; |
| 4246 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4247 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4248 | ctx = perf_event_ctx_lock(event); |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4249 | ret = __perf_read(event, buf, count); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4250 | perf_event_ctx_unlock(event, ctx); |
| 4251 | |
| 4252 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 4256 | { |
| 4257 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4258 | struct ring_buffer *rb; |
Jiri Olsa | 61b6768 | 2014-08-13 19:39:56 +0200 | [diff] [blame] | 4259 | unsigned int events = POLLHUP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4260 | |
Sebastian Andrzej Siewior | e708d7a | 2014-08-04 15:31:08 +0200 | [diff] [blame] | 4261 | poll_wait(file, &event->waitq, wait); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 4262 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4263 | if (is_event_hup(event)) |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 4264 | return events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4265 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4266 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4267 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 4268 | * 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] | 4269 | */ |
| 4270 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4271 | rb = event->rb; |
| 4272 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4273 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4274 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4275 | return events; |
| 4276 | } |
| 4277 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4278 | static void _perf_event_reset(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4279 | { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4280 | (void)perf_event_read(event, false); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4281 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4282 | perf_event_update_userpage(event); |
| 4283 | } |
| 4284 | |
| 4285 | /* |
| 4286 | * Holding the top-level event's child_mutex means that any |
| 4287 | * descendant process that has inherited this event will block |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 4288 | * in perf_event_exit_event() if it goes to exit, thus satisfying the |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4289 | * task existence requirements of perf_event_enable/disable. |
| 4290 | */ |
| 4291 | static void perf_event_for_each_child(struct perf_event *event, |
| 4292 | void (*func)(struct perf_event *)) |
| 4293 | { |
| 4294 | struct perf_event *child; |
| 4295 | |
| 4296 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4297 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4298 | mutex_lock(&event->child_mutex); |
| 4299 | func(event); |
| 4300 | list_for_each_entry(child, &event->child_list, child_list) |
| 4301 | func(child); |
| 4302 | mutex_unlock(&event->child_mutex); |
| 4303 | } |
| 4304 | |
| 4305 | static void perf_event_for_each(struct perf_event *event, |
| 4306 | void (*func)(struct perf_event *)) |
| 4307 | { |
| 4308 | struct perf_event_context *ctx = event->ctx; |
| 4309 | struct perf_event *sibling; |
| 4310 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4311 | lockdep_assert_held(&ctx->mutex); |
| 4312 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4313 | event = event->group_leader; |
| 4314 | |
| 4315 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4316 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 4317 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4318 | } |
| 4319 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4320 | static void __perf_event_period(struct perf_event *event, |
| 4321 | struct perf_cpu_context *cpuctx, |
| 4322 | struct perf_event_context *ctx, |
| 4323 | void *info) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 4324 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4325 | u64 value = *((u64 *)info); |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4326 | bool active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4327 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4328 | if (event->attr.freq) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4329 | event->attr.sample_freq = value; |
| 4330 | } else { |
| 4331 | event->attr.sample_period = value; |
| 4332 | event->hw.sample_period = value; |
| 4333 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 4334 | |
| 4335 | active = (event->state == PERF_EVENT_STATE_ACTIVE); |
| 4336 | if (active) { |
| 4337 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | 1e02cd4 | 2016-03-10 15:39:24 +0100 | [diff] [blame] | 4338 | /* |
| 4339 | * We could be throttled; unthrottle now to avoid the tick |
| 4340 | * trying to unthrottle while we already re-started the event. |
| 4341 | */ |
| 4342 | if (event->hw.interrupts == MAX_INTERRUPTS) { |
| 4343 | event->hw.interrupts = 0; |
| 4344 | perf_log_throttle(event, 1); |
| 4345 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 4346 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 4347 | } |
| 4348 | |
| 4349 | local64_set(&event->hw.period_left, 0); |
| 4350 | |
| 4351 | if (active) { |
| 4352 | event->pmu->start(event, PERF_EF_RELOAD); |
| 4353 | perf_pmu_enable(ctx->pmu); |
| 4354 | } |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 4358 | { |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4359 | u64 value; |
| 4360 | |
| 4361 | if (!is_sampling_event(event)) |
| 4362 | return -EINVAL; |
| 4363 | |
| 4364 | if (copy_from_user(&value, arg, sizeof(value))) |
| 4365 | return -EFAULT; |
| 4366 | |
| 4367 | if (!value) |
| 4368 | return -EINVAL; |
| 4369 | |
| 4370 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) |
| 4371 | return -EINVAL; |
| 4372 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4373 | event_function_call(event, __perf_event_period, &value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4374 | |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4375 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4376 | } |
| 4377 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4378 | static const struct file_operations perf_fops; |
| 4379 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4380 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4381 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4382 | struct fd f = fdget(fd); |
| 4383 | if (!f.file) |
| 4384 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4385 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4386 | if (f.file->f_op != &perf_fops) { |
| 4387 | fdput(f); |
| 4388 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4389 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4390 | *p = f; |
| 4391 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | static int perf_event_set_output(struct perf_event *event, |
| 4395 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4396 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 4397 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4398 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4399 | 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] | 4400 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4401 | void (*func)(struct perf_event *); |
| 4402 | u32 flags = arg; |
| 4403 | |
| 4404 | switch (cmd) { |
| 4405 | case PERF_EVENT_IOC_ENABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4406 | func = _perf_event_enable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4407 | break; |
| 4408 | case PERF_EVENT_IOC_DISABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4409 | func = _perf_event_disable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4410 | break; |
| 4411 | case PERF_EVENT_IOC_RESET: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4412 | func = _perf_event_reset; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4413 | break; |
| 4414 | |
| 4415 | case PERF_EVENT_IOC_REFRESH: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4416 | return _perf_event_refresh(event, arg); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4417 | |
| 4418 | case PERF_EVENT_IOC_PERIOD: |
| 4419 | return perf_event_period(event, (u64 __user *)arg); |
| 4420 | |
Jiri Olsa | cf4957f | 2012-10-24 13:37:58 +0200 | [diff] [blame] | 4421 | case PERF_EVENT_IOC_ID: |
| 4422 | { |
| 4423 | u64 id = primary_event_id(event); |
| 4424 | |
| 4425 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) |
| 4426 | return -EFAULT; |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4430 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4431 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4432 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4433 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4434 | struct perf_event *output_event; |
| 4435 | struct fd output; |
| 4436 | ret = perf_fget_light(arg, &output); |
| 4437 | if (ret) |
| 4438 | return ret; |
| 4439 | output_event = output.file->private_data; |
| 4440 | ret = perf_event_set_output(event, output_event); |
| 4441 | fdput(output); |
| 4442 | } else { |
| 4443 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4444 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4445 | return ret; |
| 4446 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4447 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4448 | case PERF_EVENT_IOC_SET_FILTER: |
| 4449 | return perf_event_set_filter(event, (void __user *)arg); |
| 4450 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 4451 | case PERF_EVENT_IOC_SET_BPF: |
| 4452 | return perf_event_set_bpf_prog(event, arg); |
| 4453 | |
Wang Nan | 86e7972 | 2016-03-28 06:41:29 +0000 | [diff] [blame] | 4454 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
| 4455 | struct ring_buffer *rb; |
| 4456 | |
| 4457 | rcu_read_lock(); |
| 4458 | rb = rcu_dereference(event->rb); |
| 4459 | if (!rb || !rb->nr_pages) { |
| 4460 | rcu_read_unlock(); |
| 4461 | return -EINVAL; |
| 4462 | } |
| 4463 | rb_toggle_paused(rb, !!arg); |
| 4464 | rcu_read_unlock(); |
| 4465 | return 0; |
| 4466 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4467 | default: |
| 4468 | return -ENOTTY; |
| 4469 | } |
| 4470 | |
| 4471 | if (flags & PERF_IOC_FLAG_GROUP) |
| 4472 | perf_event_for_each(event, func); |
| 4473 | else |
| 4474 | perf_event_for_each_child(event, func); |
| 4475 | |
| 4476 | return 0; |
| 4477 | } |
| 4478 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4479 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 4480 | { |
| 4481 | struct perf_event *event = file->private_data; |
| 4482 | struct perf_event_context *ctx; |
| 4483 | long ret; |
| 4484 | |
| 4485 | ctx = perf_event_ctx_lock(event); |
| 4486 | ret = _perf_ioctl(event, cmd, arg); |
| 4487 | perf_event_ctx_unlock(event, ctx); |
| 4488 | |
| 4489 | return ret; |
| 4490 | } |
| 4491 | |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4492 | #ifdef CONFIG_COMPAT |
| 4493 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, |
| 4494 | unsigned long arg) |
| 4495 | { |
| 4496 | switch (_IOC_NR(cmd)) { |
| 4497 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): |
| 4498 | case _IOC_NR(PERF_EVENT_IOC_ID): |
| 4499 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
| 4500 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { |
| 4501 | cmd &= ~IOCSIZE_MASK; |
| 4502 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; |
| 4503 | } |
| 4504 | break; |
| 4505 | } |
| 4506 | return perf_ioctl(file, cmd, arg); |
| 4507 | } |
| 4508 | #else |
| 4509 | # define perf_compat_ioctl NULL |
| 4510 | #endif |
| 4511 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4512 | int perf_event_task_enable(void) |
| 4513 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4514 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4515 | struct perf_event *event; |
| 4516 | |
| 4517 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4518 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4519 | ctx = perf_event_ctx_lock(event); |
| 4520 | perf_event_for_each_child(event, _perf_event_enable); |
| 4521 | perf_event_ctx_unlock(event, ctx); |
| 4522 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4523 | mutex_unlock(¤t->perf_event_mutex); |
| 4524 | |
| 4525 | return 0; |
| 4526 | } |
| 4527 | |
| 4528 | int perf_event_task_disable(void) |
| 4529 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4530 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4531 | struct perf_event *event; |
| 4532 | |
| 4533 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4534 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4535 | ctx = perf_event_ctx_lock(event); |
| 4536 | perf_event_for_each_child(event, _perf_event_disable); |
| 4537 | perf_event_ctx_unlock(event, ctx); |
| 4538 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4539 | mutex_unlock(¤t->perf_event_mutex); |
| 4540 | |
| 4541 | return 0; |
| 4542 | } |
| 4543 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4544 | static int perf_event_index(struct perf_event *event) |
| 4545 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 4546 | if (event->hw.state & PERF_HES_STOPPED) |
| 4547 | return 0; |
| 4548 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4549 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 4550 | return 0; |
| 4551 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 4552 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4553 | } |
| 4554 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4555 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4556 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4557 | u64 *enabled, |
| 4558 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4559 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4560 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4561 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4562 | *now = perf_clock(); |
| 4563 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4564 | *enabled = ctx_time - event->tstamp_enabled; |
| 4565 | *running = ctx_time - event->tstamp_running; |
| 4566 | } |
| 4567 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4568 | static void perf_event_init_userpage(struct perf_event *event) |
| 4569 | { |
| 4570 | struct perf_event_mmap_page *userpg; |
| 4571 | struct ring_buffer *rb; |
| 4572 | |
| 4573 | rcu_read_lock(); |
| 4574 | rb = rcu_dereference(event->rb); |
| 4575 | if (!rb) |
| 4576 | goto unlock; |
| 4577 | |
| 4578 | userpg = rb->user_page; |
| 4579 | |
| 4580 | /* Allow new userspace to detect that bit 0 is deprecated */ |
| 4581 | userpg->cap_bit0_is_deprecated = 1; |
| 4582 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); |
Alexander Shishkin | e8c6dea | 2015-01-14 14:18:10 +0200 | [diff] [blame] | 4583 | userpg->data_offset = PAGE_SIZE; |
| 4584 | userpg->data_size = perf_data_size(rb); |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4585 | |
| 4586 | unlock: |
| 4587 | rcu_read_unlock(); |
| 4588 | } |
| 4589 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4590 | void __weak arch_perf_update_userpage( |
| 4591 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4592 | { |
| 4593 | } |
| 4594 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4595 | /* |
| 4596 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 4597 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 4598 | * code calls this from NMI context. |
| 4599 | */ |
| 4600 | void perf_event_update_userpage(struct perf_event *event) |
| 4601 | { |
| 4602 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4603 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4604 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4605 | |
| 4606 | rcu_read_lock(); |
Peter Zijlstra | 5ec4c59 | 2013-08-02 21:16:30 +0200 | [diff] [blame] | 4607 | rb = rcu_dereference(event->rb); |
| 4608 | if (!rb) |
| 4609 | goto unlock; |
| 4610 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4611 | /* |
| 4612 | * compute total_time_enabled, total_time_running |
| 4613 | * based on snapshot values taken when the event |
| 4614 | * was last scheduled in. |
| 4615 | * |
| 4616 | * we cannot simply called update_context_time() |
| 4617 | * because of locking issue as we can be called in |
| 4618 | * NMI context |
| 4619 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4620 | calc_timer_values(event, &now, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4621 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4622 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4623 | /* |
| 4624 | * Disable preemption so as to not let the corresponding user-space |
| 4625 | * spin too long if we get preempted. |
| 4626 | */ |
| 4627 | preempt_disable(); |
| 4628 | ++userpg->lock; |
| 4629 | barrier(); |
| 4630 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4631 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 4632 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4633 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4634 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4635 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4636 | atomic64_read(&event->child_total_time_enabled); |
| 4637 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4638 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4639 | atomic64_read(&event->child_total_time_running); |
| 4640 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4641 | arch_perf_update_userpage(event, userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4642 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4643 | barrier(); |
| 4644 | ++userpg->lock; |
| 4645 | preempt_enable(); |
| 4646 | unlock: |
| 4647 | rcu_read_unlock(); |
| 4648 | } |
| 4649 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4650 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 4651 | { |
| 4652 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4653 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4654 | int ret = VM_FAULT_SIGBUS; |
| 4655 | |
| 4656 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 4657 | if (vmf->pgoff == 0) |
| 4658 | ret = 0; |
| 4659 | return ret; |
| 4660 | } |
| 4661 | |
| 4662 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4663 | rb = rcu_dereference(event->rb); |
| 4664 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4665 | goto unlock; |
| 4666 | |
| 4667 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 4668 | goto unlock; |
| 4669 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4670 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4671 | if (!vmf->page) |
| 4672 | goto unlock; |
| 4673 | |
| 4674 | get_page(vmf->page); |
| 4675 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 4676 | vmf->page->index = vmf->pgoff; |
| 4677 | |
| 4678 | ret = 0; |
| 4679 | unlock: |
| 4680 | rcu_read_unlock(); |
| 4681 | |
| 4682 | return ret; |
| 4683 | } |
| 4684 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4685 | static void ring_buffer_attach(struct perf_event *event, |
| 4686 | struct ring_buffer *rb) |
| 4687 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4688 | struct ring_buffer *old_rb = NULL; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4689 | unsigned long flags; |
| 4690 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4691 | if (event->rb) { |
| 4692 | /* |
| 4693 | * Should be impossible, we set this when removing |
| 4694 | * event->rb_entry and wait/clear when adding event->rb_entry. |
| 4695 | */ |
| 4696 | WARN_ON_ONCE(event->rcu_pending); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4697 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4698 | old_rb = event->rb; |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4699 | spin_lock_irqsave(&old_rb->event_lock, flags); |
| 4700 | list_del_rcu(&event->rb_entry); |
| 4701 | spin_unlock_irqrestore(&old_rb->event_lock, flags); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4702 | |
Oleg Nesterov | 2f993cf | 2015-05-30 22:04:25 +0200 | [diff] [blame] | 4703 | event->rcu_batches = get_state_synchronize_rcu(); |
| 4704 | event->rcu_pending = 1; |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4705 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4706 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4707 | if (rb) { |
Oleg Nesterov | 2f993cf | 2015-05-30 22:04:25 +0200 | [diff] [blame] | 4708 | if (event->rcu_pending) { |
| 4709 | cond_synchronize_rcu(event->rcu_batches); |
| 4710 | event->rcu_pending = 0; |
| 4711 | } |
| 4712 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4713 | spin_lock_irqsave(&rb->event_lock, flags); |
| 4714 | list_add_rcu(&event->rb_entry, &rb->event_list); |
| 4715 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 4716 | } |
| 4717 | |
| 4718 | rcu_assign_pointer(event->rb, rb); |
| 4719 | |
| 4720 | if (old_rb) { |
| 4721 | ring_buffer_put(old_rb); |
| 4722 | /* |
| 4723 | * Since we detached before setting the new rb, so that we |
| 4724 | * could attach the new rb, we could have missed a wakeup. |
| 4725 | * Provide it now. |
| 4726 | */ |
| 4727 | wake_up_all(&event->waitq); |
| 4728 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | static void ring_buffer_wakeup(struct perf_event *event) |
| 4732 | { |
| 4733 | struct ring_buffer *rb; |
| 4734 | |
| 4735 | rcu_read_lock(); |
| 4736 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4737 | if (rb) { |
| 4738 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 4739 | wake_up_all(&event->waitq); |
| 4740 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4741 | rcu_read_unlock(); |
| 4742 | } |
| 4743 | |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 4744 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4745 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4746 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4747 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4748 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4749 | rb = rcu_dereference(event->rb); |
| 4750 | if (rb) { |
| 4751 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 4752 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4753 | } |
| 4754 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4755 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4756 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4757 | } |
| 4758 | |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 4759 | void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4760 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4761 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4762 | return; |
| 4763 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4764 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4765 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4766 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 4770 | { |
| 4771 | struct perf_event *event = vma->vm_file->private_data; |
| 4772 | |
| 4773 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4774 | atomic_inc(&event->rb->mmap_count); |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4775 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4776 | if (vma->vm_pgoff) |
| 4777 | atomic_inc(&event->rb->aux_mmap_count); |
| 4778 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4779 | if (event->pmu->event_mapped) |
| 4780 | event->pmu->event_mapped(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4781 | } |
| 4782 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 4783 | static void perf_pmu_output_stop(struct perf_event *event); |
| 4784 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4785 | /* |
| 4786 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 4787 | * event, or through other events by use of perf_event_set_output(). |
| 4788 | * |
| 4789 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 4790 | * the buffer here, where we still have a VM context. This means we need |
| 4791 | * to detach all events redirecting to us. |
| 4792 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4793 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 4794 | { |
| 4795 | struct perf_event *event = vma->vm_file->private_data; |
| 4796 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4797 | struct ring_buffer *rb = ring_buffer_get(event); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4798 | struct user_struct *mmap_user = rb->mmap_user; |
| 4799 | int mmap_locked = rb->mmap_locked; |
| 4800 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4801 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4802 | if (event->pmu->event_unmapped) |
| 4803 | event->pmu->event_unmapped(event); |
| 4804 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4805 | /* |
| 4806 | * rb->aux_mmap_count will always drop before rb->mmap_count and |
| 4807 | * event->mmap_count, so it is ok to use event->mmap_mutex to |
| 4808 | * serialize with perf_mmap here. |
| 4809 | */ |
| 4810 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && |
| 4811 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 4812 | /* |
| 4813 | * Stop all AUX events that are writing to this buffer, |
| 4814 | * so that we can free its AUX pages and corresponding PMU |
| 4815 | * data. Note that after rb::aux_mmap_count dropped to zero, |
| 4816 | * they won't start any more (see perf_aux_output_begin()). |
| 4817 | */ |
| 4818 | perf_pmu_output_stop(event); |
| 4819 | |
| 4820 | /* now it's safe to free the pages */ |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4821 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
| 4822 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; |
| 4823 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 4824 | /* this has to be the last one */ |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4825 | rb_free_aux(rb); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 4826 | WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); |
| 4827 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4828 | mutex_unlock(&event->mmap_mutex); |
| 4829 | } |
| 4830 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4831 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4832 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4833 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4834 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4835 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4836 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4837 | mutex_unlock(&event->mmap_mutex); |
| 4838 | |
| 4839 | /* 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] | 4840 | if (atomic_read(&rb->mmap_count)) |
| 4841 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4842 | |
| 4843 | /* |
| 4844 | * No other mmap()s, detach from all other events that might redirect |
| 4845 | * into the now unreachable buffer. Somewhat complicated by the |
| 4846 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 4847 | */ |
| 4848 | again: |
| 4849 | rcu_read_lock(); |
| 4850 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 4851 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 4852 | /* |
| 4853 | * This event is en-route to free_event() which will |
| 4854 | * detach it and remove it from the list. |
| 4855 | */ |
| 4856 | continue; |
| 4857 | } |
| 4858 | rcu_read_unlock(); |
| 4859 | |
| 4860 | mutex_lock(&event->mmap_mutex); |
| 4861 | /* |
| 4862 | * Check we didn't race with perf_event_set_output() which can |
| 4863 | * swizzle the rb from under us while we were waiting to |
| 4864 | * acquire mmap_mutex. |
| 4865 | * |
| 4866 | * If we find a different rb; ignore this event, a next |
| 4867 | * iteration will no longer find it on the list. We have to |
| 4868 | * still restart the iteration to make sure we're not now |
| 4869 | * iterating the wrong list. |
| 4870 | */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4871 | if (event->rb == rb) |
| 4872 | ring_buffer_attach(event, NULL); |
| 4873 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4874 | mutex_unlock(&event->mmap_mutex); |
| 4875 | put_event(event); |
| 4876 | |
| 4877 | /* |
| 4878 | * Restart the iteration; either we're on the wrong list or |
| 4879 | * destroyed its integrity by doing a deletion. |
| 4880 | */ |
| 4881 | goto again; |
| 4882 | } |
| 4883 | rcu_read_unlock(); |
| 4884 | |
| 4885 | /* |
| 4886 | * It could be there's still a few 0-ref events on the list; they'll |
| 4887 | * get cleaned up by free_event() -- they'll also still have their |
| 4888 | * ref on the rb and will free it whenever they are done with it. |
| 4889 | * |
| 4890 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 4891 | * undo the VM accounting. |
| 4892 | */ |
| 4893 | |
| 4894 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 4895 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 4896 | free_uid(mmap_user); |
| 4897 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4898 | out_put: |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4899 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4900 | } |
| 4901 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 4902 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4903 | .open = perf_mmap_open, |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4904 | .close = perf_mmap_close, /* non mergable */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4905 | .fault = perf_mmap_fault, |
| 4906 | .page_mkwrite = perf_mmap_fault, |
| 4907 | }; |
| 4908 | |
| 4909 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 4910 | { |
| 4911 | struct perf_event *event = file->private_data; |
| 4912 | unsigned long user_locked, user_lock_limit; |
| 4913 | struct user_struct *user = current_user(); |
| 4914 | unsigned long locked, lock_limit; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4915 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4916 | unsigned long vma_size; |
| 4917 | unsigned long nr_pages; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4918 | long user_extra = 0, extra = 0; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4919 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4920 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4921 | /* |
| 4922 | * Don't allow mmap() of inherited per-task counters. This would |
| 4923 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4924 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4925 | */ |
| 4926 | if (event->cpu == -1 && event->attr.inherit) |
| 4927 | return -EINVAL; |
| 4928 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4929 | if (!(vma->vm_flags & VM_SHARED)) |
| 4930 | return -EINVAL; |
| 4931 | |
| 4932 | vma_size = vma->vm_end - vma->vm_start; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4933 | |
| 4934 | if (vma->vm_pgoff == 0) { |
| 4935 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 4936 | } else { |
| 4937 | /* |
| 4938 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already |
| 4939 | * mapped, all subsequent mappings should have the same size |
| 4940 | * and offset. Must be above the normal perf buffer. |
| 4941 | */ |
| 4942 | u64 aux_offset, aux_size; |
| 4943 | |
| 4944 | if (!event->rb) |
| 4945 | return -EINVAL; |
| 4946 | |
| 4947 | nr_pages = vma_size / PAGE_SIZE; |
| 4948 | |
| 4949 | mutex_lock(&event->mmap_mutex); |
| 4950 | ret = -EINVAL; |
| 4951 | |
| 4952 | rb = event->rb; |
| 4953 | if (!rb) |
| 4954 | goto aux_unlock; |
| 4955 | |
| 4956 | aux_offset = ACCESS_ONCE(rb->user_page->aux_offset); |
| 4957 | aux_size = ACCESS_ONCE(rb->user_page->aux_size); |
| 4958 | |
| 4959 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) |
| 4960 | goto aux_unlock; |
| 4961 | |
| 4962 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) |
| 4963 | goto aux_unlock; |
| 4964 | |
| 4965 | /* already mapped with a different offset */ |
| 4966 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) |
| 4967 | goto aux_unlock; |
| 4968 | |
| 4969 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) |
| 4970 | goto aux_unlock; |
| 4971 | |
| 4972 | /* already mapped with a different size */ |
| 4973 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) |
| 4974 | goto aux_unlock; |
| 4975 | |
| 4976 | if (!is_power_of_2(nr_pages)) |
| 4977 | goto aux_unlock; |
| 4978 | |
| 4979 | if (!atomic_inc_not_zero(&rb->mmap_count)) |
| 4980 | goto aux_unlock; |
| 4981 | |
| 4982 | if (rb_has_aux(rb)) { |
| 4983 | atomic_inc(&rb->aux_mmap_count); |
| 4984 | ret = 0; |
| 4985 | goto unlock; |
| 4986 | } |
| 4987 | |
| 4988 | atomic_set(&rb->aux_mmap_count, 1); |
| 4989 | user_extra = nr_pages; |
| 4990 | |
| 4991 | goto accounting; |
| 4992 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4993 | |
| 4994 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4995 | * 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] | 4996 | * can do bitmasks instead of modulo. |
| 4997 | */ |
Kan Liang | 2ed1131 | 2015-03-02 02:14:26 -0500 | [diff] [blame] | 4998 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4999 | return -EINVAL; |
| 5000 | |
| 5001 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 5002 | return -EINVAL; |
| 5003 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5004 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5005 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5006 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5007 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5008 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5009 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5010 | goto unlock; |
| 5011 | } |
| 5012 | |
| 5013 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 5014 | /* |
| 5015 | * Raced against perf_mmap_close() through |
| 5016 | * perf_event_set_output(). Try again, hope for better |
| 5017 | * luck. |
| 5018 | */ |
| 5019 | mutex_unlock(&event->mmap_mutex); |
| 5020 | goto again; |
| 5021 | } |
| 5022 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5023 | goto unlock; |
| 5024 | } |
| 5025 | |
| 5026 | user_extra = nr_pages + 1; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5027 | |
| 5028 | accounting: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5029 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 5030 | |
| 5031 | /* |
| 5032 | * Increase the limit linearly with more CPUs: |
| 5033 | */ |
| 5034 | user_lock_limit *= num_online_cpus(); |
| 5035 | |
| 5036 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 5037 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5038 | if (user_locked > user_lock_limit) |
| 5039 | extra = user_locked - user_lock_limit; |
| 5040 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 5041 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5042 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 5043 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5044 | |
| 5045 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 5046 | !capable(CAP_IPC_LOCK)) { |
| 5047 | ret = -EPERM; |
| 5048 | goto unlock; |
| 5049 | } |
| 5050 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5051 | WARN_ON(!rb && event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 5052 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 5053 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5054 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 5055 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5056 | if (!rb) { |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5057 | rb = rb_alloc(nr_pages, |
| 5058 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 5059 | event->cpu, flags); |
| 5060 | |
| 5061 | if (!rb) { |
| 5062 | ret = -ENOMEM; |
| 5063 | goto unlock; |
| 5064 | } |
| 5065 | |
| 5066 | atomic_set(&rb->mmap_count, 1); |
| 5067 | rb->mmap_user = get_current_user(); |
| 5068 | rb->mmap_locked = extra; |
| 5069 | |
| 5070 | ring_buffer_attach(event, rb); |
| 5071 | |
| 5072 | perf_event_init_userpage(event); |
| 5073 | perf_event_update_userpage(event); |
| 5074 | } else { |
Alexander Shishkin | 1a59413 | 2015-01-14 14:18:18 +0200 | [diff] [blame] | 5075 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
| 5076 | event->attr.aux_watermark, flags); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5077 | if (!ret) |
| 5078 | rb->aux_mmap_locked = extra; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5079 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 5080 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5081 | unlock: |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5082 | if (!ret) { |
| 5083 | atomic_long_add(user_extra, &user->locked_vm); |
| 5084 | vma->vm_mm->pinned_vm += extra; |
| 5085 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5086 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5087 | } else if (rb) { |
| 5088 | atomic_dec(&rb->mmap_count); |
| 5089 | } |
| 5090 | aux_unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5091 | mutex_unlock(&event->mmap_mutex); |
| 5092 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5093 | /* |
| 5094 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 5095 | * vma. |
| 5096 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 5097 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5098 | vma->vm_ops = &perf_mmap_vmops; |
| 5099 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 5100 | if (event->pmu->event_mapped) |
| 5101 | event->pmu->event_mapped(event); |
| 5102 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5103 | return ret; |
| 5104 | } |
| 5105 | |
| 5106 | static int perf_fasync(int fd, struct file *filp, int on) |
| 5107 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5108 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5109 | struct perf_event *event = filp->private_data; |
| 5110 | int retval; |
| 5111 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5112 | inode_lock(inode); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5113 | retval = fasync_helper(fd, filp, on, &event->fasync); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5114 | inode_unlock(inode); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5115 | |
| 5116 | if (retval < 0) |
| 5117 | return retval; |
| 5118 | |
| 5119 | return 0; |
| 5120 | } |
| 5121 | |
| 5122 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 5123 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5124 | .release = perf_release, |
| 5125 | .read = perf_read, |
| 5126 | .poll = perf_poll, |
| 5127 | .unlocked_ioctl = perf_ioctl, |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 5128 | .compat_ioctl = perf_compat_ioctl, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5129 | .mmap = perf_mmap, |
| 5130 | .fasync = perf_fasync, |
| 5131 | }; |
| 5132 | |
| 5133 | /* |
| 5134 | * Perf event wakeup |
| 5135 | * |
| 5136 | * If there's data, ensure we set the poll() state and publish everything |
| 5137 | * to user-space before waking everybody up. |
| 5138 | */ |
| 5139 | |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 5140 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
| 5141 | { |
| 5142 | /* only the parent has fasync state */ |
| 5143 | if (event->parent) |
| 5144 | event = event->parent; |
| 5145 | return &event->fasync; |
| 5146 | } |
| 5147 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5148 | void perf_event_wakeup(struct perf_event *event) |
| 5149 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 5150 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5151 | |
| 5152 | if (event->pending_kill) { |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 5153 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5154 | event->pending_kill = 0; |
| 5155 | } |
| 5156 | } |
| 5157 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 5158 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5159 | { |
| 5160 | struct perf_event *event = container_of(entry, |
| 5161 | struct perf_event, pending); |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 5162 | int rctx; |
| 5163 | |
| 5164 | rctx = perf_swevent_get_recursion_context(); |
| 5165 | /* |
| 5166 | * If we 'fail' here, that's OK, it means recursion is already disabled |
| 5167 | * and we won't recurse 'further'. |
| 5168 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5169 | |
| 5170 | if (event->pending_disable) { |
| 5171 | event->pending_disable = 0; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 5172 | perf_event_disable_local(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | if (event->pending_wakeup) { |
| 5176 | event->pending_wakeup = 0; |
| 5177 | perf_event_wakeup(event); |
| 5178 | } |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 5179 | |
| 5180 | if (rctx >= 0) |
| 5181 | perf_swevent_put_recursion_context(rctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5182 | } |
| 5183 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5184 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5185 | * We assume there is only KVM supporting the callbacks. |
| 5186 | * Later on, we might change it to a list if there is |
| 5187 | * another virtualization implementation supporting the callbacks. |
| 5188 | */ |
| 5189 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 5190 | |
| 5191 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 5192 | { |
| 5193 | perf_guest_cbs = cbs; |
| 5194 | return 0; |
| 5195 | } |
| 5196 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 5197 | |
| 5198 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 5199 | { |
| 5200 | perf_guest_cbs = NULL; |
| 5201 | return 0; |
| 5202 | } |
| 5203 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 5204 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5205 | static void |
| 5206 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 5207 | struct pt_regs *regs, u64 mask) |
| 5208 | { |
| 5209 | int bit; |
| 5210 | |
| 5211 | for_each_set_bit(bit, (const unsigned long *) &mask, |
| 5212 | sizeof(mask) * BITS_PER_BYTE) { |
| 5213 | u64 val; |
| 5214 | |
| 5215 | val = perf_reg_value(regs, bit); |
| 5216 | perf_output_put(handle, val); |
| 5217 | } |
| 5218 | } |
| 5219 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5220 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5221 | struct pt_regs *regs, |
| 5222 | struct pt_regs *regs_user_copy) |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5223 | { |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5224 | if (user_mode(regs)) { |
| 5225 | regs_user->abi = perf_reg_abi(current); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5226 | regs_user->regs = regs; |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5227 | } else if (current->mm) { |
| 5228 | perf_get_regs_user(regs_user, regs, regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5229 | } else { |
| 5230 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; |
| 5231 | regs_user->regs = NULL; |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5232 | } |
| 5233 | } |
| 5234 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5235 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
| 5236 | struct pt_regs *regs) |
| 5237 | { |
| 5238 | regs_intr->regs = regs; |
| 5239 | regs_intr->abi = perf_reg_abi(current); |
| 5240 | } |
| 5241 | |
| 5242 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5243 | /* |
| 5244 | * Get remaining task size from user stack pointer. |
| 5245 | * |
| 5246 | * It'd be better to take stack vma map and limit this more |
| 5247 | * precisly, but there's no way to get it safely under interrupt, |
| 5248 | * so using TASK_SIZE as limit. |
| 5249 | */ |
| 5250 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 5251 | { |
| 5252 | unsigned long addr = perf_user_stack_pointer(regs); |
| 5253 | |
| 5254 | if (!addr || addr >= TASK_SIZE) |
| 5255 | return 0; |
| 5256 | |
| 5257 | return TASK_SIZE - addr; |
| 5258 | } |
| 5259 | |
| 5260 | static u16 |
| 5261 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 5262 | struct pt_regs *regs) |
| 5263 | { |
| 5264 | u64 task_size; |
| 5265 | |
| 5266 | /* No regs, no stack pointer, no dump. */ |
| 5267 | if (!regs) |
| 5268 | return 0; |
| 5269 | |
| 5270 | /* |
| 5271 | * Check if we fit in with the requested stack size into the: |
| 5272 | * - TASK_SIZE |
| 5273 | * If we don't, we limit the size to the TASK_SIZE. |
| 5274 | * |
| 5275 | * - remaining sample size |
| 5276 | * If we don't, we customize the stack size to |
| 5277 | * fit in to the remaining sample size. |
| 5278 | */ |
| 5279 | |
| 5280 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 5281 | stack_size = min(stack_size, (u16) task_size); |
| 5282 | |
| 5283 | /* Current header size plus static size and dynamic size. */ |
| 5284 | header_size += 2 * sizeof(u64); |
| 5285 | |
| 5286 | /* Do we fit in with the current stack dump size? */ |
| 5287 | if ((u16) (header_size + stack_size) < header_size) { |
| 5288 | /* |
| 5289 | * If we overflow the maximum size for the sample, |
| 5290 | * we customize the stack dump size to fit in. |
| 5291 | */ |
| 5292 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 5293 | stack_size = round_up(stack_size, sizeof(u64)); |
| 5294 | } |
| 5295 | |
| 5296 | return stack_size; |
| 5297 | } |
| 5298 | |
| 5299 | static void |
| 5300 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 5301 | struct pt_regs *regs) |
| 5302 | { |
| 5303 | /* Case of a kernel thread, nothing to dump */ |
| 5304 | if (!regs) { |
| 5305 | u64 size = 0; |
| 5306 | perf_output_put(handle, size); |
| 5307 | } else { |
| 5308 | unsigned long sp; |
| 5309 | unsigned int rem; |
| 5310 | u64 dyn_size; |
| 5311 | |
| 5312 | /* |
| 5313 | * We dump: |
| 5314 | * static size |
| 5315 | * - the size requested by user or the best one we can fit |
| 5316 | * in to the sample max size |
| 5317 | * data |
| 5318 | * - user stack dump data |
| 5319 | * dynamic size |
| 5320 | * - the actual dumped size |
| 5321 | */ |
| 5322 | |
| 5323 | /* Static size. */ |
| 5324 | perf_output_put(handle, dump_size); |
| 5325 | |
| 5326 | /* Data. */ |
| 5327 | sp = perf_user_stack_pointer(regs); |
| 5328 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 5329 | dyn_size = dump_size - rem; |
| 5330 | |
| 5331 | perf_output_skip(handle, rem); |
| 5332 | |
| 5333 | /* Dynamic size. */ |
| 5334 | perf_output_put(handle, dyn_size); |
| 5335 | } |
| 5336 | } |
| 5337 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5338 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 5339 | struct perf_sample_data *data, |
| 5340 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5341 | { |
| 5342 | u64 sample_type = event->attr.sample_type; |
| 5343 | |
| 5344 | data->type = sample_type; |
| 5345 | header->size += event->id_header_size; |
| 5346 | |
| 5347 | if (sample_type & PERF_SAMPLE_TID) { |
| 5348 | /* namespace issues */ |
| 5349 | data->tid_entry.pid = perf_event_pid(event, current); |
| 5350 | data->tid_entry.tid = perf_event_tid(event, current); |
| 5351 | } |
| 5352 | |
| 5353 | if (sample_type & PERF_SAMPLE_TIME) |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 5354 | data->time = perf_event_clock(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5355 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5356 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5357 | data->id = primary_event_id(event); |
| 5358 | |
| 5359 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5360 | data->stream_id = event->id; |
| 5361 | |
| 5362 | if (sample_type & PERF_SAMPLE_CPU) { |
| 5363 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 5364 | data->cpu_entry.reserved = 0; |
| 5365 | } |
| 5366 | } |
| 5367 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5368 | void perf_event_header__init_id(struct perf_event_header *header, |
| 5369 | struct perf_sample_data *data, |
| 5370 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5371 | { |
| 5372 | if (event->attr.sample_id_all) |
| 5373 | __perf_event_header__init_id(header, data, event); |
| 5374 | } |
| 5375 | |
| 5376 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 5377 | struct perf_sample_data *data) |
| 5378 | { |
| 5379 | u64 sample_type = data->type; |
| 5380 | |
| 5381 | if (sample_type & PERF_SAMPLE_TID) |
| 5382 | perf_output_put(handle, data->tid_entry); |
| 5383 | |
| 5384 | if (sample_type & PERF_SAMPLE_TIME) |
| 5385 | perf_output_put(handle, data->time); |
| 5386 | |
| 5387 | if (sample_type & PERF_SAMPLE_ID) |
| 5388 | perf_output_put(handle, data->id); |
| 5389 | |
| 5390 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5391 | perf_output_put(handle, data->stream_id); |
| 5392 | |
| 5393 | if (sample_type & PERF_SAMPLE_CPU) |
| 5394 | perf_output_put(handle, data->cpu_entry); |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5395 | |
| 5396 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 5397 | perf_output_put(handle, data->id); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5398 | } |
| 5399 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5400 | void perf_event__output_id_sample(struct perf_event *event, |
| 5401 | struct perf_output_handle *handle, |
| 5402 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5403 | { |
| 5404 | if (event->attr.sample_id_all) |
| 5405 | __perf_event__output_id_sample(handle, sample); |
| 5406 | } |
| 5407 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5408 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5409 | struct perf_event *event, |
| 5410 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5411 | { |
| 5412 | u64 read_format = event->attr.read_format; |
| 5413 | u64 values[4]; |
| 5414 | int n = 0; |
| 5415 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5416 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5417 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5418 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5419 | atomic64_read(&event->child_total_time_enabled); |
| 5420 | } |
| 5421 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5422 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5423 | atomic64_read(&event->child_total_time_running); |
| 5424 | } |
| 5425 | if (read_format & PERF_FORMAT_ID) |
| 5426 | values[n++] = primary_event_id(event); |
| 5427 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5428 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5429 | } |
| 5430 | |
| 5431 | /* |
| 5432 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 5433 | */ |
| 5434 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5435 | struct perf_event *event, |
| 5436 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5437 | { |
| 5438 | struct perf_event *leader = event->group_leader, *sub; |
| 5439 | u64 read_format = event->attr.read_format; |
| 5440 | u64 values[5]; |
| 5441 | int n = 0; |
| 5442 | |
| 5443 | values[n++] = 1 + leader->nr_siblings; |
| 5444 | |
| 5445 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5446 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5447 | |
| 5448 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5449 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5450 | |
| 5451 | if (leader != event) |
| 5452 | leader->pmu->read(leader); |
| 5453 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5454 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5455 | if (read_format & PERF_FORMAT_ID) |
| 5456 | values[n++] = primary_event_id(leader); |
| 5457 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5458 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5459 | |
| 5460 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 5461 | n = 0; |
| 5462 | |
Jiri Olsa | 6f5ab00 | 2012-10-15 20:13:45 +0200 | [diff] [blame] | 5463 | if ((sub != event) && |
| 5464 | (sub->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5465 | sub->pmu->read(sub); |
| 5466 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5467 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5468 | if (read_format & PERF_FORMAT_ID) |
| 5469 | values[n++] = primary_event_id(sub); |
| 5470 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5471 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5472 | } |
| 5473 | } |
| 5474 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5475 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 5476 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 5477 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5478 | static void perf_output_read(struct perf_output_handle *handle, |
| 5479 | struct perf_event *event) |
| 5480 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 5481 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5482 | u64 read_format = event->attr.read_format; |
| 5483 | |
| 5484 | /* |
| 5485 | * compute total_time_enabled, total_time_running |
| 5486 | * based on snapshot values taken when the event |
| 5487 | * was last scheduled in. |
| 5488 | * |
| 5489 | * we cannot simply called update_context_time() |
| 5490 | * because of locking issue as we are called in |
| 5491 | * NMI context |
| 5492 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 5493 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 5494 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5495 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5496 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5497 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5498 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5499 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | void perf_output_sample(struct perf_output_handle *handle, |
| 5503 | struct perf_event_header *header, |
| 5504 | struct perf_sample_data *data, |
| 5505 | struct perf_event *event) |
| 5506 | { |
| 5507 | u64 sample_type = data->type; |
| 5508 | |
| 5509 | perf_output_put(handle, *header); |
| 5510 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5511 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 5512 | perf_output_put(handle, data->id); |
| 5513 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5514 | if (sample_type & PERF_SAMPLE_IP) |
| 5515 | perf_output_put(handle, data->ip); |
| 5516 | |
| 5517 | if (sample_type & PERF_SAMPLE_TID) |
| 5518 | perf_output_put(handle, data->tid_entry); |
| 5519 | |
| 5520 | if (sample_type & PERF_SAMPLE_TIME) |
| 5521 | perf_output_put(handle, data->time); |
| 5522 | |
| 5523 | if (sample_type & PERF_SAMPLE_ADDR) |
| 5524 | perf_output_put(handle, data->addr); |
| 5525 | |
| 5526 | if (sample_type & PERF_SAMPLE_ID) |
| 5527 | perf_output_put(handle, data->id); |
| 5528 | |
| 5529 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5530 | perf_output_put(handle, data->stream_id); |
| 5531 | |
| 5532 | if (sample_type & PERF_SAMPLE_CPU) |
| 5533 | perf_output_put(handle, data->cpu_entry); |
| 5534 | |
| 5535 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 5536 | perf_output_put(handle, data->period); |
| 5537 | |
| 5538 | if (sample_type & PERF_SAMPLE_READ) |
| 5539 | perf_output_read(handle, event); |
| 5540 | |
| 5541 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5542 | if (data->callchain) { |
| 5543 | int size = 1; |
| 5544 | |
| 5545 | if (data->callchain) |
| 5546 | size += data->callchain->nr; |
| 5547 | |
| 5548 | size *= sizeof(u64); |
| 5549 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5550 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5551 | } else { |
| 5552 | u64 nr = 0; |
| 5553 | perf_output_put(handle, nr); |
| 5554 | } |
| 5555 | } |
| 5556 | |
| 5557 | if (sample_type & PERF_SAMPLE_RAW) { |
| 5558 | if (data->raw) { |
Alexei Starovoitov | fa128e6 | 2015-10-20 20:02:33 -0700 | [diff] [blame] | 5559 | u32 raw_size = data->raw->size; |
| 5560 | u32 real_size = round_up(raw_size + sizeof(u32), |
| 5561 | sizeof(u64)) - sizeof(u32); |
| 5562 | u64 zero = 0; |
| 5563 | |
| 5564 | perf_output_put(handle, real_size); |
| 5565 | __output_copy(handle, data->raw->data, raw_size); |
| 5566 | if (real_size - raw_size) |
| 5567 | __output_copy(handle, &zero, real_size - raw_size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5568 | } else { |
| 5569 | struct { |
| 5570 | u32 size; |
| 5571 | u32 data; |
| 5572 | } raw = { |
| 5573 | .size = sizeof(u32), |
| 5574 | .data = 0, |
| 5575 | }; |
| 5576 | perf_output_put(handle, raw); |
| 5577 | } |
| 5578 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5579 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5580 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5581 | if (data->br_stack) { |
| 5582 | size_t size; |
| 5583 | |
| 5584 | size = data->br_stack->nr |
| 5585 | * sizeof(struct perf_branch_entry); |
| 5586 | |
| 5587 | perf_output_put(handle, data->br_stack->nr); |
| 5588 | perf_output_copy(handle, data->br_stack->entries, size); |
| 5589 | } else { |
| 5590 | /* |
| 5591 | * we always store at least the value of nr |
| 5592 | */ |
| 5593 | u64 nr = 0; |
| 5594 | perf_output_put(handle, nr); |
| 5595 | } |
| 5596 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5597 | |
| 5598 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5599 | u64 abi = data->regs_user.abi; |
| 5600 | |
| 5601 | /* |
| 5602 | * If there are no regs to dump, notice it through |
| 5603 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5604 | */ |
| 5605 | perf_output_put(handle, abi); |
| 5606 | |
| 5607 | if (abi) { |
| 5608 | u64 mask = event->attr.sample_regs_user; |
| 5609 | perf_output_sample_regs(handle, |
| 5610 | data->regs_user.regs, |
| 5611 | mask); |
| 5612 | } |
| 5613 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5614 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5615 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5616 | perf_output_sample_ustack(handle, |
| 5617 | data->stack_user_size, |
| 5618 | data->regs_user.regs); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5619 | } |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 5620 | |
| 5621 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 5622 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 5623 | |
| 5624 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 5625 | perf_output_put(handle, data->data_src.val); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5626 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 5627 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 5628 | perf_output_put(handle, data->txn); |
| 5629 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5630 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5631 | u64 abi = data->regs_intr.abi; |
| 5632 | /* |
| 5633 | * If there are no regs to dump, notice it through |
| 5634 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5635 | */ |
| 5636 | perf_output_put(handle, abi); |
| 5637 | |
| 5638 | if (abi) { |
| 5639 | u64 mask = event->attr.sample_regs_intr; |
| 5640 | |
| 5641 | perf_output_sample_regs(handle, |
| 5642 | data->regs_intr.regs, |
| 5643 | mask); |
| 5644 | } |
| 5645 | } |
| 5646 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5647 | if (!event->attr.watermark) { |
| 5648 | int wakeup_events = event->attr.wakeup_events; |
| 5649 | |
| 5650 | if (wakeup_events) { |
| 5651 | struct ring_buffer *rb = handle->rb; |
| 5652 | int events = local_inc_return(&rb->events); |
| 5653 | |
| 5654 | if (events >= wakeup_events) { |
| 5655 | local_sub(wakeup_events, &rb->events); |
| 5656 | local_inc(&rb->wakeup); |
| 5657 | } |
| 5658 | } |
| 5659 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | void perf_prepare_sample(struct perf_event_header *header, |
| 5663 | struct perf_sample_data *data, |
| 5664 | struct perf_event *event, |
| 5665 | struct pt_regs *regs) |
| 5666 | { |
| 5667 | u64 sample_type = event->attr.sample_type; |
| 5668 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5669 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5670 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5671 | |
| 5672 | header->misc = 0; |
| 5673 | header->misc |= perf_misc_flags(regs); |
| 5674 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5675 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5676 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5677 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5678 | data->ip = perf_instruction_pointer(regs); |
| 5679 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5680 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5681 | int size = 1; |
| 5682 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5683 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5684 | |
| 5685 | if (data->callchain) |
| 5686 | size += data->callchain->nr; |
| 5687 | |
| 5688 | header->size += size * sizeof(u64); |
| 5689 | } |
| 5690 | |
| 5691 | if (sample_type & PERF_SAMPLE_RAW) { |
| 5692 | int size = sizeof(u32); |
| 5693 | |
| 5694 | if (data->raw) |
| 5695 | size += data->raw->size; |
| 5696 | else |
| 5697 | size += sizeof(u32); |
| 5698 | |
Alexei Starovoitov | fa128e6 | 2015-10-20 20:02:33 -0700 | [diff] [blame] | 5699 | header->size += round_up(size, sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5700 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5701 | |
| 5702 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5703 | int size = sizeof(u64); /* nr */ |
| 5704 | if (data->br_stack) { |
| 5705 | size += data->br_stack->nr |
| 5706 | * sizeof(struct perf_branch_entry); |
| 5707 | } |
| 5708 | header->size += size; |
| 5709 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5710 | |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5711 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5712 | perf_sample_regs_user(&data->regs_user, regs, |
| 5713 | &data->regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5714 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5715 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5716 | /* regs dump ABI info */ |
| 5717 | int size = sizeof(u64); |
| 5718 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5719 | if (data->regs_user.regs) { |
| 5720 | u64 mask = event->attr.sample_regs_user; |
| 5721 | size += hweight64(mask) * sizeof(u64); |
| 5722 | } |
| 5723 | |
| 5724 | header->size += size; |
| 5725 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5726 | |
| 5727 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 5728 | /* |
| 5729 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 5730 | * processed as the last one or have additional check added |
| 5731 | * in case new sample type is added, because we could eat |
| 5732 | * up the rest of the sample size. |
| 5733 | */ |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5734 | u16 stack_size = event->attr.sample_stack_user; |
| 5735 | u16 size = sizeof(u64); |
| 5736 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5737 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5738 | data->regs_user.regs); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5739 | |
| 5740 | /* |
| 5741 | * If there is something to dump, add space for the dump |
| 5742 | * itself and for the field that tells the dynamic size, |
| 5743 | * which is how many have been actually dumped. |
| 5744 | */ |
| 5745 | if (stack_size) |
| 5746 | size += sizeof(u64) + stack_size; |
| 5747 | |
| 5748 | data->stack_user_size = stack_size; |
| 5749 | header->size += size; |
| 5750 | } |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5751 | |
| 5752 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5753 | /* regs dump ABI info */ |
| 5754 | int size = sizeof(u64); |
| 5755 | |
| 5756 | perf_sample_regs_intr(&data->regs_intr, regs); |
| 5757 | |
| 5758 | if (data->regs_intr.regs) { |
| 5759 | u64 mask = event->attr.sample_regs_intr; |
| 5760 | |
| 5761 | size += hweight64(mask) * sizeof(u64); |
| 5762 | } |
| 5763 | |
| 5764 | header->size += size; |
| 5765 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5766 | } |
| 5767 | |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 5768 | static void __always_inline |
| 5769 | __perf_event_output(struct perf_event *event, |
| 5770 | struct perf_sample_data *data, |
| 5771 | struct pt_regs *regs, |
| 5772 | int (*output_begin)(struct perf_output_handle *, |
| 5773 | struct perf_event *, |
| 5774 | unsigned int)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5775 | { |
| 5776 | struct perf_output_handle handle; |
| 5777 | struct perf_event_header header; |
| 5778 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5779 | /* protect the callchain buffers */ |
| 5780 | rcu_read_lock(); |
| 5781 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5782 | perf_prepare_sample(&header, data, event, regs); |
| 5783 | |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 5784 | if (output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5785 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5786 | |
| 5787 | perf_output_sample(&handle, &header, data, event); |
| 5788 | |
| 5789 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5790 | |
| 5791 | exit: |
| 5792 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5793 | } |
| 5794 | |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 5795 | void |
| 5796 | perf_event_output_forward(struct perf_event *event, |
| 5797 | struct perf_sample_data *data, |
| 5798 | struct pt_regs *regs) |
| 5799 | { |
| 5800 | __perf_event_output(event, data, regs, perf_output_begin_forward); |
| 5801 | } |
| 5802 | |
| 5803 | void |
| 5804 | perf_event_output_backward(struct perf_event *event, |
| 5805 | struct perf_sample_data *data, |
| 5806 | struct pt_regs *regs) |
| 5807 | { |
| 5808 | __perf_event_output(event, data, regs, perf_output_begin_backward); |
| 5809 | } |
| 5810 | |
| 5811 | void |
| 5812 | perf_event_output(struct perf_event *event, |
| 5813 | struct perf_sample_data *data, |
| 5814 | struct pt_regs *regs) |
| 5815 | { |
| 5816 | __perf_event_output(event, data, regs, perf_output_begin); |
| 5817 | } |
| 5818 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5819 | /* |
| 5820 | * read event_id |
| 5821 | */ |
| 5822 | |
| 5823 | struct perf_read_event { |
| 5824 | struct perf_event_header header; |
| 5825 | |
| 5826 | u32 pid; |
| 5827 | u32 tid; |
| 5828 | }; |
| 5829 | |
| 5830 | static void |
| 5831 | perf_event_read_event(struct perf_event *event, |
| 5832 | struct task_struct *task) |
| 5833 | { |
| 5834 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5835 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5836 | struct perf_read_event read_event = { |
| 5837 | .header = { |
| 5838 | .type = PERF_RECORD_READ, |
| 5839 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5840 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5841 | }, |
| 5842 | .pid = perf_event_pid(event, task), |
| 5843 | .tid = perf_event_tid(event, task), |
| 5844 | }; |
| 5845 | int ret; |
| 5846 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5847 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5848 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5849 | if (ret) |
| 5850 | return; |
| 5851 | |
| 5852 | perf_output_put(&handle, read_event); |
| 5853 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5854 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5855 | |
| 5856 | perf_output_end(&handle); |
| 5857 | } |
| 5858 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5859 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
| 5860 | |
| 5861 | static void |
| 5862 | perf_event_aux_ctx(struct perf_event_context *ctx, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5863 | perf_event_aux_output_cb output, |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 5864 | void *data, bool all) |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5865 | { |
| 5866 | struct perf_event *event; |
| 5867 | |
| 5868 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 5869 | if (!all) { |
| 5870 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 5871 | continue; |
| 5872 | if (!event_filter_match(event)) |
| 5873 | continue; |
| 5874 | } |
| 5875 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5876 | output(event, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5877 | } |
| 5878 | } |
| 5879 | |
| 5880 | static void |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 5881 | perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data, |
| 5882 | struct perf_event_context *task_ctx) |
| 5883 | { |
| 5884 | rcu_read_lock(); |
| 5885 | preempt_disable(); |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 5886 | perf_event_aux_ctx(task_ctx, output, data, false); |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 5887 | preempt_enable(); |
| 5888 | rcu_read_unlock(); |
| 5889 | } |
| 5890 | |
| 5891 | static void |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5892 | perf_event_aux(perf_event_aux_output_cb output, void *data, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5893 | struct perf_event_context *task_ctx) |
| 5894 | { |
| 5895 | struct perf_cpu_context *cpuctx; |
| 5896 | struct perf_event_context *ctx; |
| 5897 | struct pmu *pmu; |
| 5898 | int ctxn; |
| 5899 | |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 5900 | /* |
| 5901 | * If we have task_ctx != NULL we only notify |
| 5902 | * the task context itself. The task_ctx is set |
| 5903 | * only for EXIT events before releasing task |
| 5904 | * context. |
| 5905 | */ |
| 5906 | if (task_ctx) { |
| 5907 | perf_event_aux_task_ctx(output, data, task_ctx); |
| 5908 | return; |
| 5909 | } |
| 5910 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5911 | rcu_read_lock(); |
| 5912 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 5913 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 5914 | if (cpuctx->unique_pmu != pmu) |
| 5915 | goto next; |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 5916 | perf_event_aux_ctx(&cpuctx->ctx, output, data, false); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5917 | ctxn = pmu->task_ctx_nr; |
| 5918 | if (ctxn < 0) |
| 5919 | goto next; |
| 5920 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 5921 | if (ctx) |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 5922 | perf_event_aux_ctx(ctx, output, data, false); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5923 | next: |
| 5924 | put_cpu_ptr(pmu->pmu_cpu_context); |
| 5925 | } |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5926 | rcu_read_unlock(); |
| 5927 | } |
| 5928 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 5929 | /* |
| 5930 | * Clear all file-based filters at exec, they'll have to be |
| 5931 | * re-instated when/if these objects are mmapped again. |
| 5932 | */ |
| 5933 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) |
| 5934 | { |
| 5935 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 5936 | struct perf_addr_filter *filter; |
| 5937 | unsigned int restart = 0, count = 0; |
| 5938 | unsigned long flags; |
| 5939 | |
| 5940 | if (!has_addr_filter(event)) |
| 5941 | return; |
| 5942 | |
| 5943 | raw_spin_lock_irqsave(&ifh->lock, flags); |
| 5944 | list_for_each_entry(filter, &ifh->list, entry) { |
| 5945 | if (filter->inode) { |
| 5946 | event->addr_filters_offs[count] = 0; |
| 5947 | restart++; |
| 5948 | } |
| 5949 | |
| 5950 | count++; |
| 5951 | } |
| 5952 | |
| 5953 | if (restart) |
| 5954 | event->addr_filters_gen++; |
| 5955 | raw_spin_unlock_irqrestore(&ifh->lock, flags); |
| 5956 | |
| 5957 | if (restart) |
| 5958 | perf_event_restart(event); |
| 5959 | } |
| 5960 | |
| 5961 | void perf_event_exec(void) |
| 5962 | { |
| 5963 | struct perf_event_context *ctx; |
| 5964 | int ctxn; |
| 5965 | |
| 5966 | rcu_read_lock(); |
| 5967 | for_each_task_context_nr(ctxn) { |
| 5968 | ctx = current->perf_event_ctxp[ctxn]; |
| 5969 | if (!ctx) |
| 5970 | continue; |
| 5971 | |
| 5972 | perf_event_enable_on_exec(ctxn); |
| 5973 | |
| 5974 | perf_event_aux_ctx(ctx, perf_event_addr_filters_exec, NULL, |
| 5975 | true); |
| 5976 | } |
| 5977 | rcu_read_unlock(); |
| 5978 | } |
| 5979 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 5980 | struct remote_output { |
| 5981 | struct ring_buffer *rb; |
| 5982 | int err; |
| 5983 | }; |
| 5984 | |
| 5985 | static void __perf_event_output_stop(struct perf_event *event, void *data) |
| 5986 | { |
| 5987 | struct perf_event *parent = event->parent; |
| 5988 | struct remote_output *ro = data; |
| 5989 | struct ring_buffer *rb = ro->rb; |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 5990 | struct stop_event_data sd = { |
| 5991 | .event = event, |
| 5992 | }; |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 5993 | |
| 5994 | if (!has_aux(event)) |
| 5995 | return; |
| 5996 | |
| 5997 | if (!parent) |
| 5998 | parent = event; |
| 5999 | |
| 6000 | /* |
| 6001 | * In case of inheritance, it will be the parent that links to the |
| 6002 | * ring-buffer, but it will be the child that's actually using it: |
| 6003 | */ |
| 6004 | if (rcu_dereference(parent->rb) == rb) |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6005 | ro->err = __perf_event_stop(&sd); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6006 | } |
| 6007 | |
| 6008 | static int __perf_pmu_output_stop(void *info) |
| 6009 | { |
| 6010 | struct perf_event *event = info; |
| 6011 | struct pmu *pmu = event->pmu; |
| 6012 | struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 6013 | struct remote_output ro = { |
| 6014 | .rb = event->rb, |
| 6015 | }; |
| 6016 | |
| 6017 | rcu_read_lock(); |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 6018 | perf_event_aux_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6019 | if (cpuctx->task_ctx) |
| 6020 | perf_event_aux_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 6021 | &ro, false); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6022 | rcu_read_unlock(); |
| 6023 | |
| 6024 | return ro.err; |
| 6025 | } |
| 6026 | |
| 6027 | static void perf_pmu_output_stop(struct perf_event *event) |
| 6028 | { |
| 6029 | struct perf_event *iter; |
| 6030 | int err, cpu; |
| 6031 | |
| 6032 | restart: |
| 6033 | rcu_read_lock(); |
| 6034 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { |
| 6035 | /* |
| 6036 | * For per-CPU events, we need to make sure that neither they |
| 6037 | * nor their children are running; for cpu==-1 events it's |
| 6038 | * sufficient to stop the event itself if it's active, since |
| 6039 | * it can't have children. |
| 6040 | */ |
| 6041 | cpu = iter->cpu; |
| 6042 | if (cpu == -1) |
| 6043 | cpu = READ_ONCE(iter->oncpu); |
| 6044 | |
| 6045 | if (cpu == -1) |
| 6046 | continue; |
| 6047 | |
| 6048 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); |
| 6049 | if (err == -EAGAIN) { |
| 6050 | rcu_read_unlock(); |
| 6051 | goto restart; |
| 6052 | } |
| 6053 | } |
| 6054 | rcu_read_unlock(); |
| 6055 | } |
| 6056 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6057 | /* |
| 6058 | * task tracking -- fork/exit |
| 6059 | * |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6060 | * 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] | 6061 | */ |
| 6062 | |
| 6063 | struct perf_task_event { |
| 6064 | struct task_struct *task; |
| 6065 | struct perf_event_context *task_ctx; |
| 6066 | |
| 6067 | struct { |
| 6068 | struct perf_event_header header; |
| 6069 | |
| 6070 | u32 pid; |
| 6071 | u32 ppid; |
| 6072 | u32 tid; |
| 6073 | u32 ptid; |
| 6074 | u64 time; |
| 6075 | } event_id; |
| 6076 | }; |
| 6077 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6078 | static int perf_event_task_match(struct perf_event *event) |
| 6079 | { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6080 | return event->attr.comm || event->attr.mmap || |
| 6081 | event->attr.mmap2 || event->attr.mmap_data || |
| 6082 | event->attr.task; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6083 | } |
| 6084 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6085 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6086 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6087 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6088 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6089 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6090 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6091 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6092 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 6093 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6094 | if (!perf_event_task_match(event)) |
| 6095 | return; |
| 6096 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6097 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6098 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6099 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6100 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 6101 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6102 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6103 | |
| 6104 | task_event->event_id.pid = perf_event_pid(event, task); |
| 6105 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 6106 | |
| 6107 | task_event->event_id.tid = perf_event_tid(event, task); |
| 6108 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 6109 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6110 | task_event->event_id.time = perf_event_clock(event); |
| 6111 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6112 | perf_output_put(&handle, task_event->event_id); |
| 6113 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6114 | perf_event__output_id_sample(event, &handle, &sample); |
| 6115 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6116 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6117 | out: |
| 6118 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6119 | } |
| 6120 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6121 | static void perf_event_task(struct task_struct *task, |
| 6122 | struct perf_event_context *task_ctx, |
| 6123 | int new) |
| 6124 | { |
| 6125 | struct perf_task_event task_event; |
| 6126 | |
| 6127 | if (!atomic_read(&nr_comm_events) && |
| 6128 | !atomic_read(&nr_mmap_events) && |
| 6129 | !atomic_read(&nr_task_events)) |
| 6130 | return; |
| 6131 | |
| 6132 | task_event = (struct perf_task_event){ |
| 6133 | .task = task, |
| 6134 | .task_ctx = task_ctx, |
| 6135 | .event_id = { |
| 6136 | .header = { |
| 6137 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 6138 | .misc = 0, |
| 6139 | .size = sizeof(task_event.event_id), |
| 6140 | }, |
| 6141 | /* .pid */ |
| 6142 | /* .ppid */ |
| 6143 | /* .tid */ |
| 6144 | /* .ptid */ |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6145 | /* .time */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6146 | }, |
| 6147 | }; |
| 6148 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6149 | perf_event_aux(perf_event_task_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6150 | &task_event, |
| 6151 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | void perf_event_fork(struct task_struct *task) |
| 6155 | { |
| 6156 | perf_event_task(task, NULL, 1); |
| 6157 | } |
| 6158 | |
| 6159 | /* |
| 6160 | * comm tracking |
| 6161 | */ |
| 6162 | |
| 6163 | struct perf_comm_event { |
| 6164 | struct task_struct *task; |
| 6165 | char *comm; |
| 6166 | int comm_size; |
| 6167 | |
| 6168 | struct { |
| 6169 | struct perf_event_header header; |
| 6170 | |
| 6171 | u32 pid; |
| 6172 | u32 tid; |
| 6173 | } event_id; |
| 6174 | }; |
| 6175 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6176 | static int perf_event_comm_match(struct perf_event *event) |
| 6177 | { |
| 6178 | return event->attr.comm; |
| 6179 | } |
| 6180 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6181 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6182 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6183 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6184 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6185 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6186 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6187 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6188 | int ret; |
| 6189 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6190 | if (!perf_event_comm_match(event)) |
| 6191 | return; |
| 6192 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6193 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 6194 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6195 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6196 | |
| 6197 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6198 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6199 | |
| 6200 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 6201 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 6202 | |
| 6203 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6204 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6205 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6206 | |
| 6207 | perf_event__output_id_sample(event, &handle, &sample); |
| 6208 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6209 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6210 | out: |
| 6211 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6212 | } |
| 6213 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6214 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 6215 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6216 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6217 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6218 | |
| 6219 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 6220 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6221 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 6222 | |
| 6223 | comm_event->comm = comm; |
| 6224 | comm_event->comm_size = size; |
| 6225 | |
| 6226 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6227 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6228 | perf_event_aux(perf_event_comm_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6229 | comm_event, |
| 6230 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6231 | } |
| 6232 | |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 6233 | void perf_event_comm(struct task_struct *task, bool exec) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6234 | { |
| 6235 | struct perf_comm_event comm_event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6236 | |
| 6237 | if (!atomic_read(&nr_comm_events)) |
| 6238 | return; |
| 6239 | |
| 6240 | comm_event = (struct perf_comm_event){ |
| 6241 | .task = task, |
| 6242 | /* .comm */ |
| 6243 | /* .comm_size */ |
| 6244 | .event_id = { |
| 6245 | .header = { |
| 6246 | .type = PERF_RECORD_COMM, |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 6247 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6248 | /* .size */ |
| 6249 | }, |
| 6250 | /* .pid */ |
| 6251 | /* .tid */ |
| 6252 | }, |
| 6253 | }; |
| 6254 | |
| 6255 | perf_event_comm_event(&comm_event); |
| 6256 | } |
| 6257 | |
| 6258 | /* |
| 6259 | * mmap tracking |
| 6260 | */ |
| 6261 | |
| 6262 | struct perf_mmap_event { |
| 6263 | struct vm_area_struct *vma; |
| 6264 | |
| 6265 | const char *file_name; |
| 6266 | int file_size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6267 | int maj, min; |
| 6268 | u64 ino; |
| 6269 | u64 ino_generation; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6270 | u32 prot, flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6271 | |
| 6272 | struct { |
| 6273 | struct perf_event_header header; |
| 6274 | |
| 6275 | u32 pid; |
| 6276 | u32 tid; |
| 6277 | u64 start; |
| 6278 | u64 len; |
| 6279 | u64 pgoff; |
| 6280 | } event_id; |
| 6281 | }; |
| 6282 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6283 | static int perf_event_mmap_match(struct perf_event *event, |
| 6284 | void *data) |
| 6285 | { |
| 6286 | struct perf_mmap_event *mmap_event = data; |
| 6287 | struct vm_area_struct *vma = mmap_event->vma; |
| 6288 | int executable = vma->vm_flags & VM_EXEC; |
| 6289 | |
| 6290 | return (!executable && event->attr.mmap_data) || |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6291 | (executable && (event->attr.mmap || event->attr.mmap2)); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6292 | } |
| 6293 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6294 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6295 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6296 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6297 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6298 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6299 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6300 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6301 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6302 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6303 | if (!perf_event_mmap_match(event, data)) |
| 6304 | return; |
| 6305 | |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6306 | if (event->attr.mmap2) { |
| 6307 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; |
| 6308 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); |
| 6309 | mmap_event->event_id.header.size += sizeof(mmap_event->min); |
| 6310 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); |
Arnaldo Carvalho de Melo | d008d52 | 2013-09-10 10:24:05 -0300 | [diff] [blame] | 6311 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6312 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
| 6313 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6314 | } |
| 6315 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6316 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 6317 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6318 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6319 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6320 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6321 | |
| 6322 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 6323 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 6324 | |
| 6325 | perf_output_put(&handle, mmap_event->event_id); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6326 | |
| 6327 | if (event->attr.mmap2) { |
| 6328 | perf_output_put(&handle, mmap_event->maj); |
| 6329 | perf_output_put(&handle, mmap_event->min); |
| 6330 | perf_output_put(&handle, mmap_event->ino); |
| 6331 | perf_output_put(&handle, mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6332 | perf_output_put(&handle, mmap_event->prot); |
| 6333 | perf_output_put(&handle, mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6334 | } |
| 6335 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6336 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6337 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6338 | |
| 6339 | perf_event__output_id_sample(event, &handle, &sample); |
| 6340 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6341 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6342 | out: |
| 6343 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6344 | } |
| 6345 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6346 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 6347 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6348 | struct vm_area_struct *vma = mmap_event->vma; |
| 6349 | struct file *file = vma->vm_file; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6350 | int maj = 0, min = 0; |
| 6351 | u64 ino = 0, gen = 0; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6352 | u32 prot = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6353 | unsigned int size; |
| 6354 | char tmp[16]; |
| 6355 | char *buf = NULL; |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6356 | char *name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6357 | |
| 6358 | if (file) { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6359 | struct inode *inode; |
| 6360 | dev_t dev; |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 6361 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6362 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6363 | if (!buf) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6364 | name = "//enomem"; |
| 6365 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6366 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6367 | /* |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 6368 | * d_path() works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6369 | * need to add enough zero bytes after the string to handle |
| 6370 | * the 64bit alignment we do later. |
| 6371 | */ |
Miklos Szeredi | 9bf39ab | 2015-06-19 10:29:13 +0200 | [diff] [blame] | 6372 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6373 | if (IS_ERR(name)) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6374 | name = "//toolong"; |
| 6375 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6376 | } |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6377 | inode = file_inode(vma->vm_file); |
| 6378 | dev = inode->i_sb->s_dev; |
| 6379 | ino = inode->i_ino; |
| 6380 | gen = inode->i_generation; |
| 6381 | maj = MAJOR(dev); |
| 6382 | min = MINOR(dev); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6383 | |
| 6384 | if (vma->vm_flags & VM_READ) |
| 6385 | prot |= PROT_READ; |
| 6386 | if (vma->vm_flags & VM_WRITE) |
| 6387 | prot |= PROT_WRITE; |
| 6388 | if (vma->vm_flags & VM_EXEC) |
| 6389 | prot |= PROT_EXEC; |
| 6390 | |
| 6391 | if (vma->vm_flags & VM_MAYSHARE) |
| 6392 | flags = MAP_SHARED; |
| 6393 | else |
| 6394 | flags = MAP_PRIVATE; |
| 6395 | |
| 6396 | if (vma->vm_flags & VM_DENYWRITE) |
| 6397 | flags |= MAP_DENYWRITE; |
| 6398 | if (vma->vm_flags & VM_MAYEXEC) |
| 6399 | flags |= MAP_EXECUTABLE; |
| 6400 | if (vma->vm_flags & VM_LOCKED) |
| 6401 | flags |= MAP_LOCKED; |
| 6402 | if (vma->vm_flags & VM_HUGETLB) |
| 6403 | flags |= MAP_HUGETLB; |
| 6404 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6405 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6406 | } else { |
Jiri Olsa | fbe26ab | 2014-07-14 17:57:19 +0200 | [diff] [blame] | 6407 | if (vma->vm_ops && vma->vm_ops->name) { |
| 6408 | name = (char *) vma->vm_ops->name(vma); |
| 6409 | if (name) |
| 6410 | goto cpy_name; |
| 6411 | } |
| 6412 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6413 | name = (char *)arch_vma_name(vma); |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6414 | if (name) |
| 6415 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6416 | |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 6417 | if (vma->vm_start <= vma->vm_mm->start_brk && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6418 | vma->vm_end >= vma->vm_mm->brk) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6419 | name = "[heap]"; |
| 6420 | goto cpy_name; |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 6421 | } |
| 6422 | if (vma->vm_start <= vma->vm_mm->start_stack && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6423 | vma->vm_end >= vma->vm_mm->start_stack) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6424 | name = "[stack]"; |
| 6425 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6426 | } |
| 6427 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6428 | name = "//anon"; |
| 6429 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6430 | } |
| 6431 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6432 | cpy_name: |
| 6433 | strlcpy(tmp, name, sizeof(tmp)); |
| 6434 | name = tmp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6435 | got_name: |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6436 | /* |
| 6437 | * Since our buffer works in 8 byte units we need to align our string |
| 6438 | * size to a multiple of 8. However, we must guarantee the tail end is |
| 6439 | * zero'd out to avoid leaking random bits to userspace. |
| 6440 | */ |
| 6441 | size = strlen(name)+1; |
| 6442 | while (!IS_ALIGNED(size, sizeof(u64))) |
| 6443 | name[size++] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6444 | |
| 6445 | mmap_event->file_name = name; |
| 6446 | mmap_event->file_size = size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6447 | mmap_event->maj = maj; |
| 6448 | mmap_event->min = min; |
| 6449 | mmap_event->ino = ino; |
| 6450 | mmap_event->ino_generation = gen; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6451 | mmap_event->prot = prot; |
| 6452 | mmap_event->flags = flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6453 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 6454 | if (!(vma->vm_flags & VM_EXEC)) |
| 6455 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 6456 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6457 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 6458 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6459 | perf_event_aux(perf_event_mmap_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6460 | mmap_event, |
| 6461 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6462 | |
| 6463 | kfree(buf); |
| 6464 | } |
| 6465 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6466 | /* |
| 6467 | * Whether this @filter depends on a dynamic object which is not loaded |
| 6468 | * yet or its load addresses are not known. |
| 6469 | */ |
| 6470 | static bool perf_addr_filter_needs_mmap(struct perf_addr_filter *filter) |
| 6471 | { |
| 6472 | return filter->filter && filter->inode; |
| 6473 | } |
| 6474 | |
| 6475 | /* |
| 6476 | * Check whether inode and address range match filter criteria. |
| 6477 | */ |
| 6478 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, |
| 6479 | struct file *file, unsigned long offset, |
| 6480 | unsigned long size) |
| 6481 | { |
| 6482 | if (filter->inode != file->f_inode) |
| 6483 | return false; |
| 6484 | |
| 6485 | if (filter->offset > offset + size) |
| 6486 | return false; |
| 6487 | |
| 6488 | if (filter->offset + filter->size < offset) |
| 6489 | return false; |
| 6490 | |
| 6491 | return true; |
| 6492 | } |
| 6493 | |
| 6494 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) |
| 6495 | { |
| 6496 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 6497 | struct vm_area_struct *vma = data; |
| 6498 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags; |
| 6499 | struct file *file = vma->vm_file; |
| 6500 | struct perf_addr_filter *filter; |
| 6501 | unsigned int restart = 0, count = 0; |
| 6502 | |
| 6503 | if (!has_addr_filter(event)) |
| 6504 | return; |
| 6505 | |
| 6506 | if (!file) |
| 6507 | return; |
| 6508 | |
| 6509 | raw_spin_lock_irqsave(&ifh->lock, flags); |
| 6510 | list_for_each_entry(filter, &ifh->list, entry) { |
| 6511 | if (perf_addr_filter_match(filter, file, off, |
| 6512 | vma->vm_end - vma->vm_start)) { |
| 6513 | event->addr_filters_offs[count] = vma->vm_start; |
| 6514 | restart++; |
| 6515 | } |
| 6516 | |
| 6517 | count++; |
| 6518 | } |
| 6519 | |
| 6520 | if (restart) |
| 6521 | event->addr_filters_gen++; |
| 6522 | raw_spin_unlock_irqrestore(&ifh->lock, flags); |
| 6523 | |
| 6524 | if (restart) |
| 6525 | perf_event_restart(event); |
| 6526 | } |
| 6527 | |
| 6528 | /* |
| 6529 | * Adjust all task's events' filters to the new vma |
| 6530 | */ |
| 6531 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) |
| 6532 | { |
| 6533 | struct perf_event_context *ctx; |
| 6534 | int ctxn; |
| 6535 | |
| 6536 | rcu_read_lock(); |
| 6537 | for_each_task_context_nr(ctxn) { |
| 6538 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 6539 | if (!ctx) |
| 6540 | continue; |
| 6541 | |
| 6542 | perf_event_aux_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
| 6543 | } |
| 6544 | rcu_read_unlock(); |
| 6545 | } |
| 6546 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 6547 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6548 | { |
| 6549 | struct perf_mmap_event mmap_event; |
| 6550 | |
| 6551 | if (!atomic_read(&nr_mmap_events)) |
| 6552 | return; |
| 6553 | |
| 6554 | mmap_event = (struct perf_mmap_event){ |
| 6555 | .vma = vma, |
| 6556 | /* .file_name */ |
| 6557 | /* .file_size */ |
| 6558 | .event_id = { |
| 6559 | .header = { |
| 6560 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 6561 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6562 | /* .size */ |
| 6563 | }, |
| 6564 | /* .pid */ |
| 6565 | /* .tid */ |
| 6566 | .start = vma->vm_start, |
| 6567 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 6568 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6569 | }, |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6570 | /* .maj (attr_mmap2 only) */ |
| 6571 | /* .min (attr_mmap2 only) */ |
| 6572 | /* .ino (attr_mmap2 only) */ |
| 6573 | /* .ino_generation (attr_mmap2 only) */ |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6574 | /* .prot (attr_mmap2 only) */ |
| 6575 | /* .flags (attr_mmap2 only) */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6576 | }; |
| 6577 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6578 | perf_addr_filters_adjust(vma); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6579 | perf_event_mmap_event(&mmap_event); |
| 6580 | } |
| 6581 | |
Alexander Shishkin | 68db7e9 | 2015-01-14 14:18:15 +0200 | [diff] [blame] | 6582 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
| 6583 | unsigned long size, u64 flags) |
| 6584 | { |
| 6585 | struct perf_output_handle handle; |
| 6586 | struct perf_sample_data sample; |
| 6587 | struct perf_aux_event { |
| 6588 | struct perf_event_header header; |
| 6589 | u64 offset; |
| 6590 | u64 size; |
| 6591 | u64 flags; |
| 6592 | } rec = { |
| 6593 | .header = { |
| 6594 | .type = PERF_RECORD_AUX, |
| 6595 | .misc = 0, |
| 6596 | .size = sizeof(rec), |
| 6597 | }, |
| 6598 | .offset = head, |
| 6599 | .size = size, |
| 6600 | .flags = flags, |
| 6601 | }; |
| 6602 | int ret; |
| 6603 | |
| 6604 | perf_event_header__init_id(&rec.header, &sample, event); |
| 6605 | ret = perf_output_begin(&handle, event, rec.header.size); |
| 6606 | |
| 6607 | if (ret) |
| 6608 | return; |
| 6609 | |
| 6610 | perf_output_put(&handle, rec); |
| 6611 | perf_event__output_id_sample(event, &handle, &sample); |
| 6612 | |
| 6613 | perf_output_end(&handle); |
| 6614 | } |
| 6615 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6616 | /* |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 6617 | * Lost/dropped samples logging |
| 6618 | */ |
| 6619 | void perf_log_lost_samples(struct perf_event *event, u64 lost) |
| 6620 | { |
| 6621 | struct perf_output_handle handle; |
| 6622 | struct perf_sample_data sample; |
| 6623 | int ret; |
| 6624 | |
| 6625 | struct { |
| 6626 | struct perf_event_header header; |
| 6627 | u64 lost; |
| 6628 | } lost_samples_event = { |
| 6629 | .header = { |
| 6630 | .type = PERF_RECORD_LOST_SAMPLES, |
| 6631 | .misc = 0, |
| 6632 | .size = sizeof(lost_samples_event), |
| 6633 | }, |
| 6634 | .lost = lost, |
| 6635 | }; |
| 6636 | |
| 6637 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); |
| 6638 | |
| 6639 | ret = perf_output_begin(&handle, event, |
| 6640 | lost_samples_event.header.size); |
| 6641 | if (ret) |
| 6642 | return; |
| 6643 | |
| 6644 | perf_output_put(&handle, lost_samples_event); |
| 6645 | perf_event__output_id_sample(event, &handle, &sample); |
| 6646 | perf_output_end(&handle); |
| 6647 | } |
| 6648 | |
| 6649 | /* |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 6650 | * context_switch tracking |
| 6651 | */ |
| 6652 | |
| 6653 | struct perf_switch_event { |
| 6654 | struct task_struct *task; |
| 6655 | struct task_struct *next_prev; |
| 6656 | |
| 6657 | struct { |
| 6658 | struct perf_event_header header; |
| 6659 | u32 next_prev_pid; |
| 6660 | u32 next_prev_tid; |
| 6661 | } event_id; |
| 6662 | }; |
| 6663 | |
| 6664 | static int perf_event_switch_match(struct perf_event *event) |
| 6665 | { |
| 6666 | return event->attr.context_switch; |
| 6667 | } |
| 6668 | |
| 6669 | static void perf_event_switch_output(struct perf_event *event, void *data) |
| 6670 | { |
| 6671 | struct perf_switch_event *se = data; |
| 6672 | struct perf_output_handle handle; |
| 6673 | struct perf_sample_data sample; |
| 6674 | int ret; |
| 6675 | |
| 6676 | if (!perf_event_switch_match(event)) |
| 6677 | return; |
| 6678 | |
| 6679 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ |
| 6680 | if (event->ctx->task) { |
| 6681 | se->event_id.header.type = PERF_RECORD_SWITCH; |
| 6682 | se->event_id.header.size = sizeof(se->event_id.header); |
| 6683 | } else { |
| 6684 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; |
| 6685 | se->event_id.header.size = sizeof(se->event_id); |
| 6686 | se->event_id.next_prev_pid = |
| 6687 | perf_event_pid(event, se->next_prev); |
| 6688 | se->event_id.next_prev_tid = |
| 6689 | perf_event_tid(event, se->next_prev); |
| 6690 | } |
| 6691 | |
| 6692 | perf_event_header__init_id(&se->event_id.header, &sample, event); |
| 6693 | |
| 6694 | ret = perf_output_begin(&handle, event, se->event_id.header.size); |
| 6695 | if (ret) |
| 6696 | return; |
| 6697 | |
| 6698 | if (event->ctx->task) |
| 6699 | perf_output_put(&handle, se->event_id.header); |
| 6700 | else |
| 6701 | perf_output_put(&handle, se->event_id); |
| 6702 | |
| 6703 | perf_event__output_id_sample(event, &handle, &sample); |
| 6704 | |
| 6705 | perf_output_end(&handle); |
| 6706 | } |
| 6707 | |
| 6708 | static void perf_event_switch(struct task_struct *task, |
| 6709 | struct task_struct *next_prev, bool sched_in) |
| 6710 | { |
| 6711 | struct perf_switch_event switch_event; |
| 6712 | |
| 6713 | /* N.B. caller checks nr_switch_events != 0 */ |
| 6714 | |
| 6715 | switch_event = (struct perf_switch_event){ |
| 6716 | .task = task, |
| 6717 | .next_prev = next_prev, |
| 6718 | .event_id = { |
| 6719 | .header = { |
| 6720 | /* .type */ |
| 6721 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, |
| 6722 | /* .size */ |
| 6723 | }, |
| 6724 | /* .next_prev_pid */ |
| 6725 | /* .next_prev_tid */ |
| 6726 | }, |
| 6727 | }; |
| 6728 | |
| 6729 | perf_event_aux(perf_event_switch_output, |
| 6730 | &switch_event, |
| 6731 | NULL); |
| 6732 | } |
| 6733 | |
| 6734 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6735 | * IRQ throttle logging |
| 6736 | */ |
| 6737 | |
| 6738 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 6739 | { |
| 6740 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6741 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6742 | int ret; |
| 6743 | |
| 6744 | struct { |
| 6745 | struct perf_event_header header; |
| 6746 | u64 time; |
| 6747 | u64 id; |
| 6748 | u64 stream_id; |
| 6749 | } throttle_event = { |
| 6750 | .header = { |
| 6751 | .type = PERF_RECORD_THROTTLE, |
| 6752 | .misc = 0, |
| 6753 | .size = sizeof(throttle_event), |
| 6754 | }, |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6755 | .time = perf_event_clock(event), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6756 | .id = primary_event_id(event), |
| 6757 | .stream_id = event->id, |
| 6758 | }; |
| 6759 | |
| 6760 | if (enable) |
| 6761 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 6762 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6763 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 6764 | |
| 6765 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6766 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6767 | if (ret) |
| 6768 | return; |
| 6769 | |
| 6770 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6771 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6772 | perf_output_end(&handle); |
| 6773 | } |
| 6774 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 6775 | static void perf_log_itrace_start(struct perf_event *event) |
| 6776 | { |
| 6777 | struct perf_output_handle handle; |
| 6778 | struct perf_sample_data sample; |
| 6779 | struct perf_aux_event { |
| 6780 | struct perf_event_header header; |
| 6781 | u32 pid; |
| 6782 | u32 tid; |
| 6783 | } rec; |
| 6784 | int ret; |
| 6785 | |
| 6786 | if (event->parent) |
| 6787 | event = event->parent; |
| 6788 | |
| 6789 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || |
| 6790 | event->hw.itrace_started) |
| 6791 | return; |
| 6792 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 6793 | rec.header.type = PERF_RECORD_ITRACE_START; |
| 6794 | rec.header.misc = 0; |
| 6795 | rec.header.size = sizeof(rec); |
| 6796 | rec.pid = perf_event_pid(event, current); |
| 6797 | rec.tid = perf_event_tid(event, current); |
| 6798 | |
| 6799 | perf_event_header__init_id(&rec.header, &sample, event); |
| 6800 | ret = perf_output_begin(&handle, event, rec.header.size); |
| 6801 | |
| 6802 | if (ret) |
| 6803 | return; |
| 6804 | |
| 6805 | perf_output_put(&handle, rec); |
| 6806 | perf_event__output_id_sample(event, &handle, &sample); |
| 6807 | |
| 6808 | perf_output_end(&handle); |
| 6809 | } |
| 6810 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6811 | /* |
| 6812 | * Generic event overflow handling, sampling. |
| 6813 | */ |
| 6814 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6815 | static int __perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6816 | int throttle, struct perf_sample_data *data, |
| 6817 | struct pt_regs *regs) |
| 6818 | { |
| 6819 | int events = atomic_read(&event->event_limit); |
| 6820 | struct hw_perf_event *hwc = &event->hw; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 6821 | u64 seq; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6822 | int ret = 0; |
| 6823 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 6824 | /* |
| 6825 | * Non-sampling counters might still use the PMI to fold short |
| 6826 | * hardware counters, ignore those. |
| 6827 | */ |
| 6828 | if (unlikely(!is_sampling_event(event))) |
| 6829 | return 0; |
| 6830 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 6831 | seq = __this_cpu_read(perf_throttled_seq); |
| 6832 | if (seq != hwc->interrupts_seq) { |
| 6833 | hwc->interrupts_seq = seq; |
| 6834 | hwc->interrupts = 1; |
| 6835 | } else { |
| 6836 | hwc->interrupts++; |
| 6837 | if (unlikely(throttle |
| 6838 | && hwc->interrupts >= max_samples_per_tick)) { |
| 6839 | __this_cpu_inc(perf_throttled_count); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 6840 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 6841 | hwc->interrupts = MAX_INTERRUPTS; |
| 6842 | perf_log_throttle(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6843 | ret = 1; |
| 6844 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 6845 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6846 | |
| 6847 | if (event->attr.freq) { |
| 6848 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 6849 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6850 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 6851 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6852 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 6853 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 6854 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6855 | } |
| 6856 | |
| 6857 | /* |
| 6858 | * XXX event_limit might not quite work as expected on inherited |
| 6859 | * events |
| 6860 | */ |
| 6861 | |
| 6862 | event->pending_kill = POLL_IN; |
| 6863 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 6864 | ret = 1; |
| 6865 | event->pending_kill = POLL_HUP; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6866 | event->pending_disable = 1; |
| 6867 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6868 | } |
| 6869 | |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 6870 | event->overflow_handler(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 6871 | |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 6872 | if (*perf_event_fasync(event) && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6873 | event->pending_wakeup = 1; |
| 6874 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 6875 | } |
| 6876 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6877 | return ret; |
| 6878 | } |
| 6879 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6880 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6881 | struct perf_sample_data *data, |
| 6882 | struct pt_regs *regs) |
| 6883 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6884 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6885 | } |
| 6886 | |
| 6887 | /* |
| 6888 | * Generic software event infrastructure |
| 6889 | */ |
| 6890 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6891 | struct swevent_htable { |
| 6892 | struct swevent_hlist *swevent_hlist; |
| 6893 | struct mutex hlist_mutex; |
| 6894 | int hlist_refcount; |
| 6895 | |
| 6896 | /* Recursion avoidance in each contexts */ |
| 6897 | int recursion[PERF_NR_CONTEXTS]; |
| 6898 | }; |
| 6899 | |
| 6900 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 6901 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6902 | /* |
| 6903 | * We directly increment event->count and keep a second value in |
| 6904 | * event->hw.period_left to count intervals. This period event |
| 6905 | * is kept in the range [-sample_period, 0] so that we can use the |
| 6906 | * sign as trigger. |
| 6907 | */ |
| 6908 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 6909 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6910 | { |
| 6911 | struct hw_perf_event *hwc = &event->hw; |
| 6912 | u64 period = hwc->last_period; |
| 6913 | u64 nr, offset; |
| 6914 | s64 old, val; |
| 6915 | |
| 6916 | hwc->last_period = hwc->sample_period; |
| 6917 | |
| 6918 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6919 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6920 | if (val < 0) |
| 6921 | return 0; |
| 6922 | |
| 6923 | nr = div64_u64(period + val, period); |
| 6924 | offset = nr * period; |
| 6925 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6926 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6927 | goto again; |
| 6928 | |
| 6929 | return nr; |
| 6930 | } |
| 6931 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6932 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6933 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6934 | struct pt_regs *regs) |
| 6935 | { |
| 6936 | struct hw_perf_event *hwc = &event->hw; |
| 6937 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6938 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6939 | if (!overflow) |
| 6940 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6941 | |
| 6942 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 6943 | return; |
| 6944 | |
| 6945 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6946 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6947 | data, regs)) { |
| 6948 | /* |
| 6949 | * We inhibit the overflow from happening when |
| 6950 | * hwc->interrupts == MAX_INTERRUPTS. |
| 6951 | */ |
| 6952 | break; |
| 6953 | } |
| 6954 | throttle = 1; |
| 6955 | } |
| 6956 | } |
| 6957 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6958 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6959 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6960 | struct pt_regs *regs) |
| 6961 | { |
| 6962 | struct hw_perf_event *hwc = &event->hw; |
| 6963 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6964 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6965 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6966 | if (!regs) |
| 6967 | return; |
| 6968 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6969 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6970 | return; |
| 6971 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 6972 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 6973 | data->period = nr; |
| 6974 | return perf_swevent_overflow(event, 1, data, regs); |
| 6975 | } else |
| 6976 | data->period = event->hw.last_period; |
| 6977 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6978 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6979 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6980 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6981 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6982 | return; |
| 6983 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6984 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6985 | } |
| 6986 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6987 | static int perf_exclude_event(struct perf_event *event, |
| 6988 | struct pt_regs *regs) |
| 6989 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6990 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 6991 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6992 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6993 | if (regs) { |
| 6994 | if (event->attr.exclude_user && user_mode(regs)) |
| 6995 | return 1; |
| 6996 | |
| 6997 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 6998 | return 1; |
| 6999 | } |
| 7000 | |
| 7001 | return 0; |
| 7002 | } |
| 7003 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7004 | static int perf_swevent_match(struct perf_event *event, |
| 7005 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7006 | u32 event_id, |
| 7007 | struct perf_sample_data *data, |
| 7008 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7009 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7010 | if (event->attr.type != type) |
| 7011 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7012 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7013 | if (event->attr.config != event_id) |
| 7014 | return 0; |
| 7015 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7016 | if (perf_exclude_event(event, regs)) |
| 7017 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7018 | |
| 7019 | return 1; |
| 7020 | } |
| 7021 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7022 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7023 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7024 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7025 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7026 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 7027 | } |
| 7028 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7029 | static inline struct hlist_head * |
| 7030 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7031 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7032 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7033 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7034 | return &hlist->heads[hash]; |
| 7035 | } |
| 7036 | |
| 7037 | /* For the read side: events when they trigger */ |
| 7038 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7039 | 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] | 7040 | { |
| 7041 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7042 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7043 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7044 | if (!hlist) |
| 7045 | return NULL; |
| 7046 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7047 | return __find_swevent_head(hlist, type, event_id); |
| 7048 | } |
| 7049 | |
| 7050 | /* For the event head insertion and removal in the hlist */ |
| 7051 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7052 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7053 | { |
| 7054 | struct swevent_hlist *hlist; |
| 7055 | u32 event_id = event->attr.config; |
| 7056 | u64 type = event->attr.type; |
| 7057 | |
| 7058 | /* |
| 7059 | * Event scheduling is always serialized against hlist allocation |
| 7060 | * and release. Which makes the protected version suitable here. |
| 7061 | * The context lock guarantees that. |
| 7062 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7063 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7064 | lockdep_is_held(&event->ctx->lock)); |
| 7065 | if (!hlist) |
| 7066 | return NULL; |
| 7067 | |
| 7068 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | 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] | 7072 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7073 | struct perf_sample_data *data, |
| 7074 | struct pt_regs *regs) |
| 7075 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7076 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7077 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7078 | struct hlist_head *head; |
| 7079 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7080 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7081 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7082 | if (!head) |
| 7083 | goto end; |
| 7084 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7085 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7086 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7087 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7088 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7089 | end: |
| 7090 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7091 | } |
| 7092 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7093 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
| 7094 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7095 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7096 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7097 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 7098 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7099 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7100 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 7101 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7102 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 7103 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7104 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7105 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 7106 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7107 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 7108 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7109 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7110 | 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] | 7111 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 7112 | struct perf_sample_data data; |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7113 | |
| 7114 | if (WARN_ON_ONCE(!regs)) |
| 7115 | return; |
| 7116 | |
| 7117 | perf_sample_data_init(&data, addr, 0); |
| 7118 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
| 7119 | } |
| 7120 | |
| 7121 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
| 7122 | { |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7123 | int rctx; |
| 7124 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7125 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7126 | rctx = perf_swevent_get_recursion_context(); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7127 | if (unlikely(rctx < 0)) |
| 7128 | goto fail; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7129 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7130 | ___perf_sw_event(event_id, nr, regs, addr); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7131 | |
| 7132 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7133 | fail: |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7134 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7135 | } |
| 7136 | |
| 7137 | static void perf_swevent_read(struct perf_event *event) |
| 7138 | { |
| 7139 | } |
| 7140 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7141 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7142 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7143 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7144 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7145 | struct hlist_head *head; |
| 7146 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 7147 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7148 | hwc->last_period = hwc->sample_period; |
| 7149 | perf_swevent_set_period(event); |
| 7150 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7151 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7152 | hwc->state = !(flags & PERF_EF_START); |
| 7153 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7154 | head = find_swevent_head(swhash, event); |
Peter Zijlstra | 12ca6ad | 2015-12-15 13:49:05 +0100 | [diff] [blame] | 7155 | if (WARN_ON_ONCE(!head)) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7156 | return -EINVAL; |
| 7157 | |
| 7158 | hlist_add_head_rcu(&event->hlist_entry, head); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 7159 | perf_event_update_userpage(event); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7160 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7161 | return 0; |
| 7162 | } |
| 7163 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7164 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7165 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7166 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7167 | } |
| 7168 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7169 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7170 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7171 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7172 | } |
| 7173 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7174 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7175 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7176 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7177 | } |
| 7178 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7179 | /* Deref the hlist from the update side */ |
| 7180 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7181 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7182 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7183 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 7184 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7185 | } |
| 7186 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7187 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7188 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7189 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7190 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7191 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7192 | return; |
| 7193 | |
Andreea-Cristina Bernat | 70691d4 | 2014-08-22 16:26:05 +0300 | [diff] [blame] | 7194 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 7195 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7196 | } |
| 7197 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7198 | static void swevent_hlist_put_cpu(int cpu) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7199 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7200 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7201 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7202 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7203 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7204 | if (!--swhash->hlist_refcount) |
| 7205 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7206 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7207 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7208 | } |
| 7209 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7210 | static void swevent_hlist_put(void) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7211 | { |
| 7212 | int cpu; |
| 7213 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7214 | for_each_possible_cpu(cpu) |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7215 | swevent_hlist_put_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7216 | } |
| 7217 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7218 | static int swevent_hlist_get_cpu(int cpu) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7219 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7220 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7221 | int err = 0; |
| 7222 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7223 | mutex_lock(&swhash->hlist_mutex); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7224 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7225 | struct swevent_hlist *hlist; |
| 7226 | |
| 7227 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 7228 | if (!hlist) { |
| 7229 | err = -ENOMEM; |
| 7230 | goto exit; |
| 7231 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7232 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7233 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7234 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 7235 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7236 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7237 | |
| 7238 | return err; |
| 7239 | } |
| 7240 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7241 | static int swevent_hlist_get(void) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7242 | { |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7243 | int err, cpu, failed_cpu; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7244 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7245 | get_online_cpus(); |
| 7246 | for_each_possible_cpu(cpu) { |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7247 | err = swevent_hlist_get_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7248 | if (err) { |
| 7249 | failed_cpu = cpu; |
| 7250 | goto fail; |
| 7251 | } |
| 7252 | } |
| 7253 | put_online_cpus(); |
| 7254 | |
| 7255 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 7256 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7257 | for_each_possible_cpu(cpu) { |
| 7258 | if (cpu == failed_cpu) |
| 7259 | break; |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7260 | swevent_hlist_put_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7261 | } |
| 7262 | |
| 7263 | put_online_cpus(); |
| 7264 | return err; |
| 7265 | } |
| 7266 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 7267 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7268 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7269 | static void sw_perf_event_destroy(struct perf_event *event) |
| 7270 | { |
| 7271 | u64 event_id = event->attr.config; |
| 7272 | |
| 7273 | WARN_ON(event->parent); |
| 7274 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 7275 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7276 | swevent_hlist_put(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7277 | } |
| 7278 | |
| 7279 | static int perf_swevent_init(struct perf_event *event) |
| 7280 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 7281 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7282 | |
| 7283 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 7284 | return -ENOENT; |
| 7285 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 7286 | /* |
| 7287 | * no branch sampling for software events |
| 7288 | */ |
| 7289 | if (has_branch_stack(event)) |
| 7290 | return -EOPNOTSUPP; |
| 7291 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7292 | switch (event_id) { |
| 7293 | case PERF_COUNT_SW_CPU_CLOCK: |
| 7294 | case PERF_COUNT_SW_TASK_CLOCK: |
| 7295 | return -ENOENT; |
| 7296 | |
| 7297 | default: |
| 7298 | break; |
| 7299 | } |
| 7300 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 7301 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7302 | return -ENOENT; |
| 7303 | |
| 7304 | if (!event->parent) { |
| 7305 | int err; |
| 7306 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7307 | err = swevent_hlist_get(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7308 | if (err) |
| 7309 | return err; |
| 7310 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 7311 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7312 | event->destroy = sw_perf_event_destroy; |
| 7313 | } |
| 7314 | |
| 7315 | return 0; |
| 7316 | } |
| 7317 | |
| 7318 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7319 | .task_ctx_nr = perf_sw_context, |
| 7320 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 7321 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 7322 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7323 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7324 | .add = perf_swevent_add, |
| 7325 | .del = perf_swevent_del, |
| 7326 | .start = perf_swevent_start, |
| 7327 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7328 | .read = perf_swevent_read, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7329 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7330 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7331 | #ifdef CONFIG_EVENT_TRACING |
| 7332 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7333 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7334 | struct perf_sample_data *data) |
| 7335 | { |
| 7336 | void *record = data->raw->data; |
| 7337 | |
Peter Zijlstra | b71b437 | 2015-11-02 10:50:51 +0100 | [diff] [blame] | 7338 | /* only top level events have filters set */ |
| 7339 | if (event->parent) |
| 7340 | event = event->parent; |
| 7341 | |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7342 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 7343 | return 1; |
| 7344 | return 0; |
| 7345 | } |
| 7346 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7347 | static int perf_tp_event_match(struct perf_event *event, |
| 7348 | struct perf_sample_data *data, |
| 7349 | struct pt_regs *regs) |
| 7350 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 7351 | if (event->hw.state & PERF_HES_STOPPED) |
| 7352 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 7353 | /* |
| 7354 | * All tracepoints are from kernel-space. |
| 7355 | */ |
| 7356 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7357 | return 0; |
| 7358 | |
| 7359 | if (!perf_tp_filter_match(event, data)) |
| 7360 | return 0; |
| 7361 | |
| 7362 | return 1; |
| 7363 | } |
| 7364 | |
| 7365 | 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] | 7366 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 7367 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7368 | { |
| 7369 | struct perf_sample_data data; |
| 7370 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7371 | |
| 7372 | struct perf_raw_record raw = { |
| 7373 | .size = entry_size, |
| 7374 | .data = record, |
| 7375 | }; |
| 7376 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7377 | perf_sample_data_init(&data, addr, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7378 | data.raw = &raw; |
| 7379 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7380 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7381 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7382 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7383 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 7384 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 7385 | /* |
| 7386 | * If we got specified a target task, also iterate its context and |
| 7387 | * deliver this event there too. |
| 7388 | */ |
| 7389 | if (task && task != current) { |
| 7390 | struct perf_event_context *ctx; |
| 7391 | struct trace_entry *entry = record; |
| 7392 | |
| 7393 | rcu_read_lock(); |
| 7394 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 7395 | if (!ctx) |
| 7396 | goto unlock; |
| 7397 | |
| 7398 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 7399 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7400 | continue; |
| 7401 | if (event->attr.config != entry->type) |
| 7402 | continue; |
| 7403 | if (perf_tp_event_match(event, &data, regs)) |
| 7404 | perf_swevent_event(event, count, &data, regs); |
| 7405 | } |
| 7406 | unlock: |
| 7407 | rcu_read_unlock(); |
| 7408 | } |
| 7409 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 7410 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7411 | } |
| 7412 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 7413 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7414 | static void tp_perf_event_destroy(struct perf_event *event) |
| 7415 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7416 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7417 | } |
| 7418 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7419 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7420 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7421 | int err; |
| 7422 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7423 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7424 | return -ENOENT; |
| 7425 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 7426 | /* |
| 7427 | * no branch sampling for tracepoint events |
| 7428 | */ |
| 7429 | if (has_branch_stack(event)) |
| 7430 | return -EOPNOTSUPP; |
| 7431 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7432 | err = perf_trace_init(event); |
| 7433 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7434 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7435 | |
| 7436 | event->destroy = tp_perf_event_destroy; |
| 7437 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7438 | return 0; |
| 7439 | } |
| 7440 | |
| 7441 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7442 | .task_ctx_nr = perf_sw_context, |
| 7443 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7444 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7445 | .add = perf_trace_add, |
| 7446 | .del = perf_trace_del, |
| 7447 | .start = perf_swevent_start, |
| 7448 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7449 | .read = perf_swevent_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7450 | }; |
| 7451 | |
| 7452 | static inline void perf_tp_register(void) |
| 7453 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7454 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7455 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7456 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7457 | static void perf_event_free_filter(struct perf_event *event) |
| 7458 | { |
| 7459 | ftrace_profile_free_filter(event); |
| 7460 | } |
| 7461 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7462 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
| 7463 | { |
| 7464 | struct bpf_prog *prog; |
| 7465 | |
| 7466 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7467 | return -EINVAL; |
| 7468 | |
| 7469 | if (event->tp_event->prog) |
| 7470 | return -EEXIST; |
| 7471 | |
Wang Nan | 04a22fa | 2015-07-01 02:13:50 +0000 | [diff] [blame] | 7472 | if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE)) |
| 7473 | /* bpf programs can only be attached to u/kprobes */ |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7474 | return -EINVAL; |
| 7475 | |
| 7476 | prog = bpf_prog_get(prog_fd); |
| 7477 | if (IS_ERR(prog)) |
| 7478 | return PTR_ERR(prog); |
| 7479 | |
Linus Torvalds | 6c373ca | 2015-04-15 09:00:47 -0700 | [diff] [blame] | 7480 | if (prog->type != BPF_PROG_TYPE_KPROBE) { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7481 | /* valid fd, but invalid bpf program type */ |
| 7482 | bpf_prog_put(prog); |
| 7483 | return -EINVAL; |
| 7484 | } |
| 7485 | |
| 7486 | event->tp_event->prog = prog; |
| 7487 | |
| 7488 | return 0; |
| 7489 | } |
| 7490 | |
| 7491 | static void perf_event_free_bpf_prog(struct perf_event *event) |
| 7492 | { |
| 7493 | struct bpf_prog *prog; |
| 7494 | |
| 7495 | if (!event->tp_event) |
| 7496 | return; |
| 7497 | |
| 7498 | prog = event->tp_event->prog; |
| 7499 | if (prog) { |
| 7500 | event->tp_event->prog = NULL; |
| 7501 | bpf_prog_put(prog); |
| 7502 | } |
| 7503 | } |
| 7504 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7505 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7506 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7507 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7508 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7509 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7510 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7511 | static void perf_event_free_filter(struct perf_event *event) |
| 7512 | { |
| 7513 | } |
| 7514 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7515 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
| 7516 | { |
| 7517 | return -ENOENT; |
| 7518 | } |
| 7519 | |
| 7520 | static void perf_event_free_bpf_prog(struct perf_event *event) |
| 7521 | { |
| 7522 | } |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 7523 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7524 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7525 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7526 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7527 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7528 | struct perf_sample_data sample; |
| 7529 | struct pt_regs *regs = data; |
| 7530 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7531 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7532 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7533 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7534 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7535 | } |
| 7536 | #endif |
| 7537 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 7538 | /* |
| 7539 | * Allocate a new address filter |
| 7540 | */ |
| 7541 | static struct perf_addr_filter * |
| 7542 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) |
| 7543 | { |
| 7544 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); |
| 7545 | struct perf_addr_filter *filter; |
| 7546 | |
| 7547 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); |
| 7548 | if (!filter) |
| 7549 | return NULL; |
| 7550 | |
| 7551 | INIT_LIST_HEAD(&filter->entry); |
| 7552 | list_add_tail(&filter->entry, filters); |
| 7553 | |
| 7554 | return filter; |
| 7555 | } |
| 7556 | |
| 7557 | static void free_filters_list(struct list_head *filters) |
| 7558 | { |
| 7559 | struct perf_addr_filter *filter, *iter; |
| 7560 | |
| 7561 | list_for_each_entry_safe(filter, iter, filters, entry) { |
| 7562 | if (filter->inode) |
| 7563 | iput(filter->inode); |
| 7564 | list_del(&filter->entry); |
| 7565 | kfree(filter); |
| 7566 | } |
| 7567 | } |
| 7568 | |
| 7569 | /* |
| 7570 | * Free existing address filters and optionally install new ones |
| 7571 | */ |
| 7572 | static void perf_addr_filters_splice(struct perf_event *event, |
| 7573 | struct list_head *head) |
| 7574 | { |
| 7575 | unsigned long flags; |
| 7576 | LIST_HEAD(list); |
| 7577 | |
| 7578 | if (!has_addr_filter(event)) |
| 7579 | return; |
| 7580 | |
| 7581 | /* don't bother with children, they don't have their own filters */ |
| 7582 | if (event->parent) |
| 7583 | return; |
| 7584 | |
| 7585 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); |
| 7586 | |
| 7587 | list_splice_init(&event->addr_filters.list, &list); |
| 7588 | if (head) |
| 7589 | list_splice(head, &event->addr_filters.list); |
| 7590 | |
| 7591 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); |
| 7592 | |
| 7593 | free_filters_list(&list); |
| 7594 | } |
| 7595 | |
| 7596 | /* |
| 7597 | * Scan through mm's vmas and see if one of them matches the |
| 7598 | * @filter; if so, adjust filter's address range. |
| 7599 | * Called with mm::mmap_sem down for reading. |
| 7600 | */ |
| 7601 | static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter, |
| 7602 | struct mm_struct *mm) |
| 7603 | { |
| 7604 | struct vm_area_struct *vma; |
| 7605 | |
| 7606 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 7607 | struct file *file = vma->vm_file; |
| 7608 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; |
| 7609 | unsigned long vma_size = vma->vm_end - vma->vm_start; |
| 7610 | |
| 7611 | if (!file) |
| 7612 | continue; |
| 7613 | |
| 7614 | if (!perf_addr_filter_match(filter, file, off, vma_size)) |
| 7615 | continue; |
| 7616 | |
| 7617 | return vma->vm_start; |
| 7618 | } |
| 7619 | |
| 7620 | return 0; |
| 7621 | } |
| 7622 | |
| 7623 | /* |
| 7624 | * Update event's address range filters based on the |
| 7625 | * task's existing mappings, if any. |
| 7626 | */ |
| 7627 | static void perf_event_addr_filters_apply(struct perf_event *event) |
| 7628 | { |
| 7629 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 7630 | struct task_struct *task = READ_ONCE(event->ctx->task); |
| 7631 | struct perf_addr_filter *filter; |
| 7632 | struct mm_struct *mm = NULL; |
| 7633 | unsigned int count = 0; |
| 7634 | unsigned long flags; |
| 7635 | |
| 7636 | /* |
| 7637 | * We may observe TASK_TOMBSTONE, which means that the event tear-down |
| 7638 | * will stop on the parent's child_mutex that our caller is also holding |
| 7639 | */ |
| 7640 | if (task == TASK_TOMBSTONE) |
| 7641 | return; |
| 7642 | |
| 7643 | mm = get_task_mm(event->ctx->task); |
| 7644 | if (!mm) |
| 7645 | goto restart; |
| 7646 | |
| 7647 | down_read(&mm->mmap_sem); |
| 7648 | |
| 7649 | raw_spin_lock_irqsave(&ifh->lock, flags); |
| 7650 | list_for_each_entry(filter, &ifh->list, entry) { |
| 7651 | event->addr_filters_offs[count] = 0; |
| 7652 | |
| 7653 | if (perf_addr_filter_needs_mmap(filter)) |
| 7654 | event->addr_filters_offs[count] = |
| 7655 | perf_addr_filter_apply(filter, mm); |
| 7656 | |
| 7657 | count++; |
| 7658 | } |
| 7659 | |
| 7660 | event->addr_filters_gen++; |
| 7661 | raw_spin_unlock_irqrestore(&ifh->lock, flags); |
| 7662 | |
| 7663 | up_read(&mm->mmap_sem); |
| 7664 | |
| 7665 | mmput(mm); |
| 7666 | |
| 7667 | restart: |
| 7668 | perf_event_restart(event); |
| 7669 | } |
| 7670 | |
| 7671 | /* |
| 7672 | * Address range filtering: limiting the data to certain |
| 7673 | * instruction address ranges. Filters are ioctl()ed to us from |
| 7674 | * userspace as ascii strings. |
| 7675 | * |
| 7676 | * Filter string format: |
| 7677 | * |
| 7678 | * ACTION RANGE_SPEC |
| 7679 | * where ACTION is one of the |
| 7680 | * * "filter": limit the trace to this region |
| 7681 | * * "start": start tracing from this address |
| 7682 | * * "stop": stop tracing at this address/region; |
| 7683 | * RANGE_SPEC is |
| 7684 | * * for kernel addresses: <start address>[/<size>] |
| 7685 | * * for object files: <start address>[/<size>]@</path/to/object/file> |
| 7686 | * |
| 7687 | * if <size> is not specified, the range is treated as a single address. |
| 7688 | */ |
| 7689 | enum { |
| 7690 | IF_ACT_FILTER, |
| 7691 | IF_ACT_START, |
| 7692 | IF_ACT_STOP, |
| 7693 | IF_SRC_FILE, |
| 7694 | IF_SRC_KERNEL, |
| 7695 | IF_SRC_FILEADDR, |
| 7696 | IF_SRC_KERNELADDR, |
| 7697 | }; |
| 7698 | |
| 7699 | enum { |
| 7700 | IF_STATE_ACTION = 0, |
| 7701 | IF_STATE_SOURCE, |
| 7702 | IF_STATE_END, |
| 7703 | }; |
| 7704 | |
| 7705 | static const match_table_t if_tokens = { |
| 7706 | { IF_ACT_FILTER, "filter" }, |
| 7707 | { IF_ACT_START, "start" }, |
| 7708 | { IF_ACT_STOP, "stop" }, |
| 7709 | { IF_SRC_FILE, "%u/%u@%s" }, |
| 7710 | { IF_SRC_KERNEL, "%u/%u" }, |
| 7711 | { IF_SRC_FILEADDR, "%u@%s" }, |
| 7712 | { IF_SRC_KERNELADDR, "%u" }, |
| 7713 | }; |
| 7714 | |
| 7715 | /* |
| 7716 | * Address filter string parser |
| 7717 | */ |
| 7718 | static int |
| 7719 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, |
| 7720 | struct list_head *filters) |
| 7721 | { |
| 7722 | struct perf_addr_filter *filter = NULL; |
| 7723 | char *start, *orig, *filename = NULL; |
| 7724 | struct path path; |
| 7725 | substring_t args[MAX_OPT_ARGS]; |
| 7726 | int state = IF_STATE_ACTION, token; |
| 7727 | unsigned int kernel = 0; |
| 7728 | int ret = -EINVAL; |
| 7729 | |
| 7730 | orig = fstr = kstrdup(fstr, GFP_KERNEL); |
| 7731 | if (!fstr) |
| 7732 | return -ENOMEM; |
| 7733 | |
| 7734 | while ((start = strsep(&fstr, " ,\n")) != NULL) { |
| 7735 | ret = -EINVAL; |
| 7736 | |
| 7737 | if (!*start) |
| 7738 | continue; |
| 7739 | |
| 7740 | /* filter definition begins */ |
| 7741 | if (state == IF_STATE_ACTION) { |
| 7742 | filter = perf_addr_filter_new(event, filters); |
| 7743 | if (!filter) |
| 7744 | goto fail; |
| 7745 | } |
| 7746 | |
| 7747 | token = match_token(start, if_tokens, args); |
| 7748 | switch (token) { |
| 7749 | case IF_ACT_FILTER: |
| 7750 | case IF_ACT_START: |
| 7751 | filter->filter = 1; |
| 7752 | |
| 7753 | case IF_ACT_STOP: |
| 7754 | if (state != IF_STATE_ACTION) |
| 7755 | goto fail; |
| 7756 | |
| 7757 | state = IF_STATE_SOURCE; |
| 7758 | break; |
| 7759 | |
| 7760 | case IF_SRC_KERNELADDR: |
| 7761 | case IF_SRC_KERNEL: |
| 7762 | kernel = 1; |
| 7763 | |
| 7764 | case IF_SRC_FILEADDR: |
| 7765 | case IF_SRC_FILE: |
| 7766 | if (state != IF_STATE_SOURCE) |
| 7767 | goto fail; |
| 7768 | |
| 7769 | if (token == IF_SRC_FILE || token == IF_SRC_KERNEL) |
| 7770 | filter->range = 1; |
| 7771 | |
| 7772 | *args[0].to = 0; |
| 7773 | ret = kstrtoul(args[0].from, 0, &filter->offset); |
| 7774 | if (ret) |
| 7775 | goto fail; |
| 7776 | |
| 7777 | if (filter->range) { |
| 7778 | *args[1].to = 0; |
| 7779 | ret = kstrtoul(args[1].from, 0, &filter->size); |
| 7780 | if (ret) |
| 7781 | goto fail; |
| 7782 | } |
| 7783 | |
| 7784 | if (token == IF_SRC_FILE) { |
| 7785 | filename = match_strdup(&args[2]); |
| 7786 | if (!filename) { |
| 7787 | ret = -ENOMEM; |
| 7788 | goto fail; |
| 7789 | } |
| 7790 | } |
| 7791 | |
| 7792 | state = IF_STATE_END; |
| 7793 | break; |
| 7794 | |
| 7795 | default: |
| 7796 | goto fail; |
| 7797 | } |
| 7798 | |
| 7799 | /* |
| 7800 | * Filter definition is fully parsed, validate and install it. |
| 7801 | * Make sure that it doesn't contradict itself or the event's |
| 7802 | * attribute. |
| 7803 | */ |
| 7804 | if (state == IF_STATE_END) { |
| 7805 | if (kernel && event->attr.exclude_kernel) |
| 7806 | goto fail; |
| 7807 | |
| 7808 | if (!kernel) { |
| 7809 | if (!filename) |
| 7810 | goto fail; |
| 7811 | |
| 7812 | /* look up the path and grab its inode */ |
| 7813 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); |
| 7814 | if (ret) |
| 7815 | goto fail_free_name; |
| 7816 | |
| 7817 | filter->inode = igrab(d_inode(path.dentry)); |
| 7818 | path_put(&path); |
| 7819 | kfree(filename); |
| 7820 | filename = NULL; |
| 7821 | |
| 7822 | ret = -EINVAL; |
| 7823 | if (!filter->inode || |
| 7824 | !S_ISREG(filter->inode->i_mode)) |
| 7825 | /* free_filters_list() will iput() */ |
| 7826 | goto fail; |
| 7827 | } |
| 7828 | |
| 7829 | /* ready to consume more filters */ |
| 7830 | state = IF_STATE_ACTION; |
| 7831 | filter = NULL; |
| 7832 | } |
| 7833 | } |
| 7834 | |
| 7835 | if (state != IF_STATE_ACTION) |
| 7836 | goto fail; |
| 7837 | |
| 7838 | kfree(orig); |
| 7839 | |
| 7840 | return 0; |
| 7841 | |
| 7842 | fail_free_name: |
| 7843 | kfree(filename); |
| 7844 | fail: |
| 7845 | free_filters_list(filters); |
| 7846 | kfree(orig); |
| 7847 | |
| 7848 | return ret; |
| 7849 | } |
| 7850 | |
| 7851 | static int |
| 7852 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) |
| 7853 | { |
| 7854 | LIST_HEAD(filters); |
| 7855 | int ret; |
| 7856 | |
| 7857 | /* |
| 7858 | * Since this is called in perf_ioctl() path, we're already holding |
| 7859 | * ctx::mutex. |
| 7860 | */ |
| 7861 | lockdep_assert_held(&event->ctx->mutex); |
| 7862 | |
| 7863 | if (WARN_ON_ONCE(event->parent)) |
| 7864 | return -EINVAL; |
| 7865 | |
| 7866 | /* |
| 7867 | * For now, we only support filtering in per-task events; doing so |
| 7868 | * for CPU-wide events requires additional context switching trickery, |
| 7869 | * since same object code will be mapped at different virtual |
| 7870 | * addresses in different processes. |
| 7871 | */ |
| 7872 | if (!event->ctx->task) |
| 7873 | return -EOPNOTSUPP; |
| 7874 | |
| 7875 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); |
| 7876 | if (ret) |
| 7877 | return ret; |
| 7878 | |
| 7879 | ret = event->pmu->addr_filters_validate(&filters); |
| 7880 | if (ret) { |
| 7881 | free_filters_list(&filters); |
| 7882 | return ret; |
| 7883 | } |
| 7884 | |
| 7885 | /* remove existing filters, if any */ |
| 7886 | perf_addr_filters_splice(event, &filters); |
| 7887 | |
| 7888 | /* install new filters */ |
| 7889 | perf_event_for_each_child(event, perf_event_addr_filters_apply); |
| 7890 | |
| 7891 | return ret; |
| 7892 | } |
| 7893 | |
Alexander Shishkin | c796bbb | 2016-04-27 18:44:42 +0300 | [diff] [blame] | 7894 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 7895 | { |
| 7896 | char *filter_str; |
| 7897 | int ret = -EINVAL; |
| 7898 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 7899 | if ((event->attr.type != PERF_TYPE_TRACEPOINT || |
| 7900 | !IS_ENABLED(CONFIG_EVENT_TRACING)) && |
| 7901 | !has_addr_filter(event)) |
Alexander Shishkin | c796bbb | 2016-04-27 18:44:42 +0300 | [diff] [blame] | 7902 | return -EINVAL; |
| 7903 | |
| 7904 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 7905 | if (IS_ERR(filter_str)) |
| 7906 | return PTR_ERR(filter_str); |
| 7907 | |
| 7908 | if (IS_ENABLED(CONFIG_EVENT_TRACING) && |
| 7909 | event->attr.type == PERF_TYPE_TRACEPOINT) |
| 7910 | ret = ftrace_profile_set_filter(event, event->attr.config, |
| 7911 | filter_str); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 7912 | else if (has_addr_filter(event)) |
| 7913 | ret = perf_event_set_addr_filter(event, filter_str); |
Alexander Shishkin | c796bbb | 2016-04-27 18:44:42 +0300 | [diff] [blame] | 7914 | |
| 7915 | kfree(filter_str); |
| 7916 | return ret; |
| 7917 | } |
| 7918 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7919 | /* |
| 7920 | * hrtimer based swevent callback |
| 7921 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7922 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7923 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7924 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7925 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 7926 | struct perf_sample_data data; |
| 7927 | struct pt_regs *regs; |
| 7928 | struct perf_event *event; |
| 7929 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7930 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7931 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 7932 | |
| 7933 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 7934 | return HRTIMER_NORESTART; |
| 7935 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7936 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7937 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7938 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7939 | regs = get_irq_regs(); |
| 7940 | |
| 7941 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 7942 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 7943 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7944 | ret = HRTIMER_NORESTART; |
| 7945 | } |
| 7946 | |
| 7947 | period = max_t(u64, 10000, event->hw.sample_period); |
| 7948 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 7949 | |
| 7950 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7951 | } |
| 7952 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7953 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7954 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7955 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 7956 | s64 period; |
| 7957 | |
| 7958 | if (!is_sampling_event(event)) |
| 7959 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7960 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 7961 | period = local64_read(&hwc->period_left); |
| 7962 | if (period) { |
| 7963 | if (period < 0) |
| 7964 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 7965 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 7966 | local64_set(&hwc->period_left, 0); |
| 7967 | } else { |
| 7968 | period = max_t(u64, 10000, hwc->sample_period); |
| 7969 | } |
Thomas Gleixner | 3497d20 | 2015-04-14 21:09:03 +0000 | [diff] [blame] | 7970 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
| 7971 | HRTIMER_MODE_REL_PINNED); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7972 | } |
| 7973 | |
| 7974 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 7975 | { |
| 7976 | struct hw_perf_event *hwc = &event->hw; |
| 7977 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 7978 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7979 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 7980 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7981 | |
| 7982 | hrtimer_cancel(&hwc->hrtimer); |
| 7983 | } |
| 7984 | } |
| 7985 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 7986 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 7987 | { |
| 7988 | struct hw_perf_event *hwc = &event->hw; |
| 7989 | |
| 7990 | if (!is_sampling_event(event)) |
| 7991 | return; |
| 7992 | |
| 7993 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 7994 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 7995 | |
| 7996 | /* |
| 7997 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 7998 | * mapping and avoid the whole period adjust feedback stuff. |
| 7999 | */ |
| 8000 | if (event->attr.freq) { |
| 8001 | long freq = event->attr.sample_freq; |
| 8002 | |
| 8003 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 8004 | hwc->sample_period = event->attr.sample_period; |
| 8005 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 8006 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8007 | event->attr.freq = 0; |
| 8008 | } |
| 8009 | } |
| 8010 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8011 | /* |
| 8012 | * Software event: cpu wall time clock |
| 8013 | */ |
| 8014 | |
| 8015 | static void cpu_clock_event_update(struct perf_event *event) |
| 8016 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8017 | s64 prev; |
| 8018 | u64 now; |
| 8019 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8020 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8021 | prev = local64_xchg(&event->hw.prev_count, now); |
| 8022 | local64_add(now - prev, &event->count); |
| 8023 | } |
| 8024 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8025 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8026 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8027 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8028 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8029 | } |
| 8030 | |
| 8031 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 8032 | { |
| 8033 | perf_swevent_cancel_hrtimer(event); |
| 8034 | cpu_clock_event_update(event); |
| 8035 | } |
| 8036 | |
| 8037 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 8038 | { |
| 8039 | if (flags & PERF_EF_START) |
| 8040 | cpu_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 8041 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8042 | |
| 8043 | return 0; |
| 8044 | } |
| 8045 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8046 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8047 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8048 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8049 | } |
| 8050 | |
| 8051 | static void cpu_clock_event_read(struct perf_event *event) |
| 8052 | { |
| 8053 | cpu_clock_event_update(event); |
| 8054 | } |
| 8055 | |
| 8056 | static int cpu_clock_event_init(struct perf_event *event) |
| 8057 | { |
| 8058 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 8059 | return -ENOENT; |
| 8060 | |
| 8061 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 8062 | return -ENOENT; |
| 8063 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 8064 | /* |
| 8065 | * no branch sampling for software events |
| 8066 | */ |
| 8067 | if (has_branch_stack(event)) |
| 8068 | return -EOPNOTSUPP; |
| 8069 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8070 | perf_swevent_init_hrtimer(event); |
| 8071 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8072 | return 0; |
| 8073 | } |
| 8074 | |
| 8075 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8076 | .task_ctx_nr = perf_sw_context, |
| 8077 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8078 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 8079 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8080 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8081 | .add = cpu_clock_event_add, |
| 8082 | .del = cpu_clock_event_del, |
| 8083 | .start = cpu_clock_event_start, |
| 8084 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8085 | .read = cpu_clock_event_read, |
| 8086 | }; |
| 8087 | |
| 8088 | /* |
| 8089 | * Software event: task time clock |
| 8090 | */ |
| 8091 | |
| 8092 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 8093 | { |
| 8094 | u64 prev; |
| 8095 | s64 delta; |
| 8096 | |
| 8097 | prev = local64_xchg(&event->hw.prev_count, now); |
| 8098 | delta = now - prev; |
| 8099 | local64_add(delta, &event->count); |
| 8100 | } |
| 8101 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8102 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8103 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8104 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8105 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8106 | } |
| 8107 | |
| 8108 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 8109 | { |
| 8110 | perf_swevent_cancel_hrtimer(event); |
| 8111 | task_clock_event_update(event, event->ctx->time); |
| 8112 | } |
| 8113 | |
| 8114 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 8115 | { |
| 8116 | if (flags & PERF_EF_START) |
| 8117 | task_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 8118 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8119 | |
| 8120 | return 0; |
| 8121 | } |
| 8122 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8123 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8124 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8125 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8126 | } |
| 8127 | |
| 8128 | static void task_clock_event_read(struct perf_event *event) |
| 8129 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 8130 | u64 now = perf_clock(); |
| 8131 | u64 delta = now - event->ctx->timestamp; |
| 8132 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8133 | |
| 8134 | task_clock_event_update(event, time); |
| 8135 | } |
| 8136 | |
| 8137 | static int task_clock_event_init(struct perf_event *event) |
| 8138 | { |
| 8139 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 8140 | return -ENOENT; |
| 8141 | |
| 8142 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 8143 | return -ENOENT; |
| 8144 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 8145 | /* |
| 8146 | * no branch sampling for software events |
| 8147 | */ |
| 8148 | if (has_branch_stack(event)) |
| 8149 | return -EOPNOTSUPP; |
| 8150 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8151 | perf_swevent_init_hrtimer(event); |
| 8152 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8153 | return 0; |
| 8154 | } |
| 8155 | |
| 8156 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8157 | .task_ctx_nr = perf_sw_context, |
| 8158 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8159 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 8160 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8161 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8162 | .add = task_clock_event_add, |
| 8163 | .del = task_clock_event_del, |
| 8164 | .start = task_clock_event_start, |
| 8165 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8166 | .read = task_clock_event_read, |
| 8167 | }; |
| 8168 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8169 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 8170 | { |
| 8171 | } |
| 8172 | |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8173 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
| 8174 | { |
| 8175 | } |
| 8176 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8177 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 8178 | { |
| 8179 | return 0; |
| 8180 | } |
| 8181 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 8182 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8183 | |
| 8184 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8185 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8186 | __this_cpu_write(nop_txn_flags, flags); |
| 8187 | |
| 8188 | if (flags & ~PERF_PMU_TXN_ADD) |
| 8189 | return; |
| 8190 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8191 | perf_pmu_disable(pmu); |
| 8192 | } |
| 8193 | |
| 8194 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 8195 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8196 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
| 8197 | |
| 8198 | __this_cpu_write(nop_txn_flags, 0); |
| 8199 | |
| 8200 | if (flags & ~PERF_PMU_TXN_ADD) |
| 8201 | return 0; |
| 8202 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8203 | perf_pmu_enable(pmu); |
| 8204 | return 0; |
| 8205 | } |
| 8206 | |
| 8207 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 8208 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8209 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
| 8210 | |
| 8211 | __this_cpu_write(nop_txn_flags, 0); |
| 8212 | |
| 8213 | if (flags & ~PERF_PMU_TXN_ADD) |
| 8214 | return; |
| 8215 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8216 | perf_pmu_enable(pmu); |
| 8217 | } |
| 8218 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 8219 | static int perf_event_idx_default(struct perf_event *event) |
| 8220 | { |
Peter Zijlstra | c719f56 | 2014-10-21 11:10:21 +0200 | [diff] [blame] | 8221 | return 0; |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 8222 | } |
| 8223 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8224 | /* |
| 8225 | * Ensures all contexts with the same task_ctx_nr have the same |
| 8226 | * pmu_cpu_context too. |
| 8227 | */ |
Mark Rutland | 9e31704 | 2014-02-10 17:44:18 +0000 | [diff] [blame] | 8228 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8229 | { |
| 8230 | struct pmu *pmu; |
| 8231 | |
| 8232 | if (ctxn < 0) |
| 8233 | return NULL; |
| 8234 | |
| 8235 | list_for_each_entry(pmu, &pmus, entry) { |
| 8236 | if (pmu->task_ctx_nr == ctxn) |
| 8237 | return pmu->pmu_cpu_context; |
| 8238 | } |
| 8239 | |
| 8240 | return NULL; |
| 8241 | } |
| 8242 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8243 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8244 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8245 | int cpu; |
| 8246 | |
| 8247 | for_each_possible_cpu(cpu) { |
| 8248 | struct perf_cpu_context *cpuctx; |
| 8249 | |
| 8250 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 8251 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 8252 | if (cpuctx->unique_pmu == old_pmu) |
| 8253 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8254 | } |
| 8255 | } |
| 8256 | |
| 8257 | static void free_pmu_context(struct pmu *pmu) |
| 8258 | { |
| 8259 | struct pmu *i; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8260 | |
| 8261 | mutex_lock(&pmus_lock); |
| 8262 | /* |
| 8263 | * Like a real lame refcount. |
| 8264 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8265 | list_for_each_entry(i, &pmus, entry) { |
| 8266 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 8267 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8268 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8269 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8270 | } |
| 8271 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8272 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8273 | out: |
| 8274 | mutex_unlock(&pmus_lock); |
| 8275 | } |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8276 | |
| 8277 | /* |
| 8278 | * Let userspace know that this PMU supports address range filtering: |
| 8279 | */ |
| 8280 | static ssize_t nr_addr_filters_show(struct device *dev, |
| 8281 | struct device_attribute *attr, |
| 8282 | char *page) |
| 8283 | { |
| 8284 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8285 | |
| 8286 | return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); |
| 8287 | } |
| 8288 | DEVICE_ATTR_RO(nr_addr_filters); |
| 8289 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8290 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8291 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8292 | static ssize_t |
| 8293 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 8294 | { |
| 8295 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8296 | |
| 8297 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 8298 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8299 | static DEVICE_ATTR_RO(type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8300 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8301 | static ssize_t |
| 8302 | perf_event_mux_interval_ms_show(struct device *dev, |
| 8303 | struct device_attribute *attr, |
| 8304 | char *page) |
| 8305 | { |
| 8306 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8307 | |
| 8308 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 8309 | } |
| 8310 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8311 | static DEFINE_MUTEX(mux_interval_mutex); |
| 8312 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8313 | static ssize_t |
| 8314 | perf_event_mux_interval_ms_store(struct device *dev, |
| 8315 | struct device_attribute *attr, |
| 8316 | const char *buf, size_t count) |
| 8317 | { |
| 8318 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8319 | int timer, cpu, ret; |
| 8320 | |
| 8321 | ret = kstrtoint(buf, 0, &timer); |
| 8322 | if (ret) |
| 8323 | return ret; |
| 8324 | |
| 8325 | if (timer < 1) |
| 8326 | return -EINVAL; |
| 8327 | |
| 8328 | /* same value, noting to do */ |
| 8329 | if (timer == pmu->hrtimer_interval_ms) |
| 8330 | return count; |
| 8331 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8332 | mutex_lock(&mux_interval_mutex); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8333 | pmu->hrtimer_interval_ms = timer; |
| 8334 | |
| 8335 | /* update all cpuctx for this PMU */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8336 | get_online_cpus(); |
| 8337 | for_each_online_cpu(cpu) { |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8338 | struct perf_cpu_context *cpuctx; |
| 8339 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 8340 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 8341 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8342 | cpu_function_call(cpu, |
| 8343 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8344 | } |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8345 | put_online_cpus(); |
| 8346 | mutex_unlock(&mux_interval_mutex); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8347 | |
| 8348 | return count; |
| 8349 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8350 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8351 | |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8352 | static struct attribute *pmu_dev_attrs[] = { |
| 8353 | &dev_attr_type.attr, |
| 8354 | &dev_attr_perf_event_mux_interval_ms.attr, |
| 8355 | NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8356 | }; |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8357 | ATTRIBUTE_GROUPS(pmu_dev); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8358 | |
| 8359 | static int pmu_bus_running; |
| 8360 | static struct bus_type pmu_bus = { |
| 8361 | .name = "event_source", |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8362 | .dev_groups = pmu_dev_groups, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8363 | }; |
| 8364 | |
| 8365 | static void pmu_dev_release(struct device *dev) |
| 8366 | { |
| 8367 | kfree(dev); |
| 8368 | } |
| 8369 | |
| 8370 | static int pmu_dev_alloc(struct pmu *pmu) |
| 8371 | { |
| 8372 | int ret = -ENOMEM; |
| 8373 | |
| 8374 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 8375 | if (!pmu->dev) |
| 8376 | goto out; |
| 8377 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 8378 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8379 | device_initialize(pmu->dev); |
| 8380 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 8381 | if (ret) |
| 8382 | goto free_dev; |
| 8383 | |
| 8384 | dev_set_drvdata(pmu->dev, pmu); |
| 8385 | pmu->dev->bus = &pmu_bus; |
| 8386 | pmu->dev->release = pmu_dev_release; |
| 8387 | ret = device_add(pmu->dev); |
| 8388 | if (ret) |
| 8389 | goto free_dev; |
| 8390 | |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8391 | /* For PMUs with address filters, throw in an extra attribute: */ |
| 8392 | if (pmu->nr_addr_filters) |
| 8393 | ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters); |
| 8394 | |
| 8395 | if (ret) |
| 8396 | goto del_dev; |
| 8397 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8398 | out: |
| 8399 | return ret; |
| 8400 | |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8401 | del_dev: |
| 8402 | device_del(pmu->dev); |
| 8403 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8404 | free_dev: |
| 8405 | put_device(pmu->dev); |
| 8406 | goto out; |
| 8407 | } |
| 8408 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 8409 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 8410 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 8411 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 8412 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8413 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8414 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8415 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8416 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8417 | ret = -ENOMEM; |
| 8418 | pmu->pmu_disable_count = alloc_percpu(int); |
| 8419 | if (!pmu->pmu_disable_count) |
| 8420 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8421 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8422 | pmu->type = -1; |
| 8423 | if (!name) |
| 8424 | goto skip_type; |
| 8425 | pmu->name = name; |
| 8426 | |
| 8427 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 8428 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 8429 | if (type < 0) { |
| 8430 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8431 | goto free_pdc; |
| 8432 | } |
| 8433 | } |
| 8434 | pmu->type = type; |
| 8435 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8436 | if (pmu_bus_running) { |
| 8437 | ret = pmu_dev_alloc(pmu); |
| 8438 | if (ret) |
| 8439 | goto free_idr; |
| 8440 | } |
| 8441 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8442 | skip_type: |
Peter Zijlstra | 2665784 | 2016-03-22 22:09:18 +0100 | [diff] [blame] | 8443 | if (pmu->task_ctx_nr == perf_hw_context) { |
| 8444 | static int hw_context_taken = 0; |
| 8445 | |
Mark Rutland | 5101ef2 | 2016-04-26 11:33:46 +0100 | [diff] [blame] | 8446 | /* |
| 8447 | * Other than systems with heterogeneous CPUs, it never makes |
| 8448 | * sense for two PMUs to share perf_hw_context. PMUs which are |
| 8449 | * uncore must use perf_invalid_context. |
| 8450 | */ |
| 8451 | if (WARN_ON_ONCE(hw_context_taken && |
| 8452 | !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS))) |
Peter Zijlstra | 2665784 | 2016-03-22 22:09:18 +0100 | [diff] [blame] | 8453 | pmu->task_ctx_nr = perf_invalid_context; |
| 8454 | |
| 8455 | hw_context_taken = 1; |
| 8456 | } |
| 8457 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8458 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 8459 | if (pmu->pmu_cpu_context) |
| 8460 | goto got_cpu_context; |
| 8461 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 8462 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8463 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 8464 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8465 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8466 | |
| 8467 | for_each_possible_cpu(cpu) { |
| 8468 | struct perf_cpu_context *cpuctx; |
| 8469 | |
| 8470 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 8471 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 8472 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 8473 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8474 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 8475 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8476 | __perf_mux_hrtimer_init(cpuctx, cpu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 8477 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 8478 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8479 | } |
| 8480 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8481 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8482 | if (!pmu->start_txn) { |
| 8483 | if (pmu->pmu_enable) { |
| 8484 | /* |
| 8485 | * If we have pmu_enable/pmu_disable calls, install |
| 8486 | * transaction stubs that use that to try and batch |
| 8487 | * hardware accesses. |
| 8488 | */ |
| 8489 | pmu->start_txn = perf_pmu_start_txn; |
| 8490 | pmu->commit_txn = perf_pmu_commit_txn; |
| 8491 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 8492 | } else { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8493 | pmu->start_txn = perf_pmu_nop_txn; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8494 | pmu->commit_txn = perf_pmu_nop_int; |
| 8495 | pmu->cancel_txn = perf_pmu_nop_void; |
| 8496 | } |
| 8497 | } |
| 8498 | |
| 8499 | if (!pmu->pmu_enable) { |
| 8500 | pmu->pmu_enable = perf_pmu_nop_void; |
| 8501 | pmu->pmu_disable = perf_pmu_nop_void; |
| 8502 | } |
| 8503 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 8504 | if (!pmu->event_idx) |
| 8505 | pmu->event_idx = perf_event_idx_default; |
| 8506 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8507 | list_add_rcu(&pmu->entry, &pmus); |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8508 | atomic_set(&pmu->exclusive_cnt, 0); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8509 | ret = 0; |
| 8510 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8511 | mutex_unlock(&pmus_lock); |
| 8512 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8513 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8514 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8515 | free_dev: |
| 8516 | device_del(pmu->dev); |
| 8517 | put_device(pmu->dev); |
| 8518 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8519 | free_idr: |
| 8520 | if (pmu->type >= PERF_TYPE_MAX) |
| 8521 | idr_remove(&pmu_idr, pmu->type); |
| 8522 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8523 | free_pdc: |
| 8524 | free_percpu(pmu->pmu_disable_count); |
| 8525 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8526 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 8527 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8528 | |
| 8529 | void perf_pmu_unregister(struct pmu *pmu) |
| 8530 | { |
| 8531 | mutex_lock(&pmus_lock); |
| 8532 | list_del_rcu(&pmu->entry); |
| 8533 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8534 | |
| 8535 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 8536 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 8537 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8538 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8539 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 8540 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8541 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8542 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8543 | if (pmu->type >= PERF_TYPE_MAX) |
| 8544 | idr_remove(&pmu_idr, pmu->type); |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8545 | if (pmu->nr_addr_filters) |
| 8546 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8547 | device_del(pmu->dev); |
| 8548 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8549 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8550 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 8551 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8552 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8553 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
| 8554 | { |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8555 | struct perf_event_context *ctx = NULL; |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8556 | int ret; |
| 8557 | |
| 8558 | if (!try_module_get(pmu->module)) |
| 8559 | return -ENODEV; |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8560 | |
| 8561 | if (event->group_leader != event) { |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 8562 | /* |
| 8563 | * This ctx->mutex can nest when we're called through |
| 8564 | * inheritance. See the perf_event_ctx_lock_nested() comment. |
| 8565 | */ |
| 8566 | ctx = perf_event_ctx_lock_nested(event->group_leader, |
| 8567 | SINGLE_DEPTH_NESTING); |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8568 | BUG_ON(!ctx); |
| 8569 | } |
| 8570 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8571 | event->pmu = pmu; |
| 8572 | ret = pmu->event_init(event); |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8573 | |
| 8574 | if (ctx) |
| 8575 | perf_event_ctx_unlock(event->group_leader, ctx); |
| 8576 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8577 | if (ret) |
| 8578 | module_put(pmu->module); |
| 8579 | |
| 8580 | return ret; |
| 8581 | } |
| 8582 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 8583 | static struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8584 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 8585 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8586 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 8587 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8588 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8589 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8590 | |
| 8591 | rcu_read_lock(); |
| 8592 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 8593 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 8594 | if (pmu) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8595 | ret = perf_try_init_event(pmu, event); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 8596 | if (ret) |
| 8597 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8598 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 8599 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8600 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8601 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8602 | ret = perf_try_init_event(pmu, event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8603 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 8604 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8605 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8606 | if (ret != -ENOENT) { |
| 8607 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 8608 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8609 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8610 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 8611 | pmu = ERR_PTR(-ENOENT); |
| 8612 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8613 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8614 | |
| 8615 | return pmu; |
| 8616 | } |
| 8617 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8618 | static void account_event_cpu(struct perf_event *event, int cpu) |
| 8619 | { |
| 8620 | if (event->parent) |
| 8621 | return; |
| 8622 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8623 | if (is_cgroup_event(event)) |
| 8624 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); |
| 8625 | } |
| 8626 | |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 8627 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
| 8628 | static void account_freq_event_nohz(void) |
| 8629 | { |
| 8630 | #ifdef CONFIG_NO_HZ_FULL |
| 8631 | /* Lock so we don't race with concurrent unaccount */ |
| 8632 | spin_lock(&nr_freq_lock); |
| 8633 | if (atomic_inc_return(&nr_freq_events) == 1) |
| 8634 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); |
| 8635 | spin_unlock(&nr_freq_lock); |
| 8636 | #endif |
| 8637 | } |
| 8638 | |
| 8639 | static void account_freq_event(void) |
| 8640 | { |
| 8641 | if (tick_nohz_full_enabled()) |
| 8642 | account_freq_event_nohz(); |
| 8643 | else |
| 8644 | atomic_inc(&nr_freq_events); |
| 8645 | } |
| 8646 | |
| 8647 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8648 | static void account_event(struct perf_event *event) |
| 8649 | { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8650 | bool inc = false; |
| 8651 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8652 | if (event->parent) |
| 8653 | return; |
| 8654 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8655 | if (event->attach_state & PERF_ATTACH_TASK) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8656 | inc = true; |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8657 | if (event->attr.mmap || event->attr.mmap_data) |
| 8658 | atomic_inc(&nr_mmap_events); |
| 8659 | if (event->attr.comm) |
| 8660 | atomic_inc(&nr_comm_events); |
| 8661 | if (event->attr.task) |
| 8662 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 8663 | if (event->attr.freq) |
| 8664 | account_freq_event(); |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 8665 | if (event->attr.context_switch) { |
| 8666 | atomic_inc(&nr_switch_events); |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8667 | inc = true; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 8668 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8669 | if (has_branch_stack(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8670 | inc = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8671 | if (is_cgroup_event(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8672 | inc = true; |
| 8673 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 8674 | if (inc) { |
| 8675 | if (atomic_inc_not_zero(&perf_sched_count)) |
| 8676 | goto enabled; |
| 8677 | |
| 8678 | mutex_lock(&perf_sched_mutex); |
| 8679 | if (!atomic_read(&perf_sched_count)) { |
| 8680 | static_branch_enable(&perf_sched_events); |
| 8681 | /* |
| 8682 | * Guarantee that all CPUs observe they key change and |
| 8683 | * call the perf scheduling hooks before proceeding to |
| 8684 | * install events that need them. |
| 8685 | */ |
| 8686 | synchronize_sched(); |
| 8687 | } |
| 8688 | /* |
| 8689 | * Now that we have waited for the sync_sched(), allow further |
| 8690 | * increments to by-pass the mutex. |
| 8691 | */ |
| 8692 | atomic_inc(&perf_sched_count); |
| 8693 | mutex_unlock(&perf_sched_mutex); |
| 8694 | } |
| 8695 | enabled: |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8696 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8697 | account_event_cpu(event, event->cpu); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8698 | } |
| 8699 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8700 | /* |
| 8701 | * Allocate and initialize a event structure |
| 8702 | */ |
| 8703 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8704 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8705 | struct task_struct *task, |
| 8706 | struct perf_event *group_leader, |
| 8707 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8708 | perf_overflow_handler_t overflow_handler, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8709 | void *context, int cgroup_fd) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8710 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 8711 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8712 | struct perf_event *event; |
| 8713 | struct hw_perf_event *hwc; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8714 | long err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8715 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 8716 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 8717 | if (!task || cpu != -1) |
| 8718 | return ERR_PTR(-EINVAL); |
| 8719 | } |
| 8720 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8721 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8722 | if (!event) |
| 8723 | return ERR_PTR(-ENOMEM); |
| 8724 | |
| 8725 | /* |
| 8726 | * Single events are their own group leaders, with an |
| 8727 | * empty sibling list: |
| 8728 | */ |
| 8729 | if (!group_leader) |
| 8730 | group_leader = event; |
| 8731 | |
| 8732 | mutex_init(&event->child_mutex); |
| 8733 | INIT_LIST_HEAD(&event->child_list); |
| 8734 | |
| 8735 | INIT_LIST_HEAD(&event->group_entry); |
| 8736 | INIT_LIST_HEAD(&event->event_entry); |
| 8737 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 8738 | INIT_LIST_HEAD(&event->rb_entry); |
Stephane Eranian | 71ad88e | 2013-11-12 17:58:48 +0100 | [diff] [blame] | 8739 | INIT_LIST_HEAD(&event->active_entry); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8740 | INIT_LIST_HEAD(&event->addr_filters.list); |
Stephane Eranian | f3ae75d | 2014-01-08 11:15:52 +0100 | [diff] [blame] | 8741 | INIT_HLIST_NODE(&event->hlist_entry); |
| 8742 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 8743 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8744 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 8745 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8746 | |
| 8747 | mutex_init(&event->mmap_mutex); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8748 | raw_spin_lock_init(&event->addr_filters.lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8749 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8750 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8751 | event->cpu = cpu; |
| 8752 | event->attr = *attr; |
| 8753 | event->group_leader = group_leader; |
| 8754 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8755 | event->oncpu = -1; |
| 8756 | |
| 8757 | event->parent = parent_event; |
| 8758 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 8759 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8760 | event->id = atomic64_inc_return(&perf_event_id); |
| 8761 | |
| 8762 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 8763 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8764 | if (task) { |
| 8765 | event->attach_state = PERF_ATTACH_TASK; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8766 | /* |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 8767 | * XXX pmu::event_init needs to know what task to account to |
| 8768 | * and we cannot use the ctx information because we need the |
| 8769 | * pmu before we get a ctx. |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8770 | */ |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 8771 | event->hw.target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8772 | } |
| 8773 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8774 | event->clock = &local_clock; |
| 8775 | if (parent_event) |
| 8776 | event->clock = parent_event->clock; |
| 8777 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8778 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 8779 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8780 | context = parent_event->overflow_handler_context; |
| 8781 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 8782 | |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 8783 | if (overflow_handler) { |
| 8784 | event->overflow_handler = overflow_handler; |
| 8785 | event->overflow_handler_context = context; |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 8786 | } else if (is_write_backward(event)){ |
| 8787 | event->overflow_handler = perf_event_output_backward; |
| 8788 | event->overflow_handler_context = NULL; |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 8789 | } else { |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 8790 | event->overflow_handler = perf_event_output_forward; |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 8791 | event->overflow_handler_context = NULL; |
| 8792 | } |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 8793 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 8794 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8795 | |
| 8796 | pmu = NULL; |
| 8797 | |
| 8798 | hwc = &event->hw; |
| 8799 | hwc->sample_period = attr->sample_period; |
| 8800 | if (attr->freq && attr->sample_freq) |
| 8801 | hwc->sample_period = 1; |
| 8802 | hwc->last_period = hwc->sample_period; |
| 8803 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 8804 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8805 | |
| 8806 | /* |
| 8807 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 8808 | */ |
| 8809 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8810 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8811 | |
Yan, Zheng | a46a230 | 2014-11-04 21:56:06 -0500 | [diff] [blame] | 8812 | if (!has_branch_stack(event)) |
| 8813 | event->attr.branch_sample_type = 0; |
| 8814 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8815 | if (cgroup_fd != -1) { |
| 8816 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); |
| 8817 | if (err) |
| 8818 | goto err_ns; |
| 8819 | } |
| 8820 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8821 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8822 | if (!pmu) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8823 | goto err_ns; |
| 8824 | else if (IS_ERR(pmu)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8825 | err = PTR_ERR(pmu); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8826 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8827 | } |
| 8828 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8829 | err = exclusive_event_init(event); |
| 8830 | if (err) |
| 8831 | goto err_pmu; |
| 8832 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8833 | if (has_addr_filter(event)) { |
| 8834 | event->addr_filters_offs = kcalloc(pmu->nr_addr_filters, |
| 8835 | sizeof(unsigned long), |
| 8836 | GFP_KERNEL); |
| 8837 | if (!event->addr_filters_offs) |
| 8838 | goto err_per_task; |
| 8839 | |
| 8840 | /* force hw sync on the address filters */ |
| 8841 | event->addr_filters_gen = 1; |
| 8842 | } |
| 8843 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8844 | if (!event->parent) { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 8845 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
Arnaldo Carvalho de Melo | 97c79a3 | 2016-04-28 13:16:33 -0300 | [diff] [blame^] | 8846 | err = get_callchain_buffers(attr->sample_max_stack); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8847 | if (err) |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8848 | goto err_addr_filters; |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 8849 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8850 | } |
| 8851 | |
Alexander Shishkin | 927a557 | 2016-03-02 13:24:14 +0200 | [diff] [blame] | 8852 | /* symmetric to unaccount_event() in _free_event() */ |
| 8853 | account_event(event); |
| 8854 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8855 | return event; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8856 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8857 | err_addr_filters: |
| 8858 | kfree(event->addr_filters_offs); |
| 8859 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8860 | err_per_task: |
| 8861 | exclusive_event_destroy(event); |
| 8862 | |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8863 | err_pmu: |
| 8864 | if (event->destroy) |
| 8865 | event->destroy(event); |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 8866 | module_put(pmu->module); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8867 | err_ns: |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8868 | if (is_cgroup_event(event)) |
| 8869 | perf_detach_cgroup(event); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8870 | if (event->ns) |
| 8871 | put_pid_ns(event->ns); |
| 8872 | kfree(event); |
| 8873 | |
| 8874 | return ERR_PTR(err); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8875 | } |
| 8876 | |
| 8877 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 8878 | struct perf_event_attr *attr) |
| 8879 | { |
| 8880 | u32 size; |
| 8881 | int ret; |
| 8882 | |
| 8883 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 8884 | return -EFAULT; |
| 8885 | |
| 8886 | /* |
| 8887 | * zero the full structure, so that a short copy will be nice. |
| 8888 | */ |
| 8889 | memset(attr, 0, sizeof(*attr)); |
| 8890 | |
| 8891 | ret = get_user(size, &uattr->size); |
| 8892 | if (ret) |
| 8893 | return ret; |
| 8894 | |
| 8895 | if (size > PAGE_SIZE) /* silly large */ |
| 8896 | goto err_size; |
| 8897 | |
| 8898 | if (!size) /* abi compat */ |
| 8899 | size = PERF_ATTR_SIZE_VER0; |
| 8900 | |
| 8901 | if (size < PERF_ATTR_SIZE_VER0) |
| 8902 | goto err_size; |
| 8903 | |
| 8904 | /* |
| 8905 | * If we're handed a bigger struct than we know of, |
| 8906 | * ensure all the unknown bits are 0 - i.e. new |
| 8907 | * user-space does not rely on any kernel feature |
| 8908 | * extensions we dont know about yet. |
| 8909 | */ |
| 8910 | if (size > sizeof(*attr)) { |
| 8911 | unsigned char __user *addr; |
| 8912 | unsigned char __user *end; |
| 8913 | unsigned char val; |
| 8914 | |
| 8915 | addr = (void __user *)uattr + sizeof(*attr); |
| 8916 | end = (void __user *)uattr + size; |
| 8917 | |
| 8918 | for (; addr < end; addr++) { |
| 8919 | ret = get_user(val, addr); |
| 8920 | if (ret) |
| 8921 | return ret; |
| 8922 | if (val) |
| 8923 | goto err_size; |
| 8924 | } |
| 8925 | size = sizeof(*attr); |
| 8926 | } |
| 8927 | |
| 8928 | ret = copy_from_user(attr, uattr, size); |
| 8929 | if (ret) |
| 8930 | return -EFAULT; |
| 8931 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 8932 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8933 | return -EINVAL; |
| 8934 | |
| 8935 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 8936 | return -EINVAL; |
| 8937 | |
| 8938 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 8939 | return -EINVAL; |
| 8940 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 8941 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 8942 | u64 mask = attr->branch_sample_type; |
| 8943 | |
| 8944 | /* only using defined bits */ |
| 8945 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 8946 | return -EINVAL; |
| 8947 | |
| 8948 | /* at least one branch bit must be set */ |
| 8949 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 8950 | return -EINVAL; |
| 8951 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 8952 | /* propagate priv level, when not set for branch */ |
| 8953 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 8954 | |
| 8955 | /* exclude_kernel checked on syscall entry */ |
| 8956 | if (!attr->exclude_kernel) |
| 8957 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 8958 | |
| 8959 | if (!attr->exclude_user) |
| 8960 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 8961 | |
| 8962 | if (!attr->exclude_hv) |
| 8963 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 8964 | /* |
| 8965 | * adjust user setting (for HW filter setup) |
| 8966 | */ |
| 8967 | attr->branch_sample_type = mask; |
| 8968 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 8969 | /* privileged levels capture (kernel, hv): check permissions */ |
| 8970 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 8971 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 8972 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 8973 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 8974 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 8975 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 8976 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 8977 | if (ret) |
| 8978 | return ret; |
| 8979 | } |
| 8980 | |
| 8981 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 8982 | if (!arch_perf_have_user_stack_dump()) |
| 8983 | return -ENOSYS; |
| 8984 | |
| 8985 | /* |
| 8986 | * We have __u32 type for the size, but so far |
| 8987 | * we can only use __u16 as maximum due to the |
| 8988 | * __u16 sample size limit. |
| 8989 | */ |
| 8990 | if (attr->sample_stack_user >= USHRT_MAX) |
| 8991 | ret = -EINVAL; |
| 8992 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
| 8993 | ret = -EINVAL; |
| 8994 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 8995 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 8996 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
| 8997 | ret = perf_reg_validate(attr->sample_regs_intr); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8998 | out: |
| 8999 | return ret; |
| 9000 | |
| 9001 | err_size: |
| 9002 | put_user(sizeof(*attr), &uattr->size); |
| 9003 | ret = -E2BIG; |
| 9004 | goto out; |
| 9005 | } |
| 9006 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9007 | static int |
| 9008 | 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] | 9009 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 9010 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9011 | int ret = -EINVAL; |
| 9012 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9013 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9014 | goto set; |
| 9015 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9016 | /* don't allow circular references */ |
| 9017 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9018 | goto out; |
| 9019 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 9020 | /* |
| 9021 | * Don't allow cross-cpu buffers |
| 9022 | */ |
| 9023 | if (output_event->cpu != event->cpu) |
| 9024 | goto out; |
| 9025 | |
| 9026 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 9027 | * 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] | 9028 | */ |
| 9029 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 9030 | goto out; |
| 9031 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9032 | /* |
| 9033 | * Mixing clocks in the same buffer is trouble you don't need. |
| 9034 | */ |
| 9035 | if (output_event->clock != event->clock) |
| 9036 | goto out; |
| 9037 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 9038 | /* |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 9039 | * Either writing ring buffer from beginning or from end. |
| 9040 | * Mixing is not allowed. |
| 9041 | */ |
| 9042 | if (is_write_backward(output_event) != is_write_backward(event)) |
| 9043 | goto out; |
| 9044 | |
| 9045 | /* |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 9046 | * If both events generate aux data, they must be on the same PMU |
| 9047 | */ |
| 9048 | if (has_aux(event) && has_aux(output_event) && |
| 9049 | event->pmu != output_event->pmu) |
| 9050 | goto out; |
| 9051 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9052 | set: |
| 9053 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9054 | /* Can't redirect output if we've got an active mmap() */ |
| 9055 | if (atomic_read(&event->mmap_count)) |
| 9056 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9057 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9058 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 9059 | /* get the rb we want to redirect to */ |
| 9060 | rb = ring_buffer_get(output_event); |
| 9061 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9062 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9063 | } |
| 9064 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 9065 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 9066 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9067 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9068 | unlock: |
| 9069 | mutex_unlock(&event->mmap_mutex); |
| 9070 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9071 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9072 | return ret; |
| 9073 | } |
| 9074 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9075 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
| 9076 | { |
| 9077 | if (b < a) |
| 9078 | swap(a, b); |
| 9079 | |
| 9080 | mutex_lock(a); |
| 9081 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); |
| 9082 | } |
| 9083 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9084 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
| 9085 | { |
| 9086 | bool nmi_safe = false; |
| 9087 | |
| 9088 | switch (clk_id) { |
| 9089 | case CLOCK_MONOTONIC: |
| 9090 | event->clock = &ktime_get_mono_fast_ns; |
| 9091 | nmi_safe = true; |
| 9092 | break; |
| 9093 | |
| 9094 | case CLOCK_MONOTONIC_RAW: |
| 9095 | event->clock = &ktime_get_raw_fast_ns; |
| 9096 | nmi_safe = true; |
| 9097 | break; |
| 9098 | |
| 9099 | case CLOCK_REALTIME: |
| 9100 | event->clock = &ktime_get_real_ns; |
| 9101 | break; |
| 9102 | |
| 9103 | case CLOCK_BOOTTIME: |
| 9104 | event->clock = &ktime_get_boot_ns; |
| 9105 | break; |
| 9106 | |
| 9107 | case CLOCK_TAI: |
| 9108 | event->clock = &ktime_get_tai_ns; |
| 9109 | break; |
| 9110 | |
| 9111 | default: |
| 9112 | return -EINVAL; |
| 9113 | } |
| 9114 | |
| 9115 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) |
| 9116 | return -EINVAL; |
| 9117 | |
| 9118 | return 0; |
| 9119 | } |
| 9120 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9121 | /** |
| 9122 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 9123 | * |
| 9124 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 9125 | * @pid: target pid |
| 9126 | * @cpu: target cpu |
| 9127 | * @group_fd: group leader event fd |
| 9128 | */ |
| 9129 | SYSCALL_DEFINE5(perf_event_open, |
| 9130 | struct perf_event_attr __user *, attr_uptr, |
| 9131 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 9132 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9133 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 9134 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9135 | struct perf_event_attr attr; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9136 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9137 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9138 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 9139 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9140 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9141 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9142 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9143 | int err; |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 9144 | int f_flags = O_RDWR; |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9145 | int cgroup_fd = -1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9146 | |
| 9147 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9148 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9149 | return -EINVAL; |
| 9150 | |
| 9151 | err = perf_copy_attr(attr_uptr, &attr); |
| 9152 | if (err) |
| 9153 | return err; |
| 9154 | |
| 9155 | if (!attr.exclude_kernel) { |
| 9156 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 9157 | return -EACCES; |
| 9158 | } |
| 9159 | |
| 9160 | if (attr.freq) { |
| 9161 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 9162 | return -EINVAL; |
Peter Zijlstra | 0819b2e | 2014-05-15 20:23:48 +0200 | [diff] [blame] | 9163 | } else { |
| 9164 | if (attr.sample_period & (1ULL << 63)) |
| 9165 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9166 | } |
| 9167 | |
Arnaldo Carvalho de Melo | 97c79a3 | 2016-04-28 13:16:33 -0300 | [diff] [blame^] | 9168 | if (!attr.sample_max_stack) |
| 9169 | attr.sample_max_stack = sysctl_perf_event_max_stack; |
| 9170 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9171 | /* |
| 9172 | * In cgroup mode, the pid argument is used to pass the fd |
| 9173 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 9174 | * designates the cpu on which to monitor threads from that |
| 9175 | * cgroup. |
| 9176 | */ |
| 9177 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 9178 | return -EINVAL; |
| 9179 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 9180 | if (flags & PERF_FLAG_FD_CLOEXEC) |
| 9181 | f_flags |= O_CLOEXEC; |
| 9182 | |
| 9183 | event_fd = get_unused_fd_flags(f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9184 | if (event_fd < 0) |
| 9185 | return event_fd; |
| 9186 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9187 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9188 | err = perf_fget_light(group_fd, &group); |
| 9189 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 9190 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9191 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9192 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 9193 | output_event = group_leader; |
| 9194 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 9195 | group_leader = NULL; |
| 9196 | } |
| 9197 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9198 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 9199 | task = find_lively_task_by_vpid(pid); |
| 9200 | if (IS_ERR(task)) { |
| 9201 | err = PTR_ERR(task); |
| 9202 | goto err_group_fd; |
| 9203 | } |
| 9204 | } |
| 9205 | |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 9206 | if (task && group_leader && |
| 9207 | group_leader->attr.inherit != attr.inherit) { |
| 9208 | err = -EINVAL; |
| 9209 | goto err_task; |
| 9210 | } |
| 9211 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 9212 | get_online_cpus(); |
| 9213 | |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9214 | if (task) { |
| 9215 | err = mutex_lock_interruptible(&task->signal->cred_guard_mutex); |
| 9216 | if (err) |
| 9217 | goto err_cpus; |
| 9218 | |
| 9219 | /* |
| 9220 | * Reuse ptrace permission checks for now. |
| 9221 | * |
| 9222 | * We must hold cred_guard_mutex across this and any potential |
| 9223 | * perf_install_in_context() call for this new event to |
| 9224 | * serialize against exec() altering our credentials (and the |
| 9225 | * perf_event_exit_task() that could imply). |
| 9226 | */ |
| 9227 | err = -EACCES; |
| 9228 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) |
| 9229 | goto err_cred; |
| 9230 | } |
| 9231 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9232 | if (flags & PERF_FLAG_PID_CGROUP) |
| 9233 | cgroup_fd = pid; |
| 9234 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9235 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9236 | NULL, NULL, cgroup_fd); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 9237 | if (IS_ERR(event)) { |
| 9238 | err = PTR_ERR(event); |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9239 | goto err_cred; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 9240 | } |
| 9241 | |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 9242 | if (is_sampling_event(event)) { |
| 9243 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { |
| 9244 | err = -ENOTSUPP; |
| 9245 | goto err_alloc; |
| 9246 | } |
| 9247 | } |
| 9248 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9249 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9250 | * Special case software events and allow them to be part of |
| 9251 | * any hardware group. |
| 9252 | */ |
| 9253 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9254 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9255 | if (attr.use_clockid) { |
| 9256 | err = perf_event_set_clock(event, attr.clockid); |
| 9257 | if (err) |
| 9258 | goto err_alloc; |
| 9259 | } |
| 9260 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9261 | if (group_leader && |
| 9262 | (is_software_event(event) != is_software_event(group_leader))) { |
| 9263 | if (is_software_event(event)) { |
| 9264 | /* |
| 9265 | * If event and group_leader are not both a software |
| 9266 | * event, and event is, then group leader is not. |
| 9267 | * |
| 9268 | * Allow the addition of software events to !software |
| 9269 | * groups, this is safe because software events never |
| 9270 | * fail to schedule. |
| 9271 | */ |
| 9272 | pmu = group_leader->pmu; |
| 9273 | } else if (is_software_event(group_leader) && |
| 9274 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 9275 | /* |
| 9276 | * In case the group is a pure software group, and we |
| 9277 | * try to add a hardware event, move the whole group to |
| 9278 | * the hardware context. |
| 9279 | */ |
| 9280 | move_group = 1; |
| 9281 | } |
| 9282 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9283 | |
| 9284 | /* |
| 9285 | * Get the target context (task or percpu): |
| 9286 | */ |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 9287 | ctx = find_get_context(pmu, task, event); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9288 | if (IS_ERR(ctx)) { |
| 9289 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 9290 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9291 | } |
| 9292 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9293 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
| 9294 | err = -EBUSY; |
| 9295 | goto err_context; |
| 9296 | } |
| 9297 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9298 | /* |
| 9299 | * Look up the group leader (we will attach this event to it): |
| 9300 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9301 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9302 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9303 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9304 | /* |
| 9305 | * Do not allow a recursive hierarchy (this new sibling |
| 9306 | * becoming part of another group-sibling): |
| 9307 | */ |
| 9308 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9309 | goto err_context; |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9310 | |
| 9311 | /* All events in a group should have the same clock */ |
| 9312 | if (group_leader->clock != event->clock) |
| 9313 | goto err_context; |
| 9314 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9315 | /* |
| 9316 | * Do not allow to attach to a group in a different |
| 9317 | * task or CPU context: |
| 9318 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9319 | if (move_group) { |
Peter Zijlstra | c3c87e7 | 2015-01-23 11:19:48 +0100 | [diff] [blame] | 9320 | /* |
| 9321 | * Make sure we're both on the same task, or both |
| 9322 | * per-cpu events. |
| 9323 | */ |
| 9324 | if (group_leader->ctx->task != ctx->task) |
| 9325 | goto err_context; |
| 9326 | |
| 9327 | /* |
| 9328 | * Make sure we're both events for the same CPU; |
| 9329 | * grouping events for different CPUs is broken; since |
| 9330 | * you can never concurrently schedule them anyhow. |
| 9331 | */ |
| 9332 | if (group_leader->cpu != event->cpu) |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9333 | goto err_context; |
| 9334 | } else { |
| 9335 | if (group_leader->ctx != ctx) |
| 9336 | goto err_context; |
| 9337 | } |
| 9338 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9339 | /* |
| 9340 | * Only a group leader can be exclusive or pinned |
| 9341 | */ |
| 9342 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9343 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9344 | } |
| 9345 | |
| 9346 | if (output_event) { |
| 9347 | err = perf_event_set_output(event, output_event); |
| 9348 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9349 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9350 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9351 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 9352 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
| 9353 | f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9354 | if (IS_ERR(event_file)) { |
| 9355 | err = PTR_ERR(event_file); |
Alexander Shishkin | 201c2f8 | 2016-03-21 10:02:42 +0200 | [diff] [blame] | 9356 | event_file = NULL; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9357 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9358 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9359 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9360 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9361 | gctx = group_leader->ctx; |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9362 | mutex_lock_double(&gctx->mutex, &ctx->mutex); |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9363 | if (gctx->task == TASK_TOMBSTONE) { |
| 9364 | err = -ESRCH; |
| 9365 | goto err_locked; |
| 9366 | } |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9367 | } else { |
| 9368 | mutex_lock(&ctx->mutex); |
| 9369 | } |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9370 | |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9371 | if (ctx->task == TASK_TOMBSTONE) { |
| 9372 | err = -ESRCH; |
| 9373 | goto err_locked; |
| 9374 | } |
| 9375 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9376 | if (!perf_event_validate_size(event)) { |
| 9377 | err = -E2BIG; |
| 9378 | goto err_locked; |
| 9379 | } |
| 9380 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9381 | /* |
| 9382 | * Must be under the same ctx::mutex as perf_install_in_context(), |
| 9383 | * because we need to serialize with concurrent event creation. |
| 9384 | */ |
| 9385 | if (!exclusive_event_installable(event, ctx)) { |
| 9386 | /* exclusive and group stuff are assumed mutually exclusive */ |
| 9387 | WARN_ON_ONCE(move_group); |
| 9388 | |
| 9389 | err = -EBUSY; |
| 9390 | goto err_locked; |
| 9391 | } |
| 9392 | |
| 9393 | WARN_ON_ONCE(ctx->parent_ctx); |
| 9394 | |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9395 | /* |
| 9396 | * This is the point on no return; we cannot fail hereafter. This is |
| 9397 | * where we start modifying current state. |
| 9398 | */ |
| 9399 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9400 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9401 | /* |
| 9402 | * See perf_event_ctx_lock() for comments on the details |
| 9403 | * of swizzling perf_event::ctx. |
| 9404 | */ |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 9405 | perf_remove_from_context(group_leader, 0); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 9406 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9407 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 9408 | group_entry) { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 9409 | perf_remove_from_context(sibling, 0); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9410 | put_ctx(gctx); |
| 9411 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9412 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9413 | /* |
| 9414 | * Wait for everybody to stop referencing the events through |
| 9415 | * the old lists, before installing it on new lists. |
| 9416 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9417 | synchronize_rcu(); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9418 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9419 | /* |
| 9420 | * Install the group siblings before the group leader. |
| 9421 | * |
| 9422 | * Because a group leader will try and install the entire group |
| 9423 | * (through the sibling list, which is still in-tact), we can |
| 9424 | * end up with siblings installed in the wrong context. |
| 9425 | * |
| 9426 | * By installing siblings first we NO-OP because they're not |
| 9427 | * reachable through the group lists. |
| 9428 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9429 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 9430 | group_entry) { |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9431 | perf_event__state_init(sibling); |
Jiri Olsa | 9fc81d8 | 2014-12-10 21:23:51 +0100 | [diff] [blame] | 9432 | perf_install_in_context(ctx, sibling, sibling->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9433 | get_ctx(ctx); |
| 9434 | } |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9435 | |
| 9436 | /* |
| 9437 | * Removing from the context ends up with disabled |
| 9438 | * event. What we want here is event in the initial |
| 9439 | * startup state, ready to be add into new context. |
| 9440 | */ |
| 9441 | perf_event__state_init(group_leader); |
| 9442 | perf_install_in_context(ctx, group_leader, group_leader->cpu); |
| 9443 | get_ctx(ctx); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9444 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9445 | /* |
| 9446 | * Now that all events are installed in @ctx, nothing |
| 9447 | * references @gctx anymore, so drop the last reference we have |
| 9448 | * on it. |
| 9449 | */ |
| 9450 | put_ctx(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9451 | } |
| 9452 | |
Peter Zijlstra | f73e22a | 2015-09-09 20:48:22 +0200 | [diff] [blame] | 9453 | /* |
| 9454 | * Precalculate sample_data sizes; do while holding ctx::mutex such |
| 9455 | * that we're serialized against further additions and before |
| 9456 | * perf_install_in_context() which is the point the event is active and |
| 9457 | * can use these values. |
| 9458 | */ |
| 9459 | perf_event__header_size(event); |
| 9460 | perf_event__id_header_size(event); |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9461 | |
Peter Zijlstra | 78cd2c7 | 2016-01-25 14:08:45 +0100 | [diff] [blame] | 9462 | event->owner = current; |
| 9463 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 9464 | perf_install_in_context(ctx, event, event->cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 9465 | perf_unpin_context(ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9466 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9467 | if (move_group) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9468 | mutex_unlock(&gctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9469 | mutex_unlock(&ctx->mutex); |
| 9470 | |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9471 | if (task) { |
| 9472 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 9473 | put_task_struct(task); |
| 9474 | } |
| 9475 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 9476 | put_online_cpus(); |
| 9477 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9478 | mutex_lock(¤t->perf_event_mutex); |
| 9479 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 9480 | mutex_unlock(¤t->perf_event_mutex); |
| 9481 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 9482 | /* |
| 9483 | * Drop the reference on the group_event after placing the |
| 9484 | * new event on the sibling_list. This ensures destruction |
| 9485 | * of the group leader will find the pointer to itself in |
| 9486 | * perf_group_detach(). |
| 9487 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9488 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9489 | fd_install(event_fd, event_file); |
| 9490 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9491 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9492 | err_locked: |
| 9493 | if (move_group) |
| 9494 | mutex_unlock(&gctx->mutex); |
| 9495 | mutex_unlock(&ctx->mutex); |
| 9496 | /* err_file: */ |
| 9497 | fput(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9498 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 9499 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9500 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 9501 | err_alloc: |
Peter Zijlstra | 1300562 | 2016-02-24 18:45:41 +0100 | [diff] [blame] | 9502 | /* |
| 9503 | * If event_file is set, the fput() above will have called ->release() |
| 9504 | * and that will take care of freeing the event. |
| 9505 | */ |
| 9506 | if (!event_file) |
| 9507 | free_event(event); |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9508 | err_cred: |
| 9509 | if (task) |
| 9510 | mutex_unlock(&task->signal->cred_guard_mutex); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 9511 | err_cpus: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 9512 | put_online_cpus(); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 9513 | err_task: |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 9514 | if (task) |
| 9515 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9516 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9517 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9518 | err_fd: |
| 9519 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9520 | return err; |
| 9521 | } |
| 9522 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9523 | /** |
| 9524 | * perf_event_create_kernel_counter |
| 9525 | * |
| 9526 | * @attr: attributes of the counter to create |
| 9527 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 9528 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9529 | */ |
| 9530 | struct perf_event * |
| 9531 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 9532 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9533 | perf_overflow_handler_t overflow_handler, |
| 9534 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9535 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9536 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9537 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9538 | int err; |
| 9539 | |
| 9540 | /* |
| 9541 | * Get the target context (task or percpu): |
| 9542 | */ |
| 9543 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9544 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9545 | overflow_handler, context, -1); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 9546 | if (IS_ERR(event)) { |
| 9547 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9548 | goto err; |
| 9549 | } |
| 9550 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 9551 | /* Mark owner so we could distinguish it from user events. */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9552 | event->owner = TASK_TOMBSTONE; |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 9553 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 9554 | ctx = find_get_context(event->pmu, task, event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9555 | if (IS_ERR(ctx)) { |
| 9556 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9557 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 9558 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9559 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9560 | WARN_ON_ONCE(ctx->parent_ctx); |
| 9561 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9562 | if (ctx->task == TASK_TOMBSTONE) { |
| 9563 | err = -ESRCH; |
| 9564 | goto err_unlock; |
| 9565 | } |
| 9566 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9567 | if (!exclusive_event_installable(event, ctx)) { |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9568 | err = -EBUSY; |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9569 | goto err_unlock; |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9570 | } |
| 9571 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9572 | perf_install_in_context(ctx, event, cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 9573 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9574 | mutex_unlock(&ctx->mutex); |
| 9575 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9576 | return event; |
| 9577 | |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9578 | err_unlock: |
| 9579 | mutex_unlock(&ctx->mutex); |
| 9580 | perf_unpin_context(ctx); |
| 9581 | put_ctx(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9582 | err_free: |
| 9583 | free_event(event); |
| 9584 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 9585 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9586 | } |
| 9587 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 9588 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9589 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 9590 | { |
| 9591 | struct perf_event_context *src_ctx; |
| 9592 | struct perf_event_context *dst_ctx; |
| 9593 | struct perf_event *event, *tmp; |
| 9594 | LIST_HEAD(events); |
| 9595 | |
| 9596 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 9597 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 9598 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9599 | /* |
| 9600 | * See perf_event_ctx_lock() for comments on the details |
| 9601 | * of swizzling perf_event::ctx. |
| 9602 | */ |
| 9603 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9604 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 9605 | event_entry) { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 9606 | perf_remove_from_context(event, 0); |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 9607 | unaccount_event_cpu(event, src_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9608 | put_ctx(src_ctx); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 9609 | list_add(&event->migrate_entry, &events); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9610 | } |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9611 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9612 | /* |
| 9613 | * Wait for the events to quiesce before re-instating them. |
| 9614 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9615 | synchronize_rcu(); |
| 9616 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9617 | /* |
| 9618 | * Re-instate events in 2 passes. |
| 9619 | * |
| 9620 | * Skip over group leaders and only install siblings on this first |
| 9621 | * pass, siblings will not get enabled without a leader, however a |
| 9622 | * leader will enable its siblings, even if those are still on the old |
| 9623 | * context. |
| 9624 | */ |
| 9625 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 9626 | if (event->group_leader == event) |
| 9627 | continue; |
| 9628 | |
| 9629 | list_del(&event->migrate_entry); |
| 9630 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 9631 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 9632 | account_event_cpu(event, dst_cpu); |
| 9633 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 9634 | get_ctx(dst_ctx); |
| 9635 | } |
| 9636 | |
| 9637 | /* |
| 9638 | * Once all the siblings are setup properly, install the group leaders |
| 9639 | * to make it go. |
| 9640 | */ |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 9641 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 9642 | list_del(&event->migrate_entry); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9643 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 9644 | event->state = PERF_EVENT_STATE_INACTIVE; |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 9645 | account_event_cpu(event, dst_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9646 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 9647 | get_ctx(dst_ctx); |
| 9648 | } |
| 9649 | mutex_unlock(&dst_ctx->mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9650 | mutex_unlock(&src_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9651 | } |
| 9652 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 9653 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9654 | static void sync_child_event(struct perf_event *child_event, |
| 9655 | struct task_struct *child) |
| 9656 | { |
| 9657 | struct perf_event *parent_event = child_event->parent; |
| 9658 | u64 child_val; |
| 9659 | |
| 9660 | if (child_event->attr.inherit_stat) |
| 9661 | perf_event_read_event(child_event, child); |
| 9662 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 9663 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9664 | |
| 9665 | /* |
| 9666 | * Add back the child's count to the parent's count: |
| 9667 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 9668 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9669 | atomic64_add(child_event->total_time_enabled, |
| 9670 | &parent_event->child_total_time_enabled); |
| 9671 | atomic64_add(child_event->total_time_running, |
| 9672 | &parent_event->child_total_time_running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9673 | } |
| 9674 | |
| 9675 | static void |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9676 | perf_event_exit_event(struct perf_event *child_event, |
| 9677 | struct perf_event_context *child_ctx, |
| 9678 | struct task_struct *child) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9679 | { |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9680 | struct perf_event *parent_event = child_event->parent; |
| 9681 | |
Peter Zijlstra | 1903d50 | 2014-07-15 17:27:27 +0200 | [diff] [blame] | 9682 | /* |
| 9683 | * Do not destroy the 'original' grouping; because of the context |
| 9684 | * switch optimization the original events could've ended up in a |
| 9685 | * random child task. |
| 9686 | * |
| 9687 | * If we were to destroy the original group, all group related |
| 9688 | * operations would cease to function properly after this random |
| 9689 | * child dies. |
| 9690 | * |
| 9691 | * Do destroy all inherited groups, we don't care about those |
| 9692 | * and being thorough is better. |
| 9693 | */ |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 9694 | raw_spin_lock_irq(&child_ctx->lock); |
| 9695 | WARN_ON_ONCE(child_ctx->is_active); |
| 9696 | |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9697 | if (parent_event) |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 9698 | perf_group_detach(child_event); |
| 9699 | list_del_event(child_event, child_ctx); |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 9700 | child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */ |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 9701 | raw_spin_unlock_irq(&child_ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9702 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9703 | /* |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9704 | * Parent events are governed by their filedesc, retain them. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9705 | */ |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9706 | if (!parent_event) { |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 9707 | perf_event_wakeup(child_event); |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9708 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9709 | } |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9710 | /* |
| 9711 | * Child events can be cleaned up. |
| 9712 | */ |
| 9713 | |
| 9714 | sync_child_event(child_event, child); |
| 9715 | |
| 9716 | /* |
| 9717 | * Remove this event from the parent's list |
| 9718 | */ |
| 9719 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 9720 | mutex_lock(&parent_event->child_mutex); |
| 9721 | list_del_init(&child_event->child_list); |
| 9722 | mutex_unlock(&parent_event->child_mutex); |
| 9723 | |
| 9724 | /* |
| 9725 | * Kick perf_poll() for is_event_hup(). |
| 9726 | */ |
| 9727 | perf_event_wakeup(parent_event); |
| 9728 | free_event(child_event); |
| 9729 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9730 | } |
| 9731 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9732 | 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] | 9733 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 9734 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9735 | struct perf_event *child_event, *next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9736 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9737 | WARN_ON_ONCE(child != current); |
| 9738 | |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9739 | child_ctx = perf_pin_task_context(child, ctxn); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9740 | if (!child_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9741 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9742 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9743 | /* |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9744 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
| 9745 | * ctx::mutex over the entire thing. This serializes against almost |
| 9746 | * everything that wants to access the ctx. |
| 9747 | * |
| 9748 | * The exception is sys_perf_event_open() / |
| 9749 | * perf_event_create_kernel_count() which does find_get_context() |
| 9750 | * without ctx::mutex (it cannot because of the move_group double mutex |
| 9751 | * lock thing). See the comments in perf_install_in_context(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9752 | */ |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9753 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9754 | |
| 9755 | /* |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9756 | * In a single ctx::lock section, de-schedule the events and detach the |
| 9757 | * context from the task such that we cannot ever get it scheduled back |
| 9758 | * in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9759 | */ |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9760 | raw_spin_lock_irq(&child_ctx->lock); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9761 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 9762 | |
| 9763 | /* |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9764 | * Now that the context is inactive, destroy the task <-> ctx relation |
| 9765 | * and mark the context dead. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9766 | */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9767 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
| 9768 | put_ctx(child_ctx); /* cannot be last */ |
| 9769 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); |
| 9770 | put_task_struct(current); /* cannot be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9771 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 9772 | clone_ctx = unclone_ctx(child_ctx); |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9773 | raw_spin_unlock_irq(&child_ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9774 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 9775 | if (clone_ctx) |
| 9776 | put_ctx(clone_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 9777 | |
| 9778 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9779 | * Report the task dead after unscheduling the events so that we |
| 9780 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 9781 | * get a few PERF_RECORD_READ events. |
| 9782 | */ |
| 9783 | perf_event_task(child, child_ctx, 0); |
| 9784 | |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 9785 | list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9786 | perf_event_exit_event(child_event, child_ctx, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9787 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9788 | mutex_unlock(&child_ctx->mutex); |
| 9789 | |
| 9790 | put_ctx(child_ctx); |
| 9791 | } |
| 9792 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9793 | /* |
| 9794 | * When a child task exits, feed back event values to parent events. |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9795 | * |
| 9796 | * Can be called with cred_guard_mutex held when called from |
| 9797 | * install_exec_creds(). |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9798 | */ |
| 9799 | void perf_event_exit_task(struct task_struct *child) |
| 9800 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 9801 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9802 | int ctxn; |
| 9803 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 9804 | mutex_lock(&child->perf_event_mutex); |
| 9805 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 9806 | owner_entry) { |
| 9807 | list_del_init(&event->owner_entry); |
| 9808 | |
| 9809 | /* |
| 9810 | * Ensure the list deletion is visible before we clear |
| 9811 | * the owner, closes a race against perf_release() where |
| 9812 | * we need to serialize on the owner->perf_event_mutex. |
| 9813 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 9814 | smp_store_release(&event->owner, NULL); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 9815 | } |
| 9816 | mutex_unlock(&child->perf_event_mutex); |
| 9817 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9818 | for_each_task_context_nr(ctxn) |
| 9819 | perf_event_exit_task_context(child, ctxn); |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 9820 | |
| 9821 | /* |
| 9822 | * The perf_event_exit_task_context calls perf_event_task |
| 9823 | * with child's task_ctx, which generates EXIT events for |
| 9824 | * child contexts and sets child->perf_event_ctxp[] to NULL. |
| 9825 | * At this point we need to send EXIT events to cpu contexts. |
| 9826 | */ |
| 9827 | perf_event_task(child, NULL, 0); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9828 | } |
| 9829 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9830 | static void perf_free_event(struct perf_event *event, |
| 9831 | struct perf_event_context *ctx) |
| 9832 | { |
| 9833 | struct perf_event *parent = event->parent; |
| 9834 | |
| 9835 | if (WARN_ON_ONCE(!parent)) |
| 9836 | return; |
| 9837 | |
| 9838 | mutex_lock(&parent->child_mutex); |
| 9839 | list_del_init(&event->child_list); |
| 9840 | mutex_unlock(&parent->child_mutex); |
| 9841 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 9842 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9843 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9844 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 9845 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9846 | list_del_event(event, ctx); |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9847 | raw_spin_unlock_irq(&ctx->lock); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9848 | free_event(event); |
| 9849 | } |
| 9850 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9851 | /* |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9852 | * Free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9853 | * perf_event_init_task below, used by fork() in case of fail. |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9854 | * |
| 9855 | * Not all locks are strictly required, but take them anyway to be nice and |
| 9856 | * help out with the lockdep assertions. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9857 | */ |
| 9858 | void perf_event_free_task(struct task_struct *task) |
| 9859 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9860 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9861 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9862 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9863 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9864 | for_each_task_context_nr(ctxn) { |
| 9865 | ctx = task->perf_event_ctxp[ctxn]; |
| 9866 | if (!ctx) |
| 9867 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9868 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9869 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9870 | again: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9871 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 9872 | group_entry) |
| 9873 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9874 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9875 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 9876 | group_entry) |
| 9877 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9878 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9879 | if (!list_empty(&ctx->pinned_groups) || |
| 9880 | !list_empty(&ctx->flexible_groups)) |
| 9881 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9882 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9883 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9884 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9885 | put_ctx(ctx); |
| 9886 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9887 | } |
| 9888 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 9889 | void perf_event_delayed_put(struct task_struct *task) |
| 9890 | { |
| 9891 | int ctxn; |
| 9892 | |
| 9893 | for_each_task_context_nr(ctxn) |
| 9894 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 9895 | } |
| 9896 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9897 | struct file *perf_event_get(unsigned int fd) |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9898 | { |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9899 | struct file *file; |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9900 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9901 | file = fget_raw(fd); |
| 9902 | if (!file) |
| 9903 | return ERR_PTR(-EBADF); |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9904 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9905 | if (file->f_op != &perf_fops) { |
| 9906 | fput(file); |
| 9907 | return ERR_PTR(-EBADF); |
| 9908 | } |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9909 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9910 | return file; |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9911 | } |
| 9912 | |
| 9913 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) |
| 9914 | { |
| 9915 | if (!event) |
| 9916 | return ERR_PTR(-EINVAL); |
| 9917 | |
| 9918 | return &event->attr; |
| 9919 | } |
| 9920 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9921 | /* |
| 9922 | * inherit a event from parent task to child task: |
| 9923 | */ |
| 9924 | static struct perf_event * |
| 9925 | inherit_event(struct perf_event *parent_event, |
| 9926 | struct task_struct *parent, |
| 9927 | struct perf_event_context *parent_ctx, |
| 9928 | struct task_struct *child, |
| 9929 | struct perf_event *group_leader, |
| 9930 | struct perf_event_context *child_ctx) |
| 9931 | { |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 9932 | enum perf_event_active_state parent_state = parent_event->state; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9933 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 9934 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9935 | |
| 9936 | /* |
| 9937 | * Instead of creating recursive hierarchies of events, |
| 9938 | * we link inherited events back to the original parent, |
| 9939 | * which has a filp for sure, which we use as the reference |
| 9940 | * count: |
| 9941 | */ |
| 9942 | if (parent_event->parent) |
| 9943 | parent_event = parent_event->parent; |
| 9944 | |
| 9945 | child_event = perf_event_alloc(&parent_event->attr, |
| 9946 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 9947 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9948 | group_leader, parent_event, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9949 | NULL, NULL, -1); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9950 | if (IS_ERR(child_event)) |
| 9951 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 9952 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 9953 | /* |
| 9954 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) |
| 9955 | * must be under the same lock in order to serialize against |
| 9956 | * perf_event_release_kernel(), such that either we must observe |
| 9957 | * is_orphaned_event() or they will observe us on the child_list. |
| 9958 | */ |
| 9959 | mutex_lock(&parent_event->child_mutex); |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 9960 | if (is_orphaned_event(parent_event) || |
| 9961 | !atomic_long_inc_not_zero(&parent_event->refcount)) { |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 9962 | mutex_unlock(&parent_event->child_mutex); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 9963 | free_event(child_event); |
| 9964 | return NULL; |
| 9965 | } |
| 9966 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9967 | get_ctx(child_ctx); |
| 9968 | |
| 9969 | /* |
| 9970 | * Make the child state follow the state of the parent event, |
| 9971 | * not its attr.disabled bit. We hold the parent's mutex, |
| 9972 | * so we won't race with perf_event_{en, dis}able_family. |
| 9973 | */ |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 9974 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9975 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 9976 | else |
| 9977 | child_event->state = PERF_EVENT_STATE_OFF; |
| 9978 | |
| 9979 | if (parent_event->attr.freq) { |
| 9980 | u64 sample_period = parent_event->hw.sample_period; |
| 9981 | struct hw_perf_event *hwc = &child_event->hw; |
| 9982 | |
| 9983 | hwc->sample_period = sample_period; |
| 9984 | hwc->last_period = sample_period; |
| 9985 | |
| 9986 | local64_set(&hwc->period_left, sample_period); |
| 9987 | } |
| 9988 | |
| 9989 | child_event->ctx = child_ctx; |
| 9990 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9991 | child_event->overflow_handler_context |
| 9992 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9993 | |
| 9994 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 9995 | * Precalculate sample_data sizes |
| 9996 | */ |
| 9997 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 9998 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 9999 | |
| 10000 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10001 | * Link it up in the child's context: |
| 10002 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 10003 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10004 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 10005 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10006 | |
| 10007 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10008 | * Link this into the parent event's child list |
| 10009 | */ |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10010 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 10011 | mutex_unlock(&parent_event->child_mutex); |
| 10012 | |
| 10013 | return child_event; |
| 10014 | } |
| 10015 | |
| 10016 | static int inherit_group(struct perf_event *parent_event, |
| 10017 | struct task_struct *parent, |
| 10018 | struct perf_event_context *parent_ctx, |
| 10019 | struct task_struct *child, |
| 10020 | struct perf_event_context *child_ctx) |
| 10021 | { |
| 10022 | struct perf_event *leader; |
| 10023 | struct perf_event *sub; |
| 10024 | struct perf_event *child_ctr; |
| 10025 | |
| 10026 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 10027 | child, NULL, child_ctx); |
| 10028 | if (IS_ERR(leader)) |
| 10029 | return PTR_ERR(leader); |
| 10030 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 10031 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 10032 | child, leader, child_ctx); |
| 10033 | if (IS_ERR(child_ctr)) |
| 10034 | return PTR_ERR(child_ctr); |
| 10035 | } |
| 10036 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10037 | } |
| 10038 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10039 | static int |
| 10040 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 10041 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10042 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10043 | int *inherited_all) |
| 10044 | { |
| 10045 | int ret; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10046 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10047 | |
| 10048 | if (!event->attr.inherit) { |
| 10049 | *inherited_all = 0; |
| 10050 | return 0; |
| 10051 | } |
| 10052 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 10053 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10054 | if (!child_ctx) { |
| 10055 | /* |
| 10056 | * This is executed from the parent task context, so |
| 10057 | * inherit events that have been marked for cloning. |
| 10058 | * First allocate and initialize a context for the |
| 10059 | * child. |
| 10060 | */ |
| 10061 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 10062 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10063 | if (!child_ctx) |
| 10064 | return -ENOMEM; |
| 10065 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10066 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10067 | } |
| 10068 | |
| 10069 | ret = inherit_group(event, parent, parent_ctx, |
| 10070 | child, child_ctx); |
| 10071 | |
| 10072 | if (ret) |
| 10073 | *inherited_all = 0; |
| 10074 | |
| 10075 | return ret; |
| 10076 | } |
| 10077 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10078 | /* |
| 10079 | * Initialize the perf_event context in task_struct |
| 10080 | */ |
Jiri Olsa | 985c8dc | 2014-06-24 10:20:24 +0200 | [diff] [blame] | 10081 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10082 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10083 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10084 | struct perf_event_context *cloned_ctx; |
| 10085 | struct perf_event *event; |
| 10086 | struct task_struct *parent = current; |
| 10087 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10088 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10089 | int ret = 0; |
| 10090 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10091 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10092 | return 0; |
| 10093 | |
| 10094 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10095 | * If the parent's context is a clone, pin it so it won't get |
| 10096 | * swapped under us. |
| 10097 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10098 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Peter Zijlstra | ffb4ef2 | 2014-05-05 19:12:20 +0200 | [diff] [blame] | 10099 | if (!parent_ctx) |
| 10100 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10101 | |
| 10102 | /* |
| 10103 | * No need to check if parent_ctx != NULL here; since we saw |
| 10104 | * it non-NULL earlier, the only reason for it to become NULL |
| 10105 | * is if we exit, and since we're currently in the middle of |
| 10106 | * a fork we can't be exiting at the same time. |
| 10107 | */ |
| 10108 | |
| 10109 | /* |
| 10110 | * Lock the parent list. No need to lock the child - not PID |
| 10111 | * hashed yet and not running, so nobody can access it. |
| 10112 | */ |
| 10113 | mutex_lock(&parent_ctx->mutex); |
| 10114 | |
| 10115 | /* |
| 10116 | * We dont have to disable NMIs - we are only looking at |
| 10117 | * the list, not manipulating it: |
| 10118 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10119 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10120 | ret = inherit_task_group(event, parent, parent_ctx, |
| 10121 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10122 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10123 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10124 | } |
| 10125 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10126 | /* |
| 10127 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 10128 | * to allocations, but we need to prevent rotation because |
| 10129 | * rotate_ctx() will change the list from interrupt context. |
| 10130 | */ |
| 10131 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 10132 | parent_ctx->rotate_disable = 1; |
| 10133 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 10134 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10135 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10136 | ret = inherit_task_group(event, parent, parent_ctx, |
| 10137 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10138 | if (ret) |
| 10139 | break; |
| 10140 | } |
| 10141 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10142 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 10143 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10144 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10145 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10146 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 10147 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10148 | /* |
| 10149 | * Mark the child context as a clone of the parent |
| 10150 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 10151 | * |
| 10152 | * Note that if the parent is a clone, the holding of |
| 10153 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10154 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 10155 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10156 | if (cloned_ctx) { |
| 10157 | child_ctx->parent_ctx = cloned_ctx; |
| 10158 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 10159 | } else { |
| 10160 | child_ctx->parent_ctx = parent_ctx; |
| 10161 | child_ctx->parent_gen = parent_ctx->generation; |
| 10162 | } |
| 10163 | get_ctx(child_ctx->parent_ctx); |
| 10164 | } |
| 10165 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 10166 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10167 | mutex_unlock(&parent_ctx->mutex); |
| 10168 | |
| 10169 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 10170 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10171 | |
| 10172 | return ret; |
| 10173 | } |
| 10174 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10175 | /* |
| 10176 | * Initialize the perf_event context in task_struct |
| 10177 | */ |
| 10178 | int perf_event_init_task(struct task_struct *child) |
| 10179 | { |
| 10180 | int ctxn, ret; |
| 10181 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 10182 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 10183 | mutex_init(&child->perf_event_mutex); |
| 10184 | INIT_LIST_HEAD(&child->perf_event_list); |
| 10185 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10186 | for_each_task_context_nr(ctxn) { |
| 10187 | ret = perf_event_init_context(child, ctxn); |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 10188 | if (ret) { |
| 10189 | perf_event_free_task(child); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10190 | return ret; |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 10191 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10192 | } |
| 10193 | |
| 10194 | return 0; |
| 10195 | } |
| 10196 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10197 | static void __init perf_event_init_all_cpus(void) |
| 10198 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10199 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10200 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10201 | |
| 10202 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10203 | swhash = &per_cpu(swevent_htable, cpu); |
| 10204 | mutex_init(&swhash->hlist_mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 10205 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10206 | } |
| 10207 | } |
| 10208 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 10209 | static void perf_event_init_cpu(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10210 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10211 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10212 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10213 | mutex_lock(&swhash->hlist_mutex); |
Thomas Gleixner | 059fcd8 | 2016-02-09 20:11:34 +0000 | [diff] [blame] | 10214 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 10215 | struct swevent_hlist *hlist; |
| 10216 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10217 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 10218 | WARN_ON(!hlist); |
| 10219 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 10220 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10221 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10222 | } |
| 10223 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 10224 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10225 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10226 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10227 | struct perf_event_context *ctx = __info; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 10228 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 10229 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10230 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 10231 | raw_spin_lock(&ctx->lock); |
| 10232 | list_for_each_entry(event, &ctx->event_list, event_entry) |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 10233 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 10234 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10235 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10236 | |
| 10237 | static void perf_event_exit_cpu_context(int cpu) |
| 10238 | { |
| 10239 | struct perf_event_context *ctx; |
| 10240 | struct pmu *pmu; |
| 10241 | int idx; |
| 10242 | |
| 10243 | idx = srcu_read_lock(&pmus_srcu); |
| 10244 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 10245 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10246 | |
| 10247 | mutex_lock(&ctx->mutex); |
| 10248 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 10249 | mutex_unlock(&ctx->mutex); |
| 10250 | } |
| 10251 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10252 | } |
| 10253 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10254 | static void perf_event_exit_cpu(int cpu) |
| 10255 | { |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 10256 | perf_event_exit_cpu_context(cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10257 | } |
| 10258 | #else |
| 10259 | static inline void perf_event_exit_cpu(int cpu) { } |
| 10260 | #endif |
| 10261 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 10262 | static int |
| 10263 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 10264 | { |
| 10265 | int cpu; |
| 10266 | |
| 10267 | for_each_online_cpu(cpu) |
| 10268 | perf_event_exit_cpu(cpu); |
| 10269 | |
| 10270 | return NOTIFY_OK; |
| 10271 | } |
| 10272 | |
| 10273 | /* |
| 10274 | * Run the perf reboot notifier at the very last possible moment so that |
| 10275 | * the generic watchdog code runs as long as possible. |
| 10276 | */ |
| 10277 | static struct notifier_block perf_reboot_notifier = { |
| 10278 | .notifier_call = perf_reboot, |
| 10279 | .priority = INT_MIN, |
| 10280 | }; |
| 10281 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 10282 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10283 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 10284 | { |
| 10285 | unsigned int cpu = (long)hcpu; |
| 10286 | |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 10287 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10288 | |
| 10289 | case CPU_UP_PREPARE: |
Peter Zijlstra | 1dcaac1 | 2016-03-08 17:56:05 +0100 | [diff] [blame] | 10290 | /* |
| 10291 | * This must be done before the CPU comes alive, because the |
| 10292 | * moment we can run tasks we can encounter (software) events. |
| 10293 | * |
| 10294 | * Specifically, someone can have inherited events on kthreadd |
| 10295 | * or a pre-existing worker thread that gets re-bound. |
| 10296 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10297 | perf_event_init_cpu(cpu); |
| 10298 | break; |
| 10299 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10300 | case CPU_DOWN_PREPARE: |
Peter Zijlstra | 1dcaac1 | 2016-03-08 17:56:05 +0100 | [diff] [blame] | 10301 | /* |
| 10302 | * This must be done before the CPU dies because after that an |
| 10303 | * active event might want to IPI the CPU and that'll not work |
| 10304 | * so great for dead CPUs. |
| 10305 | * |
| 10306 | * XXX smp_call_function_single() return -ENXIO without a warn |
| 10307 | * so we could possibly deal with this. |
| 10308 | * |
| 10309 | * This is safe against new events arriving because |
| 10310 | * sys_perf_event_open() serializes against hotplug using |
| 10311 | * get_online_cpus(). |
| 10312 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10313 | perf_event_exit_cpu(cpu); |
| 10314 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10315 | default: |
| 10316 | break; |
| 10317 | } |
| 10318 | |
| 10319 | return NOTIFY_OK; |
| 10320 | } |
| 10321 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10322 | void __init perf_event_init(void) |
| 10323 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 10324 | int ret; |
| 10325 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 10326 | idr_init(&pmu_idr); |
| 10327 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10328 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 10329 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 10330 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 10331 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 10332 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 10333 | perf_tp_register(); |
| 10334 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 10335 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 10336 | |
| 10337 | ret = init_hw_breakpoint(); |
| 10338 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 10339 | |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 10340 | /* |
| 10341 | * Build time assertion that we keep the data_head at the intended |
| 10342 | * location. IOW, validation we got the __reserved[] size right. |
| 10343 | */ |
| 10344 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 10345 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10346 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 10347 | |
Cody P Schafer | fd979c0 | 2015-01-30 13:45:57 -0800 | [diff] [blame] | 10348 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
| 10349 | char *page) |
| 10350 | { |
| 10351 | struct perf_pmu_events_attr *pmu_attr = |
| 10352 | container_of(attr, struct perf_pmu_events_attr, attr); |
| 10353 | |
| 10354 | if (pmu_attr->event_str) |
| 10355 | return sprintf(page, "%s\n", pmu_attr->event_str); |
| 10356 | |
| 10357 | return 0; |
| 10358 | } |
Thomas Gleixner | 675965b | 2016-02-22 22:19:27 +0000 | [diff] [blame] | 10359 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
Cody P Schafer | fd979c0 | 2015-01-30 13:45:57 -0800 | [diff] [blame] | 10360 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 10361 | static int __init perf_event_sysfs_init(void) |
| 10362 | { |
| 10363 | struct pmu *pmu; |
| 10364 | int ret; |
| 10365 | |
| 10366 | mutex_lock(&pmus_lock); |
| 10367 | |
| 10368 | ret = bus_register(&pmu_bus); |
| 10369 | if (ret) |
| 10370 | goto unlock; |
| 10371 | |
| 10372 | list_for_each_entry(pmu, &pmus, entry) { |
| 10373 | if (!pmu->name || pmu->type < 0) |
| 10374 | continue; |
| 10375 | |
| 10376 | ret = pmu_dev_alloc(pmu); |
| 10377 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 10378 | } |
| 10379 | pmu_bus_running = 1; |
| 10380 | ret = 0; |
| 10381 | |
| 10382 | unlock: |
| 10383 | mutex_unlock(&pmus_lock); |
| 10384 | |
| 10385 | return ret; |
| 10386 | } |
| 10387 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10388 | |
| 10389 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 10390 | static struct cgroup_subsys_state * |
| 10391 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10392 | { |
| 10393 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10394 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 10395 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10396 | if (!jc) |
| 10397 | return ERR_PTR(-ENOMEM); |
| 10398 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10399 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 10400 | if (!jc->info) { |
| 10401 | kfree(jc); |
| 10402 | return ERR_PTR(-ENOMEM); |
| 10403 | } |
| 10404 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10405 | return &jc->css; |
| 10406 | } |
| 10407 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 10408 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10409 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 10410 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
| 10411 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10412 | free_percpu(jc->info); |
| 10413 | kfree(jc); |
| 10414 | } |
| 10415 | |
| 10416 | static int __perf_cgroup_move(void *info) |
| 10417 | { |
| 10418 | struct task_struct *task = info; |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 10419 | rcu_read_lock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10420 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 10421 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10422 | return 0; |
| 10423 | } |
| 10424 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 10425 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10426 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10427 | struct task_struct *task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 10428 | struct cgroup_subsys_state *css; |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10429 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 10430 | cgroup_taskset_for_each(task, css, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10431 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10432 | } |
| 10433 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 10434 | struct cgroup_subsys perf_event_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 10435 | .css_alloc = perf_cgroup_css_alloc, |
| 10436 | .css_free = perf_cgroup_css_free, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10437 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10438 | }; |
| 10439 | #endif /* CONFIG_CGROUP_PERF */ |