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 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 245 | 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] | 246 | { |
| 247 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 248 | struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 249 | struct event_function_struct efs = { |
| 250 | .event = event, |
| 251 | .func = func, |
| 252 | .data = data, |
| 253 | }; |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 254 | |
Peter Zijlstra | c97f473 | 2016-01-14 10:51:03 +0100 | [diff] [blame] | 255 | if (!event->parent) { |
| 256 | /* |
| 257 | * If this is a !child event, we must hold ctx::mutex to |
| 258 | * stabilize the the event->ctx relation. See |
| 259 | * perf_event_ctx_lock(). |
| 260 | */ |
| 261 | lockdep_assert_held(&ctx->mutex); |
| 262 | } |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 263 | |
| 264 | if (!task) { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 265 | cpu_function_call(event->cpu, event_function, &efs); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 269 | if (task == TASK_TOMBSTONE) |
| 270 | return; |
| 271 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 272 | again: |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 273 | if (!task_function_call(task, event_function, &efs)) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 274 | return; |
| 275 | |
| 276 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 277 | /* |
| 278 | * Reload the task pointer, it might have been changed by |
| 279 | * a concurrent perf_event_context_sched_out(). |
| 280 | */ |
| 281 | task = ctx->task; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 282 | if (task == TASK_TOMBSTONE) { |
| 283 | raw_spin_unlock_irq(&ctx->lock); |
| 284 | return; |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 285 | } |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 286 | if (ctx->is_active) { |
| 287 | raw_spin_unlock_irq(&ctx->lock); |
| 288 | goto again; |
| 289 | } |
| 290 | func(event, NULL, ctx, data); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 291 | raw_spin_unlock_irq(&ctx->lock); |
| 292 | } |
| 293 | |
Peter Zijlstra | cca2094 | 2016-08-16 13:33:26 +0200 | [diff] [blame] | 294 | /* |
| 295 | * Similar to event_function_call() + event_function(), but hard assumes IRQs |
| 296 | * are already disabled and we're on the right CPU. |
| 297 | */ |
| 298 | static void event_function_local(struct perf_event *event, event_f func, void *data) |
| 299 | { |
| 300 | struct perf_event_context *ctx = event->ctx; |
| 301 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 302 | struct task_struct *task = READ_ONCE(ctx->task); |
| 303 | struct perf_event_context *task_ctx = NULL; |
| 304 | |
| 305 | WARN_ON_ONCE(!irqs_disabled()); |
| 306 | |
| 307 | if (task) { |
| 308 | if (task == TASK_TOMBSTONE) |
| 309 | return; |
| 310 | |
| 311 | task_ctx = ctx; |
| 312 | } |
| 313 | |
| 314 | perf_ctx_lock(cpuctx, task_ctx); |
| 315 | |
| 316 | task = ctx->task; |
| 317 | if (task == TASK_TOMBSTONE) |
| 318 | goto unlock; |
| 319 | |
| 320 | if (task) { |
| 321 | /* |
| 322 | * We must be either inactive or active and the right task, |
| 323 | * otherwise we're screwed, since we cannot IPI to somewhere |
| 324 | * else. |
| 325 | */ |
| 326 | if (ctx->is_active) { |
| 327 | if (WARN_ON_ONCE(task != current)) |
| 328 | goto unlock; |
| 329 | |
| 330 | if (WARN_ON_ONCE(cpuctx->task_ctx != ctx)) |
| 331 | goto unlock; |
| 332 | } |
| 333 | } else { |
| 334 | WARN_ON_ONCE(&cpuctx->ctx != ctx); |
| 335 | } |
| 336 | |
| 337 | func(event, cpuctx, ctx, data); |
| 338 | unlock: |
| 339 | perf_ctx_unlock(cpuctx, task_ctx); |
| 340 | } |
| 341 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 342 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
| 343 | PERF_FLAG_FD_OUTPUT |\ |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 344 | PERF_FLAG_PID_CGROUP |\ |
| 345 | PERF_FLAG_FD_CLOEXEC) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 346 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 347 | /* |
| 348 | * branch priv levels that need permission checks |
| 349 | */ |
| 350 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ |
| 351 | (PERF_SAMPLE_BRANCH_KERNEL |\ |
| 352 | PERF_SAMPLE_BRANCH_HV) |
| 353 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 354 | enum event_type_t { |
| 355 | EVENT_FLEXIBLE = 0x1, |
| 356 | EVENT_PINNED = 0x2, |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 357 | EVENT_TIME = 0x4, |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 358 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
| 359 | }; |
| 360 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 361 | /* |
| 362 | * perf_sched_events : >0 events exist |
| 363 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu |
| 364 | */ |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 365 | |
| 366 | static void perf_sched_delayed(struct work_struct *work); |
| 367 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); |
| 368 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); |
| 369 | static DEFINE_MUTEX(perf_sched_mutex); |
| 370 | static atomic_t perf_sched_count; |
| 371 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 372 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 373 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 374 | static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 375 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 376 | static atomic_t nr_mmap_events __read_mostly; |
| 377 | static atomic_t nr_comm_events __read_mostly; |
| 378 | static atomic_t nr_task_events __read_mostly; |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 379 | static atomic_t nr_freq_events __read_mostly; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 380 | static atomic_t nr_switch_events __read_mostly; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 381 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 382 | static LIST_HEAD(pmus); |
| 383 | static DEFINE_MUTEX(pmus_lock); |
| 384 | static struct srcu_struct pmus_srcu; |
| 385 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 386 | /* |
| 387 | * perf event paranoia level: |
| 388 | * -1 - not paranoid at all |
| 389 | * 0 - disallow raw tracepoint access for unpriv |
| 390 | * 1 - disallow cpu events for unpriv |
| 391 | * 2 - disallow kernel profiling for unpriv |
| 392 | */ |
Andy Lutomirski | 0161028 | 2016-05-09 15:48:51 -0700 | [diff] [blame] | 393 | int sysctl_perf_event_paranoid __read_mostly = 2; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 394 | |
Frederic Weisbecker | 2044338 | 2011-03-31 03:33:29 +0200 | [diff] [blame] | 395 | /* Minimum for 512 kiB + 1 user control page */ |
| 396 | 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] | 397 | |
| 398 | /* |
| 399 | * max perf event sample rate |
| 400 | */ |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 401 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
| 402 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) |
| 403 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 |
| 404 | |
| 405 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; |
| 406 | |
| 407 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); |
| 408 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; |
| 409 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 410 | static int perf_sample_allowed_ns __read_mostly = |
| 411 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 412 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 413 | static void update_perf_cpu_limits(void) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 414 | { |
| 415 | u64 tmp = perf_sample_period_ns; |
| 416 | |
| 417 | tmp *= sysctl_perf_cpu_time_max_percent; |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 418 | tmp = div_u64(tmp, 100); |
| 419 | if (!tmp) |
| 420 | tmp = 1; |
| 421 | |
| 422 | WRITE_ONCE(perf_sample_allowed_ns, tmp); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 423 | } |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 424 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 425 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
| 426 | |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 427 | int perf_proc_update_handler(struct ctl_table *table, int write, |
| 428 | void __user *buffer, size_t *lenp, |
| 429 | loff_t *ppos) |
| 430 | { |
Knut Petersen | 723478c | 2013-09-25 14:29:37 +0200 | [diff] [blame] | 431 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 432 | |
| 433 | if (ret || !write) |
| 434 | return ret; |
| 435 | |
Kan Liang | ab7fdef | 2016-05-03 00:26:06 -0700 | [diff] [blame] | 436 | /* |
| 437 | * If throttling is disabled don't allow the write: |
| 438 | */ |
| 439 | if (sysctl_perf_cpu_time_max_percent == 100 || |
| 440 | sysctl_perf_cpu_time_max_percent == 0) |
| 441 | return -EINVAL; |
| 442 | |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 443 | 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] | 444 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 445 | update_perf_cpu_limits(); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 446 | |
| 447 | return 0; |
| 448 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 449 | |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 450 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; |
| 451 | |
| 452 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, |
| 453 | void __user *buffer, size_t *lenp, |
| 454 | loff_t *ppos) |
| 455 | { |
Tan Xiaojun | 0f8a75e | 2017-02-23 14:04:39 +0800 | [diff] [blame] | 456 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 457 | |
| 458 | if (ret || !write) |
| 459 | return ret; |
| 460 | |
Peter Zijlstra | b303e7c | 2016-04-04 09:57:40 +0200 | [diff] [blame] | 461 | if (sysctl_perf_cpu_time_max_percent == 100 || |
| 462 | sysctl_perf_cpu_time_max_percent == 0) { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 463 | printk(KERN_WARNING |
| 464 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); |
| 465 | WRITE_ONCE(perf_sample_allowed_ns, 0); |
| 466 | } else { |
| 467 | update_perf_cpu_limits(); |
| 468 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * perf samples are done in some very critical code paths (NMIs). |
| 475 | * If they take too much CPU time, the system can lock up and not |
| 476 | * get any real work done. This will drop the sample rate when |
| 477 | * we detect that events are taking too long. |
| 478 | */ |
| 479 | #define NR_ACCUMULATED_SAMPLES 128 |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 480 | static DEFINE_PER_CPU(u64, running_sample_length); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 481 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 482 | static u64 __report_avg; |
| 483 | static u64 __report_allowed; |
| 484 | |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 485 | static void perf_duration_warn(struct irq_work *w) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 486 | { |
David Ahern | 0d87d7e | 2016-08-01 13:49:29 -0700 | [diff] [blame] | 487 | printk_ratelimited(KERN_INFO |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 488 | "perf: interrupt took too long (%lld > %lld), lowering " |
| 489 | "kernel.perf_event_max_sample_rate to %d\n", |
| 490 | __report_avg, __report_allowed, |
| 491 | sysctl_perf_event_sample_rate); |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); |
| 495 | |
| 496 | void perf_sample_event_took(u64 sample_len_ns) |
| 497 | { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 498 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
| 499 | u64 running_len; |
| 500 | u64 avg_len; |
| 501 | u32 max; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 502 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 503 | if (max_len == 0) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 504 | return; |
| 505 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 506 | /* Decay the counter by 1 average sample. */ |
| 507 | running_len = __this_cpu_read(running_sample_length); |
| 508 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; |
| 509 | running_len += sample_len_ns; |
| 510 | __this_cpu_write(running_sample_length, running_len); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 511 | |
| 512 | /* |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 513 | * Note: this will be biased artifically low until we have |
| 514 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 515 | * from having to maintain a count. |
| 516 | */ |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 517 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
| 518 | if (avg_len <= max_len) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 519 | return; |
| 520 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 521 | __report_avg = avg_len; |
| 522 | __report_allowed = max_len; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 523 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 524 | /* |
| 525 | * Compute a throttle threshold 25% below the current duration. |
| 526 | */ |
| 527 | avg_len += avg_len / 4; |
| 528 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; |
| 529 | if (avg_len < max) |
| 530 | max /= (u32)avg_len; |
| 531 | else |
| 532 | max = 1; |
| 533 | |
| 534 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
| 535 | WRITE_ONCE(max_samples_per_tick, max); |
| 536 | |
| 537 | sysctl_perf_event_sample_rate = max * HZ; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 538 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 539 | |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 540 | if (!irq_work_queue(&perf_duration_work)) { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 541 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 542 | "kernel.perf_event_max_sample_rate to %d\n", |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 543 | __report_avg, __report_allowed, |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 544 | sysctl_perf_event_sample_rate); |
| 545 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 548 | static atomic64_t perf_event_id; |
| 549 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 550 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 551 | enum event_type_t event_type); |
| 552 | |
| 553 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 554 | enum event_type_t event_type, |
| 555 | struct task_struct *task); |
| 556 | |
| 557 | static void update_context_time(struct perf_event_context *ctx); |
| 558 | static u64 perf_event_time(struct perf_event *event); |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 559 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 560 | void __weak perf_event_print_debug(void) { } |
| 561 | |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 562 | extern __weak const char *perf_pmu_name(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 563 | { |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 564 | return "pmu"; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 567 | static inline u64 perf_clock(void) |
| 568 | { |
| 569 | return local_clock(); |
| 570 | } |
| 571 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 572 | static inline u64 perf_event_clock(struct perf_event *event) |
| 573 | { |
| 574 | return event->clock(); |
| 575 | } |
| 576 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 577 | #ifdef CONFIG_CGROUP_PERF |
| 578 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 579 | static inline bool |
| 580 | perf_cgroup_match(struct perf_event *event) |
| 581 | { |
| 582 | struct perf_event_context *ctx = event->ctx; |
| 583 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 584 | |
Tejun Heo | ef824fa | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 585 | /* @event doesn't care about cgroup */ |
| 586 | if (!event->cgrp) |
| 587 | return true; |
| 588 | |
| 589 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ |
| 590 | if (!cpuctx->cgrp) |
| 591 | return false; |
| 592 | |
| 593 | /* |
| 594 | * Cgroup scoping is recursive. An event enabled for a cgroup is |
| 595 | * also enabled for all its descendant cgroups. If @cpuctx's |
| 596 | * cgroup is a descendant of @event's (the test covers identity |
| 597 | * case), it's a match. |
| 598 | */ |
| 599 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, |
| 600 | event->cgrp->css.cgroup); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 603 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 604 | { |
Zefan Li | 4e2ba65 | 2014-09-19 16:53:14 +0800 | [diff] [blame] | 605 | css_put(&event->cgrp->css); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 606 | event->cgrp = NULL; |
| 607 | } |
| 608 | |
| 609 | static inline int is_cgroup_event(struct perf_event *event) |
| 610 | { |
| 611 | return event->cgrp != NULL; |
| 612 | } |
| 613 | |
| 614 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 615 | { |
| 616 | struct perf_cgroup_info *t; |
| 617 | |
| 618 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 619 | return t->time; |
| 620 | } |
| 621 | |
| 622 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) |
| 623 | { |
| 624 | struct perf_cgroup_info *info; |
| 625 | u64 now; |
| 626 | |
| 627 | now = perf_clock(); |
| 628 | |
| 629 | info = this_cpu_ptr(cgrp->info); |
| 630 | |
| 631 | info->time += now - info->timestamp; |
| 632 | info->timestamp = now; |
| 633 | } |
| 634 | |
| 635 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 636 | { |
Song Liu | d4271d8 | 2018-03-12 09:59:43 -0700 | [diff] [blame] | 637 | struct perf_cgroup *cgrp = cpuctx->cgrp; |
| 638 | struct cgroup_subsys_state *css; |
| 639 | |
| 640 | if (cgrp) { |
| 641 | for (css = &cgrp->css; css; css = css->parent) { |
| 642 | cgrp = container_of(css, struct perf_cgroup, css); |
| 643 | __update_cgrp_time(cgrp); |
| 644 | } |
| 645 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 649 | { |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 650 | struct perf_cgroup *cgrp; |
| 651 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 652 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 653 | * ensure we access cgroup data only when needed and |
| 654 | * when we know the cgroup is pinned (css_get) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 655 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 656 | if (!is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 657 | return; |
| 658 | |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 659 | cgrp = perf_cgroup_from_task(current, event->ctx); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 660 | /* |
| 661 | * Do not update time when cgroup is not active |
| 662 | */ |
| 663 | if (cgrp == event->cgrp) |
| 664 | __update_cgrp_time(event->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 668 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 669 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 670 | { |
| 671 | struct perf_cgroup *cgrp; |
| 672 | struct perf_cgroup_info *info; |
Song Liu | d4271d8 | 2018-03-12 09:59:43 -0700 | [diff] [blame] | 673 | struct cgroup_subsys_state *css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 674 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 675 | /* |
| 676 | * ctx->lock held by caller |
| 677 | * ensure we do not access cgroup data |
| 678 | * unless we have the cgroup pinned (css_get) |
| 679 | */ |
| 680 | if (!task || !ctx->nr_cgroups) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 681 | return; |
| 682 | |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 683 | cgrp = perf_cgroup_from_task(task, ctx); |
Song Liu | d4271d8 | 2018-03-12 09:59:43 -0700 | [diff] [blame] | 684 | |
| 685 | for (css = &cgrp->css; css; css = css->parent) { |
| 686 | cgrp = container_of(css, struct perf_cgroup, css); |
| 687 | info = this_cpu_ptr(cgrp->info); |
| 688 | info->timestamp = ctx->timestamp; |
| 689 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
| 693 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ |
| 694 | |
| 695 | /* |
| 696 | * reschedule events based on the cgroup constraint of task. |
| 697 | * |
| 698 | * mode SWOUT : schedule out everything |
| 699 | * mode SWIN : schedule in based on cgroup for next |
| 700 | */ |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 701 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 702 | { |
| 703 | struct perf_cpu_context *cpuctx; |
| 704 | struct pmu *pmu; |
| 705 | unsigned long flags; |
| 706 | |
| 707 | /* |
| 708 | * disable interrupts to avoid geting nr_cgroup |
| 709 | * changes via __perf_event_disable(). Also |
| 710 | * avoids preemption. |
| 711 | */ |
| 712 | local_irq_save(flags); |
| 713 | |
| 714 | /* |
| 715 | * we reschedule only in the presence of cgroup |
| 716 | * constrained events. |
| 717 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 718 | |
| 719 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 720 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 721 | if (cpuctx->unique_pmu != pmu) |
| 722 | continue; /* ensure we process each cpuctx once */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 723 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 724 | /* |
| 725 | * perf_cgroup_events says at least one |
| 726 | * context on this CPU has cgroup events. |
| 727 | * |
| 728 | * ctx->nr_cgroups reports the number of cgroup |
| 729 | * events for a context. |
| 730 | */ |
| 731 | if (cpuctx->ctx.nr_cgroups > 0) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 732 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 733 | perf_pmu_disable(cpuctx->ctx.pmu); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 734 | |
| 735 | if (mode & PERF_CGROUP_SWOUT) { |
| 736 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 737 | /* |
| 738 | * must not be done before ctxswout due |
| 739 | * to event_filter_match() in event_sched_out() |
| 740 | */ |
| 741 | cpuctx->cgrp = NULL; |
| 742 | } |
| 743 | |
| 744 | if (mode & PERF_CGROUP_SWIN) { |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 745 | WARN_ON_ONCE(cpuctx->cgrp); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 746 | /* |
| 747 | * set cgrp before ctxsw in to allow |
| 748 | * event_filter_match() to not have to pass |
| 749 | * task around |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 750 | * we pass the cpuctx->ctx to perf_cgroup_from_task() |
| 751 | * because cgorup events are only per-cpu |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 752 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 753 | cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 754 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
| 755 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 756 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 757 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 758 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 761 | local_irq_restore(flags); |
| 762 | } |
| 763 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 764 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 765 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 766 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 767 | struct perf_cgroup *cgrp1; |
| 768 | struct perf_cgroup *cgrp2 = NULL; |
| 769 | |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 770 | rcu_read_lock(); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 771 | /* |
| 772 | * we come here when we know perf_cgroup_events > 0 |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 773 | * we do not need to pass the ctx here because we know |
| 774 | * we are holding the rcu lock |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 775 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 776 | cgrp1 = perf_cgroup_from_task(task, NULL); |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 777 | cgrp2 = perf_cgroup_from_task(next, NULL); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 778 | |
| 779 | /* |
| 780 | * only schedule out current cgroup events if we know |
| 781 | * that we are switching to a different cgroup. Otherwise, |
| 782 | * do no touch the cgroup events. |
| 783 | */ |
| 784 | if (cgrp1 != cgrp2) |
| 785 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 786 | |
| 787 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 788 | } |
| 789 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 790 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 791 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 792 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 793 | struct perf_cgroup *cgrp1; |
| 794 | struct perf_cgroup *cgrp2 = NULL; |
| 795 | |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 796 | rcu_read_lock(); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 797 | /* |
| 798 | * we come here when we know perf_cgroup_events > 0 |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 799 | * we do not need to pass the ctx here because we know |
| 800 | * we are holding the rcu lock |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 801 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 802 | cgrp1 = perf_cgroup_from_task(task, NULL); |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 803 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 804 | |
| 805 | /* |
| 806 | * only need to schedule in cgroup events if we are changing |
| 807 | * cgroup during ctxsw. Cgroup events were not scheduled |
| 808 | * out of ctxsw out if that was not the case. |
| 809 | */ |
| 810 | if (cgrp1 != cgrp2) |
| 811 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 812 | |
| 813 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
| 817 | struct perf_event_attr *attr, |
| 818 | struct perf_event *group_leader) |
| 819 | { |
| 820 | struct perf_cgroup *cgrp; |
| 821 | struct cgroup_subsys_state *css; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 822 | struct fd f = fdget(fd); |
| 823 | int ret = 0; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 824 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 825 | if (!f.file) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 826 | return -EBADF; |
| 827 | |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 828 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 829 | &perf_event_cgrp_subsys); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 830 | if (IS_ERR(css)) { |
| 831 | ret = PTR_ERR(css); |
| 832 | goto out; |
| 833 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 834 | |
| 835 | cgrp = container_of(css, struct perf_cgroup, css); |
| 836 | event->cgrp = cgrp; |
| 837 | |
| 838 | /* |
| 839 | * all events in a group must monitor |
| 840 | * the same cgroup because a task belongs |
| 841 | * to only one perf cgroup at a time |
| 842 | */ |
| 843 | if (group_leader && group_leader->cgrp != cgrp) { |
| 844 | perf_detach_cgroup(event); |
| 845 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 846 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 847 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 848 | fdput(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | static inline void |
| 853 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 854 | { |
| 855 | struct perf_cgroup_info *t; |
| 856 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 857 | event->shadow_ctx_time = now - t->timestamp; |
| 858 | } |
| 859 | |
| 860 | static inline void |
| 861 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 862 | { |
| 863 | /* |
| 864 | * when the current task's perf cgroup does not match |
| 865 | * the event's, we need to remember to call the |
| 866 | * perf_mark_enable() function the first time a task with |
| 867 | * a matching perf cgroup is scheduled in. |
| 868 | */ |
| 869 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 870 | event->cgrp_defer_enabled = 1; |
| 871 | } |
| 872 | |
| 873 | static inline void |
| 874 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 875 | struct perf_event_context *ctx) |
| 876 | { |
| 877 | struct perf_event *sub; |
| 878 | u64 tstamp = perf_event_time(event); |
| 879 | |
| 880 | if (!event->cgrp_defer_enabled) |
| 881 | return; |
| 882 | |
| 883 | event->cgrp_defer_enabled = 0; |
| 884 | |
| 885 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 886 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 887 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 888 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 889 | sub->cgrp_defer_enabled = 0; |
| 890 | } |
| 891 | } |
| 892 | } |
David Carrillo-Cisneros | db4a835 | 2016-08-02 00:48:12 -0700 | [diff] [blame] | 893 | |
| 894 | /* |
| 895 | * Update cpuctx->cgrp so that it is set when first cgroup event is added and |
| 896 | * cleared when last cgroup event is removed. |
| 897 | */ |
| 898 | static inline void |
| 899 | list_update_cgroup_event(struct perf_event *event, |
| 900 | struct perf_event_context *ctx, bool add) |
| 901 | { |
| 902 | struct perf_cpu_context *cpuctx; |
| 903 | |
| 904 | if (!is_cgroup_event(event)) |
| 905 | return; |
| 906 | |
| 907 | if (add && ctx->nr_cgroups++) |
| 908 | return; |
| 909 | else if (!add && --ctx->nr_cgroups) |
| 910 | return; |
| 911 | /* |
| 912 | * Because cgroup events are always per-cpu events, |
| 913 | * this will always be called from the right CPU. |
| 914 | */ |
| 915 | cpuctx = __get_cpu_context(ctx); |
David Carrillo-Cisneros | 864c235 | 2016-11-01 11:52:58 -0700 | [diff] [blame] | 916 | |
David Carrillo-Cisneros | 8fc31ce | 2016-12-04 00:46:17 -0800 | [diff] [blame] | 917 | /* |
| 918 | * cpuctx->cgrp is NULL until a cgroup event is sched in or |
| 919 | * ctx->nr_cgroup == 0 . |
| 920 | */ |
| 921 | if (add && perf_cgroup_from_task(current, ctx) == event->cgrp) |
| 922 | cpuctx->cgrp = event->cgrp; |
| 923 | else if (!add) |
| 924 | cpuctx->cgrp = NULL; |
David Carrillo-Cisneros | db4a835 | 2016-08-02 00:48:12 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 927 | #else /* !CONFIG_CGROUP_PERF */ |
| 928 | |
| 929 | static inline bool |
| 930 | perf_cgroup_match(struct perf_event *event) |
| 931 | { |
| 932 | return true; |
| 933 | } |
| 934 | |
| 935 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 936 | {} |
| 937 | |
| 938 | static inline int is_cgroup_event(struct perf_event *event) |
| 939 | { |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 944 | { |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 949 | { |
| 950 | } |
| 951 | |
| 952 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 953 | { |
| 954 | } |
| 955 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 956 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 957 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 958 | { |
| 959 | } |
| 960 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 961 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 962 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 963 | { |
| 964 | } |
| 965 | |
| 966 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 967 | struct perf_event_attr *attr, |
| 968 | struct perf_event *group_leader) |
| 969 | { |
| 970 | return -EINVAL; |
| 971 | } |
| 972 | |
| 973 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 974 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 975 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 976 | { |
| 977 | } |
| 978 | |
| 979 | void |
| 980 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 981 | { |
| 982 | } |
| 983 | |
| 984 | static inline void |
| 985 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 986 | { |
| 987 | } |
| 988 | |
| 989 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 990 | { |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | static inline void |
| 995 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 996 | { |
| 997 | } |
| 998 | |
| 999 | static inline void |
| 1000 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 1001 | struct perf_event_context *ctx) |
| 1002 | { |
| 1003 | } |
David Carrillo-Cisneros | db4a835 | 2016-08-02 00:48:12 -0700 | [diff] [blame] | 1004 | |
| 1005 | static inline void |
| 1006 | list_update_cgroup_event(struct perf_event *event, |
| 1007 | struct perf_event_context *ctx, bool add) |
| 1008 | { |
| 1009 | } |
| 1010 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1011 | #endif |
| 1012 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1013 | /* |
| 1014 | * set default to be dependent on timer tick just |
| 1015 | * like original code |
| 1016 | */ |
| 1017 | #define PERF_CPU_HRTIMER (1000 / HZ) |
| 1018 | /* |
| 1019 | * function must be called with interrupts disbled |
| 1020 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1021 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1022 | { |
| 1023 | struct perf_cpu_context *cpuctx; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1024 | int rotations = 0; |
| 1025 | |
| 1026 | WARN_ON(!irqs_disabled()); |
| 1027 | |
| 1028 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1029 | rotations = perf_rotate_context(cpuctx); |
| 1030 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 1031 | raw_spin_lock(&cpuctx->hrtimer_lock); |
| 1032 | if (rotations) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1033 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 1034 | else |
| 1035 | cpuctx->hrtimer_active = 0; |
| 1036 | raw_spin_unlock(&cpuctx->hrtimer_lock); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1037 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 1038 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1039 | } |
| 1040 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1041 | 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] | 1042 | { |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1043 | struct hrtimer *timer = &cpuctx->hrtimer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1044 | struct pmu *pmu = cpuctx->ctx.pmu; |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1045 | u64 interval; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1046 | |
| 1047 | /* no multiplexing needed for SW PMU */ |
| 1048 | if (pmu->task_ctx_nr == perf_sw_context) |
| 1049 | return; |
| 1050 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 1051 | /* |
| 1052 | * check default is sane, if not set then force to |
| 1053 | * default interval (1/tick) |
| 1054 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1055 | interval = pmu->hrtimer_interval_ms; |
| 1056 | if (interval < 1) |
| 1057 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 1058 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1059 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1060 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 1061 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
| 1062 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1063 | timer->function = perf_mux_hrtimer_handler; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1066 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1067 | { |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1068 | struct hrtimer *timer = &cpuctx->hrtimer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1069 | struct pmu *pmu = cpuctx->ctx.pmu; |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 1070 | unsigned long flags; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1071 | |
| 1072 | /* not for SW PMU */ |
| 1073 | if (pmu->task_ctx_nr == perf_sw_context) |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1074 | return 0; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1075 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 1076 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
| 1077 | if (!cpuctx->hrtimer_active) { |
| 1078 | cpuctx->hrtimer_active = 1; |
| 1079 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); |
| 1080 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); |
| 1081 | } |
| 1082 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1083 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1084 | return 0; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1087 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1088 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1089 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 1090 | if (!(*count)++) |
| 1091 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1094 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1095 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1096 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 1097 | if (!--(*count)) |
| 1098 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1101 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1102 | |
| 1103 | /* |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1104 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
| 1105 | * perf_event_task_tick() are fully serialized because they're strictly cpu |
| 1106 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs |
| 1107 | * disabled, while perf_event_task_tick is called from IRQ context. |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1108 | */ |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1109 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1110 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1111 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1112 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1113 | WARN_ON(!irqs_disabled()); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1114 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1115 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
| 1116 | |
| 1117 | list_add(&ctx->active_ctx_list, head); |
| 1118 | } |
| 1119 | |
| 1120 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) |
| 1121 | { |
| 1122 | WARN_ON(!irqs_disabled()); |
| 1123 | |
| 1124 | WARN_ON(list_empty(&ctx->active_ctx_list)); |
| 1125 | |
| 1126 | list_del_init(&ctx->active_ctx_list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | static void get_ctx(struct perf_event_context *ctx) |
| 1130 | { |
| 1131 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 1132 | } |
| 1133 | |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 1134 | static void free_ctx(struct rcu_head *head) |
| 1135 | { |
| 1136 | struct perf_event_context *ctx; |
| 1137 | |
| 1138 | ctx = container_of(head, struct perf_event_context, rcu_head); |
| 1139 | kfree(ctx->task_ctx_data); |
| 1140 | kfree(ctx); |
| 1141 | } |
| 1142 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1143 | static void put_ctx(struct perf_event_context *ctx) |
| 1144 | { |
| 1145 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 1146 | if (ctx->parent_ctx) |
| 1147 | put_ctx(ctx->parent_ctx); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1148 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1149 | put_task_struct(ctx->task); |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 1150 | call_rcu(&ctx->rcu_head, free_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1154 | /* |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1155 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and |
| 1156 | * perf_pmu_migrate_context() we need some magic. |
| 1157 | * |
| 1158 | * Those places that change perf_event::ctx will hold both |
| 1159 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. |
| 1160 | * |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 1161 | * Lock ordering is by mutex address. There are two other sites where |
| 1162 | * perf_event_context::mutex nests and those are: |
| 1163 | * |
| 1164 | * - perf_event_exit_task_context() [ child , 0 ] |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 1165 | * perf_event_exit_event() |
| 1166 | * put_event() [ parent, 1 ] |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 1167 | * |
| 1168 | * - perf_event_init_context() [ parent, 0 ] |
| 1169 | * inherit_task_group() |
| 1170 | * inherit_group() |
| 1171 | * inherit_event() |
| 1172 | * perf_event_alloc() |
| 1173 | * perf_init_event() |
| 1174 | * perf_try_init_event() [ child , 1 ] |
| 1175 | * |
| 1176 | * While it appears there is an obvious deadlock here -- the parent and child |
| 1177 | * nesting levels are inverted between the two. This is in fact safe because |
| 1178 | * life-time rules separate them. That is an exiting task cannot fork, and a |
| 1179 | * spawning task cannot (yet) exit. |
| 1180 | * |
| 1181 | * But remember that that these are parent<->child context relations, and |
| 1182 | * migration does not affect children, therefore these two orderings should not |
| 1183 | * interact. |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1184 | * |
| 1185 | * The change in perf_event::ctx does not affect children (as claimed above) |
| 1186 | * because the sys_perf_event_open() case will install a new event and break |
| 1187 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only |
| 1188 | * concerned with cpuctx and that doesn't have children. |
| 1189 | * |
| 1190 | * The places that change perf_event::ctx will issue: |
| 1191 | * |
| 1192 | * perf_remove_from_context(); |
| 1193 | * synchronize_rcu(); |
| 1194 | * perf_install_in_context(); |
| 1195 | * |
| 1196 | * to affect the change. The remove_from_context() + synchronize_rcu() should |
| 1197 | * quiesce the event, after which we can install it in the new location. This |
| 1198 | * means that only external vectors (perf_fops, prctl) can perturb the event |
| 1199 | * while in transit. Therefore all such accessors should also acquire |
| 1200 | * perf_event_context::mutex to serialize against this. |
| 1201 | * |
| 1202 | * However; because event->ctx can change while we're waiting to acquire |
| 1203 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() |
| 1204 | * function. |
| 1205 | * |
| 1206 | * Lock order: |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 1207 | * cred_guard_mutex |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1208 | * task_struct::perf_event_mutex |
| 1209 | * perf_event_context::mutex |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1210 | * perf_event::child_mutex; |
Peter Zijlstra | 07c4a77 | 2016-01-26 12:15:37 +0100 | [diff] [blame] | 1211 | * perf_event_context::lock |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1212 | * perf_event::mmap_mutex |
| 1213 | * mmap_sem |
| 1214 | */ |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1215 | static struct perf_event_context * |
| 1216 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1217 | { |
| 1218 | struct perf_event_context *ctx; |
| 1219 | |
| 1220 | again: |
| 1221 | rcu_read_lock(); |
| 1222 | ctx = ACCESS_ONCE(event->ctx); |
| 1223 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
| 1224 | rcu_read_unlock(); |
| 1225 | goto again; |
| 1226 | } |
| 1227 | rcu_read_unlock(); |
| 1228 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1229 | mutex_lock_nested(&ctx->mutex, nesting); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1230 | if (event->ctx != ctx) { |
| 1231 | mutex_unlock(&ctx->mutex); |
| 1232 | put_ctx(ctx); |
| 1233 | goto again; |
| 1234 | } |
| 1235 | |
| 1236 | return ctx; |
| 1237 | } |
| 1238 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1239 | static inline struct perf_event_context * |
| 1240 | perf_event_ctx_lock(struct perf_event *event) |
| 1241 | { |
| 1242 | return perf_event_ctx_lock_nested(event, 0); |
| 1243 | } |
| 1244 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1245 | static void perf_event_ctx_unlock(struct perf_event *event, |
| 1246 | struct perf_event_context *ctx) |
| 1247 | { |
| 1248 | mutex_unlock(&ctx->mutex); |
| 1249 | put_ctx(ctx); |
| 1250 | } |
| 1251 | |
| 1252 | /* |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1253 | * This must be done under the ctx->lock, such as to serialize against |
| 1254 | * context_equiv(), therefore we cannot call put_ctx() since that might end up |
| 1255 | * calling scheduler related locks and ctx->lock nests inside those. |
| 1256 | */ |
| 1257 | static __must_check struct perf_event_context * |
| 1258 | unclone_ctx(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1259 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1260 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
| 1261 | |
| 1262 | lockdep_assert_held(&ctx->lock); |
| 1263 | |
| 1264 | if (parent_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1265 | ctx->parent_ctx = NULL; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1266 | ctx->generation++; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1267 | |
| 1268 | return parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1269 | } |
| 1270 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1271 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 1272 | { |
| 1273 | /* |
| 1274 | * only top level events have the pid namespace they were created in |
| 1275 | */ |
| 1276 | if (event->parent) |
| 1277 | event = event->parent; |
| 1278 | |
| 1279 | return task_tgid_nr_ns(p, event->ns); |
| 1280 | } |
| 1281 | |
| 1282 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 1283 | { |
| 1284 | /* |
| 1285 | * only top level events have the pid namespace they were created in |
| 1286 | */ |
| 1287 | if (event->parent) |
| 1288 | event = event->parent; |
| 1289 | |
| 1290 | return task_pid_nr_ns(p, event->ns); |
| 1291 | } |
| 1292 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1293 | /* |
| 1294 | * If we inherit events we want to return the parent event id |
| 1295 | * to userspace. |
| 1296 | */ |
| 1297 | static u64 primary_event_id(struct perf_event *event) |
| 1298 | { |
| 1299 | u64 id = event->id; |
| 1300 | |
| 1301 | if (event->parent) |
| 1302 | id = event->parent->id; |
| 1303 | |
| 1304 | return id; |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Get the perf_event_context for a task and lock it. |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1309 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1310 | * This has to cope with with the fact that until it is locked, |
| 1311 | * the context could get moved to another task. |
| 1312 | */ |
| 1313 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1314 | 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] | 1315 | { |
| 1316 | struct perf_event_context *ctx; |
| 1317 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1318 | retry: |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1319 | /* |
| 1320 | * One of the few rules of preemptible RCU is that one cannot do |
| 1321 | * rcu_read_unlock() while holding a scheduler (or nested) lock when |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1322 | * part of the read side critical section was irqs-enabled -- see |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1323 | * rcu_read_unlock_special(). |
| 1324 | * |
| 1325 | * 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] | 1326 | * side critical section has interrupts disabled. |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1327 | */ |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1328 | local_irq_save(*flags); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1329 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1330 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1331 | if (ctx) { |
| 1332 | /* |
| 1333 | * If this context is a clone of another, it might |
| 1334 | * get swapped for another underneath us by |
| 1335 | * perf_event_task_sched_out, though the |
| 1336 | * rcu_read_lock() protects us from any context |
| 1337 | * getting freed. Lock the context and check if it |
| 1338 | * got swapped before we could get the lock, and retry |
| 1339 | * if so. If we locked the right context, then it |
| 1340 | * can't get swapped on us any more. |
| 1341 | */ |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1342 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1343 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1344 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1345 | rcu_read_unlock(); |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1346 | local_irq_restore(*flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1347 | goto retry; |
| 1348 | } |
| 1349 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1350 | if (ctx->task == TASK_TOMBSTONE || |
| 1351 | !atomic_inc_not_zero(&ctx->refcount)) { |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1352 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1353 | ctx = NULL; |
Peter Zijlstra | 828b6f0 | 2016-01-27 21:59:04 +0100 | [diff] [blame] | 1354 | } else { |
| 1355 | WARN_ON_ONCE(ctx->task != task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | rcu_read_unlock(); |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1359 | if (!ctx) |
| 1360 | local_irq_restore(*flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1361 | return ctx; |
| 1362 | } |
| 1363 | |
| 1364 | /* |
| 1365 | * Get the context for a task and increment its pin_count so it |
| 1366 | * can't get swapped to another task. This also increments its |
| 1367 | * reference count so that the context can't get freed. |
| 1368 | */ |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1369 | static struct perf_event_context * |
| 1370 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1371 | { |
| 1372 | struct perf_event_context *ctx; |
| 1373 | unsigned long flags; |
| 1374 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1375 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1376 | if (ctx) { |
| 1377 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1378 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1379 | } |
| 1380 | return ctx; |
| 1381 | } |
| 1382 | |
| 1383 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 1384 | { |
| 1385 | unsigned long flags; |
| 1386 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1387 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1388 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1389 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1392 | /* |
| 1393 | * Update the record of the current time in a context. |
| 1394 | */ |
| 1395 | static void update_context_time(struct perf_event_context *ctx) |
| 1396 | { |
| 1397 | u64 now = perf_clock(); |
| 1398 | |
| 1399 | ctx->time += now - ctx->timestamp; |
| 1400 | ctx->timestamp = now; |
| 1401 | } |
| 1402 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1403 | static u64 perf_event_time(struct perf_event *event) |
| 1404 | { |
| 1405 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1406 | |
| 1407 | if (is_cgroup_event(event)) |
| 1408 | return perf_cgroup_event_time(event); |
| 1409 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1410 | return ctx ? ctx->time : 0; |
| 1411 | } |
| 1412 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1413 | /* |
| 1414 | * Update the total_time_enabled and total_time_running fields for a event. |
| 1415 | */ |
| 1416 | static void update_event_times(struct perf_event *event) |
| 1417 | { |
| 1418 | struct perf_event_context *ctx = event->ctx; |
| 1419 | u64 run_end; |
| 1420 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 1421 | lockdep_assert_held(&ctx->lock); |
| 1422 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1423 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1424 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1425 | return; |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 1426 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1427 | /* |
| 1428 | * in cgroup mode, time_enabled represents |
| 1429 | * the time the event was enabled AND active |
| 1430 | * tasks were in the monitored cgroup. This is |
| 1431 | * independent of the activity of the context as |
| 1432 | * there may be a mix of cgroup and non-cgroup events. |
| 1433 | * |
| 1434 | * That is why we treat cgroup events differently |
| 1435 | * here. |
| 1436 | */ |
| 1437 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1438 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1439 | else if (ctx->is_active) |
| 1440 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1441 | else |
| 1442 | run_end = event->tstamp_stopped; |
| 1443 | |
| 1444 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1445 | |
| 1446 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1447 | run_end = event->tstamp_stopped; |
| 1448 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1449 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1450 | |
| 1451 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1452 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1453 | } |
| 1454 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1455 | /* |
| 1456 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1457 | */ |
| 1458 | static void update_group_times(struct perf_event *leader) |
| 1459 | { |
| 1460 | struct perf_event *event; |
| 1461 | |
| 1462 | update_event_times(leader); |
| 1463 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1464 | update_event_times(event); |
| 1465 | } |
| 1466 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1467 | static struct list_head * |
| 1468 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1469 | { |
| 1470 | if (event->attr.pinned) |
| 1471 | return &ctx->pinned_groups; |
| 1472 | else |
| 1473 | return &ctx->flexible_groups; |
| 1474 | } |
| 1475 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1476 | /* |
| 1477 | * Add a event from the lists for its context. |
| 1478 | * Must be called with ctx->mutex and ctx->lock held. |
| 1479 | */ |
| 1480 | static void |
| 1481 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1482 | { |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 1483 | lockdep_assert_held(&ctx->lock); |
| 1484 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1485 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1486 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1487 | |
| 1488 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1489 | * If we're a stand alone event or group leader, we go to the context |
| 1490 | * list, group events are kept attached to the group so that |
| 1491 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1492 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1493 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1494 | struct list_head *list; |
| 1495 | |
David Carrillo-Cisneros | 4ff6a8d | 2016-08-17 13:55:05 -0700 | [diff] [blame] | 1496 | event->group_caps = event->event_caps; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1497 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1498 | list = ctx_group_list(event, ctx); |
| 1499 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1500 | } |
| 1501 | |
David Carrillo-Cisneros | db4a835 | 2016-08-02 00:48:12 -0700 | [diff] [blame] | 1502 | list_update_cgroup_event(event, ctx, true); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1503 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1504 | list_add_rcu(&event->event_entry, &ctx->event_list); |
| 1505 | ctx->nr_events++; |
| 1506 | if (event->attr.inherit_stat) |
| 1507 | ctx->nr_stat++; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1508 | |
| 1509 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1510 | } |
| 1511 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1512 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1513 | * Initialize event state based on the perf_event_attr::disabled. |
| 1514 | */ |
| 1515 | static inline void perf_event__state_init(struct perf_event *event) |
| 1516 | { |
| 1517 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1518 | PERF_EVENT_STATE_INACTIVE; |
| 1519 | } |
| 1520 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1521 | 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] | 1522 | { |
| 1523 | int entry = sizeof(u64); /* value */ |
| 1524 | int size = 0; |
| 1525 | int nr = 1; |
| 1526 | |
| 1527 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1528 | size += sizeof(u64); |
| 1529 | |
| 1530 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1531 | size += sizeof(u64); |
| 1532 | |
| 1533 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1534 | entry += sizeof(u64); |
| 1535 | |
| 1536 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1537 | nr += nr_siblings; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1538 | size += sizeof(u64); |
| 1539 | } |
| 1540 | |
| 1541 | size += entry * nr; |
| 1542 | event->read_size = size; |
| 1543 | } |
| 1544 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1545 | 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] | 1546 | { |
| 1547 | struct perf_sample_data *data; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1548 | u16 size = 0; |
| 1549 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1550 | if (sample_type & PERF_SAMPLE_IP) |
| 1551 | size += sizeof(data->ip); |
| 1552 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1553 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1554 | size += sizeof(data->addr); |
| 1555 | |
| 1556 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1557 | size += sizeof(data->period); |
| 1558 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1559 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1560 | size += sizeof(data->weight); |
| 1561 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1562 | if (sample_type & PERF_SAMPLE_READ) |
| 1563 | size += event->read_size; |
| 1564 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1565 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1566 | size += sizeof(data->data_src.val); |
| 1567 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 1568 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1569 | size += sizeof(data->txn); |
| 1570 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1571 | event->header_size = size; |
| 1572 | } |
| 1573 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1574 | /* |
| 1575 | * Called at perf_event creation and when events are attached/detached from a |
| 1576 | * group. |
| 1577 | */ |
| 1578 | static void perf_event__header_size(struct perf_event *event) |
| 1579 | { |
| 1580 | __perf_event_read_size(event, |
| 1581 | event->group_leader->nr_siblings); |
| 1582 | __perf_event_header_size(event, event->attr.sample_type); |
| 1583 | } |
| 1584 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1585 | static void perf_event__id_header_size(struct perf_event *event) |
| 1586 | { |
| 1587 | struct perf_sample_data *data; |
| 1588 | u64 sample_type = event->attr.sample_type; |
| 1589 | u16 size = 0; |
| 1590 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1591 | if (sample_type & PERF_SAMPLE_TID) |
| 1592 | size += sizeof(data->tid_entry); |
| 1593 | |
| 1594 | if (sample_type & PERF_SAMPLE_TIME) |
| 1595 | size += sizeof(data->time); |
| 1596 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 1597 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1598 | size += sizeof(data->id); |
| 1599 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1600 | if (sample_type & PERF_SAMPLE_ID) |
| 1601 | size += sizeof(data->id); |
| 1602 | |
| 1603 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1604 | size += sizeof(data->stream_id); |
| 1605 | |
| 1606 | if (sample_type & PERF_SAMPLE_CPU) |
| 1607 | size += sizeof(data->cpu_entry); |
| 1608 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1609 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1610 | } |
| 1611 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1612 | static bool perf_event_validate_size(struct perf_event *event) |
| 1613 | { |
| 1614 | /* |
| 1615 | * The values computed here will be over-written when we actually |
| 1616 | * attach the event. |
| 1617 | */ |
| 1618 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); |
| 1619 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); |
| 1620 | perf_event__id_header_size(event); |
| 1621 | |
| 1622 | /* |
| 1623 | * Sum the lot; should not exceed the 64k limit we have on records. |
| 1624 | * Conservative limit to allow for callchains and other variable fields. |
| 1625 | */ |
| 1626 | if (event->read_size + event->header_size + |
| 1627 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) |
| 1628 | return false; |
| 1629 | |
| 1630 | return true; |
| 1631 | } |
| 1632 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1633 | static void perf_group_attach(struct perf_event *event) |
| 1634 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1635 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1636 | |
Peter Zijlstra | 3996a91 | 2017-01-26 16:39:55 +0100 | [diff] [blame] | 1637 | lockdep_assert_held(&event->ctx->lock); |
| 1638 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1639 | /* |
| 1640 | * We can have double attach due to group movement in perf_event_open. |
| 1641 | */ |
| 1642 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1643 | return; |
| 1644 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1645 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1646 | |
| 1647 | if (group_leader == event) |
| 1648 | return; |
| 1649 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1650 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
| 1651 | |
David Carrillo-Cisneros | 4ff6a8d | 2016-08-17 13:55:05 -0700 | [diff] [blame] | 1652 | group_leader->group_caps &= event->event_caps; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1653 | |
| 1654 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1655 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1656 | |
| 1657 | perf_event__header_size(group_leader); |
| 1658 | |
| 1659 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1660 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1661 | } |
| 1662 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1663 | /* |
| 1664 | * Remove a event from the lists for its context. |
| 1665 | * Must be called with ctx->mutex and ctx->lock held. |
| 1666 | */ |
| 1667 | static void |
| 1668 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1669 | { |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1670 | WARN_ON_ONCE(event->ctx != ctx); |
| 1671 | lockdep_assert_held(&ctx->lock); |
| 1672 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1673 | /* |
| 1674 | * We can have double detach due to exit/hot-unplug + close. |
| 1675 | */ |
| 1676 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1677 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1678 | |
| 1679 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1680 | |
David Carrillo-Cisneros | db4a835 | 2016-08-02 00:48:12 -0700 | [diff] [blame] | 1681 | list_update_cgroup_event(event, ctx, false); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1682 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1683 | ctx->nr_events--; |
| 1684 | if (event->attr.inherit_stat) |
| 1685 | ctx->nr_stat--; |
| 1686 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1687 | list_del_rcu(&event->event_entry); |
| 1688 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1689 | if (event->group_leader == event) |
| 1690 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1691 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1692 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1693 | |
| 1694 | /* |
| 1695 | * If event was in error state, then keep it |
| 1696 | * that way, otherwise bogus counts will be |
| 1697 | * returned on read(). The only way to get out |
| 1698 | * of error state is by explicit re-enabling |
| 1699 | * of the event |
| 1700 | */ |
| 1701 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1702 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1703 | |
| 1704 | ctx->generation++; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1705 | } |
| 1706 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1707 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1708 | { |
| 1709 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1710 | struct list_head *list = NULL; |
| 1711 | |
Peter Zijlstra | 3996a91 | 2017-01-26 16:39:55 +0100 | [diff] [blame] | 1712 | lockdep_assert_held(&event->ctx->lock); |
| 1713 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1714 | /* |
| 1715 | * We can have double detach due to exit/hot-unplug + close. |
| 1716 | */ |
| 1717 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1718 | return; |
| 1719 | |
| 1720 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1721 | |
| 1722 | /* |
| 1723 | * If this is a sibling, remove it from its group. |
| 1724 | */ |
| 1725 | if (event->group_leader != event) { |
| 1726 | list_del_init(&event->group_entry); |
| 1727 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1728 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | if (!list_empty(&event->group_entry)) |
| 1732 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1733 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1734 | /* |
| 1735 | * If this was a group event with sibling events then |
| 1736 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1737 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1738 | */ |
| 1739 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1740 | if (list) |
| 1741 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1742 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1743 | |
| 1744 | /* Inherit group flags from the previous leader */ |
David Carrillo-Cisneros | 4ff6a8d | 2016-08-17 13:55:05 -0700 | [diff] [blame] | 1745 | sibling->group_caps = event->group_caps; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1746 | |
| 1747 | WARN_ON_ONCE(sibling->ctx != event->ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1748 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1749 | |
| 1750 | out: |
| 1751 | perf_event__header_size(event->group_leader); |
| 1752 | |
| 1753 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1754 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1755 | } |
| 1756 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1757 | static bool is_orphaned_event(struct perf_event *event) |
| 1758 | { |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 1759 | return event->state == PERF_EVENT_STATE_DEAD; |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1760 | } |
| 1761 | |
Mark Rutland | 2c81a64 | 2016-06-14 16:10:41 +0100 | [diff] [blame] | 1762 | static inline int __pmu_filter_match(struct perf_event *event) |
Mark Rutland | 66eb579 | 2015-05-13 17:12:23 +0100 | [diff] [blame] | 1763 | { |
| 1764 | struct pmu *pmu = event->pmu; |
| 1765 | return pmu->filter_match ? pmu->filter_match(event) : 1; |
| 1766 | } |
| 1767 | |
Mark Rutland | 2c81a64 | 2016-06-14 16:10:41 +0100 | [diff] [blame] | 1768 | /* |
| 1769 | * Check whether we should attempt to schedule an event group based on |
| 1770 | * PMU-specific filtering. An event group can consist of HW and SW events, |
| 1771 | * potentially with a SW leader, so we must check all the filters, to |
| 1772 | * determine whether a group is schedulable: |
| 1773 | */ |
| 1774 | static inline int pmu_filter_match(struct perf_event *event) |
| 1775 | { |
| 1776 | struct perf_event *child; |
| 1777 | |
| 1778 | if (!__pmu_filter_match(event)) |
| 1779 | return 0; |
| 1780 | |
| 1781 | list_for_each_entry(child, &event->sibling_list, group_entry) { |
| 1782 | if (!__pmu_filter_match(child)) |
| 1783 | return 0; |
| 1784 | } |
| 1785 | |
| 1786 | return 1; |
| 1787 | } |
| 1788 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1789 | static inline int |
| 1790 | event_filter_match(struct perf_event *event) |
| 1791 | { |
Peter Zijlstra | 0b8f1e2 | 2016-08-04 14:37:24 +0200 | [diff] [blame] | 1792 | return (event->cpu == -1 || event->cpu == smp_processor_id()) && |
| 1793 | perf_cgroup_match(event) && pmu_filter_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1794 | } |
| 1795 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1796 | static void |
| 1797 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1798 | struct perf_cpu_context *cpuctx, |
| 1799 | struct perf_event_context *ctx) |
| 1800 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1801 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1802 | u64 delta; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1803 | |
| 1804 | WARN_ON_ONCE(event->ctx != ctx); |
| 1805 | lockdep_assert_held(&ctx->lock); |
| 1806 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1807 | /* |
| 1808 | * An event which could not be activated because of |
| 1809 | * filter mismatch still needs to have its timings |
| 1810 | * maintained, otherwise bogus information is return |
| 1811 | * via read() for time_enabled, time_running: |
| 1812 | */ |
Peter Zijlstra | 0b8f1e2 | 2016-08-04 14:37:24 +0200 | [diff] [blame] | 1813 | if (event->state == PERF_EVENT_STATE_INACTIVE && |
| 1814 | !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1815 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1816 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1817 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1818 | } |
| 1819 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1820 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1821 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1822 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1823 | perf_pmu_disable(event->pmu); |
| 1824 | |
Peter Zijlstra | 28a967c | 2016-02-24 18:45:46 +0100 | [diff] [blame] | 1825 | event->tstamp_stopped = tstamp; |
| 1826 | event->pmu->del(event, 0); |
| 1827 | event->oncpu = -1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1828 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1829 | if (event->pending_disable) { |
| 1830 | event->pending_disable = 0; |
| 1831 | event->state = PERF_EVENT_STATE_OFF; |
| 1832 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1833 | |
| 1834 | if (!is_software_event(event)) |
| 1835 | cpuctx->active_oncpu--; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1836 | if (!--ctx->nr_active) |
| 1837 | perf_event_ctx_deactivate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1838 | if (event->attr.freq && event->attr.sample_freq) |
| 1839 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1840 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1841 | cpuctx->exclusive = 0; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1842 | |
| 1843 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | static void |
| 1847 | group_sched_out(struct perf_event *group_event, |
| 1848 | struct perf_cpu_context *cpuctx, |
| 1849 | struct perf_event_context *ctx) |
| 1850 | { |
| 1851 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1852 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1853 | |
Mark Rutland | 3f005e7 | 2016-07-26 18:12:21 +0100 | [diff] [blame] | 1854 | perf_pmu_disable(ctx->pmu); |
| 1855 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1856 | event_sched_out(group_event, cpuctx, ctx); |
| 1857 | |
| 1858 | /* |
| 1859 | * Schedule out siblings (if any): |
| 1860 | */ |
| 1861 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1862 | event_sched_out(event, cpuctx, ctx); |
| 1863 | |
Mark Rutland | 3f005e7 | 2016-07-26 18:12:21 +0100 | [diff] [blame] | 1864 | perf_pmu_enable(ctx->pmu); |
| 1865 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1866 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1867 | cpuctx->exclusive = 0; |
| 1868 | } |
| 1869 | |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1870 | #define DETACH_GROUP 0x01UL |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 1871 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1872 | /* |
| 1873 | * Cross CPU call to remove a performance event |
| 1874 | * |
| 1875 | * We disable the event on the hardware level first. After that we |
| 1876 | * remove it from the context list. |
| 1877 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1878 | static void |
| 1879 | __perf_remove_from_context(struct perf_event *event, |
| 1880 | struct perf_cpu_context *cpuctx, |
| 1881 | struct perf_event_context *ctx, |
| 1882 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1883 | { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1884 | unsigned long flags = (unsigned long)info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1885 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1886 | event_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1887 | if (flags & DETACH_GROUP) |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1888 | perf_group_detach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1889 | list_del_event(event, ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1890 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 1891 | if (!ctx->nr_events && ctx->is_active) { |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1892 | ctx->is_active = 0; |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 1893 | if (ctx->task) { |
| 1894 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 1895 | cpuctx->task_ctx = NULL; |
| 1896 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1897 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1898 | } |
| 1899 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1900 | /* |
| 1901 | * Remove the event from a task's (or a CPU's) list of events. |
| 1902 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1903 | * If event->ctx is a cloned context, callers must make sure that |
| 1904 | * every task struct that event->ctx->task could possibly point to |
| 1905 | * remains valid. This is OK when called from perf_release since |
| 1906 | * that only calls us on the top-level context, which can't be a clone. |
| 1907 | * When called from perf_event_exit_task, it's OK because the |
| 1908 | * context has been detached from its task. |
| 1909 | */ |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1910 | 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] | 1911 | { |
Peter Zijlstra | 3996a91 | 2017-01-26 16:39:55 +0100 | [diff] [blame] | 1912 | struct perf_event_context *ctx = event->ctx; |
| 1913 | |
| 1914 | lockdep_assert_held(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1915 | |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1916 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
Peter Zijlstra | 3996a91 | 2017-01-26 16:39:55 +0100 | [diff] [blame] | 1917 | |
| 1918 | /* |
| 1919 | * The above event_function_call() can NO-OP when it hits |
| 1920 | * TASK_TOMBSTONE. In that case we must already have been detached |
| 1921 | * from the context (by perf_event_exit_event()) but the grouping |
| 1922 | * might still be in-tact. |
| 1923 | */ |
| 1924 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1925 | if ((flags & DETACH_GROUP) && |
| 1926 | (event->attach_state & PERF_ATTACH_GROUP)) { |
| 1927 | /* |
| 1928 | * Since in that case we cannot possibly be scheduled, simply |
| 1929 | * detach now. |
| 1930 | */ |
| 1931 | raw_spin_lock_irq(&ctx->lock); |
| 1932 | perf_group_detach(event); |
| 1933 | raw_spin_unlock_irq(&ctx->lock); |
| 1934 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1935 | } |
| 1936 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1937 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1938 | * Cross CPU call to disable a performance event |
| 1939 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1940 | static void __perf_event_disable(struct perf_event *event, |
| 1941 | struct perf_cpu_context *cpuctx, |
| 1942 | struct perf_event_context *ctx, |
| 1943 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1944 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1945 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 1946 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1947 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1948 | update_context_time(ctx); |
| 1949 | update_cgrp_time_from_event(event); |
| 1950 | update_group_times(event); |
| 1951 | if (event == event->group_leader) |
| 1952 | group_sched_out(event, cpuctx, ctx); |
| 1953 | else |
| 1954 | event_sched_out(event, cpuctx, ctx); |
| 1955 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1956 | } |
| 1957 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1958 | /* |
| 1959 | * Disable a event. |
| 1960 | * |
| 1961 | * If event->ctx is a cloned context, callers must make sure that |
| 1962 | * every task struct that event->ctx->task could possibly point to |
| 1963 | * remains valid. This condition is satisifed when called through |
| 1964 | * perf_event_for_each_child or perf_event_for_each because they |
| 1965 | * hold the top-level event's child_mutex, so any descendant that |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 1966 | * goes to exit will block in perf_event_exit_event(). |
| 1967 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1968 | * When called from perf_pending_event it's OK because event->ctx |
| 1969 | * is the current context on this CPU and preemption is disabled, |
| 1970 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1971 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1972 | static void _perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1973 | { |
| 1974 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1975 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1976 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1977 | if (event->state <= PERF_EVENT_STATE_OFF) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1978 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1979 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1980 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1981 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1982 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1983 | event_function_call(event, __perf_event_disable, NULL); |
| 1984 | } |
| 1985 | |
| 1986 | void perf_event_disable_local(struct perf_event *event) |
| 1987 | { |
| 1988 | event_function_local(event, __perf_event_disable, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1989 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1990 | |
| 1991 | /* |
| 1992 | * Strictly speaking kernel users cannot create groups and therefore this |
| 1993 | * interface does not need the perf_event_ctx_lock() magic. |
| 1994 | */ |
| 1995 | void perf_event_disable(struct perf_event *event) |
| 1996 | { |
| 1997 | struct perf_event_context *ctx; |
| 1998 | |
| 1999 | ctx = perf_event_ctx_lock(event); |
| 2000 | _perf_event_disable(event); |
| 2001 | perf_event_ctx_unlock(event, ctx); |
| 2002 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2003 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2004 | |
Jiri Olsa | 5aab90c | 2016-10-26 11:48:24 +0200 | [diff] [blame] | 2005 | void perf_event_disable_inatomic(struct perf_event *event) |
| 2006 | { |
| 2007 | event->pending_disable = 1; |
| 2008 | irq_work_queue(&event->pending); |
| 2009 | } |
| 2010 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2011 | static void perf_set_shadow_time(struct perf_event *event, |
| 2012 | struct perf_event_context *ctx, |
| 2013 | u64 tstamp) |
| 2014 | { |
| 2015 | /* |
| 2016 | * use the correct time source for the time snapshot |
| 2017 | * |
| 2018 | * We could get by without this by leveraging the |
| 2019 | * fact that to get to this function, the caller |
| 2020 | * has most likely already called update_context_time() |
| 2021 | * and update_cgrp_time_xx() and thus both timestamp |
| 2022 | * are identical (or very close). Given that tstamp is, |
| 2023 | * already adjusted for cgroup, we could say that: |
| 2024 | * tstamp - ctx->timestamp |
| 2025 | * is equivalent to |
| 2026 | * tstamp - cgrp->timestamp. |
| 2027 | * |
| 2028 | * Then, in perf_output_read(), the calculation would |
| 2029 | * work with no changes because: |
| 2030 | * - event is guaranteed scheduled in |
| 2031 | * - no scheduled out in between |
| 2032 | * - thus the timestamp would be the same |
| 2033 | * |
| 2034 | * But this is a bit hairy. |
| 2035 | * |
| 2036 | * So instead, we have an explicit cgroup call to remain |
| 2037 | * within the time time source all along. We believe it |
| 2038 | * is cleaner and simpler to understand. |
| 2039 | */ |
| 2040 | if (is_cgroup_event(event)) |
| 2041 | perf_cgroup_set_shadow_time(event, tstamp); |
| 2042 | else |
| 2043 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 2044 | } |
| 2045 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 2046 | #define MAX_INTERRUPTS (~0ULL) |
| 2047 | |
| 2048 | static void perf_log_throttle(struct perf_event *event, int enable); |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 2049 | static void perf_log_itrace_start(struct perf_event *event); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 2050 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2051 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2052 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2053 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2054 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2055 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2056 | u64 tstamp = perf_event_time(event); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2057 | int ret = 0; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2058 | |
Peter Zijlstra | 6334241 | 2014-05-05 11:49:16 +0200 | [diff] [blame] | 2059 | lockdep_assert_held(&ctx->lock); |
| 2060 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2061 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 2062 | return 0; |
| 2063 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2064 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
| 2065 | /* |
| 2066 | * Order event::oncpu write to happen before the ACTIVE state |
| 2067 | * is visible. |
| 2068 | */ |
| 2069 | smp_wmb(); |
| 2070 | WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 2071 | |
| 2072 | /* |
| 2073 | * Unthrottle events, since we scheduled we might have missed several |
| 2074 | * ticks already, also for a heavily scheduling task there is little |
| 2075 | * guarantee it'll get a tick in a timely manner. |
| 2076 | */ |
| 2077 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 2078 | perf_log_throttle(event, 1); |
| 2079 | event->hw.interrupts = 0; |
| 2080 | } |
| 2081 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2082 | /* |
| 2083 | * The new state must be visible before we turn it on in the hardware: |
| 2084 | */ |
| 2085 | smp_wmb(); |
| 2086 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2087 | perf_pmu_disable(event->pmu); |
| 2088 | |
Shaohua Li | 72f669c | 2015-02-05 15:55:31 -0800 | [diff] [blame] | 2089 | perf_set_shadow_time(event, ctx, tstamp); |
| 2090 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 2091 | perf_log_itrace_start(event); |
| 2092 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 2093 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2094 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 2095 | event->oncpu = -1; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2096 | ret = -EAGAIN; |
| 2097 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2098 | } |
| 2099 | |
Peter Zijlstra | 00a2916 | 2015-07-27 10:35:07 +0200 | [diff] [blame] | 2100 | event->tstamp_running += tstamp - event->tstamp_stopped; |
| 2101 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2102 | if (!is_software_event(event)) |
| 2103 | cpuctx->active_oncpu++; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 2104 | if (!ctx->nr_active++) |
| 2105 | perf_event_ctx_activate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 2106 | if (event->attr.freq && event->attr.sample_freq) |
| 2107 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2108 | |
| 2109 | if (event->attr.exclusive) |
| 2110 | cpuctx->exclusive = 1; |
| 2111 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 2112 | out: |
| 2113 | perf_pmu_enable(event->pmu); |
| 2114 | |
| 2115 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | static int |
| 2119 | group_sched_in(struct perf_event *group_event, |
| 2120 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2121 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2122 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 2123 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 4a23459 | 2014-02-24 12:43:31 +0100 | [diff] [blame] | 2124 | struct pmu *pmu = ctx->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2125 | u64 now = ctx->time; |
| 2126 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2127 | |
| 2128 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 2129 | return 0; |
| 2130 | |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 2131 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 2132 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2133 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 2134 | pmu->cancel_txn(pmu); |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 2135 | perf_mux_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2136 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 2137 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2138 | |
| 2139 | /* |
| 2140 | * Schedule in siblings as one group (if any): |
| 2141 | */ |
| 2142 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2143 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2144 | partial_group = event; |
| 2145 | goto group_error; |
| 2146 | } |
| 2147 | } |
| 2148 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2149 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 2150 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2151 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2152 | group_error: |
| 2153 | /* |
| 2154 | * Groups can be scheduled in as one unit only, so undo any |
| 2155 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2156 | * The events up to the failed event are scheduled out normally, |
| 2157 | * tstamp_stopped will be updated. |
| 2158 | * |
| 2159 | * The failed events and the remaining siblings need to have |
| 2160 | * their timings updated as if they had gone thru event_sched_in() |
| 2161 | * and event_sched_out(). This is required to get consistent timings |
| 2162 | * across the group. This also takes care of the case where the group |
| 2163 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 2164 | * the time the event was actually stopped, such that time delta |
| 2165 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2166 | */ |
| 2167 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 2168 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2169 | simulate = true; |
| 2170 | |
| 2171 | if (simulate) { |
| 2172 | event->tstamp_running += now - event->tstamp_stopped; |
| 2173 | event->tstamp_stopped = now; |
| 2174 | } else { |
| 2175 | event_sched_out(event, cpuctx, ctx); |
| 2176 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2177 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2178 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2179 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 2180 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 2181 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 2182 | perf_mux_hrtimer_restart(cpuctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2183 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2184 | return -EAGAIN; |
| 2185 | } |
| 2186 | |
| 2187 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2188 | * Work out whether we can put this event group on the CPU now. |
| 2189 | */ |
| 2190 | static int group_can_go_on(struct perf_event *event, |
| 2191 | struct perf_cpu_context *cpuctx, |
| 2192 | int can_add_hw) |
| 2193 | { |
| 2194 | /* |
| 2195 | * Groups consisting entirely of software events can always go on. |
| 2196 | */ |
David Carrillo-Cisneros | 4ff6a8d | 2016-08-17 13:55:05 -0700 | [diff] [blame] | 2197 | if (event->group_caps & PERF_EV_CAP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2198 | return 1; |
| 2199 | /* |
| 2200 | * If an exclusive group is already on, no other hardware |
| 2201 | * events can go on. |
| 2202 | */ |
| 2203 | if (cpuctx->exclusive) |
| 2204 | return 0; |
| 2205 | /* |
| 2206 | * If this group is exclusive and there are already |
| 2207 | * events on the CPU, it can't go on. |
| 2208 | */ |
| 2209 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 2210 | return 0; |
| 2211 | /* |
| 2212 | * Otherwise, try to add it if all previous groups were able |
| 2213 | * to go on. |
| 2214 | */ |
| 2215 | return can_add_hw; |
| 2216 | } |
| 2217 | |
| 2218 | static void add_event_to_ctx(struct perf_event *event, |
| 2219 | struct perf_event_context *ctx) |
| 2220 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2221 | u64 tstamp = perf_event_time(event); |
| 2222 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2223 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 2224 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2225 | event->tstamp_enabled = tstamp; |
| 2226 | event->tstamp_running = tstamp; |
| 2227 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2228 | } |
| 2229 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2230 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2231 | struct perf_cpu_context *cpuctx, |
| 2232 | enum event_type_t event_type); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2233 | static void |
| 2234 | ctx_sched_in(struct perf_event_context *ctx, |
| 2235 | struct perf_cpu_context *cpuctx, |
| 2236 | enum event_type_t event_type, |
| 2237 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2238 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2239 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2240 | struct perf_event_context *ctx) |
| 2241 | { |
| 2242 | if (!cpuctx->task_ctx) |
| 2243 | return; |
| 2244 | |
| 2245 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2246 | return; |
| 2247 | |
| 2248 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
| 2249 | } |
| 2250 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 2251 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 2252 | struct perf_event_context *ctx, |
| 2253 | struct task_struct *task) |
| 2254 | { |
| 2255 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 2256 | if (ctx) |
| 2257 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 2258 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 2259 | if (ctx) |
| 2260 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 2261 | } |
| 2262 | |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 2263 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
| 2264 | struct perf_event_context *task_ctx) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 2265 | { |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 2266 | perf_pmu_disable(cpuctx->ctx.pmu); |
| 2267 | if (task_ctx) |
| 2268 | task_ctx_sched_out(cpuctx, task_ctx); |
| 2269 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 2270 | perf_event_sched_in(cpuctx, task_ctx, current); |
| 2271 | perf_pmu_enable(cpuctx->ctx.pmu); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2274 | /* |
| 2275 | * Cross CPU call to install and enable a performance event |
| 2276 | * |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2277 | * Very similar to remote_function() + event_function() but cannot assume that |
| 2278 | * things like ctx->is_active and cpuctx->task_ctx are set. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2279 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2280 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2281 | { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2282 | struct perf_event *event = info; |
| 2283 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2284 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2285 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2286 | bool reprogram = true; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2287 | int ret = 0; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2288 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2289 | raw_spin_lock(&cpuctx->ctx.lock); |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2290 | if (ctx->task) { |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2291 | raw_spin_lock(&ctx->lock); |
| 2292 | task_ctx = ctx; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2293 | |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2294 | reprogram = (ctx->task == current); |
| 2295 | |
| 2296 | /* |
| 2297 | * If the task is running, it must be running on this CPU, |
| 2298 | * otherwise we cannot reprogram things. |
| 2299 | * |
| 2300 | * If its not running, we don't care, ctx->lock will |
| 2301 | * serialize against it becoming runnable. |
| 2302 | */ |
| 2303 | if (task_curr(ctx->task) && !reprogram) { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2304 | ret = -ESRCH; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2305 | goto unlock; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2306 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2307 | |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2308 | WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2309 | } else if (task_ctx) { |
| 2310 | raw_spin_lock(&task_ctx->lock); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2311 | } |
| 2312 | |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2313 | if (reprogram) { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2314 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
| 2315 | add_event_to_ctx(event, ctx); |
| 2316 | ctx_resched(cpuctx, task_ctx); |
| 2317 | } else { |
| 2318 | add_event_to_ctx(event, ctx); |
| 2319 | } |
| 2320 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2321 | unlock: |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2322 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2323 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2324 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | /* |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2328 | * Attach a performance event to a context. |
| 2329 | * |
| 2330 | * Very similar to event_function_call, see comment there. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2331 | */ |
| 2332 | static void |
| 2333 | perf_install_in_context(struct perf_event_context *ctx, |
| 2334 | struct perf_event *event, |
| 2335 | int cpu) |
| 2336 | { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2337 | struct task_struct *task = READ_ONCE(ctx->task); |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2338 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2339 | lockdep_assert_held(&ctx->mutex); |
| 2340 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 2341 | if (event->cpu != -1) |
| 2342 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2343 | |
Peter Zijlstra | 0b8f1e2 | 2016-08-04 14:37:24 +0200 | [diff] [blame] | 2344 | /* |
| 2345 | * Ensures that if we can observe event->ctx, both the event and ctx |
| 2346 | * will be 'complete'. See perf_iterate_sb_cpu(). |
| 2347 | */ |
| 2348 | smp_store_release(&event->ctx, ctx); |
| 2349 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2350 | if (!task) { |
| 2351 | cpu_function_call(cpu, __perf_install_in_context, event); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2352 | return; |
| 2353 | } |
Peter Zijlstra | 6f932e5 | 2016-02-24 18:45:43 +0100 | [diff] [blame] | 2354 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2355 | /* |
| 2356 | * Should not happen, we validate the ctx is still alive before calling. |
| 2357 | */ |
| 2358 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) |
| 2359 | return; |
| 2360 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2361 | /* |
| 2362 | * Installing events is tricky because we cannot rely on ctx->is_active |
| 2363 | * to be set in case this is the nr_events 0 -> 1 transition. |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2364 | * |
| 2365 | * Instead we use task_curr(), which tells us if the task is running. |
| 2366 | * However, since we use task_curr() outside of rq::lock, we can race |
| 2367 | * against the actual state. This means the result can be wrong. |
| 2368 | * |
| 2369 | * If we get a false positive, we retry, this is harmless. |
| 2370 | * |
| 2371 | * If we get a false negative, things are complicated. If we are after |
| 2372 | * perf_event_context_sched_in() ctx::lock will serialize us, and the |
| 2373 | * value must be correct. If we're before, it doesn't matter since |
| 2374 | * perf_event_context_sched_in() will program the counter. |
| 2375 | * |
| 2376 | * However, this hinges on the remote context switch having observed |
| 2377 | * our task->perf_event_ctxp[] store, such that it will in fact take |
| 2378 | * ctx::lock in perf_event_context_sched_in(). |
| 2379 | * |
| 2380 | * We do this by task_function_call(), if the IPI fails to hit the task |
| 2381 | * we know any future context switch of task must see the |
| 2382 | * perf_event_ctpx[] store. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2383 | */ |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2384 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2385 | /* |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2386 | * This smp_mb() orders the task->perf_event_ctxp[] store with the |
| 2387 | * task_cpu() load, such that if the IPI then does not find the task |
| 2388 | * running, a future context switch of that task must observe the |
| 2389 | * store. |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2390 | */ |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2391 | smp_mb(); |
| 2392 | again: |
| 2393 | if (!task_function_call(task, __perf_install_in_context, event)) |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2394 | return; |
| 2395 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2396 | raw_spin_lock_irq(&ctx->lock); |
| 2397 | task = ctx->task; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2398 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
| 2399 | /* |
| 2400 | * Cannot happen because we already checked above (which also |
| 2401 | * cannot happen), and we hold ctx->mutex, which serializes us |
| 2402 | * against perf_event_exit_task_context(). |
| 2403 | */ |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2404 | raw_spin_unlock_irq(&ctx->lock); |
| 2405 | return; |
| 2406 | } |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2407 | /* |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2408 | * If the task is not running, ctx->lock will avoid it becoming so, |
| 2409 | * thus we can safely install the event. |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2410 | */ |
Peter Zijlstra | 1c68633 | 2016-12-09 14:59:00 +0100 | [diff] [blame] | 2411 | if (task_curr(task)) { |
| 2412 | raw_spin_unlock_irq(&ctx->lock); |
| 2413 | goto again; |
| 2414 | } |
| 2415 | add_event_to_ctx(event, ctx); |
| 2416 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /* |
| 2420 | * Put a event into inactive state and update time fields. |
| 2421 | * Enabling the leader of a group effectively enables all |
| 2422 | * the group members that aren't explicitly disabled, so we |
| 2423 | * have to update their ->tstamp_enabled also. |
| 2424 | * Note: this works for group members as well as group leaders |
| 2425 | * since the non-leader members' sibling_lists will be empty. |
| 2426 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2427 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2428 | { |
| 2429 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2430 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2431 | |
| 2432 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2433 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2434 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2435 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 2436 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2437 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | /* |
| 2441 | * Cross CPU call to enable a performance event |
| 2442 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2443 | static void __perf_event_enable(struct perf_event *event, |
| 2444 | struct perf_cpu_context *cpuctx, |
| 2445 | struct perf_event_context *ctx, |
| 2446 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2447 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2448 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2449 | struct perf_event_context *task_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2450 | |
Peter Zijlstra | 6e801e01 | 2016-01-26 12:17:08 +0100 | [diff] [blame] | 2451 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
| 2452 | event->state <= PERF_EVENT_STATE_ERROR) |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2453 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2454 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2455 | if (ctx->is_active) |
| 2456 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
| 2457 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2458 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2459 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2460 | if (!ctx->is_active) |
| 2461 | return; |
| 2462 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2463 | if (!event_filter_match(event)) { |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2464 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2465 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2466 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2467 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2468 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2469 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2470 | /* |
| 2471 | * If the event is in a group and isn't the group leader, |
| 2472 | * then don't put it on unless the group is on. |
| 2473 | */ |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2474 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
| 2475 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2476 | return; |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2477 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2478 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2479 | task_ctx = cpuctx->task_ctx; |
| 2480 | if (ctx->task) |
| 2481 | WARN_ON_ONCE(task_ctx != ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2482 | |
Peter Zijlstra | aee7dbc | 2016-01-08 10:45:11 +0100 | [diff] [blame] | 2483 | ctx_resched(cpuctx, task_ctx); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2484 | } |
| 2485 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2486 | /* |
| 2487 | * Enable a event. |
| 2488 | * |
| 2489 | * If event->ctx is a cloned context, callers must make sure that |
| 2490 | * every task struct that event->ctx->task could possibly point to |
| 2491 | * remains valid. This condition is satisfied when called through |
| 2492 | * perf_event_for_each_child or perf_event_for_each as described |
| 2493 | * for perf_event_disable. |
| 2494 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2495 | static void _perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2496 | { |
| 2497 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2498 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2499 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 6e801e01 | 2016-01-26 12:17:08 +0100 | [diff] [blame] | 2500 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
| 2501 | event->state < PERF_EVENT_STATE_ERROR) { |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2502 | raw_spin_unlock_irq(&ctx->lock); |
| 2503 | return; |
| 2504 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2505 | |
| 2506 | /* |
| 2507 | * If the event is in error state, clear that first. |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2508 | * |
| 2509 | * That way, if we see the event in error state below, we know that it |
| 2510 | * has gone back into error state, as distinct from the task having |
| 2511 | * been scheduled away before the cross-call arrived. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2512 | */ |
| 2513 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2514 | event->state = PERF_EVENT_STATE_OFF; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2515 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2516 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2517 | event_function_call(event, __perf_event_enable, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2518 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2519 | |
| 2520 | /* |
| 2521 | * See perf_event_disable(); |
| 2522 | */ |
| 2523 | void perf_event_enable(struct perf_event *event) |
| 2524 | { |
| 2525 | struct perf_event_context *ctx; |
| 2526 | |
| 2527 | ctx = perf_event_ctx_lock(event); |
| 2528 | _perf_event_enable(event); |
| 2529 | perf_event_ctx_unlock(event, ctx); |
| 2530 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2531 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2532 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2533 | struct stop_event_data { |
| 2534 | struct perf_event *event; |
| 2535 | unsigned int restart; |
| 2536 | }; |
| 2537 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2538 | static int __perf_event_stop(void *info) |
| 2539 | { |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2540 | struct stop_event_data *sd = info; |
| 2541 | struct perf_event *event = sd->event; |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2542 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2543 | /* if it's already INACTIVE, do nothing */ |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2544 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
| 2545 | return 0; |
| 2546 | |
| 2547 | /* matches smp_wmb() in event_sched_in() */ |
| 2548 | smp_rmb(); |
| 2549 | |
| 2550 | /* |
| 2551 | * There is a window with interrupts enabled before we get here, |
| 2552 | * so we need to check again lest we try to stop another CPU's event. |
| 2553 | */ |
| 2554 | if (READ_ONCE(event->oncpu) != smp_processor_id()) |
| 2555 | return -EAGAIN; |
| 2556 | |
| 2557 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2558 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2559 | /* |
| 2560 | * May race with the actual stop (through perf_pmu_output_stop()), |
| 2561 | * but it is only used for events with AUX ring buffer, and such |
| 2562 | * events will refuse to restart because of rb::aux_mmap_count==0, |
| 2563 | * see comments in perf_aux_output_begin(). |
| 2564 | * |
| 2565 | * Since this is happening on a event-local CPU, no trace is lost |
| 2566 | * while restarting. |
| 2567 | */ |
| 2568 | if (sd->restart) |
Will Deacon | c9bbdd4 | 2016-08-15 11:42:45 +0100 | [diff] [blame] | 2569 | event->pmu->start(event, 0); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2570 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2571 | return 0; |
| 2572 | } |
| 2573 | |
Alexander Shishkin | 767ae08 | 2016-09-06 16:23:49 +0300 | [diff] [blame] | 2574 | static int perf_event_stop(struct perf_event *event, int restart) |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2575 | { |
| 2576 | struct stop_event_data sd = { |
| 2577 | .event = event, |
Alexander Shishkin | 767ae08 | 2016-09-06 16:23:49 +0300 | [diff] [blame] | 2578 | .restart = restart, |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2579 | }; |
| 2580 | int ret = 0; |
| 2581 | |
| 2582 | do { |
| 2583 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
| 2584 | return 0; |
| 2585 | |
| 2586 | /* matches smp_wmb() in event_sched_in() */ |
| 2587 | smp_rmb(); |
| 2588 | |
| 2589 | /* |
| 2590 | * We only want to restart ACTIVE events, so if the event goes |
| 2591 | * inactive here (event->oncpu==-1), there's nothing more to do; |
| 2592 | * fall through with ret==-ENXIO. |
| 2593 | */ |
| 2594 | ret = cpu_function_call(READ_ONCE(event->oncpu), |
| 2595 | __perf_event_stop, &sd); |
| 2596 | } while (ret == -EAGAIN); |
| 2597 | |
| 2598 | return ret; |
| 2599 | } |
| 2600 | |
| 2601 | /* |
| 2602 | * In order to contain the amount of racy and tricky in the address filter |
| 2603 | * configuration management, it is a two part process: |
| 2604 | * |
| 2605 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, |
| 2606 | * we update the addresses of corresponding vmas in |
| 2607 | * event::addr_filters_offs array and bump the event::addr_filters_gen; |
| 2608 | * (p2) when an event is scheduled in (pmu::add), it calls |
| 2609 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() |
| 2610 | * if the generation has changed since the previous call. |
| 2611 | * |
| 2612 | * If (p1) happens while the event is active, we restart it to force (p2). |
| 2613 | * |
| 2614 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on |
| 2615 | * pre-existing mappings, called once when new filters arrive via SET_FILTER |
| 2616 | * ioctl; |
| 2617 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly |
| 2618 | * registered mapping, called for every new mmap(), with mm::mmap_sem down |
| 2619 | * for reading; |
| 2620 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process |
| 2621 | * of exec. |
| 2622 | */ |
| 2623 | void perf_event_addr_filters_sync(struct perf_event *event) |
| 2624 | { |
| 2625 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 2626 | |
| 2627 | if (!has_addr_filter(event)) |
| 2628 | return; |
| 2629 | |
| 2630 | raw_spin_lock(&ifh->lock); |
| 2631 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { |
| 2632 | event->pmu->addr_filters_sync(event); |
| 2633 | event->hw.addr_filters_gen = event->addr_filters_gen; |
| 2634 | } |
| 2635 | raw_spin_unlock(&ifh->lock); |
| 2636 | } |
| 2637 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); |
| 2638 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2639 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2640 | { |
| 2641 | /* |
| 2642 | * not supported on inherited events |
| 2643 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2644 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2645 | return -EINVAL; |
| 2646 | |
| 2647 | atomic_add(refresh, &event->event_limit); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2648 | _perf_event_enable(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2649 | |
| 2650 | return 0; |
| 2651 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2652 | |
| 2653 | /* |
| 2654 | * See perf_event_disable() |
| 2655 | */ |
| 2656 | int perf_event_refresh(struct perf_event *event, int refresh) |
| 2657 | { |
| 2658 | struct perf_event_context *ctx; |
| 2659 | int ret; |
| 2660 | |
| 2661 | ctx = perf_event_ctx_lock(event); |
| 2662 | ret = _perf_event_refresh(event, refresh); |
| 2663 | perf_event_ctx_unlock(event, ctx); |
| 2664 | |
| 2665 | return ret; |
| 2666 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2667 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2668 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2669 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2670 | struct perf_cpu_context *cpuctx, |
| 2671 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2672 | { |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2673 | int is_active = ctx->is_active; |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2674 | struct perf_event *event; |
| 2675 | |
| 2676 | lockdep_assert_held(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2677 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2678 | if (likely(!ctx->nr_events)) { |
| 2679 | /* |
| 2680 | * See __perf_remove_from_context(). |
| 2681 | */ |
| 2682 | WARN_ON_ONCE(ctx->is_active); |
| 2683 | if (ctx->task) |
| 2684 | WARN_ON_ONCE(cpuctx->task_ctx); |
| 2685 | return; |
| 2686 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2687 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2688 | ctx->is_active &= ~event_type; |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2689 | if (!(ctx->is_active & EVENT_ALL)) |
| 2690 | ctx->is_active = 0; |
| 2691 | |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2692 | if (ctx->task) { |
| 2693 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 2694 | if (!ctx->is_active) |
| 2695 | cpuctx->task_ctx = NULL; |
| 2696 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2697 | |
Peter Zijlstra | 8fdc653 | 2016-03-29 09:26:44 +0200 | [diff] [blame] | 2698 | /* |
| 2699 | * Always update time if it was set; not only when it changes. |
| 2700 | * Otherwise we can 'forget' to update time for any but the last |
| 2701 | * context we sched out. For example: |
| 2702 | * |
| 2703 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) |
| 2704 | * ctx_sched_out(.event_type = EVENT_PINNED) |
| 2705 | * |
| 2706 | * would only update time for the pinned events. |
| 2707 | */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2708 | if (is_active & EVENT_TIME) { |
| 2709 | /* update (and stop) ctx time */ |
| 2710 | update_context_time(ctx); |
| 2711 | update_cgrp_time_from_cpuctx(cpuctx); |
| 2712 | } |
| 2713 | |
Peter Zijlstra | 8fdc653 | 2016-03-29 09:26:44 +0200 | [diff] [blame] | 2714 | is_active ^= ctx->is_active; /* changed bits */ |
| 2715 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2716 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2717 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2718 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2719 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2720 | if (is_active & EVENT_PINNED) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2721 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2722 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2723 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2724 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2725 | if (is_active & EVENT_FLEXIBLE) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2726 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2727 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2728 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2729 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /* |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2733 | * Test whether two contexts are equivalent, i.e. whether they have both been |
| 2734 | * cloned from the same version of the same context. |
| 2735 | * |
| 2736 | * Equivalence is measured using a generation number in the context that is |
| 2737 | * incremented on each modification to it; see unclone_ctx(), list_add_event() |
| 2738 | * and list_del_event(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2739 | */ |
| 2740 | static int context_equiv(struct perf_event_context *ctx1, |
| 2741 | struct perf_event_context *ctx2) |
| 2742 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 2743 | lockdep_assert_held(&ctx1->lock); |
| 2744 | lockdep_assert_held(&ctx2->lock); |
| 2745 | |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2746 | /* Pinning disables the swap optimization */ |
| 2747 | if (ctx1->pin_count || ctx2->pin_count) |
| 2748 | return 0; |
| 2749 | |
| 2750 | /* If ctx1 is the parent of ctx2 */ |
| 2751 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) |
| 2752 | return 1; |
| 2753 | |
| 2754 | /* If ctx2 is the parent of ctx1 */ |
| 2755 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) |
| 2756 | return 1; |
| 2757 | |
| 2758 | /* |
| 2759 | * If ctx1 and ctx2 have the same parent; we flatten the parent |
| 2760 | * hierarchy, see perf_event_init_context(). |
| 2761 | */ |
| 2762 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && |
| 2763 | ctx1->parent_gen == ctx2->parent_gen) |
| 2764 | return 1; |
| 2765 | |
| 2766 | /* Unmatched */ |
| 2767 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2768 | } |
| 2769 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2770 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2771 | struct perf_event *next_event) |
| 2772 | { |
| 2773 | u64 value; |
| 2774 | |
| 2775 | if (!event->attr.inherit_stat) |
| 2776 | return; |
| 2777 | |
| 2778 | /* |
| 2779 | * Update the event value, we cannot use perf_event_read() |
| 2780 | * because we're in the middle of a context switch and have IRQs |
| 2781 | * disabled, which upsets smp_call_function_single(), however |
| 2782 | * we know the event must be on the current CPU, therefore we |
| 2783 | * don't need to use it. |
| 2784 | */ |
| 2785 | switch (event->state) { |
| 2786 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2787 | event->pmu->read(event); |
| 2788 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2789 | |
| 2790 | case PERF_EVENT_STATE_INACTIVE: |
| 2791 | update_event_times(event); |
| 2792 | break; |
| 2793 | |
| 2794 | default: |
| 2795 | break; |
| 2796 | } |
| 2797 | |
| 2798 | /* |
| 2799 | * In order to keep per-task stats reliable we need to flip the event |
| 2800 | * values when we flip the contexts. |
| 2801 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2802 | value = local64_read(&next_event->count); |
| 2803 | value = local64_xchg(&event->count, value); |
| 2804 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2805 | |
| 2806 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2807 | swap(event->total_time_running, next_event->total_time_running); |
| 2808 | |
| 2809 | /* |
| 2810 | * Since we swizzled the values, update the user visible data too. |
| 2811 | */ |
| 2812 | perf_event_update_userpage(event); |
| 2813 | perf_event_update_userpage(next_event); |
| 2814 | } |
| 2815 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2816 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2817 | struct perf_event_context *next_ctx) |
| 2818 | { |
| 2819 | struct perf_event *event, *next_event; |
| 2820 | |
| 2821 | if (!ctx->nr_stat) |
| 2822 | return; |
| 2823 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2824 | update_context_time(ctx); |
| 2825 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2826 | event = list_first_entry(&ctx->event_list, |
| 2827 | struct perf_event, event_entry); |
| 2828 | |
| 2829 | next_event = list_first_entry(&next_ctx->event_list, |
| 2830 | struct perf_event, event_entry); |
| 2831 | |
| 2832 | while (&event->event_entry != &ctx->event_list && |
| 2833 | &next_event->event_entry != &next_ctx->event_list) { |
| 2834 | |
| 2835 | __perf_event_sync_stat(event, next_event); |
| 2836 | |
| 2837 | event = list_next_entry(event, event_entry); |
| 2838 | next_event = list_next_entry(next_event, event_entry); |
| 2839 | } |
| 2840 | } |
| 2841 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2842 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2843 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2844 | { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2845 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2846 | struct perf_event_context *next_ctx; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2847 | struct perf_event_context *parent, *next_parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2848 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2849 | int do_switch = 1; |
| 2850 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2851 | if (likely(!ctx)) |
| 2852 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2853 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2854 | cpuctx = __get_cpu_context(ctx); |
| 2855 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2856 | return; |
| 2857 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2858 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2859 | next_ctx = next->perf_event_ctxp[ctxn]; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2860 | if (!next_ctx) |
| 2861 | goto unlock; |
| 2862 | |
| 2863 | parent = rcu_dereference(ctx->parent_ctx); |
| 2864 | next_parent = rcu_dereference(next_ctx->parent_ctx); |
| 2865 | |
| 2866 | /* If neither context have a parent context; they cannot be clones. */ |
Jiri Olsa | 802c8a6 | 2014-09-12 13:18:28 +0200 | [diff] [blame] | 2867 | if (!parent && !next_parent) |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2868 | goto unlock; |
| 2869 | |
| 2870 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2871 | /* |
| 2872 | * Looks like the two contexts are clones, so we might be |
| 2873 | * able to optimize the context switch. We lock both |
| 2874 | * contexts and check that they are clones under the |
| 2875 | * lock (including re-checking that neither has been |
| 2876 | * uncloned in the meantime). It doesn't matter which |
| 2877 | * order we take the locks because no other cpu could |
| 2878 | * be trying to lock both of these tasks. |
| 2879 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2880 | raw_spin_lock(&ctx->lock); |
| 2881 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2882 | if (context_equiv(ctx, next_ctx)) { |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2883 | WRITE_ONCE(ctx->task, next); |
| 2884 | WRITE_ONCE(next_ctx->task, task); |
Yan, Zheng | 5a158c3 | 2014-11-04 21:56:02 -0500 | [diff] [blame] | 2885 | |
| 2886 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); |
| 2887 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2888 | /* |
| 2889 | * RCU_INIT_POINTER here is safe because we've not |
| 2890 | * modified the ctx and the above modification of |
| 2891 | * ctx->task and ctx->task_ctx_data are immaterial |
| 2892 | * since those values are always verified under |
| 2893 | * ctx->lock which we're now holding. |
| 2894 | */ |
| 2895 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); |
| 2896 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); |
| 2897 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2898 | do_switch = 0; |
| 2899 | |
| 2900 | perf_event_sync_stat(ctx, next_ctx); |
| 2901 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2902 | raw_spin_unlock(&next_ctx->lock); |
| 2903 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2904 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2905 | unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2906 | rcu_read_unlock(); |
| 2907 | |
| 2908 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2909 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 8833d0e | 2016-01-08 10:02:37 +0100 | [diff] [blame] | 2910 | task_ctx_sched_out(cpuctx, ctx); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2911 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2912 | } |
| 2913 | } |
| 2914 | |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2915 | static DEFINE_PER_CPU(struct list_head, sched_cb_list); |
| 2916 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2917 | void perf_sched_cb_dec(struct pmu *pmu) |
| 2918 | { |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2919 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2920 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2921 | this_cpu_dec(perf_sched_cb_usages); |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2922 | |
| 2923 | if (!--cpuctx->sched_cb_usage) |
| 2924 | list_del(&cpuctx->sched_cb_entry); |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2925 | } |
| 2926 | |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2927 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2928 | void perf_sched_cb_inc(struct pmu *pmu) |
| 2929 | { |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2930 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2931 | |
| 2932 | if (!cpuctx->sched_cb_usage++) |
| 2933 | list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list)); |
| 2934 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2935 | this_cpu_inc(perf_sched_cb_usages); |
| 2936 | } |
| 2937 | |
| 2938 | /* |
| 2939 | * This function provides the context switch callback to the lower code |
| 2940 | * layer. It is invoked ONLY when the context switch callback is enabled. |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 2941 | * |
| 2942 | * This callback is relevant even to per-cpu events; for example multi event |
| 2943 | * PEBS requires this to provide PID/TID information. This requires we flush |
| 2944 | * all queued PEBS records before we context switch to a new task. |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2945 | */ |
| 2946 | static void perf_pmu_sched_task(struct task_struct *prev, |
| 2947 | struct task_struct *next, |
| 2948 | bool sched_in) |
| 2949 | { |
| 2950 | struct perf_cpu_context *cpuctx; |
| 2951 | struct pmu *pmu; |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2952 | |
| 2953 | if (prev == next) |
| 2954 | return; |
| 2955 | |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2956 | list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) { |
| 2957 | pmu = cpuctx->unique_pmu; /* software PMUs will not have sched_task */ |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2958 | |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2959 | if (WARN_ON_ONCE(!pmu->sched_task)) |
| 2960 | continue; |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2961 | |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2962 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2963 | perf_pmu_disable(pmu); |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2964 | |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2965 | pmu->sched_task(cpuctx->task_ctx, sched_in); |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2966 | |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 2967 | perf_pmu_enable(pmu); |
| 2968 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2969 | } |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2970 | } |
| 2971 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 2972 | static void perf_event_switch(struct task_struct *task, |
| 2973 | struct task_struct *next_prev, bool sched_in); |
| 2974 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2975 | #define for_each_task_context_nr(ctxn) \ |
| 2976 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2977 | |
| 2978 | /* |
| 2979 | * Called from scheduler to remove the events of the current task, |
| 2980 | * with interrupts disabled. |
| 2981 | * |
| 2982 | * We stop each event and update the event value in event->count. |
| 2983 | * |
| 2984 | * This does not protect us against NMI, but disable() |
| 2985 | * sets the disabled bit in the control field of event _before_ |
| 2986 | * accessing the event control register. If a NMI hits, then it will |
| 2987 | * not restart the event. |
| 2988 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2989 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2990 | struct task_struct *next) |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2991 | { |
| 2992 | int ctxn; |
| 2993 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2994 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 2995 | perf_pmu_sched_task(task, next, false); |
| 2996 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 2997 | if (atomic_read(&nr_switch_events)) |
| 2998 | perf_event_switch(task, next, false); |
| 2999 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3000 | for_each_task_context_nr(ctxn) |
| 3001 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3002 | |
| 3003 | /* |
| 3004 | * if cgroup events exist on this CPU, then we need |
| 3005 | * to check if we have to switch out PMU state. |
| 3006 | * cgroup event are system-wide mode only |
| 3007 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 3008 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 3009 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3010 | } |
| 3011 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3012 | /* |
| 3013 | * Called with IRQs disabled |
| 3014 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3015 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 3016 | enum event_type_t event_type) |
| 3017 | { |
| 3018 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3022 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 3023 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3024 | { |
| 3025 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3026 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3027 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 3028 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3029 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 3030 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3031 | continue; |
| 3032 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3033 | /* may need to reset tstamp_enabled */ |
| 3034 | if (is_cgroup_event(event)) |
| 3035 | perf_cgroup_mark_enabled(event, ctx); |
| 3036 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 3037 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 3038 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3039 | |
| 3040 | /* |
| 3041 | * If this pinned group hasn't been scheduled, |
| 3042 | * put it in error state. |
| 3043 | */ |
| 3044 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 3045 | update_group_times(event); |
| 3046 | event->state = PERF_EVENT_STATE_ERROR; |
| 3047 | } |
| 3048 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | static void |
| 3052 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 3053 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3054 | { |
| 3055 | struct perf_event *event; |
| 3056 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3057 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3058 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 3059 | /* Ignore events in OFF or ERROR state */ |
| 3060 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3061 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3062 | /* |
| 3063 | * Listen to the 'cpu' scheduling filter constraint |
| 3064 | * of events: |
| 3065 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 3066 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3067 | continue; |
| 3068 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3069 | /* may need to reset tstamp_enabled */ |
| 3070 | if (is_cgroup_event(event)) |
| 3071 | perf_cgroup_mark_enabled(event, ctx); |
| 3072 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3073 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 3074 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3075 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3076 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3077 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | static void |
| 3081 | ctx_sched_in(struct perf_event_context *ctx, |
| 3082 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3083 | enum event_type_t event_type, |
| 3084 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3085 | { |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 3086 | int is_active = ctx->is_active; |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 3087 | u64 now; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3088 | |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 3089 | lockdep_assert_held(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3090 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3091 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 3092 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3093 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 3094 | ctx->is_active |= (event_type | EVENT_TIME); |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 3095 | if (ctx->task) { |
| 3096 | if (!is_active) |
| 3097 | cpuctx->task_ctx = ctx; |
| 3098 | else |
| 3099 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 3100 | } |
| 3101 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 3102 | is_active ^= ctx->is_active; /* changed bits */ |
| 3103 | |
| 3104 | if (is_active & EVENT_TIME) { |
| 3105 | /* start ctx time */ |
| 3106 | now = perf_clock(); |
| 3107 | ctx->timestamp = now; |
| 3108 | perf_cgroup_set_timestamp(task, ctx); |
| 3109 | } |
| 3110 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3111 | /* |
| 3112 | * First go through the list and put on any pinned groups |
| 3113 | * in order to give them the best chance of going on. |
| 3114 | */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 3115 | if (is_active & EVENT_PINNED) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 3116 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 3117 | |
| 3118 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 3119 | if (is_active & EVENT_FLEXIBLE) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 3120 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3121 | } |
| 3122 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 3123 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3124 | enum event_type_t event_type, |
| 3125 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 3126 | { |
| 3127 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 3128 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3129 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 3130 | } |
| 3131 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3132 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 3133 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3134 | { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3135 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3136 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3137 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 3138 | if (cpuctx->task_ctx == ctx) |
| 3139 | return; |
| 3140 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 3141 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 3142 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 3143 | /* |
| 3144 | * We want to keep the following priority order: |
| 3145 | * cpu pinned (that don't need to move), task pinned, |
| 3146 | * cpu flexible, task flexible. |
| 3147 | */ |
| 3148 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 3149 | perf_event_sched_in(cpuctx, ctx, task); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 3150 | perf_pmu_enable(ctx->pmu); |
| 3151 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3152 | } |
| 3153 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3154 | /* |
| 3155 | * Called from scheduler to add the events of the current task |
| 3156 | * with interrupts disabled. |
| 3157 | * |
| 3158 | * We restore the event value and then enable it. |
| 3159 | * |
| 3160 | * This does not protect us against NMI, but enable() |
| 3161 | * sets the enabled bit in the control field of event _before_ |
| 3162 | * accessing the event control register. If a NMI hits, then it will |
| 3163 | * keep the event running. |
| 3164 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 3165 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 3166 | struct task_struct *task) |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3167 | { |
| 3168 | struct perf_event_context *ctx; |
| 3169 | int ctxn; |
| 3170 | |
Peter Zijlstra | 7e41d17 | 2016-01-08 09:21:40 +0100 | [diff] [blame] | 3171 | /* |
| 3172 | * If cgroup events exist on this CPU, then we need to check if we have |
| 3173 | * to switch in PMU state; cgroup event are system-wide mode only. |
| 3174 | * |
| 3175 | * Since cgroup events are CPU events, we must schedule these in before |
| 3176 | * we schedule in the task events. |
| 3177 | */ |
| 3178 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
| 3179 | perf_cgroup_sched_in(prev, task); |
| 3180 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3181 | for_each_task_context_nr(ctxn) { |
| 3182 | ctx = task->perf_event_ctxp[ctxn]; |
| 3183 | if (likely(!ctx)) |
| 3184 | continue; |
| 3185 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3186 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3187 | } |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 3188 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3189 | if (atomic_read(&nr_switch_events)) |
| 3190 | perf_event_switch(task, prev, true); |
| 3191 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 3192 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 3193 | perf_pmu_sched_task(prev, task, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3194 | } |
| 3195 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3196 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 3197 | { |
| 3198 | u64 frequency = event->attr.sample_freq; |
| 3199 | u64 sec = NSEC_PER_SEC; |
| 3200 | u64 divisor, dividend; |
| 3201 | |
| 3202 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 3203 | |
| 3204 | count_fls = fls64(count); |
| 3205 | nsec_fls = fls64(nsec); |
| 3206 | frequency_fls = fls64(frequency); |
| 3207 | sec_fls = 30; |
| 3208 | |
| 3209 | /* |
| 3210 | * We got @count in @nsec, with a target of sample_freq HZ |
| 3211 | * the target period becomes: |
| 3212 | * |
| 3213 | * @count * 10^9 |
| 3214 | * period = ------------------- |
| 3215 | * @nsec * sample_freq |
| 3216 | * |
| 3217 | */ |
| 3218 | |
| 3219 | /* |
| 3220 | * Reduce accuracy by one bit such that @a and @b converge |
| 3221 | * to a similar magnitude. |
| 3222 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3223 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3224 | do { \ |
| 3225 | if (a##_fls > b##_fls) { \ |
| 3226 | a >>= 1; \ |
| 3227 | a##_fls--; \ |
| 3228 | } else { \ |
| 3229 | b >>= 1; \ |
| 3230 | b##_fls--; \ |
| 3231 | } \ |
| 3232 | } while (0) |
| 3233 | |
| 3234 | /* |
| 3235 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 3236 | * the other, so that finally we can do a u64/u64 division. |
| 3237 | */ |
| 3238 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 3239 | REDUCE_FLS(nsec, frequency); |
| 3240 | REDUCE_FLS(sec, count); |
| 3241 | } |
| 3242 | |
| 3243 | if (count_fls + sec_fls > 64) { |
| 3244 | divisor = nsec * frequency; |
| 3245 | |
| 3246 | while (count_fls + sec_fls > 64) { |
| 3247 | REDUCE_FLS(count, sec); |
| 3248 | divisor >>= 1; |
| 3249 | } |
| 3250 | |
| 3251 | dividend = count * sec; |
| 3252 | } else { |
| 3253 | dividend = count * sec; |
| 3254 | |
| 3255 | while (nsec_fls + frequency_fls > 64) { |
| 3256 | REDUCE_FLS(nsec, frequency); |
| 3257 | dividend >>= 1; |
| 3258 | } |
| 3259 | |
| 3260 | divisor = nsec * frequency; |
| 3261 | } |
| 3262 | |
Peter Zijlstra | f6ab91a | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 3263 | if (!divisor) |
| 3264 | return dividend; |
| 3265 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3266 | return div64_u64(dividend, divisor); |
| 3267 | } |
| 3268 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3269 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 3270 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 3271 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3272 | 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] | 3273 | { |
| 3274 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91a | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 3275 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3276 | s64 delta; |
| 3277 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3278 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3279 | |
| 3280 | delta = (s64)(period - hwc->sample_period); |
| 3281 | delta = (delta + 7) / 8; /* low pass filter */ |
| 3282 | |
| 3283 | sample_period = hwc->sample_period + delta; |
| 3284 | |
| 3285 | if (!sample_period) |
| 3286 | sample_period = 1; |
| 3287 | |
| 3288 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3289 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3290 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3291 | if (disable) |
| 3292 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3293 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3294 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3295 | |
| 3296 | if (disable) |
| 3297 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3298 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3299 | } |
| 3300 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3301 | /* |
| 3302 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 3303 | * events. At the same time, make sure, having freq events does not change |
| 3304 | * the rate of unthrottling as that would introduce bias. |
| 3305 | */ |
| 3306 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 3307 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3308 | { |
| 3309 | struct perf_event *event; |
| 3310 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3311 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3312 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3313 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3314 | /* |
| 3315 | * only need to iterate over all events iff: |
| 3316 | * - context have events in frequency mode (needs freq adjust) |
| 3317 | * - there are events to unthrottle on this cpu |
| 3318 | */ |
| 3319 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3320 | return; |
| 3321 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3322 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3323 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3324 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 3325 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3326 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 3327 | continue; |
| 3328 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 3329 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 3330 | continue; |
| 3331 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3332 | perf_pmu_disable(event->pmu); |
| 3333 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3334 | hwc = &event->hw; |
| 3335 | |
Jiri Olsa | ae23bff | 2013-08-24 16:45:54 +0200 | [diff] [blame] | 3336 | if (hwc->interrupts == MAX_INTERRUPTS) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3337 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3338 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 3339 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | if (!event->attr.freq || !event->attr.sample_freq) |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3343 | goto next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3344 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3345 | /* |
| 3346 | * stop the event and update event->count |
| 3347 | */ |
| 3348 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3349 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3350 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3351 | delta = now - hwc->freq_count_stamp; |
| 3352 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3353 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3354 | /* |
| 3355 | * restart the event |
| 3356 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3357 | * we have stopped the event so tell that |
| 3358 | * to perf_adjust_period() to avoid stopping it |
| 3359 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3360 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3361 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3362 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3363 | |
| 3364 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3365 | next: |
| 3366 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3367 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3368 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3369 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3370 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | /* |
| 3374 | * Round-robin a context's events: |
| 3375 | */ |
| 3376 | static void rotate_ctx(struct perf_event_context *ctx) |
| 3377 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 3378 | /* |
| 3379 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 3380 | * disabled by the inheritance code. |
| 3381 | */ |
| 3382 | if (!ctx->rotate_disable) |
| 3383 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3386 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3387 | { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3388 | struct perf_event_context *ctx = NULL; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3389 | int rotate = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3390 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3391 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3392 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 3393 | rotate = 1; |
| 3394 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3395 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3396 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3397 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3398 | if (ctx->nr_events != ctx->nr_active) |
| 3399 | rotate = 1; |
| 3400 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3401 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3402 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3403 | goto done; |
| 3404 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 3405 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 3406 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3407 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3408 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 3409 | if (ctx) |
| 3410 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 3411 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3412 | rotate_ctx(&cpuctx->ctx); |
| 3413 | if (ctx) |
| 3414 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3415 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3416 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3417 | |
| 3418 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 3419 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3420 | done: |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3421 | |
| 3422 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | void perf_event_task_tick(void) |
| 3426 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3427 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
| 3428 | struct perf_event_context *ctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3429 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3430 | |
| 3431 | WARN_ON(!irqs_disabled()); |
| 3432 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3433 | __this_cpu_inc(perf_throttled_seq); |
| 3434 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3435 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3436 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3437 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3438 | perf_adjust_freq_unthr_context(ctx, throttled); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3439 | } |
| 3440 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3441 | static int event_enable_on_exec(struct perf_event *event, |
| 3442 | struct perf_event_context *ctx) |
| 3443 | { |
| 3444 | if (!event->attr.enable_on_exec) |
| 3445 | return 0; |
| 3446 | |
| 3447 | event->attr.enable_on_exec = 0; |
| 3448 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 3449 | return 0; |
| 3450 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 3451 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3452 | |
| 3453 | return 1; |
| 3454 | } |
| 3455 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3456 | /* |
| 3457 | * Enable all of a task's events that have been marked enable-on-exec. |
| 3458 | * This expects task == current. |
| 3459 | */ |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3460 | static void perf_event_enable_on_exec(int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3461 | { |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3462 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3463 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3464 | struct perf_event *event; |
| 3465 | unsigned long flags; |
| 3466 | int enabled = 0; |
| 3467 | |
| 3468 | local_irq_save(flags); |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3469 | ctx = current->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3470 | if (!ctx || !ctx->nr_events) |
| 3471 | goto out; |
| 3472 | |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3473 | cpuctx = __get_cpu_context(ctx); |
| 3474 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 7fce250 | 2016-02-24 18:45:48 +0100 | [diff] [blame] | 3475 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3476 | list_for_each_entry(event, &ctx->event_list, event_entry) |
| 3477 | enabled |= event_enable_on_exec(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3478 | |
| 3479 | /* |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3480 | * Unclone and reschedule this context if we enabled any event. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3481 | */ |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3482 | if (enabled) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3483 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3484 | ctx_resched(cpuctx, ctx); |
| 3485 | } |
| 3486 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3487 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3488 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3489 | local_irq_restore(flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3490 | |
| 3491 | if (clone_ctx) |
| 3492 | put_ctx(clone_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3493 | } |
| 3494 | |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3495 | struct perf_read_data { |
| 3496 | struct perf_event *event; |
| 3497 | bool group; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3498 | int ret; |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3499 | }; |
| 3500 | |
Peter Zijlstra | e5c2e51 | 2017-01-31 11:27:10 +0100 | [diff] [blame] | 3501 | static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) |
David Carrillo-Cisneros | d6a2f903 | 2016-08-17 13:55:06 -0700 | [diff] [blame] | 3502 | { |
David Carrillo-Cisneros | d6a2f903 | 2016-08-17 13:55:06 -0700 | [diff] [blame] | 3503 | u16 local_pkg, event_pkg; |
| 3504 | |
| 3505 | if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { |
Peter Zijlstra | e5c2e51 | 2017-01-31 11:27:10 +0100 | [diff] [blame] | 3506 | int local_cpu = smp_processor_id(); |
| 3507 | |
| 3508 | event_pkg = topology_physical_package_id(event_cpu); |
| 3509 | local_pkg = topology_physical_package_id(local_cpu); |
David Carrillo-Cisneros | d6a2f903 | 2016-08-17 13:55:06 -0700 | [diff] [blame] | 3510 | |
| 3511 | if (event_pkg == local_pkg) |
| 3512 | return local_cpu; |
| 3513 | } |
| 3514 | |
| 3515 | return event_cpu; |
| 3516 | } |
| 3517 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3518 | /* |
| 3519 | * Cross CPU call to read the hardware event |
| 3520 | */ |
| 3521 | static void __perf_event_read(void *info) |
| 3522 | { |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3523 | struct perf_read_data *data = info; |
| 3524 | struct perf_event *sub, *event = data->event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3525 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3526 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3527 | struct pmu *pmu = event->pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3528 | |
| 3529 | /* |
| 3530 | * If this is a task context, we need to check whether it is |
| 3531 | * the current task context of this cpu. If not it has been |
| 3532 | * scheduled out before the smp call arrived. In that case |
| 3533 | * event->count would have been updated to a recent sample |
| 3534 | * when the event was scheduled out. |
| 3535 | */ |
| 3536 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 3537 | return; |
| 3538 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3539 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3540 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3541 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3542 | update_cgrp_time_from_event(event); |
| 3543 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3544 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3545 | update_event_times(event); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3546 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3547 | goto unlock; |
| 3548 | |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3549 | if (!data->group) { |
| 3550 | pmu->read(event); |
| 3551 | data->ret = 0; |
| 3552 | goto unlock; |
| 3553 | } |
| 3554 | |
| 3555 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); |
| 3556 | |
| 3557 | pmu->read(event); |
| 3558 | |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3559 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 3560 | update_event_times(sub); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3561 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
| 3562 | /* |
| 3563 | * Use sibling's PMU rather than @event's since |
| 3564 | * sibling could be on different (eg: software) PMU. |
| 3565 | */ |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3566 | sub->pmu->read(sub); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3567 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3568 | } |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3569 | |
| 3570 | data->ret = pmu->commit_txn(pmu); |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3571 | |
| 3572 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3573 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3574 | } |
| 3575 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3576 | static inline u64 perf_event_count(struct perf_event *event) |
| 3577 | { |
Matt Fleming | eacd3ec | 2015-01-23 18:45:41 +0000 | [diff] [blame] | 3578 | if (event->pmu->count) |
| 3579 | return event->pmu->count(event); |
| 3580 | |
| 3581 | return __perf_event_count(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3582 | } |
| 3583 | |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 3584 | /* |
| 3585 | * NMI-safe method to read a local event, that is an event that |
| 3586 | * is: |
| 3587 | * - either for the current task, or for this CPU |
| 3588 | * - does not have inherit set, for inherited task events |
| 3589 | * will not be local and we cannot read them atomically |
| 3590 | * - must not have a pmu::count method |
| 3591 | */ |
| 3592 | u64 perf_event_read_local(struct perf_event *event) |
| 3593 | { |
| 3594 | unsigned long flags; |
| 3595 | u64 val; |
| 3596 | |
| 3597 | /* |
| 3598 | * Disabling interrupts avoids all counter scheduling (context |
| 3599 | * switches, timer based rotation and IPIs). |
| 3600 | */ |
| 3601 | local_irq_save(flags); |
| 3602 | |
| 3603 | /* If this is a per-task event, it must be for current */ |
| 3604 | WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) && |
| 3605 | event->hw.target != current); |
| 3606 | |
| 3607 | /* If this is a per-CPU event, it must be for this CPU */ |
| 3608 | WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) && |
| 3609 | event->cpu != smp_processor_id()); |
| 3610 | |
| 3611 | /* |
| 3612 | * It must not be an event with inherit set, we cannot read |
| 3613 | * all child counters from atomic context. |
| 3614 | */ |
| 3615 | WARN_ON_ONCE(event->attr.inherit); |
| 3616 | |
| 3617 | /* |
| 3618 | * It must not have a pmu::count method, those are not |
| 3619 | * NMI safe. |
| 3620 | */ |
| 3621 | WARN_ON_ONCE(event->pmu->count); |
| 3622 | |
| 3623 | /* |
| 3624 | * If the event is currently on this CPU, its either a per-task event, |
| 3625 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise |
| 3626 | * oncpu == -1). |
| 3627 | */ |
| 3628 | if (event->oncpu == smp_processor_id()) |
| 3629 | event->pmu->read(event); |
| 3630 | |
| 3631 | val = local64_read(&event->count); |
| 3632 | local_irq_restore(flags); |
| 3633 | |
| 3634 | return val; |
| 3635 | } |
| 3636 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3637 | static int perf_event_read(struct perf_event *event, bool group) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3638 | { |
Peter Zijlstra | e5c2e51 | 2017-01-31 11:27:10 +0100 | [diff] [blame] | 3639 | int event_cpu, ret = 0; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3640 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3641 | /* |
| 3642 | * If event is enabled and currently active on a CPU, update the |
| 3643 | * value in the event structure: |
| 3644 | */ |
| 3645 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3646 | struct perf_read_data data = { |
| 3647 | .event = event, |
| 3648 | .group = group, |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3649 | .ret = 0, |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3650 | }; |
David Carrillo-Cisneros | d6a2f903 | 2016-08-17 13:55:06 -0700 | [diff] [blame] | 3651 | |
Peter Zijlstra | e5c2e51 | 2017-01-31 11:27:10 +0100 | [diff] [blame] | 3652 | event_cpu = READ_ONCE(event->oncpu); |
| 3653 | if ((unsigned)event_cpu >= nr_cpu_ids) |
| 3654 | return 0; |
| 3655 | |
| 3656 | preempt_disable(); |
| 3657 | event_cpu = __perf_event_read_cpu(event, event_cpu); |
David Carrillo-Cisneros | d6a2f903 | 2016-08-17 13:55:06 -0700 | [diff] [blame] | 3658 | |
Peter Zijlstra | 5876314 | 2016-08-30 10:15:03 +0200 | [diff] [blame] | 3659 | /* |
| 3660 | * Purposely ignore the smp_call_function_single() return |
| 3661 | * value. |
| 3662 | * |
Peter Zijlstra | e5c2e51 | 2017-01-31 11:27:10 +0100 | [diff] [blame] | 3663 | * If event_cpu isn't a valid CPU it means the event got |
Peter Zijlstra | 5876314 | 2016-08-30 10:15:03 +0200 | [diff] [blame] | 3664 | * scheduled out and that will have updated the event count. |
| 3665 | * |
| 3666 | * Therefore, either way, we'll have an up-to-date event count |
| 3667 | * after this. |
| 3668 | */ |
Peter Zijlstra | e5c2e51 | 2017-01-31 11:27:10 +0100 | [diff] [blame] | 3669 | (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1); |
| 3670 | preempt_enable(); |
Peter Zijlstra | 5876314 | 2016-08-30 10:15:03 +0200 | [diff] [blame] | 3671 | ret = data.ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3672 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 3673 | struct perf_event_context *ctx = event->ctx; |
| 3674 | unsigned long flags; |
| 3675 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3676 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3677 | /* |
| 3678 | * may read while context is not active |
| 3679 | * (e.g., thread is blocked), in that case |
| 3680 | * we cannot update context time |
| 3681 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3682 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3683 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3684 | update_cgrp_time_from_event(event); |
| 3685 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3686 | if (group) |
| 3687 | update_group_times(event); |
| 3688 | else |
| 3689 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3690 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3691 | } |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3692 | |
| 3693 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | /* |
| 3697 | * Initialize the perf_event context in a task_struct: |
| 3698 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3699 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3700 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3701 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3702 | mutex_init(&ctx->mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3703 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3704 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 3705 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3706 | INIT_LIST_HEAD(&ctx->event_list); |
| 3707 | atomic_set(&ctx->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3708 | } |
| 3709 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3710 | static struct perf_event_context * |
| 3711 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3712 | { |
| 3713 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3714 | |
| 3715 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 3716 | if (!ctx) |
| 3717 | return NULL; |
| 3718 | |
| 3719 | __perf_event_init_context(ctx); |
| 3720 | if (task) { |
| 3721 | ctx->task = task; |
| 3722 | get_task_struct(task); |
| 3723 | } |
| 3724 | ctx->pmu = pmu; |
| 3725 | |
| 3726 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3727 | } |
| 3728 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3729 | static struct task_struct * |
| 3730 | find_lively_task_by_vpid(pid_t vpid) |
| 3731 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3732 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3733 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3734 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3735 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3736 | task = current; |
| 3737 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3738 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3739 | if (task) |
| 3740 | get_task_struct(task); |
| 3741 | rcu_read_unlock(); |
| 3742 | |
| 3743 | if (!task) |
| 3744 | return ERR_PTR(-ESRCH); |
| 3745 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3746 | return task; |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3747 | } |
| 3748 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3749 | /* |
| 3750 | * Returns a matching context with refcount and pincount. |
| 3751 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3752 | static struct perf_event_context * |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3753 | find_get_context(struct pmu *pmu, struct task_struct *task, |
| 3754 | struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3755 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3756 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3757 | struct perf_cpu_context *cpuctx; |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3758 | void *task_ctx_data = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3759 | unsigned long flags; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3760 | int ctxn, err; |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3761 | int cpu = event->cpu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3762 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3763 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3764 | /* Must be root to operate on a CPU event: */ |
| 3765 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3766 | return ERR_PTR(-EACCES); |
| 3767 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3768 | /* |
| 3769 | * We could be clever and allow to attach a event to an |
| 3770 | * offline CPU and activate it when the CPU comes up, but |
| 3771 | * that's for later. |
| 3772 | */ |
| 3773 | if (!cpu_online(cpu)) |
| 3774 | return ERR_PTR(-ENODEV); |
| 3775 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3776 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3777 | ctx = &cpuctx->ctx; |
| 3778 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3779 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3780 | |
| 3781 | return ctx; |
| 3782 | } |
| 3783 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3784 | err = -EINVAL; |
| 3785 | ctxn = pmu->task_ctx_nr; |
| 3786 | if (ctxn < 0) |
| 3787 | goto errout; |
| 3788 | |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3789 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
| 3790 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); |
| 3791 | if (!task_ctx_data) { |
| 3792 | err = -ENOMEM; |
| 3793 | goto errout; |
| 3794 | } |
| 3795 | } |
| 3796 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3797 | retry: |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3798 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3799 | if (ctx) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3800 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3801 | ++ctx->pin_count; |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3802 | |
| 3803 | if (task_ctx_data && !ctx->task_ctx_data) { |
| 3804 | ctx->task_ctx_data = task_ctx_data; |
| 3805 | task_ctx_data = NULL; |
| 3806 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3807 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3808 | |
| 3809 | if (clone_ctx) |
| 3810 | put_ctx(clone_ctx); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3811 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3812 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3813 | err = -ENOMEM; |
| 3814 | if (!ctx) |
| 3815 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3816 | |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3817 | if (task_ctx_data) { |
| 3818 | ctx->task_ctx_data = task_ctx_data; |
| 3819 | task_ctx_data = NULL; |
| 3820 | } |
| 3821 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3822 | err = 0; |
| 3823 | mutex_lock(&task->perf_event_mutex); |
| 3824 | /* |
| 3825 | * If it has already passed perf_event_exit_task(). |
| 3826 | * we must see PF_EXITING, it takes this mutex too. |
| 3827 | */ |
| 3828 | if (task->flags & PF_EXITING) |
| 3829 | err = -ESRCH; |
| 3830 | else if (task->perf_event_ctxp[ctxn]) |
| 3831 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3832 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3833 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3834 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3835 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3836 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3837 | mutex_unlock(&task->perf_event_mutex); |
| 3838 | |
| 3839 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3840 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3841 | |
| 3842 | if (err == -EAGAIN) |
| 3843 | goto retry; |
| 3844 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3845 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3846 | } |
| 3847 | |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3848 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3849 | return ctx; |
| 3850 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3851 | errout: |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3852 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3853 | return ERR_PTR(err); |
| 3854 | } |
| 3855 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3856 | static void perf_event_free_filter(struct perf_event *event); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 3857 | static void perf_event_free_bpf_prog(struct perf_event *event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3858 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3859 | static void free_event_rcu(struct rcu_head *head) |
| 3860 | { |
| 3861 | struct perf_event *event; |
| 3862 | |
| 3863 | event = container_of(head, struct perf_event, rcu_head); |
| 3864 | if (event->ns) |
| 3865 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3866 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3867 | kfree(event); |
| 3868 | } |
| 3869 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3870 | static void ring_buffer_attach(struct perf_event *event, |
| 3871 | struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3872 | |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 3873 | static void detach_sb_event(struct perf_event *event) |
| 3874 | { |
| 3875 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); |
| 3876 | |
| 3877 | raw_spin_lock(&pel->lock); |
| 3878 | list_del_rcu(&event->sb_list); |
| 3879 | raw_spin_unlock(&pel->lock); |
| 3880 | } |
| 3881 | |
David Carrillo-Cisneros | a4f144e | 2016-06-01 12:33:05 -0700 | [diff] [blame] | 3882 | static bool is_sb_event(struct perf_event *event) |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 3883 | { |
David Carrillo-Cisneros | a4f144e | 2016-06-01 12:33:05 -0700 | [diff] [blame] | 3884 | struct perf_event_attr *attr = &event->attr; |
| 3885 | |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 3886 | if (event->parent) |
David Carrillo-Cisneros | a4f144e | 2016-06-01 12:33:05 -0700 | [diff] [blame] | 3887 | return false; |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 3888 | |
| 3889 | if (event->attach_state & PERF_ATTACH_TASK) |
David Carrillo-Cisneros | a4f144e | 2016-06-01 12:33:05 -0700 | [diff] [blame] | 3890 | return false; |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 3891 | |
David Carrillo-Cisneros | a4f144e | 2016-06-01 12:33:05 -0700 | [diff] [blame] | 3892 | if (attr->mmap || attr->mmap_data || attr->mmap2 || |
| 3893 | attr->comm || attr->comm_exec || |
| 3894 | attr->task || |
| 3895 | attr->context_switch) |
| 3896 | return true; |
| 3897 | return false; |
| 3898 | } |
| 3899 | |
| 3900 | static void unaccount_pmu_sb_event(struct perf_event *event) |
| 3901 | { |
| 3902 | if (is_sb_event(event)) |
| 3903 | detach_sb_event(event); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 3904 | } |
| 3905 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3906 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
| 3907 | { |
| 3908 | if (event->parent) |
| 3909 | return; |
| 3910 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3911 | if (is_cgroup_event(event)) |
| 3912 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); |
| 3913 | } |
| 3914 | |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3915 | #ifdef CONFIG_NO_HZ_FULL |
| 3916 | static DEFINE_SPINLOCK(nr_freq_lock); |
| 3917 | #endif |
| 3918 | |
| 3919 | static void unaccount_freq_event_nohz(void) |
| 3920 | { |
| 3921 | #ifdef CONFIG_NO_HZ_FULL |
| 3922 | spin_lock(&nr_freq_lock); |
| 3923 | if (atomic_dec_and_test(&nr_freq_events)) |
| 3924 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); |
| 3925 | spin_unlock(&nr_freq_lock); |
| 3926 | #endif |
| 3927 | } |
| 3928 | |
| 3929 | static void unaccount_freq_event(void) |
| 3930 | { |
| 3931 | if (tick_nohz_full_enabled()) |
| 3932 | unaccount_freq_event_nohz(); |
| 3933 | else |
| 3934 | atomic_dec(&nr_freq_events); |
| 3935 | } |
| 3936 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3937 | static void unaccount_event(struct perf_event *event) |
| 3938 | { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3939 | bool dec = false; |
| 3940 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3941 | if (event->parent) |
| 3942 | return; |
| 3943 | |
| 3944 | if (event->attach_state & PERF_ATTACH_TASK) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3945 | dec = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3946 | if (event->attr.mmap || event->attr.mmap_data) |
| 3947 | atomic_dec(&nr_mmap_events); |
| 3948 | if (event->attr.comm) |
| 3949 | atomic_dec(&nr_comm_events); |
| 3950 | if (event->attr.task) |
| 3951 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3952 | if (event->attr.freq) |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3953 | unaccount_freq_event(); |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3954 | if (event->attr.context_switch) { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3955 | dec = true; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3956 | atomic_dec(&nr_switch_events); |
| 3957 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3958 | if (is_cgroup_event(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3959 | dec = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3960 | if (has_branch_stack(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3961 | dec = true; |
| 3962 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 3963 | if (dec) { |
| 3964 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) |
| 3965 | schedule_delayed_work(&perf_sched_work, HZ); |
| 3966 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3967 | |
| 3968 | unaccount_event_cpu(event, event->cpu); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 3969 | |
| 3970 | unaccount_pmu_sb_event(event); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3971 | } |
| 3972 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 3973 | static void perf_sched_delayed(struct work_struct *work) |
| 3974 | { |
| 3975 | mutex_lock(&perf_sched_mutex); |
| 3976 | if (atomic_dec_and_test(&perf_sched_count)) |
| 3977 | static_branch_disable(&perf_sched_events); |
| 3978 | mutex_unlock(&perf_sched_mutex); |
| 3979 | } |
| 3980 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 3981 | /* |
| 3982 | * The following implement mutual exclusion of events on "exclusive" pmus |
| 3983 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled |
| 3984 | * at a time, so we disallow creating events that might conflict, namely: |
| 3985 | * |
| 3986 | * 1) cpu-wide events in the presence of per-task events, |
| 3987 | * 2) per-task events in the presence of cpu-wide events, |
| 3988 | * 3) two matching events on the same context. |
| 3989 | * |
| 3990 | * 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] | 3991 | * _free_event()), the latter -- before the first perf_install_in_context(). |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 3992 | */ |
| 3993 | static int exclusive_event_init(struct perf_event *event) |
| 3994 | { |
| 3995 | struct pmu *pmu = event->pmu; |
| 3996 | |
| 3997 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3998 | return 0; |
| 3999 | |
| 4000 | /* |
| 4001 | * Prevent co-existence of per-task and cpu-wide events on the |
| 4002 | * same exclusive pmu. |
| 4003 | * |
| 4004 | * Negative pmu::exclusive_cnt means there are cpu-wide |
| 4005 | * events on this "exclusive" pmu, positive means there are |
| 4006 | * per-task events. |
| 4007 | * |
| 4008 | * Since this is called in perf_event_alloc() path, event::ctx |
| 4009 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK |
| 4010 | * to mean "per-task event", because unlike other attach states it |
| 4011 | * never gets cleared. |
| 4012 | */ |
| 4013 | if (event->attach_state & PERF_ATTACH_TASK) { |
| 4014 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) |
| 4015 | return -EBUSY; |
| 4016 | } else { |
| 4017 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) |
| 4018 | return -EBUSY; |
| 4019 | } |
| 4020 | |
| 4021 | return 0; |
| 4022 | } |
| 4023 | |
| 4024 | static void exclusive_event_destroy(struct perf_event *event) |
| 4025 | { |
| 4026 | struct pmu *pmu = event->pmu; |
| 4027 | |
| 4028 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 4029 | return; |
| 4030 | |
| 4031 | /* see comment in exclusive_event_init() */ |
| 4032 | if (event->attach_state & PERF_ATTACH_TASK) |
| 4033 | atomic_dec(&pmu->exclusive_cnt); |
| 4034 | else |
| 4035 | atomic_inc(&pmu->exclusive_cnt); |
| 4036 | } |
| 4037 | |
| 4038 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) |
| 4039 | { |
Alexander Shishkin | 3bf6215 | 2016-09-20 18:48:11 +0300 | [diff] [blame] | 4040 | if ((e1->pmu == e2->pmu) && |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 4041 | (e1->cpu == e2->cpu || |
| 4042 | e1->cpu == -1 || |
| 4043 | e2->cpu == -1)) |
| 4044 | return true; |
| 4045 | return false; |
| 4046 | } |
| 4047 | |
| 4048 | /* Called under the same ctx::mutex as perf_install_in_context() */ |
| 4049 | static bool exclusive_event_installable(struct perf_event *event, |
| 4050 | struct perf_event_context *ctx) |
| 4051 | { |
| 4052 | struct perf_event *iter_event; |
| 4053 | struct pmu *pmu = event->pmu; |
| 4054 | |
| 4055 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 4056 | return true; |
| 4057 | |
| 4058 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { |
| 4059 | if (exclusive_event_match(iter_event, event)) |
| 4060 | return false; |
| 4061 | } |
| 4062 | |
| 4063 | return true; |
| 4064 | } |
| 4065 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 4066 | static void perf_addr_filters_splice(struct perf_event *event, |
| 4067 | struct list_head *head); |
| 4068 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4069 | static void _free_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4070 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 4071 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4072 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 4073 | unaccount_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4074 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4075 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4076 | /* |
| 4077 | * Can happen when we close an event with re-directed output. |
| 4078 | * |
| 4079 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 4080 | * over us; possibly making our ring_buffer_put() the last. |
| 4081 | */ |
| 4082 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4083 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4084 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4085 | } |
| 4086 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4087 | if (is_cgroup_event(event)) |
| 4088 | perf_detach_cgroup(event); |
| 4089 | |
Peter Zijlstra | a0733e6 | 2016-01-26 12:14:40 +0100 | [diff] [blame] | 4090 | if (!event->parent) { |
| 4091 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 4092 | put_callchain_buffers(); |
| 4093 | } |
| 4094 | |
| 4095 | perf_event_free_bpf_prog(event); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 4096 | perf_addr_filters_splice(event, NULL); |
| 4097 | kfree(event->addr_filters_offs); |
Peter Zijlstra | a0733e6 | 2016-01-26 12:14:40 +0100 | [diff] [blame] | 4098 | |
| 4099 | if (event->destroy) |
| 4100 | event->destroy(event); |
| 4101 | |
| 4102 | if (event->ctx) |
| 4103 | put_ctx(event->ctx); |
| 4104 | |
Prashant Bhole | b951ffb | 2018-04-09 19:03:46 +0900 | [diff] [blame] | 4105 | if (event->hw.target) |
| 4106 | put_task_struct(event->hw.target); |
| 4107 | |
Alexander Shishkin | 62a92c8 | 2016-06-07 15:44:15 +0300 | [diff] [blame] | 4108 | exclusive_event_destroy(event); |
| 4109 | module_put(event->pmu->module); |
Peter Zijlstra | a0733e6 | 2016-01-26 12:14:40 +0100 | [diff] [blame] | 4110 | |
| 4111 | call_rcu(&event->rcu_head, free_event_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4112 | } |
| 4113 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4114 | /* |
| 4115 | * Used to free events which have a known refcount of 1, such as in error paths |
| 4116 | * where the event isn't exposed yet and inherited events. |
| 4117 | */ |
| 4118 | static void free_event(struct perf_event *event) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 4119 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4120 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
| 4121 | "unexpected event refcount: %ld; ptr=%p\n", |
| 4122 | atomic_long_read(&event->refcount), event)) { |
| 4123 | /* leak to avoid use-after-free */ |
| 4124 | return; |
| 4125 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 4126 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4127 | _free_event(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 4128 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 4129 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 4130 | /* |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 4131 | * Remove user event from the owner task. |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 4132 | */ |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 4133 | static void perf_remove_from_owner(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 4134 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4135 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 4136 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4137 | rcu_read_lock(); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4138 | /* |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 4139 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
| 4140 | * observe !owner it means the list deletion is complete and we can |
| 4141 | * indeed free this event, otherwise we need to serialize on |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4142 | * owner->perf_event_mutex. |
| 4143 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 4144 | owner = lockless_dereference(event->owner); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4145 | if (owner) { |
| 4146 | /* |
| 4147 | * Since delayed_put_task_struct() also drops the last |
| 4148 | * task reference we can safely take a new reference |
| 4149 | * while holding the rcu_read_lock(). |
| 4150 | */ |
| 4151 | get_task_struct(owner); |
| 4152 | } |
| 4153 | rcu_read_unlock(); |
| 4154 | |
| 4155 | if (owner) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4156 | /* |
| 4157 | * If we're here through perf_event_exit_task() we're already |
| 4158 | * holding ctx->mutex which would be an inversion wrt. the |
| 4159 | * normal lock order. |
| 4160 | * |
| 4161 | * However we can safely take this lock because its the child |
| 4162 | * ctx->mutex. |
| 4163 | */ |
| 4164 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); |
| 4165 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4166 | /* |
| 4167 | * We have to re-check the event->owner field, if it is cleared |
| 4168 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 4169 | * ensured they're done, and we can proceed with freeing the |
| 4170 | * event. |
| 4171 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 4172 | if (event->owner) { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4173 | list_del_init(&event->owner_entry); |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 4174 | smp_store_release(&event->owner, NULL); |
| 4175 | } |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4176 | mutex_unlock(&owner->perf_event_mutex); |
| 4177 | put_task_struct(owner); |
| 4178 | } |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 4181 | static void put_event(struct perf_event *event) |
| 4182 | { |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 4183 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 4184 | return; |
| 4185 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4186 | _free_event(event); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 4187 | } |
| 4188 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4189 | /* |
| 4190 | * Kill an event dead; while event:refcount will preserve the event |
| 4191 | * object, it will not preserve its functionality. Once the last 'user' |
| 4192 | * gives up the object, we'll destroy the thing. |
| 4193 | */ |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4194 | int perf_event_release_kernel(struct perf_event *event) |
| 4195 | { |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 4196 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4197 | struct perf_event *child, *tmp; |
| 4198 | |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 4199 | /* |
| 4200 | * If we got here through err_file: fput(event_file); we will not have |
| 4201 | * attached to a context yet. |
| 4202 | */ |
| 4203 | if (!ctx) { |
| 4204 | WARN_ON_ONCE(event->attach_state & |
| 4205 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); |
| 4206 | goto no_ctx; |
| 4207 | } |
| 4208 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4209 | if (!is_kernel_event(event)) |
| 4210 | perf_remove_from_owner(event); |
| 4211 | |
Peter Zijlstra | 5fa7c8e | 2016-01-26 15:25:15 +0100 | [diff] [blame] | 4212 | ctx = perf_event_ctx_lock(event); |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4213 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4214 | perf_remove_from_context(event, DETACH_GROUP); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 4215 | |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4216 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 4217 | /* |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4218 | * Mark this even as STATE_DEAD, there is no external reference to it |
| 4219 | * anymore. |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4220 | * |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4221 | * Anybody acquiring event->child_mutex after the below loop _must_ |
| 4222 | * also see this, most importantly inherit_event() which will avoid |
| 4223 | * placing more children on the list. |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4224 | * |
| 4225 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
| 4226 | * child events. |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 4227 | */ |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4228 | event->state = PERF_EVENT_STATE_DEAD; |
| 4229 | raw_spin_unlock_irq(&ctx->lock); |
| 4230 | |
| 4231 | perf_event_ctx_unlock(event, ctx); |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 4232 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4233 | again: |
| 4234 | mutex_lock(&event->child_mutex); |
| 4235 | list_for_each_entry(child, &event->child_list, child_list) { |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 4236 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4237 | /* |
| 4238 | * Cannot change, child events are not migrated, see the |
| 4239 | * comment with perf_event_ctx_lock_nested(). |
| 4240 | */ |
| 4241 | ctx = lockless_dereference(child->ctx); |
| 4242 | /* |
| 4243 | * Since child_mutex nests inside ctx::mutex, we must jump |
| 4244 | * through hoops. We start by grabbing a reference on the ctx. |
| 4245 | * |
| 4246 | * Since the event cannot get freed while we hold the |
| 4247 | * child_mutex, the context must also exist and have a !0 |
| 4248 | * reference count. |
| 4249 | */ |
| 4250 | get_ctx(ctx); |
| 4251 | |
| 4252 | /* |
| 4253 | * Now that we have a ctx ref, we can drop child_mutex, and |
| 4254 | * acquire ctx::mutex without fear of it going away. Then we |
| 4255 | * can re-acquire child_mutex. |
| 4256 | */ |
| 4257 | mutex_unlock(&event->child_mutex); |
| 4258 | mutex_lock(&ctx->mutex); |
| 4259 | mutex_lock(&event->child_mutex); |
| 4260 | |
| 4261 | /* |
| 4262 | * Now that we hold ctx::mutex and child_mutex, revalidate our |
| 4263 | * state, if child is still the first entry, it didn't get freed |
| 4264 | * and we can continue doing so. |
| 4265 | */ |
| 4266 | tmp = list_first_entry_or_null(&event->child_list, |
| 4267 | struct perf_event, child_list); |
| 4268 | if (tmp == child) { |
| 4269 | perf_remove_from_context(child, DETACH_GROUP); |
| 4270 | list_del(&child->child_list); |
| 4271 | free_event(child); |
| 4272 | /* |
| 4273 | * This matches the refcount bump in inherit_event(); |
| 4274 | * this can't be the last reference. |
| 4275 | */ |
| 4276 | put_event(event); |
| 4277 | } |
| 4278 | |
| 4279 | mutex_unlock(&event->child_mutex); |
| 4280 | mutex_unlock(&ctx->mutex); |
| 4281 | put_ctx(ctx); |
| 4282 | goto again; |
| 4283 | } |
| 4284 | mutex_unlock(&event->child_mutex); |
| 4285 | |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 4286 | no_ctx: |
| 4287 | put_event(event); /* Must be the 'last' reference */ |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4288 | return 0; |
| 4289 | } |
| 4290 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 4291 | |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 4292 | /* |
| 4293 | * Called when the last reference to the file is gone. |
| 4294 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 4295 | static int perf_release(struct inode *inode, struct file *file) |
| 4296 | { |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4297 | perf_event_release_kernel(file->private_data); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 4298 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 4299 | } |
| 4300 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4301 | 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] | 4302 | { |
| 4303 | struct perf_event *child; |
| 4304 | u64 total = 0; |
| 4305 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4306 | *enabled = 0; |
| 4307 | *running = 0; |
| 4308 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 4309 | mutex_lock(&event->child_mutex); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 4310 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4311 | (void)perf_event_read(event, false); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 4312 | total += perf_event_count(event); |
| 4313 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4314 | *enabled += event->total_time_enabled + |
| 4315 | atomic64_read(&event->child_total_time_enabled); |
| 4316 | *running += event->total_time_running + |
| 4317 | atomic64_read(&event->child_total_time_running); |
| 4318 | |
| 4319 | list_for_each_entry(child, &event->child_list, child_list) { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4320 | (void)perf_event_read(child, false); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 4321 | total += perf_event_count(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4322 | *enabled += child->total_time_enabled; |
| 4323 | *running += child->total_time_running; |
| 4324 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 4325 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4326 | |
| 4327 | return total; |
| 4328 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 4329 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4330 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4331 | static int __perf_read_group_add(struct perf_event *leader, |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4332 | u64 read_format, u64 *values) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4333 | { |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4334 | struct perf_event *sub; |
| 4335 | int n = 1; /* skip @nr */ |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4336 | int ret; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4337 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4338 | ret = perf_event_read(leader, true); |
| 4339 | if (ret) |
| 4340 | return ret; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4341 | |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4342 | /* |
| 4343 | * Since we co-schedule groups, {enabled,running} times of siblings |
| 4344 | * will be identical to those of the leader, so we only publish one |
| 4345 | * set. |
| 4346 | */ |
| 4347 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
| 4348 | values[n++] += leader->total_time_enabled + |
| 4349 | atomic64_read(&leader->child_total_time_enabled); |
| 4350 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4351 | |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4352 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
| 4353 | values[n++] += leader->total_time_running + |
| 4354 | atomic64_read(&leader->child_total_time_running); |
| 4355 | } |
| 4356 | |
| 4357 | /* |
| 4358 | * Write {count,id} tuples for every sibling. |
| 4359 | */ |
| 4360 | values[n++] += perf_event_count(leader); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4361 | if (read_format & PERF_FORMAT_ID) |
| 4362 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4363 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4364 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4365 | values[n++] += perf_event_count(sub); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4366 | if (read_format & PERF_FORMAT_ID) |
| 4367 | values[n++] = primary_event_id(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4368 | } |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4369 | |
| 4370 | return 0; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | static int perf_read_group(struct perf_event *event, |
| 4374 | u64 read_format, char __user *buf) |
| 4375 | { |
| 4376 | struct perf_event *leader = event->group_leader, *child; |
| 4377 | struct perf_event_context *ctx = leader->ctx; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4378 | int ret; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4379 | u64 *values; |
| 4380 | |
| 4381 | lockdep_assert_held(&ctx->mutex); |
| 4382 | |
| 4383 | values = kzalloc(event->read_size, GFP_KERNEL); |
| 4384 | if (!values) |
| 4385 | return -ENOMEM; |
| 4386 | |
| 4387 | values[0] = 1 + leader->nr_siblings; |
| 4388 | |
| 4389 | /* |
| 4390 | * By locking the child_mutex of the leader we effectively |
| 4391 | * lock the child list of all siblings.. XXX explain how. |
| 4392 | */ |
| 4393 | mutex_lock(&leader->child_mutex); |
| 4394 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4395 | ret = __perf_read_group_add(leader, read_format, values); |
| 4396 | if (ret) |
| 4397 | goto unlock; |
| 4398 | |
| 4399 | list_for_each_entry(child, &leader->child_list, child_list) { |
| 4400 | ret = __perf_read_group_add(child, read_format, values); |
| 4401 | if (ret) |
| 4402 | goto unlock; |
| 4403 | } |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4404 | |
| 4405 | mutex_unlock(&leader->child_mutex); |
| 4406 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4407 | ret = event->read_size; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4408 | if (copy_to_user(buf, values, event->read_size)) |
| 4409 | ret = -EFAULT; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4410 | goto out; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4411 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4412 | unlock: |
| 4413 | mutex_unlock(&leader->child_mutex); |
| 4414 | out: |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4415 | kfree(values); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4416 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4417 | } |
| 4418 | |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4419 | static int perf_read_one(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4420 | u64 read_format, char __user *buf) |
| 4421 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4422 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4423 | u64 values[4]; |
| 4424 | int n = 0; |
| 4425 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4426 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 4427 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 4428 | values[n++] = enabled; |
| 4429 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4430 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4431 | if (read_format & PERF_FORMAT_ID) |
| 4432 | values[n++] = primary_event_id(event); |
| 4433 | |
| 4434 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 4435 | return -EFAULT; |
| 4436 | |
| 4437 | return n * sizeof(u64); |
| 4438 | } |
| 4439 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4440 | static bool is_event_hup(struct perf_event *event) |
| 4441 | { |
| 4442 | bool no_children; |
| 4443 | |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4444 | if (event->state > PERF_EVENT_STATE_EXIT) |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4445 | return false; |
| 4446 | |
| 4447 | mutex_lock(&event->child_mutex); |
| 4448 | no_children = list_empty(&event->child_list); |
| 4449 | mutex_unlock(&event->child_mutex); |
| 4450 | return no_children; |
| 4451 | } |
| 4452 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4453 | /* |
| 4454 | * Read the performance event - simple non blocking version for now |
| 4455 | */ |
| 4456 | static ssize_t |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4457 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4458 | { |
| 4459 | u64 read_format = event->attr.read_format; |
| 4460 | int ret; |
| 4461 | |
| 4462 | /* |
| 4463 | * Return end-of-file for a read on a event that is in |
| 4464 | * error state (i.e. because it was pinned but it couldn't be |
| 4465 | * scheduled on to the CPU at some point). |
| 4466 | */ |
| 4467 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 4468 | return 0; |
| 4469 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4470 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4471 | return -ENOSPC; |
| 4472 | |
| 4473 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4474 | if (read_format & PERF_FORMAT_GROUP) |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4475 | ret = perf_read_group(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4476 | else |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4477 | ret = perf_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4478 | |
| 4479 | return ret; |
| 4480 | } |
| 4481 | |
| 4482 | static ssize_t |
| 4483 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 4484 | { |
| 4485 | struct perf_event *event = file->private_data; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4486 | struct perf_event_context *ctx; |
| 4487 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4488 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4489 | ctx = perf_event_ctx_lock(event); |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4490 | ret = __perf_read(event, buf, count); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4491 | perf_event_ctx_unlock(event, ctx); |
| 4492 | |
| 4493 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4494 | } |
| 4495 | |
| 4496 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 4497 | { |
| 4498 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4499 | struct ring_buffer *rb; |
Jiri Olsa | 61b6768 | 2014-08-13 19:39:56 +0200 | [diff] [blame] | 4500 | unsigned int events = POLLHUP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4501 | |
Sebastian Andrzej Siewior | e708d7a | 2014-08-04 15:31:08 +0200 | [diff] [blame] | 4502 | poll_wait(file, &event->waitq, wait); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 4503 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4504 | if (is_event_hup(event)) |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 4505 | return events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4506 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4507 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4508 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 4509 | * 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] | 4510 | */ |
| 4511 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4512 | rb = event->rb; |
| 4513 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4514 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4515 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4516 | return events; |
| 4517 | } |
| 4518 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4519 | static void _perf_event_reset(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4520 | { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4521 | (void)perf_event_read(event, false); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4522 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4523 | perf_event_update_userpage(event); |
| 4524 | } |
| 4525 | |
| 4526 | /* |
| 4527 | * Holding the top-level event's child_mutex means that any |
| 4528 | * descendant process that has inherited this event will block |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 4529 | * 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] | 4530 | * task existence requirements of perf_event_enable/disable. |
| 4531 | */ |
| 4532 | static void perf_event_for_each_child(struct perf_event *event, |
| 4533 | void (*func)(struct perf_event *)) |
| 4534 | { |
| 4535 | struct perf_event *child; |
| 4536 | |
| 4537 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4538 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4539 | mutex_lock(&event->child_mutex); |
| 4540 | func(event); |
| 4541 | list_for_each_entry(child, &event->child_list, child_list) |
| 4542 | func(child); |
| 4543 | mutex_unlock(&event->child_mutex); |
| 4544 | } |
| 4545 | |
| 4546 | static void perf_event_for_each(struct perf_event *event, |
| 4547 | void (*func)(struct perf_event *)) |
| 4548 | { |
| 4549 | struct perf_event_context *ctx = event->ctx; |
| 4550 | struct perf_event *sibling; |
| 4551 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4552 | lockdep_assert_held(&ctx->mutex); |
| 4553 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4554 | event = event->group_leader; |
| 4555 | |
| 4556 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4557 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 4558 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4559 | } |
| 4560 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4561 | static void __perf_event_period(struct perf_event *event, |
| 4562 | struct perf_cpu_context *cpuctx, |
| 4563 | struct perf_event_context *ctx, |
| 4564 | void *info) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 4565 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4566 | u64 value = *((u64 *)info); |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4567 | bool active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4568 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4569 | if (event->attr.freq) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4570 | event->attr.sample_freq = value; |
| 4571 | } else { |
| 4572 | event->attr.sample_period = value; |
| 4573 | event->hw.sample_period = value; |
| 4574 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 4575 | |
| 4576 | active = (event->state == PERF_EVENT_STATE_ACTIVE); |
| 4577 | if (active) { |
| 4578 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | 1e02cd4 | 2016-03-10 15:39:24 +0100 | [diff] [blame] | 4579 | /* |
| 4580 | * We could be throttled; unthrottle now to avoid the tick |
| 4581 | * trying to unthrottle while we already re-started the event. |
| 4582 | */ |
| 4583 | if (event->hw.interrupts == MAX_INTERRUPTS) { |
| 4584 | event->hw.interrupts = 0; |
| 4585 | perf_log_throttle(event, 1); |
| 4586 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 4587 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 4588 | } |
| 4589 | |
| 4590 | local64_set(&event->hw.period_left, 0); |
| 4591 | |
| 4592 | if (active) { |
| 4593 | event->pmu->start(event, PERF_EF_RELOAD); |
| 4594 | perf_pmu_enable(ctx->pmu); |
| 4595 | } |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 4599 | { |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4600 | u64 value; |
| 4601 | |
| 4602 | if (!is_sampling_event(event)) |
| 4603 | return -EINVAL; |
| 4604 | |
| 4605 | if (copy_from_user(&value, arg, sizeof(value))) |
| 4606 | return -EFAULT; |
| 4607 | |
| 4608 | if (!value) |
| 4609 | return -EINVAL; |
| 4610 | |
| 4611 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) |
| 4612 | return -EINVAL; |
| 4613 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4614 | event_function_call(event, __perf_event_period, &value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4615 | |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4616 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4617 | } |
| 4618 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4619 | static const struct file_operations perf_fops; |
| 4620 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4621 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4622 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4623 | struct fd f = fdget(fd); |
| 4624 | if (!f.file) |
| 4625 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4626 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4627 | if (f.file->f_op != &perf_fops) { |
| 4628 | fdput(f); |
| 4629 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4630 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4631 | *p = f; |
| 4632 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | static int perf_event_set_output(struct perf_event *event, |
| 4636 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4637 | 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] | 4638 | 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] | 4639 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4640 | 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] | 4641 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4642 | void (*func)(struct perf_event *); |
| 4643 | u32 flags = arg; |
| 4644 | |
| 4645 | switch (cmd) { |
| 4646 | case PERF_EVENT_IOC_ENABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4647 | func = _perf_event_enable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4648 | break; |
| 4649 | case PERF_EVENT_IOC_DISABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4650 | func = _perf_event_disable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4651 | break; |
| 4652 | case PERF_EVENT_IOC_RESET: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4653 | func = _perf_event_reset; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4654 | break; |
| 4655 | |
| 4656 | case PERF_EVENT_IOC_REFRESH: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4657 | return _perf_event_refresh(event, arg); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4658 | |
| 4659 | case PERF_EVENT_IOC_PERIOD: |
| 4660 | return perf_event_period(event, (u64 __user *)arg); |
| 4661 | |
Jiri Olsa | cf4957f | 2012-10-24 13:37:58 +0200 | [diff] [blame] | 4662 | case PERF_EVENT_IOC_ID: |
| 4663 | { |
| 4664 | u64 id = primary_event_id(event); |
| 4665 | |
| 4666 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) |
| 4667 | return -EFAULT; |
| 4668 | return 0; |
| 4669 | } |
| 4670 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4671 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4672 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4673 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4674 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4675 | struct perf_event *output_event; |
| 4676 | struct fd output; |
| 4677 | ret = perf_fget_light(arg, &output); |
| 4678 | if (ret) |
| 4679 | return ret; |
| 4680 | output_event = output.file->private_data; |
| 4681 | ret = perf_event_set_output(event, output_event); |
| 4682 | fdput(output); |
| 4683 | } else { |
| 4684 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4685 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4686 | return ret; |
| 4687 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4688 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4689 | case PERF_EVENT_IOC_SET_FILTER: |
| 4690 | return perf_event_set_filter(event, (void __user *)arg); |
| 4691 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 4692 | case PERF_EVENT_IOC_SET_BPF: |
| 4693 | return perf_event_set_bpf_prog(event, arg); |
| 4694 | |
Wang Nan | 86e7972 | 2016-03-28 06:41:29 +0000 | [diff] [blame] | 4695 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
| 4696 | struct ring_buffer *rb; |
| 4697 | |
| 4698 | rcu_read_lock(); |
| 4699 | rb = rcu_dereference(event->rb); |
| 4700 | if (!rb || !rb->nr_pages) { |
| 4701 | rcu_read_unlock(); |
| 4702 | return -EINVAL; |
| 4703 | } |
| 4704 | rb_toggle_paused(rb, !!arg); |
| 4705 | rcu_read_unlock(); |
| 4706 | return 0; |
| 4707 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4708 | default: |
| 4709 | return -ENOTTY; |
| 4710 | } |
| 4711 | |
| 4712 | if (flags & PERF_IOC_FLAG_GROUP) |
| 4713 | perf_event_for_each(event, func); |
| 4714 | else |
| 4715 | perf_event_for_each_child(event, func); |
| 4716 | |
| 4717 | return 0; |
| 4718 | } |
| 4719 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4720 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 4721 | { |
| 4722 | struct perf_event *event = file->private_data; |
| 4723 | struct perf_event_context *ctx; |
| 4724 | long ret; |
| 4725 | |
| 4726 | ctx = perf_event_ctx_lock(event); |
| 4727 | ret = _perf_ioctl(event, cmd, arg); |
| 4728 | perf_event_ctx_unlock(event, ctx); |
| 4729 | |
| 4730 | return ret; |
| 4731 | } |
| 4732 | |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4733 | #ifdef CONFIG_COMPAT |
| 4734 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, |
| 4735 | unsigned long arg) |
| 4736 | { |
| 4737 | switch (_IOC_NR(cmd)) { |
| 4738 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): |
| 4739 | case _IOC_NR(PERF_EVENT_IOC_ID): |
| 4740 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
| 4741 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { |
| 4742 | cmd &= ~IOCSIZE_MASK; |
| 4743 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; |
| 4744 | } |
| 4745 | break; |
| 4746 | } |
| 4747 | return perf_ioctl(file, cmd, arg); |
| 4748 | } |
| 4749 | #else |
| 4750 | # define perf_compat_ioctl NULL |
| 4751 | #endif |
| 4752 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4753 | int perf_event_task_enable(void) |
| 4754 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4755 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4756 | struct perf_event *event; |
| 4757 | |
| 4758 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4759 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4760 | ctx = perf_event_ctx_lock(event); |
| 4761 | perf_event_for_each_child(event, _perf_event_enable); |
| 4762 | perf_event_ctx_unlock(event, ctx); |
| 4763 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4764 | mutex_unlock(¤t->perf_event_mutex); |
| 4765 | |
| 4766 | return 0; |
| 4767 | } |
| 4768 | |
| 4769 | int perf_event_task_disable(void) |
| 4770 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4771 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4772 | struct perf_event *event; |
| 4773 | |
| 4774 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4775 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4776 | ctx = perf_event_ctx_lock(event); |
| 4777 | perf_event_for_each_child(event, _perf_event_disable); |
| 4778 | perf_event_ctx_unlock(event, ctx); |
| 4779 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4780 | mutex_unlock(¤t->perf_event_mutex); |
| 4781 | |
| 4782 | return 0; |
| 4783 | } |
| 4784 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4785 | static int perf_event_index(struct perf_event *event) |
| 4786 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 4787 | if (event->hw.state & PERF_HES_STOPPED) |
| 4788 | return 0; |
| 4789 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4790 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 4791 | return 0; |
| 4792 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 4793 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4794 | } |
| 4795 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4796 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4797 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4798 | u64 *enabled, |
| 4799 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4800 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4801 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4802 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4803 | *now = perf_clock(); |
| 4804 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4805 | *enabled = ctx_time - event->tstamp_enabled; |
| 4806 | *running = ctx_time - event->tstamp_running; |
| 4807 | } |
| 4808 | |
Peter Zijlstra | fa73158 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4809 | static void perf_event_init_userpage(struct perf_event *event) |
| 4810 | { |
| 4811 | struct perf_event_mmap_page *userpg; |
| 4812 | struct ring_buffer *rb; |
| 4813 | |
| 4814 | rcu_read_lock(); |
| 4815 | rb = rcu_dereference(event->rb); |
| 4816 | if (!rb) |
| 4817 | goto unlock; |
| 4818 | |
| 4819 | userpg = rb->user_page; |
| 4820 | |
| 4821 | /* Allow new userspace to detect that bit 0 is deprecated */ |
| 4822 | userpg->cap_bit0_is_deprecated = 1; |
| 4823 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); |
Alexander Shishkin | e8c6dea | 2015-01-14 14:18:10 +0200 | [diff] [blame] | 4824 | userpg->data_offset = PAGE_SIZE; |
| 4825 | userpg->data_size = perf_data_size(rb); |
Peter Zijlstra | fa73158 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4826 | |
| 4827 | unlock: |
| 4828 | rcu_read_unlock(); |
| 4829 | } |
| 4830 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4831 | void __weak arch_perf_update_userpage( |
| 4832 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4833 | { |
| 4834 | } |
| 4835 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4836 | /* |
| 4837 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 4838 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 4839 | * code calls this from NMI context. |
| 4840 | */ |
| 4841 | void perf_event_update_userpage(struct perf_event *event) |
| 4842 | { |
| 4843 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4844 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4845 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4846 | |
| 4847 | rcu_read_lock(); |
Peter Zijlstra | 5ec4c59 | 2013-08-02 21:16:30 +0200 | [diff] [blame] | 4848 | rb = rcu_dereference(event->rb); |
| 4849 | if (!rb) |
| 4850 | goto unlock; |
| 4851 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4852 | /* |
| 4853 | * compute total_time_enabled, total_time_running |
| 4854 | * based on snapshot values taken when the event |
| 4855 | * was last scheduled in. |
| 4856 | * |
| 4857 | * we cannot simply called update_context_time() |
| 4858 | * because of locking issue as we can be called in |
| 4859 | * NMI context |
| 4860 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4861 | calc_timer_values(event, &now, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4862 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4863 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4864 | /* |
| 4865 | * Disable preemption so as to not let the corresponding user-space |
| 4866 | * spin too long if we get preempted. |
| 4867 | */ |
| 4868 | preempt_disable(); |
| 4869 | ++userpg->lock; |
| 4870 | barrier(); |
| 4871 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4872 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 4873 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4874 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4875 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4876 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4877 | atomic64_read(&event->child_total_time_enabled); |
| 4878 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4879 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4880 | atomic64_read(&event->child_total_time_running); |
| 4881 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4882 | arch_perf_update_userpage(event, userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4883 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4884 | barrier(); |
| 4885 | ++userpg->lock; |
| 4886 | preempt_enable(); |
| 4887 | unlock: |
| 4888 | rcu_read_unlock(); |
| 4889 | } |
| 4890 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4891 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 4892 | { |
| 4893 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4894 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4895 | int ret = VM_FAULT_SIGBUS; |
| 4896 | |
| 4897 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 4898 | if (vmf->pgoff == 0) |
| 4899 | ret = 0; |
| 4900 | return ret; |
| 4901 | } |
| 4902 | |
| 4903 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4904 | rb = rcu_dereference(event->rb); |
| 4905 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4906 | goto unlock; |
| 4907 | |
| 4908 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 4909 | goto unlock; |
| 4910 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4911 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4912 | if (!vmf->page) |
| 4913 | goto unlock; |
| 4914 | |
| 4915 | get_page(vmf->page); |
| 4916 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 4917 | vmf->page->index = vmf->pgoff; |
| 4918 | |
| 4919 | ret = 0; |
| 4920 | unlock: |
| 4921 | rcu_read_unlock(); |
| 4922 | |
| 4923 | return ret; |
| 4924 | } |
| 4925 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4926 | static void ring_buffer_attach(struct perf_event *event, |
| 4927 | struct ring_buffer *rb) |
| 4928 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4929 | struct ring_buffer *old_rb = NULL; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4930 | unsigned long flags; |
| 4931 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4932 | if (event->rb) { |
| 4933 | /* |
| 4934 | * Should be impossible, we set this when removing |
| 4935 | * event->rb_entry and wait/clear when adding event->rb_entry. |
| 4936 | */ |
| 4937 | WARN_ON_ONCE(event->rcu_pending); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4938 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4939 | old_rb = event->rb; |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4940 | spin_lock_irqsave(&old_rb->event_lock, flags); |
| 4941 | list_del_rcu(&event->rb_entry); |
| 4942 | spin_unlock_irqrestore(&old_rb->event_lock, flags); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4943 | |
Oleg Nesterov | 2f993cf | 2015-05-30 22:04:25 +0200 | [diff] [blame] | 4944 | event->rcu_batches = get_state_synchronize_rcu(); |
| 4945 | event->rcu_pending = 1; |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4946 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4947 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4948 | if (rb) { |
Oleg Nesterov | 2f993cf | 2015-05-30 22:04:25 +0200 | [diff] [blame] | 4949 | if (event->rcu_pending) { |
| 4950 | cond_synchronize_rcu(event->rcu_batches); |
| 4951 | event->rcu_pending = 0; |
| 4952 | } |
| 4953 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4954 | spin_lock_irqsave(&rb->event_lock, flags); |
| 4955 | list_add_rcu(&event->rb_entry, &rb->event_list); |
| 4956 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 4957 | } |
| 4958 | |
Alexander Shishkin | 767ae08 | 2016-09-06 16:23:49 +0300 | [diff] [blame] | 4959 | /* |
| 4960 | * Avoid racing with perf_mmap_close(AUX): stop the event |
| 4961 | * before swizzling the event::rb pointer; if it's getting |
| 4962 | * unmapped, its aux_mmap_count will be 0 and it won't |
| 4963 | * restart. See the comment in __perf_pmu_output_stop(). |
| 4964 | * |
| 4965 | * Data will inevitably be lost when set_output is done in |
| 4966 | * mid-air, but then again, whoever does it like this is |
| 4967 | * not in for the data anyway. |
| 4968 | */ |
| 4969 | if (has_aux(event)) |
| 4970 | perf_event_stop(event, 0); |
| 4971 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4972 | rcu_assign_pointer(event->rb, rb); |
| 4973 | |
| 4974 | if (old_rb) { |
| 4975 | ring_buffer_put(old_rb); |
| 4976 | /* |
| 4977 | * Since we detached before setting the new rb, so that we |
| 4978 | * could attach the new rb, we could have missed a wakeup. |
| 4979 | * Provide it now. |
| 4980 | */ |
| 4981 | wake_up_all(&event->waitq); |
| 4982 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4983 | } |
| 4984 | |
| 4985 | static void ring_buffer_wakeup(struct perf_event *event) |
| 4986 | { |
| 4987 | struct ring_buffer *rb; |
| 4988 | |
| 4989 | rcu_read_lock(); |
| 4990 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4991 | if (rb) { |
| 4992 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 4993 | wake_up_all(&event->waitq); |
| 4994 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4995 | rcu_read_unlock(); |
| 4996 | } |
| 4997 | |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 4998 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4999 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5000 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5001 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5002 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5003 | rb = rcu_dereference(event->rb); |
| 5004 | if (rb) { |
| 5005 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 5006 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5007 | } |
| 5008 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5009 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5010 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5011 | } |
| 5012 | |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 5013 | void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5014 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5015 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5016 | return; |
| 5017 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5018 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 5019 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5020 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5021 | } |
| 5022 | |
| 5023 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 5024 | { |
| 5025 | struct perf_event *event = vma->vm_file->private_data; |
| 5026 | |
| 5027 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5028 | atomic_inc(&event->rb->mmap_count); |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 5029 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5030 | if (vma->vm_pgoff) |
| 5031 | atomic_inc(&event->rb->aux_mmap_count); |
| 5032 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 5033 | if (event->pmu->event_mapped) |
| 5034 | event->pmu->event_mapped(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5035 | } |
| 5036 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 5037 | static void perf_pmu_output_stop(struct perf_event *event); |
| 5038 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5039 | /* |
| 5040 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 5041 | * event, or through other events by use of perf_event_set_output(). |
| 5042 | * |
| 5043 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 5044 | * the buffer here, where we still have a VM context. This means we need |
| 5045 | * to detach all events redirecting to us. |
| 5046 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5047 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 5048 | { |
| 5049 | struct perf_event *event = vma->vm_file->private_data; |
| 5050 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 5051 | struct ring_buffer *rb = ring_buffer_get(event); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5052 | struct user_struct *mmap_user = rb->mmap_user; |
| 5053 | int mmap_locked = rb->mmap_locked; |
| 5054 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5055 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 5056 | if (event->pmu->event_unmapped) |
| 5057 | event->pmu->event_unmapped(event); |
| 5058 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5059 | /* |
| 5060 | * rb->aux_mmap_count will always drop before rb->mmap_count and |
| 5061 | * event->mmap_count, so it is ok to use event->mmap_mutex to |
| 5062 | * serialize with perf_mmap here. |
| 5063 | */ |
| 5064 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && |
| 5065 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 5066 | /* |
| 5067 | * Stop all AUX events that are writing to this buffer, |
| 5068 | * so that we can free its AUX pages and corresponding PMU |
| 5069 | * data. Note that after rb::aux_mmap_count dropped to zero, |
| 5070 | * they won't start any more (see perf_aux_output_begin()). |
| 5071 | */ |
| 5072 | perf_pmu_output_stop(event); |
| 5073 | |
| 5074 | /* now it's safe to free the pages */ |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5075 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
| 5076 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; |
| 5077 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 5078 | /* this has to be the last one */ |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5079 | rb_free_aux(rb); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 5080 | WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); |
| 5081 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5082 | mutex_unlock(&event->mmap_mutex); |
| 5083 | } |
| 5084 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5085 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5086 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5087 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 5088 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5089 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 5090 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5091 | mutex_unlock(&event->mmap_mutex); |
| 5092 | |
| 5093 | /* 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] | 5094 | if (atomic_read(&rb->mmap_count)) |
| 5095 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5096 | |
| 5097 | /* |
| 5098 | * No other mmap()s, detach from all other events that might redirect |
| 5099 | * into the now unreachable buffer. Somewhat complicated by the |
| 5100 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 5101 | */ |
| 5102 | again: |
| 5103 | rcu_read_lock(); |
| 5104 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 5105 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 5106 | /* |
| 5107 | * This event is en-route to free_event() which will |
| 5108 | * detach it and remove it from the list. |
| 5109 | */ |
| 5110 | continue; |
| 5111 | } |
| 5112 | rcu_read_unlock(); |
| 5113 | |
| 5114 | mutex_lock(&event->mmap_mutex); |
| 5115 | /* |
| 5116 | * Check we didn't race with perf_event_set_output() which can |
| 5117 | * swizzle the rb from under us while we were waiting to |
| 5118 | * acquire mmap_mutex. |
| 5119 | * |
| 5120 | * If we find a different rb; ignore this event, a next |
| 5121 | * iteration will no longer find it on the list. We have to |
| 5122 | * still restart the iteration to make sure we're not now |
| 5123 | * iterating the wrong list. |
| 5124 | */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 5125 | if (event->rb == rb) |
| 5126 | ring_buffer_attach(event, NULL); |
| 5127 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5128 | mutex_unlock(&event->mmap_mutex); |
| 5129 | put_event(event); |
| 5130 | |
| 5131 | /* |
| 5132 | * Restart the iteration; either we're on the wrong list or |
| 5133 | * destroyed its integrity by doing a deletion. |
| 5134 | */ |
| 5135 | goto again; |
| 5136 | } |
| 5137 | rcu_read_unlock(); |
| 5138 | |
| 5139 | /* |
| 5140 | * It could be there's still a few 0-ref events on the list; they'll |
| 5141 | * get cleaned up by free_event() -- they'll also still have their |
| 5142 | * ref on the rb and will free it whenever they are done with it. |
| 5143 | * |
| 5144 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 5145 | * undo the VM accounting. |
| 5146 | */ |
| 5147 | |
| 5148 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 5149 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 5150 | free_uid(mmap_user); |
| 5151 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 5152 | out_put: |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5153 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5154 | } |
| 5155 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 5156 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5157 | .open = perf_mmap_open, |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5158 | .close = perf_mmap_close, /* non mergable */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5159 | .fault = perf_mmap_fault, |
| 5160 | .page_mkwrite = perf_mmap_fault, |
| 5161 | }; |
| 5162 | |
| 5163 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 5164 | { |
| 5165 | struct perf_event *event = file->private_data; |
| 5166 | unsigned long user_locked, user_lock_limit; |
| 5167 | struct user_struct *user = current_user(); |
| 5168 | unsigned long locked, lock_limit; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5169 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5170 | unsigned long vma_size; |
| 5171 | unsigned long nr_pages; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5172 | long user_extra = 0, extra = 0; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 5173 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5174 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 5175 | /* |
| 5176 | * Don't allow mmap() of inherited per-task counters. This would |
| 5177 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5178 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 5179 | */ |
| 5180 | if (event->cpu == -1 && event->attr.inherit) |
| 5181 | return -EINVAL; |
| 5182 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5183 | if (!(vma->vm_flags & VM_SHARED)) |
| 5184 | return -EINVAL; |
| 5185 | |
| 5186 | vma_size = vma->vm_end - vma->vm_start; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5187 | |
| 5188 | if (vma->vm_pgoff == 0) { |
| 5189 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 5190 | } else { |
| 5191 | /* |
| 5192 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already |
| 5193 | * mapped, all subsequent mappings should have the same size |
| 5194 | * and offset. Must be above the normal perf buffer. |
| 5195 | */ |
| 5196 | u64 aux_offset, aux_size; |
| 5197 | |
| 5198 | if (!event->rb) |
| 5199 | return -EINVAL; |
| 5200 | |
| 5201 | nr_pages = vma_size / PAGE_SIZE; |
| 5202 | |
| 5203 | mutex_lock(&event->mmap_mutex); |
| 5204 | ret = -EINVAL; |
| 5205 | |
| 5206 | rb = event->rb; |
| 5207 | if (!rb) |
| 5208 | goto aux_unlock; |
| 5209 | |
| 5210 | aux_offset = ACCESS_ONCE(rb->user_page->aux_offset); |
| 5211 | aux_size = ACCESS_ONCE(rb->user_page->aux_size); |
| 5212 | |
| 5213 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) |
| 5214 | goto aux_unlock; |
| 5215 | |
| 5216 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) |
| 5217 | goto aux_unlock; |
| 5218 | |
| 5219 | /* already mapped with a different offset */ |
| 5220 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) |
| 5221 | goto aux_unlock; |
| 5222 | |
| 5223 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) |
| 5224 | goto aux_unlock; |
| 5225 | |
| 5226 | /* already mapped with a different size */ |
| 5227 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) |
| 5228 | goto aux_unlock; |
| 5229 | |
| 5230 | if (!is_power_of_2(nr_pages)) |
| 5231 | goto aux_unlock; |
| 5232 | |
| 5233 | if (!atomic_inc_not_zero(&rb->mmap_count)) |
| 5234 | goto aux_unlock; |
| 5235 | |
| 5236 | if (rb_has_aux(rb)) { |
| 5237 | atomic_inc(&rb->aux_mmap_count); |
| 5238 | ret = 0; |
| 5239 | goto unlock; |
| 5240 | } |
| 5241 | |
| 5242 | atomic_set(&rb->aux_mmap_count, 1); |
| 5243 | user_extra = nr_pages; |
| 5244 | |
| 5245 | goto accounting; |
| 5246 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5247 | |
| 5248 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5249 | * 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] | 5250 | * can do bitmasks instead of modulo. |
| 5251 | */ |
Kan Liang | 2ed1131 | 2015-03-02 02:14:26 -0500 | [diff] [blame] | 5252 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5253 | return -EINVAL; |
| 5254 | |
| 5255 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 5256 | return -EINVAL; |
| 5257 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5258 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5259 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5260 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5261 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5262 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5263 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5264 | goto unlock; |
| 5265 | } |
| 5266 | |
| 5267 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 5268 | /* |
| 5269 | * Raced against perf_mmap_close() through |
| 5270 | * perf_event_set_output(). Try again, hope for better |
| 5271 | * luck. |
| 5272 | */ |
| 5273 | mutex_unlock(&event->mmap_mutex); |
| 5274 | goto again; |
| 5275 | } |
| 5276 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5277 | goto unlock; |
| 5278 | } |
| 5279 | |
| 5280 | user_extra = nr_pages + 1; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5281 | |
| 5282 | accounting: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5283 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 5284 | |
| 5285 | /* |
| 5286 | * Increase the limit linearly with more CPUs: |
| 5287 | */ |
| 5288 | user_lock_limit *= num_online_cpus(); |
| 5289 | |
| 5290 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 5291 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5292 | if (user_locked > user_lock_limit) |
| 5293 | extra = user_locked - user_lock_limit; |
| 5294 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 5295 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5296 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 5297 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5298 | |
| 5299 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 5300 | !capable(CAP_IPC_LOCK)) { |
| 5301 | ret = -EPERM; |
| 5302 | goto unlock; |
| 5303 | } |
| 5304 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5305 | WARN_ON(!rb && event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 5306 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 5307 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5308 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 5309 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5310 | if (!rb) { |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5311 | rb = rb_alloc(nr_pages, |
| 5312 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 5313 | event->cpu, flags); |
| 5314 | |
| 5315 | if (!rb) { |
| 5316 | ret = -ENOMEM; |
| 5317 | goto unlock; |
| 5318 | } |
| 5319 | |
| 5320 | atomic_set(&rb->mmap_count, 1); |
| 5321 | rb->mmap_user = get_current_user(); |
| 5322 | rb->mmap_locked = extra; |
| 5323 | |
| 5324 | ring_buffer_attach(event, rb); |
| 5325 | |
| 5326 | perf_event_init_userpage(event); |
| 5327 | perf_event_update_userpage(event); |
| 5328 | } else { |
Alexander Shishkin | 1a59413 | 2015-01-14 14:18:18 +0200 | [diff] [blame] | 5329 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
| 5330 | event->attr.aux_watermark, flags); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5331 | if (!ret) |
| 5332 | rb->aux_mmap_locked = extra; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5333 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 5334 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5335 | unlock: |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5336 | if (!ret) { |
| 5337 | atomic_long_add(user_extra, &user->locked_vm); |
| 5338 | vma->vm_mm->pinned_vm += extra; |
| 5339 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5340 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5341 | } else if (rb) { |
| 5342 | atomic_dec(&rb->mmap_count); |
| 5343 | } |
| 5344 | aux_unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5345 | mutex_unlock(&event->mmap_mutex); |
| 5346 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5347 | /* |
| 5348 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 5349 | * vma. |
| 5350 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 5351 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5352 | vma->vm_ops = &perf_mmap_vmops; |
| 5353 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 5354 | if (event->pmu->event_mapped) |
| 5355 | event->pmu->event_mapped(event); |
| 5356 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5357 | return ret; |
| 5358 | } |
| 5359 | |
| 5360 | static int perf_fasync(int fd, struct file *filp, int on) |
| 5361 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5362 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5363 | struct perf_event *event = filp->private_data; |
| 5364 | int retval; |
| 5365 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5366 | inode_lock(inode); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5367 | retval = fasync_helper(fd, filp, on, &event->fasync); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5368 | inode_unlock(inode); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5369 | |
| 5370 | if (retval < 0) |
| 5371 | return retval; |
| 5372 | |
| 5373 | return 0; |
| 5374 | } |
| 5375 | |
| 5376 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 5377 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5378 | .release = perf_release, |
| 5379 | .read = perf_read, |
| 5380 | .poll = perf_poll, |
| 5381 | .unlocked_ioctl = perf_ioctl, |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 5382 | .compat_ioctl = perf_compat_ioctl, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5383 | .mmap = perf_mmap, |
| 5384 | .fasync = perf_fasync, |
| 5385 | }; |
| 5386 | |
| 5387 | /* |
| 5388 | * Perf event wakeup |
| 5389 | * |
| 5390 | * If there's data, ensure we set the poll() state and publish everything |
| 5391 | * to user-space before waking everybody up. |
| 5392 | */ |
| 5393 | |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 5394 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
| 5395 | { |
| 5396 | /* only the parent has fasync state */ |
| 5397 | if (event->parent) |
| 5398 | event = event->parent; |
| 5399 | return &event->fasync; |
| 5400 | } |
| 5401 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5402 | void perf_event_wakeup(struct perf_event *event) |
| 5403 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 5404 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5405 | |
| 5406 | if (event->pending_kill) { |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 5407 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5408 | event->pending_kill = 0; |
| 5409 | } |
| 5410 | } |
| 5411 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 5412 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5413 | { |
| 5414 | struct perf_event *event = container_of(entry, |
| 5415 | struct perf_event, pending); |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 5416 | int rctx; |
| 5417 | |
| 5418 | rctx = perf_swevent_get_recursion_context(); |
| 5419 | /* |
| 5420 | * If we 'fail' here, that's OK, it means recursion is already disabled |
| 5421 | * and we won't recurse 'further'. |
| 5422 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5423 | |
| 5424 | if (event->pending_disable) { |
| 5425 | event->pending_disable = 0; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 5426 | perf_event_disable_local(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | if (event->pending_wakeup) { |
| 5430 | event->pending_wakeup = 0; |
| 5431 | perf_event_wakeup(event); |
| 5432 | } |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 5433 | |
| 5434 | if (rctx >= 0) |
| 5435 | perf_swevent_put_recursion_context(rctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5436 | } |
| 5437 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5438 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5439 | * We assume there is only KVM supporting the callbacks. |
| 5440 | * Later on, we might change it to a list if there is |
| 5441 | * another virtualization implementation supporting the callbacks. |
| 5442 | */ |
| 5443 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 5444 | |
| 5445 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 5446 | { |
| 5447 | perf_guest_cbs = cbs; |
| 5448 | return 0; |
| 5449 | } |
| 5450 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 5451 | |
| 5452 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 5453 | { |
| 5454 | perf_guest_cbs = NULL; |
| 5455 | return 0; |
| 5456 | } |
| 5457 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 5458 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5459 | static void |
| 5460 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 5461 | struct pt_regs *regs, u64 mask) |
| 5462 | { |
| 5463 | int bit; |
Madhavan Srinivasan | 29dd328 | 2016-08-17 15:06:08 +0530 | [diff] [blame] | 5464 | DECLARE_BITMAP(_mask, 64); |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5465 | |
Madhavan Srinivasan | 29dd328 | 2016-08-17 15:06:08 +0530 | [diff] [blame] | 5466 | bitmap_from_u64(_mask, mask); |
| 5467 | for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5468 | u64 val; |
| 5469 | |
| 5470 | val = perf_reg_value(regs, bit); |
| 5471 | perf_output_put(handle, val); |
| 5472 | } |
| 5473 | } |
| 5474 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5475 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5476 | struct pt_regs *regs, |
| 5477 | struct pt_regs *regs_user_copy) |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5478 | { |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5479 | if (user_mode(regs)) { |
| 5480 | regs_user->abi = perf_reg_abi(current); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5481 | regs_user->regs = regs; |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5482 | } else if (current->mm) { |
| 5483 | perf_get_regs_user(regs_user, regs, regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5484 | } else { |
| 5485 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; |
| 5486 | regs_user->regs = NULL; |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5487 | } |
| 5488 | } |
| 5489 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5490 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
| 5491 | struct pt_regs *regs) |
| 5492 | { |
| 5493 | regs_intr->regs = regs; |
| 5494 | regs_intr->abi = perf_reg_abi(current); |
| 5495 | } |
| 5496 | |
| 5497 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5498 | /* |
| 5499 | * Get remaining task size from user stack pointer. |
| 5500 | * |
| 5501 | * It'd be better to take stack vma map and limit this more |
| 5502 | * precisly, but there's no way to get it safely under interrupt, |
| 5503 | * so using TASK_SIZE as limit. |
| 5504 | */ |
| 5505 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 5506 | { |
| 5507 | unsigned long addr = perf_user_stack_pointer(regs); |
| 5508 | |
| 5509 | if (!addr || addr >= TASK_SIZE) |
| 5510 | return 0; |
| 5511 | |
| 5512 | return TASK_SIZE - addr; |
| 5513 | } |
| 5514 | |
| 5515 | static u16 |
| 5516 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 5517 | struct pt_regs *regs) |
| 5518 | { |
| 5519 | u64 task_size; |
| 5520 | |
| 5521 | /* No regs, no stack pointer, no dump. */ |
| 5522 | if (!regs) |
| 5523 | return 0; |
| 5524 | |
| 5525 | /* |
| 5526 | * Check if we fit in with the requested stack size into the: |
| 5527 | * - TASK_SIZE |
| 5528 | * If we don't, we limit the size to the TASK_SIZE. |
| 5529 | * |
| 5530 | * - remaining sample size |
| 5531 | * If we don't, we customize the stack size to |
| 5532 | * fit in to the remaining sample size. |
| 5533 | */ |
| 5534 | |
| 5535 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 5536 | stack_size = min(stack_size, (u16) task_size); |
| 5537 | |
| 5538 | /* Current header size plus static size and dynamic size. */ |
| 5539 | header_size += 2 * sizeof(u64); |
| 5540 | |
| 5541 | /* Do we fit in with the current stack dump size? */ |
| 5542 | if ((u16) (header_size + stack_size) < header_size) { |
| 5543 | /* |
| 5544 | * If we overflow the maximum size for the sample, |
| 5545 | * we customize the stack dump size to fit in. |
| 5546 | */ |
| 5547 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 5548 | stack_size = round_up(stack_size, sizeof(u64)); |
| 5549 | } |
| 5550 | |
| 5551 | return stack_size; |
| 5552 | } |
| 5553 | |
| 5554 | static void |
| 5555 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 5556 | struct pt_regs *regs) |
| 5557 | { |
| 5558 | /* Case of a kernel thread, nothing to dump */ |
| 5559 | if (!regs) { |
| 5560 | u64 size = 0; |
| 5561 | perf_output_put(handle, size); |
| 5562 | } else { |
| 5563 | unsigned long sp; |
| 5564 | unsigned int rem; |
| 5565 | u64 dyn_size; |
| 5566 | |
| 5567 | /* |
| 5568 | * We dump: |
| 5569 | * static size |
| 5570 | * - the size requested by user or the best one we can fit |
| 5571 | * in to the sample max size |
| 5572 | * data |
| 5573 | * - user stack dump data |
| 5574 | * dynamic size |
| 5575 | * - the actual dumped size |
| 5576 | */ |
| 5577 | |
| 5578 | /* Static size. */ |
| 5579 | perf_output_put(handle, dump_size); |
| 5580 | |
| 5581 | /* Data. */ |
| 5582 | sp = perf_user_stack_pointer(regs); |
| 5583 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 5584 | dyn_size = dump_size - rem; |
| 5585 | |
| 5586 | perf_output_skip(handle, rem); |
| 5587 | |
| 5588 | /* Dynamic size. */ |
| 5589 | perf_output_put(handle, dyn_size); |
| 5590 | } |
| 5591 | } |
| 5592 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5593 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 5594 | struct perf_sample_data *data, |
| 5595 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5596 | { |
| 5597 | u64 sample_type = event->attr.sample_type; |
| 5598 | |
| 5599 | data->type = sample_type; |
| 5600 | header->size += event->id_header_size; |
| 5601 | |
| 5602 | if (sample_type & PERF_SAMPLE_TID) { |
| 5603 | /* namespace issues */ |
| 5604 | data->tid_entry.pid = perf_event_pid(event, current); |
| 5605 | data->tid_entry.tid = perf_event_tid(event, current); |
| 5606 | } |
| 5607 | |
| 5608 | if (sample_type & PERF_SAMPLE_TIME) |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 5609 | data->time = perf_event_clock(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5610 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5611 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5612 | data->id = primary_event_id(event); |
| 5613 | |
| 5614 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5615 | data->stream_id = event->id; |
| 5616 | |
| 5617 | if (sample_type & PERF_SAMPLE_CPU) { |
| 5618 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 5619 | data->cpu_entry.reserved = 0; |
| 5620 | } |
| 5621 | } |
| 5622 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5623 | void perf_event_header__init_id(struct perf_event_header *header, |
| 5624 | struct perf_sample_data *data, |
| 5625 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5626 | { |
| 5627 | if (event->attr.sample_id_all) |
| 5628 | __perf_event_header__init_id(header, data, event); |
| 5629 | } |
| 5630 | |
| 5631 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 5632 | struct perf_sample_data *data) |
| 5633 | { |
| 5634 | u64 sample_type = data->type; |
| 5635 | |
| 5636 | if (sample_type & PERF_SAMPLE_TID) |
| 5637 | perf_output_put(handle, data->tid_entry); |
| 5638 | |
| 5639 | if (sample_type & PERF_SAMPLE_TIME) |
| 5640 | perf_output_put(handle, data->time); |
| 5641 | |
| 5642 | if (sample_type & PERF_SAMPLE_ID) |
| 5643 | perf_output_put(handle, data->id); |
| 5644 | |
| 5645 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5646 | perf_output_put(handle, data->stream_id); |
| 5647 | |
| 5648 | if (sample_type & PERF_SAMPLE_CPU) |
| 5649 | perf_output_put(handle, data->cpu_entry); |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5650 | |
| 5651 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 5652 | perf_output_put(handle, data->id); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5653 | } |
| 5654 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5655 | void perf_event__output_id_sample(struct perf_event *event, |
| 5656 | struct perf_output_handle *handle, |
| 5657 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5658 | { |
| 5659 | if (event->attr.sample_id_all) |
| 5660 | __perf_event__output_id_sample(handle, sample); |
| 5661 | } |
| 5662 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5663 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5664 | struct perf_event *event, |
| 5665 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5666 | { |
| 5667 | u64 read_format = event->attr.read_format; |
| 5668 | u64 values[4]; |
| 5669 | int n = 0; |
| 5670 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5671 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5672 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5673 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5674 | atomic64_read(&event->child_total_time_enabled); |
| 5675 | } |
| 5676 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5677 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5678 | atomic64_read(&event->child_total_time_running); |
| 5679 | } |
| 5680 | if (read_format & PERF_FORMAT_ID) |
| 5681 | values[n++] = primary_event_id(event); |
| 5682 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5683 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5684 | } |
| 5685 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5686 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5687 | struct perf_event *event, |
| 5688 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5689 | { |
| 5690 | struct perf_event *leader = event->group_leader, *sub; |
| 5691 | u64 read_format = event->attr.read_format; |
| 5692 | u64 values[5]; |
| 5693 | int n = 0; |
| 5694 | |
| 5695 | values[n++] = 1 + leader->nr_siblings; |
| 5696 | |
| 5697 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5698 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5699 | |
| 5700 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5701 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5702 | |
Peter Zijlstra | bc09bf8 | 2018-03-09 12:52:04 +0100 | [diff] [blame] | 5703 | if ((leader != event) && |
| 5704 | (leader->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5705 | leader->pmu->read(leader); |
| 5706 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5707 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5708 | if (read_format & PERF_FORMAT_ID) |
| 5709 | values[n++] = primary_event_id(leader); |
| 5710 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5711 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5712 | |
| 5713 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 5714 | n = 0; |
| 5715 | |
Jiri Olsa | 6f5ab00 | 2012-10-15 20:13:45 +0200 | [diff] [blame] | 5716 | if ((sub != event) && |
| 5717 | (sub->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5718 | sub->pmu->read(sub); |
| 5719 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5720 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5721 | if (read_format & PERF_FORMAT_ID) |
| 5722 | values[n++] = primary_event_id(sub); |
| 5723 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5724 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5725 | } |
| 5726 | } |
| 5727 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5728 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 5729 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 5730 | |
Peter Zijlstra | 50fe37e | 2017-05-30 11:45:12 +0200 | [diff] [blame] | 5731 | /* |
| 5732 | * XXX PERF_SAMPLE_READ vs inherited events seems difficult. |
| 5733 | * |
| 5734 | * The problem is that its both hard and excessively expensive to iterate the |
| 5735 | * child list, not to mention that its impossible to IPI the children running |
| 5736 | * on another CPU, from interrupt/NMI context. |
| 5737 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5738 | static void perf_output_read(struct perf_output_handle *handle, |
| 5739 | struct perf_event *event) |
| 5740 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 5741 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5742 | u64 read_format = event->attr.read_format; |
| 5743 | |
| 5744 | /* |
| 5745 | * compute total_time_enabled, total_time_running |
| 5746 | * based on snapshot values taken when the event |
| 5747 | * was last scheduled in. |
| 5748 | * |
| 5749 | * we cannot simply called update_context_time() |
| 5750 | * because of locking issue as we are called in |
| 5751 | * NMI context |
| 5752 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 5753 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 5754 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5755 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5756 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5757 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5758 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5759 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | void perf_output_sample(struct perf_output_handle *handle, |
| 5763 | struct perf_event_header *header, |
| 5764 | struct perf_sample_data *data, |
| 5765 | struct perf_event *event) |
| 5766 | { |
| 5767 | u64 sample_type = data->type; |
| 5768 | |
| 5769 | perf_output_put(handle, *header); |
| 5770 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5771 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 5772 | perf_output_put(handle, data->id); |
| 5773 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5774 | if (sample_type & PERF_SAMPLE_IP) |
| 5775 | perf_output_put(handle, data->ip); |
| 5776 | |
| 5777 | if (sample_type & PERF_SAMPLE_TID) |
| 5778 | perf_output_put(handle, data->tid_entry); |
| 5779 | |
| 5780 | if (sample_type & PERF_SAMPLE_TIME) |
| 5781 | perf_output_put(handle, data->time); |
| 5782 | |
| 5783 | if (sample_type & PERF_SAMPLE_ADDR) |
| 5784 | perf_output_put(handle, data->addr); |
| 5785 | |
| 5786 | if (sample_type & PERF_SAMPLE_ID) |
| 5787 | perf_output_put(handle, data->id); |
| 5788 | |
| 5789 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5790 | perf_output_put(handle, data->stream_id); |
| 5791 | |
| 5792 | if (sample_type & PERF_SAMPLE_CPU) |
| 5793 | perf_output_put(handle, data->cpu_entry); |
| 5794 | |
| 5795 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 5796 | perf_output_put(handle, data->period); |
| 5797 | |
| 5798 | if (sample_type & PERF_SAMPLE_READ) |
| 5799 | perf_output_read(handle, event); |
| 5800 | |
| 5801 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5802 | if (data->callchain) { |
| 5803 | int size = 1; |
| 5804 | |
| 5805 | if (data->callchain) |
| 5806 | size += data->callchain->nr; |
| 5807 | |
| 5808 | size *= sizeof(u64); |
| 5809 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5810 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5811 | } else { |
| 5812 | u64 nr = 0; |
| 5813 | perf_output_put(handle, nr); |
| 5814 | } |
| 5815 | } |
| 5816 | |
| 5817 | if (sample_type & PERF_SAMPLE_RAW) { |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 5818 | struct perf_raw_record *raw = data->raw; |
Alexei Starovoitov | fa128e6 | 2015-10-20 20:02:33 -0700 | [diff] [blame] | 5819 | |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 5820 | if (raw) { |
| 5821 | struct perf_raw_frag *frag = &raw->frag; |
| 5822 | |
| 5823 | perf_output_put(handle, raw->size); |
| 5824 | do { |
| 5825 | if (frag->copy) { |
| 5826 | __output_custom(handle, frag->copy, |
| 5827 | frag->data, frag->size); |
| 5828 | } else { |
| 5829 | __output_copy(handle, frag->data, |
| 5830 | frag->size); |
| 5831 | } |
| 5832 | if (perf_raw_frag_last(frag)) |
| 5833 | break; |
| 5834 | frag = frag->next; |
| 5835 | } while (1); |
| 5836 | if (frag->pad) |
| 5837 | __output_skip(handle, NULL, frag->pad); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5838 | } else { |
| 5839 | struct { |
| 5840 | u32 size; |
| 5841 | u32 data; |
| 5842 | } raw = { |
| 5843 | .size = sizeof(u32), |
| 5844 | .data = 0, |
| 5845 | }; |
| 5846 | perf_output_put(handle, raw); |
| 5847 | } |
| 5848 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5849 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5850 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5851 | if (data->br_stack) { |
| 5852 | size_t size; |
| 5853 | |
| 5854 | size = data->br_stack->nr |
| 5855 | * sizeof(struct perf_branch_entry); |
| 5856 | |
| 5857 | perf_output_put(handle, data->br_stack->nr); |
| 5858 | perf_output_copy(handle, data->br_stack->entries, size); |
| 5859 | } else { |
| 5860 | /* |
| 5861 | * we always store at least the value of nr |
| 5862 | */ |
| 5863 | u64 nr = 0; |
| 5864 | perf_output_put(handle, nr); |
| 5865 | } |
| 5866 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5867 | |
| 5868 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5869 | u64 abi = data->regs_user.abi; |
| 5870 | |
| 5871 | /* |
| 5872 | * If there are no regs to dump, notice it through |
| 5873 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5874 | */ |
| 5875 | perf_output_put(handle, abi); |
| 5876 | |
| 5877 | if (abi) { |
| 5878 | u64 mask = event->attr.sample_regs_user; |
| 5879 | perf_output_sample_regs(handle, |
| 5880 | data->regs_user.regs, |
| 5881 | mask); |
| 5882 | } |
| 5883 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5884 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5885 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5886 | perf_output_sample_ustack(handle, |
| 5887 | data->stack_user_size, |
| 5888 | data->regs_user.regs); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5889 | } |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 5890 | |
| 5891 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 5892 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 5893 | |
| 5894 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 5895 | perf_output_put(handle, data->data_src.val); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5896 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 5897 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 5898 | perf_output_put(handle, data->txn); |
| 5899 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5900 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5901 | u64 abi = data->regs_intr.abi; |
| 5902 | /* |
| 5903 | * If there are no regs to dump, notice it through |
| 5904 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5905 | */ |
| 5906 | perf_output_put(handle, abi); |
| 5907 | |
| 5908 | if (abi) { |
| 5909 | u64 mask = event->attr.sample_regs_intr; |
| 5910 | |
| 5911 | perf_output_sample_regs(handle, |
| 5912 | data->regs_intr.regs, |
| 5913 | mask); |
| 5914 | } |
| 5915 | } |
| 5916 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5917 | if (!event->attr.watermark) { |
| 5918 | int wakeup_events = event->attr.wakeup_events; |
| 5919 | |
| 5920 | if (wakeup_events) { |
| 5921 | struct ring_buffer *rb = handle->rb; |
| 5922 | int events = local_inc_return(&rb->events); |
| 5923 | |
| 5924 | if (events >= wakeup_events) { |
| 5925 | local_sub(wakeup_events, &rb->events); |
| 5926 | local_inc(&rb->wakeup); |
| 5927 | } |
| 5928 | } |
| 5929 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5930 | } |
| 5931 | |
| 5932 | void perf_prepare_sample(struct perf_event_header *header, |
| 5933 | struct perf_sample_data *data, |
| 5934 | struct perf_event *event, |
| 5935 | struct pt_regs *regs) |
| 5936 | { |
| 5937 | u64 sample_type = event->attr.sample_type; |
| 5938 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5939 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5940 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5941 | |
| 5942 | header->misc = 0; |
| 5943 | header->misc |= perf_misc_flags(regs); |
| 5944 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5945 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5946 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5947 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5948 | data->ip = perf_instruction_pointer(regs); |
| 5949 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5950 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5951 | int size = 1; |
| 5952 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5953 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5954 | |
| 5955 | if (data->callchain) |
| 5956 | size += data->callchain->nr; |
| 5957 | |
| 5958 | header->size += size * sizeof(u64); |
| 5959 | } |
| 5960 | |
| 5961 | if (sample_type & PERF_SAMPLE_RAW) { |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 5962 | struct perf_raw_record *raw = data->raw; |
| 5963 | int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5964 | |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 5965 | if (raw) { |
| 5966 | struct perf_raw_frag *frag = &raw->frag; |
| 5967 | u32 sum = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5968 | |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 5969 | do { |
| 5970 | sum += frag->size; |
| 5971 | if (perf_raw_frag_last(frag)) |
| 5972 | break; |
| 5973 | frag = frag->next; |
| 5974 | } while (1); |
| 5975 | |
| 5976 | size = round_up(sum + sizeof(u32), sizeof(u64)); |
| 5977 | raw->size = size - sizeof(u32); |
| 5978 | frag->pad = raw->size - sum; |
| 5979 | } else { |
| 5980 | size = sizeof(u64); |
| 5981 | } |
| 5982 | |
| 5983 | header->size += size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5984 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5985 | |
| 5986 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5987 | int size = sizeof(u64); /* nr */ |
| 5988 | if (data->br_stack) { |
| 5989 | size += data->br_stack->nr |
| 5990 | * sizeof(struct perf_branch_entry); |
| 5991 | } |
| 5992 | header->size += size; |
| 5993 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5994 | |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5995 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5996 | perf_sample_regs_user(&data->regs_user, regs, |
| 5997 | &data->regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5998 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5999 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 6000 | /* regs dump ABI info */ |
| 6001 | int size = sizeof(u64); |
| 6002 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 6003 | if (data->regs_user.regs) { |
| 6004 | u64 mask = event->attr.sample_regs_user; |
| 6005 | size += hweight64(mask) * sizeof(u64); |
| 6006 | } |
| 6007 | |
| 6008 | header->size += size; |
| 6009 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6010 | |
| 6011 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 6012 | /* |
| 6013 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 6014 | * processed as the last one or have additional check added |
| 6015 | * in case new sample type is added, because we could eat |
| 6016 | * up the rest of the sample size. |
| 6017 | */ |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6018 | u16 stack_size = event->attr.sample_stack_user; |
| 6019 | u16 size = sizeof(u64); |
| 6020 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6021 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 6022 | data->regs_user.regs); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 6023 | |
| 6024 | /* |
| 6025 | * If there is something to dump, add space for the dump |
| 6026 | * itself and for the field that tells the dynamic size, |
| 6027 | * which is how many have been actually dumped. |
| 6028 | */ |
| 6029 | if (stack_size) |
| 6030 | size += sizeof(u64) + stack_size; |
| 6031 | |
| 6032 | data->stack_user_size = stack_size; |
| 6033 | header->size += size; |
| 6034 | } |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 6035 | |
| 6036 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 6037 | /* regs dump ABI info */ |
| 6038 | int size = sizeof(u64); |
| 6039 | |
| 6040 | perf_sample_regs_intr(&data->regs_intr, regs); |
| 6041 | |
| 6042 | if (data->regs_intr.regs) { |
| 6043 | u64 mask = event->attr.sample_regs_intr; |
| 6044 | |
| 6045 | size += hweight64(mask) * sizeof(u64); |
| 6046 | } |
| 6047 | |
| 6048 | header->size += size; |
| 6049 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6050 | } |
| 6051 | |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 6052 | static void __always_inline |
| 6053 | __perf_event_output(struct perf_event *event, |
| 6054 | struct perf_sample_data *data, |
| 6055 | struct pt_regs *regs, |
| 6056 | int (*output_begin)(struct perf_output_handle *, |
| 6057 | struct perf_event *, |
| 6058 | unsigned int)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6059 | { |
| 6060 | struct perf_output_handle handle; |
| 6061 | struct perf_event_header header; |
| 6062 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6063 | /* protect the callchain buffers */ |
| 6064 | rcu_read_lock(); |
| 6065 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6066 | perf_prepare_sample(&header, data, event, regs); |
| 6067 | |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 6068 | if (output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6069 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6070 | |
| 6071 | perf_output_sample(&handle, &header, data, event); |
| 6072 | |
| 6073 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6074 | |
| 6075 | exit: |
| 6076 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6077 | } |
| 6078 | |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 6079 | void |
| 6080 | perf_event_output_forward(struct perf_event *event, |
| 6081 | struct perf_sample_data *data, |
| 6082 | struct pt_regs *regs) |
| 6083 | { |
| 6084 | __perf_event_output(event, data, regs, perf_output_begin_forward); |
| 6085 | } |
| 6086 | |
| 6087 | void |
| 6088 | perf_event_output_backward(struct perf_event *event, |
| 6089 | struct perf_sample_data *data, |
| 6090 | struct pt_regs *regs) |
| 6091 | { |
| 6092 | __perf_event_output(event, data, regs, perf_output_begin_backward); |
| 6093 | } |
| 6094 | |
| 6095 | void |
| 6096 | perf_event_output(struct perf_event *event, |
| 6097 | struct perf_sample_data *data, |
| 6098 | struct pt_regs *regs) |
| 6099 | { |
| 6100 | __perf_event_output(event, data, regs, perf_output_begin); |
| 6101 | } |
| 6102 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6103 | /* |
| 6104 | * read event_id |
| 6105 | */ |
| 6106 | |
| 6107 | struct perf_read_event { |
| 6108 | struct perf_event_header header; |
| 6109 | |
| 6110 | u32 pid; |
| 6111 | u32 tid; |
| 6112 | }; |
| 6113 | |
| 6114 | static void |
| 6115 | perf_event_read_event(struct perf_event *event, |
| 6116 | struct task_struct *task) |
| 6117 | { |
| 6118 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6119 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6120 | struct perf_read_event read_event = { |
| 6121 | .header = { |
| 6122 | .type = PERF_RECORD_READ, |
| 6123 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 6124 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6125 | }, |
| 6126 | .pid = perf_event_pid(event, task), |
| 6127 | .tid = perf_event_tid(event, task), |
| 6128 | }; |
| 6129 | int ret; |
| 6130 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6131 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6132 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6133 | if (ret) |
| 6134 | return; |
| 6135 | |
| 6136 | perf_output_put(&handle, read_event); |
| 6137 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6138 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6139 | |
| 6140 | perf_output_end(&handle); |
| 6141 | } |
| 6142 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6143 | typedef void (perf_iterate_f)(struct perf_event *event, void *data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6144 | |
| 6145 | static void |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6146 | perf_iterate_ctx(struct perf_event_context *ctx, |
| 6147 | perf_iterate_f output, |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 6148 | void *data, bool all) |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6149 | { |
| 6150 | struct perf_event *event; |
| 6151 | |
| 6152 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 6153 | if (!all) { |
| 6154 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 6155 | continue; |
| 6156 | if (!event_filter_match(event)) |
| 6157 | continue; |
| 6158 | } |
| 6159 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6160 | output(event, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6161 | } |
| 6162 | } |
| 6163 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6164 | static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 6165 | { |
| 6166 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); |
| 6167 | struct perf_event *event; |
| 6168 | |
| 6169 | list_for_each_entry_rcu(event, &pel->list, sb_list) { |
Peter Zijlstra | 0b8f1e2 | 2016-08-04 14:37:24 +0200 | [diff] [blame] | 6170 | /* |
| 6171 | * Skip events that are not fully formed yet; ensure that |
| 6172 | * if we observe event->ctx, both event and ctx will be |
| 6173 | * complete enough. See perf_install_in_context(). |
| 6174 | */ |
| 6175 | if (!smp_load_acquire(&event->ctx)) |
| 6176 | continue; |
| 6177 | |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 6178 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 6179 | continue; |
| 6180 | if (!event_filter_match(event)) |
| 6181 | continue; |
| 6182 | output(event, data); |
| 6183 | } |
| 6184 | } |
| 6185 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6186 | /* |
| 6187 | * Iterate all events that need to receive side-band events. |
| 6188 | * |
| 6189 | * For new callers; ensure that account_pmu_sb_event() includes |
| 6190 | * your event, otherwise it might not get delivered. |
| 6191 | */ |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 6192 | static void |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6193 | perf_iterate_sb(perf_iterate_f output, void *data, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6194 | struct perf_event_context *task_ctx) |
| 6195 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6196 | struct perf_event_context *ctx; |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6197 | int ctxn; |
| 6198 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6199 | rcu_read_lock(); |
| 6200 | preempt_disable(); |
| 6201 | |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 6202 | /* |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6203 | * If we have task_ctx != NULL we only notify the task context itself. |
| 6204 | * The task_ctx is set only for EXIT events before releasing task |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 6205 | * context. |
| 6206 | */ |
| 6207 | if (task_ctx) { |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6208 | perf_iterate_ctx(task_ctx, output, data, false); |
| 6209 | goto done; |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 6210 | } |
| 6211 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6212 | perf_iterate_sb_cpu(output, data); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 6213 | |
| 6214 | for_each_task_context_nr(ctxn) { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6215 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 6216 | if (ctx) |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6217 | perf_iterate_ctx(ctx, output, data, false); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6218 | } |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6219 | done: |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 6220 | preempt_enable(); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6221 | rcu_read_unlock(); |
| 6222 | } |
| 6223 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6224 | /* |
| 6225 | * Clear all file-based filters at exec, they'll have to be |
| 6226 | * re-instated when/if these objects are mmapped again. |
| 6227 | */ |
| 6228 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) |
| 6229 | { |
| 6230 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 6231 | struct perf_addr_filter *filter; |
| 6232 | unsigned int restart = 0, count = 0; |
| 6233 | unsigned long flags; |
| 6234 | |
| 6235 | if (!has_addr_filter(event)) |
| 6236 | return; |
| 6237 | |
| 6238 | raw_spin_lock_irqsave(&ifh->lock, flags); |
| 6239 | list_for_each_entry(filter, &ifh->list, entry) { |
| 6240 | if (filter->inode) { |
| 6241 | event->addr_filters_offs[count] = 0; |
| 6242 | restart++; |
| 6243 | } |
| 6244 | |
| 6245 | count++; |
| 6246 | } |
| 6247 | |
| 6248 | if (restart) |
| 6249 | event->addr_filters_gen++; |
| 6250 | raw_spin_unlock_irqrestore(&ifh->lock, flags); |
| 6251 | |
| 6252 | if (restart) |
Alexander Shishkin | 767ae08 | 2016-09-06 16:23:49 +0300 | [diff] [blame] | 6253 | perf_event_stop(event, 1); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6254 | } |
| 6255 | |
| 6256 | void perf_event_exec(void) |
| 6257 | { |
| 6258 | struct perf_event_context *ctx; |
| 6259 | int ctxn; |
| 6260 | |
| 6261 | rcu_read_lock(); |
| 6262 | for_each_task_context_nr(ctxn) { |
| 6263 | ctx = current->perf_event_ctxp[ctxn]; |
| 6264 | if (!ctx) |
| 6265 | continue; |
| 6266 | |
| 6267 | perf_event_enable_on_exec(ctxn); |
| 6268 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6269 | perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6270 | true); |
| 6271 | } |
| 6272 | rcu_read_unlock(); |
| 6273 | } |
| 6274 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6275 | struct remote_output { |
| 6276 | struct ring_buffer *rb; |
| 6277 | int err; |
| 6278 | }; |
| 6279 | |
| 6280 | static void __perf_event_output_stop(struct perf_event *event, void *data) |
| 6281 | { |
| 6282 | struct perf_event *parent = event->parent; |
| 6283 | struct remote_output *ro = data; |
| 6284 | struct ring_buffer *rb = ro->rb; |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6285 | struct stop_event_data sd = { |
| 6286 | .event = event, |
| 6287 | }; |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6288 | |
| 6289 | if (!has_aux(event)) |
| 6290 | return; |
| 6291 | |
| 6292 | if (!parent) |
| 6293 | parent = event; |
| 6294 | |
| 6295 | /* |
| 6296 | * In case of inheritance, it will be the parent that links to the |
Alexander Shishkin | 767ae08 | 2016-09-06 16:23:49 +0300 | [diff] [blame] | 6297 | * ring-buffer, but it will be the child that's actually using it. |
| 6298 | * |
| 6299 | * We are using event::rb to determine if the event should be stopped, |
| 6300 | * however this may race with ring_buffer_attach() (through set_output), |
| 6301 | * which will make us skip the event that actually needs to be stopped. |
| 6302 | * So ring_buffer_attach() has to stop an aux event before re-assigning |
| 6303 | * its rb pointer. |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6304 | */ |
| 6305 | if (rcu_dereference(parent->rb) == rb) |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6306 | ro->err = __perf_event_stop(&sd); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6307 | } |
| 6308 | |
| 6309 | static int __perf_pmu_output_stop(void *info) |
| 6310 | { |
| 6311 | struct perf_event *event = info; |
| 6312 | struct pmu *pmu = event->pmu; |
Will Deacon | 8b6a3fe | 2016-08-24 10:07:14 +0100 | [diff] [blame] | 6313 | struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6314 | struct remote_output ro = { |
| 6315 | .rb = event->rb, |
| 6316 | }; |
| 6317 | |
| 6318 | rcu_read_lock(); |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6319 | perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6320 | if (cpuctx->task_ctx) |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6321 | perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 6322 | &ro, false); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6323 | rcu_read_unlock(); |
| 6324 | |
| 6325 | return ro.err; |
| 6326 | } |
| 6327 | |
| 6328 | static void perf_pmu_output_stop(struct perf_event *event) |
| 6329 | { |
| 6330 | struct perf_event *iter; |
| 6331 | int err, cpu; |
| 6332 | |
| 6333 | restart: |
| 6334 | rcu_read_lock(); |
| 6335 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { |
| 6336 | /* |
| 6337 | * For per-CPU events, we need to make sure that neither they |
| 6338 | * nor their children are running; for cpu==-1 events it's |
| 6339 | * sufficient to stop the event itself if it's active, since |
| 6340 | * it can't have children. |
| 6341 | */ |
| 6342 | cpu = iter->cpu; |
| 6343 | if (cpu == -1) |
| 6344 | cpu = READ_ONCE(iter->oncpu); |
| 6345 | |
| 6346 | if (cpu == -1) |
| 6347 | continue; |
| 6348 | |
| 6349 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); |
| 6350 | if (err == -EAGAIN) { |
| 6351 | rcu_read_unlock(); |
| 6352 | goto restart; |
| 6353 | } |
| 6354 | } |
| 6355 | rcu_read_unlock(); |
| 6356 | } |
| 6357 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6358 | /* |
| 6359 | * task tracking -- fork/exit |
| 6360 | * |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6361 | * 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] | 6362 | */ |
| 6363 | |
| 6364 | struct perf_task_event { |
| 6365 | struct task_struct *task; |
| 6366 | struct perf_event_context *task_ctx; |
| 6367 | |
| 6368 | struct { |
| 6369 | struct perf_event_header header; |
| 6370 | |
| 6371 | u32 pid; |
| 6372 | u32 ppid; |
| 6373 | u32 tid; |
| 6374 | u32 ptid; |
| 6375 | u64 time; |
| 6376 | } event_id; |
| 6377 | }; |
| 6378 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6379 | static int perf_event_task_match(struct perf_event *event) |
| 6380 | { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6381 | return event->attr.comm || event->attr.mmap || |
| 6382 | event->attr.mmap2 || event->attr.mmap_data || |
| 6383 | event->attr.task; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6384 | } |
| 6385 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6386 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6387 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6388 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6389 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6390 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6391 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6392 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6393 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 6394 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6395 | if (!perf_event_task_match(event)) |
| 6396 | return; |
| 6397 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6398 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6399 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6400 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6401 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 6402 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6403 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6404 | |
| 6405 | task_event->event_id.pid = perf_event_pid(event, task); |
| 6406 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 6407 | |
| 6408 | task_event->event_id.tid = perf_event_tid(event, task); |
| 6409 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 6410 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6411 | task_event->event_id.time = perf_event_clock(event); |
| 6412 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6413 | perf_output_put(&handle, task_event->event_id); |
| 6414 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6415 | perf_event__output_id_sample(event, &handle, &sample); |
| 6416 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6417 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6418 | out: |
| 6419 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6420 | } |
| 6421 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6422 | static void perf_event_task(struct task_struct *task, |
| 6423 | struct perf_event_context *task_ctx, |
| 6424 | int new) |
| 6425 | { |
| 6426 | struct perf_task_event task_event; |
| 6427 | |
| 6428 | if (!atomic_read(&nr_comm_events) && |
| 6429 | !atomic_read(&nr_mmap_events) && |
| 6430 | !atomic_read(&nr_task_events)) |
| 6431 | return; |
| 6432 | |
| 6433 | task_event = (struct perf_task_event){ |
| 6434 | .task = task, |
| 6435 | .task_ctx = task_ctx, |
| 6436 | .event_id = { |
| 6437 | .header = { |
| 6438 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 6439 | .misc = 0, |
| 6440 | .size = sizeof(task_event.event_id), |
| 6441 | }, |
| 6442 | /* .pid */ |
| 6443 | /* .ppid */ |
| 6444 | /* .tid */ |
| 6445 | /* .ptid */ |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6446 | /* .time */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6447 | }, |
| 6448 | }; |
| 6449 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6450 | perf_iterate_sb(perf_event_task_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6451 | &task_event, |
| 6452 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6453 | } |
| 6454 | |
| 6455 | void perf_event_fork(struct task_struct *task) |
| 6456 | { |
| 6457 | perf_event_task(task, NULL, 1); |
| 6458 | } |
| 6459 | |
| 6460 | /* |
| 6461 | * comm tracking |
| 6462 | */ |
| 6463 | |
| 6464 | struct perf_comm_event { |
| 6465 | struct task_struct *task; |
| 6466 | char *comm; |
| 6467 | int comm_size; |
| 6468 | |
| 6469 | struct { |
| 6470 | struct perf_event_header header; |
| 6471 | |
| 6472 | u32 pid; |
| 6473 | u32 tid; |
| 6474 | } event_id; |
| 6475 | }; |
| 6476 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6477 | static int perf_event_comm_match(struct perf_event *event) |
| 6478 | { |
| 6479 | return event->attr.comm; |
| 6480 | } |
| 6481 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6482 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6483 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6484 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6485 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6486 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6487 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6488 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6489 | int ret; |
| 6490 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6491 | if (!perf_event_comm_match(event)) |
| 6492 | return; |
| 6493 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6494 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 6495 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6496 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6497 | |
| 6498 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6499 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6500 | |
| 6501 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 6502 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 6503 | |
| 6504 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6505 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6506 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6507 | |
| 6508 | perf_event__output_id_sample(event, &handle, &sample); |
| 6509 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6510 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6511 | out: |
| 6512 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6513 | } |
| 6514 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6515 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 6516 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6517 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6518 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6519 | |
| 6520 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 6521 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6522 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 6523 | |
| 6524 | comm_event->comm = comm; |
| 6525 | comm_event->comm_size = size; |
| 6526 | |
| 6527 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6528 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6529 | perf_iterate_sb(perf_event_comm_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6530 | comm_event, |
| 6531 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6532 | } |
| 6533 | |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 6534 | void perf_event_comm(struct task_struct *task, bool exec) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6535 | { |
| 6536 | struct perf_comm_event comm_event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6537 | |
| 6538 | if (!atomic_read(&nr_comm_events)) |
| 6539 | return; |
| 6540 | |
| 6541 | comm_event = (struct perf_comm_event){ |
| 6542 | .task = task, |
| 6543 | /* .comm */ |
| 6544 | /* .comm_size */ |
| 6545 | .event_id = { |
| 6546 | .header = { |
| 6547 | .type = PERF_RECORD_COMM, |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 6548 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6549 | /* .size */ |
| 6550 | }, |
| 6551 | /* .pid */ |
| 6552 | /* .tid */ |
| 6553 | }, |
| 6554 | }; |
| 6555 | |
| 6556 | perf_event_comm_event(&comm_event); |
| 6557 | } |
| 6558 | |
| 6559 | /* |
| 6560 | * mmap tracking |
| 6561 | */ |
| 6562 | |
| 6563 | struct perf_mmap_event { |
| 6564 | struct vm_area_struct *vma; |
| 6565 | |
| 6566 | const char *file_name; |
| 6567 | int file_size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6568 | int maj, min; |
| 6569 | u64 ino; |
| 6570 | u64 ino_generation; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6571 | u32 prot, flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6572 | |
| 6573 | struct { |
| 6574 | struct perf_event_header header; |
| 6575 | |
| 6576 | u32 pid; |
| 6577 | u32 tid; |
| 6578 | u64 start; |
| 6579 | u64 len; |
| 6580 | u64 pgoff; |
| 6581 | } event_id; |
| 6582 | }; |
| 6583 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6584 | static int perf_event_mmap_match(struct perf_event *event, |
| 6585 | void *data) |
| 6586 | { |
| 6587 | struct perf_mmap_event *mmap_event = data; |
| 6588 | struct vm_area_struct *vma = mmap_event->vma; |
| 6589 | int executable = vma->vm_flags & VM_EXEC; |
| 6590 | |
| 6591 | return (!executable && event->attr.mmap_data) || |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6592 | (executable && (event->attr.mmap || event->attr.mmap2)); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6593 | } |
| 6594 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6595 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6596 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6597 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6598 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6599 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6600 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6601 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6602 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6603 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6604 | if (!perf_event_mmap_match(event, data)) |
| 6605 | return; |
| 6606 | |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6607 | if (event->attr.mmap2) { |
| 6608 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; |
| 6609 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); |
| 6610 | mmap_event->event_id.header.size += sizeof(mmap_event->min); |
| 6611 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); |
Arnaldo Carvalho de Melo | d008d52 | 2013-09-10 10:24:05 -0300 | [diff] [blame] | 6612 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6613 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
| 6614 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6615 | } |
| 6616 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6617 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 6618 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6619 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6620 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6621 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6622 | |
| 6623 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 6624 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 6625 | |
| 6626 | perf_output_put(&handle, mmap_event->event_id); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6627 | |
| 6628 | if (event->attr.mmap2) { |
| 6629 | perf_output_put(&handle, mmap_event->maj); |
| 6630 | perf_output_put(&handle, mmap_event->min); |
| 6631 | perf_output_put(&handle, mmap_event->ino); |
| 6632 | perf_output_put(&handle, mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6633 | perf_output_put(&handle, mmap_event->prot); |
| 6634 | perf_output_put(&handle, mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6635 | } |
| 6636 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6637 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6638 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6639 | |
| 6640 | perf_event__output_id_sample(event, &handle, &sample); |
| 6641 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6642 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6643 | out: |
| 6644 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6645 | } |
| 6646 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6647 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 6648 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6649 | struct vm_area_struct *vma = mmap_event->vma; |
| 6650 | struct file *file = vma->vm_file; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6651 | int maj = 0, min = 0; |
| 6652 | u64 ino = 0, gen = 0; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6653 | u32 prot = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6654 | unsigned int size; |
| 6655 | char tmp[16]; |
| 6656 | char *buf = NULL; |
Peter Zijlstra | 2c42cfb | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6657 | char *name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6658 | |
Peter Zijlstra | b41615a | 2017-01-26 23:15:08 +0100 | [diff] [blame] | 6659 | if (vma->vm_flags & VM_READ) |
| 6660 | prot |= PROT_READ; |
| 6661 | if (vma->vm_flags & VM_WRITE) |
| 6662 | prot |= PROT_WRITE; |
| 6663 | if (vma->vm_flags & VM_EXEC) |
| 6664 | prot |= PROT_EXEC; |
| 6665 | |
| 6666 | if (vma->vm_flags & VM_MAYSHARE) |
| 6667 | flags = MAP_SHARED; |
| 6668 | else |
| 6669 | flags = MAP_PRIVATE; |
| 6670 | |
| 6671 | if (vma->vm_flags & VM_DENYWRITE) |
| 6672 | flags |= MAP_DENYWRITE; |
| 6673 | if (vma->vm_flags & VM_MAYEXEC) |
| 6674 | flags |= MAP_EXECUTABLE; |
| 6675 | if (vma->vm_flags & VM_LOCKED) |
| 6676 | flags |= MAP_LOCKED; |
| 6677 | if (vma->vm_flags & VM_HUGETLB) |
| 6678 | flags |= MAP_HUGETLB; |
| 6679 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6680 | if (file) { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6681 | struct inode *inode; |
| 6682 | dev_t dev; |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 6683 | |
Peter Zijlstra | 2c42cfb | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6684 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6685 | if (!buf) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6686 | name = "//enomem"; |
| 6687 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6688 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6689 | /* |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 6690 | * d_path() works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6691 | * need to add enough zero bytes after the string to handle |
| 6692 | * the 64bit alignment we do later. |
| 6693 | */ |
Miklos Szeredi | 9bf39ab | 2015-06-19 10:29:13 +0200 | [diff] [blame] | 6694 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6695 | if (IS_ERR(name)) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6696 | name = "//toolong"; |
| 6697 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6698 | } |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6699 | inode = file_inode(vma->vm_file); |
| 6700 | dev = inode->i_sb->s_dev; |
| 6701 | ino = inode->i_ino; |
| 6702 | gen = inode->i_generation; |
| 6703 | maj = MAJOR(dev); |
| 6704 | min = MINOR(dev); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6705 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6706 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6707 | } else { |
Jiri Olsa | fbe26ab | 2014-07-14 17:57:19 +0200 | [diff] [blame] | 6708 | if (vma->vm_ops && vma->vm_ops->name) { |
| 6709 | name = (char *) vma->vm_ops->name(vma); |
| 6710 | if (name) |
| 6711 | goto cpy_name; |
| 6712 | } |
| 6713 | |
Peter Zijlstra | 2c42cfb | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6714 | name = (char *)arch_vma_name(vma); |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6715 | if (name) |
| 6716 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6717 | |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 6718 | if (vma->vm_start <= vma->vm_mm->start_brk && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6719 | vma->vm_end >= vma->vm_mm->brk) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6720 | name = "[heap]"; |
| 6721 | goto cpy_name; |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 6722 | } |
| 6723 | if (vma->vm_start <= vma->vm_mm->start_stack && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6724 | vma->vm_end >= vma->vm_mm->start_stack) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6725 | name = "[stack]"; |
| 6726 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6727 | } |
| 6728 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6729 | name = "//anon"; |
| 6730 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6731 | } |
| 6732 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6733 | cpy_name: |
| 6734 | strlcpy(tmp, name, sizeof(tmp)); |
| 6735 | name = tmp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6736 | got_name: |
Peter Zijlstra | 2c42cfb | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6737 | /* |
| 6738 | * Since our buffer works in 8 byte units we need to align our string |
| 6739 | * size to a multiple of 8. However, we must guarantee the tail end is |
| 6740 | * zero'd out to avoid leaking random bits to userspace. |
| 6741 | */ |
| 6742 | size = strlen(name)+1; |
| 6743 | while (!IS_ALIGNED(size, sizeof(u64))) |
| 6744 | name[size++] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6745 | |
| 6746 | mmap_event->file_name = name; |
| 6747 | mmap_event->file_size = size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6748 | mmap_event->maj = maj; |
| 6749 | mmap_event->min = min; |
| 6750 | mmap_event->ino = ino; |
| 6751 | mmap_event->ino_generation = gen; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6752 | mmap_event->prot = prot; |
| 6753 | mmap_event->flags = flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6754 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 6755 | if (!(vma->vm_flags & VM_EXEC)) |
| 6756 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 6757 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6758 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 6759 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6760 | perf_iterate_sb(perf_event_mmap_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6761 | mmap_event, |
| 6762 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6763 | |
| 6764 | kfree(buf); |
| 6765 | } |
| 6766 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6767 | /* |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6768 | * Check whether inode and address range match filter criteria. |
| 6769 | */ |
| 6770 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, |
| 6771 | struct file *file, unsigned long offset, |
| 6772 | unsigned long size) |
| 6773 | { |
| 6774 | if (filter->inode != file->f_inode) |
| 6775 | return false; |
| 6776 | |
| 6777 | if (filter->offset > offset + size) |
| 6778 | return false; |
| 6779 | |
| 6780 | if (filter->offset + filter->size < offset) |
| 6781 | return false; |
| 6782 | |
| 6783 | return true; |
| 6784 | } |
| 6785 | |
| 6786 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) |
| 6787 | { |
| 6788 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 6789 | struct vm_area_struct *vma = data; |
| 6790 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags; |
| 6791 | struct file *file = vma->vm_file; |
| 6792 | struct perf_addr_filter *filter; |
| 6793 | unsigned int restart = 0, count = 0; |
| 6794 | |
| 6795 | if (!has_addr_filter(event)) |
| 6796 | return; |
| 6797 | |
| 6798 | if (!file) |
| 6799 | return; |
| 6800 | |
| 6801 | raw_spin_lock_irqsave(&ifh->lock, flags); |
| 6802 | list_for_each_entry(filter, &ifh->list, entry) { |
| 6803 | if (perf_addr_filter_match(filter, file, off, |
| 6804 | vma->vm_end - vma->vm_start)) { |
| 6805 | event->addr_filters_offs[count] = vma->vm_start; |
| 6806 | restart++; |
| 6807 | } |
| 6808 | |
| 6809 | count++; |
| 6810 | } |
| 6811 | |
| 6812 | if (restart) |
| 6813 | event->addr_filters_gen++; |
| 6814 | raw_spin_unlock_irqrestore(&ifh->lock, flags); |
| 6815 | |
| 6816 | if (restart) |
Alexander Shishkin | 767ae08 | 2016-09-06 16:23:49 +0300 | [diff] [blame] | 6817 | perf_event_stop(event, 1); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6818 | } |
| 6819 | |
| 6820 | /* |
| 6821 | * Adjust all task's events' filters to the new vma |
| 6822 | */ |
| 6823 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) |
| 6824 | { |
| 6825 | struct perf_event_context *ctx; |
| 6826 | int ctxn; |
| 6827 | |
Mathieu Poirier | 12b40a2 | 2016-07-18 10:43:06 -0600 | [diff] [blame] | 6828 | /* |
| 6829 | * Data tracing isn't supported yet and as such there is no need |
| 6830 | * to keep track of anything that isn't related to executable code: |
| 6831 | */ |
| 6832 | if (!(vma->vm_flags & VM_EXEC)) |
| 6833 | return; |
| 6834 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6835 | rcu_read_lock(); |
| 6836 | for_each_task_context_nr(ctxn) { |
| 6837 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 6838 | if (!ctx) |
| 6839 | continue; |
| 6840 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 6841 | perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6842 | } |
| 6843 | rcu_read_unlock(); |
| 6844 | } |
| 6845 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 6846 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6847 | { |
| 6848 | struct perf_mmap_event mmap_event; |
| 6849 | |
| 6850 | if (!atomic_read(&nr_mmap_events)) |
| 6851 | return; |
| 6852 | |
| 6853 | mmap_event = (struct perf_mmap_event){ |
| 6854 | .vma = vma, |
| 6855 | /* .file_name */ |
| 6856 | /* .file_size */ |
| 6857 | .event_id = { |
| 6858 | .header = { |
| 6859 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 6860 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6861 | /* .size */ |
| 6862 | }, |
| 6863 | /* .pid */ |
| 6864 | /* .tid */ |
| 6865 | .start = vma->vm_start, |
| 6866 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 6867 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6868 | }, |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6869 | /* .maj (attr_mmap2 only) */ |
| 6870 | /* .min (attr_mmap2 only) */ |
| 6871 | /* .ino (attr_mmap2 only) */ |
| 6872 | /* .ino_generation (attr_mmap2 only) */ |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6873 | /* .prot (attr_mmap2 only) */ |
| 6874 | /* .flags (attr_mmap2 only) */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6875 | }; |
| 6876 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6877 | perf_addr_filters_adjust(vma); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6878 | perf_event_mmap_event(&mmap_event); |
| 6879 | } |
| 6880 | |
Alexander Shishkin | 68db7e9 | 2015-01-14 14:18:15 +0200 | [diff] [blame] | 6881 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
| 6882 | unsigned long size, u64 flags) |
| 6883 | { |
| 6884 | struct perf_output_handle handle; |
| 6885 | struct perf_sample_data sample; |
| 6886 | struct perf_aux_event { |
| 6887 | struct perf_event_header header; |
| 6888 | u64 offset; |
| 6889 | u64 size; |
| 6890 | u64 flags; |
| 6891 | } rec = { |
| 6892 | .header = { |
| 6893 | .type = PERF_RECORD_AUX, |
| 6894 | .misc = 0, |
| 6895 | .size = sizeof(rec), |
| 6896 | }, |
| 6897 | .offset = head, |
| 6898 | .size = size, |
| 6899 | .flags = flags, |
| 6900 | }; |
| 6901 | int ret; |
| 6902 | |
| 6903 | perf_event_header__init_id(&rec.header, &sample, event); |
| 6904 | ret = perf_output_begin(&handle, event, rec.header.size); |
| 6905 | |
| 6906 | if (ret) |
| 6907 | return; |
| 6908 | |
| 6909 | perf_output_put(&handle, rec); |
| 6910 | perf_event__output_id_sample(event, &handle, &sample); |
| 6911 | |
| 6912 | perf_output_end(&handle); |
| 6913 | } |
| 6914 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6915 | /* |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 6916 | * Lost/dropped samples logging |
| 6917 | */ |
| 6918 | void perf_log_lost_samples(struct perf_event *event, u64 lost) |
| 6919 | { |
| 6920 | struct perf_output_handle handle; |
| 6921 | struct perf_sample_data sample; |
| 6922 | int ret; |
| 6923 | |
| 6924 | struct { |
| 6925 | struct perf_event_header header; |
| 6926 | u64 lost; |
| 6927 | } lost_samples_event = { |
| 6928 | .header = { |
| 6929 | .type = PERF_RECORD_LOST_SAMPLES, |
| 6930 | .misc = 0, |
| 6931 | .size = sizeof(lost_samples_event), |
| 6932 | }, |
| 6933 | .lost = lost, |
| 6934 | }; |
| 6935 | |
| 6936 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); |
| 6937 | |
| 6938 | ret = perf_output_begin(&handle, event, |
| 6939 | lost_samples_event.header.size); |
| 6940 | if (ret) |
| 6941 | return; |
| 6942 | |
| 6943 | perf_output_put(&handle, lost_samples_event); |
| 6944 | perf_event__output_id_sample(event, &handle, &sample); |
| 6945 | perf_output_end(&handle); |
| 6946 | } |
| 6947 | |
| 6948 | /* |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 6949 | * context_switch tracking |
| 6950 | */ |
| 6951 | |
| 6952 | struct perf_switch_event { |
| 6953 | struct task_struct *task; |
| 6954 | struct task_struct *next_prev; |
| 6955 | |
| 6956 | struct { |
| 6957 | struct perf_event_header header; |
| 6958 | u32 next_prev_pid; |
| 6959 | u32 next_prev_tid; |
| 6960 | } event_id; |
| 6961 | }; |
| 6962 | |
| 6963 | static int perf_event_switch_match(struct perf_event *event) |
| 6964 | { |
| 6965 | return event->attr.context_switch; |
| 6966 | } |
| 6967 | |
| 6968 | static void perf_event_switch_output(struct perf_event *event, void *data) |
| 6969 | { |
| 6970 | struct perf_switch_event *se = data; |
| 6971 | struct perf_output_handle handle; |
| 6972 | struct perf_sample_data sample; |
| 6973 | int ret; |
| 6974 | |
| 6975 | if (!perf_event_switch_match(event)) |
| 6976 | return; |
| 6977 | |
| 6978 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ |
| 6979 | if (event->ctx->task) { |
| 6980 | se->event_id.header.type = PERF_RECORD_SWITCH; |
| 6981 | se->event_id.header.size = sizeof(se->event_id.header); |
| 6982 | } else { |
| 6983 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; |
| 6984 | se->event_id.header.size = sizeof(se->event_id); |
| 6985 | se->event_id.next_prev_pid = |
| 6986 | perf_event_pid(event, se->next_prev); |
| 6987 | se->event_id.next_prev_tid = |
| 6988 | perf_event_tid(event, se->next_prev); |
| 6989 | } |
| 6990 | |
| 6991 | perf_event_header__init_id(&se->event_id.header, &sample, event); |
| 6992 | |
| 6993 | ret = perf_output_begin(&handle, event, se->event_id.header.size); |
| 6994 | if (ret) |
| 6995 | return; |
| 6996 | |
| 6997 | if (event->ctx->task) |
| 6998 | perf_output_put(&handle, se->event_id.header); |
| 6999 | else |
| 7000 | perf_output_put(&handle, se->event_id); |
| 7001 | |
| 7002 | perf_event__output_id_sample(event, &handle, &sample); |
| 7003 | |
| 7004 | perf_output_end(&handle); |
| 7005 | } |
| 7006 | |
| 7007 | static void perf_event_switch(struct task_struct *task, |
| 7008 | struct task_struct *next_prev, bool sched_in) |
| 7009 | { |
| 7010 | struct perf_switch_event switch_event; |
| 7011 | |
| 7012 | /* N.B. caller checks nr_switch_events != 0 */ |
| 7013 | |
| 7014 | switch_event = (struct perf_switch_event){ |
| 7015 | .task = task, |
| 7016 | .next_prev = next_prev, |
| 7017 | .event_id = { |
| 7018 | .header = { |
| 7019 | /* .type */ |
| 7020 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, |
| 7021 | /* .size */ |
| 7022 | }, |
| 7023 | /* .next_prev_pid */ |
| 7024 | /* .next_prev_tid */ |
| 7025 | }, |
| 7026 | }; |
| 7027 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 7028 | perf_iterate_sb(perf_event_switch_output, |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 7029 | &switch_event, |
| 7030 | NULL); |
| 7031 | } |
| 7032 | |
| 7033 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7034 | * IRQ throttle logging |
| 7035 | */ |
| 7036 | |
| 7037 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 7038 | { |
| 7039 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 7040 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7041 | int ret; |
| 7042 | |
| 7043 | struct { |
| 7044 | struct perf_event_header header; |
| 7045 | u64 time; |
| 7046 | u64 id; |
| 7047 | u64 stream_id; |
| 7048 | } throttle_event = { |
| 7049 | .header = { |
| 7050 | .type = PERF_RECORD_THROTTLE, |
| 7051 | .misc = 0, |
| 7052 | .size = sizeof(throttle_event), |
| 7053 | }, |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 7054 | .time = perf_event_clock(event), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7055 | .id = primary_event_id(event), |
| 7056 | .stream_id = event->id, |
| 7057 | }; |
| 7058 | |
| 7059 | if (enable) |
| 7060 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 7061 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 7062 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 7063 | |
| 7064 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 7065 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7066 | if (ret) |
| 7067 | return; |
| 7068 | |
| 7069 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 7070 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7071 | perf_output_end(&handle); |
| 7072 | } |
| 7073 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 7074 | static void perf_log_itrace_start(struct perf_event *event) |
| 7075 | { |
| 7076 | struct perf_output_handle handle; |
| 7077 | struct perf_sample_data sample; |
| 7078 | struct perf_aux_event { |
| 7079 | struct perf_event_header header; |
| 7080 | u32 pid; |
| 7081 | u32 tid; |
| 7082 | } rec; |
| 7083 | int ret; |
| 7084 | |
| 7085 | if (event->parent) |
| 7086 | event = event->parent; |
| 7087 | |
| 7088 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || |
| 7089 | event->hw.itrace_started) |
| 7090 | return; |
| 7091 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 7092 | rec.header.type = PERF_RECORD_ITRACE_START; |
| 7093 | rec.header.misc = 0; |
| 7094 | rec.header.size = sizeof(rec); |
| 7095 | rec.pid = perf_event_pid(event, current); |
| 7096 | rec.tid = perf_event_tid(event, current); |
| 7097 | |
| 7098 | perf_event_header__init_id(&rec.header, &sample, event); |
| 7099 | ret = perf_output_begin(&handle, event, rec.header.size); |
| 7100 | |
| 7101 | if (ret) |
| 7102 | return; |
| 7103 | |
| 7104 | perf_output_put(&handle, rec); |
| 7105 | perf_event__output_id_sample(event, &handle, &sample); |
| 7106 | |
| 7107 | perf_output_end(&handle); |
| 7108 | } |
| 7109 | |
Jiri Olsa | a88ff23 | 2016-12-28 14:31:03 +0100 | [diff] [blame] | 7110 | static int |
| 7111 | __perf_event_account_interrupt(struct perf_event *event, int throttle) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7112 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7113 | struct hw_perf_event *hwc = &event->hw; |
| 7114 | int ret = 0; |
Jiri Olsa | a88ff23 | 2016-12-28 14:31:03 +0100 | [diff] [blame] | 7115 | u64 seq; |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 7116 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 7117 | seq = __this_cpu_read(perf_throttled_seq); |
| 7118 | if (seq != hwc->interrupts_seq) { |
| 7119 | hwc->interrupts_seq = seq; |
| 7120 | hwc->interrupts = 1; |
| 7121 | } else { |
| 7122 | hwc->interrupts++; |
| 7123 | if (unlikely(throttle |
| 7124 | && hwc->interrupts >= max_samples_per_tick)) { |
| 7125 | __this_cpu_inc(perf_throttled_count); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 7126 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 7127 | hwc->interrupts = MAX_INTERRUPTS; |
| 7128 | perf_log_throttle(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7129 | ret = 1; |
| 7130 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 7131 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7132 | |
| 7133 | if (event->attr.freq) { |
| 7134 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 7135 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7136 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 7137 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7138 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 7139 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 7140 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7141 | } |
| 7142 | |
Jiri Olsa | a88ff23 | 2016-12-28 14:31:03 +0100 | [diff] [blame] | 7143 | return ret; |
| 7144 | } |
| 7145 | |
| 7146 | int perf_event_account_interrupt(struct perf_event *event) |
| 7147 | { |
| 7148 | return __perf_event_account_interrupt(event, 1); |
| 7149 | } |
| 7150 | |
| 7151 | /* |
| 7152 | * Generic event overflow handling, sampling. |
| 7153 | */ |
| 7154 | |
| 7155 | static int __perf_event_overflow(struct perf_event *event, |
| 7156 | int throttle, struct perf_sample_data *data, |
| 7157 | struct pt_regs *regs) |
| 7158 | { |
| 7159 | int events = atomic_read(&event->event_limit); |
| 7160 | int ret = 0; |
| 7161 | |
| 7162 | /* |
| 7163 | * Non-sampling counters might still use the PMI to fold short |
| 7164 | * hardware counters, ignore those. |
| 7165 | */ |
| 7166 | if (unlikely(!is_sampling_event(event))) |
| 7167 | return 0; |
| 7168 | |
| 7169 | ret = __perf_event_account_interrupt(event, throttle); |
| 7170 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7171 | /* |
| 7172 | * XXX event_limit might not quite work as expected on inherited |
| 7173 | * events |
| 7174 | */ |
| 7175 | |
| 7176 | event->pending_kill = POLL_IN; |
| 7177 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 7178 | ret = 1; |
| 7179 | event->pending_kill = POLL_HUP; |
Jiri Olsa | 5aab90c | 2016-10-26 11:48:24 +0200 | [diff] [blame] | 7180 | |
| 7181 | perf_event_disable_inatomic(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7182 | } |
| 7183 | |
Alexei Starovoitov | aa6a5f3 | 2016-09-01 18:37:24 -0700 | [diff] [blame] | 7184 | READ_ONCE(event->overflow_handler)(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 7185 | |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 7186 | if (*perf_event_fasync(event) && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7187 | event->pending_wakeup = 1; |
| 7188 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 7189 | } |
| 7190 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7191 | return ret; |
| 7192 | } |
| 7193 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7194 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7195 | struct perf_sample_data *data, |
| 7196 | struct pt_regs *regs) |
| 7197 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7198 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7199 | } |
| 7200 | |
| 7201 | /* |
| 7202 | * Generic software event infrastructure |
| 7203 | */ |
| 7204 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7205 | struct swevent_htable { |
| 7206 | struct swevent_hlist *swevent_hlist; |
| 7207 | struct mutex hlist_mutex; |
| 7208 | int hlist_refcount; |
| 7209 | |
| 7210 | /* Recursion avoidance in each contexts */ |
| 7211 | int recursion[PERF_NR_CONTEXTS]; |
| 7212 | }; |
| 7213 | |
| 7214 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 7215 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7216 | /* |
| 7217 | * We directly increment event->count and keep a second value in |
| 7218 | * event->hw.period_left to count intervals. This period event |
| 7219 | * is kept in the range [-sample_period, 0] so that we can use the |
| 7220 | * sign as trigger. |
| 7221 | */ |
| 7222 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 7223 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7224 | { |
| 7225 | struct hw_perf_event *hwc = &event->hw; |
| 7226 | u64 period = hwc->last_period; |
| 7227 | u64 nr, offset; |
| 7228 | s64 old, val; |
| 7229 | |
| 7230 | hwc->last_period = hwc->sample_period; |
| 7231 | |
| 7232 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 7233 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7234 | if (val < 0) |
| 7235 | return 0; |
| 7236 | |
| 7237 | nr = div64_u64(period + val, period); |
| 7238 | offset = nr * period; |
| 7239 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 7240 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7241 | goto again; |
| 7242 | |
| 7243 | return nr; |
| 7244 | } |
| 7245 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 7246 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7247 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7248 | struct pt_regs *regs) |
| 7249 | { |
| 7250 | struct hw_perf_event *hwc = &event->hw; |
| 7251 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7252 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 7253 | if (!overflow) |
| 7254 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7255 | |
| 7256 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 7257 | return; |
| 7258 | |
| 7259 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7260 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7261 | data, regs)) { |
| 7262 | /* |
| 7263 | * We inhibit the overflow from happening when |
| 7264 | * hwc->interrupts == MAX_INTERRUPTS. |
| 7265 | */ |
| 7266 | break; |
| 7267 | } |
| 7268 | throttle = 1; |
| 7269 | } |
| 7270 | } |
| 7271 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7272 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7273 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7274 | struct pt_regs *regs) |
| 7275 | { |
| 7276 | struct hw_perf_event *hwc = &event->hw; |
| 7277 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 7278 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7279 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7280 | if (!regs) |
| 7281 | return; |
| 7282 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 7283 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 7284 | return; |
| 7285 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 7286 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 7287 | data->period = nr; |
| 7288 | return perf_swevent_overflow(event, 1, data, regs); |
| 7289 | } else |
| 7290 | data->period = event->hw.last_period; |
| 7291 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 7292 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7293 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 7294 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 7295 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 7296 | return; |
| 7297 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7298 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7299 | } |
| 7300 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7301 | static int perf_exclude_event(struct perf_event *event, |
| 7302 | struct pt_regs *regs) |
| 7303 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7304 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 7305 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7306 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7307 | if (regs) { |
| 7308 | if (event->attr.exclude_user && user_mode(regs)) |
| 7309 | return 1; |
| 7310 | |
| 7311 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 7312 | return 1; |
| 7313 | } |
| 7314 | |
| 7315 | return 0; |
| 7316 | } |
| 7317 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7318 | static int perf_swevent_match(struct perf_event *event, |
| 7319 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7320 | u32 event_id, |
| 7321 | struct perf_sample_data *data, |
| 7322 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7323 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7324 | if (event->attr.type != type) |
| 7325 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7326 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7327 | if (event->attr.config != event_id) |
| 7328 | return 0; |
| 7329 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7330 | if (perf_exclude_event(event, regs)) |
| 7331 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7332 | |
| 7333 | return 1; |
| 7334 | } |
| 7335 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7336 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7337 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7338 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7339 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7340 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 7341 | } |
| 7342 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7343 | static inline struct hlist_head * |
| 7344 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7345 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7346 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7347 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7348 | return &hlist->heads[hash]; |
| 7349 | } |
| 7350 | |
| 7351 | /* For the read side: events when they trigger */ |
| 7352 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7353 | 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] | 7354 | { |
| 7355 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7356 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7357 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7358 | if (!hlist) |
| 7359 | return NULL; |
| 7360 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7361 | return __find_swevent_head(hlist, type, event_id); |
| 7362 | } |
| 7363 | |
| 7364 | /* For the event head insertion and removal in the hlist */ |
| 7365 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7366 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7367 | { |
| 7368 | struct swevent_hlist *hlist; |
| 7369 | u32 event_id = event->attr.config; |
| 7370 | u64 type = event->attr.type; |
| 7371 | |
| 7372 | /* |
| 7373 | * Event scheduling is always serialized against hlist allocation |
| 7374 | * and release. Which makes the protected version suitable here. |
| 7375 | * The context lock guarantees that. |
| 7376 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7377 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7378 | lockdep_is_held(&event->ctx->lock)); |
| 7379 | if (!hlist) |
| 7380 | return NULL; |
| 7381 | |
| 7382 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7383 | } |
| 7384 | |
| 7385 | 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] | 7386 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7387 | struct perf_sample_data *data, |
| 7388 | struct pt_regs *regs) |
| 7389 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7390 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7391 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7392 | struct hlist_head *head; |
| 7393 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7394 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7395 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7396 | if (!head) |
| 7397 | goto end; |
| 7398 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7399 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7400 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7401 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7402 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7403 | end: |
| 7404 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7405 | } |
| 7406 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7407 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
| 7408 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7409 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7410 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7411 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 7412 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7413 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7414 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 7415 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7416 | |
Alexei Starovoitov | 98b5c2c | 2016-04-06 18:43:25 -0700 | [diff] [blame] | 7417 | void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7418 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7419 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 7420 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7421 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 7422 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7423 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7424 | 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] | 7425 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 7426 | struct perf_sample_data data; |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7427 | |
| 7428 | if (WARN_ON_ONCE(!regs)) |
| 7429 | return; |
| 7430 | |
| 7431 | perf_sample_data_init(&data, addr, 0); |
| 7432 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
| 7433 | } |
| 7434 | |
| 7435 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
| 7436 | { |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7437 | int rctx; |
| 7438 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7439 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7440 | rctx = perf_swevent_get_recursion_context(); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7441 | if (unlikely(rctx < 0)) |
| 7442 | goto fail; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7443 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7444 | ___perf_sw_event(event_id, nr, regs, addr); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7445 | |
| 7446 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7447 | fail: |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7448 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7449 | } |
| 7450 | |
| 7451 | static void perf_swevent_read(struct perf_event *event) |
| 7452 | { |
| 7453 | } |
| 7454 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7455 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7456 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7457 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7458 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7459 | struct hlist_head *head; |
| 7460 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 7461 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7462 | hwc->last_period = hwc->sample_period; |
| 7463 | perf_swevent_set_period(event); |
| 7464 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7465 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7466 | hwc->state = !(flags & PERF_EF_START); |
| 7467 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7468 | head = find_swevent_head(swhash, event); |
Peter Zijlstra | 12ca6ad | 2015-12-15 13:49:05 +0100 | [diff] [blame] | 7469 | if (WARN_ON_ONCE(!head)) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7470 | return -EINVAL; |
| 7471 | |
| 7472 | hlist_add_head_rcu(&event->hlist_entry, head); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 7473 | perf_event_update_userpage(event); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7474 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7475 | return 0; |
| 7476 | } |
| 7477 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7478 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7479 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7480 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7481 | } |
| 7482 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7483 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7484 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7485 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7486 | } |
| 7487 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7488 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7489 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7490 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7491 | } |
| 7492 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7493 | /* Deref the hlist from the update side */ |
| 7494 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7495 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7496 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7497 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 7498 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7499 | } |
| 7500 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7501 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7502 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7503 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7504 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7505 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7506 | return; |
| 7507 | |
Andreea-Cristina Bernat | 70691d4 | 2014-08-22 16:26:05 +0300 | [diff] [blame] | 7508 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 7509 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7510 | } |
| 7511 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7512 | static void swevent_hlist_put_cpu(int cpu) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7513 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7514 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7515 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7516 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7517 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7518 | if (!--swhash->hlist_refcount) |
| 7519 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7520 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7521 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7522 | } |
| 7523 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7524 | static void swevent_hlist_put(void) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7525 | { |
| 7526 | int cpu; |
| 7527 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7528 | for_each_possible_cpu(cpu) |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7529 | swevent_hlist_put_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7530 | } |
| 7531 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7532 | static int swevent_hlist_get_cpu(int cpu) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7533 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7534 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7535 | int err = 0; |
| 7536 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7537 | mutex_lock(&swhash->hlist_mutex); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7538 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7539 | struct swevent_hlist *hlist; |
| 7540 | |
| 7541 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 7542 | if (!hlist) { |
| 7543 | err = -ENOMEM; |
| 7544 | goto exit; |
| 7545 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7546 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7547 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7548 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 7549 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7550 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7551 | |
| 7552 | return err; |
| 7553 | } |
| 7554 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7555 | static int swevent_hlist_get(void) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7556 | { |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7557 | int err, cpu, failed_cpu; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7558 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7559 | get_online_cpus(); |
| 7560 | for_each_possible_cpu(cpu) { |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7561 | err = swevent_hlist_get_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7562 | if (err) { |
| 7563 | failed_cpu = cpu; |
| 7564 | goto fail; |
| 7565 | } |
| 7566 | } |
| 7567 | put_online_cpus(); |
| 7568 | |
| 7569 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 7570 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7571 | for_each_possible_cpu(cpu) { |
| 7572 | if (cpu == failed_cpu) |
| 7573 | break; |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7574 | swevent_hlist_put_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7575 | } |
| 7576 | |
| 7577 | put_online_cpus(); |
| 7578 | return err; |
| 7579 | } |
| 7580 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 7581 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7582 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7583 | static void sw_perf_event_destroy(struct perf_event *event) |
| 7584 | { |
| 7585 | u64 event_id = event->attr.config; |
| 7586 | |
| 7587 | WARN_ON(event->parent); |
| 7588 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 7589 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7590 | swevent_hlist_put(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7591 | } |
| 7592 | |
| 7593 | static int perf_swevent_init(struct perf_event *event) |
| 7594 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 7595 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7596 | |
| 7597 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 7598 | return -ENOENT; |
| 7599 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 7600 | /* |
| 7601 | * no branch sampling for software events |
| 7602 | */ |
| 7603 | if (has_branch_stack(event)) |
| 7604 | return -EOPNOTSUPP; |
| 7605 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7606 | switch (event_id) { |
| 7607 | case PERF_COUNT_SW_CPU_CLOCK: |
| 7608 | case PERF_COUNT_SW_TASK_CLOCK: |
| 7609 | return -ENOENT; |
| 7610 | |
| 7611 | default: |
| 7612 | break; |
| 7613 | } |
| 7614 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 7615 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7616 | return -ENOENT; |
| 7617 | |
| 7618 | if (!event->parent) { |
| 7619 | int err; |
| 7620 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7621 | err = swevent_hlist_get(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7622 | if (err) |
| 7623 | return err; |
| 7624 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 7625 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7626 | event->destroy = sw_perf_event_destroy; |
| 7627 | } |
| 7628 | |
| 7629 | return 0; |
| 7630 | } |
| 7631 | |
| 7632 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7633 | .task_ctx_nr = perf_sw_context, |
| 7634 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 7635 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 7636 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7637 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7638 | .add = perf_swevent_add, |
| 7639 | .del = perf_swevent_del, |
| 7640 | .start = perf_swevent_start, |
| 7641 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7642 | .read = perf_swevent_read, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7643 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7644 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7645 | #ifdef CONFIG_EVENT_TRACING |
| 7646 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7647 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7648 | struct perf_sample_data *data) |
| 7649 | { |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 7650 | void *record = data->raw->frag.data; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7651 | |
Peter Zijlstra | b71b437 | 2015-11-02 10:50:51 +0100 | [diff] [blame] | 7652 | /* only top level events have filters set */ |
| 7653 | if (event->parent) |
| 7654 | event = event->parent; |
| 7655 | |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7656 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 7657 | return 1; |
| 7658 | return 0; |
| 7659 | } |
| 7660 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7661 | static int perf_tp_event_match(struct perf_event *event, |
| 7662 | struct perf_sample_data *data, |
| 7663 | struct pt_regs *regs) |
| 7664 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 7665 | if (event->hw.state & PERF_HES_STOPPED) |
| 7666 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 7667 | /* |
| 7668 | * All tracepoints are from kernel-space. |
| 7669 | */ |
| 7670 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7671 | return 0; |
| 7672 | |
| 7673 | if (!perf_tp_filter_match(event, data)) |
| 7674 | return 0; |
| 7675 | |
| 7676 | return 1; |
| 7677 | } |
| 7678 | |
Alexei Starovoitov | 85b67bc | 2016-04-18 20:11:50 -0700 | [diff] [blame] | 7679 | void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx, |
| 7680 | struct trace_event_call *call, u64 count, |
| 7681 | struct pt_regs *regs, struct hlist_head *head, |
| 7682 | struct task_struct *task) |
| 7683 | { |
| 7684 | struct bpf_prog *prog = call->prog; |
| 7685 | |
| 7686 | if (prog) { |
| 7687 | *(struct pt_regs **)raw_data = regs; |
| 7688 | if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) { |
| 7689 | perf_swevent_put_recursion_context(rctx); |
| 7690 | return; |
| 7691 | } |
| 7692 | } |
| 7693 | perf_tp_event(call->event.type, count, raw_data, size, regs, head, |
| 7694 | rctx, task); |
| 7695 | } |
| 7696 | EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit); |
| 7697 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 7698 | void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 7699 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 7700 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7701 | { |
| 7702 | struct perf_sample_data data; |
| 7703 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7704 | |
| 7705 | struct perf_raw_record raw = { |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 7706 | .frag = { |
| 7707 | .size = entry_size, |
| 7708 | .data = record, |
| 7709 | }, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7710 | }; |
| 7711 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 7712 | perf_sample_data_init(&data, 0, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7713 | data.raw = &raw; |
| 7714 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 7715 | perf_trace_buf_update(record, event_type); |
| 7716 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7717 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7718 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7719 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7720 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 7721 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 7722 | /* |
| 7723 | * If we got specified a target task, also iterate its context and |
| 7724 | * deliver this event there too. |
| 7725 | */ |
| 7726 | if (task && task != current) { |
| 7727 | struct perf_event_context *ctx; |
| 7728 | struct trace_entry *entry = record; |
| 7729 | |
| 7730 | rcu_read_lock(); |
| 7731 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 7732 | if (!ctx) |
| 7733 | goto unlock; |
| 7734 | |
| 7735 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 7736 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7737 | continue; |
| 7738 | if (event->attr.config != entry->type) |
| 7739 | continue; |
| 7740 | if (perf_tp_event_match(event, &data, regs)) |
| 7741 | perf_swevent_event(event, count, &data, regs); |
| 7742 | } |
| 7743 | unlock: |
| 7744 | rcu_read_unlock(); |
| 7745 | } |
| 7746 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 7747 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7748 | } |
| 7749 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 7750 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7751 | static void tp_perf_event_destroy(struct perf_event *event) |
| 7752 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7753 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7754 | } |
| 7755 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7756 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7757 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7758 | int err; |
| 7759 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7760 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7761 | return -ENOENT; |
| 7762 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 7763 | /* |
| 7764 | * no branch sampling for tracepoint events |
| 7765 | */ |
| 7766 | if (has_branch_stack(event)) |
| 7767 | return -EOPNOTSUPP; |
| 7768 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7769 | err = perf_trace_init(event); |
| 7770 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7771 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7772 | |
| 7773 | event->destroy = tp_perf_event_destroy; |
| 7774 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7775 | return 0; |
| 7776 | } |
| 7777 | |
| 7778 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7779 | .task_ctx_nr = perf_sw_context, |
| 7780 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7781 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7782 | .add = perf_trace_add, |
| 7783 | .del = perf_trace_del, |
| 7784 | .start = perf_swevent_start, |
| 7785 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7786 | .read = perf_swevent_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7787 | }; |
| 7788 | |
| 7789 | static inline void perf_tp_register(void) |
| 7790 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7791 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7792 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7793 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7794 | static void perf_event_free_filter(struct perf_event *event) |
| 7795 | { |
| 7796 | ftrace_profile_free_filter(event); |
| 7797 | } |
| 7798 | |
Alexei Starovoitov | aa6a5f3 | 2016-09-01 18:37:24 -0700 | [diff] [blame] | 7799 | #ifdef CONFIG_BPF_SYSCALL |
| 7800 | static void bpf_overflow_handler(struct perf_event *event, |
| 7801 | struct perf_sample_data *data, |
| 7802 | struct pt_regs *regs) |
| 7803 | { |
| 7804 | struct bpf_perf_event_data_kern ctx = { |
| 7805 | .data = data, |
| 7806 | .regs = regs, |
| 7807 | }; |
| 7808 | int ret = 0; |
| 7809 | |
| 7810 | preempt_disable(); |
| 7811 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) |
| 7812 | goto out; |
| 7813 | rcu_read_lock(); |
| 7814 | ret = BPF_PROG_RUN(event->prog, (void *)&ctx); |
| 7815 | rcu_read_unlock(); |
| 7816 | out: |
| 7817 | __this_cpu_dec(bpf_prog_active); |
| 7818 | preempt_enable(); |
| 7819 | if (!ret) |
| 7820 | return; |
| 7821 | |
| 7822 | event->orig_overflow_handler(event, data, regs); |
| 7823 | } |
| 7824 | |
| 7825 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) |
| 7826 | { |
| 7827 | struct bpf_prog *prog; |
| 7828 | |
| 7829 | if (event->overflow_handler_context) |
| 7830 | /* hw breakpoint or kernel counter */ |
| 7831 | return -EINVAL; |
| 7832 | |
| 7833 | if (event->prog) |
| 7834 | return -EEXIST; |
| 7835 | |
| 7836 | prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT); |
| 7837 | if (IS_ERR(prog)) |
| 7838 | return PTR_ERR(prog); |
| 7839 | |
| 7840 | event->prog = prog; |
| 7841 | event->orig_overflow_handler = READ_ONCE(event->overflow_handler); |
| 7842 | WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); |
| 7843 | return 0; |
| 7844 | } |
| 7845 | |
| 7846 | static void perf_event_free_bpf_handler(struct perf_event *event) |
| 7847 | { |
| 7848 | struct bpf_prog *prog = event->prog; |
| 7849 | |
| 7850 | if (!prog) |
| 7851 | return; |
| 7852 | |
| 7853 | WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); |
| 7854 | event->prog = NULL; |
| 7855 | bpf_prog_put(prog); |
| 7856 | } |
| 7857 | #else |
| 7858 | static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) |
| 7859 | { |
| 7860 | return -EOPNOTSUPP; |
| 7861 | } |
| 7862 | static void perf_event_free_bpf_handler(struct perf_event *event) |
| 7863 | { |
| 7864 | } |
| 7865 | #endif |
| 7866 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7867 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
| 7868 | { |
Alexei Starovoitov | 98b5c2c | 2016-04-06 18:43:25 -0700 | [diff] [blame] | 7869 | bool is_kprobe, is_tracepoint; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7870 | struct bpf_prog *prog; |
| 7871 | |
Alexei Starovoitov | aa6a5f3 | 2016-09-01 18:37:24 -0700 | [diff] [blame] | 7872 | if (event->attr.type == PERF_TYPE_HARDWARE || |
| 7873 | event->attr.type == PERF_TYPE_SOFTWARE) |
| 7874 | return perf_event_set_bpf_handler(event, prog_fd); |
| 7875 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7876 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7877 | return -EINVAL; |
| 7878 | |
| 7879 | if (event->tp_event->prog) |
| 7880 | return -EEXIST; |
| 7881 | |
Alexei Starovoitov | 98b5c2c | 2016-04-06 18:43:25 -0700 | [diff] [blame] | 7882 | is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE; |
| 7883 | is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; |
| 7884 | if (!is_kprobe && !is_tracepoint) |
| 7885 | /* bpf programs can only be attached to u/kprobe or tracepoint */ |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7886 | return -EINVAL; |
| 7887 | |
| 7888 | prog = bpf_prog_get(prog_fd); |
| 7889 | if (IS_ERR(prog)) |
| 7890 | return PTR_ERR(prog); |
| 7891 | |
Alexei Starovoitov | 98b5c2c | 2016-04-06 18:43:25 -0700 | [diff] [blame] | 7892 | if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) || |
| 7893 | (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7894 | /* valid fd, but invalid bpf program type */ |
| 7895 | bpf_prog_put(prog); |
| 7896 | return -EINVAL; |
| 7897 | } |
| 7898 | |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 7899 | if (is_tracepoint) { |
| 7900 | int off = trace_event_get_offsets(event->tp_event); |
| 7901 | |
| 7902 | if (prog->aux->max_ctx_offset > off) { |
| 7903 | bpf_prog_put(prog); |
| 7904 | return -EACCES; |
| 7905 | } |
| 7906 | } |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7907 | event->tp_event->prog = prog; |
Yonghong Song | 0dee549 | 2017-09-18 16:38:36 -0700 | [diff] [blame] | 7908 | event->tp_event->bpf_prog_owner = event; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7909 | |
| 7910 | return 0; |
| 7911 | } |
| 7912 | |
| 7913 | static void perf_event_free_bpf_prog(struct perf_event *event) |
| 7914 | { |
| 7915 | struct bpf_prog *prog; |
| 7916 | |
Alexei Starovoitov | aa6a5f3 | 2016-09-01 18:37:24 -0700 | [diff] [blame] | 7917 | perf_event_free_bpf_handler(event); |
| 7918 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7919 | if (!event->tp_event) |
| 7920 | return; |
| 7921 | |
| 7922 | prog = event->tp_event->prog; |
Yonghong Song | 0dee549 | 2017-09-18 16:38:36 -0700 | [diff] [blame] | 7923 | if (prog && event->tp_event->bpf_prog_owner == event) { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7924 | event->tp_event->prog = NULL; |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 7925 | bpf_prog_put(prog); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7926 | } |
| 7927 | } |
| 7928 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7929 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7930 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7931 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7932 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7933 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7934 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7935 | static void perf_event_free_filter(struct perf_event *event) |
| 7936 | { |
| 7937 | } |
| 7938 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7939 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
| 7940 | { |
| 7941 | return -ENOENT; |
| 7942 | } |
| 7943 | |
| 7944 | static void perf_event_free_bpf_prog(struct perf_event *event) |
| 7945 | { |
| 7946 | } |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 7947 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7948 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7949 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7950 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7951 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7952 | struct perf_sample_data sample; |
| 7953 | struct pt_regs *regs = data; |
| 7954 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7955 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7956 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7957 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7958 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7959 | } |
| 7960 | #endif |
| 7961 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 7962 | /* |
| 7963 | * Allocate a new address filter |
| 7964 | */ |
| 7965 | static struct perf_addr_filter * |
| 7966 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) |
| 7967 | { |
| 7968 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); |
| 7969 | struct perf_addr_filter *filter; |
| 7970 | |
| 7971 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); |
| 7972 | if (!filter) |
| 7973 | return NULL; |
| 7974 | |
| 7975 | INIT_LIST_HEAD(&filter->entry); |
| 7976 | list_add_tail(&filter->entry, filters); |
| 7977 | |
| 7978 | return filter; |
| 7979 | } |
| 7980 | |
| 7981 | static void free_filters_list(struct list_head *filters) |
| 7982 | { |
| 7983 | struct perf_addr_filter *filter, *iter; |
| 7984 | |
| 7985 | list_for_each_entry_safe(filter, iter, filters, entry) { |
| 7986 | if (filter->inode) |
| 7987 | iput(filter->inode); |
| 7988 | list_del(&filter->entry); |
| 7989 | kfree(filter); |
| 7990 | } |
| 7991 | } |
| 7992 | |
| 7993 | /* |
| 7994 | * Free existing address filters and optionally install new ones |
| 7995 | */ |
| 7996 | static void perf_addr_filters_splice(struct perf_event *event, |
| 7997 | struct list_head *head) |
| 7998 | { |
| 7999 | unsigned long flags; |
| 8000 | LIST_HEAD(list); |
| 8001 | |
| 8002 | if (!has_addr_filter(event)) |
| 8003 | return; |
| 8004 | |
| 8005 | /* don't bother with children, they don't have their own filters */ |
| 8006 | if (event->parent) |
| 8007 | return; |
| 8008 | |
| 8009 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); |
| 8010 | |
| 8011 | list_splice_init(&event->addr_filters.list, &list); |
| 8012 | if (head) |
| 8013 | list_splice(head, &event->addr_filters.list); |
| 8014 | |
| 8015 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); |
| 8016 | |
| 8017 | free_filters_list(&list); |
| 8018 | } |
| 8019 | |
| 8020 | /* |
| 8021 | * Scan through mm's vmas and see if one of them matches the |
| 8022 | * @filter; if so, adjust filter's address range. |
| 8023 | * Called with mm::mmap_sem down for reading. |
| 8024 | */ |
| 8025 | static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter, |
| 8026 | struct mm_struct *mm) |
| 8027 | { |
| 8028 | struct vm_area_struct *vma; |
| 8029 | |
| 8030 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 8031 | struct file *file = vma->vm_file; |
| 8032 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; |
| 8033 | unsigned long vma_size = vma->vm_end - vma->vm_start; |
| 8034 | |
| 8035 | if (!file) |
| 8036 | continue; |
| 8037 | |
| 8038 | if (!perf_addr_filter_match(filter, file, off, vma_size)) |
| 8039 | continue; |
| 8040 | |
| 8041 | return vma->vm_start; |
| 8042 | } |
| 8043 | |
| 8044 | return 0; |
| 8045 | } |
| 8046 | |
| 8047 | /* |
| 8048 | * Update event's address range filters based on the |
| 8049 | * task's existing mappings, if any. |
| 8050 | */ |
| 8051 | static void perf_event_addr_filters_apply(struct perf_event *event) |
| 8052 | { |
| 8053 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 8054 | struct task_struct *task = READ_ONCE(event->ctx->task); |
| 8055 | struct perf_addr_filter *filter; |
| 8056 | struct mm_struct *mm = NULL; |
| 8057 | unsigned int count = 0; |
| 8058 | unsigned long flags; |
| 8059 | |
| 8060 | /* |
| 8061 | * We may observe TASK_TOMBSTONE, which means that the event tear-down |
| 8062 | * will stop on the parent's child_mutex that our caller is also holding |
| 8063 | */ |
| 8064 | if (task == TASK_TOMBSTONE) |
| 8065 | return; |
| 8066 | |
| 8067 | mm = get_task_mm(event->ctx->task); |
| 8068 | if (!mm) |
| 8069 | goto restart; |
| 8070 | |
| 8071 | down_read(&mm->mmap_sem); |
| 8072 | |
| 8073 | raw_spin_lock_irqsave(&ifh->lock, flags); |
| 8074 | list_for_each_entry(filter, &ifh->list, entry) { |
| 8075 | event->addr_filters_offs[count] = 0; |
| 8076 | |
Mathieu Poirier | 99f5bc9 | 2016-07-18 10:43:07 -0600 | [diff] [blame] | 8077 | /* |
| 8078 | * Adjust base offset if the filter is associated to a binary |
| 8079 | * that needs to be mapped: |
| 8080 | */ |
| 8081 | if (filter->inode) |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8082 | event->addr_filters_offs[count] = |
| 8083 | perf_addr_filter_apply(filter, mm); |
| 8084 | |
| 8085 | count++; |
| 8086 | } |
| 8087 | |
| 8088 | event->addr_filters_gen++; |
| 8089 | raw_spin_unlock_irqrestore(&ifh->lock, flags); |
| 8090 | |
| 8091 | up_read(&mm->mmap_sem); |
| 8092 | |
| 8093 | mmput(mm); |
| 8094 | |
| 8095 | restart: |
Alexander Shishkin | 767ae08 | 2016-09-06 16:23:49 +0300 | [diff] [blame] | 8096 | perf_event_stop(event, 1); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8097 | } |
| 8098 | |
| 8099 | /* |
| 8100 | * Address range filtering: limiting the data to certain |
| 8101 | * instruction address ranges. Filters are ioctl()ed to us from |
| 8102 | * userspace as ascii strings. |
| 8103 | * |
| 8104 | * Filter string format: |
| 8105 | * |
| 8106 | * ACTION RANGE_SPEC |
| 8107 | * where ACTION is one of the |
| 8108 | * * "filter": limit the trace to this region |
| 8109 | * * "start": start tracing from this address |
| 8110 | * * "stop": stop tracing at this address/region; |
| 8111 | * RANGE_SPEC is |
| 8112 | * * for kernel addresses: <start address>[/<size>] |
| 8113 | * * for object files: <start address>[/<size>]@</path/to/object/file> |
| 8114 | * |
| 8115 | * if <size> is not specified, the range is treated as a single address. |
| 8116 | */ |
| 8117 | enum { |
Alexander Shishkin | e96271f | 2016-11-18 13:38:43 +0200 | [diff] [blame] | 8118 | IF_ACT_NONE = -1, |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8119 | IF_ACT_FILTER, |
| 8120 | IF_ACT_START, |
| 8121 | IF_ACT_STOP, |
| 8122 | IF_SRC_FILE, |
| 8123 | IF_SRC_KERNEL, |
| 8124 | IF_SRC_FILEADDR, |
| 8125 | IF_SRC_KERNELADDR, |
| 8126 | }; |
| 8127 | |
| 8128 | enum { |
| 8129 | IF_STATE_ACTION = 0, |
| 8130 | IF_STATE_SOURCE, |
| 8131 | IF_STATE_END, |
| 8132 | }; |
| 8133 | |
| 8134 | static const match_table_t if_tokens = { |
| 8135 | { IF_ACT_FILTER, "filter" }, |
| 8136 | { IF_ACT_START, "start" }, |
| 8137 | { IF_ACT_STOP, "stop" }, |
| 8138 | { IF_SRC_FILE, "%u/%u@%s" }, |
| 8139 | { IF_SRC_KERNEL, "%u/%u" }, |
| 8140 | { IF_SRC_FILEADDR, "%u@%s" }, |
| 8141 | { IF_SRC_KERNELADDR, "%u" }, |
Alexander Shishkin | e96271f | 2016-11-18 13:38:43 +0200 | [diff] [blame] | 8142 | { IF_ACT_NONE, NULL }, |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8143 | }; |
| 8144 | |
| 8145 | /* |
| 8146 | * Address filter string parser |
| 8147 | */ |
| 8148 | static int |
| 8149 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, |
| 8150 | struct list_head *filters) |
| 8151 | { |
| 8152 | struct perf_addr_filter *filter = NULL; |
| 8153 | char *start, *orig, *filename = NULL; |
| 8154 | struct path path; |
| 8155 | substring_t args[MAX_OPT_ARGS]; |
| 8156 | int state = IF_STATE_ACTION, token; |
| 8157 | unsigned int kernel = 0; |
| 8158 | int ret = -EINVAL; |
| 8159 | |
| 8160 | orig = fstr = kstrdup(fstr, GFP_KERNEL); |
| 8161 | if (!fstr) |
| 8162 | return -ENOMEM; |
| 8163 | |
| 8164 | while ((start = strsep(&fstr, " ,\n")) != NULL) { |
| 8165 | ret = -EINVAL; |
| 8166 | |
| 8167 | if (!*start) |
| 8168 | continue; |
| 8169 | |
| 8170 | /* filter definition begins */ |
| 8171 | if (state == IF_STATE_ACTION) { |
| 8172 | filter = perf_addr_filter_new(event, filters); |
| 8173 | if (!filter) |
| 8174 | goto fail; |
| 8175 | } |
| 8176 | |
| 8177 | token = match_token(start, if_tokens, args); |
| 8178 | switch (token) { |
| 8179 | case IF_ACT_FILTER: |
| 8180 | case IF_ACT_START: |
| 8181 | filter->filter = 1; |
| 8182 | |
| 8183 | case IF_ACT_STOP: |
| 8184 | if (state != IF_STATE_ACTION) |
| 8185 | goto fail; |
| 8186 | |
| 8187 | state = IF_STATE_SOURCE; |
| 8188 | break; |
| 8189 | |
| 8190 | case IF_SRC_KERNELADDR: |
| 8191 | case IF_SRC_KERNEL: |
| 8192 | kernel = 1; |
| 8193 | |
| 8194 | case IF_SRC_FILEADDR: |
| 8195 | case IF_SRC_FILE: |
| 8196 | if (state != IF_STATE_SOURCE) |
| 8197 | goto fail; |
| 8198 | |
| 8199 | if (token == IF_SRC_FILE || token == IF_SRC_KERNEL) |
| 8200 | filter->range = 1; |
| 8201 | |
| 8202 | *args[0].to = 0; |
| 8203 | ret = kstrtoul(args[0].from, 0, &filter->offset); |
| 8204 | if (ret) |
| 8205 | goto fail; |
| 8206 | |
| 8207 | if (filter->range) { |
| 8208 | *args[1].to = 0; |
| 8209 | ret = kstrtoul(args[1].from, 0, &filter->size); |
| 8210 | if (ret) |
| 8211 | goto fail; |
| 8212 | } |
| 8213 | |
Mathieu Poirier | 4059ffd | 2016-07-18 10:43:05 -0600 | [diff] [blame] | 8214 | if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) { |
| 8215 | int fpos = filter->range ? 2 : 1; |
| 8216 | |
| 8217 | filename = match_strdup(&args[fpos]); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8218 | if (!filename) { |
| 8219 | ret = -ENOMEM; |
| 8220 | goto fail; |
| 8221 | } |
| 8222 | } |
| 8223 | |
| 8224 | state = IF_STATE_END; |
| 8225 | break; |
| 8226 | |
| 8227 | default: |
| 8228 | goto fail; |
| 8229 | } |
| 8230 | |
| 8231 | /* |
| 8232 | * Filter definition is fully parsed, validate and install it. |
| 8233 | * Make sure that it doesn't contradict itself or the event's |
| 8234 | * attribute. |
| 8235 | */ |
| 8236 | if (state == IF_STATE_END) { |
| 8237 | if (kernel && event->attr.exclude_kernel) |
| 8238 | goto fail; |
| 8239 | |
| 8240 | if (!kernel) { |
| 8241 | if (!filename) |
| 8242 | goto fail; |
| 8243 | |
| 8244 | /* look up the path and grab its inode */ |
| 8245 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); |
| 8246 | if (ret) |
| 8247 | goto fail_free_name; |
| 8248 | |
| 8249 | filter->inode = igrab(d_inode(path.dentry)); |
| 8250 | path_put(&path); |
| 8251 | kfree(filename); |
| 8252 | filename = NULL; |
| 8253 | |
| 8254 | ret = -EINVAL; |
| 8255 | if (!filter->inode || |
| 8256 | !S_ISREG(filter->inode->i_mode)) |
| 8257 | /* free_filters_list() will iput() */ |
| 8258 | goto fail; |
| 8259 | } |
| 8260 | |
| 8261 | /* ready to consume more filters */ |
| 8262 | state = IF_STATE_ACTION; |
| 8263 | filter = NULL; |
| 8264 | } |
| 8265 | } |
| 8266 | |
| 8267 | if (state != IF_STATE_ACTION) |
| 8268 | goto fail; |
| 8269 | |
| 8270 | kfree(orig); |
| 8271 | |
| 8272 | return 0; |
| 8273 | |
| 8274 | fail_free_name: |
| 8275 | kfree(filename); |
| 8276 | fail: |
| 8277 | free_filters_list(filters); |
| 8278 | kfree(orig); |
| 8279 | |
| 8280 | return ret; |
| 8281 | } |
| 8282 | |
| 8283 | static int |
| 8284 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) |
| 8285 | { |
| 8286 | LIST_HEAD(filters); |
| 8287 | int ret; |
| 8288 | |
| 8289 | /* |
| 8290 | * Since this is called in perf_ioctl() path, we're already holding |
| 8291 | * ctx::mutex. |
| 8292 | */ |
| 8293 | lockdep_assert_held(&event->ctx->mutex); |
| 8294 | |
| 8295 | if (WARN_ON_ONCE(event->parent)) |
| 8296 | return -EINVAL; |
| 8297 | |
| 8298 | /* |
| 8299 | * For now, we only support filtering in per-task events; doing so |
| 8300 | * for CPU-wide events requires additional context switching trickery, |
| 8301 | * since same object code will be mapped at different virtual |
| 8302 | * addresses in different processes. |
| 8303 | */ |
| 8304 | if (!event->ctx->task) |
| 8305 | return -EOPNOTSUPP; |
| 8306 | |
| 8307 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); |
| 8308 | if (ret) |
| 8309 | return ret; |
| 8310 | |
| 8311 | ret = event->pmu->addr_filters_validate(&filters); |
| 8312 | if (ret) { |
| 8313 | free_filters_list(&filters); |
| 8314 | return ret; |
| 8315 | } |
| 8316 | |
| 8317 | /* remove existing filters, if any */ |
| 8318 | perf_addr_filters_splice(event, &filters); |
| 8319 | |
| 8320 | /* install new filters */ |
| 8321 | perf_event_for_each_child(event, perf_event_addr_filters_apply); |
| 8322 | |
| 8323 | return ret; |
| 8324 | } |
| 8325 | |
Alexander Shishkin | c796bbb | 2016-04-27 18:44:42 +0300 | [diff] [blame] | 8326 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 8327 | { |
| 8328 | char *filter_str; |
| 8329 | int ret = -EINVAL; |
| 8330 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8331 | if ((event->attr.type != PERF_TYPE_TRACEPOINT || |
| 8332 | !IS_ENABLED(CONFIG_EVENT_TRACING)) && |
| 8333 | !has_addr_filter(event)) |
Alexander Shishkin | c796bbb | 2016-04-27 18:44:42 +0300 | [diff] [blame] | 8334 | return -EINVAL; |
| 8335 | |
| 8336 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 8337 | if (IS_ERR(filter_str)) |
| 8338 | return PTR_ERR(filter_str); |
| 8339 | |
| 8340 | if (IS_ENABLED(CONFIG_EVENT_TRACING) && |
| 8341 | event->attr.type == PERF_TYPE_TRACEPOINT) |
| 8342 | ret = ftrace_profile_set_filter(event, event->attr.config, |
| 8343 | filter_str); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8344 | else if (has_addr_filter(event)) |
| 8345 | ret = perf_event_set_addr_filter(event, filter_str); |
Alexander Shishkin | c796bbb | 2016-04-27 18:44:42 +0300 | [diff] [blame] | 8346 | |
| 8347 | kfree(filter_str); |
| 8348 | return ret; |
| 8349 | } |
| 8350 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8351 | /* |
| 8352 | * hrtimer based swevent callback |
| 8353 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8354 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8355 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8356 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8357 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 8358 | struct perf_sample_data data; |
| 8359 | struct pt_regs *regs; |
| 8360 | struct perf_event *event; |
| 8361 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8362 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8363 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8364 | |
| 8365 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 8366 | return HRTIMER_NORESTART; |
| 8367 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8368 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8369 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 8370 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8371 | regs = get_irq_regs(); |
| 8372 | |
| 8373 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 8374 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 8375 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8376 | ret = HRTIMER_NORESTART; |
| 8377 | } |
| 8378 | |
| 8379 | period = max_t(u64, 10000, event->hw.sample_period); |
| 8380 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 8381 | |
| 8382 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8383 | } |
| 8384 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8385 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8386 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8387 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 8388 | s64 period; |
| 8389 | |
| 8390 | if (!is_sampling_event(event)) |
| 8391 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8392 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 8393 | period = local64_read(&hwc->period_left); |
| 8394 | if (period) { |
| 8395 | if (period < 0) |
| 8396 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 8397 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 8398 | local64_set(&hwc->period_left, 0); |
| 8399 | } else { |
| 8400 | period = max_t(u64, 10000, hwc->sample_period); |
| 8401 | } |
Thomas Gleixner | 3497d20 | 2015-04-14 21:09:03 +0000 | [diff] [blame] | 8402 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
| 8403 | HRTIMER_MODE_REL_PINNED); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8404 | } |
| 8405 | |
| 8406 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 8407 | { |
| 8408 | struct hw_perf_event *hwc = &event->hw; |
| 8409 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 8410 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8411 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 8412 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8413 | |
| 8414 | hrtimer_cancel(&hwc->hrtimer); |
| 8415 | } |
| 8416 | } |
| 8417 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8418 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 8419 | { |
| 8420 | struct hw_perf_event *hwc = &event->hw; |
| 8421 | |
| 8422 | if (!is_sampling_event(event)) |
| 8423 | return; |
| 8424 | |
| 8425 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 8426 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 8427 | |
| 8428 | /* |
| 8429 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 8430 | * mapping and avoid the whole period adjust feedback stuff. |
| 8431 | */ |
| 8432 | if (event->attr.freq) { |
| 8433 | long freq = event->attr.sample_freq; |
| 8434 | |
| 8435 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 8436 | hwc->sample_period = event->attr.sample_period; |
| 8437 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 8438 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8439 | event->attr.freq = 0; |
| 8440 | } |
| 8441 | } |
| 8442 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8443 | /* |
| 8444 | * Software event: cpu wall time clock |
| 8445 | */ |
| 8446 | |
| 8447 | static void cpu_clock_event_update(struct perf_event *event) |
| 8448 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8449 | s64 prev; |
| 8450 | u64 now; |
| 8451 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8452 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8453 | prev = local64_xchg(&event->hw.prev_count, now); |
| 8454 | local64_add(now - prev, &event->count); |
| 8455 | } |
| 8456 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8457 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8458 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8459 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8460 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8461 | } |
| 8462 | |
| 8463 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 8464 | { |
| 8465 | perf_swevent_cancel_hrtimer(event); |
| 8466 | cpu_clock_event_update(event); |
| 8467 | } |
| 8468 | |
| 8469 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 8470 | { |
| 8471 | if (flags & PERF_EF_START) |
| 8472 | cpu_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 8473 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8474 | |
| 8475 | return 0; |
| 8476 | } |
| 8477 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8478 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8479 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8480 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8481 | } |
| 8482 | |
| 8483 | static void cpu_clock_event_read(struct perf_event *event) |
| 8484 | { |
| 8485 | cpu_clock_event_update(event); |
| 8486 | } |
| 8487 | |
| 8488 | static int cpu_clock_event_init(struct perf_event *event) |
| 8489 | { |
| 8490 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 8491 | return -ENOENT; |
| 8492 | |
| 8493 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 8494 | return -ENOENT; |
| 8495 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 8496 | /* |
| 8497 | * no branch sampling for software events |
| 8498 | */ |
| 8499 | if (has_branch_stack(event)) |
| 8500 | return -EOPNOTSUPP; |
| 8501 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8502 | perf_swevent_init_hrtimer(event); |
| 8503 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8504 | return 0; |
| 8505 | } |
| 8506 | |
| 8507 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8508 | .task_ctx_nr = perf_sw_context, |
| 8509 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8510 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 8511 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8512 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8513 | .add = cpu_clock_event_add, |
| 8514 | .del = cpu_clock_event_del, |
| 8515 | .start = cpu_clock_event_start, |
| 8516 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8517 | .read = cpu_clock_event_read, |
| 8518 | }; |
| 8519 | |
| 8520 | /* |
| 8521 | * Software event: task time clock |
| 8522 | */ |
| 8523 | |
| 8524 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 8525 | { |
| 8526 | u64 prev; |
| 8527 | s64 delta; |
| 8528 | |
| 8529 | prev = local64_xchg(&event->hw.prev_count, now); |
| 8530 | delta = now - prev; |
| 8531 | local64_add(delta, &event->count); |
| 8532 | } |
| 8533 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8534 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8535 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8536 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8537 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8538 | } |
| 8539 | |
| 8540 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 8541 | { |
| 8542 | perf_swevent_cancel_hrtimer(event); |
| 8543 | task_clock_event_update(event, event->ctx->time); |
| 8544 | } |
| 8545 | |
| 8546 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 8547 | { |
| 8548 | if (flags & PERF_EF_START) |
| 8549 | task_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 8550 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8551 | |
| 8552 | return 0; |
| 8553 | } |
| 8554 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8555 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8556 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8557 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8558 | } |
| 8559 | |
| 8560 | static void task_clock_event_read(struct perf_event *event) |
| 8561 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 8562 | u64 now = perf_clock(); |
| 8563 | u64 delta = now - event->ctx->timestamp; |
| 8564 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8565 | |
| 8566 | task_clock_event_update(event, time); |
| 8567 | } |
| 8568 | |
| 8569 | static int task_clock_event_init(struct perf_event *event) |
| 8570 | { |
| 8571 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 8572 | return -ENOENT; |
| 8573 | |
| 8574 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 8575 | return -ENOENT; |
| 8576 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 8577 | /* |
| 8578 | * no branch sampling for software events |
| 8579 | */ |
| 8580 | if (has_branch_stack(event)) |
| 8581 | return -EOPNOTSUPP; |
| 8582 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8583 | perf_swevent_init_hrtimer(event); |
| 8584 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8585 | return 0; |
| 8586 | } |
| 8587 | |
| 8588 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8589 | .task_ctx_nr = perf_sw_context, |
| 8590 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8591 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 8592 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8593 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8594 | .add = task_clock_event_add, |
| 8595 | .del = task_clock_event_del, |
| 8596 | .start = task_clock_event_start, |
| 8597 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8598 | .read = task_clock_event_read, |
| 8599 | }; |
| 8600 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8601 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 8602 | { |
| 8603 | } |
| 8604 | |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8605 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
| 8606 | { |
| 8607 | } |
| 8608 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8609 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 8610 | { |
| 8611 | return 0; |
| 8612 | } |
| 8613 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 8614 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8615 | |
| 8616 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8617 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8618 | __this_cpu_write(nop_txn_flags, flags); |
| 8619 | |
| 8620 | if (flags & ~PERF_PMU_TXN_ADD) |
| 8621 | return; |
| 8622 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8623 | perf_pmu_disable(pmu); |
| 8624 | } |
| 8625 | |
| 8626 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 8627 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8628 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
| 8629 | |
| 8630 | __this_cpu_write(nop_txn_flags, 0); |
| 8631 | |
| 8632 | if (flags & ~PERF_PMU_TXN_ADD) |
| 8633 | return 0; |
| 8634 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8635 | perf_pmu_enable(pmu); |
| 8636 | return 0; |
| 8637 | } |
| 8638 | |
| 8639 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 8640 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8641 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
| 8642 | |
| 8643 | __this_cpu_write(nop_txn_flags, 0); |
| 8644 | |
| 8645 | if (flags & ~PERF_PMU_TXN_ADD) |
| 8646 | return; |
| 8647 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8648 | perf_pmu_enable(pmu); |
| 8649 | } |
| 8650 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 8651 | static int perf_event_idx_default(struct perf_event *event) |
| 8652 | { |
Peter Zijlstra | c719f56 | 2014-10-21 11:10:21 +0200 | [diff] [blame] | 8653 | return 0; |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 8654 | } |
| 8655 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8656 | /* |
| 8657 | * Ensures all contexts with the same task_ctx_nr have the same |
| 8658 | * pmu_cpu_context too. |
| 8659 | */ |
Mark Rutland | 9e31704 | 2014-02-10 17:44:18 +0000 | [diff] [blame] | 8660 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8661 | { |
| 8662 | struct pmu *pmu; |
| 8663 | |
| 8664 | if (ctxn < 0) |
| 8665 | return NULL; |
| 8666 | |
| 8667 | list_for_each_entry(pmu, &pmus, entry) { |
| 8668 | if (pmu->task_ctx_nr == ctxn) |
| 8669 | return pmu->pmu_cpu_context; |
| 8670 | } |
| 8671 | |
| 8672 | return NULL; |
| 8673 | } |
| 8674 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8675 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8676 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8677 | int cpu; |
| 8678 | |
| 8679 | for_each_possible_cpu(cpu) { |
| 8680 | struct perf_cpu_context *cpuctx; |
| 8681 | |
| 8682 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 8683 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 8684 | if (cpuctx->unique_pmu == old_pmu) |
| 8685 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8686 | } |
| 8687 | } |
| 8688 | |
| 8689 | static void free_pmu_context(struct pmu *pmu) |
| 8690 | { |
| 8691 | struct pmu *i; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8692 | |
| 8693 | mutex_lock(&pmus_lock); |
| 8694 | /* |
| 8695 | * Like a real lame refcount. |
| 8696 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8697 | list_for_each_entry(i, &pmus, entry) { |
| 8698 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 8699 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8700 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8701 | } |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8702 | } |
| 8703 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8704 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8705 | out: |
| 8706 | mutex_unlock(&pmus_lock); |
| 8707 | } |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8708 | |
| 8709 | /* |
| 8710 | * Let userspace know that this PMU supports address range filtering: |
| 8711 | */ |
| 8712 | static ssize_t nr_addr_filters_show(struct device *dev, |
| 8713 | struct device_attribute *attr, |
| 8714 | char *page) |
| 8715 | { |
| 8716 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8717 | |
| 8718 | return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); |
| 8719 | } |
| 8720 | DEVICE_ATTR_RO(nr_addr_filters); |
| 8721 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8722 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8723 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8724 | static ssize_t |
| 8725 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 8726 | { |
| 8727 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8728 | |
| 8729 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 8730 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8731 | static DEVICE_ATTR_RO(type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8732 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8733 | static ssize_t |
| 8734 | perf_event_mux_interval_ms_show(struct device *dev, |
| 8735 | struct device_attribute *attr, |
| 8736 | char *page) |
| 8737 | { |
| 8738 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8739 | |
| 8740 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 8741 | } |
| 8742 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8743 | static DEFINE_MUTEX(mux_interval_mutex); |
| 8744 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8745 | static ssize_t |
| 8746 | perf_event_mux_interval_ms_store(struct device *dev, |
| 8747 | struct device_attribute *attr, |
| 8748 | const char *buf, size_t count) |
| 8749 | { |
| 8750 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8751 | int timer, cpu, ret; |
| 8752 | |
| 8753 | ret = kstrtoint(buf, 0, &timer); |
| 8754 | if (ret) |
| 8755 | return ret; |
| 8756 | |
| 8757 | if (timer < 1) |
| 8758 | return -EINVAL; |
| 8759 | |
| 8760 | /* same value, noting to do */ |
| 8761 | if (timer == pmu->hrtimer_interval_ms) |
| 8762 | return count; |
| 8763 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8764 | mutex_lock(&mux_interval_mutex); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8765 | pmu->hrtimer_interval_ms = timer; |
| 8766 | |
| 8767 | /* update all cpuctx for this PMU */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8768 | get_online_cpus(); |
| 8769 | for_each_online_cpu(cpu) { |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8770 | struct perf_cpu_context *cpuctx; |
| 8771 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 8772 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 8773 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8774 | cpu_function_call(cpu, |
| 8775 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8776 | } |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8777 | put_online_cpus(); |
| 8778 | mutex_unlock(&mux_interval_mutex); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8779 | |
| 8780 | return count; |
| 8781 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8782 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8783 | |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8784 | static struct attribute *pmu_dev_attrs[] = { |
| 8785 | &dev_attr_type.attr, |
| 8786 | &dev_attr_perf_event_mux_interval_ms.attr, |
| 8787 | NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8788 | }; |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8789 | ATTRIBUTE_GROUPS(pmu_dev); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8790 | |
| 8791 | static int pmu_bus_running; |
| 8792 | static struct bus_type pmu_bus = { |
| 8793 | .name = "event_source", |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8794 | .dev_groups = pmu_dev_groups, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8795 | }; |
| 8796 | |
| 8797 | static void pmu_dev_release(struct device *dev) |
| 8798 | { |
| 8799 | kfree(dev); |
| 8800 | } |
| 8801 | |
| 8802 | static int pmu_dev_alloc(struct pmu *pmu) |
| 8803 | { |
| 8804 | int ret = -ENOMEM; |
| 8805 | |
| 8806 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 8807 | if (!pmu->dev) |
| 8808 | goto out; |
| 8809 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 8810 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8811 | device_initialize(pmu->dev); |
| 8812 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 8813 | if (ret) |
| 8814 | goto free_dev; |
| 8815 | |
| 8816 | dev_set_drvdata(pmu->dev, pmu); |
| 8817 | pmu->dev->bus = &pmu_bus; |
| 8818 | pmu->dev->release = pmu_dev_release; |
| 8819 | ret = device_add(pmu->dev); |
| 8820 | if (ret) |
| 8821 | goto free_dev; |
| 8822 | |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8823 | /* For PMUs with address filters, throw in an extra attribute: */ |
| 8824 | if (pmu->nr_addr_filters) |
| 8825 | ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters); |
| 8826 | |
| 8827 | if (ret) |
| 8828 | goto del_dev; |
| 8829 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8830 | out: |
| 8831 | return ret; |
| 8832 | |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8833 | del_dev: |
| 8834 | device_del(pmu->dev); |
| 8835 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8836 | free_dev: |
| 8837 | put_device(pmu->dev); |
| 8838 | goto out; |
| 8839 | } |
| 8840 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 8841 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 8842 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 8843 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 8844 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8845 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8846 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8847 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8848 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8849 | ret = -ENOMEM; |
| 8850 | pmu->pmu_disable_count = alloc_percpu(int); |
| 8851 | if (!pmu->pmu_disable_count) |
| 8852 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8853 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8854 | pmu->type = -1; |
| 8855 | if (!name) |
| 8856 | goto skip_type; |
| 8857 | pmu->name = name; |
| 8858 | |
| 8859 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 8860 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 8861 | if (type < 0) { |
| 8862 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8863 | goto free_pdc; |
| 8864 | } |
| 8865 | } |
| 8866 | pmu->type = type; |
| 8867 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8868 | if (pmu_bus_running) { |
| 8869 | ret = pmu_dev_alloc(pmu); |
| 8870 | if (ret) |
| 8871 | goto free_idr; |
| 8872 | } |
| 8873 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8874 | skip_type: |
Peter Zijlstra | 2665784 | 2016-03-22 22:09:18 +0100 | [diff] [blame] | 8875 | if (pmu->task_ctx_nr == perf_hw_context) { |
| 8876 | static int hw_context_taken = 0; |
| 8877 | |
Mark Rutland | 5101ef2 | 2016-04-26 11:33:46 +0100 | [diff] [blame] | 8878 | /* |
| 8879 | * Other than systems with heterogeneous CPUs, it never makes |
| 8880 | * sense for two PMUs to share perf_hw_context. PMUs which are |
| 8881 | * uncore must use perf_invalid_context. |
| 8882 | */ |
| 8883 | if (WARN_ON_ONCE(hw_context_taken && |
| 8884 | !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS))) |
Peter Zijlstra | 2665784 | 2016-03-22 22:09:18 +0100 | [diff] [blame] | 8885 | pmu->task_ctx_nr = perf_invalid_context; |
| 8886 | |
| 8887 | hw_context_taken = 1; |
| 8888 | } |
| 8889 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8890 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 8891 | if (pmu->pmu_cpu_context) |
| 8892 | goto got_cpu_context; |
| 8893 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 8894 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8895 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 8896 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8897 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8898 | |
| 8899 | for_each_possible_cpu(cpu) { |
| 8900 | struct perf_cpu_context *cpuctx; |
| 8901 | |
| 8902 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 8903 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 8904 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 8905 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8906 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 8907 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8908 | __perf_mux_hrtimer_init(cpuctx, cpu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 8909 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 8910 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8911 | } |
| 8912 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8913 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8914 | if (!pmu->start_txn) { |
| 8915 | if (pmu->pmu_enable) { |
| 8916 | /* |
| 8917 | * If we have pmu_enable/pmu_disable calls, install |
| 8918 | * transaction stubs that use that to try and batch |
| 8919 | * hardware accesses. |
| 8920 | */ |
| 8921 | pmu->start_txn = perf_pmu_start_txn; |
| 8922 | pmu->commit_txn = perf_pmu_commit_txn; |
| 8923 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 8924 | } else { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8925 | pmu->start_txn = perf_pmu_nop_txn; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8926 | pmu->commit_txn = perf_pmu_nop_int; |
| 8927 | pmu->cancel_txn = perf_pmu_nop_void; |
| 8928 | } |
| 8929 | } |
| 8930 | |
| 8931 | if (!pmu->pmu_enable) { |
| 8932 | pmu->pmu_enable = perf_pmu_nop_void; |
| 8933 | pmu->pmu_disable = perf_pmu_nop_void; |
| 8934 | } |
| 8935 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 8936 | if (!pmu->event_idx) |
| 8937 | pmu->event_idx = perf_event_idx_default; |
| 8938 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8939 | list_add_rcu(&pmu->entry, &pmus); |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8940 | atomic_set(&pmu->exclusive_cnt, 0); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8941 | ret = 0; |
| 8942 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8943 | mutex_unlock(&pmus_lock); |
| 8944 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8945 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8946 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8947 | free_dev: |
| 8948 | device_del(pmu->dev); |
| 8949 | put_device(pmu->dev); |
| 8950 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8951 | free_idr: |
| 8952 | if (pmu->type >= PERF_TYPE_MAX) |
| 8953 | idr_remove(&pmu_idr, pmu->type); |
| 8954 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8955 | free_pdc: |
| 8956 | free_percpu(pmu->pmu_disable_count); |
| 8957 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8958 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 8959 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8960 | |
| 8961 | void perf_pmu_unregister(struct pmu *pmu) |
| 8962 | { |
Jiri Olsa | 0933840 | 2016-10-20 13:10:11 +0200 | [diff] [blame] | 8963 | int remove_device; |
| 8964 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8965 | mutex_lock(&pmus_lock); |
Jiri Olsa | 0933840 | 2016-10-20 13:10:11 +0200 | [diff] [blame] | 8966 | remove_device = pmu_bus_running; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8967 | list_del_rcu(&pmu->entry); |
| 8968 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8969 | |
| 8970 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 8971 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 8972 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8973 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8974 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 8975 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8976 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8977 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8978 | if (pmu->type >= PERF_TYPE_MAX) |
| 8979 | idr_remove(&pmu_idr, pmu->type); |
Jiri Olsa | 0933840 | 2016-10-20 13:10:11 +0200 | [diff] [blame] | 8980 | if (remove_device) { |
| 8981 | if (pmu->nr_addr_filters) |
| 8982 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); |
| 8983 | device_del(pmu->dev); |
| 8984 | put_device(pmu->dev); |
| 8985 | } |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8986 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8987 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 8988 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8989 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8990 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
| 8991 | { |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8992 | struct perf_event_context *ctx = NULL; |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8993 | int ret; |
| 8994 | |
| 8995 | if (!try_module_get(pmu->module)) |
| 8996 | return -ENODEV; |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8997 | |
| 8998 | if (event->group_leader != event) { |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 8999 | /* |
| 9000 | * This ctx->mutex can nest when we're called through |
| 9001 | * inheritance. See the perf_event_ctx_lock_nested() comment. |
| 9002 | */ |
| 9003 | ctx = perf_event_ctx_lock_nested(event->group_leader, |
| 9004 | SINGLE_DEPTH_NESTING); |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 9005 | BUG_ON(!ctx); |
| 9006 | } |
| 9007 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 9008 | event->pmu = pmu; |
| 9009 | ret = pmu->event_init(event); |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 9010 | |
| 9011 | if (ctx) |
| 9012 | perf_event_ctx_unlock(event->group_leader, ctx); |
| 9013 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 9014 | if (ret) |
| 9015 | module_put(pmu->module); |
| 9016 | |
| 9017 | return ret; |
| 9018 | } |
| 9019 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 9020 | static struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9021 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 9022 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 9023 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 9024 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 9025 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 9026 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 9027 | |
| 9028 | rcu_read_lock(); |
| 9029 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 9030 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 9031 | if (pmu) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 9032 | ret = perf_try_init_event(pmu, event); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 9033 | if (ret) |
| 9034 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 9035 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 9036 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 9037 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 9038 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 9039 | ret = perf_try_init_event(pmu, event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 9040 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 9041 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 9042 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 9043 | if (ret != -ENOENT) { |
| 9044 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 9045 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9046 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9047 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 9048 | pmu = ERR_PTR(-ENOENT); |
| 9049 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 9050 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9051 | |
| 9052 | return pmu; |
| 9053 | } |
| 9054 | |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 9055 | static void attach_sb_event(struct perf_event *event) |
| 9056 | { |
| 9057 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); |
| 9058 | |
| 9059 | raw_spin_lock(&pel->lock); |
| 9060 | list_add_rcu(&event->sb_list, &pel->list); |
| 9061 | raw_spin_unlock(&pel->lock); |
| 9062 | } |
| 9063 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame] | 9064 | /* |
| 9065 | * We keep a list of all !task (and therefore per-cpu) events |
| 9066 | * that need to receive side-band records. |
| 9067 | * |
| 9068 | * This avoids having to scan all the various PMU per-cpu contexts |
| 9069 | * looking for them. |
| 9070 | */ |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 9071 | static void account_pmu_sb_event(struct perf_event *event) |
| 9072 | { |
David Carrillo-Cisneros | a4f144e | 2016-06-01 12:33:05 -0700 | [diff] [blame] | 9073 | if (is_sb_event(event)) |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 9074 | attach_sb_event(event); |
| 9075 | } |
| 9076 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 9077 | static void account_event_cpu(struct perf_event *event, int cpu) |
| 9078 | { |
| 9079 | if (event->parent) |
| 9080 | return; |
| 9081 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 9082 | if (is_cgroup_event(event)) |
| 9083 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); |
| 9084 | } |
| 9085 | |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 9086 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
| 9087 | static void account_freq_event_nohz(void) |
| 9088 | { |
| 9089 | #ifdef CONFIG_NO_HZ_FULL |
| 9090 | /* Lock so we don't race with concurrent unaccount */ |
| 9091 | spin_lock(&nr_freq_lock); |
| 9092 | if (atomic_inc_return(&nr_freq_events) == 1) |
| 9093 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); |
| 9094 | spin_unlock(&nr_freq_lock); |
| 9095 | #endif |
| 9096 | } |
| 9097 | |
| 9098 | static void account_freq_event(void) |
| 9099 | { |
| 9100 | if (tick_nohz_full_enabled()) |
| 9101 | account_freq_event_nohz(); |
| 9102 | else |
| 9103 | atomic_inc(&nr_freq_events); |
| 9104 | } |
| 9105 | |
| 9106 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 9107 | static void account_event(struct perf_event *event) |
| 9108 | { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 9109 | bool inc = false; |
| 9110 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 9111 | if (event->parent) |
| 9112 | return; |
| 9113 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 9114 | if (event->attach_state & PERF_ATTACH_TASK) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 9115 | inc = true; |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 9116 | if (event->attr.mmap || event->attr.mmap_data) |
| 9117 | atomic_inc(&nr_mmap_events); |
| 9118 | if (event->attr.comm) |
| 9119 | atomic_inc(&nr_comm_events); |
| 9120 | if (event->attr.task) |
| 9121 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 9122 | if (event->attr.freq) |
| 9123 | account_freq_event(); |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 9124 | if (event->attr.context_switch) { |
| 9125 | atomic_inc(&nr_switch_events); |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 9126 | inc = true; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 9127 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 9128 | if (has_branch_stack(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 9129 | inc = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 9130 | if (is_cgroup_event(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 9131 | inc = true; |
| 9132 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 9133 | if (inc) { |
| 9134 | if (atomic_inc_not_zero(&perf_sched_count)) |
| 9135 | goto enabled; |
| 9136 | |
| 9137 | mutex_lock(&perf_sched_mutex); |
| 9138 | if (!atomic_read(&perf_sched_count)) { |
| 9139 | static_branch_enable(&perf_sched_events); |
| 9140 | /* |
| 9141 | * Guarantee that all CPUs observe they key change and |
| 9142 | * call the perf scheduling hooks before proceeding to |
| 9143 | * install events that need them. |
| 9144 | */ |
| 9145 | synchronize_sched(); |
| 9146 | } |
| 9147 | /* |
| 9148 | * Now that we have waited for the sync_sched(), allow further |
| 9149 | * increments to by-pass the mutex. |
| 9150 | */ |
| 9151 | atomic_inc(&perf_sched_count); |
| 9152 | mutex_unlock(&perf_sched_mutex); |
| 9153 | } |
| 9154 | enabled: |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 9155 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 9156 | account_event_cpu(event, event->cpu); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 9157 | |
| 9158 | account_pmu_sb_event(event); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 9159 | } |
| 9160 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9161 | /* |
| 9162 | * Allocate and initialize a event structure |
| 9163 | */ |
| 9164 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9165 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 9166 | struct task_struct *task, |
| 9167 | struct perf_event *group_leader, |
| 9168 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9169 | perf_overflow_handler_t overflow_handler, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9170 | void *context, int cgroup_fd) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9171 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 9172 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9173 | struct perf_event *event; |
| 9174 | struct hw_perf_event *hwc; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9175 | long err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9176 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 9177 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 9178 | if (!task || cpu != -1) |
| 9179 | return ERR_PTR(-EINVAL); |
| 9180 | } |
| 9181 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9182 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9183 | if (!event) |
| 9184 | return ERR_PTR(-ENOMEM); |
| 9185 | |
| 9186 | /* |
| 9187 | * Single events are their own group leaders, with an |
| 9188 | * empty sibling list: |
| 9189 | */ |
| 9190 | if (!group_leader) |
| 9191 | group_leader = event; |
| 9192 | |
| 9193 | mutex_init(&event->child_mutex); |
| 9194 | INIT_LIST_HEAD(&event->child_list); |
| 9195 | |
| 9196 | INIT_LIST_HEAD(&event->group_entry); |
| 9197 | INIT_LIST_HEAD(&event->event_entry); |
| 9198 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 9199 | INIT_LIST_HEAD(&event->rb_entry); |
Stephane Eranian | 71ad88e | 2013-11-12 17:58:48 +0100 | [diff] [blame] | 9200 | INIT_LIST_HEAD(&event->active_entry); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 9201 | INIT_LIST_HEAD(&event->addr_filters.list); |
Stephane Eranian | f3ae75d | 2014-01-08 11:15:52 +0100 | [diff] [blame] | 9202 | INIT_HLIST_NODE(&event->hlist_entry); |
| 9203 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 9204 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9205 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 9206 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9207 | |
| 9208 | mutex_init(&event->mmap_mutex); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 9209 | raw_spin_lock_init(&event->addr_filters.lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9210 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 9211 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9212 | event->cpu = cpu; |
| 9213 | event->attr = *attr; |
| 9214 | event->group_leader = group_leader; |
| 9215 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9216 | event->oncpu = -1; |
| 9217 | |
| 9218 | event->parent = parent_event; |
| 9219 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 9220 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9221 | event->id = atomic64_inc_return(&perf_event_id); |
| 9222 | |
| 9223 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 9224 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 9225 | if (task) { |
| 9226 | event->attach_state = PERF_ATTACH_TASK; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 9227 | /* |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 9228 | * XXX pmu::event_init needs to know what task to account to |
| 9229 | * and we cannot use the ctx information because we need the |
| 9230 | * pmu before we get a ctx. |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 9231 | */ |
Prashant Bhole | b951ffb | 2018-04-09 19:03:46 +0900 | [diff] [blame] | 9232 | get_task_struct(task); |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 9233 | event->hw.target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 9234 | } |
| 9235 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9236 | event->clock = &local_clock; |
| 9237 | if (parent_event) |
| 9238 | event->clock = parent_event->clock; |
| 9239 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9240 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 9241 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9242 | context = parent_event->overflow_handler_context; |
Arnd Bergmann | f1e4ba5 | 2016-09-06 15:10:22 +0200 | [diff] [blame] | 9243 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) |
Alexei Starovoitov | aa6a5f3 | 2016-09-01 18:37:24 -0700 | [diff] [blame] | 9244 | if (overflow_handler == bpf_overflow_handler) { |
| 9245 | struct bpf_prog *prog = bpf_prog_inc(parent_event->prog); |
| 9246 | |
| 9247 | if (IS_ERR(prog)) { |
| 9248 | err = PTR_ERR(prog); |
| 9249 | goto err_ns; |
| 9250 | } |
| 9251 | event->prog = prog; |
| 9252 | event->orig_overflow_handler = |
| 9253 | parent_event->orig_overflow_handler; |
| 9254 | } |
| 9255 | #endif |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9256 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 9257 | |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 9258 | if (overflow_handler) { |
| 9259 | event->overflow_handler = overflow_handler; |
| 9260 | event->overflow_handler_context = context; |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 9261 | } else if (is_write_backward(event)){ |
| 9262 | event->overflow_handler = perf_event_output_backward; |
| 9263 | event->overflow_handler_context = NULL; |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 9264 | } else { |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 9265 | event->overflow_handler = perf_event_output_forward; |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 9266 | event->overflow_handler_context = NULL; |
| 9267 | } |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 9268 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 9269 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9270 | |
| 9271 | pmu = NULL; |
| 9272 | |
| 9273 | hwc = &event->hw; |
| 9274 | hwc->sample_period = attr->sample_period; |
| 9275 | if (attr->freq && attr->sample_freq) |
| 9276 | hwc->sample_period = 1; |
| 9277 | hwc->last_period = hwc->sample_period; |
| 9278 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 9279 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9280 | |
| 9281 | /* |
Peter Zijlstra | 50fe37e | 2017-05-30 11:45:12 +0200 | [diff] [blame] | 9282 | * We currently do not support PERF_SAMPLE_READ on inherited events. |
| 9283 | * See perf_output_read(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9284 | */ |
Peter Zijlstra | 50fe37e | 2017-05-30 11:45:12 +0200 | [diff] [blame] | 9285 | if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9286 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9287 | |
Yan, Zheng | a46a230 | 2014-11-04 21:56:06 -0500 | [diff] [blame] | 9288 | if (!has_branch_stack(event)) |
| 9289 | event->attr.branch_sample_type = 0; |
| 9290 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9291 | if (cgroup_fd != -1) { |
| 9292 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); |
| 9293 | if (err) |
| 9294 | goto err_ns; |
| 9295 | } |
| 9296 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 9297 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9298 | if (!pmu) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9299 | goto err_ns; |
| 9300 | else if (IS_ERR(pmu)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9301 | err = PTR_ERR(pmu); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9302 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9303 | } |
| 9304 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9305 | err = exclusive_event_init(event); |
| 9306 | if (err) |
| 9307 | goto err_pmu; |
| 9308 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 9309 | if (has_addr_filter(event)) { |
| 9310 | event->addr_filters_offs = kcalloc(pmu->nr_addr_filters, |
| 9311 | sizeof(unsigned long), |
| 9312 | GFP_KERNEL); |
Dan Carpenter | f02bfec | 2017-05-22 12:04:18 +0300 | [diff] [blame] | 9313 | if (!event->addr_filters_offs) { |
| 9314 | err = -ENOMEM; |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 9315 | goto err_per_task; |
Dan Carpenter | f02bfec | 2017-05-22 12:04:18 +0300 | [diff] [blame] | 9316 | } |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 9317 | |
| 9318 | /* force hw sync on the address filters */ |
| 9319 | event->addr_filters_gen = 1; |
| 9320 | } |
| 9321 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9322 | if (!event->parent) { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 9323 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
Arnaldo Carvalho de Melo | 97c79a3 | 2016-04-28 13:16:33 -0300 | [diff] [blame] | 9324 | err = get_callchain_buffers(attr->sample_max_stack); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9325 | if (err) |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 9326 | goto err_addr_filters; |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 9327 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9328 | } |
| 9329 | |
Alexander Shishkin | 927a557 | 2016-03-02 13:24:14 +0200 | [diff] [blame] | 9330 | /* symmetric to unaccount_event() in _free_event() */ |
| 9331 | account_event(event); |
| 9332 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9333 | return event; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9334 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 9335 | err_addr_filters: |
| 9336 | kfree(event->addr_filters_offs); |
| 9337 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9338 | err_per_task: |
| 9339 | exclusive_event_destroy(event); |
| 9340 | |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9341 | err_pmu: |
| 9342 | if (event->destroy) |
| 9343 | event->destroy(event); |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 9344 | module_put(pmu->module); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9345 | err_ns: |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9346 | if (is_cgroup_event(event)) |
| 9347 | perf_detach_cgroup(event); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9348 | if (event->ns) |
| 9349 | put_pid_ns(event->ns); |
Prashant Bhole | b951ffb | 2018-04-09 19:03:46 +0900 | [diff] [blame] | 9350 | if (event->hw.target) |
| 9351 | put_task_struct(event->hw.target); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 9352 | kfree(event); |
| 9353 | |
| 9354 | return ERR_PTR(err); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9355 | } |
| 9356 | |
| 9357 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 9358 | struct perf_event_attr *attr) |
| 9359 | { |
| 9360 | u32 size; |
| 9361 | int ret; |
| 9362 | |
| 9363 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 9364 | return -EFAULT; |
| 9365 | |
| 9366 | /* |
| 9367 | * zero the full structure, so that a short copy will be nice. |
| 9368 | */ |
| 9369 | memset(attr, 0, sizeof(*attr)); |
| 9370 | |
| 9371 | ret = get_user(size, &uattr->size); |
| 9372 | if (ret) |
| 9373 | return ret; |
| 9374 | |
| 9375 | if (size > PAGE_SIZE) /* silly large */ |
| 9376 | goto err_size; |
| 9377 | |
| 9378 | if (!size) /* abi compat */ |
| 9379 | size = PERF_ATTR_SIZE_VER0; |
| 9380 | |
| 9381 | if (size < PERF_ATTR_SIZE_VER0) |
| 9382 | goto err_size; |
| 9383 | |
| 9384 | /* |
| 9385 | * If we're handed a bigger struct than we know of, |
| 9386 | * ensure all the unknown bits are 0 - i.e. new |
| 9387 | * user-space does not rely on any kernel feature |
| 9388 | * extensions we dont know about yet. |
| 9389 | */ |
| 9390 | if (size > sizeof(*attr)) { |
| 9391 | unsigned char __user *addr; |
| 9392 | unsigned char __user *end; |
| 9393 | unsigned char val; |
| 9394 | |
| 9395 | addr = (void __user *)uattr + sizeof(*attr); |
| 9396 | end = (void __user *)uattr + size; |
| 9397 | |
| 9398 | for (; addr < end; addr++) { |
| 9399 | ret = get_user(val, addr); |
| 9400 | if (ret) |
| 9401 | return ret; |
| 9402 | if (val) |
| 9403 | goto err_size; |
| 9404 | } |
| 9405 | size = sizeof(*attr); |
| 9406 | } |
| 9407 | |
| 9408 | ret = copy_from_user(attr, uattr, size); |
| 9409 | if (ret) |
| 9410 | return -EFAULT; |
| 9411 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 9412 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9413 | return -EINVAL; |
| 9414 | |
| 9415 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 9416 | return -EINVAL; |
| 9417 | |
| 9418 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 9419 | return -EINVAL; |
| 9420 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 9421 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 9422 | u64 mask = attr->branch_sample_type; |
| 9423 | |
| 9424 | /* only using defined bits */ |
| 9425 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 9426 | return -EINVAL; |
| 9427 | |
| 9428 | /* at least one branch bit must be set */ |
| 9429 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 9430 | return -EINVAL; |
| 9431 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 9432 | /* propagate priv level, when not set for branch */ |
| 9433 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 9434 | |
| 9435 | /* exclude_kernel checked on syscall entry */ |
| 9436 | if (!attr->exclude_kernel) |
| 9437 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 9438 | |
| 9439 | if (!attr->exclude_user) |
| 9440 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 9441 | |
| 9442 | if (!attr->exclude_hv) |
| 9443 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 9444 | /* |
| 9445 | * adjust user setting (for HW filter setup) |
| 9446 | */ |
| 9447 | attr->branch_sample_type = mask; |
| 9448 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 9449 | /* privileged levels capture (kernel, hv): check permissions */ |
| 9450 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 9451 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 9452 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 9453 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 9454 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 9455 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 9456 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 9457 | if (ret) |
| 9458 | return ret; |
| 9459 | } |
| 9460 | |
| 9461 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 9462 | if (!arch_perf_have_user_stack_dump()) |
| 9463 | return -ENOSYS; |
| 9464 | |
| 9465 | /* |
| 9466 | * We have __u32 type for the size, but so far |
| 9467 | * we can only use __u16 as maximum due to the |
| 9468 | * __u16 sample size limit. |
| 9469 | */ |
| 9470 | if (attr->sample_stack_user >= USHRT_MAX) |
Jiri Olsa | 9acdfe4 | 2018-04-15 11:23:50 +0200 | [diff] [blame] | 9471 | return -EINVAL; |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 9472 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
Jiri Olsa | 9acdfe4 | 2018-04-15 11:23:50 +0200 | [diff] [blame] | 9473 | return -EINVAL; |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 9474 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 9475 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 9476 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
| 9477 | ret = perf_reg_validate(attr->sample_regs_intr); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9478 | out: |
| 9479 | return ret; |
| 9480 | |
| 9481 | err_size: |
| 9482 | put_user(sizeof(*attr), &uattr->size); |
| 9483 | ret = -E2BIG; |
| 9484 | goto out; |
| 9485 | } |
| 9486 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9487 | static int |
| 9488 | 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] | 9489 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 9490 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9491 | int ret = -EINVAL; |
| 9492 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9493 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9494 | goto set; |
| 9495 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9496 | /* don't allow circular references */ |
| 9497 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9498 | goto out; |
| 9499 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 9500 | /* |
| 9501 | * Don't allow cross-cpu buffers |
| 9502 | */ |
| 9503 | if (output_event->cpu != event->cpu) |
| 9504 | goto out; |
| 9505 | |
| 9506 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 9507 | * 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] | 9508 | */ |
| 9509 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 9510 | goto out; |
| 9511 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9512 | /* |
| 9513 | * Mixing clocks in the same buffer is trouble you don't need. |
| 9514 | */ |
| 9515 | if (output_event->clock != event->clock) |
| 9516 | goto out; |
| 9517 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 9518 | /* |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 9519 | * Either writing ring buffer from beginning or from end. |
| 9520 | * Mixing is not allowed. |
| 9521 | */ |
| 9522 | if (is_write_backward(output_event) != is_write_backward(event)) |
| 9523 | goto out; |
| 9524 | |
| 9525 | /* |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 9526 | * If both events generate aux data, they must be on the same PMU |
| 9527 | */ |
| 9528 | if (has_aux(event) && has_aux(output_event) && |
| 9529 | event->pmu != output_event->pmu) |
| 9530 | goto out; |
| 9531 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9532 | set: |
| 9533 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9534 | /* Can't redirect output if we've got an active mmap() */ |
| 9535 | if (atomic_read(&event->mmap_count)) |
| 9536 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9537 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9538 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 9539 | /* get the rb we want to redirect to */ |
| 9540 | rb = ring_buffer_get(output_event); |
| 9541 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9542 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9543 | } |
| 9544 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 9545 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 9546 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9547 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9548 | unlock: |
| 9549 | mutex_unlock(&event->mmap_mutex); |
| 9550 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9551 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9552 | return ret; |
| 9553 | } |
| 9554 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9555 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
| 9556 | { |
| 9557 | if (b < a) |
| 9558 | swap(a, b); |
| 9559 | |
| 9560 | mutex_lock(a); |
| 9561 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); |
| 9562 | } |
| 9563 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9564 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
| 9565 | { |
| 9566 | bool nmi_safe = false; |
| 9567 | |
| 9568 | switch (clk_id) { |
| 9569 | case CLOCK_MONOTONIC: |
| 9570 | event->clock = &ktime_get_mono_fast_ns; |
| 9571 | nmi_safe = true; |
| 9572 | break; |
| 9573 | |
| 9574 | case CLOCK_MONOTONIC_RAW: |
| 9575 | event->clock = &ktime_get_raw_fast_ns; |
| 9576 | nmi_safe = true; |
| 9577 | break; |
| 9578 | |
| 9579 | case CLOCK_REALTIME: |
| 9580 | event->clock = &ktime_get_real_ns; |
| 9581 | break; |
| 9582 | |
| 9583 | case CLOCK_BOOTTIME: |
| 9584 | event->clock = &ktime_get_boot_ns; |
| 9585 | break; |
| 9586 | |
| 9587 | case CLOCK_TAI: |
| 9588 | event->clock = &ktime_get_tai_ns; |
| 9589 | break; |
| 9590 | |
| 9591 | default: |
| 9592 | return -EINVAL; |
| 9593 | } |
| 9594 | |
| 9595 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) |
| 9596 | return -EINVAL; |
| 9597 | |
| 9598 | return 0; |
| 9599 | } |
| 9600 | |
Peter Zijlstra | 922813f | 2017-01-11 21:09:50 +0100 | [diff] [blame] | 9601 | /* |
| 9602 | * Variation on perf_event_ctx_lock_nested(), except we take two context |
| 9603 | * mutexes. |
| 9604 | */ |
| 9605 | static struct perf_event_context * |
| 9606 | __perf_event_ctx_lock_double(struct perf_event *group_leader, |
| 9607 | struct perf_event_context *ctx) |
| 9608 | { |
| 9609 | struct perf_event_context *gctx; |
| 9610 | |
| 9611 | again: |
| 9612 | rcu_read_lock(); |
| 9613 | gctx = READ_ONCE(group_leader->ctx); |
| 9614 | if (!atomic_inc_not_zero(&gctx->refcount)) { |
| 9615 | rcu_read_unlock(); |
| 9616 | goto again; |
| 9617 | } |
| 9618 | rcu_read_unlock(); |
| 9619 | |
| 9620 | mutex_lock_double(&gctx->mutex, &ctx->mutex); |
| 9621 | |
| 9622 | if (group_leader->ctx != gctx) { |
| 9623 | mutex_unlock(&ctx->mutex); |
| 9624 | mutex_unlock(&gctx->mutex); |
| 9625 | put_ctx(gctx); |
| 9626 | goto again; |
| 9627 | } |
| 9628 | |
| 9629 | return gctx; |
| 9630 | } |
| 9631 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9632 | /** |
| 9633 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 9634 | * |
| 9635 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 9636 | * @pid: target pid |
| 9637 | * @cpu: target cpu |
| 9638 | * @group_fd: group leader event fd |
| 9639 | */ |
| 9640 | SYSCALL_DEFINE5(perf_event_open, |
| 9641 | struct perf_event_attr __user *, attr_uptr, |
| 9642 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 9643 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9644 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 9645 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9646 | struct perf_event_attr attr; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9647 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9648 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9649 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 9650 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9651 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9652 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9653 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9654 | int err; |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 9655 | int f_flags = O_RDWR; |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9656 | int cgroup_fd = -1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9657 | |
| 9658 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9659 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9660 | return -EINVAL; |
| 9661 | |
| 9662 | err = perf_copy_attr(attr_uptr, &attr); |
| 9663 | if (err) |
| 9664 | return err; |
| 9665 | |
| 9666 | if (!attr.exclude_kernel) { |
| 9667 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 9668 | return -EACCES; |
| 9669 | } |
| 9670 | |
| 9671 | if (attr.freq) { |
| 9672 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 9673 | return -EINVAL; |
Peter Zijlstra | 0819b2e | 2014-05-15 20:23:48 +0200 | [diff] [blame] | 9674 | } else { |
| 9675 | if (attr.sample_period & (1ULL << 63)) |
| 9676 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9677 | } |
| 9678 | |
Arnaldo Carvalho de Melo | 97c79a3 | 2016-04-28 13:16:33 -0300 | [diff] [blame] | 9679 | if (!attr.sample_max_stack) |
| 9680 | attr.sample_max_stack = sysctl_perf_event_max_stack; |
| 9681 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9682 | /* |
| 9683 | * In cgroup mode, the pid argument is used to pass the fd |
| 9684 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 9685 | * designates the cpu on which to monitor threads from that |
| 9686 | * cgroup. |
| 9687 | */ |
| 9688 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 9689 | return -EINVAL; |
| 9690 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 9691 | if (flags & PERF_FLAG_FD_CLOEXEC) |
| 9692 | f_flags |= O_CLOEXEC; |
| 9693 | |
| 9694 | event_fd = get_unused_fd_flags(f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9695 | if (event_fd < 0) |
| 9696 | return event_fd; |
| 9697 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9698 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9699 | err = perf_fget_light(group_fd, &group); |
| 9700 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 9701 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9702 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9703 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 9704 | output_event = group_leader; |
| 9705 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 9706 | group_leader = NULL; |
| 9707 | } |
| 9708 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9709 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 9710 | task = find_lively_task_by_vpid(pid); |
| 9711 | if (IS_ERR(task)) { |
| 9712 | err = PTR_ERR(task); |
| 9713 | goto err_group_fd; |
| 9714 | } |
| 9715 | } |
| 9716 | |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 9717 | if (task && group_leader && |
| 9718 | group_leader->attr.inherit != attr.inherit) { |
| 9719 | err = -EINVAL; |
| 9720 | goto err_task; |
| 9721 | } |
| 9722 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 9723 | get_online_cpus(); |
| 9724 | |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9725 | if (task) { |
| 9726 | err = mutex_lock_interruptible(&task->signal->cred_guard_mutex); |
| 9727 | if (err) |
| 9728 | goto err_cpus; |
| 9729 | |
| 9730 | /* |
| 9731 | * Reuse ptrace permission checks for now. |
| 9732 | * |
| 9733 | * We must hold cred_guard_mutex across this and any potential |
| 9734 | * perf_install_in_context() call for this new event to |
| 9735 | * serialize against exec() altering our credentials (and the |
| 9736 | * perf_event_exit_task() that could imply). |
| 9737 | */ |
| 9738 | err = -EACCES; |
| 9739 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) |
| 9740 | goto err_cred; |
| 9741 | } |
| 9742 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9743 | if (flags & PERF_FLAG_PID_CGROUP) |
| 9744 | cgroup_fd = pid; |
| 9745 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9746 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9747 | NULL, NULL, cgroup_fd); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 9748 | if (IS_ERR(event)) { |
| 9749 | err = PTR_ERR(event); |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9750 | goto err_cred; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 9751 | } |
| 9752 | |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 9753 | if (is_sampling_event(event)) { |
| 9754 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { |
Vineet Gupta | a139655 | 2016-05-09 15:07:40 +0530 | [diff] [blame] | 9755 | err = -EOPNOTSUPP; |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 9756 | goto err_alloc; |
| 9757 | } |
| 9758 | } |
| 9759 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9760 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9761 | * Special case software events and allow them to be part of |
| 9762 | * any hardware group. |
| 9763 | */ |
| 9764 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9765 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9766 | if (attr.use_clockid) { |
| 9767 | err = perf_event_set_clock(event, attr.clockid); |
| 9768 | if (err) |
| 9769 | goto err_alloc; |
| 9770 | } |
| 9771 | |
David Carrillo-Cisneros | 4ff6a8d | 2016-08-17 13:55:05 -0700 | [diff] [blame] | 9772 | if (pmu->task_ctx_nr == perf_sw_context) |
| 9773 | event->event_caps |= PERF_EV_CAP_SOFTWARE; |
| 9774 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9775 | if (group_leader && |
| 9776 | (is_software_event(event) != is_software_event(group_leader))) { |
| 9777 | if (is_software_event(event)) { |
| 9778 | /* |
| 9779 | * If event and group_leader are not both a software |
| 9780 | * event, and event is, then group leader is not. |
| 9781 | * |
| 9782 | * Allow the addition of software events to !software |
| 9783 | * groups, this is safe because software events never |
| 9784 | * fail to schedule. |
| 9785 | */ |
| 9786 | pmu = group_leader->pmu; |
| 9787 | } else if (is_software_event(group_leader) && |
David Carrillo-Cisneros | 4ff6a8d | 2016-08-17 13:55:05 -0700 | [diff] [blame] | 9788 | (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9789 | /* |
| 9790 | * In case the group is a pure software group, and we |
| 9791 | * try to add a hardware event, move the whole group to |
| 9792 | * the hardware context. |
| 9793 | */ |
| 9794 | move_group = 1; |
| 9795 | } |
| 9796 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9797 | |
| 9798 | /* |
| 9799 | * Get the target context (task or percpu): |
| 9800 | */ |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 9801 | ctx = find_get_context(pmu, task, event); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9802 | if (IS_ERR(ctx)) { |
| 9803 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 9804 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9805 | } |
| 9806 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9807 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
| 9808 | err = -EBUSY; |
| 9809 | goto err_context; |
| 9810 | } |
| 9811 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9812 | /* |
| 9813 | * Look up the group leader (we will attach this event to it): |
| 9814 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9815 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9816 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9817 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9818 | /* |
| 9819 | * Do not allow a recursive hierarchy (this new sibling |
| 9820 | * becoming part of another group-sibling): |
| 9821 | */ |
| 9822 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9823 | goto err_context; |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9824 | |
| 9825 | /* All events in a group should have the same clock */ |
| 9826 | if (group_leader->clock != event->clock) |
| 9827 | goto err_context; |
| 9828 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9829 | /* |
Mark Rutland | bde6608 | 2017-06-22 15:41:38 +0100 | [diff] [blame] | 9830 | * Make sure we're both events for the same CPU; |
| 9831 | * grouping events for different CPUs is broken; since |
| 9832 | * you can never concurrently schedule them anyhow. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9833 | */ |
Mark Rutland | bde6608 | 2017-06-22 15:41:38 +0100 | [diff] [blame] | 9834 | if (group_leader->cpu != event->cpu) |
| 9835 | goto err_context; |
Peter Zijlstra | c3c87e7 | 2015-01-23 11:19:48 +0100 | [diff] [blame] | 9836 | |
Mark Rutland | bde6608 | 2017-06-22 15:41:38 +0100 | [diff] [blame] | 9837 | /* |
| 9838 | * Make sure we're both on the same task, or both |
| 9839 | * per-CPU events. |
| 9840 | */ |
| 9841 | if (group_leader->ctx->task != ctx->task) |
| 9842 | goto err_context; |
| 9843 | |
| 9844 | /* |
| 9845 | * Do not allow to attach to a group in a different task |
| 9846 | * or CPU context. If we're moving SW events, we'll fix |
| 9847 | * this up later, so allow that. |
| 9848 | */ |
| 9849 | if (!move_group && group_leader->ctx != ctx) |
| 9850 | goto err_context; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9851 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9852 | /* |
| 9853 | * Only a group leader can be exclusive or pinned |
| 9854 | */ |
| 9855 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9856 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9857 | } |
| 9858 | |
| 9859 | if (output_event) { |
| 9860 | err = perf_event_set_output(event, output_event); |
| 9861 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9862 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9863 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9864 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 9865 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
| 9866 | f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9867 | if (IS_ERR(event_file)) { |
| 9868 | err = PTR_ERR(event_file); |
Alexander Shishkin | 201c2f8 | 2016-03-21 10:02:42 +0200 | [diff] [blame] | 9869 | event_file = NULL; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9870 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9871 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9872 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9873 | if (move_group) { |
Peter Zijlstra | 922813f | 2017-01-11 21:09:50 +0100 | [diff] [blame] | 9874 | gctx = __perf_event_ctx_lock_double(group_leader, ctx); |
| 9875 | |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9876 | if (gctx->task == TASK_TOMBSTONE) { |
| 9877 | err = -ESRCH; |
| 9878 | goto err_locked; |
| 9879 | } |
Peter Zijlstra | 922813f | 2017-01-11 21:09:50 +0100 | [diff] [blame] | 9880 | |
| 9881 | /* |
| 9882 | * Check if we raced against another sys_perf_event_open() call |
| 9883 | * moving the software group underneath us. |
| 9884 | */ |
| 9885 | if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { |
| 9886 | /* |
| 9887 | * If someone moved the group out from under us, check |
| 9888 | * if this new event wound up on the same ctx, if so |
| 9889 | * its the regular !move_group case, otherwise fail. |
| 9890 | */ |
| 9891 | if (gctx != ctx) { |
| 9892 | err = -EINVAL; |
| 9893 | goto err_locked; |
| 9894 | } else { |
| 9895 | perf_event_ctx_unlock(group_leader, gctx); |
| 9896 | move_group = 0; |
| 9897 | } |
| 9898 | } |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9899 | } else { |
| 9900 | mutex_lock(&ctx->mutex); |
| 9901 | } |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9902 | |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9903 | if (ctx->task == TASK_TOMBSTONE) { |
| 9904 | err = -ESRCH; |
| 9905 | goto err_locked; |
| 9906 | } |
| 9907 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9908 | if (!perf_event_validate_size(event)) { |
| 9909 | err = -E2BIG; |
| 9910 | goto err_locked; |
| 9911 | } |
| 9912 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9913 | /* |
| 9914 | * Must be under the same ctx::mutex as perf_install_in_context(), |
| 9915 | * because we need to serialize with concurrent event creation. |
| 9916 | */ |
| 9917 | if (!exclusive_event_installable(event, ctx)) { |
| 9918 | /* exclusive and group stuff are assumed mutually exclusive */ |
| 9919 | WARN_ON_ONCE(move_group); |
| 9920 | |
| 9921 | err = -EBUSY; |
| 9922 | goto err_locked; |
| 9923 | } |
| 9924 | |
| 9925 | WARN_ON_ONCE(ctx->parent_ctx); |
| 9926 | |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9927 | /* |
| 9928 | * This is the point on no return; we cannot fail hereafter. This is |
| 9929 | * where we start modifying current state. |
| 9930 | */ |
| 9931 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9932 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9933 | /* |
| 9934 | * See perf_event_ctx_lock() for comments on the details |
| 9935 | * of swizzling perf_event::ctx. |
| 9936 | */ |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 9937 | perf_remove_from_context(group_leader, 0); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 9938 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9939 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 9940 | group_entry) { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 9941 | perf_remove_from_context(sibling, 0); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9942 | put_ctx(gctx); |
| 9943 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9944 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9945 | /* |
| 9946 | * Wait for everybody to stop referencing the events through |
| 9947 | * the old lists, before installing it on new lists. |
| 9948 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9949 | synchronize_rcu(); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9950 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9951 | /* |
| 9952 | * Install the group siblings before the group leader. |
| 9953 | * |
| 9954 | * Because a group leader will try and install the entire group |
| 9955 | * (through the sibling list, which is still in-tact), we can |
| 9956 | * end up with siblings installed in the wrong context. |
| 9957 | * |
| 9958 | * By installing siblings first we NO-OP because they're not |
| 9959 | * reachable through the group lists. |
| 9960 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9961 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 9962 | group_entry) { |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9963 | perf_event__state_init(sibling); |
Jiri Olsa | 9fc81d8 | 2014-12-10 21:23:51 +0100 | [diff] [blame] | 9964 | perf_install_in_context(ctx, sibling, sibling->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9965 | get_ctx(ctx); |
| 9966 | } |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9967 | |
| 9968 | /* |
| 9969 | * Removing from the context ends up with disabled |
| 9970 | * event. What we want here is event in the initial |
| 9971 | * startup state, ready to be add into new context. |
| 9972 | */ |
| 9973 | perf_event__state_init(group_leader); |
| 9974 | perf_install_in_context(ctx, group_leader, group_leader->cpu); |
| 9975 | get_ctx(ctx); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9976 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9977 | /* |
| 9978 | * Now that all events are installed in @ctx, nothing |
| 9979 | * references @gctx anymore, so drop the last reference we have |
| 9980 | * on it. |
| 9981 | */ |
| 9982 | put_ctx(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9983 | } |
| 9984 | |
Peter Zijlstra | f73e22a | 2015-09-09 20:48:22 +0200 | [diff] [blame] | 9985 | /* |
| 9986 | * Precalculate sample_data sizes; do while holding ctx::mutex such |
| 9987 | * that we're serialized against further additions and before |
| 9988 | * perf_install_in_context() which is the point the event is active and |
| 9989 | * can use these values. |
| 9990 | */ |
| 9991 | perf_event__header_size(event); |
| 9992 | perf_event__id_header_size(event); |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9993 | |
Peter Zijlstra | 78cd2c7 | 2016-01-25 14:08:45 +0100 | [diff] [blame] | 9994 | event->owner = current; |
| 9995 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 9996 | perf_install_in_context(ctx, event, event->cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 9997 | perf_unpin_context(ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9998 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9999 | if (move_group) |
Peter Zijlstra | 922813f | 2017-01-11 21:09:50 +0100 | [diff] [blame] | 10000 | perf_event_ctx_unlock(group_leader, gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10001 | mutex_unlock(&ctx->mutex); |
| 10002 | |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 10003 | if (task) { |
| 10004 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 10005 | put_task_struct(task); |
| 10006 | } |
| 10007 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 10008 | put_online_cpus(); |
| 10009 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10010 | mutex_lock(¤t->perf_event_mutex); |
| 10011 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 10012 | mutex_unlock(¤t->perf_event_mutex); |
| 10013 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 10014 | /* |
| 10015 | * Drop the reference on the group_event after placing the |
| 10016 | * new event on the sibling_list. This ensures destruction |
| 10017 | * of the group leader will find the pointer to itself in |
| 10018 | * perf_group_detach(). |
| 10019 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 10020 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 10021 | fd_install(event_fd, event_file); |
| 10022 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10023 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 10024 | err_locked: |
| 10025 | if (move_group) |
Peter Zijlstra | 922813f | 2017-01-11 21:09:50 +0100 | [diff] [blame] | 10026 | perf_event_ctx_unlock(group_leader, gctx); |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 10027 | mutex_unlock(&ctx->mutex); |
| 10028 | /* err_file: */ |
| 10029 | fput(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 10030 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 10031 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 10032 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 10033 | err_alloc: |
Peter Zijlstra | 1300562 | 2016-02-24 18:45:41 +0100 | [diff] [blame] | 10034 | /* |
| 10035 | * If event_file is set, the fput() above will have called ->release() |
| 10036 | * and that will take care of freeing the event. |
| 10037 | */ |
| 10038 | if (!event_file) |
| 10039 | free_event(event); |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 10040 | err_cred: |
| 10041 | if (task) |
| 10042 | mutex_unlock(&task->signal->cred_guard_mutex); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 10043 | err_cpus: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 10044 | put_online_cpus(); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 10045 | err_task: |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 10046 | if (task) |
| 10047 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 10048 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 10049 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 10050 | err_fd: |
| 10051 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10052 | return err; |
| 10053 | } |
| 10054 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10055 | /** |
| 10056 | * perf_event_create_kernel_counter |
| 10057 | * |
| 10058 | * @attr: attributes of the counter to create |
| 10059 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 10060 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10061 | */ |
| 10062 | struct perf_event * |
| 10063 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 10064 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 10065 | perf_overflow_handler_t overflow_handler, |
| 10066 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10067 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10068 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 10069 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10070 | int err; |
| 10071 | |
| 10072 | /* |
| 10073 | * Get the target context (task or percpu): |
| 10074 | */ |
| 10075 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 10076 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 10077 | overflow_handler, context, -1); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 10078 | if (IS_ERR(event)) { |
| 10079 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 10080 | goto err; |
| 10081 | } |
| 10082 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 10083 | /* Mark owner so we could distinguish it from user events. */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 10084 | event->owner = TASK_TOMBSTONE; |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 10085 | |
Yan, Zheng | 4af57ef28 | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 10086 | ctx = find_get_context(event->pmu, task, event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10087 | if (IS_ERR(ctx)) { |
| 10088 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 10089 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 10090 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10091 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10092 | WARN_ON_ONCE(ctx->parent_ctx); |
| 10093 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 10094 | if (ctx->task == TASK_TOMBSTONE) { |
| 10095 | err = -ESRCH; |
| 10096 | goto err_unlock; |
| 10097 | } |
| 10098 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 10099 | if (!exclusive_event_installable(event, ctx)) { |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 10100 | err = -EBUSY; |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 10101 | goto err_unlock; |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 10102 | } |
| 10103 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10104 | perf_install_in_context(ctx, event, cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 10105 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10106 | mutex_unlock(&ctx->mutex); |
| 10107 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10108 | return event; |
| 10109 | |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 10110 | err_unlock: |
| 10111 | mutex_unlock(&ctx->mutex); |
| 10112 | perf_unpin_context(ctx); |
| 10113 | put_ctx(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 10114 | err_free: |
| 10115 | free_event(event); |
| 10116 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 10117 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 10118 | } |
| 10119 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 10120 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 10121 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 10122 | { |
| 10123 | struct perf_event_context *src_ctx; |
| 10124 | struct perf_event_context *dst_ctx; |
| 10125 | struct perf_event *event, *tmp; |
| 10126 | LIST_HEAD(events); |
| 10127 | |
| 10128 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 10129 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 10130 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 10131 | /* |
| 10132 | * See perf_event_ctx_lock() for comments on the details |
| 10133 | * of swizzling perf_event::ctx. |
| 10134 | */ |
| 10135 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 10136 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 10137 | event_entry) { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 10138 | perf_remove_from_context(event, 0); |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 10139 | unaccount_event_cpu(event, src_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 10140 | put_ctx(src_ctx); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 10141 | list_add(&event->migrate_entry, &events); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 10142 | } |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 10143 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 10144 | /* |
| 10145 | * Wait for the events to quiesce before re-instating them. |
| 10146 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 10147 | synchronize_rcu(); |
| 10148 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 10149 | /* |
| 10150 | * Re-instate events in 2 passes. |
| 10151 | * |
| 10152 | * Skip over group leaders and only install siblings on this first |
| 10153 | * pass, siblings will not get enabled without a leader, however a |
| 10154 | * leader will enable its siblings, even if those are still on the old |
| 10155 | * context. |
| 10156 | */ |
| 10157 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 10158 | if (event->group_leader == event) |
| 10159 | continue; |
| 10160 | |
| 10161 | list_del(&event->migrate_entry); |
| 10162 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 10163 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 10164 | account_event_cpu(event, dst_cpu); |
| 10165 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 10166 | get_ctx(dst_ctx); |
| 10167 | } |
| 10168 | |
| 10169 | /* |
| 10170 | * Once all the siblings are setup properly, install the group leaders |
| 10171 | * to make it go. |
| 10172 | */ |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 10173 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 10174 | list_del(&event->migrate_entry); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 10175 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 10176 | event->state = PERF_EVENT_STATE_INACTIVE; |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 10177 | account_event_cpu(event, dst_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 10178 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 10179 | get_ctx(dst_ctx); |
| 10180 | } |
| 10181 | mutex_unlock(&dst_ctx->mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 10182 | mutex_unlock(&src_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 10183 | } |
| 10184 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 10185 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10186 | static void sync_child_event(struct perf_event *child_event, |
| 10187 | struct task_struct *child) |
| 10188 | { |
| 10189 | struct perf_event *parent_event = child_event->parent; |
| 10190 | u64 child_val; |
| 10191 | |
| 10192 | if (child_event->attr.inherit_stat) |
| 10193 | perf_event_read_event(child_event, child); |
| 10194 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 10195 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10196 | |
| 10197 | /* |
| 10198 | * Add back the child's count to the parent's count: |
| 10199 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 10200 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10201 | atomic64_add(child_event->total_time_enabled, |
| 10202 | &parent_event->child_total_time_enabled); |
| 10203 | atomic64_add(child_event->total_time_running, |
| 10204 | &parent_event->child_total_time_running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10205 | } |
| 10206 | |
| 10207 | static void |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 10208 | perf_event_exit_event(struct perf_event *child_event, |
| 10209 | struct perf_event_context *child_ctx, |
| 10210 | struct task_struct *child) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10211 | { |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 10212 | struct perf_event *parent_event = child_event->parent; |
| 10213 | |
Peter Zijlstra | 1903d50 | 2014-07-15 17:27:27 +0200 | [diff] [blame] | 10214 | /* |
| 10215 | * Do not destroy the 'original' grouping; because of the context |
| 10216 | * switch optimization the original events could've ended up in a |
| 10217 | * random child task. |
| 10218 | * |
| 10219 | * If we were to destroy the original group, all group related |
| 10220 | * operations would cease to function properly after this random |
| 10221 | * child dies. |
| 10222 | * |
| 10223 | * Do destroy all inherited groups, we don't care about those |
| 10224 | * and being thorough is better. |
| 10225 | */ |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 10226 | raw_spin_lock_irq(&child_ctx->lock); |
| 10227 | WARN_ON_ONCE(child_ctx->is_active); |
| 10228 | |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 10229 | if (parent_event) |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 10230 | perf_group_detach(child_event); |
| 10231 | list_del_event(child_event, child_ctx); |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 10232 | child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */ |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 10233 | raw_spin_unlock_irq(&child_ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10234 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10235 | /* |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 10236 | * Parent events are governed by their filedesc, retain them. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10237 | */ |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 10238 | if (!parent_event) { |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 10239 | perf_event_wakeup(child_event); |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 10240 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10241 | } |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 10242 | /* |
| 10243 | * Child events can be cleaned up. |
| 10244 | */ |
| 10245 | |
| 10246 | sync_child_event(child_event, child); |
| 10247 | |
| 10248 | /* |
| 10249 | * Remove this event from the parent's list |
| 10250 | */ |
| 10251 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 10252 | mutex_lock(&parent_event->child_mutex); |
| 10253 | list_del_init(&child_event->child_list); |
| 10254 | mutex_unlock(&parent_event->child_mutex); |
| 10255 | |
| 10256 | /* |
| 10257 | * Kick perf_poll() for is_event_hup(). |
| 10258 | */ |
| 10259 | perf_event_wakeup(parent_event); |
| 10260 | free_event(child_event); |
| 10261 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10262 | } |
| 10263 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10264 | 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] | 10265 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 10266 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 10267 | struct perf_event *child_event, *next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10268 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 10269 | WARN_ON_ONCE(child != current); |
| 10270 | |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 10271 | child_ctx = perf_pin_task_context(child, ctxn); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 10272 | if (!child_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10273 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10274 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10275 | /* |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 10276 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
| 10277 | * ctx::mutex over the entire thing. This serializes against almost |
| 10278 | * everything that wants to access the ctx. |
| 10279 | * |
| 10280 | * The exception is sys_perf_event_open() / |
| 10281 | * perf_event_create_kernel_count() which does find_get_context() |
| 10282 | * without ctx::mutex (it cannot because of the move_group double mutex |
| 10283 | * lock thing). See the comments in perf_install_in_context(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10284 | */ |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 10285 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10286 | |
| 10287 | /* |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 10288 | * In a single ctx::lock section, de-schedule the events and detach the |
| 10289 | * context from the task such that we cannot ever get it scheduled back |
| 10290 | * in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10291 | */ |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 10292 | raw_spin_lock_irq(&child_ctx->lock); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 10293 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 10294 | |
| 10295 | /* |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 10296 | * Now that the context is inactive, destroy the task <-> ctx relation |
| 10297 | * and mark the context dead. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10298 | */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 10299 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
| 10300 | put_ctx(child_ctx); /* cannot be last */ |
| 10301 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); |
| 10302 | put_task_struct(current); /* cannot be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10303 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 10304 | clone_ctx = unclone_ctx(child_ctx); |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 10305 | raw_spin_unlock_irq(&child_ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10306 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 10307 | if (clone_ctx) |
| 10308 | put_ctx(clone_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 10309 | |
| 10310 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10311 | * Report the task dead after unscheduling the events so that we |
| 10312 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 10313 | * get a few PERF_RECORD_READ events. |
| 10314 | */ |
| 10315 | perf_event_task(child, child_ctx, 0); |
| 10316 | |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 10317 | 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] | 10318 | perf_event_exit_event(child_event, child_ctx, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10319 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10320 | mutex_unlock(&child_ctx->mutex); |
| 10321 | |
| 10322 | put_ctx(child_ctx); |
| 10323 | } |
| 10324 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10325 | /* |
| 10326 | * When a child task exits, feed back event values to parent events. |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 10327 | * |
| 10328 | * Can be called with cred_guard_mutex held when called from |
| 10329 | * install_exec_creds(). |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10330 | */ |
| 10331 | void perf_event_exit_task(struct task_struct *child) |
| 10332 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 10333 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10334 | int ctxn; |
| 10335 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 10336 | mutex_lock(&child->perf_event_mutex); |
| 10337 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 10338 | owner_entry) { |
| 10339 | list_del_init(&event->owner_entry); |
| 10340 | |
| 10341 | /* |
| 10342 | * Ensure the list deletion is visible before we clear |
| 10343 | * the owner, closes a race against perf_release() where |
| 10344 | * we need to serialize on the owner->perf_event_mutex. |
| 10345 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 10346 | smp_store_release(&event->owner, NULL); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 10347 | } |
| 10348 | mutex_unlock(&child->perf_event_mutex); |
| 10349 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10350 | for_each_task_context_nr(ctxn) |
| 10351 | perf_event_exit_task_context(child, ctxn); |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 10352 | |
| 10353 | /* |
| 10354 | * The perf_event_exit_task_context calls perf_event_task |
| 10355 | * with child's task_ctx, which generates EXIT events for |
| 10356 | * child contexts and sets child->perf_event_ctxp[] to NULL. |
| 10357 | * At this point we need to send EXIT events to cpu contexts. |
| 10358 | */ |
| 10359 | perf_event_task(child, NULL, 0); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10360 | } |
| 10361 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10362 | static void perf_free_event(struct perf_event *event, |
| 10363 | struct perf_event_context *ctx) |
| 10364 | { |
| 10365 | struct perf_event *parent = event->parent; |
| 10366 | |
| 10367 | if (WARN_ON_ONCE(!parent)) |
| 10368 | return; |
| 10369 | |
| 10370 | mutex_lock(&parent->child_mutex); |
| 10371 | list_del_init(&event->child_list); |
| 10372 | mutex_unlock(&parent->child_mutex); |
| 10373 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 10374 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10375 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 10376 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 10377 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10378 | list_del_event(event, ctx); |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 10379 | raw_spin_unlock_irq(&ctx->lock); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10380 | free_event(event); |
| 10381 | } |
| 10382 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10383 | /* |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 10384 | * Free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10385 | * perf_event_init_task below, used by fork() in case of fail. |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 10386 | * |
| 10387 | * Not all locks are strictly required, but take them anyway to be nice and |
| 10388 | * help out with the lockdep assertions. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10389 | */ |
| 10390 | void perf_event_free_task(struct task_struct *task) |
| 10391 | { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10392 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10393 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10394 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10395 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10396 | for_each_task_context_nr(ctxn) { |
| 10397 | ctx = task->perf_event_ctxp[ctxn]; |
| 10398 | if (!ctx) |
| 10399 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10400 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10401 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | c04a938 | 2017-03-16 13:47:48 +0100 | [diff] [blame] | 10402 | raw_spin_lock_irq(&ctx->lock); |
| 10403 | /* |
| 10404 | * Destroy the task <-> ctx relation and mark the context dead. |
| 10405 | * |
| 10406 | * This is important because even though the task hasn't been |
| 10407 | * exposed yet the context has been (through child_list). |
| 10408 | */ |
| 10409 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL); |
| 10410 | WRITE_ONCE(ctx->task, TASK_TOMBSTONE); |
| 10411 | put_task_struct(task); /* cannot be last */ |
| 10412 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10413 | again: |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10414 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 10415 | group_entry) |
| 10416 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10417 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10418 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 10419 | group_entry) |
| 10420 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10421 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10422 | if (!list_empty(&ctx->pinned_groups) || |
| 10423 | !list_empty(&ctx->flexible_groups)) |
| 10424 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10425 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10426 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10427 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10428 | put_ctx(ctx); |
| 10429 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10430 | } |
| 10431 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 10432 | void perf_event_delayed_put(struct task_struct *task) |
| 10433 | { |
| 10434 | int ctxn; |
| 10435 | |
| 10436 | for_each_task_context_nr(ctxn) |
| 10437 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 10438 | } |
| 10439 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 10440 | struct file *perf_event_get(unsigned int fd) |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 10441 | { |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 10442 | struct file *file; |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 10443 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 10444 | file = fget_raw(fd); |
| 10445 | if (!file) |
| 10446 | return ERR_PTR(-EBADF); |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 10447 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 10448 | if (file->f_op != &perf_fops) { |
| 10449 | fput(file); |
| 10450 | return ERR_PTR(-EBADF); |
| 10451 | } |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 10452 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 10453 | return file; |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 10454 | } |
| 10455 | |
| 10456 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) |
| 10457 | { |
| 10458 | if (!event) |
| 10459 | return ERR_PTR(-EINVAL); |
| 10460 | |
| 10461 | return &event->attr; |
| 10462 | } |
| 10463 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10464 | /* |
| 10465 | * inherit a event from parent task to child task: |
| 10466 | */ |
| 10467 | static struct perf_event * |
| 10468 | inherit_event(struct perf_event *parent_event, |
| 10469 | struct task_struct *parent, |
| 10470 | struct perf_event_context *parent_ctx, |
| 10471 | struct task_struct *child, |
| 10472 | struct perf_event *group_leader, |
| 10473 | struct perf_event_context *child_ctx) |
| 10474 | { |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 10475 | enum perf_event_active_state parent_state = parent_event->state; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10476 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 10477 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10478 | |
| 10479 | /* |
| 10480 | * Instead of creating recursive hierarchies of events, |
| 10481 | * we link inherited events back to the original parent, |
| 10482 | * which has a filp for sure, which we use as the reference |
| 10483 | * count: |
| 10484 | */ |
| 10485 | if (parent_event->parent) |
| 10486 | parent_event = parent_event->parent; |
| 10487 | |
| 10488 | child_event = perf_event_alloc(&parent_event->attr, |
| 10489 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 10490 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10491 | group_leader, parent_event, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 10492 | NULL, NULL, -1); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10493 | if (IS_ERR(child_event)) |
| 10494 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 10495 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 10496 | /* |
| 10497 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) |
| 10498 | * must be under the same lock in order to serialize against |
| 10499 | * perf_event_release_kernel(), such that either we must observe |
| 10500 | * is_orphaned_event() or they will observe us on the child_list. |
| 10501 | */ |
| 10502 | mutex_lock(&parent_event->child_mutex); |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 10503 | if (is_orphaned_event(parent_event) || |
| 10504 | !atomic_long_inc_not_zero(&parent_event->refcount)) { |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 10505 | mutex_unlock(&parent_event->child_mutex); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 10506 | free_event(child_event); |
| 10507 | return NULL; |
| 10508 | } |
| 10509 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10510 | get_ctx(child_ctx); |
| 10511 | |
| 10512 | /* |
| 10513 | * Make the child state follow the state of the parent event, |
| 10514 | * not its attr.disabled bit. We hold the parent's mutex, |
| 10515 | * so we won't race with perf_event_{en, dis}able_family. |
| 10516 | */ |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 10517 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10518 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 10519 | else |
| 10520 | child_event->state = PERF_EVENT_STATE_OFF; |
| 10521 | |
| 10522 | if (parent_event->attr.freq) { |
| 10523 | u64 sample_period = parent_event->hw.sample_period; |
| 10524 | struct hw_perf_event *hwc = &child_event->hw; |
| 10525 | |
| 10526 | hwc->sample_period = sample_period; |
| 10527 | hwc->last_period = sample_period; |
| 10528 | |
| 10529 | local64_set(&hwc->period_left, sample_period); |
| 10530 | } |
| 10531 | |
| 10532 | child_event->ctx = child_ctx; |
| 10533 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 10534 | child_event->overflow_handler_context |
| 10535 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10536 | |
| 10537 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 10538 | * Precalculate sample_data sizes |
| 10539 | */ |
| 10540 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 10541 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 10542 | |
| 10543 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10544 | * Link it up in the child's context: |
| 10545 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 10546 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10547 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 10548 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10549 | |
| 10550 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10551 | * Link this into the parent event's child list |
| 10552 | */ |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10553 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 10554 | mutex_unlock(&parent_event->child_mutex); |
| 10555 | |
| 10556 | return child_event; |
| 10557 | } |
| 10558 | |
| 10559 | static int inherit_group(struct perf_event *parent_event, |
| 10560 | struct task_struct *parent, |
| 10561 | struct perf_event_context *parent_ctx, |
| 10562 | struct task_struct *child, |
| 10563 | struct perf_event_context *child_ctx) |
| 10564 | { |
| 10565 | struct perf_event *leader; |
| 10566 | struct perf_event *sub; |
| 10567 | struct perf_event *child_ctr; |
| 10568 | |
| 10569 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 10570 | child, NULL, child_ctx); |
| 10571 | if (IS_ERR(leader)) |
| 10572 | return PTR_ERR(leader); |
| 10573 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 10574 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 10575 | child, leader, child_ctx); |
| 10576 | if (IS_ERR(child_ctr)) |
| 10577 | return PTR_ERR(child_ctr); |
| 10578 | } |
| 10579 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10580 | } |
| 10581 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10582 | static int |
| 10583 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 10584 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10585 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10586 | int *inherited_all) |
| 10587 | { |
| 10588 | int ret; |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10589 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10590 | |
| 10591 | if (!event->attr.inherit) { |
| 10592 | *inherited_all = 0; |
| 10593 | return 0; |
| 10594 | } |
| 10595 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 10596 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10597 | if (!child_ctx) { |
| 10598 | /* |
| 10599 | * This is executed from the parent task context, so |
| 10600 | * inherit events that have been marked for cloning. |
| 10601 | * First allocate and initialize a context for the |
| 10602 | * child. |
| 10603 | */ |
| 10604 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 10605 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10606 | if (!child_ctx) |
| 10607 | return -ENOMEM; |
| 10608 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10609 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10610 | } |
| 10611 | |
| 10612 | ret = inherit_group(event, parent, parent_ctx, |
| 10613 | child, child_ctx); |
| 10614 | |
| 10615 | if (ret) |
| 10616 | *inherited_all = 0; |
| 10617 | |
| 10618 | return ret; |
| 10619 | } |
| 10620 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10621 | /* |
| 10622 | * Initialize the perf_event context in task_struct |
| 10623 | */ |
Jiri Olsa | 985c8dc | 2014-06-24 10:20:24 +0200 | [diff] [blame] | 10624 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10625 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10626 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10627 | struct perf_event_context *cloned_ctx; |
| 10628 | struct perf_event *event; |
| 10629 | struct task_struct *parent = current; |
| 10630 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10631 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10632 | int ret = 0; |
| 10633 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10634 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10635 | return 0; |
| 10636 | |
| 10637 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10638 | * If the parent's context is a clone, pin it so it won't get |
| 10639 | * swapped under us. |
| 10640 | */ |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10641 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Peter Zijlstra | ffb4ef2 | 2014-05-05 19:12:20 +0200 | [diff] [blame] | 10642 | if (!parent_ctx) |
| 10643 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10644 | |
| 10645 | /* |
| 10646 | * No need to check if parent_ctx != NULL here; since we saw |
| 10647 | * it non-NULL earlier, the only reason for it to become NULL |
| 10648 | * is if we exit, and since we're currently in the middle of |
| 10649 | * a fork we can't be exiting at the same time. |
| 10650 | */ |
| 10651 | |
| 10652 | /* |
| 10653 | * Lock the parent list. No need to lock the child - not PID |
| 10654 | * hashed yet and not running, so nobody can access it. |
| 10655 | */ |
| 10656 | mutex_lock(&parent_ctx->mutex); |
| 10657 | |
| 10658 | /* |
| 10659 | * We dont have to disable NMIs - we are only looking at |
| 10660 | * the list, not manipulating it: |
| 10661 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10662 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10663 | ret = inherit_task_group(event, parent, parent_ctx, |
| 10664 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10665 | if (ret) |
Peter Zijlstra | 69efd8e | 2017-03-16 13:47:49 +0100 | [diff] [blame] | 10666 | goto out_unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10667 | } |
| 10668 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10669 | /* |
| 10670 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 10671 | * to allocations, but we need to prevent rotation because |
| 10672 | * rotate_ctx() will change the list from interrupt context. |
| 10673 | */ |
| 10674 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 10675 | parent_ctx->rotate_disable = 1; |
| 10676 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 10677 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10678 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10679 | ret = inherit_task_group(event, parent, parent_ctx, |
| 10680 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10681 | if (ret) |
Peter Zijlstra | 69efd8e | 2017-03-16 13:47:49 +0100 | [diff] [blame] | 10682 | goto out_unlock; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10683 | } |
| 10684 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10685 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 10686 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10687 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10688 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10689 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 10690 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10691 | /* |
| 10692 | * Mark the child context as a clone of the parent |
| 10693 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 10694 | * |
| 10695 | * Note that if the parent is a clone, the holding of |
| 10696 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10697 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 10698 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10699 | if (cloned_ctx) { |
| 10700 | child_ctx->parent_ctx = cloned_ctx; |
| 10701 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 10702 | } else { |
| 10703 | child_ctx->parent_ctx = parent_ctx; |
| 10704 | child_ctx->parent_gen = parent_ctx->generation; |
| 10705 | } |
| 10706 | get_ctx(child_ctx->parent_ctx); |
| 10707 | } |
| 10708 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 10709 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Peter Zijlstra | 69efd8e | 2017-03-16 13:47:49 +0100 | [diff] [blame] | 10710 | out_unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10711 | mutex_unlock(&parent_ctx->mutex); |
| 10712 | |
| 10713 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 10714 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10715 | |
| 10716 | return ret; |
| 10717 | } |
| 10718 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10719 | /* |
| 10720 | * Initialize the perf_event context in task_struct |
| 10721 | */ |
| 10722 | int perf_event_init_task(struct task_struct *child) |
| 10723 | { |
| 10724 | int ctxn, ret; |
| 10725 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 10726 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 10727 | mutex_init(&child->perf_event_mutex); |
| 10728 | INIT_LIST_HEAD(&child->perf_event_list); |
| 10729 | |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10730 | for_each_task_context_nr(ctxn) { |
| 10731 | ret = perf_event_init_context(child, ctxn); |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 10732 | if (ret) { |
| 10733 | perf_event_free_task(child); |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10734 | return ret; |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 10735 | } |
Peter Zijlstra | 8dc85d5 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10736 | } |
| 10737 | |
| 10738 | return 0; |
| 10739 | } |
| 10740 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10741 | static void __init perf_event_init_all_cpus(void) |
| 10742 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10743 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10744 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10745 | |
| 10746 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10747 | swhash = &per_cpu(swevent_htable, cpu); |
| 10748 | mutex_init(&swhash->hlist_mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 10749 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 10750 | |
| 10751 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); |
| 10752 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); |
Peter Zijlstra | e48c178 | 2016-07-06 09:18:30 +0200 | [diff] [blame] | 10753 | |
| 10754 | INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10755 | } |
| 10756 | } |
| 10757 | |
Thomas Gleixner | 00e16c3 | 2016-07-13 17:16:09 +0000 | [diff] [blame] | 10758 | int perf_event_init_cpu(unsigned int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10759 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10760 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10761 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10762 | mutex_lock(&swhash->hlist_mutex); |
Thomas Gleixner | 059fcd8 | 2016-02-09 20:11:34 +0000 | [diff] [blame] | 10763 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 10764 | struct swevent_hlist *hlist; |
| 10765 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10766 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 10767 | WARN_ON(!hlist); |
| 10768 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 10769 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10770 | mutex_unlock(&swhash->hlist_mutex); |
Thomas Gleixner | 00e16c3 | 2016-07-13 17:16:09 +0000 | [diff] [blame] | 10771 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10772 | } |
| 10773 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 10774 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10775 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10776 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10777 | struct perf_event_context *ctx = __info; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 10778 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 10779 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10780 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 10781 | raw_spin_lock(&ctx->lock); |
| 10782 | list_for_each_entry(event, &ctx->event_list, event_entry) |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 10783 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 10784 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10785 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10786 | |
| 10787 | static void perf_event_exit_cpu_context(int cpu) |
| 10788 | { |
| 10789 | struct perf_event_context *ctx; |
| 10790 | struct pmu *pmu; |
| 10791 | int idx; |
| 10792 | |
| 10793 | idx = srcu_read_lock(&pmus_srcu); |
| 10794 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 10795 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10796 | |
| 10797 | mutex_lock(&ctx->mutex); |
| 10798 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 10799 | mutex_unlock(&ctx->mutex); |
| 10800 | } |
| 10801 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10802 | } |
Thomas Gleixner | 00e16c3 | 2016-07-13 17:16:09 +0000 | [diff] [blame] | 10803 | #else |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10804 | |
Thomas Gleixner | 00e16c3 | 2016-07-13 17:16:09 +0000 | [diff] [blame] | 10805 | static void perf_event_exit_cpu_context(int cpu) { } |
| 10806 | |
| 10807 | #endif |
| 10808 | |
| 10809 | int perf_event_exit_cpu(unsigned int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10810 | { |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 10811 | perf_event_exit_cpu_context(cpu); |
Thomas Gleixner | 00e16c3 | 2016-07-13 17:16:09 +0000 | [diff] [blame] | 10812 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10813 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10814 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 10815 | static int |
| 10816 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 10817 | { |
| 10818 | int cpu; |
| 10819 | |
| 10820 | for_each_online_cpu(cpu) |
| 10821 | perf_event_exit_cpu(cpu); |
| 10822 | |
| 10823 | return NOTIFY_OK; |
| 10824 | } |
| 10825 | |
| 10826 | /* |
| 10827 | * Run the perf reboot notifier at the very last possible moment so that |
| 10828 | * the generic watchdog code runs as long as possible. |
| 10829 | */ |
| 10830 | static struct notifier_block perf_reboot_notifier = { |
| 10831 | .notifier_call = perf_reboot, |
| 10832 | .priority = INT_MIN, |
| 10833 | }; |
| 10834 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10835 | void __init perf_event_init(void) |
| 10836 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 10837 | int ret; |
| 10838 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 10839 | idr_init(&pmu_idr); |
| 10840 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10841 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 10842 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 10843 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 10844 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 10845 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 10846 | perf_tp_register(); |
Thomas Gleixner | 00e16c3 | 2016-07-13 17:16:09 +0000 | [diff] [blame] | 10847 | perf_event_init_cpu(smp_processor_id()); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 10848 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 10849 | |
| 10850 | ret = init_hw_breakpoint(); |
| 10851 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 10852 | |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 10853 | /* |
| 10854 | * Build time assertion that we keep the data_head at the intended |
| 10855 | * location. IOW, validation we got the __reserved[] size right. |
| 10856 | */ |
| 10857 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 10858 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10859 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 10860 | |
Cody P Schafer | fd979c0 | 2015-01-30 13:45:57 -0800 | [diff] [blame] | 10861 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
| 10862 | char *page) |
| 10863 | { |
| 10864 | struct perf_pmu_events_attr *pmu_attr = |
| 10865 | container_of(attr, struct perf_pmu_events_attr, attr); |
| 10866 | |
| 10867 | if (pmu_attr->event_str) |
| 10868 | return sprintf(page, "%s\n", pmu_attr->event_str); |
| 10869 | |
| 10870 | return 0; |
| 10871 | } |
Thomas Gleixner | 675965b | 2016-02-22 22:19:27 +0000 | [diff] [blame] | 10872 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
Cody P Schafer | fd979c0 | 2015-01-30 13:45:57 -0800 | [diff] [blame] | 10873 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 10874 | static int __init perf_event_sysfs_init(void) |
| 10875 | { |
| 10876 | struct pmu *pmu; |
| 10877 | int ret; |
| 10878 | |
| 10879 | mutex_lock(&pmus_lock); |
| 10880 | |
| 10881 | ret = bus_register(&pmu_bus); |
| 10882 | if (ret) |
| 10883 | goto unlock; |
| 10884 | |
| 10885 | list_for_each_entry(pmu, &pmus, entry) { |
| 10886 | if (!pmu->name || pmu->type < 0) |
| 10887 | continue; |
| 10888 | |
| 10889 | ret = pmu_dev_alloc(pmu); |
| 10890 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 10891 | } |
| 10892 | pmu_bus_running = 1; |
| 10893 | ret = 0; |
| 10894 | |
| 10895 | unlock: |
| 10896 | mutex_unlock(&pmus_lock); |
| 10897 | |
| 10898 | return ret; |
| 10899 | } |
| 10900 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10901 | |
| 10902 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 10903 | static struct cgroup_subsys_state * |
| 10904 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10905 | { |
| 10906 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10907 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 10908 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10909 | if (!jc) |
| 10910 | return ERR_PTR(-ENOMEM); |
| 10911 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10912 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 10913 | if (!jc->info) { |
| 10914 | kfree(jc); |
| 10915 | return ERR_PTR(-ENOMEM); |
| 10916 | } |
| 10917 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10918 | return &jc->css; |
| 10919 | } |
| 10920 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 10921 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10922 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 10923 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
| 10924 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10925 | free_percpu(jc->info); |
| 10926 | kfree(jc); |
| 10927 | } |
| 10928 | |
| 10929 | static int __perf_cgroup_move(void *info) |
| 10930 | { |
| 10931 | struct task_struct *task = info; |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 10932 | rcu_read_lock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10933 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 10934 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10935 | return 0; |
| 10936 | } |
| 10937 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 10938 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10939 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10940 | struct task_struct *task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 10941 | struct cgroup_subsys_state *css; |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10942 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 10943 | cgroup_taskset_for_each(task, css, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10944 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10945 | } |
| 10946 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 10947 | struct cgroup_subsys perf_event_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 10948 | .css_alloc = perf_cgroup_css_alloc, |
| 10949 | .css_free = perf_cgroup_css_free, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10950 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10951 | }; |
| 10952 | #endif /* CONFIG_CGROUP_PERF */ |