Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 2 | * Performance events core code: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 5 | * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 6 | * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra |
Al Viro | d36b691 | 2011-12-29 17:09:01 -0500 | [diff] [blame] | 7 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8 | * |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 9 | * For licensing details see kernel-base/COPYING |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/cpu.h> |
| 15 | #include <linux/smp.h> |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 16 | #include <linux/idr.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 17 | #include <linux/file.h> |
| 18 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 20 | #include <linux/hash.h> |
Frederic Weisbecker | 12351ef | 2013-04-20 15:48:22 +0200 | [diff] [blame] | 21 | #include <linux/tick.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 22 | #include <linux/sysfs.h> |
| 23 | #include <linux/dcache.h> |
| 24 | #include <linux/percpu.h> |
| 25 | #include <linux/ptrace.h> |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 26 | #include <linux/reboot.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 27 | #include <linux/vmstat.h> |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 28 | #include <linux/device.h> |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 31 | #include <linux/hardirq.h> |
| 32 | #include <linux/rculist.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/syscalls.h> |
| 35 | #include <linux/anon_inodes.h> |
| 36 | #include <linux/kernel_stat.h> |
Matt Fleming | 39bed6c | 2015-01-23 18:45:40 +0000 | [diff] [blame] | 37 | #include <linux/cgroup.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 38 | #include <linux/perf_event.h> |
Steven Rostedt (Red Hat) | af658dc | 2015-04-29 14:36:05 -0400 | [diff] [blame] | 39 | #include <linux/trace_events.h> |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 40 | #include <linux/hw_breakpoint.h> |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 41 | #include <linux/mm_types.h> |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 42 | #include <linux/module.h> |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 43 | #include <linux/mman.h> |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 44 | #include <linux/compat.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 45 | #include <linux/bpf.h> |
| 46 | #include <linux/filter.h> |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 47 | #include <linux/namei.h> |
| 48 | #include <linux/parser.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 49 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 50 | #include "internal.h" |
| 51 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 52 | #include <asm/irq_regs.h> |
| 53 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 54 | typedef int (*remote_function_f)(void *); |
| 55 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 56 | struct remote_function_call { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 57 | struct task_struct *p; |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 58 | remote_function_f func; |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 59 | void *info; |
| 60 | int ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static void remote_function(void *data) |
| 64 | { |
| 65 | struct remote_function_call *tfc = data; |
| 66 | struct task_struct *p = tfc->p; |
| 67 | |
| 68 | if (p) { |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 69 | /* -EAGAIN */ |
| 70 | if (task_cpu(p) != smp_processor_id()) |
| 71 | return; |
| 72 | |
| 73 | /* |
| 74 | * Now that we're on right CPU with IRQs disabled, we can test |
| 75 | * if we hit the right task without races. |
| 76 | */ |
| 77 | |
| 78 | tfc->ret = -ESRCH; /* No such (running) process */ |
| 79 | if (p != current) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
| 83 | tfc->ret = tfc->func(tfc->info); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * task_function_call - call a function on the cpu on which a task runs |
| 88 | * @p: the task to evaluate |
| 89 | * @func: the function to be called |
| 90 | * @info: the function call argument |
| 91 | * |
| 92 | * Calls the function @func when the task is currently running. This might |
| 93 | * be on the current CPU, which just calls the function directly |
| 94 | * |
| 95 | * returns: @func return value, or |
| 96 | * -ESRCH - when the process isn't running |
| 97 | * -EAGAIN - when the process moved away |
| 98 | */ |
| 99 | static int |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 100 | task_function_call(struct task_struct *p, remote_function_f func, void *info) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 101 | { |
| 102 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 103 | .p = p, |
| 104 | .func = func, |
| 105 | .info = info, |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 106 | .ret = -EAGAIN, |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 107 | }; |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 108 | int ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 109 | |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 110 | do { |
| 111 | ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1); |
| 112 | if (!ret) |
| 113 | ret = data.ret; |
| 114 | } while (ret == -EAGAIN); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 115 | |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 116 | return ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * cpu_function_call - call a function on the cpu |
| 121 | * @func: the function to be called |
| 122 | * @info: the function call argument |
| 123 | * |
| 124 | * Calls the function @func on the remote cpu. |
| 125 | * |
| 126 | * returns: @func return value or -ENXIO when the cpu is offline |
| 127 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 128 | static int cpu_function_call(int cpu, remote_function_f func, void *info) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 129 | { |
| 130 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 131 | .p = NULL, |
| 132 | .func = func, |
| 133 | .info = info, |
| 134 | .ret = -ENXIO, /* No such CPU */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | smp_call_function_single(cpu, remote_function, &data, 1); |
| 138 | |
| 139 | return data.ret; |
| 140 | } |
| 141 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 142 | static inline struct perf_cpu_context * |
| 143 | __get_cpu_context(struct perf_event_context *ctx) |
| 144 | { |
| 145 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); |
| 146 | } |
| 147 | |
| 148 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, |
| 149 | struct perf_event_context *ctx) |
| 150 | { |
| 151 | raw_spin_lock(&cpuctx->ctx.lock); |
| 152 | if (ctx) |
| 153 | raw_spin_lock(&ctx->lock); |
| 154 | } |
| 155 | |
| 156 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, |
| 157 | struct perf_event_context *ctx) |
| 158 | { |
| 159 | if (ctx) |
| 160 | raw_spin_unlock(&ctx->lock); |
| 161 | raw_spin_unlock(&cpuctx->ctx.lock); |
| 162 | } |
| 163 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 164 | #define TASK_TOMBSTONE ((void *)-1L) |
| 165 | |
| 166 | static bool is_kernel_event(struct perf_event *event) |
| 167 | { |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 168 | return READ_ONCE(event->owner) == TASK_TOMBSTONE; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 171 | /* |
| 172 | * On task ctx scheduling... |
| 173 | * |
| 174 | * When !ctx->nr_events a task context will not be scheduled. This means |
| 175 | * we can disable the scheduler hooks (for performance) without leaving |
| 176 | * pending task ctx state. |
| 177 | * |
| 178 | * This however results in two special cases: |
| 179 | * |
| 180 | * - removing the last event from a task ctx; this is relatively straight |
| 181 | * forward and is done in __perf_remove_from_context. |
| 182 | * |
| 183 | * - adding the first event to a task ctx; this is tricky because we cannot |
| 184 | * rely on ctx->is_active and therefore cannot use event_function_call(). |
| 185 | * See perf_install_in_context(). |
| 186 | * |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 187 | * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. |
| 188 | */ |
| 189 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 190 | typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, |
| 191 | struct perf_event_context *, void *); |
| 192 | |
| 193 | struct event_function_struct { |
| 194 | struct perf_event *event; |
| 195 | event_f func; |
| 196 | void *data; |
| 197 | }; |
| 198 | |
| 199 | static int event_function(void *info) |
| 200 | { |
| 201 | struct event_function_struct *efs = info; |
| 202 | struct perf_event *event = efs->event; |
| 203 | struct perf_event_context *ctx = event->ctx; |
| 204 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 205 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 206 | int ret = 0; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 207 | |
| 208 | WARN_ON_ONCE(!irqs_disabled()); |
| 209 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 210 | perf_ctx_lock(cpuctx, task_ctx); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 211 | /* |
| 212 | * Since we do the IPI call without holding ctx->lock things can have |
| 213 | * changed, double check we hit the task we set out to hit. |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 214 | */ |
| 215 | if (ctx->task) { |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 216 | if (ctx->task != current) { |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 217 | ret = -ESRCH; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 218 | goto unlock; |
| 219 | } |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 220 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 221 | /* |
| 222 | * We only use event_function_call() on established contexts, |
| 223 | * and event_function() is only ever called when active (or |
| 224 | * rather, we'll have bailed in task_function_call() or the |
| 225 | * above ctx->task != current test), therefore we must have |
| 226 | * ctx->is_active here. |
| 227 | */ |
| 228 | WARN_ON_ONCE(!ctx->is_active); |
| 229 | /* |
| 230 | * And since we have ctx->is_active, cpuctx->task_ctx must |
| 231 | * match. |
| 232 | */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 233 | WARN_ON_ONCE(task_ctx != ctx); |
| 234 | } else { |
| 235 | WARN_ON_ONCE(&cpuctx->ctx != ctx); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 236 | } |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 237 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 238 | efs->func(event, cpuctx, ctx, efs->data); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 239 | unlock: |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 240 | perf_ctx_unlock(cpuctx, task_ctx); |
| 241 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 242 | return ret; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static void event_function_local(struct perf_event *event, event_f func, void *data) |
| 246 | { |
| 247 | struct event_function_struct efs = { |
| 248 | .event = event, |
| 249 | .func = func, |
| 250 | .data = data, |
| 251 | }; |
| 252 | |
| 253 | int ret = event_function(&efs); |
| 254 | WARN_ON_ONCE(ret); |
| 255 | } |
| 256 | |
| 257 | static void event_function_call(struct perf_event *event, event_f func, void *data) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 258 | { |
| 259 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 260 | struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 261 | struct event_function_struct efs = { |
| 262 | .event = event, |
| 263 | .func = func, |
| 264 | .data = data, |
| 265 | }; |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 266 | |
Peter Zijlstra | c97f473 | 2016-01-14 10:51:03 +0100 | [diff] [blame] | 267 | if (!event->parent) { |
| 268 | /* |
| 269 | * If this is a !child event, we must hold ctx::mutex to |
| 270 | * stabilize the the event->ctx relation. See |
| 271 | * perf_event_ctx_lock(). |
| 272 | */ |
| 273 | lockdep_assert_held(&ctx->mutex); |
| 274 | } |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 275 | |
| 276 | if (!task) { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 277 | cpu_function_call(event->cpu, event_function, &efs); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 281 | if (task == TASK_TOMBSTONE) |
| 282 | return; |
| 283 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 284 | again: |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 285 | if (!task_function_call(task, event_function, &efs)) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 286 | return; |
| 287 | |
| 288 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 289 | /* |
| 290 | * Reload the task pointer, it might have been changed by |
| 291 | * a concurrent perf_event_context_sched_out(). |
| 292 | */ |
| 293 | task = ctx->task; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 294 | if (task == TASK_TOMBSTONE) { |
| 295 | raw_spin_unlock_irq(&ctx->lock); |
| 296 | return; |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 297 | } |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 298 | if (ctx->is_active) { |
| 299 | raw_spin_unlock_irq(&ctx->lock); |
| 300 | goto again; |
| 301 | } |
| 302 | func(event, NULL, ctx, data); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 303 | raw_spin_unlock_irq(&ctx->lock); |
| 304 | } |
| 305 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 306 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
| 307 | PERF_FLAG_FD_OUTPUT |\ |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 308 | PERF_FLAG_PID_CGROUP |\ |
| 309 | PERF_FLAG_FD_CLOEXEC) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 310 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 311 | /* |
| 312 | * branch priv levels that need permission checks |
| 313 | */ |
| 314 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ |
| 315 | (PERF_SAMPLE_BRANCH_KERNEL |\ |
| 316 | PERF_SAMPLE_BRANCH_HV) |
| 317 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 318 | enum event_type_t { |
| 319 | EVENT_FLEXIBLE = 0x1, |
| 320 | EVENT_PINNED = 0x2, |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 321 | EVENT_TIME = 0x4, |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 322 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
| 323 | }; |
| 324 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 325 | /* |
| 326 | * perf_sched_events : >0 events exist |
| 327 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu |
| 328 | */ |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 329 | |
| 330 | static void perf_sched_delayed(struct work_struct *work); |
| 331 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); |
| 332 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); |
| 333 | static DEFINE_MUTEX(perf_sched_mutex); |
| 334 | static atomic_t perf_sched_count; |
| 335 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 336 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 337 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 338 | static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 339 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 340 | static atomic_t nr_mmap_events __read_mostly; |
| 341 | static atomic_t nr_comm_events __read_mostly; |
| 342 | static atomic_t nr_task_events __read_mostly; |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 343 | static atomic_t nr_freq_events __read_mostly; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 344 | static atomic_t nr_switch_events __read_mostly; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 345 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 346 | static LIST_HEAD(pmus); |
| 347 | static DEFINE_MUTEX(pmus_lock); |
| 348 | static struct srcu_struct pmus_srcu; |
| 349 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 350 | /* |
| 351 | * perf event paranoia level: |
| 352 | * -1 - not paranoid at all |
| 353 | * 0 - disallow raw tracepoint access for unpriv |
| 354 | * 1 - disallow cpu events for unpriv |
| 355 | * 2 - disallow kernel profiling for unpriv |
| 356 | */ |
Andy Lutomirski | 0161028 | 2016-05-09 15:48:51 -0700 | [diff] [blame] | 357 | int sysctl_perf_event_paranoid __read_mostly = 2; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 358 | |
Frederic Weisbecker | 2044338 | 2011-03-31 03:33:29 +0200 | [diff] [blame] | 359 | /* Minimum for 512 kiB + 1 user control page */ |
| 360 | 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] | 361 | |
| 362 | /* |
| 363 | * max perf event sample rate |
| 364 | */ |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 365 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
| 366 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) |
| 367 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 |
| 368 | |
| 369 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; |
| 370 | |
| 371 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); |
| 372 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; |
| 373 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 374 | static int perf_sample_allowed_ns __read_mostly = |
| 375 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 376 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 377 | static void update_perf_cpu_limits(void) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 378 | { |
| 379 | u64 tmp = perf_sample_period_ns; |
| 380 | |
| 381 | tmp *= sysctl_perf_cpu_time_max_percent; |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 382 | tmp = div_u64(tmp, 100); |
| 383 | if (!tmp) |
| 384 | tmp = 1; |
| 385 | |
| 386 | WRITE_ONCE(perf_sample_allowed_ns, tmp); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 387 | } |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 388 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 389 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
| 390 | |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 391 | int perf_proc_update_handler(struct ctl_table *table, int write, |
| 392 | void __user *buffer, size_t *lenp, |
| 393 | loff_t *ppos) |
| 394 | { |
Knut Petersen | 723478c | 2013-09-25 14:29:37 +0200 | [diff] [blame] | 395 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 396 | |
| 397 | if (ret || !write) |
| 398 | return ret; |
| 399 | |
| 400 | 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] | 401 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 402 | update_perf_cpu_limits(); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 403 | |
| 404 | return 0; |
| 405 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 406 | |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 407 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; |
| 408 | |
| 409 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, |
| 410 | void __user *buffer, size_t *lenp, |
| 411 | loff_t *ppos) |
| 412 | { |
| 413 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 414 | |
| 415 | if (ret || !write) |
| 416 | return ret; |
| 417 | |
Peter Zijlstra | b303e7c | 2016-04-04 09:57:40 +0200 | [diff] [blame] | 418 | if (sysctl_perf_cpu_time_max_percent == 100 || |
| 419 | sysctl_perf_cpu_time_max_percent == 0) { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 420 | printk(KERN_WARNING |
| 421 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); |
| 422 | WRITE_ONCE(perf_sample_allowed_ns, 0); |
| 423 | } else { |
| 424 | update_perf_cpu_limits(); |
| 425 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * perf samples are done in some very critical code paths (NMIs). |
| 432 | * If they take too much CPU time, the system can lock up and not |
| 433 | * get any real work done. This will drop the sample rate when |
| 434 | * we detect that events are taking too long. |
| 435 | */ |
| 436 | #define NR_ACCUMULATED_SAMPLES 128 |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 437 | static DEFINE_PER_CPU(u64, running_sample_length); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 438 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 439 | static u64 __report_avg; |
| 440 | static u64 __report_allowed; |
| 441 | |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 442 | static void perf_duration_warn(struct irq_work *w) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 443 | { |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 444 | printk_ratelimited(KERN_WARNING |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 445 | "perf: interrupt took too long (%lld > %lld), lowering " |
| 446 | "kernel.perf_event_max_sample_rate to %d\n", |
| 447 | __report_avg, __report_allowed, |
| 448 | sysctl_perf_event_sample_rate); |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); |
| 452 | |
| 453 | void perf_sample_event_took(u64 sample_len_ns) |
| 454 | { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 455 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
| 456 | u64 running_len; |
| 457 | u64 avg_len; |
| 458 | u32 max; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 459 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 460 | if (max_len == 0) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 461 | return; |
| 462 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 463 | /* Decay the counter by 1 average sample. */ |
| 464 | running_len = __this_cpu_read(running_sample_length); |
| 465 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; |
| 466 | running_len += sample_len_ns; |
| 467 | __this_cpu_write(running_sample_length, running_len); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 468 | |
| 469 | /* |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 470 | * Note: this will be biased artifically low until we have |
| 471 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 472 | * from having to maintain a count. |
| 473 | */ |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 474 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
| 475 | if (avg_len <= max_len) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 476 | return; |
| 477 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 478 | __report_avg = avg_len; |
| 479 | __report_allowed = max_len; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 480 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 481 | /* |
| 482 | * Compute a throttle threshold 25% below the current duration. |
| 483 | */ |
| 484 | avg_len += avg_len / 4; |
| 485 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; |
| 486 | if (avg_len < max) |
| 487 | max /= (u32)avg_len; |
| 488 | else |
| 489 | max = 1; |
| 490 | |
| 491 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
| 492 | WRITE_ONCE(max_samples_per_tick, max); |
| 493 | |
| 494 | sysctl_perf_event_sample_rate = max * HZ; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 495 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 496 | |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 497 | if (!irq_work_queue(&perf_duration_work)) { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 498 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 499 | "kernel.perf_event_max_sample_rate to %d\n", |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame] | 500 | __report_avg, __report_allowed, |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 501 | sysctl_perf_event_sample_rate); |
| 502 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 505 | static atomic64_t perf_event_id; |
| 506 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 507 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 508 | enum event_type_t event_type); |
| 509 | |
| 510 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 511 | enum event_type_t event_type, |
| 512 | struct task_struct *task); |
| 513 | |
| 514 | static void update_context_time(struct perf_event_context *ctx); |
| 515 | static u64 perf_event_time(struct perf_event *event); |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 516 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 517 | void __weak perf_event_print_debug(void) { } |
| 518 | |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 519 | extern __weak const char *perf_pmu_name(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 520 | { |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 521 | return "pmu"; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 524 | static inline u64 perf_clock(void) |
| 525 | { |
| 526 | return local_clock(); |
| 527 | } |
| 528 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 529 | static inline u64 perf_event_clock(struct perf_event *event) |
| 530 | { |
| 531 | return event->clock(); |
| 532 | } |
| 533 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 534 | #ifdef CONFIG_CGROUP_PERF |
| 535 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 536 | static inline bool |
| 537 | perf_cgroup_match(struct perf_event *event) |
| 538 | { |
| 539 | struct perf_event_context *ctx = event->ctx; |
| 540 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 541 | |
Tejun Heo | ef824fa | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 542 | /* @event doesn't care about cgroup */ |
| 543 | if (!event->cgrp) |
| 544 | return true; |
| 545 | |
| 546 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ |
| 547 | if (!cpuctx->cgrp) |
| 548 | return false; |
| 549 | |
| 550 | /* |
| 551 | * Cgroup scoping is recursive. An event enabled for a cgroup is |
| 552 | * also enabled for all its descendant cgroups. If @cpuctx's |
| 553 | * cgroup is a descendant of @event's (the test covers identity |
| 554 | * case), it's a match. |
| 555 | */ |
| 556 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, |
| 557 | event->cgrp->css.cgroup); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 560 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 561 | { |
Zefan Li | 4e2ba65 | 2014-09-19 16:53:14 +0800 | [diff] [blame] | 562 | css_put(&event->cgrp->css); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 563 | event->cgrp = NULL; |
| 564 | } |
| 565 | |
| 566 | static inline int is_cgroup_event(struct perf_event *event) |
| 567 | { |
| 568 | return event->cgrp != NULL; |
| 569 | } |
| 570 | |
| 571 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 572 | { |
| 573 | struct perf_cgroup_info *t; |
| 574 | |
| 575 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 576 | return t->time; |
| 577 | } |
| 578 | |
| 579 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) |
| 580 | { |
| 581 | struct perf_cgroup_info *info; |
| 582 | u64 now; |
| 583 | |
| 584 | now = perf_clock(); |
| 585 | |
| 586 | info = this_cpu_ptr(cgrp->info); |
| 587 | |
| 588 | info->time += now - info->timestamp; |
| 589 | info->timestamp = now; |
| 590 | } |
| 591 | |
| 592 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 593 | { |
| 594 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; |
| 595 | if (cgrp_out) |
| 596 | __update_cgrp_time(cgrp_out); |
| 597 | } |
| 598 | |
| 599 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 600 | { |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 601 | struct perf_cgroup *cgrp; |
| 602 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 603 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 604 | * ensure we access cgroup data only when needed and |
| 605 | * when we know the cgroup is pinned (css_get) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 606 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 607 | if (!is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 608 | return; |
| 609 | |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 610 | cgrp = perf_cgroup_from_task(current, event->ctx); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 611 | /* |
| 612 | * Do not update time when cgroup is not active |
| 613 | */ |
| 614 | if (cgrp == event->cgrp) |
| 615 | __update_cgrp_time(event->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 619 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 620 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 621 | { |
| 622 | struct perf_cgroup *cgrp; |
| 623 | struct perf_cgroup_info *info; |
| 624 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 625 | /* |
| 626 | * ctx->lock held by caller |
| 627 | * ensure we do not access cgroup data |
| 628 | * unless we have the cgroup pinned (css_get) |
| 629 | */ |
| 630 | if (!task || !ctx->nr_cgroups) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 631 | return; |
| 632 | |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 633 | cgrp = perf_cgroup_from_task(task, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 634 | info = this_cpu_ptr(cgrp->info); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 635 | info->timestamp = ctx->timestamp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
| 639 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ |
| 640 | |
| 641 | /* |
| 642 | * reschedule events based on the cgroup constraint of task. |
| 643 | * |
| 644 | * mode SWOUT : schedule out everything |
| 645 | * mode SWIN : schedule in based on cgroup for next |
| 646 | */ |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 647 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 648 | { |
| 649 | struct perf_cpu_context *cpuctx; |
| 650 | struct pmu *pmu; |
| 651 | unsigned long flags; |
| 652 | |
| 653 | /* |
| 654 | * disable interrupts to avoid geting nr_cgroup |
| 655 | * changes via __perf_event_disable(). Also |
| 656 | * avoids preemption. |
| 657 | */ |
| 658 | local_irq_save(flags); |
| 659 | |
| 660 | /* |
| 661 | * we reschedule only in the presence of cgroup |
| 662 | * constrained events. |
| 663 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 664 | |
| 665 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 666 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 667 | if (cpuctx->unique_pmu != pmu) |
| 668 | continue; /* ensure we process each cpuctx once */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 669 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 670 | /* |
| 671 | * perf_cgroup_events says at least one |
| 672 | * context on this CPU has cgroup events. |
| 673 | * |
| 674 | * ctx->nr_cgroups reports the number of cgroup |
| 675 | * events for a context. |
| 676 | */ |
| 677 | if (cpuctx->ctx.nr_cgroups > 0) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 678 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 679 | perf_pmu_disable(cpuctx->ctx.pmu); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 680 | |
| 681 | if (mode & PERF_CGROUP_SWOUT) { |
| 682 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 683 | /* |
| 684 | * must not be done before ctxswout due |
| 685 | * to event_filter_match() in event_sched_out() |
| 686 | */ |
| 687 | cpuctx->cgrp = NULL; |
| 688 | } |
| 689 | |
| 690 | if (mode & PERF_CGROUP_SWIN) { |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 691 | WARN_ON_ONCE(cpuctx->cgrp); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 692 | /* |
| 693 | * set cgrp before ctxsw in to allow |
| 694 | * event_filter_match() to not have to pass |
| 695 | * task around |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 696 | * we pass the cpuctx->ctx to perf_cgroup_from_task() |
| 697 | * because cgorup events are only per-cpu |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 698 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 699 | cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 700 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
| 701 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 702 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 703 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 704 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 707 | local_irq_restore(flags); |
| 708 | } |
| 709 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 710 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 711 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 712 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 713 | struct perf_cgroup *cgrp1; |
| 714 | struct perf_cgroup *cgrp2 = NULL; |
| 715 | |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 716 | rcu_read_lock(); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 717 | /* |
| 718 | * we come here when we know perf_cgroup_events > 0 |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 719 | * we do not need to pass the ctx here because we know |
| 720 | * we are holding the rcu lock |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 721 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 722 | cgrp1 = perf_cgroup_from_task(task, NULL); |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 723 | cgrp2 = perf_cgroup_from_task(next, NULL); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 724 | |
| 725 | /* |
| 726 | * only schedule out current cgroup events if we know |
| 727 | * that we are switching to a different cgroup. Otherwise, |
| 728 | * do no touch the cgroup events. |
| 729 | */ |
| 730 | if (cgrp1 != cgrp2) |
| 731 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 732 | |
| 733 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 736 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 737 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 738 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 739 | struct perf_cgroup *cgrp1; |
| 740 | struct perf_cgroup *cgrp2 = NULL; |
| 741 | |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 742 | rcu_read_lock(); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 743 | /* |
| 744 | * we come here when we know perf_cgroup_events > 0 |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 745 | * we do not need to pass the ctx here because we know |
| 746 | * we are holding the rcu lock |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 747 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 748 | cgrp1 = perf_cgroup_from_task(task, NULL); |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 749 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 750 | |
| 751 | /* |
| 752 | * only need to schedule in cgroup events if we are changing |
| 753 | * cgroup during ctxsw. Cgroup events were not scheduled |
| 754 | * out of ctxsw out if that was not the case. |
| 755 | */ |
| 756 | if (cgrp1 != cgrp2) |
| 757 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 758 | |
| 759 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
| 763 | struct perf_event_attr *attr, |
| 764 | struct perf_event *group_leader) |
| 765 | { |
| 766 | struct perf_cgroup *cgrp; |
| 767 | struct cgroup_subsys_state *css; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 768 | struct fd f = fdget(fd); |
| 769 | int ret = 0; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 770 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 771 | if (!f.file) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 772 | return -EBADF; |
| 773 | |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 774 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 775 | &perf_event_cgrp_subsys); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 776 | if (IS_ERR(css)) { |
| 777 | ret = PTR_ERR(css); |
| 778 | goto out; |
| 779 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 780 | |
| 781 | cgrp = container_of(css, struct perf_cgroup, css); |
| 782 | event->cgrp = cgrp; |
| 783 | |
| 784 | /* |
| 785 | * all events in a group must monitor |
| 786 | * the same cgroup because a task belongs |
| 787 | * to only one perf cgroup at a time |
| 788 | */ |
| 789 | if (group_leader && group_leader->cgrp != cgrp) { |
| 790 | perf_detach_cgroup(event); |
| 791 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 792 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 793 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 794 | fdput(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | static inline void |
| 799 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 800 | { |
| 801 | struct perf_cgroup_info *t; |
| 802 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 803 | event->shadow_ctx_time = now - t->timestamp; |
| 804 | } |
| 805 | |
| 806 | static inline void |
| 807 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 808 | { |
| 809 | /* |
| 810 | * when the current task's perf cgroup does not match |
| 811 | * the event's, we need to remember to call the |
| 812 | * perf_mark_enable() function the first time a task with |
| 813 | * a matching perf cgroup is scheduled in. |
| 814 | */ |
| 815 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 816 | event->cgrp_defer_enabled = 1; |
| 817 | } |
| 818 | |
| 819 | static inline void |
| 820 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 821 | struct perf_event_context *ctx) |
| 822 | { |
| 823 | struct perf_event *sub; |
| 824 | u64 tstamp = perf_event_time(event); |
| 825 | |
| 826 | if (!event->cgrp_defer_enabled) |
| 827 | return; |
| 828 | |
| 829 | event->cgrp_defer_enabled = 0; |
| 830 | |
| 831 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 832 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 833 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 834 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 835 | sub->cgrp_defer_enabled = 0; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | #else /* !CONFIG_CGROUP_PERF */ |
| 840 | |
| 841 | static inline bool |
| 842 | perf_cgroup_match(struct perf_event *event) |
| 843 | { |
| 844 | return true; |
| 845 | } |
| 846 | |
| 847 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 848 | {} |
| 849 | |
| 850 | static inline int is_cgroup_event(struct perf_event *event) |
| 851 | { |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 856 | { |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 861 | { |
| 862 | } |
| 863 | |
| 864 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 865 | { |
| 866 | } |
| 867 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 868 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 869 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 870 | { |
| 871 | } |
| 872 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 873 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 874 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 875 | { |
| 876 | } |
| 877 | |
| 878 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 879 | struct perf_event_attr *attr, |
| 880 | struct perf_event *group_leader) |
| 881 | { |
| 882 | return -EINVAL; |
| 883 | } |
| 884 | |
| 885 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 886 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 887 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 888 | { |
| 889 | } |
| 890 | |
| 891 | void |
| 892 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 893 | { |
| 894 | } |
| 895 | |
| 896 | static inline void |
| 897 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 898 | { |
| 899 | } |
| 900 | |
| 901 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 902 | { |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static inline void |
| 907 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 908 | { |
| 909 | } |
| 910 | |
| 911 | static inline void |
| 912 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 913 | struct perf_event_context *ctx) |
| 914 | { |
| 915 | } |
| 916 | #endif |
| 917 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 918 | /* |
| 919 | * set default to be dependent on timer tick just |
| 920 | * like original code |
| 921 | */ |
| 922 | #define PERF_CPU_HRTIMER (1000 / HZ) |
| 923 | /* |
| 924 | * function must be called with interrupts disbled |
| 925 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 926 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 927 | { |
| 928 | struct perf_cpu_context *cpuctx; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 929 | int rotations = 0; |
| 930 | |
| 931 | WARN_ON(!irqs_disabled()); |
| 932 | |
| 933 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 934 | rotations = perf_rotate_context(cpuctx); |
| 935 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 936 | raw_spin_lock(&cpuctx->hrtimer_lock); |
| 937 | if (rotations) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 938 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 939 | else |
| 940 | cpuctx->hrtimer_active = 0; |
| 941 | raw_spin_unlock(&cpuctx->hrtimer_lock); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 942 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 943 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 946 | 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] | 947 | { |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 948 | struct hrtimer *timer = &cpuctx->hrtimer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 949 | struct pmu *pmu = cpuctx->ctx.pmu; |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 950 | u64 interval; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 951 | |
| 952 | /* no multiplexing needed for SW PMU */ |
| 953 | if (pmu->task_ctx_nr == perf_sw_context) |
| 954 | return; |
| 955 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 956 | /* |
| 957 | * check default is sane, if not set then force to |
| 958 | * default interval (1/tick) |
| 959 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 960 | interval = pmu->hrtimer_interval_ms; |
| 961 | if (interval < 1) |
| 962 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 963 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 964 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 965 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 966 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
| 967 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 968 | timer->function = perf_mux_hrtimer_handler; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 971 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 972 | { |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 973 | struct hrtimer *timer = &cpuctx->hrtimer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 974 | struct pmu *pmu = cpuctx->ctx.pmu; |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 975 | unsigned long flags; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 976 | |
| 977 | /* not for SW PMU */ |
| 978 | if (pmu->task_ctx_nr == perf_sw_context) |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 979 | return 0; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 980 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 981 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
| 982 | if (!cpuctx->hrtimer_active) { |
| 983 | cpuctx->hrtimer_active = 1; |
| 984 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); |
| 985 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); |
| 986 | } |
| 987 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 988 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 989 | return 0; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 990 | } |
| 991 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 992 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 993 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 994 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 995 | if (!(*count)++) |
| 996 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 997 | } |
| 998 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 999 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1000 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1001 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 1002 | if (!--(*count)) |
| 1003 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1006 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1007 | |
| 1008 | /* |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1009 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
| 1010 | * perf_event_task_tick() are fully serialized because they're strictly cpu |
| 1011 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs |
| 1012 | * disabled, while perf_event_task_tick is called from IRQ context. |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1013 | */ |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1014 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1015 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1016 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1017 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1018 | WARN_ON(!irqs_disabled()); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1019 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1020 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
| 1021 | |
| 1022 | list_add(&ctx->active_ctx_list, head); |
| 1023 | } |
| 1024 | |
| 1025 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) |
| 1026 | { |
| 1027 | WARN_ON(!irqs_disabled()); |
| 1028 | |
| 1029 | WARN_ON(list_empty(&ctx->active_ctx_list)); |
| 1030 | |
| 1031 | list_del_init(&ctx->active_ctx_list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | static void get_ctx(struct perf_event_context *ctx) |
| 1035 | { |
| 1036 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 1037 | } |
| 1038 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 1039 | static void free_ctx(struct rcu_head *head) |
| 1040 | { |
| 1041 | struct perf_event_context *ctx; |
| 1042 | |
| 1043 | ctx = container_of(head, struct perf_event_context, rcu_head); |
| 1044 | kfree(ctx->task_ctx_data); |
| 1045 | kfree(ctx); |
| 1046 | } |
| 1047 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1048 | static void put_ctx(struct perf_event_context *ctx) |
| 1049 | { |
| 1050 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 1051 | if (ctx->parent_ctx) |
| 1052 | put_ctx(ctx->parent_ctx); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1053 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1054 | put_task_struct(ctx->task); |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 1055 | call_rcu(&ctx->rcu_head, free_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1059 | /* |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1060 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and |
| 1061 | * perf_pmu_migrate_context() we need some magic. |
| 1062 | * |
| 1063 | * Those places that change perf_event::ctx will hold both |
| 1064 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. |
| 1065 | * |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 1066 | * Lock ordering is by mutex address. There are two other sites where |
| 1067 | * perf_event_context::mutex nests and those are: |
| 1068 | * |
| 1069 | * - perf_event_exit_task_context() [ child , 0 ] |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 1070 | * perf_event_exit_event() |
| 1071 | * put_event() [ parent, 1 ] |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 1072 | * |
| 1073 | * - perf_event_init_context() [ parent, 0 ] |
| 1074 | * inherit_task_group() |
| 1075 | * inherit_group() |
| 1076 | * inherit_event() |
| 1077 | * perf_event_alloc() |
| 1078 | * perf_init_event() |
| 1079 | * perf_try_init_event() [ child , 1 ] |
| 1080 | * |
| 1081 | * While it appears there is an obvious deadlock here -- the parent and child |
| 1082 | * nesting levels are inverted between the two. This is in fact safe because |
| 1083 | * life-time rules separate them. That is an exiting task cannot fork, and a |
| 1084 | * spawning task cannot (yet) exit. |
| 1085 | * |
| 1086 | * But remember that that these are parent<->child context relations, and |
| 1087 | * migration does not affect children, therefore these two orderings should not |
| 1088 | * interact. |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1089 | * |
| 1090 | * The change in perf_event::ctx does not affect children (as claimed above) |
| 1091 | * because the sys_perf_event_open() case will install a new event and break |
| 1092 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only |
| 1093 | * concerned with cpuctx and that doesn't have children. |
| 1094 | * |
| 1095 | * The places that change perf_event::ctx will issue: |
| 1096 | * |
| 1097 | * perf_remove_from_context(); |
| 1098 | * synchronize_rcu(); |
| 1099 | * perf_install_in_context(); |
| 1100 | * |
| 1101 | * to affect the change. The remove_from_context() + synchronize_rcu() should |
| 1102 | * quiesce the event, after which we can install it in the new location. This |
| 1103 | * means that only external vectors (perf_fops, prctl) can perturb the event |
| 1104 | * while in transit. Therefore all such accessors should also acquire |
| 1105 | * perf_event_context::mutex to serialize against this. |
| 1106 | * |
| 1107 | * However; because event->ctx can change while we're waiting to acquire |
| 1108 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() |
| 1109 | * function. |
| 1110 | * |
| 1111 | * Lock order: |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 1112 | * cred_guard_mutex |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1113 | * task_struct::perf_event_mutex |
| 1114 | * perf_event_context::mutex |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1115 | * perf_event::child_mutex; |
Peter Zijlstra | 07c4a77 | 2016-01-26 12:15:37 +0100 | [diff] [blame] | 1116 | * perf_event_context::lock |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1117 | * perf_event::mmap_mutex |
| 1118 | * mmap_sem |
| 1119 | */ |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1120 | static struct perf_event_context * |
| 1121 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1122 | { |
| 1123 | struct perf_event_context *ctx; |
| 1124 | |
| 1125 | again: |
| 1126 | rcu_read_lock(); |
| 1127 | ctx = ACCESS_ONCE(event->ctx); |
| 1128 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
| 1129 | rcu_read_unlock(); |
| 1130 | goto again; |
| 1131 | } |
| 1132 | rcu_read_unlock(); |
| 1133 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1134 | mutex_lock_nested(&ctx->mutex, nesting); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1135 | if (event->ctx != ctx) { |
| 1136 | mutex_unlock(&ctx->mutex); |
| 1137 | put_ctx(ctx); |
| 1138 | goto again; |
| 1139 | } |
| 1140 | |
| 1141 | return ctx; |
| 1142 | } |
| 1143 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1144 | static inline struct perf_event_context * |
| 1145 | perf_event_ctx_lock(struct perf_event *event) |
| 1146 | { |
| 1147 | return perf_event_ctx_lock_nested(event, 0); |
| 1148 | } |
| 1149 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1150 | static void perf_event_ctx_unlock(struct perf_event *event, |
| 1151 | struct perf_event_context *ctx) |
| 1152 | { |
| 1153 | mutex_unlock(&ctx->mutex); |
| 1154 | put_ctx(ctx); |
| 1155 | } |
| 1156 | |
| 1157 | /* |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1158 | * This must be done under the ctx->lock, such as to serialize against |
| 1159 | * context_equiv(), therefore we cannot call put_ctx() since that might end up |
| 1160 | * calling scheduler related locks and ctx->lock nests inside those. |
| 1161 | */ |
| 1162 | static __must_check struct perf_event_context * |
| 1163 | unclone_ctx(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1164 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1165 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
| 1166 | |
| 1167 | lockdep_assert_held(&ctx->lock); |
| 1168 | |
| 1169 | if (parent_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1170 | ctx->parent_ctx = NULL; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1171 | ctx->generation++; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1172 | |
| 1173 | return parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1176 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 1177 | { |
| 1178 | /* |
| 1179 | * only top level events have the pid namespace they were created in |
| 1180 | */ |
| 1181 | if (event->parent) |
| 1182 | event = event->parent; |
| 1183 | |
| 1184 | return task_tgid_nr_ns(p, event->ns); |
| 1185 | } |
| 1186 | |
| 1187 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 1188 | { |
| 1189 | /* |
| 1190 | * only top level events have the pid namespace they were created in |
| 1191 | */ |
| 1192 | if (event->parent) |
| 1193 | event = event->parent; |
| 1194 | |
| 1195 | return task_pid_nr_ns(p, event->ns); |
| 1196 | } |
| 1197 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1198 | /* |
| 1199 | * If we inherit events we want to return the parent event id |
| 1200 | * to userspace. |
| 1201 | */ |
| 1202 | static u64 primary_event_id(struct perf_event *event) |
| 1203 | { |
| 1204 | u64 id = event->id; |
| 1205 | |
| 1206 | if (event->parent) |
| 1207 | id = event->parent->id; |
| 1208 | |
| 1209 | return id; |
| 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Get the perf_event_context for a task and lock it. |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1214 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1215 | * This has to cope with with the fact that until it is locked, |
| 1216 | * the context could get moved to another task. |
| 1217 | */ |
| 1218 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1219 | 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] | 1220 | { |
| 1221 | struct perf_event_context *ctx; |
| 1222 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1223 | retry: |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1224 | /* |
| 1225 | * One of the few rules of preemptible RCU is that one cannot do |
| 1226 | * rcu_read_unlock() while holding a scheduler (or nested) lock when |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1227 | * part of the read side critical section was irqs-enabled -- see |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1228 | * rcu_read_unlock_special(). |
| 1229 | * |
| 1230 | * 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] | 1231 | * side critical section has interrupts disabled. |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1232 | */ |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1233 | local_irq_save(*flags); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1234 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1235 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1236 | if (ctx) { |
| 1237 | /* |
| 1238 | * If this context is a clone of another, it might |
| 1239 | * get swapped for another underneath us by |
| 1240 | * perf_event_task_sched_out, though the |
| 1241 | * rcu_read_lock() protects us from any context |
| 1242 | * getting freed. Lock the context and check if it |
| 1243 | * got swapped before we could get the lock, and retry |
| 1244 | * if so. If we locked the right context, then it |
| 1245 | * can't get swapped on us any more. |
| 1246 | */ |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1247 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1248 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1249 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1250 | rcu_read_unlock(); |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1251 | local_irq_restore(*flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1252 | goto retry; |
| 1253 | } |
| 1254 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1255 | if (ctx->task == TASK_TOMBSTONE || |
| 1256 | !atomic_inc_not_zero(&ctx->refcount)) { |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1257 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1258 | ctx = NULL; |
Peter Zijlstra | 828b6f0 | 2016-01-27 21:59:04 +0100 | [diff] [blame] | 1259 | } else { |
| 1260 | WARN_ON_ONCE(ctx->task != task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | rcu_read_unlock(); |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1264 | if (!ctx) |
| 1265 | local_irq_restore(*flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1266 | return ctx; |
| 1267 | } |
| 1268 | |
| 1269 | /* |
| 1270 | * Get the context for a task and increment its pin_count so it |
| 1271 | * can't get swapped to another task. This also increments its |
| 1272 | * reference count so that the context can't get freed. |
| 1273 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1274 | static struct perf_event_context * |
| 1275 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1276 | { |
| 1277 | struct perf_event_context *ctx; |
| 1278 | unsigned long flags; |
| 1279 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1280 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1281 | if (ctx) { |
| 1282 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1283 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1284 | } |
| 1285 | return ctx; |
| 1286 | } |
| 1287 | |
| 1288 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 1289 | { |
| 1290 | unsigned long flags; |
| 1291 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1292 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1293 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1294 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1295 | } |
| 1296 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1297 | /* |
| 1298 | * Update the record of the current time in a context. |
| 1299 | */ |
| 1300 | static void update_context_time(struct perf_event_context *ctx) |
| 1301 | { |
| 1302 | u64 now = perf_clock(); |
| 1303 | |
| 1304 | ctx->time += now - ctx->timestamp; |
| 1305 | ctx->timestamp = now; |
| 1306 | } |
| 1307 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1308 | static u64 perf_event_time(struct perf_event *event) |
| 1309 | { |
| 1310 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1311 | |
| 1312 | if (is_cgroup_event(event)) |
| 1313 | return perf_cgroup_event_time(event); |
| 1314 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1315 | return ctx ? ctx->time : 0; |
| 1316 | } |
| 1317 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1318 | /* |
| 1319 | * Update the total_time_enabled and total_time_running fields for a event. |
| 1320 | */ |
| 1321 | static void update_event_times(struct perf_event *event) |
| 1322 | { |
| 1323 | struct perf_event_context *ctx = event->ctx; |
| 1324 | u64 run_end; |
| 1325 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 1326 | lockdep_assert_held(&ctx->lock); |
| 1327 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1328 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1329 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1330 | return; |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 1331 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1332 | /* |
| 1333 | * in cgroup mode, time_enabled represents |
| 1334 | * the time the event was enabled AND active |
| 1335 | * tasks were in the monitored cgroup. This is |
| 1336 | * independent of the activity of the context as |
| 1337 | * there may be a mix of cgroup and non-cgroup events. |
| 1338 | * |
| 1339 | * That is why we treat cgroup events differently |
| 1340 | * here. |
| 1341 | */ |
| 1342 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1343 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1344 | else if (ctx->is_active) |
| 1345 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1346 | else |
| 1347 | run_end = event->tstamp_stopped; |
| 1348 | |
| 1349 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1350 | |
| 1351 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1352 | run_end = event->tstamp_stopped; |
| 1353 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1354 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1355 | |
| 1356 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1357 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1358 | } |
| 1359 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1360 | /* |
| 1361 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1362 | */ |
| 1363 | static void update_group_times(struct perf_event *leader) |
| 1364 | { |
| 1365 | struct perf_event *event; |
| 1366 | |
| 1367 | update_event_times(leader); |
| 1368 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1369 | update_event_times(event); |
| 1370 | } |
| 1371 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1372 | static struct list_head * |
| 1373 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1374 | { |
| 1375 | if (event->attr.pinned) |
| 1376 | return &ctx->pinned_groups; |
| 1377 | else |
| 1378 | return &ctx->flexible_groups; |
| 1379 | } |
| 1380 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1381 | /* |
| 1382 | * Add a event from the lists for its context. |
| 1383 | * Must be called with ctx->mutex and ctx->lock held. |
| 1384 | */ |
| 1385 | static void |
| 1386 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1387 | { |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 1388 | lockdep_assert_held(&ctx->lock); |
| 1389 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1390 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1391 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1392 | |
| 1393 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1394 | * If we're a stand alone event or group leader, we go to the context |
| 1395 | * list, group events are kept attached to the group so that |
| 1396 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1397 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1398 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1399 | struct list_head *list; |
| 1400 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1401 | if (is_software_event(event)) |
| 1402 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 1403 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1404 | list = ctx_group_list(event, ctx); |
| 1405 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1406 | } |
| 1407 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 1408 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1409 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1410 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1411 | list_add_rcu(&event->event_entry, &ctx->event_list); |
| 1412 | ctx->nr_events++; |
| 1413 | if (event->attr.inherit_stat) |
| 1414 | ctx->nr_stat++; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1415 | |
| 1416 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1419 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1420 | * Initialize event state based on the perf_event_attr::disabled. |
| 1421 | */ |
| 1422 | static inline void perf_event__state_init(struct perf_event *event) |
| 1423 | { |
| 1424 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1425 | PERF_EVENT_STATE_INACTIVE; |
| 1426 | } |
| 1427 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1428 | 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] | 1429 | { |
| 1430 | int entry = sizeof(u64); /* value */ |
| 1431 | int size = 0; |
| 1432 | int nr = 1; |
| 1433 | |
| 1434 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1435 | size += sizeof(u64); |
| 1436 | |
| 1437 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1438 | size += sizeof(u64); |
| 1439 | |
| 1440 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1441 | entry += sizeof(u64); |
| 1442 | |
| 1443 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1444 | nr += nr_siblings; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1445 | size += sizeof(u64); |
| 1446 | } |
| 1447 | |
| 1448 | size += entry * nr; |
| 1449 | event->read_size = size; |
| 1450 | } |
| 1451 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1452 | 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] | 1453 | { |
| 1454 | struct perf_sample_data *data; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1455 | u16 size = 0; |
| 1456 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1457 | if (sample_type & PERF_SAMPLE_IP) |
| 1458 | size += sizeof(data->ip); |
| 1459 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1460 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1461 | size += sizeof(data->addr); |
| 1462 | |
| 1463 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1464 | size += sizeof(data->period); |
| 1465 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1466 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1467 | size += sizeof(data->weight); |
| 1468 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1469 | if (sample_type & PERF_SAMPLE_READ) |
| 1470 | size += event->read_size; |
| 1471 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1472 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1473 | size += sizeof(data->data_src.val); |
| 1474 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 1475 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1476 | size += sizeof(data->txn); |
| 1477 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1478 | event->header_size = size; |
| 1479 | } |
| 1480 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1481 | /* |
| 1482 | * Called at perf_event creation and when events are attached/detached from a |
| 1483 | * group. |
| 1484 | */ |
| 1485 | static void perf_event__header_size(struct perf_event *event) |
| 1486 | { |
| 1487 | __perf_event_read_size(event, |
| 1488 | event->group_leader->nr_siblings); |
| 1489 | __perf_event_header_size(event, event->attr.sample_type); |
| 1490 | } |
| 1491 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1492 | static void perf_event__id_header_size(struct perf_event *event) |
| 1493 | { |
| 1494 | struct perf_sample_data *data; |
| 1495 | u64 sample_type = event->attr.sample_type; |
| 1496 | u16 size = 0; |
| 1497 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1498 | if (sample_type & PERF_SAMPLE_TID) |
| 1499 | size += sizeof(data->tid_entry); |
| 1500 | |
| 1501 | if (sample_type & PERF_SAMPLE_TIME) |
| 1502 | size += sizeof(data->time); |
| 1503 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 1504 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1505 | size += sizeof(data->id); |
| 1506 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1507 | if (sample_type & PERF_SAMPLE_ID) |
| 1508 | size += sizeof(data->id); |
| 1509 | |
| 1510 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1511 | size += sizeof(data->stream_id); |
| 1512 | |
| 1513 | if (sample_type & PERF_SAMPLE_CPU) |
| 1514 | size += sizeof(data->cpu_entry); |
| 1515 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1516 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1517 | } |
| 1518 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1519 | static bool perf_event_validate_size(struct perf_event *event) |
| 1520 | { |
| 1521 | /* |
| 1522 | * The values computed here will be over-written when we actually |
| 1523 | * attach the event. |
| 1524 | */ |
| 1525 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); |
| 1526 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); |
| 1527 | perf_event__id_header_size(event); |
| 1528 | |
| 1529 | /* |
| 1530 | * Sum the lot; should not exceed the 64k limit we have on records. |
| 1531 | * Conservative limit to allow for callchains and other variable fields. |
| 1532 | */ |
| 1533 | if (event->read_size + event->header_size + |
| 1534 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) |
| 1535 | return false; |
| 1536 | |
| 1537 | return true; |
| 1538 | } |
| 1539 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1540 | static void perf_group_attach(struct perf_event *event) |
| 1541 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1542 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1543 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1544 | /* |
| 1545 | * We can have double attach due to group movement in perf_event_open. |
| 1546 | */ |
| 1547 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1548 | return; |
| 1549 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1550 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1551 | |
| 1552 | if (group_leader == event) |
| 1553 | return; |
| 1554 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1555 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
| 1556 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1557 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 1558 | !is_software_event(event)) |
| 1559 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 1560 | |
| 1561 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1562 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1563 | |
| 1564 | perf_event__header_size(group_leader); |
| 1565 | |
| 1566 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1567 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1568 | } |
| 1569 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1570 | /* |
| 1571 | * Remove a event from the lists for its context. |
| 1572 | * Must be called with ctx->mutex and ctx->lock held. |
| 1573 | */ |
| 1574 | static void |
| 1575 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1576 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1577 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1578 | |
| 1579 | WARN_ON_ONCE(event->ctx != ctx); |
| 1580 | lockdep_assert_held(&ctx->lock); |
| 1581 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1582 | /* |
| 1583 | * We can have double detach due to exit/hot-unplug + close. |
| 1584 | */ |
| 1585 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1586 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1587 | |
| 1588 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1589 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1590 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1591 | ctx->nr_cgroups--; |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 1592 | /* |
| 1593 | * Because cgroup events are always per-cpu events, this will |
| 1594 | * always be called from the right CPU. |
| 1595 | */ |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1596 | cpuctx = __get_cpu_context(ctx); |
| 1597 | /* |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 1598 | * If there are no more cgroup events then clear cgrp to avoid |
| 1599 | * stale pointer in update_cgrp_time_from_cpuctx(). |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1600 | */ |
| 1601 | if (!ctx->nr_cgroups) |
| 1602 | cpuctx->cgrp = NULL; |
| 1603 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1604 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1605 | ctx->nr_events--; |
| 1606 | if (event->attr.inherit_stat) |
| 1607 | ctx->nr_stat--; |
| 1608 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1609 | list_del_rcu(&event->event_entry); |
| 1610 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1611 | if (event->group_leader == event) |
| 1612 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1613 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1614 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1615 | |
| 1616 | /* |
| 1617 | * If event was in error state, then keep it |
| 1618 | * that way, otherwise bogus counts will be |
| 1619 | * returned on read(). The only way to get out |
| 1620 | * of error state is by explicit re-enabling |
| 1621 | * of the event |
| 1622 | */ |
| 1623 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1624 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1625 | |
| 1626 | ctx->generation++; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1629 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1630 | { |
| 1631 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1632 | struct list_head *list = NULL; |
| 1633 | |
| 1634 | /* |
| 1635 | * We can have double detach due to exit/hot-unplug + close. |
| 1636 | */ |
| 1637 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1638 | return; |
| 1639 | |
| 1640 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1641 | |
| 1642 | /* |
| 1643 | * If this is a sibling, remove it from its group. |
| 1644 | */ |
| 1645 | if (event->group_leader != event) { |
| 1646 | list_del_init(&event->group_entry); |
| 1647 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1648 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | if (!list_empty(&event->group_entry)) |
| 1652 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1653 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1654 | /* |
| 1655 | * If this was a group event with sibling events then |
| 1656 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1657 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1658 | */ |
| 1659 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1660 | if (list) |
| 1661 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1662 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1663 | |
| 1664 | /* Inherit group flags from the previous leader */ |
| 1665 | sibling->group_flags = event->group_flags; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1666 | |
| 1667 | WARN_ON_ONCE(sibling->ctx != event->ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1668 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1669 | |
| 1670 | out: |
| 1671 | perf_event__header_size(event->group_leader); |
| 1672 | |
| 1673 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1674 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1675 | } |
| 1676 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1677 | static bool is_orphaned_event(struct perf_event *event) |
| 1678 | { |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 1679 | return event->state == PERF_EVENT_STATE_DEAD; |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1680 | } |
| 1681 | |
Mark Rutland | 66eb579 | 2015-05-13 17:12:23 +0100 | [diff] [blame] | 1682 | static inline int pmu_filter_match(struct perf_event *event) |
| 1683 | { |
| 1684 | struct pmu *pmu = event->pmu; |
| 1685 | return pmu->filter_match ? pmu->filter_match(event) : 1; |
| 1686 | } |
| 1687 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1688 | static inline int |
| 1689 | event_filter_match(struct perf_event *event) |
| 1690 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1691 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
Mark Rutland | 66eb579 | 2015-05-13 17:12:23 +0100 | [diff] [blame] | 1692 | && perf_cgroup_match(event) && pmu_filter_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1693 | } |
| 1694 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1695 | static void |
| 1696 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1697 | struct perf_cpu_context *cpuctx, |
| 1698 | struct perf_event_context *ctx) |
| 1699 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1700 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1701 | u64 delta; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1702 | |
| 1703 | WARN_ON_ONCE(event->ctx != ctx); |
| 1704 | lockdep_assert_held(&ctx->lock); |
| 1705 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1706 | /* |
| 1707 | * An event which could not be activated because of |
| 1708 | * filter mismatch still needs to have its timings |
| 1709 | * maintained, otherwise bogus information is return |
| 1710 | * via read() for time_enabled, time_running: |
| 1711 | */ |
| 1712 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1713 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1714 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1715 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1716 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1717 | } |
| 1718 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1719 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1720 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1721 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1722 | perf_pmu_disable(event->pmu); |
| 1723 | |
Peter Zijlstra | 28a967c | 2016-02-24 18:45:46 +0100 | [diff] [blame] | 1724 | event->tstamp_stopped = tstamp; |
| 1725 | event->pmu->del(event, 0); |
| 1726 | event->oncpu = -1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1727 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1728 | if (event->pending_disable) { |
| 1729 | event->pending_disable = 0; |
| 1730 | event->state = PERF_EVENT_STATE_OFF; |
| 1731 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1732 | |
| 1733 | if (!is_software_event(event)) |
| 1734 | cpuctx->active_oncpu--; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1735 | if (!--ctx->nr_active) |
| 1736 | perf_event_ctx_deactivate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1737 | if (event->attr.freq && event->attr.sample_freq) |
| 1738 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1739 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1740 | cpuctx->exclusive = 0; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1741 | |
| 1742 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | static void |
| 1746 | group_sched_out(struct perf_event *group_event, |
| 1747 | struct perf_cpu_context *cpuctx, |
| 1748 | struct perf_event_context *ctx) |
| 1749 | { |
| 1750 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1751 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1752 | |
| 1753 | event_sched_out(group_event, cpuctx, ctx); |
| 1754 | |
| 1755 | /* |
| 1756 | * Schedule out siblings (if any): |
| 1757 | */ |
| 1758 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1759 | event_sched_out(event, cpuctx, ctx); |
| 1760 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1761 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1762 | cpuctx->exclusive = 0; |
| 1763 | } |
| 1764 | |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1765 | #define DETACH_GROUP 0x01UL |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 1766 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1767 | /* |
| 1768 | * Cross CPU call to remove a performance event |
| 1769 | * |
| 1770 | * We disable the event on the hardware level first. After that we |
| 1771 | * remove it from the context list. |
| 1772 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1773 | static void |
| 1774 | __perf_remove_from_context(struct perf_event *event, |
| 1775 | struct perf_cpu_context *cpuctx, |
| 1776 | struct perf_event_context *ctx, |
| 1777 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1778 | { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1779 | unsigned long flags = (unsigned long)info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1780 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1781 | event_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1782 | if (flags & DETACH_GROUP) |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1783 | perf_group_detach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1784 | list_del_event(event, ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1785 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 1786 | if (!ctx->nr_events && ctx->is_active) { |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1787 | ctx->is_active = 0; |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 1788 | if (ctx->task) { |
| 1789 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 1790 | cpuctx->task_ctx = NULL; |
| 1791 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1792 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1793 | } |
| 1794 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1795 | /* |
| 1796 | * Remove the event from a task's (or a CPU's) list of events. |
| 1797 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1798 | * If event->ctx is a cloned context, callers must make sure that |
| 1799 | * every task struct that event->ctx->task could possibly point to |
| 1800 | * remains valid. This is OK when called from perf_release since |
| 1801 | * that only calls us on the top-level context, which can't be a clone. |
| 1802 | * When called from perf_event_exit_task, it's OK because the |
| 1803 | * context has been detached from its task. |
| 1804 | */ |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1805 | 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] | 1806 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1807 | lockdep_assert_held(&event->ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1808 | |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1809 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1810 | } |
| 1811 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1812 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1813 | * Cross CPU call to disable a performance event |
| 1814 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1815 | static void __perf_event_disable(struct perf_event *event, |
| 1816 | struct perf_cpu_context *cpuctx, |
| 1817 | struct perf_event_context *ctx, |
| 1818 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1819 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1820 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 1821 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1822 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1823 | update_context_time(ctx); |
| 1824 | update_cgrp_time_from_event(event); |
| 1825 | update_group_times(event); |
| 1826 | if (event == event->group_leader) |
| 1827 | group_sched_out(event, cpuctx, ctx); |
| 1828 | else |
| 1829 | event_sched_out(event, cpuctx, ctx); |
| 1830 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1831 | } |
| 1832 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1833 | /* |
| 1834 | * Disable a event. |
| 1835 | * |
| 1836 | * If event->ctx is a cloned context, callers must make sure that |
| 1837 | * every task struct that event->ctx->task could possibly point to |
| 1838 | * remains valid. This condition is satisifed when called through |
| 1839 | * perf_event_for_each_child or perf_event_for_each because they |
| 1840 | * hold the top-level event's child_mutex, so any descendant that |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 1841 | * goes to exit will block in perf_event_exit_event(). |
| 1842 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1843 | * When called from perf_pending_event it's OK because event->ctx |
| 1844 | * is the current context on this CPU and preemption is disabled, |
| 1845 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1846 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1847 | static void _perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1848 | { |
| 1849 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1850 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1851 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1852 | if (event->state <= PERF_EVENT_STATE_OFF) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1853 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1854 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1855 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1856 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1857 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1858 | event_function_call(event, __perf_event_disable, NULL); |
| 1859 | } |
| 1860 | |
| 1861 | void perf_event_disable_local(struct perf_event *event) |
| 1862 | { |
| 1863 | event_function_local(event, __perf_event_disable, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1864 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1865 | |
| 1866 | /* |
| 1867 | * Strictly speaking kernel users cannot create groups and therefore this |
| 1868 | * interface does not need the perf_event_ctx_lock() magic. |
| 1869 | */ |
| 1870 | void perf_event_disable(struct perf_event *event) |
| 1871 | { |
| 1872 | struct perf_event_context *ctx; |
| 1873 | |
| 1874 | ctx = perf_event_ctx_lock(event); |
| 1875 | _perf_event_disable(event); |
| 1876 | perf_event_ctx_unlock(event, ctx); |
| 1877 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 1878 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1879 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1880 | static void perf_set_shadow_time(struct perf_event *event, |
| 1881 | struct perf_event_context *ctx, |
| 1882 | u64 tstamp) |
| 1883 | { |
| 1884 | /* |
| 1885 | * use the correct time source for the time snapshot |
| 1886 | * |
| 1887 | * We could get by without this by leveraging the |
| 1888 | * fact that to get to this function, the caller |
| 1889 | * has most likely already called update_context_time() |
| 1890 | * and update_cgrp_time_xx() and thus both timestamp |
| 1891 | * are identical (or very close). Given that tstamp is, |
| 1892 | * already adjusted for cgroup, we could say that: |
| 1893 | * tstamp - ctx->timestamp |
| 1894 | * is equivalent to |
| 1895 | * tstamp - cgrp->timestamp. |
| 1896 | * |
| 1897 | * Then, in perf_output_read(), the calculation would |
| 1898 | * work with no changes because: |
| 1899 | * - event is guaranteed scheduled in |
| 1900 | * - no scheduled out in between |
| 1901 | * - thus the timestamp would be the same |
| 1902 | * |
| 1903 | * But this is a bit hairy. |
| 1904 | * |
| 1905 | * So instead, we have an explicit cgroup call to remain |
| 1906 | * within the time time source all along. We believe it |
| 1907 | * is cleaner and simpler to understand. |
| 1908 | */ |
| 1909 | if (is_cgroup_event(event)) |
| 1910 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1911 | else |
| 1912 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1913 | } |
| 1914 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1915 | #define MAX_INTERRUPTS (~0ULL) |
| 1916 | |
| 1917 | static void perf_log_throttle(struct perf_event *event, int enable); |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 1918 | static void perf_log_itrace_start(struct perf_event *event); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1919 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1920 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1921 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1922 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1923 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1924 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1925 | u64 tstamp = perf_event_time(event); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1926 | int ret = 0; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1927 | |
Peter Zijlstra | 6334241 | 2014-05-05 11:49:16 +0200 | [diff] [blame] | 1928 | lockdep_assert_held(&ctx->lock); |
| 1929 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1930 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1931 | return 0; |
| 1932 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 1933 | WRITE_ONCE(event->oncpu, smp_processor_id()); |
| 1934 | /* |
| 1935 | * Order event::oncpu write to happen before the ACTIVE state |
| 1936 | * is visible. |
| 1937 | */ |
| 1938 | smp_wmb(); |
| 1939 | WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1940 | |
| 1941 | /* |
| 1942 | * Unthrottle events, since we scheduled we might have missed several |
| 1943 | * ticks already, also for a heavily scheduling task there is little |
| 1944 | * guarantee it'll get a tick in a timely manner. |
| 1945 | */ |
| 1946 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1947 | perf_log_throttle(event, 1); |
| 1948 | event->hw.interrupts = 0; |
| 1949 | } |
| 1950 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1951 | /* |
| 1952 | * The new state must be visible before we turn it on in the hardware: |
| 1953 | */ |
| 1954 | smp_wmb(); |
| 1955 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1956 | perf_pmu_disable(event->pmu); |
| 1957 | |
Shaohua Li | 72f669c | 2015-02-05 15:55:31 -0800 | [diff] [blame] | 1958 | perf_set_shadow_time(event, ctx, tstamp); |
| 1959 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 1960 | perf_log_itrace_start(event); |
| 1961 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1962 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1963 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1964 | event->oncpu = -1; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1965 | ret = -EAGAIN; |
| 1966 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1967 | } |
| 1968 | |
Peter Zijlstra | 00a2916 | 2015-07-27 10:35:07 +0200 | [diff] [blame] | 1969 | event->tstamp_running += tstamp - event->tstamp_stopped; |
| 1970 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1971 | if (!is_software_event(event)) |
| 1972 | cpuctx->active_oncpu++; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1973 | if (!ctx->nr_active++) |
| 1974 | perf_event_ctx_activate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1975 | if (event->attr.freq && event->attr.sample_freq) |
| 1976 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1977 | |
| 1978 | if (event->attr.exclusive) |
| 1979 | cpuctx->exclusive = 1; |
| 1980 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1981 | out: |
| 1982 | perf_pmu_enable(event->pmu); |
| 1983 | |
| 1984 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | static int |
| 1988 | group_sched_in(struct perf_event *group_event, |
| 1989 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1990 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1991 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1992 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 4a23459 | 2014-02-24 12:43:31 +0100 | [diff] [blame] | 1993 | struct pmu *pmu = ctx->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1994 | u64 now = ctx->time; |
| 1995 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1996 | |
| 1997 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1998 | return 0; |
| 1999 | |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 2000 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 2001 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2002 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 2003 | pmu->cancel_txn(pmu); |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 2004 | perf_mux_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2005 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 2006 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2007 | |
| 2008 | /* |
| 2009 | * Schedule in siblings as one group (if any): |
| 2010 | */ |
| 2011 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2012 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2013 | partial_group = event; |
| 2014 | goto group_error; |
| 2015 | } |
| 2016 | } |
| 2017 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2018 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 2019 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2020 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2021 | group_error: |
| 2022 | /* |
| 2023 | * Groups can be scheduled in as one unit only, so undo any |
| 2024 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2025 | * The events up to the failed event are scheduled out normally, |
| 2026 | * tstamp_stopped will be updated. |
| 2027 | * |
| 2028 | * The failed events and the remaining siblings need to have |
| 2029 | * their timings updated as if they had gone thru event_sched_in() |
| 2030 | * and event_sched_out(). This is required to get consistent timings |
| 2031 | * across the group. This also takes care of the case where the group |
| 2032 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 2033 | * the time the event was actually stopped, such that time delta |
| 2034 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2035 | */ |
| 2036 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 2037 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2038 | simulate = true; |
| 2039 | |
| 2040 | if (simulate) { |
| 2041 | event->tstamp_running += now - event->tstamp_stopped; |
| 2042 | event->tstamp_stopped = now; |
| 2043 | } else { |
| 2044 | event_sched_out(event, cpuctx, ctx); |
| 2045 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2046 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2047 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2048 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 2049 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 2050 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 2051 | perf_mux_hrtimer_restart(cpuctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2052 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2053 | return -EAGAIN; |
| 2054 | } |
| 2055 | |
| 2056 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2057 | * Work out whether we can put this event group on the CPU now. |
| 2058 | */ |
| 2059 | static int group_can_go_on(struct perf_event *event, |
| 2060 | struct perf_cpu_context *cpuctx, |
| 2061 | int can_add_hw) |
| 2062 | { |
| 2063 | /* |
| 2064 | * Groups consisting entirely of software events can always go on. |
| 2065 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 2066 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2067 | return 1; |
| 2068 | /* |
| 2069 | * If an exclusive group is already on, no other hardware |
| 2070 | * events can go on. |
| 2071 | */ |
| 2072 | if (cpuctx->exclusive) |
| 2073 | return 0; |
| 2074 | /* |
| 2075 | * If this group is exclusive and there are already |
| 2076 | * events on the CPU, it can't go on. |
| 2077 | */ |
| 2078 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 2079 | return 0; |
| 2080 | /* |
| 2081 | * Otherwise, try to add it if all previous groups were able |
| 2082 | * to go on. |
| 2083 | */ |
| 2084 | return can_add_hw; |
| 2085 | } |
| 2086 | |
| 2087 | static void add_event_to_ctx(struct perf_event *event, |
| 2088 | struct perf_event_context *ctx) |
| 2089 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2090 | u64 tstamp = perf_event_time(event); |
| 2091 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2092 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 2093 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2094 | event->tstamp_enabled = tstamp; |
| 2095 | event->tstamp_running = tstamp; |
| 2096 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2097 | } |
| 2098 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2099 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2100 | struct perf_cpu_context *cpuctx, |
| 2101 | enum event_type_t event_type); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2102 | static void |
| 2103 | ctx_sched_in(struct perf_event_context *ctx, |
| 2104 | struct perf_cpu_context *cpuctx, |
| 2105 | enum event_type_t event_type, |
| 2106 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2107 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2108 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2109 | struct perf_event_context *ctx) |
| 2110 | { |
| 2111 | if (!cpuctx->task_ctx) |
| 2112 | return; |
| 2113 | |
| 2114 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2115 | return; |
| 2116 | |
| 2117 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
| 2118 | } |
| 2119 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 2120 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 2121 | struct perf_event_context *ctx, |
| 2122 | struct task_struct *task) |
| 2123 | { |
| 2124 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 2125 | if (ctx) |
| 2126 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 2127 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 2128 | if (ctx) |
| 2129 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 2130 | } |
| 2131 | |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 2132 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
| 2133 | struct perf_event_context *task_ctx) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 2134 | { |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 2135 | perf_pmu_disable(cpuctx->ctx.pmu); |
| 2136 | if (task_ctx) |
| 2137 | task_ctx_sched_out(cpuctx, task_ctx); |
| 2138 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 2139 | perf_event_sched_in(cpuctx, task_ctx, current); |
| 2140 | perf_pmu_enable(cpuctx->ctx.pmu); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 2141 | } |
| 2142 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2143 | /* |
| 2144 | * Cross CPU call to install and enable a performance event |
| 2145 | * |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2146 | * Very similar to remote_function() + event_function() but cannot assume that |
| 2147 | * things like ctx->is_active and cpuctx->task_ctx are set. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2148 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2149 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2150 | { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2151 | struct perf_event *event = info; |
| 2152 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2153 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2154 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2155 | bool activate = true; |
| 2156 | int ret = 0; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2157 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2158 | raw_spin_lock(&cpuctx->ctx.lock); |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2159 | if (ctx->task) { |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2160 | raw_spin_lock(&ctx->lock); |
| 2161 | task_ctx = ctx; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2162 | |
| 2163 | /* If we're on the wrong CPU, try again */ |
| 2164 | if (task_cpu(ctx->task) != smp_processor_id()) { |
| 2165 | ret = -ESRCH; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2166 | goto unlock; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2167 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2168 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2169 | /* |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2170 | * If we're on the right CPU, see if the task we target is |
| 2171 | * current, if not we don't have to activate the ctx, a future |
| 2172 | * context switch will do that for us. |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2173 | */ |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2174 | if (ctx->task != current) |
| 2175 | activate = false; |
| 2176 | else |
| 2177 | WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
| 2178 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2179 | } else if (task_ctx) { |
| 2180 | raw_spin_lock(&task_ctx->lock); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2181 | } |
| 2182 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2183 | if (activate) { |
| 2184 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
| 2185 | add_event_to_ctx(event, ctx); |
| 2186 | ctx_resched(cpuctx, task_ctx); |
| 2187 | } else { |
| 2188 | add_event_to_ctx(event, ctx); |
| 2189 | } |
| 2190 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2191 | unlock: |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2192 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2193 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2194 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | /* |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2198 | * Attach a performance event to a context. |
| 2199 | * |
| 2200 | * Very similar to event_function_call, see comment there. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2201 | */ |
| 2202 | static void |
| 2203 | perf_install_in_context(struct perf_event_context *ctx, |
| 2204 | struct perf_event *event, |
| 2205 | int cpu) |
| 2206 | { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2207 | struct task_struct *task = READ_ONCE(ctx->task); |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2208 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2209 | lockdep_assert_held(&ctx->mutex); |
| 2210 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2211 | event->ctx = ctx; |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 2212 | if (event->cpu != -1) |
| 2213 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2214 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2215 | if (!task) { |
| 2216 | cpu_function_call(cpu, __perf_install_in_context, event); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2217 | return; |
| 2218 | } |
Peter Zijlstra | 6f932e5 | 2016-02-24 18:45:43 +0100 | [diff] [blame] | 2219 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2220 | /* |
| 2221 | * Should not happen, we validate the ctx is still alive before calling. |
| 2222 | */ |
| 2223 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) |
| 2224 | return; |
| 2225 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2226 | /* |
| 2227 | * Installing events is tricky because we cannot rely on ctx->is_active |
| 2228 | * to be set in case this is the nr_events 0 -> 1 transition. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2229 | */ |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2230 | again: |
| 2231 | /* |
| 2232 | * Cannot use task_function_call() because we need to run on the task's |
| 2233 | * CPU regardless of whether its current or not. |
| 2234 | */ |
| 2235 | if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event)) |
| 2236 | return; |
| 2237 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2238 | raw_spin_lock_irq(&ctx->lock); |
| 2239 | task = ctx->task; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2240 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
| 2241 | /* |
| 2242 | * Cannot happen because we already checked above (which also |
| 2243 | * cannot happen), and we hold ctx->mutex, which serializes us |
| 2244 | * against perf_event_exit_task_context(). |
| 2245 | */ |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2246 | raw_spin_unlock_irq(&ctx->lock); |
| 2247 | return; |
| 2248 | } |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2249 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2250 | /* |
| 2251 | * Since !ctx->is_active doesn't mean anything, we must IPI |
| 2252 | * unconditionally. |
| 2253 | */ |
| 2254 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | /* |
| 2258 | * Put a event into inactive state and update time fields. |
| 2259 | * Enabling the leader of a group effectively enables all |
| 2260 | * the group members that aren't explicitly disabled, so we |
| 2261 | * have to update their ->tstamp_enabled also. |
| 2262 | * Note: this works for group members as well as group leaders |
| 2263 | * since the non-leader members' sibling_lists will be empty. |
| 2264 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2265 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2266 | { |
| 2267 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2268 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2269 | |
| 2270 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2271 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2272 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2273 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 2274 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2275 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * Cross CPU call to enable a performance event |
| 2280 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2281 | static void __perf_event_enable(struct perf_event *event, |
| 2282 | struct perf_cpu_context *cpuctx, |
| 2283 | struct perf_event_context *ctx, |
| 2284 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2285 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2286 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2287 | struct perf_event_context *task_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2288 | |
Peter Zijlstra | 6e801e01 | 2016-01-26 12:17:08 +0100 | [diff] [blame] | 2289 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
| 2290 | event->state <= PERF_EVENT_STATE_ERROR) |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2291 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2292 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2293 | if (ctx->is_active) |
| 2294 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
| 2295 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2296 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2297 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2298 | if (!ctx->is_active) |
| 2299 | return; |
| 2300 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2301 | if (!event_filter_match(event)) { |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2302 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2303 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2304 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2305 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2306 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2307 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2308 | /* |
| 2309 | * If the event is in a group and isn't the group leader, |
| 2310 | * then don't put it on unless the group is on. |
| 2311 | */ |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2312 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
| 2313 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2314 | return; |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2315 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2316 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2317 | task_ctx = cpuctx->task_ctx; |
| 2318 | if (ctx->task) |
| 2319 | WARN_ON_ONCE(task_ctx != ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2320 | |
Peter Zijlstra | aee7dbc | 2016-01-08 10:45:11 +0100 | [diff] [blame] | 2321 | ctx_resched(cpuctx, task_ctx); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2322 | } |
| 2323 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2324 | /* |
| 2325 | * Enable a event. |
| 2326 | * |
| 2327 | * If event->ctx is a cloned context, callers must make sure that |
| 2328 | * every task struct that event->ctx->task could possibly point to |
| 2329 | * remains valid. This condition is satisfied when called through |
| 2330 | * perf_event_for_each_child or perf_event_for_each as described |
| 2331 | * for perf_event_disable. |
| 2332 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2333 | static void _perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2334 | { |
| 2335 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2336 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2337 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 6e801e01 | 2016-01-26 12:17:08 +0100 | [diff] [blame] | 2338 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
| 2339 | event->state < PERF_EVENT_STATE_ERROR) { |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2340 | raw_spin_unlock_irq(&ctx->lock); |
| 2341 | return; |
| 2342 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2343 | |
| 2344 | /* |
| 2345 | * If the event is in error state, clear that first. |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2346 | * |
| 2347 | * That way, if we see the event in error state below, we know that it |
| 2348 | * has gone back into error state, as distinct from the task having |
| 2349 | * been scheduled away before the cross-call arrived. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2350 | */ |
| 2351 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2352 | event->state = PERF_EVENT_STATE_OFF; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2353 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2354 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2355 | event_function_call(event, __perf_event_enable, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2356 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2357 | |
| 2358 | /* |
| 2359 | * See perf_event_disable(); |
| 2360 | */ |
| 2361 | void perf_event_enable(struct perf_event *event) |
| 2362 | { |
| 2363 | struct perf_event_context *ctx; |
| 2364 | |
| 2365 | ctx = perf_event_ctx_lock(event); |
| 2366 | _perf_event_enable(event); |
| 2367 | perf_event_ctx_unlock(event, ctx); |
| 2368 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2369 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2370 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2371 | struct stop_event_data { |
| 2372 | struct perf_event *event; |
| 2373 | unsigned int restart; |
| 2374 | }; |
| 2375 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2376 | static int __perf_event_stop(void *info) |
| 2377 | { |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2378 | struct stop_event_data *sd = info; |
| 2379 | struct perf_event *event = sd->event; |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2380 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2381 | /* if it's already INACTIVE, do nothing */ |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2382 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
| 2383 | return 0; |
| 2384 | |
| 2385 | /* matches smp_wmb() in event_sched_in() */ |
| 2386 | smp_rmb(); |
| 2387 | |
| 2388 | /* |
| 2389 | * There is a window with interrupts enabled before we get here, |
| 2390 | * so we need to check again lest we try to stop another CPU's event. |
| 2391 | */ |
| 2392 | if (READ_ONCE(event->oncpu) != smp_processor_id()) |
| 2393 | return -EAGAIN; |
| 2394 | |
| 2395 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2396 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2397 | /* |
| 2398 | * May race with the actual stop (through perf_pmu_output_stop()), |
| 2399 | * but it is only used for events with AUX ring buffer, and such |
| 2400 | * events will refuse to restart because of rb::aux_mmap_count==0, |
| 2401 | * see comments in perf_aux_output_begin(). |
| 2402 | * |
| 2403 | * Since this is happening on a event-local CPU, no trace is lost |
| 2404 | * while restarting. |
| 2405 | */ |
| 2406 | if (sd->restart) |
| 2407 | event->pmu->start(event, PERF_EF_START); |
| 2408 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 2409 | return 0; |
| 2410 | } |
| 2411 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 2412 | static int perf_event_restart(struct perf_event *event) |
| 2413 | { |
| 2414 | struct stop_event_data sd = { |
| 2415 | .event = event, |
| 2416 | .restart = 1, |
| 2417 | }; |
| 2418 | int ret = 0; |
| 2419 | |
| 2420 | do { |
| 2421 | if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) |
| 2422 | return 0; |
| 2423 | |
| 2424 | /* matches smp_wmb() in event_sched_in() */ |
| 2425 | smp_rmb(); |
| 2426 | |
| 2427 | /* |
| 2428 | * We only want to restart ACTIVE events, so if the event goes |
| 2429 | * inactive here (event->oncpu==-1), there's nothing more to do; |
| 2430 | * fall through with ret==-ENXIO. |
| 2431 | */ |
| 2432 | ret = cpu_function_call(READ_ONCE(event->oncpu), |
| 2433 | __perf_event_stop, &sd); |
| 2434 | } while (ret == -EAGAIN); |
| 2435 | |
| 2436 | return ret; |
| 2437 | } |
| 2438 | |
| 2439 | /* |
| 2440 | * In order to contain the amount of racy and tricky in the address filter |
| 2441 | * configuration management, it is a two part process: |
| 2442 | * |
| 2443 | * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, |
| 2444 | * we update the addresses of corresponding vmas in |
| 2445 | * event::addr_filters_offs array and bump the event::addr_filters_gen; |
| 2446 | * (p2) when an event is scheduled in (pmu::add), it calls |
| 2447 | * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() |
| 2448 | * if the generation has changed since the previous call. |
| 2449 | * |
| 2450 | * If (p1) happens while the event is active, we restart it to force (p2). |
| 2451 | * |
| 2452 | * (1) perf_addr_filters_apply(): adjusting filters' offsets based on |
| 2453 | * pre-existing mappings, called once when new filters arrive via SET_FILTER |
| 2454 | * ioctl; |
| 2455 | * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly |
| 2456 | * registered mapping, called for every new mmap(), with mm::mmap_sem down |
| 2457 | * for reading; |
| 2458 | * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process |
| 2459 | * of exec. |
| 2460 | */ |
| 2461 | void perf_event_addr_filters_sync(struct perf_event *event) |
| 2462 | { |
| 2463 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 2464 | |
| 2465 | if (!has_addr_filter(event)) |
| 2466 | return; |
| 2467 | |
| 2468 | raw_spin_lock(&ifh->lock); |
| 2469 | if (event->addr_filters_gen != event->hw.addr_filters_gen) { |
| 2470 | event->pmu->addr_filters_sync(event); |
| 2471 | event->hw.addr_filters_gen = event->addr_filters_gen; |
| 2472 | } |
| 2473 | raw_spin_unlock(&ifh->lock); |
| 2474 | } |
| 2475 | EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); |
| 2476 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2477 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2478 | { |
| 2479 | /* |
| 2480 | * not supported on inherited events |
| 2481 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2482 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2483 | return -EINVAL; |
| 2484 | |
| 2485 | atomic_add(refresh, &event->event_limit); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2486 | _perf_event_enable(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2487 | |
| 2488 | return 0; |
| 2489 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2490 | |
| 2491 | /* |
| 2492 | * See perf_event_disable() |
| 2493 | */ |
| 2494 | int perf_event_refresh(struct perf_event *event, int refresh) |
| 2495 | { |
| 2496 | struct perf_event_context *ctx; |
| 2497 | int ret; |
| 2498 | |
| 2499 | ctx = perf_event_ctx_lock(event); |
| 2500 | ret = _perf_event_refresh(event, refresh); |
| 2501 | perf_event_ctx_unlock(event, ctx); |
| 2502 | |
| 2503 | return ret; |
| 2504 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2505 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2506 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2507 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2508 | struct perf_cpu_context *cpuctx, |
| 2509 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2510 | { |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2511 | int is_active = ctx->is_active; |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2512 | struct perf_event *event; |
| 2513 | |
| 2514 | lockdep_assert_held(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2515 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2516 | if (likely(!ctx->nr_events)) { |
| 2517 | /* |
| 2518 | * See __perf_remove_from_context(). |
| 2519 | */ |
| 2520 | WARN_ON_ONCE(ctx->is_active); |
| 2521 | if (ctx->task) |
| 2522 | WARN_ON_ONCE(cpuctx->task_ctx); |
| 2523 | return; |
| 2524 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2525 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2526 | ctx->is_active &= ~event_type; |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2527 | if (!(ctx->is_active & EVENT_ALL)) |
| 2528 | ctx->is_active = 0; |
| 2529 | |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2530 | if (ctx->task) { |
| 2531 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 2532 | if (!ctx->is_active) |
| 2533 | cpuctx->task_ctx = NULL; |
| 2534 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2535 | |
Peter Zijlstra | 8fdc653 | 2016-03-29 09:26:44 +0200 | [diff] [blame] | 2536 | /* |
| 2537 | * Always update time if it was set; not only when it changes. |
| 2538 | * Otherwise we can 'forget' to update time for any but the last |
| 2539 | * context we sched out. For example: |
| 2540 | * |
| 2541 | * ctx_sched_out(.event_type = EVENT_FLEXIBLE) |
| 2542 | * ctx_sched_out(.event_type = EVENT_PINNED) |
| 2543 | * |
| 2544 | * would only update time for the pinned events. |
| 2545 | */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2546 | if (is_active & EVENT_TIME) { |
| 2547 | /* update (and stop) ctx time */ |
| 2548 | update_context_time(ctx); |
| 2549 | update_cgrp_time_from_cpuctx(cpuctx); |
| 2550 | } |
| 2551 | |
Peter Zijlstra | 8fdc653 | 2016-03-29 09:26:44 +0200 | [diff] [blame] | 2552 | is_active ^= ctx->is_active; /* changed bits */ |
| 2553 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2554 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2555 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2556 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2557 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2558 | if (is_active & EVENT_PINNED) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2559 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2560 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2561 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2562 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2563 | if (is_active & EVENT_FLEXIBLE) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2564 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2565 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2566 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2567 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | /* |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2571 | * Test whether two contexts are equivalent, i.e. whether they have both been |
| 2572 | * cloned from the same version of the same context. |
| 2573 | * |
| 2574 | * Equivalence is measured using a generation number in the context that is |
| 2575 | * incremented on each modification to it; see unclone_ctx(), list_add_event() |
| 2576 | * and list_del_event(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2577 | */ |
| 2578 | static int context_equiv(struct perf_event_context *ctx1, |
| 2579 | struct perf_event_context *ctx2) |
| 2580 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 2581 | lockdep_assert_held(&ctx1->lock); |
| 2582 | lockdep_assert_held(&ctx2->lock); |
| 2583 | |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2584 | /* Pinning disables the swap optimization */ |
| 2585 | if (ctx1->pin_count || ctx2->pin_count) |
| 2586 | return 0; |
| 2587 | |
| 2588 | /* If ctx1 is the parent of ctx2 */ |
| 2589 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) |
| 2590 | return 1; |
| 2591 | |
| 2592 | /* If ctx2 is the parent of ctx1 */ |
| 2593 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) |
| 2594 | return 1; |
| 2595 | |
| 2596 | /* |
| 2597 | * If ctx1 and ctx2 have the same parent; we flatten the parent |
| 2598 | * hierarchy, see perf_event_init_context(). |
| 2599 | */ |
| 2600 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && |
| 2601 | ctx1->parent_gen == ctx2->parent_gen) |
| 2602 | return 1; |
| 2603 | |
| 2604 | /* Unmatched */ |
| 2605 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2606 | } |
| 2607 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2608 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2609 | struct perf_event *next_event) |
| 2610 | { |
| 2611 | u64 value; |
| 2612 | |
| 2613 | if (!event->attr.inherit_stat) |
| 2614 | return; |
| 2615 | |
| 2616 | /* |
| 2617 | * Update the event value, we cannot use perf_event_read() |
| 2618 | * because we're in the middle of a context switch and have IRQs |
| 2619 | * disabled, which upsets smp_call_function_single(), however |
| 2620 | * we know the event must be on the current CPU, therefore we |
| 2621 | * don't need to use it. |
| 2622 | */ |
| 2623 | switch (event->state) { |
| 2624 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2625 | event->pmu->read(event); |
| 2626 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2627 | |
| 2628 | case PERF_EVENT_STATE_INACTIVE: |
| 2629 | update_event_times(event); |
| 2630 | break; |
| 2631 | |
| 2632 | default: |
| 2633 | break; |
| 2634 | } |
| 2635 | |
| 2636 | /* |
| 2637 | * In order to keep per-task stats reliable we need to flip the event |
| 2638 | * values when we flip the contexts. |
| 2639 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2640 | value = local64_read(&next_event->count); |
| 2641 | value = local64_xchg(&event->count, value); |
| 2642 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2643 | |
| 2644 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2645 | swap(event->total_time_running, next_event->total_time_running); |
| 2646 | |
| 2647 | /* |
| 2648 | * Since we swizzled the values, update the user visible data too. |
| 2649 | */ |
| 2650 | perf_event_update_userpage(event); |
| 2651 | perf_event_update_userpage(next_event); |
| 2652 | } |
| 2653 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2654 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2655 | struct perf_event_context *next_ctx) |
| 2656 | { |
| 2657 | struct perf_event *event, *next_event; |
| 2658 | |
| 2659 | if (!ctx->nr_stat) |
| 2660 | return; |
| 2661 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2662 | update_context_time(ctx); |
| 2663 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2664 | event = list_first_entry(&ctx->event_list, |
| 2665 | struct perf_event, event_entry); |
| 2666 | |
| 2667 | next_event = list_first_entry(&next_ctx->event_list, |
| 2668 | struct perf_event, event_entry); |
| 2669 | |
| 2670 | while (&event->event_entry != &ctx->event_list && |
| 2671 | &next_event->event_entry != &next_ctx->event_list) { |
| 2672 | |
| 2673 | __perf_event_sync_stat(event, next_event); |
| 2674 | |
| 2675 | event = list_next_entry(event, event_entry); |
| 2676 | next_event = list_next_entry(next_event, event_entry); |
| 2677 | } |
| 2678 | } |
| 2679 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2680 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2681 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2682 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2683 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2684 | struct perf_event_context *next_ctx; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2685 | struct perf_event_context *parent, *next_parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2686 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2687 | int do_switch = 1; |
| 2688 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2689 | if (likely(!ctx)) |
| 2690 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2691 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2692 | cpuctx = __get_cpu_context(ctx); |
| 2693 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2694 | return; |
| 2695 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2696 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2697 | next_ctx = next->perf_event_ctxp[ctxn]; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2698 | if (!next_ctx) |
| 2699 | goto unlock; |
| 2700 | |
| 2701 | parent = rcu_dereference(ctx->parent_ctx); |
| 2702 | next_parent = rcu_dereference(next_ctx->parent_ctx); |
| 2703 | |
| 2704 | /* If neither context have a parent context; they cannot be clones. */ |
Jiri Olsa | 802c8a6 | 2014-09-12 13:18:28 +0200 | [diff] [blame] | 2705 | if (!parent && !next_parent) |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2706 | goto unlock; |
| 2707 | |
| 2708 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2709 | /* |
| 2710 | * Looks like the two contexts are clones, so we might be |
| 2711 | * able to optimize the context switch. We lock both |
| 2712 | * contexts and check that they are clones under the |
| 2713 | * lock (including re-checking that neither has been |
| 2714 | * uncloned in the meantime). It doesn't matter which |
| 2715 | * order we take the locks because no other cpu could |
| 2716 | * be trying to lock both of these tasks. |
| 2717 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2718 | raw_spin_lock(&ctx->lock); |
| 2719 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2720 | if (context_equiv(ctx, next_ctx)) { |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2721 | WRITE_ONCE(ctx->task, next); |
| 2722 | WRITE_ONCE(next_ctx->task, task); |
Yan, Zheng | 5a158c3 | 2014-11-04 21:56:02 -0500 | [diff] [blame] | 2723 | |
| 2724 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); |
| 2725 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2726 | /* |
| 2727 | * RCU_INIT_POINTER here is safe because we've not |
| 2728 | * modified the ctx and the above modification of |
| 2729 | * ctx->task and ctx->task_ctx_data are immaterial |
| 2730 | * since those values are always verified under |
| 2731 | * ctx->lock which we're now holding. |
| 2732 | */ |
| 2733 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); |
| 2734 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); |
| 2735 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2736 | do_switch = 0; |
| 2737 | |
| 2738 | perf_event_sync_stat(ctx, next_ctx); |
| 2739 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2740 | raw_spin_unlock(&next_ctx->lock); |
| 2741 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2742 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2743 | unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2744 | rcu_read_unlock(); |
| 2745 | |
| 2746 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2747 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 8833d0e | 2016-01-08 10:02:37 +0100 | [diff] [blame] | 2748 | task_ctx_sched_out(cpuctx, ctx); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2749 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2750 | } |
| 2751 | } |
| 2752 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2753 | void perf_sched_cb_dec(struct pmu *pmu) |
| 2754 | { |
| 2755 | this_cpu_dec(perf_sched_cb_usages); |
| 2756 | } |
| 2757 | |
| 2758 | void perf_sched_cb_inc(struct pmu *pmu) |
| 2759 | { |
| 2760 | this_cpu_inc(perf_sched_cb_usages); |
| 2761 | } |
| 2762 | |
| 2763 | /* |
| 2764 | * This function provides the context switch callback to the lower code |
| 2765 | * layer. It is invoked ONLY when the context switch callback is enabled. |
| 2766 | */ |
| 2767 | static void perf_pmu_sched_task(struct task_struct *prev, |
| 2768 | struct task_struct *next, |
| 2769 | bool sched_in) |
| 2770 | { |
| 2771 | struct perf_cpu_context *cpuctx; |
| 2772 | struct pmu *pmu; |
| 2773 | unsigned long flags; |
| 2774 | |
| 2775 | if (prev == next) |
| 2776 | return; |
| 2777 | |
| 2778 | local_irq_save(flags); |
| 2779 | |
| 2780 | rcu_read_lock(); |
| 2781 | |
| 2782 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 2783 | if (pmu->sched_task) { |
| 2784 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2785 | |
| 2786 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2787 | |
| 2788 | perf_pmu_disable(pmu); |
| 2789 | |
| 2790 | pmu->sched_task(cpuctx->task_ctx, sched_in); |
| 2791 | |
| 2792 | perf_pmu_enable(pmu); |
| 2793 | |
| 2794 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | rcu_read_unlock(); |
| 2799 | |
| 2800 | local_irq_restore(flags); |
| 2801 | } |
| 2802 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 2803 | static void perf_event_switch(struct task_struct *task, |
| 2804 | struct task_struct *next_prev, bool sched_in); |
| 2805 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2806 | #define for_each_task_context_nr(ctxn) \ |
| 2807 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2808 | |
| 2809 | /* |
| 2810 | * Called from scheduler to remove the events of the current task, |
| 2811 | * with interrupts disabled. |
| 2812 | * |
| 2813 | * We stop each event and update the event value in event->count. |
| 2814 | * |
| 2815 | * This does not protect us against NMI, but disable() |
| 2816 | * sets the disabled bit in the control field of event _before_ |
| 2817 | * accessing the event control register. If a NMI hits, then it will |
| 2818 | * not restart the event. |
| 2819 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2820 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2821 | struct task_struct *next) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2822 | { |
| 2823 | int ctxn; |
| 2824 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2825 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 2826 | perf_pmu_sched_task(task, next, false); |
| 2827 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 2828 | if (atomic_read(&nr_switch_events)) |
| 2829 | perf_event_switch(task, next, false); |
| 2830 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2831 | for_each_task_context_nr(ctxn) |
| 2832 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2833 | |
| 2834 | /* |
| 2835 | * if cgroup events exist on this CPU, then we need |
| 2836 | * to check if we have to switch out PMU state. |
| 2837 | * cgroup event are system-wide mode only |
| 2838 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2839 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2840 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2841 | } |
| 2842 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2843 | /* |
| 2844 | * Called with IRQs disabled |
| 2845 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2846 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2847 | enum event_type_t event_type) |
| 2848 | { |
| 2849 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2853 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2854 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2855 | { |
| 2856 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2857 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2858 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2859 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2860 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2861 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2862 | continue; |
| 2863 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2864 | /* may need to reset tstamp_enabled */ |
| 2865 | if (is_cgroup_event(event)) |
| 2866 | perf_cgroup_mark_enabled(event, ctx); |
| 2867 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2868 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2869 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2870 | |
| 2871 | /* |
| 2872 | * If this pinned group hasn't been scheduled, |
| 2873 | * put it in error state. |
| 2874 | */ |
| 2875 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2876 | update_group_times(event); |
| 2877 | event->state = PERF_EVENT_STATE_ERROR; |
| 2878 | } |
| 2879 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | static void |
| 2883 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2884 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2885 | { |
| 2886 | struct perf_event *event; |
| 2887 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2888 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2889 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2890 | /* Ignore events in OFF or ERROR state */ |
| 2891 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2892 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2893 | /* |
| 2894 | * Listen to the 'cpu' scheduling filter constraint |
| 2895 | * of events: |
| 2896 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2897 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2898 | continue; |
| 2899 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2900 | /* may need to reset tstamp_enabled */ |
| 2901 | if (is_cgroup_event(event)) |
| 2902 | perf_cgroup_mark_enabled(event, ctx); |
| 2903 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2904 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2905 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2906 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2907 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2908 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | static void |
| 2912 | ctx_sched_in(struct perf_event_context *ctx, |
| 2913 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2914 | enum event_type_t event_type, |
| 2915 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2916 | { |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2917 | int is_active = ctx->is_active; |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2918 | u64 now; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2919 | |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2920 | lockdep_assert_held(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2921 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2922 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2923 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2924 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2925 | ctx->is_active |= (event_type | EVENT_TIME); |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2926 | if (ctx->task) { |
| 2927 | if (!is_active) |
| 2928 | cpuctx->task_ctx = ctx; |
| 2929 | else |
| 2930 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 2931 | } |
| 2932 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2933 | is_active ^= ctx->is_active; /* changed bits */ |
| 2934 | |
| 2935 | if (is_active & EVENT_TIME) { |
| 2936 | /* start ctx time */ |
| 2937 | now = perf_clock(); |
| 2938 | ctx->timestamp = now; |
| 2939 | perf_cgroup_set_timestamp(task, ctx); |
| 2940 | } |
| 2941 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2942 | /* |
| 2943 | * First go through the list and put on any pinned groups |
| 2944 | * in order to give them the best chance of going on. |
| 2945 | */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2946 | if (is_active & EVENT_PINNED) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2947 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2948 | |
| 2949 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2950 | if (is_active & EVENT_FLEXIBLE) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2951 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2952 | } |
| 2953 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2954 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2955 | enum event_type_t event_type, |
| 2956 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2957 | { |
| 2958 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2959 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2960 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2963 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2964 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2965 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2966 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2967 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2968 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2969 | if (cpuctx->task_ctx == ctx) |
| 2970 | return; |
| 2971 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2972 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2973 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2974 | /* |
| 2975 | * We want to keep the following priority order: |
| 2976 | * cpu pinned (that don't need to move), task pinned, |
| 2977 | * cpu flexible, task flexible. |
| 2978 | */ |
| 2979 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2980 | perf_event_sched_in(cpuctx, ctx, task); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2981 | perf_pmu_enable(ctx->pmu); |
| 2982 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2983 | } |
| 2984 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2985 | /* |
| 2986 | * Called from scheduler to add the events of the current task |
| 2987 | * with interrupts disabled. |
| 2988 | * |
| 2989 | * We restore the event value and then enable it. |
| 2990 | * |
| 2991 | * This does not protect us against NMI, but enable() |
| 2992 | * sets the enabled bit in the control field of event _before_ |
| 2993 | * accessing the event control register. If a NMI hits, then it will |
| 2994 | * keep the event running. |
| 2995 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2996 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 2997 | struct task_struct *task) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2998 | { |
| 2999 | struct perf_event_context *ctx; |
| 3000 | int ctxn; |
| 3001 | |
Peter Zijlstra | 7e41d17 | 2016-01-08 09:21:40 +0100 | [diff] [blame] | 3002 | /* |
| 3003 | * If cgroup events exist on this CPU, then we need to check if we have |
| 3004 | * to switch in PMU state; cgroup event are system-wide mode only. |
| 3005 | * |
| 3006 | * Since cgroup events are CPU events, we must schedule these in before |
| 3007 | * we schedule in the task events. |
| 3008 | */ |
| 3009 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
| 3010 | perf_cgroup_sched_in(prev, task); |
| 3011 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3012 | for_each_task_context_nr(ctxn) { |
| 3013 | ctx = task->perf_event_ctxp[ctxn]; |
| 3014 | if (likely(!ctx)) |
| 3015 | continue; |
| 3016 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3017 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3018 | } |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 3019 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3020 | if (atomic_read(&nr_switch_events)) |
| 3021 | perf_event_switch(task, prev, true); |
| 3022 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 3023 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 3024 | perf_pmu_sched_task(prev, task, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3025 | } |
| 3026 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3027 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 3028 | { |
| 3029 | u64 frequency = event->attr.sample_freq; |
| 3030 | u64 sec = NSEC_PER_SEC; |
| 3031 | u64 divisor, dividend; |
| 3032 | |
| 3033 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 3034 | |
| 3035 | count_fls = fls64(count); |
| 3036 | nsec_fls = fls64(nsec); |
| 3037 | frequency_fls = fls64(frequency); |
| 3038 | sec_fls = 30; |
| 3039 | |
| 3040 | /* |
| 3041 | * We got @count in @nsec, with a target of sample_freq HZ |
| 3042 | * the target period becomes: |
| 3043 | * |
| 3044 | * @count * 10^9 |
| 3045 | * period = ------------------- |
| 3046 | * @nsec * sample_freq |
| 3047 | * |
| 3048 | */ |
| 3049 | |
| 3050 | /* |
| 3051 | * Reduce accuracy by one bit such that @a and @b converge |
| 3052 | * to a similar magnitude. |
| 3053 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3054 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3055 | do { \ |
| 3056 | if (a##_fls > b##_fls) { \ |
| 3057 | a >>= 1; \ |
| 3058 | a##_fls--; \ |
| 3059 | } else { \ |
| 3060 | b >>= 1; \ |
| 3061 | b##_fls--; \ |
| 3062 | } \ |
| 3063 | } while (0) |
| 3064 | |
| 3065 | /* |
| 3066 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 3067 | * the other, so that finally we can do a u64/u64 division. |
| 3068 | */ |
| 3069 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 3070 | REDUCE_FLS(nsec, frequency); |
| 3071 | REDUCE_FLS(sec, count); |
| 3072 | } |
| 3073 | |
| 3074 | if (count_fls + sec_fls > 64) { |
| 3075 | divisor = nsec * frequency; |
| 3076 | |
| 3077 | while (count_fls + sec_fls > 64) { |
| 3078 | REDUCE_FLS(count, sec); |
| 3079 | divisor >>= 1; |
| 3080 | } |
| 3081 | |
| 3082 | dividend = count * sec; |
| 3083 | } else { |
| 3084 | dividend = count * sec; |
| 3085 | |
| 3086 | while (nsec_fls + frequency_fls > 64) { |
| 3087 | REDUCE_FLS(nsec, frequency); |
| 3088 | dividend >>= 1; |
| 3089 | } |
| 3090 | |
| 3091 | divisor = nsec * frequency; |
| 3092 | } |
| 3093 | |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 3094 | if (!divisor) |
| 3095 | return dividend; |
| 3096 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3097 | return div64_u64(dividend, divisor); |
| 3098 | } |
| 3099 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3100 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 3101 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 3102 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3103 | 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] | 3104 | { |
| 3105 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 3106 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3107 | s64 delta; |
| 3108 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3109 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3110 | |
| 3111 | delta = (s64)(period - hwc->sample_period); |
| 3112 | delta = (delta + 7) / 8; /* low pass filter */ |
| 3113 | |
| 3114 | sample_period = hwc->sample_period + delta; |
| 3115 | |
| 3116 | if (!sample_period) |
| 3117 | sample_period = 1; |
| 3118 | |
| 3119 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3120 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3121 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3122 | if (disable) |
| 3123 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3124 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3125 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3126 | |
| 3127 | if (disable) |
| 3128 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3129 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3130 | } |
| 3131 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3132 | /* |
| 3133 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 3134 | * events. At the same time, make sure, having freq events does not change |
| 3135 | * the rate of unthrottling as that would introduce bias. |
| 3136 | */ |
| 3137 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 3138 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3139 | { |
| 3140 | struct perf_event *event; |
| 3141 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3142 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3143 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3144 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3145 | /* |
| 3146 | * only need to iterate over all events iff: |
| 3147 | * - context have events in frequency mode (needs freq adjust) |
| 3148 | * - there are events to unthrottle on this cpu |
| 3149 | */ |
| 3150 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3151 | return; |
| 3152 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3153 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3154 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3155 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 3156 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3157 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 3158 | continue; |
| 3159 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 3160 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 3161 | continue; |
| 3162 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3163 | perf_pmu_disable(event->pmu); |
| 3164 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3165 | hwc = &event->hw; |
| 3166 | |
Jiri Olsa | ae23bff | 2013-08-24 16:45:54 +0200 | [diff] [blame] | 3167 | if (hwc->interrupts == MAX_INTERRUPTS) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3168 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3169 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 3170 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | if (!event->attr.freq || !event->attr.sample_freq) |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3174 | goto next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3175 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3176 | /* |
| 3177 | * stop the event and update event->count |
| 3178 | */ |
| 3179 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3180 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3181 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3182 | delta = now - hwc->freq_count_stamp; |
| 3183 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3184 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3185 | /* |
| 3186 | * restart the event |
| 3187 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3188 | * we have stopped the event so tell that |
| 3189 | * to perf_adjust_period() to avoid stopping it |
| 3190 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3191 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3192 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3193 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3194 | |
| 3195 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3196 | next: |
| 3197 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3198 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3199 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3200 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3201 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | /* |
| 3205 | * Round-robin a context's events: |
| 3206 | */ |
| 3207 | static void rotate_ctx(struct perf_event_context *ctx) |
| 3208 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 3209 | /* |
| 3210 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 3211 | * disabled by the inheritance code. |
| 3212 | */ |
| 3213 | if (!ctx->rotate_disable) |
| 3214 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3215 | } |
| 3216 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3217 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3218 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3219 | struct perf_event_context *ctx = NULL; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3220 | int rotate = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3221 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3222 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3223 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 3224 | rotate = 1; |
| 3225 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3226 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3227 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3228 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3229 | if (ctx->nr_events != ctx->nr_active) |
| 3230 | rotate = 1; |
| 3231 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3232 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3233 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3234 | goto done; |
| 3235 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 3236 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 3237 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3238 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3239 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 3240 | if (ctx) |
| 3241 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 3242 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3243 | rotate_ctx(&cpuctx->ctx); |
| 3244 | if (ctx) |
| 3245 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3246 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3247 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3248 | |
| 3249 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 3250 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3251 | done: |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3252 | |
| 3253 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | void perf_event_task_tick(void) |
| 3257 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3258 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
| 3259 | struct perf_event_context *ctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3260 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3261 | |
| 3262 | WARN_ON(!irqs_disabled()); |
| 3263 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3264 | __this_cpu_inc(perf_throttled_seq); |
| 3265 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3266 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3267 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3268 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3269 | perf_adjust_freq_unthr_context(ctx, throttled); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3270 | } |
| 3271 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3272 | static int event_enable_on_exec(struct perf_event *event, |
| 3273 | struct perf_event_context *ctx) |
| 3274 | { |
| 3275 | if (!event->attr.enable_on_exec) |
| 3276 | return 0; |
| 3277 | |
| 3278 | event->attr.enable_on_exec = 0; |
| 3279 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 3280 | return 0; |
| 3281 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 3282 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3283 | |
| 3284 | return 1; |
| 3285 | } |
| 3286 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3287 | /* |
| 3288 | * Enable all of a task's events that have been marked enable-on-exec. |
| 3289 | * This expects task == current. |
| 3290 | */ |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3291 | static void perf_event_enable_on_exec(int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3292 | { |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3293 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3294 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3295 | struct perf_event *event; |
| 3296 | unsigned long flags; |
| 3297 | int enabled = 0; |
| 3298 | |
| 3299 | local_irq_save(flags); |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3300 | ctx = current->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3301 | if (!ctx || !ctx->nr_events) |
| 3302 | goto out; |
| 3303 | |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3304 | cpuctx = __get_cpu_context(ctx); |
| 3305 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 7fce250 | 2016-02-24 18:45:48 +0100 | [diff] [blame] | 3306 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3307 | list_for_each_entry(event, &ctx->event_list, event_entry) |
| 3308 | enabled |= event_enable_on_exec(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3309 | |
| 3310 | /* |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3311 | * Unclone and reschedule this context if we enabled any event. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3312 | */ |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3313 | if (enabled) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3314 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3315 | ctx_resched(cpuctx, ctx); |
| 3316 | } |
| 3317 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3318 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3319 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3320 | local_irq_restore(flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3321 | |
| 3322 | if (clone_ctx) |
| 3323 | put_ctx(clone_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3324 | } |
| 3325 | |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3326 | struct perf_read_data { |
| 3327 | struct perf_event *event; |
| 3328 | bool group; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3329 | int ret; |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3330 | }; |
| 3331 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3332 | /* |
| 3333 | * Cross CPU call to read the hardware event |
| 3334 | */ |
| 3335 | static void __perf_event_read(void *info) |
| 3336 | { |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3337 | struct perf_read_data *data = info; |
| 3338 | struct perf_event *sub, *event = data->event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3339 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3340 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3341 | struct pmu *pmu = event->pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3342 | |
| 3343 | /* |
| 3344 | * If this is a task context, we need to check whether it is |
| 3345 | * the current task context of this cpu. If not it has been |
| 3346 | * scheduled out before the smp call arrived. In that case |
| 3347 | * event->count would have been updated to a recent sample |
| 3348 | * when the event was scheduled out. |
| 3349 | */ |
| 3350 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 3351 | return; |
| 3352 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3353 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3354 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3355 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3356 | update_cgrp_time_from_event(event); |
| 3357 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3358 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3359 | update_event_times(event); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3360 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3361 | goto unlock; |
| 3362 | |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3363 | if (!data->group) { |
| 3364 | pmu->read(event); |
| 3365 | data->ret = 0; |
| 3366 | goto unlock; |
| 3367 | } |
| 3368 | |
| 3369 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); |
| 3370 | |
| 3371 | pmu->read(event); |
| 3372 | |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3373 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 3374 | update_event_times(sub); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3375 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
| 3376 | /* |
| 3377 | * Use sibling's PMU rather than @event's since |
| 3378 | * sibling could be on different (eg: software) PMU. |
| 3379 | */ |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3380 | sub->pmu->read(sub); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3381 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3382 | } |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3383 | |
| 3384 | data->ret = pmu->commit_txn(pmu); |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3385 | |
| 3386 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3387 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3388 | } |
| 3389 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3390 | static inline u64 perf_event_count(struct perf_event *event) |
| 3391 | { |
Matt Fleming | eacd3ec | 2015-01-23 18:45:41 +0000 | [diff] [blame] | 3392 | if (event->pmu->count) |
| 3393 | return event->pmu->count(event); |
| 3394 | |
| 3395 | return __perf_event_count(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3396 | } |
| 3397 | |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 3398 | /* |
| 3399 | * NMI-safe method to read a local event, that is an event that |
| 3400 | * is: |
| 3401 | * - either for the current task, or for this CPU |
| 3402 | * - does not have inherit set, for inherited task events |
| 3403 | * will not be local and we cannot read them atomically |
| 3404 | * - must not have a pmu::count method |
| 3405 | */ |
| 3406 | u64 perf_event_read_local(struct perf_event *event) |
| 3407 | { |
| 3408 | unsigned long flags; |
| 3409 | u64 val; |
| 3410 | |
| 3411 | /* |
| 3412 | * Disabling interrupts avoids all counter scheduling (context |
| 3413 | * switches, timer based rotation and IPIs). |
| 3414 | */ |
| 3415 | local_irq_save(flags); |
| 3416 | |
| 3417 | /* If this is a per-task event, it must be for current */ |
| 3418 | WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) && |
| 3419 | event->hw.target != current); |
| 3420 | |
| 3421 | /* If this is a per-CPU event, it must be for this CPU */ |
| 3422 | WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) && |
| 3423 | event->cpu != smp_processor_id()); |
| 3424 | |
| 3425 | /* |
| 3426 | * It must not be an event with inherit set, we cannot read |
| 3427 | * all child counters from atomic context. |
| 3428 | */ |
| 3429 | WARN_ON_ONCE(event->attr.inherit); |
| 3430 | |
| 3431 | /* |
| 3432 | * It must not have a pmu::count method, those are not |
| 3433 | * NMI safe. |
| 3434 | */ |
| 3435 | WARN_ON_ONCE(event->pmu->count); |
| 3436 | |
| 3437 | /* |
| 3438 | * If the event is currently on this CPU, its either a per-task event, |
| 3439 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise |
| 3440 | * oncpu == -1). |
| 3441 | */ |
| 3442 | if (event->oncpu == smp_processor_id()) |
| 3443 | event->pmu->read(event); |
| 3444 | |
| 3445 | val = local64_read(&event->count); |
| 3446 | local_irq_restore(flags); |
| 3447 | |
| 3448 | return val; |
| 3449 | } |
| 3450 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3451 | static int perf_event_read(struct perf_event *event, bool group) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3452 | { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3453 | int ret = 0; |
| 3454 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3455 | /* |
| 3456 | * If event is enabled and currently active on a CPU, update the |
| 3457 | * value in the event structure: |
| 3458 | */ |
| 3459 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3460 | struct perf_read_data data = { |
| 3461 | .event = event, |
| 3462 | .group = group, |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3463 | .ret = 0, |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3464 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3465 | smp_call_function_single(event->oncpu, |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3466 | __perf_event_read, &data, 1); |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3467 | ret = data.ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3468 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 3469 | struct perf_event_context *ctx = event->ctx; |
| 3470 | unsigned long flags; |
| 3471 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3472 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3473 | /* |
| 3474 | * may read while context is not active |
| 3475 | * (e.g., thread is blocked), in that case |
| 3476 | * we cannot update context time |
| 3477 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3478 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3479 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3480 | update_cgrp_time_from_event(event); |
| 3481 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3482 | if (group) |
| 3483 | update_group_times(event); |
| 3484 | else |
| 3485 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3486 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3487 | } |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3488 | |
| 3489 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | /* |
| 3493 | * Initialize the perf_event context in a task_struct: |
| 3494 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3495 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3496 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3497 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3498 | mutex_init(&ctx->mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3499 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3500 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 3501 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3502 | INIT_LIST_HEAD(&ctx->event_list); |
| 3503 | atomic_set(&ctx->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3504 | } |
| 3505 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3506 | static struct perf_event_context * |
| 3507 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3508 | { |
| 3509 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3510 | |
| 3511 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 3512 | if (!ctx) |
| 3513 | return NULL; |
| 3514 | |
| 3515 | __perf_event_init_context(ctx); |
| 3516 | if (task) { |
| 3517 | ctx->task = task; |
| 3518 | get_task_struct(task); |
| 3519 | } |
| 3520 | ctx->pmu = pmu; |
| 3521 | |
| 3522 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3523 | } |
| 3524 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3525 | static struct task_struct * |
| 3526 | find_lively_task_by_vpid(pid_t vpid) |
| 3527 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3528 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3529 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3530 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3531 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3532 | task = current; |
| 3533 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3534 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3535 | if (task) |
| 3536 | get_task_struct(task); |
| 3537 | rcu_read_unlock(); |
| 3538 | |
| 3539 | if (!task) |
| 3540 | return ERR_PTR(-ESRCH); |
| 3541 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3542 | return task; |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3543 | } |
| 3544 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3545 | /* |
| 3546 | * Returns a matching context with refcount and pincount. |
| 3547 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3548 | static struct perf_event_context * |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3549 | find_get_context(struct pmu *pmu, struct task_struct *task, |
| 3550 | struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3551 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3552 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3553 | struct perf_cpu_context *cpuctx; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3554 | void *task_ctx_data = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3555 | unsigned long flags; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3556 | int ctxn, err; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3557 | int cpu = event->cpu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3558 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3559 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3560 | /* Must be root to operate on a CPU event: */ |
| 3561 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3562 | return ERR_PTR(-EACCES); |
| 3563 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3564 | /* |
| 3565 | * We could be clever and allow to attach a event to an |
| 3566 | * offline CPU and activate it when the CPU comes up, but |
| 3567 | * that's for later. |
| 3568 | */ |
| 3569 | if (!cpu_online(cpu)) |
| 3570 | return ERR_PTR(-ENODEV); |
| 3571 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3572 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3573 | ctx = &cpuctx->ctx; |
| 3574 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3575 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3576 | |
| 3577 | return ctx; |
| 3578 | } |
| 3579 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3580 | err = -EINVAL; |
| 3581 | ctxn = pmu->task_ctx_nr; |
| 3582 | if (ctxn < 0) |
| 3583 | goto errout; |
| 3584 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3585 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
| 3586 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); |
| 3587 | if (!task_ctx_data) { |
| 3588 | err = -ENOMEM; |
| 3589 | goto errout; |
| 3590 | } |
| 3591 | } |
| 3592 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3593 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3594 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3595 | if (ctx) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3596 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3597 | ++ctx->pin_count; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3598 | |
| 3599 | if (task_ctx_data && !ctx->task_ctx_data) { |
| 3600 | ctx->task_ctx_data = task_ctx_data; |
| 3601 | task_ctx_data = NULL; |
| 3602 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3603 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3604 | |
| 3605 | if (clone_ctx) |
| 3606 | put_ctx(clone_ctx); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3607 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3608 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3609 | err = -ENOMEM; |
| 3610 | if (!ctx) |
| 3611 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3612 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3613 | if (task_ctx_data) { |
| 3614 | ctx->task_ctx_data = task_ctx_data; |
| 3615 | task_ctx_data = NULL; |
| 3616 | } |
| 3617 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3618 | err = 0; |
| 3619 | mutex_lock(&task->perf_event_mutex); |
| 3620 | /* |
| 3621 | * If it has already passed perf_event_exit_task(). |
| 3622 | * we must see PF_EXITING, it takes this mutex too. |
| 3623 | */ |
| 3624 | if (task->flags & PF_EXITING) |
| 3625 | err = -ESRCH; |
| 3626 | else if (task->perf_event_ctxp[ctxn]) |
| 3627 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3628 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3629 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3630 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3631 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3632 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3633 | mutex_unlock(&task->perf_event_mutex); |
| 3634 | |
| 3635 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3636 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3637 | |
| 3638 | if (err == -EAGAIN) |
| 3639 | goto retry; |
| 3640 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3641 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3642 | } |
| 3643 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3644 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3645 | return ctx; |
| 3646 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3647 | errout: |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3648 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3649 | return ERR_PTR(err); |
| 3650 | } |
| 3651 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3652 | static void perf_event_free_filter(struct perf_event *event); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 3653 | static void perf_event_free_bpf_prog(struct perf_event *event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3654 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3655 | static void free_event_rcu(struct rcu_head *head) |
| 3656 | { |
| 3657 | struct perf_event *event; |
| 3658 | |
| 3659 | event = container_of(head, struct perf_event, rcu_head); |
| 3660 | if (event->ns) |
| 3661 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3662 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3663 | kfree(event); |
| 3664 | } |
| 3665 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3666 | static void ring_buffer_attach(struct perf_event *event, |
| 3667 | struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3668 | |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 3669 | static void detach_sb_event(struct perf_event *event) |
| 3670 | { |
| 3671 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); |
| 3672 | |
| 3673 | raw_spin_lock(&pel->lock); |
| 3674 | list_del_rcu(&event->sb_list); |
| 3675 | raw_spin_unlock(&pel->lock); |
| 3676 | } |
| 3677 | |
| 3678 | static void unaccount_pmu_sb_event(struct perf_event *event) |
| 3679 | { |
| 3680 | if (event->parent) |
| 3681 | return; |
| 3682 | |
| 3683 | if (event->attach_state & PERF_ATTACH_TASK) |
| 3684 | return; |
| 3685 | |
| 3686 | detach_sb_event(event); |
| 3687 | } |
| 3688 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3689 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
| 3690 | { |
| 3691 | if (event->parent) |
| 3692 | return; |
| 3693 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3694 | if (is_cgroup_event(event)) |
| 3695 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); |
| 3696 | } |
| 3697 | |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3698 | #ifdef CONFIG_NO_HZ_FULL |
| 3699 | static DEFINE_SPINLOCK(nr_freq_lock); |
| 3700 | #endif |
| 3701 | |
| 3702 | static void unaccount_freq_event_nohz(void) |
| 3703 | { |
| 3704 | #ifdef CONFIG_NO_HZ_FULL |
| 3705 | spin_lock(&nr_freq_lock); |
| 3706 | if (atomic_dec_and_test(&nr_freq_events)) |
| 3707 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); |
| 3708 | spin_unlock(&nr_freq_lock); |
| 3709 | #endif |
| 3710 | } |
| 3711 | |
| 3712 | static void unaccount_freq_event(void) |
| 3713 | { |
| 3714 | if (tick_nohz_full_enabled()) |
| 3715 | unaccount_freq_event_nohz(); |
| 3716 | else |
| 3717 | atomic_dec(&nr_freq_events); |
| 3718 | } |
| 3719 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3720 | static void unaccount_event(struct perf_event *event) |
| 3721 | { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3722 | bool dec = false; |
| 3723 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3724 | if (event->parent) |
| 3725 | return; |
| 3726 | |
| 3727 | if (event->attach_state & PERF_ATTACH_TASK) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3728 | dec = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3729 | if (event->attr.mmap || event->attr.mmap_data) |
| 3730 | atomic_dec(&nr_mmap_events); |
| 3731 | if (event->attr.comm) |
| 3732 | atomic_dec(&nr_comm_events); |
| 3733 | if (event->attr.task) |
| 3734 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3735 | if (event->attr.freq) |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3736 | unaccount_freq_event(); |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3737 | if (event->attr.context_switch) { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3738 | dec = true; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3739 | atomic_dec(&nr_switch_events); |
| 3740 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3741 | if (is_cgroup_event(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3742 | dec = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3743 | if (has_branch_stack(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3744 | dec = true; |
| 3745 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 3746 | if (dec) { |
| 3747 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) |
| 3748 | schedule_delayed_work(&perf_sched_work, HZ); |
| 3749 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3750 | |
| 3751 | unaccount_event_cpu(event, event->cpu); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 3752 | |
| 3753 | unaccount_pmu_sb_event(event); |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3754 | } |
| 3755 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 3756 | static void perf_sched_delayed(struct work_struct *work) |
| 3757 | { |
| 3758 | mutex_lock(&perf_sched_mutex); |
| 3759 | if (atomic_dec_and_test(&perf_sched_count)) |
| 3760 | static_branch_disable(&perf_sched_events); |
| 3761 | mutex_unlock(&perf_sched_mutex); |
| 3762 | } |
| 3763 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 3764 | /* |
| 3765 | * The following implement mutual exclusion of events on "exclusive" pmus |
| 3766 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled |
| 3767 | * at a time, so we disallow creating events that might conflict, namely: |
| 3768 | * |
| 3769 | * 1) cpu-wide events in the presence of per-task events, |
| 3770 | * 2) per-task events in the presence of cpu-wide events, |
| 3771 | * 3) two matching events on the same context. |
| 3772 | * |
| 3773 | * 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] | 3774 | * _free_event()), the latter -- before the first perf_install_in_context(). |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 3775 | */ |
| 3776 | static int exclusive_event_init(struct perf_event *event) |
| 3777 | { |
| 3778 | struct pmu *pmu = event->pmu; |
| 3779 | |
| 3780 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3781 | return 0; |
| 3782 | |
| 3783 | /* |
| 3784 | * Prevent co-existence of per-task and cpu-wide events on the |
| 3785 | * same exclusive pmu. |
| 3786 | * |
| 3787 | * Negative pmu::exclusive_cnt means there are cpu-wide |
| 3788 | * events on this "exclusive" pmu, positive means there are |
| 3789 | * per-task events. |
| 3790 | * |
| 3791 | * Since this is called in perf_event_alloc() path, event::ctx |
| 3792 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK |
| 3793 | * to mean "per-task event", because unlike other attach states it |
| 3794 | * never gets cleared. |
| 3795 | */ |
| 3796 | if (event->attach_state & PERF_ATTACH_TASK) { |
| 3797 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) |
| 3798 | return -EBUSY; |
| 3799 | } else { |
| 3800 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) |
| 3801 | return -EBUSY; |
| 3802 | } |
| 3803 | |
| 3804 | return 0; |
| 3805 | } |
| 3806 | |
| 3807 | static void exclusive_event_destroy(struct perf_event *event) |
| 3808 | { |
| 3809 | struct pmu *pmu = event->pmu; |
| 3810 | |
| 3811 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3812 | return; |
| 3813 | |
| 3814 | /* see comment in exclusive_event_init() */ |
| 3815 | if (event->attach_state & PERF_ATTACH_TASK) |
| 3816 | atomic_dec(&pmu->exclusive_cnt); |
| 3817 | else |
| 3818 | atomic_inc(&pmu->exclusive_cnt); |
| 3819 | } |
| 3820 | |
| 3821 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) |
| 3822 | { |
| 3823 | if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && |
| 3824 | (e1->cpu == e2->cpu || |
| 3825 | e1->cpu == -1 || |
| 3826 | e2->cpu == -1)) |
| 3827 | return true; |
| 3828 | return false; |
| 3829 | } |
| 3830 | |
| 3831 | /* Called under the same ctx::mutex as perf_install_in_context() */ |
| 3832 | static bool exclusive_event_installable(struct perf_event *event, |
| 3833 | struct perf_event_context *ctx) |
| 3834 | { |
| 3835 | struct perf_event *iter_event; |
| 3836 | struct pmu *pmu = event->pmu; |
| 3837 | |
| 3838 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3839 | return true; |
| 3840 | |
| 3841 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { |
| 3842 | if (exclusive_event_match(iter_event, event)) |
| 3843 | return false; |
| 3844 | } |
| 3845 | |
| 3846 | return true; |
| 3847 | } |
| 3848 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 3849 | static void perf_addr_filters_splice(struct perf_event *event, |
| 3850 | struct list_head *head); |
| 3851 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3852 | static void _free_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3853 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3854 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3855 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3856 | unaccount_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3857 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3858 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3859 | /* |
| 3860 | * Can happen when we close an event with re-directed output. |
| 3861 | * |
| 3862 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 3863 | * over us; possibly making our ring_buffer_put() the last. |
| 3864 | */ |
| 3865 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3866 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3867 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3868 | } |
| 3869 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3870 | if (is_cgroup_event(event)) |
| 3871 | perf_detach_cgroup(event); |
| 3872 | |
Peter Zijlstra | a0733e6 | 2016-01-26 12:14:40 +0100 | [diff] [blame] | 3873 | if (!event->parent) { |
| 3874 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 3875 | put_callchain_buffers(); |
| 3876 | } |
| 3877 | |
| 3878 | perf_event_free_bpf_prog(event); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 3879 | perf_addr_filters_splice(event, NULL); |
| 3880 | kfree(event->addr_filters_offs); |
Peter Zijlstra | a0733e6 | 2016-01-26 12:14:40 +0100 | [diff] [blame] | 3881 | |
| 3882 | if (event->destroy) |
| 3883 | event->destroy(event); |
| 3884 | |
| 3885 | if (event->ctx) |
| 3886 | put_ctx(event->ctx); |
| 3887 | |
| 3888 | if (event->pmu) { |
| 3889 | exclusive_event_destroy(event); |
| 3890 | module_put(event->pmu->module); |
| 3891 | } |
| 3892 | |
| 3893 | call_rcu(&event->rcu_head, free_event_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3894 | } |
| 3895 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3896 | /* |
| 3897 | * Used to free events which have a known refcount of 1, such as in error paths |
| 3898 | * where the event isn't exposed yet and inherited events. |
| 3899 | */ |
| 3900 | static void free_event(struct perf_event *event) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3901 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3902 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
| 3903 | "unexpected event refcount: %ld; ptr=%p\n", |
| 3904 | atomic_long_read(&event->refcount), event)) { |
| 3905 | /* leak to avoid use-after-free */ |
| 3906 | return; |
| 3907 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3908 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3909 | _free_event(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3910 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3911 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3912 | /* |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3913 | * Remove user event from the owner task. |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3914 | */ |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3915 | static void perf_remove_from_owner(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3916 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3917 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3918 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3919 | rcu_read_lock(); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3920 | /* |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3921 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
| 3922 | * observe !owner it means the list deletion is complete and we can |
| 3923 | * indeed free this event, otherwise we need to serialize on |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3924 | * owner->perf_event_mutex. |
| 3925 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3926 | owner = lockless_dereference(event->owner); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3927 | if (owner) { |
| 3928 | /* |
| 3929 | * Since delayed_put_task_struct() also drops the last |
| 3930 | * task reference we can safely take a new reference |
| 3931 | * while holding the rcu_read_lock(). |
| 3932 | */ |
| 3933 | get_task_struct(owner); |
| 3934 | } |
| 3935 | rcu_read_unlock(); |
| 3936 | |
| 3937 | if (owner) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3938 | /* |
| 3939 | * If we're here through perf_event_exit_task() we're already |
| 3940 | * holding ctx->mutex which would be an inversion wrt. the |
| 3941 | * normal lock order. |
| 3942 | * |
| 3943 | * However we can safely take this lock because its the child |
| 3944 | * ctx->mutex. |
| 3945 | */ |
| 3946 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); |
| 3947 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3948 | /* |
| 3949 | * We have to re-check the event->owner field, if it is cleared |
| 3950 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 3951 | * ensured they're done, and we can proceed with freeing the |
| 3952 | * event. |
| 3953 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3954 | if (event->owner) { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3955 | list_del_init(&event->owner_entry); |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3956 | smp_store_release(&event->owner, NULL); |
| 3957 | } |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3958 | mutex_unlock(&owner->perf_event_mutex); |
| 3959 | put_task_struct(owner); |
| 3960 | } |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3961 | } |
| 3962 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3963 | static void put_event(struct perf_event *event) |
| 3964 | { |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3965 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 3966 | return; |
| 3967 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3968 | _free_event(event); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3969 | } |
| 3970 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3971 | /* |
| 3972 | * Kill an event dead; while event:refcount will preserve the event |
| 3973 | * object, it will not preserve its functionality. Once the last 'user' |
| 3974 | * gives up the object, we'll destroy the thing. |
| 3975 | */ |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3976 | int perf_event_release_kernel(struct perf_event *event) |
| 3977 | { |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 3978 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3979 | struct perf_event *child, *tmp; |
| 3980 | |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 3981 | /* |
| 3982 | * If we got here through err_file: fput(event_file); we will not have |
| 3983 | * attached to a context yet. |
| 3984 | */ |
| 3985 | if (!ctx) { |
| 3986 | WARN_ON_ONCE(event->attach_state & |
| 3987 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); |
| 3988 | goto no_ctx; |
| 3989 | } |
| 3990 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3991 | if (!is_kernel_event(event)) |
| 3992 | perf_remove_from_owner(event); |
| 3993 | |
Peter Zijlstra | 5fa7c8e | 2016-01-26 15:25:15 +0100 | [diff] [blame] | 3994 | ctx = perf_event_ctx_lock(event); |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3995 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3996 | perf_remove_from_context(event, DETACH_GROUP); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3997 | |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3998 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 3999 | /* |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4000 | * Mark this even as STATE_DEAD, there is no external reference to it |
| 4001 | * anymore. |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4002 | * |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4003 | * Anybody acquiring event->child_mutex after the below loop _must_ |
| 4004 | * also see this, most importantly inherit_event() which will avoid |
| 4005 | * placing more children on the list. |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4006 | * |
| 4007 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
| 4008 | * child events. |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 4009 | */ |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4010 | event->state = PERF_EVENT_STATE_DEAD; |
| 4011 | raw_spin_unlock_irq(&ctx->lock); |
| 4012 | |
| 4013 | perf_event_ctx_unlock(event, ctx); |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 4014 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4015 | again: |
| 4016 | mutex_lock(&event->child_mutex); |
| 4017 | list_for_each_entry(child, &event->child_list, child_list) { |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 4018 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4019 | /* |
| 4020 | * Cannot change, child events are not migrated, see the |
| 4021 | * comment with perf_event_ctx_lock_nested(). |
| 4022 | */ |
| 4023 | ctx = lockless_dereference(child->ctx); |
| 4024 | /* |
| 4025 | * Since child_mutex nests inside ctx::mutex, we must jump |
| 4026 | * through hoops. We start by grabbing a reference on the ctx. |
| 4027 | * |
| 4028 | * Since the event cannot get freed while we hold the |
| 4029 | * child_mutex, the context must also exist and have a !0 |
| 4030 | * reference count. |
| 4031 | */ |
| 4032 | get_ctx(ctx); |
| 4033 | |
| 4034 | /* |
| 4035 | * Now that we have a ctx ref, we can drop child_mutex, and |
| 4036 | * acquire ctx::mutex without fear of it going away. Then we |
| 4037 | * can re-acquire child_mutex. |
| 4038 | */ |
| 4039 | mutex_unlock(&event->child_mutex); |
| 4040 | mutex_lock(&ctx->mutex); |
| 4041 | mutex_lock(&event->child_mutex); |
| 4042 | |
| 4043 | /* |
| 4044 | * Now that we hold ctx::mutex and child_mutex, revalidate our |
| 4045 | * state, if child is still the first entry, it didn't get freed |
| 4046 | * and we can continue doing so. |
| 4047 | */ |
| 4048 | tmp = list_first_entry_or_null(&event->child_list, |
| 4049 | struct perf_event, child_list); |
| 4050 | if (tmp == child) { |
| 4051 | perf_remove_from_context(child, DETACH_GROUP); |
| 4052 | list_del(&child->child_list); |
| 4053 | free_event(child); |
| 4054 | /* |
| 4055 | * This matches the refcount bump in inherit_event(); |
| 4056 | * this can't be the last reference. |
| 4057 | */ |
| 4058 | put_event(event); |
| 4059 | } |
| 4060 | |
| 4061 | mutex_unlock(&event->child_mutex); |
| 4062 | mutex_unlock(&ctx->mutex); |
| 4063 | put_ctx(ctx); |
| 4064 | goto again; |
| 4065 | } |
| 4066 | mutex_unlock(&event->child_mutex); |
| 4067 | |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 4068 | no_ctx: |
| 4069 | put_event(event); /* Must be the 'last' reference */ |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 4070 | return 0; |
| 4071 | } |
| 4072 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 4073 | |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 4074 | /* |
| 4075 | * Called when the last reference to the file is gone. |
| 4076 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 4077 | static int perf_release(struct inode *inode, struct file *file) |
| 4078 | { |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 4079 | perf_event_release_kernel(file->private_data); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 4080 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 4081 | } |
| 4082 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4083 | 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] | 4084 | { |
| 4085 | struct perf_event *child; |
| 4086 | u64 total = 0; |
| 4087 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4088 | *enabled = 0; |
| 4089 | *running = 0; |
| 4090 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 4091 | mutex_lock(&event->child_mutex); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 4092 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4093 | (void)perf_event_read(event, false); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 4094 | total += perf_event_count(event); |
| 4095 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4096 | *enabled += event->total_time_enabled + |
| 4097 | atomic64_read(&event->child_total_time_enabled); |
| 4098 | *running += event->total_time_running + |
| 4099 | atomic64_read(&event->child_total_time_running); |
| 4100 | |
| 4101 | list_for_each_entry(child, &event->child_list, child_list) { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4102 | (void)perf_event_read(child, false); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 4103 | total += perf_event_count(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4104 | *enabled += child->total_time_enabled; |
| 4105 | *running += child->total_time_running; |
| 4106 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 4107 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4108 | |
| 4109 | return total; |
| 4110 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 4111 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4112 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4113 | static int __perf_read_group_add(struct perf_event *leader, |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4114 | u64 read_format, u64 *values) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4115 | { |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4116 | struct perf_event *sub; |
| 4117 | int n = 1; /* skip @nr */ |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4118 | int ret; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4119 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4120 | ret = perf_event_read(leader, true); |
| 4121 | if (ret) |
| 4122 | return ret; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4123 | |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4124 | /* |
| 4125 | * Since we co-schedule groups, {enabled,running} times of siblings |
| 4126 | * will be identical to those of the leader, so we only publish one |
| 4127 | * set. |
| 4128 | */ |
| 4129 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
| 4130 | values[n++] += leader->total_time_enabled + |
| 4131 | atomic64_read(&leader->child_total_time_enabled); |
| 4132 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4133 | |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4134 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
| 4135 | values[n++] += leader->total_time_running + |
| 4136 | atomic64_read(&leader->child_total_time_running); |
| 4137 | } |
| 4138 | |
| 4139 | /* |
| 4140 | * Write {count,id} tuples for every sibling. |
| 4141 | */ |
| 4142 | values[n++] += perf_event_count(leader); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4143 | if (read_format & PERF_FORMAT_ID) |
| 4144 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4145 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4146 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4147 | values[n++] += perf_event_count(sub); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4148 | if (read_format & PERF_FORMAT_ID) |
| 4149 | values[n++] = primary_event_id(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4150 | } |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4151 | |
| 4152 | return 0; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | static int perf_read_group(struct perf_event *event, |
| 4156 | u64 read_format, char __user *buf) |
| 4157 | { |
| 4158 | struct perf_event *leader = event->group_leader, *child; |
| 4159 | struct perf_event_context *ctx = leader->ctx; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4160 | int ret; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4161 | u64 *values; |
| 4162 | |
| 4163 | lockdep_assert_held(&ctx->mutex); |
| 4164 | |
| 4165 | values = kzalloc(event->read_size, GFP_KERNEL); |
| 4166 | if (!values) |
| 4167 | return -ENOMEM; |
| 4168 | |
| 4169 | values[0] = 1 + leader->nr_siblings; |
| 4170 | |
| 4171 | /* |
| 4172 | * By locking the child_mutex of the leader we effectively |
| 4173 | * lock the child list of all siblings.. XXX explain how. |
| 4174 | */ |
| 4175 | mutex_lock(&leader->child_mutex); |
| 4176 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4177 | ret = __perf_read_group_add(leader, read_format, values); |
| 4178 | if (ret) |
| 4179 | goto unlock; |
| 4180 | |
| 4181 | list_for_each_entry(child, &leader->child_list, child_list) { |
| 4182 | ret = __perf_read_group_add(child, read_format, values); |
| 4183 | if (ret) |
| 4184 | goto unlock; |
| 4185 | } |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4186 | |
| 4187 | mutex_unlock(&leader->child_mutex); |
| 4188 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4189 | ret = event->read_size; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4190 | if (copy_to_user(buf, values, event->read_size)) |
| 4191 | ret = -EFAULT; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4192 | goto out; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4193 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4194 | unlock: |
| 4195 | mutex_unlock(&leader->child_mutex); |
| 4196 | out: |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4197 | kfree(values); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4198 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4199 | } |
| 4200 | |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4201 | static int perf_read_one(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4202 | u64 read_format, char __user *buf) |
| 4203 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4204 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4205 | u64 values[4]; |
| 4206 | int n = 0; |
| 4207 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4208 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 4209 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 4210 | values[n++] = enabled; |
| 4211 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4212 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4213 | if (read_format & PERF_FORMAT_ID) |
| 4214 | values[n++] = primary_event_id(event); |
| 4215 | |
| 4216 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 4217 | return -EFAULT; |
| 4218 | |
| 4219 | return n * sizeof(u64); |
| 4220 | } |
| 4221 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4222 | static bool is_event_hup(struct perf_event *event) |
| 4223 | { |
| 4224 | bool no_children; |
| 4225 | |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4226 | if (event->state > PERF_EVENT_STATE_EXIT) |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4227 | return false; |
| 4228 | |
| 4229 | mutex_lock(&event->child_mutex); |
| 4230 | no_children = list_empty(&event->child_list); |
| 4231 | mutex_unlock(&event->child_mutex); |
| 4232 | return no_children; |
| 4233 | } |
| 4234 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4235 | /* |
| 4236 | * Read the performance event - simple non blocking version for now |
| 4237 | */ |
| 4238 | static ssize_t |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4239 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4240 | { |
| 4241 | u64 read_format = event->attr.read_format; |
| 4242 | int ret; |
| 4243 | |
| 4244 | /* |
| 4245 | * Return end-of-file for a read on a event that is in |
| 4246 | * error state (i.e. because it was pinned but it couldn't be |
| 4247 | * scheduled on to the CPU at some point). |
| 4248 | */ |
| 4249 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 4250 | return 0; |
| 4251 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4252 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4253 | return -ENOSPC; |
| 4254 | |
| 4255 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4256 | if (read_format & PERF_FORMAT_GROUP) |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4257 | ret = perf_read_group(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4258 | else |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4259 | ret = perf_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4260 | |
| 4261 | return ret; |
| 4262 | } |
| 4263 | |
| 4264 | static ssize_t |
| 4265 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 4266 | { |
| 4267 | struct perf_event *event = file->private_data; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4268 | struct perf_event_context *ctx; |
| 4269 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4270 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4271 | ctx = perf_event_ctx_lock(event); |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4272 | ret = __perf_read(event, buf, count); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4273 | perf_event_ctx_unlock(event, ctx); |
| 4274 | |
| 4275 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 4279 | { |
| 4280 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4281 | struct ring_buffer *rb; |
Jiri Olsa | 61b6768 | 2014-08-13 19:39:56 +0200 | [diff] [blame] | 4282 | unsigned int events = POLLHUP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4283 | |
Sebastian Andrzej Siewior | e708d7a | 2014-08-04 15:31:08 +0200 | [diff] [blame] | 4284 | poll_wait(file, &event->waitq, wait); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 4285 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4286 | if (is_event_hup(event)) |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 4287 | return events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4288 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4289 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4290 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 4291 | * 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] | 4292 | */ |
| 4293 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4294 | rb = event->rb; |
| 4295 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4296 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4297 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4298 | return events; |
| 4299 | } |
| 4300 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4301 | static void _perf_event_reset(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4302 | { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4303 | (void)perf_event_read(event, false); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4304 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4305 | perf_event_update_userpage(event); |
| 4306 | } |
| 4307 | |
| 4308 | /* |
| 4309 | * Holding the top-level event's child_mutex means that any |
| 4310 | * descendant process that has inherited this event will block |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 4311 | * 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] | 4312 | * task existence requirements of perf_event_enable/disable. |
| 4313 | */ |
| 4314 | static void perf_event_for_each_child(struct perf_event *event, |
| 4315 | void (*func)(struct perf_event *)) |
| 4316 | { |
| 4317 | struct perf_event *child; |
| 4318 | |
| 4319 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4320 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4321 | mutex_lock(&event->child_mutex); |
| 4322 | func(event); |
| 4323 | list_for_each_entry(child, &event->child_list, child_list) |
| 4324 | func(child); |
| 4325 | mutex_unlock(&event->child_mutex); |
| 4326 | } |
| 4327 | |
| 4328 | static void perf_event_for_each(struct perf_event *event, |
| 4329 | void (*func)(struct perf_event *)) |
| 4330 | { |
| 4331 | struct perf_event_context *ctx = event->ctx; |
| 4332 | struct perf_event *sibling; |
| 4333 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4334 | lockdep_assert_held(&ctx->mutex); |
| 4335 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4336 | event = event->group_leader; |
| 4337 | |
| 4338 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4339 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 4340 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4341 | } |
| 4342 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4343 | static void __perf_event_period(struct perf_event *event, |
| 4344 | struct perf_cpu_context *cpuctx, |
| 4345 | struct perf_event_context *ctx, |
| 4346 | void *info) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 4347 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4348 | u64 value = *((u64 *)info); |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4349 | bool active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4350 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4351 | if (event->attr.freq) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4352 | event->attr.sample_freq = value; |
| 4353 | } else { |
| 4354 | event->attr.sample_period = value; |
| 4355 | event->hw.sample_period = value; |
| 4356 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 4357 | |
| 4358 | active = (event->state == PERF_EVENT_STATE_ACTIVE); |
| 4359 | if (active) { |
| 4360 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | 1e02cd4 | 2016-03-10 15:39:24 +0100 | [diff] [blame] | 4361 | /* |
| 4362 | * We could be throttled; unthrottle now to avoid the tick |
| 4363 | * trying to unthrottle while we already re-started the event. |
| 4364 | */ |
| 4365 | if (event->hw.interrupts == MAX_INTERRUPTS) { |
| 4366 | event->hw.interrupts = 0; |
| 4367 | perf_log_throttle(event, 1); |
| 4368 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 4369 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 4370 | } |
| 4371 | |
| 4372 | local64_set(&event->hw.period_left, 0); |
| 4373 | |
| 4374 | if (active) { |
| 4375 | event->pmu->start(event, PERF_EF_RELOAD); |
| 4376 | perf_pmu_enable(ctx->pmu); |
| 4377 | } |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 4381 | { |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4382 | u64 value; |
| 4383 | |
| 4384 | if (!is_sampling_event(event)) |
| 4385 | return -EINVAL; |
| 4386 | |
| 4387 | if (copy_from_user(&value, arg, sizeof(value))) |
| 4388 | return -EFAULT; |
| 4389 | |
| 4390 | if (!value) |
| 4391 | return -EINVAL; |
| 4392 | |
| 4393 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) |
| 4394 | return -EINVAL; |
| 4395 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4396 | event_function_call(event, __perf_event_period, &value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4397 | |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4398 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4399 | } |
| 4400 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4401 | static const struct file_operations perf_fops; |
| 4402 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4403 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4404 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4405 | struct fd f = fdget(fd); |
| 4406 | if (!f.file) |
| 4407 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4408 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4409 | if (f.file->f_op != &perf_fops) { |
| 4410 | fdput(f); |
| 4411 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4412 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4413 | *p = f; |
| 4414 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | static int perf_event_set_output(struct perf_event *event, |
| 4418 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4419 | 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] | 4420 | 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] | 4421 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4422 | 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] | 4423 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4424 | void (*func)(struct perf_event *); |
| 4425 | u32 flags = arg; |
| 4426 | |
| 4427 | switch (cmd) { |
| 4428 | case PERF_EVENT_IOC_ENABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4429 | func = _perf_event_enable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4430 | break; |
| 4431 | case PERF_EVENT_IOC_DISABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4432 | func = _perf_event_disable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4433 | break; |
| 4434 | case PERF_EVENT_IOC_RESET: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4435 | func = _perf_event_reset; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4436 | break; |
| 4437 | |
| 4438 | case PERF_EVENT_IOC_REFRESH: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4439 | return _perf_event_refresh(event, arg); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4440 | |
| 4441 | case PERF_EVENT_IOC_PERIOD: |
| 4442 | return perf_event_period(event, (u64 __user *)arg); |
| 4443 | |
Jiri Olsa | cf4957f | 2012-10-24 13:37:58 +0200 | [diff] [blame] | 4444 | case PERF_EVENT_IOC_ID: |
| 4445 | { |
| 4446 | u64 id = primary_event_id(event); |
| 4447 | |
| 4448 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) |
| 4449 | return -EFAULT; |
| 4450 | return 0; |
| 4451 | } |
| 4452 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4453 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4454 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4455 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4456 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4457 | struct perf_event *output_event; |
| 4458 | struct fd output; |
| 4459 | ret = perf_fget_light(arg, &output); |
| 4460 | if (ret) |
| 4461 | return ret; |
| 4462 | output_event = output.file->private_data; |
| 4463 | ret = perf_event_set_output(event, output_event); |
| 4464 | fdput(output); |
| 4465 | } else { |
| 4466 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4467 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4468 | return ret; |
| 4469 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4470 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4471 | case PERF_EVENT_IOC_SET_FILTER: |
| 4472 | return perf_event_set_filter(event, (void __user *)arg); |
| 4473 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 4474 | case PERF_EVENT_IOC_SET_BPF: |
| 4475 | return perf_event_set_bpf_prog(event, arg); |
| 4476 | |
Wang Nan | 86e7972 | 2016-03-28 06:41:29 +0000 | [diff] [blame] | 4477 | case PERF_EVENT_IOC_PAUSE_OUTPUT: { |
| 4478 | struct ring_buffer *rb; |
| 4479 | |
| 4480 | rcu_read_lock(); |
| 4481 | rb = rcu_dereference(event->rb); |
| 4482 | if (!rb || !rb->nr_pages) { |
| 4483 | rcu_read_unlock(); |
| 4484 | return -EINVAL; |
| 4485 | } |
| 4486 | rb_toggle_paused(rb, !!arg); |
| 4487 | rcu_read_unlock(); |
| 4488 | return 0; |
| 4489 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4490 | default: |
| 4491 | return -ENOTTY; |
| 4492 | } |
| 4493 | |
| 4494 | if (flags & PERF_IOC_FLAG_GROUP) |
| 4495 | perf_event_for_each(event, func); |
| 4496 | else |
| 4497 | perf_event_for_each_child(event, func); |
| 4498 | |
| 4499 | return 0; |
| 4500 | } |
| 4501 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4502 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 4503 | { |
| 4504 | struct perf_event *event = file->private_data; |
| 4505 | struct perf_event_context *ctx; |
| 4506 | long ret; |
| 4507 | |
| 4508 | ctx = perf_event_ctx_lock(event); |
| 4509 | ret = _perf_ioctl(event, cmd, arg); |
| 4510 | perf_event_ctx_unlock(event, ctx); |
| 4511 | |
| 4512 | return ret; |
| 4513 | } |
| 4514 | |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4515 | #ifdef CONFIG_COMPAT |
| 4516 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, |
| 4517 | unsigned long arg) |
| 4518 | { |
| 4519 | switch (_IOC_NR(cmd)) { |
| 4520 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): |
| 4521 | case _IOC_NR(PERF_EVENT_IOC_ID): |
| 4522 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
| 4523 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { |
| 4524 | cmd &= ~IOCSIZE_MASK; |
| 4525 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; |
| 4526 | } |
| 4527 | break; |
| 4528 | } |
| 4529 | return perf_ioctl(file, cmd, arg); |
| 4530 | } |
| 4531 | #else |
| 4532 | # define perf_compat_ioctl NULL |
| 4533 | #endif |
| 4534 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4535 | int perf_event_task_enable(void) |
| 4536 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4537 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4538 | struct perf_event *event; |
| 4539 | |
| 4540 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4541 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4542 | ctx = perf_event_ctx_lock(event); |
| 4543 | perf_event_for_each_child(event, _perf_event_enable); |
| 4544 | perf_event_ctx_unlock(event, ctx); |
| 4545 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4546 | mutex_unlock(¤t->perf_event_mutex); |
| 4547 | |
| 4548 | return 0; |
| 4549 | } |
| 4550 | |
| 4551 | int perf_event_task_disable(void) |
| 4552 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4553 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4554 | struct perf_event *event; |
| 4555 | |
| 4556 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4557 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4558 | ctx = perf_event_ctx_lock(event); |
| 4559 | perf_event_for_each_child(event, _perf_event_disable); |
| 4560 | perf_event_ctx_unlock(event, ctx); |
| 4561 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4562 | mutex_unlock(¤t->perf_event_mutex); |
| 4563 | |
| 4564 | return 0; |
| 4565 | } |
| 4566 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4567 | static int perf_event_index(struct perf_event *event) |
| 4568 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 4569 | if (event->hw.state & PERF_HES_STOPPED) |
| 4570 | return 0; |
| 4571 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4572 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 4573 | return 0; |
| 4574 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 4575 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4576 | } |
| 4577 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4578 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4579 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4580 | u64 *enabled, |
| 4581 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4582 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4583 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4584 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4585 | *now = perf_clock(); |
| 4586 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4587 | *enabled = ctx_time - event->tstamp_enabled; |
| 4588 | *running = ctx_time - event->tstamp_running; |
| 4589 | } |
| 4590 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4591 | static void perf_event_init_userpage(struct perf_event *event) |
| 4592 | { |
| 4593 | struct perf_event_mmap_page *userpg; |
| 4594 | struct ring_buffer *rb; |
| 4595 | |
| 4596 | rcu_read_lock(); |
| 4597 | rb = rcu_dereference(event->rb); |
| 4598 | if (!rb) |
| 4599 | goto unlock; |
| 4600 | |
| 4601 | userpg = rb->user_page; |
| 4602 | |
| 4603 | /* Allow new userspace to detect that bit 0 is deprecated */ |
| 4604 | userpg->cap_bit0_is_deprecated = 1; |
| 4605 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); |
Alexander Shishkin | e8c6dea | 2015-01-14 14:18:10 +0200 | [diff] [blame] | 4606 | userpg->data_offset = PAGE_SIZE; |
| 4607 | userpg->data_size = perf_data_size(rb); |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4608 | |
| 4609 | unlock: |
| 4610 | rcu_read_unlock(); |
| 4611 | } |
| 4612 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4613 | void __weak arch_perf_update_userpage( |
| 4614 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4615 | { |
| 4616 | } |
| 4617 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4618 | /* |
| 4619 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 4620 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 4621 | * code calls this from NMI context. |
| 4622 | */ |
| 4623 | void perf_event_update_userpage(struct perf_event *event) |
| 4624 | { |
| 4625 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4626 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4627 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4628 | |
| 4629 | rcu_read_lock(); |
Peter Zijlstra | 5ec4c59 | 2013-08-02 21:16:30 +0200 | [diff] [blame] | 4630 | rb = rcu_dereference(event->rb); |
| 4631 | if (!rb) |
| 4632 | goto unlock; |
| 4633 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4634 | /* |
| 4635 | * compute total_time_enabled, total_time_running |
| 4636 | * based on snapshot values taken when the event |
| 4637 | * was last scheduled in. |
| 4638 | * |
| 4639 | * we cannot simply called update_context_time() |
| 4640 | * because of locking issue as we can be called in |
| 4641 | * NMI context |
| 4642 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4643 | calc_timer_values(event, &now, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4644 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4645 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4646 | /* |
| 4647 | * Disable preemption so as to not let the corresponding user-space |
| 4648 | * spin too long if we get preempted. |
| 4649 | */ |
| 4650 | preempt_disable(); |
| 4651 | ++userpg->lock; |
| 4652 | barrier(); |
| 4653 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4654 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 4655 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4656 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4657 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4658 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4659 | atomic64_read(&event->child_total_time_enabled); |
| 4660 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4661 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4662 | atomic64_read(&event->child_total_time_running); |
| 4663 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4664 | arch_perf_update_userpage(event, userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4665 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4666 | barrier(); |
| 4667 | ++userpg->lock; |
| 4668 | preempt_enable(); |
| 4669 | unlock: |
| 4670 | rcu_read_unlock(); |
| 4671 | } |
| 4672 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4673 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 4674 | { |
| 4675 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4676 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4677 | int ret = VM_FAULT_SIGBUS; |
| 4678 | |
| 4679 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 4680 | if (vmf->pgoff == 0) |
| 4681 | ret = 0; |
| 4682 | return ret; |
| 4683 | } |
| 4684 | |
| 4685 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4686 | rb = rcu_dereference(event->rb); |
| 4687 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4688 | goto unlock; |
| 4689 | |
| 4690 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 4691 | goto unlock; |
| 4692 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4693 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4694 | if (!vmf->page) |
| 4695 | goto unlock; |
| 4696 | |
| 4697 | get_page(vmf->page); |
| 4698 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 4699 | vmf->page->index = vmf->pgoff; |
| 4700 | |
| 4701 | ret = 0; |
| 4702 | unlock: |
| 4703 | rcu_read_unlock(); |
| 4704 | |
| 4705 | return ret; |
| 4706 | } |
| 4707 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4708 | static void ring_buffer_attach(struct perf_event *event, |
| 4709 | struct ring_buffer *rb) |
| 4710 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4711 | struct ring_buffer *old_rb = NULL; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4712 | unsigned long flags; |
| 4713 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4714 | if (event->rb) { |
| 4715 | /* |
| 4716 | * Should be impossible, we set this when removing |
| 4717 | * event->rb_entry and wait/clear when adding event->rb_entry. |
| 4718 | */ |
| 4719 | WARN_ON_ONCE(event->rcu_pending); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4720 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4721 | old_rb = event->rb; |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4722 | spin_lock_irqsave(&old_rb->event_lock, flags); |
| 4723 | list_del_rcu(&event->rb_entry); |
| 4724 | spin_unlock_irqrestore(&old_rb->event_lock, flags); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4725 | |
Oleg Nesterov | 2f993cf | 2015-05-30 22:04:25 +0200 | [diff] [blame] | 4726 | event->rcu_batches = get_state_synchronize_rcu(); |
| 4727 | event->rcu_pending = 1; |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4728 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4729 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4730 | if (rb) { |
Oleg Nesterov | 2f993cf | 2015-05-30 22:04:25 +0200 | [diff] [blame] | 4731 | if (event->rcu_pending) { |
| 4732 | cond_synchronize_rcu(event->rcu_batches); |
| 4733 | event->rcu_pending = 0; |
| 4734 | } |
| 4735 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4736 | spin_lock_irqsave(&rb->event_lock, flags); |
| 4737 | list_add_rcu(&event->rb_entry, &rb->event_list); |
| 4738 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 4739 | } |
| 4740 | |
| 4741 | rcu_assign_pointer(event->rb, rb); |
| 4742 | |
| 4743 | if (old_rb) { |
| 4744 | ring_buffer_put(old_rb); |
| 4745 | /* |
| 4746 | * Since we detached before setting the new rb, so that we |
| 4747 | * could attach the new rb, we could have missed a wakeup. |
| 4748 | * Provide it now. |
| 4749 | */ |
| 4750 | wake_up_all(&event->waitq); |
| 4751 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | static void ring_buffer_wakeup(struct perf_event *event) |
| 4755 | { |
| 4756 | struct ring_buffer *rb; |
| 4757 | |
| 4758 | rcu_read_lock(); |
| 4759 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4760 | if (rb) { |
| 4761 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 4762 | wake_up_all(&event->waitq); |
| 4763 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4764 | rcu_read_unlock(); |
| 4765 | } |
| 4766 | |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 4767 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4768 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4769 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4770 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4771 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4772 | rb = rcu_dereference(event->rb); |
| 4773 | if (rb) { |
| 4774 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 4775 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4776 | } |
| 4777 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4778 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4779 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4780 | } |
| 4781 | |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 4782 | void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4783 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4784 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4785 | return; |
| 4786 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4787 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4788 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4789 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 4793 | { |
| 4794 | struct perf_event *event = vma->vm_file->private_data; |
| 4795 | |
| 4796 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4797 | atomic_inc(&event->rb->mmap_count); |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4798 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4799 | if (vma->vm_pgoff) |
| 4800 | atomic_inc(&event->rb->aux_mmap_count); |
| 4801 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4802 | if (event->pmu->event_mapped) |
| 4803 | event->pmu->event_mapped(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4804 | } |
| 4805 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 4806 | static void perf_pmu_output_stop(struct perf_event *event); |
| 4807 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4808 | /* |
| 4809 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 4810 | * event, or through other events by use of perf_event_set_output(). |
| 4811 | * |
| 4812 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 4813 | * the buffer here, where we still have a VM context. This means we need |
| 4814 | * to detach all events redirecting to us. |
| 4815 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4816 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 4817 | { |
| 4818 | struct perf_event *event = vma->vm_file->private_data; |
| 4819 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4820 | struct ring_buffer *rb = ring_buffer_get(event); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4821 | struct user_struct *mmap_user = rb->mmap_user; |
| 4822 | int mmap_locked = rb->mmap_locked; |
| 4823 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4824 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4825 | if (event->pmu->event_unmapped) |
| 4826 | event->pmu->event_unmapped(event); |
| 4827 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4828 | /* |
| 4829 | * rb->aux_mmap_count will always drop before rb->mmap_count and |
| 4830 | * event->mmap_count, so it is ok to use event->mmap_mutex to |
| 4831 | * serialize with perf_mmap here. |
| 4832 | */ |
| 4833 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && |
| 4834 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 4835 | /* |
| 4836 | * Stop all AUX events that are writing to this buffer, |
| 4837 | * so that we can free its AUX pages and corresponding PMU |
| 4838 | * data. Note that after rb::aux_mmap_count dropped to zero, |
| 4839 | * they won't start any more (see perf_aux_output_begin()). |
| 4840 | */ |
| 4841 | perf_pmu_output_stop(event); |
| 4842 | |
| 4843 | /* now it's safe to free the pages */ |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4844 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
| 4845 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; |
| 4846 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 4847 | /* this has to be the last one */ |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4848 | rb_free_aux(rb); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 4849 | WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); |
| 4850 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4851 | mutex_unlock(&event->mmap_mutex); |
| 4852 | } |
| 4853 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4854 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4855 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4856 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4857 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4858 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4859 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4860 | mutex_unlock(&event->mmap_mutex); |
| 4861 | |
| 4862 | /* 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] | 4863 | if (atomic_read(&rb->mmap_count)) |
| 4864 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4865 | |
| 4866 | /* |
| 4867 | * No other mmap()s, detach from all other events that might redirect |
| 4868 | * into the now unreachable buffer. Somewhat complicated by the |
| 4869 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 4870 | */ |
| 4871 | again: |
| 4872 | rcu_read_lock(); |
| 4873 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 4874 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 4875 | /* |
| 4876 | * This event is en-route to free_event() which will |
| 4877 | * detach it and remove it from the list. |
| 4878 | */ |
| 4879 | continue; |
| 4880 | } |
| 4881 | rcu_read_unlock(); |
| 4882 | |
| 4883 | mutex_lock(&event->mmap_mutex); |
| 4884 | /* |
| 4885 | * Check we didn't race with perf_event_set_output() which can |
| 4886 | * swizzle the rb from under us while we were waiting to |
| 4887 | * acquire mmap_mutex. |
| 4888 | * |
| 4889 | * If we find a different rb; ignore this event, a next |
| 4890 | * iteration will no longer find it on the list. We have to |
| 4891 | * still restart the iteration to make sure we're not now |
| 4892 | * iterating the wrong list. |
| 4893 | */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4894 | if (event->rb == rb) |
| 4895 | ring_buffer_attach(event, NULL); |
| 4896 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4897 | mutex_unlock(&event->mmap_mutex); |
| 4898 | put_event(event); |
| 4899 | |
| 4900 | /* |
| 4901 | * Restart the iteration; either we're on the wrong list or |
| 4902 | * destroyed its integrity by doing a deletion. |
| 4903 | */ |
| 4904 | goto again; |
| 4905 | } |
| 4906 | rcu_read_unlock(); |
| 4907 | |
| 4908 | /* |
| 4909 | * It could be there's still a few 0-ref events on the list; they'll |
| 4910 | * get cleaned up by free_event() -- they'll also still have their |
| 4911 | * ref on the rb and will free it whenever they are done with it. |
| 4912 | * |
| 4913 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 4914 | * undo the VM accounting. |
| 4915 | */ |
| 4916 | |
| 4917 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 4918 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 4919 | free_uid(mmap_user); |
| 4920 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4921 | out_put: |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4922 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4923 | } |
| 4924 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 4925 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4926 | .open = perf_mmap_open, |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4927 | .close = perf_mmap_close, /* non mergable */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4928 | .fault = perf_mmap_fault, |
| 4929 | .page_mkwrite = perf_mmap_fault, |
| 4930 | }; |
| 4931 | |
| 4932 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 4933 | { |
| 4934 | struct perf_event *event = file->private_data; |
| 4935 | unsigned long user_locked, user_lock_limit; |
| 4936 | struct user_struct *user = current_user(); |
| 4937 | unsigned long locked, lock_limit; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4938 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4939 | unsigned long vma_size; |
| 4940 | unsigned long nr_pages; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4941 | long user_extra = 0, extra = 0; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4942 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4943 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4944 | /* |
| 4945 | * Don't allow mmap() of inherited per-task counters. This would |
| 4946 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4947 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4948 | */ |
| 4949 | if (event->cpu == -1 && event->attr.inherit) |
| 4950 | return -EINVAL; |
| 4951 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4952 | if (!(vma->vm_flags & VM_SHARED)) |
| 4953 | return -EINVAL; |
| 4954 | |
| 4955 | vma_size = vma->vm_end - vma->vm_start; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4956 | |
| 4957 | if (vma->vm_pgoff == 0) { |
| 4958 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 4959 | } else { |
| 4960 | /* |
| 4961 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already |
| 4962 | * mapped, all subsequent mappings should have the same size |
| 4963 | * and offset. Must be above the normal perf buffer. |
| 4964 | */ |
| 4965 | u64 aux_offset, aux_size; |
| 4966 | |
| 4967 | if (!event->rb) |
| 4968 | return -EINVAL; |
| 4969 | |
| 4970 | nr_pages = vma_size / PAGE_SIZE; |
| 4971 | |
| 4972 | mutex_lock(&event->mmap_mutex); |
| 4973 | ret = -EINVAL; |
| 4974 | |
| 4975 | rb = event->rb; |
| 4976 | if (!rb) |
| 4977 | goto aux_unlock; |
| 4978 | |
| 4979 | aux_offset = ACCESS_ONCE(rb->user_page->aux_offset); |
| 4980 | aux_size = ACCESS_ONCE(rb->user_page->aux_size); |
| 4981 | |
| 4982 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) |
| 4983 | goto aux_unlock; |
| 4984 | |
| 4985 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) |
| 4986 | goto aux_unlock; |
| 4987 | |
| 4988 | /* already mapped with a different offset */ |
| 4989 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) |
| 4990 | goto aux_unlock; |
| 4991 | |
| 4992 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) |
| 4993 | goto aux_unlock; |
| 4994 | |
| 4995 | /* already mapped with a different size */ |
| 4996 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) |
| 4997 | goto aux_unlock; |
| 4998 | |
| 4999 | if (!is_power_of_2(nr_pages)) |
| 5000 | goto aux_unlock; |
| 5001 | |
| 5002 | if (!atomic_inc_not_zero(&rb->mmap_count)) |
| 5003 | goto aux_unlock; |
| 5004 | |
| 5005 | if (rb_has_aux(rb)) { |
| 5006 | atomic_inc(&rb->aux_mmap_count); |
| 5007 | ret = 0; |
| 5008 | goto unlock; |
| 5009 | } |
| 5010 | |
| 5011 | atomic_set(&rb->aux_mmap_count, 1); |
| 5012 | user_extra = nr_pages; |
| 5013 | |
| 5014 | goto accounting; |
| 5015 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5016 | |
| 5017 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5018 | * 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] | 5019 | * can do bitmasks instead of modulo. |
| 5020 | */ |
Kan Liang | 2ed1131 | 2015-03-02 02:14:26 -0500 | [diff] [blame] | 5021 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5022 | return -EINVAL; |
| 5023 | |
| 5024 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 5025 | return -EINVAL; |
| 5026 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5027 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5028 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5029 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5030 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5031 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5032 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5033 | goto unlock; |
| 5034 | } |
| 5035 | |
| 5036 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 5037 | /* |
| 5038 | * Raced against perf_mmap_close() through |
| 5039 | * perf_event_set_output(). Try again, hope for better |
| 5040 | * luck. |
| 5041 | */ |
| 5042 | mutex_unlock(&event->mmap_mutex); |
| 5043 | goto again; |
| 5044 | } |
| 5045 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5046 | goto unlock; |
| 5047 | } |
| 5048 | |
| 5049 | user_extra = nr_pages + 1; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5050 | |
| 5051 | accounting: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5052 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 5053 | |
| 5054 | /* |
| 5055 | * Increase the limit linearly with more CPUs: |
| 5056 | */ |
| 5057 | user_lock_limit *= num_online_cpus(); |
| 5058 | |
| 5059 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 5060 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5061 | if (user_locked > user_lock_limit) |
| 5062 | extra = user_locked - user_lock_limit; |
| 5063 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 5064 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5065 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 5066 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5067 | |
| 5068 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 5069 | !capable(CAP_IPC_LOCK)) { |
| 5070 | ret = -EPERM; |
| 5071 | goto unlock; |
| 5072 | } |
| 5073 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5074 | WARN_ON(!rb && event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 5075 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 5076 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5077 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 5078 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5079 | if (!rb) { |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5080 | rb = rb_alloc(nr_pages, |
| 5081 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 5082 | event->cpu, flags); |
| 5083 | |
| 5084 | if (!rb) { |
| 5085 | ret = -ENOMEM; |
| 5086 | goto unlock; |
| 5087 | } |
| 5088 | |
| 5089 | atomic_set(&rb->mmap_count, 1); |
| 5090 | rb->mmap_user = get_current_user(); |
| 5091 | rb->mmap_locked = extra; |
| 5092 | |
| 5093 | ring_buffer_attach(event, rb); |
| 5094 | |
| 5095 | perf_event_init_userpage(event); |
| 5096 | perf_event_update_userpage(event); |
| 5097 | } else { |
Alexander Shishkin | 1a59413 | 2015-01-14 14:18:18 +0200 | [diff] [blame] | 5098 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
| 5099 | event->attr.aux_watermark, flags); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5100 | if (!ret) |
| 5101 | rb->aux_mmap_locked = extra; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5102 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 5103 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5104 | unlock: |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5105 | if (!ret) { |
| 5106 | atomic_long_add(user_extra, &user->locked_vm); |
| 5107 | vma->vm_mm->pinned_vm += extra; |
| 5108 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5109 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 5110 | } else if (rb) { |
| 5111 | atomic_dec(&rb->mmap_count); |
| 5112 | } |
| 5113 | aux_unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5114 | mutex_unlock(&event->mmap_mutex); |
| 5115 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 5116 | /* |
| 5117 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 5118 | * vma. |
| 5119 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 5120 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5121 | vma->vm_ops = &perf_mmap_vmops; |
| 5122 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 5123 | if (event->pmu->event_mapped) |
| 5124 | event->pmu->event_mapped(event); |
| 5125 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5126 | return ret; |
| 5127 | } |
| 5128 | |
| 5129 | static int perf_fasync(int fd, struct file *filp, int on) |
| 5130 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5131 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5132 | struct perf_event *event = filp->private_data; |
| 5133 | int retval; |
| 5134 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5135 | inode_lock(inode); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5136 | retval = fasync_helper(fd, filp, on, &event->fasync); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5137 | inode_unlock(inode); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5138 | |
| 5139 | if (retval < 0) |
| 5140 | return retval; |
| 5141 | |
| 5142 | return 0; |
| 5143 | } |
| 5144 | |
| 5145 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 5146 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5147 | .release = perf_release, |
| 5148 | .read = perf_read, |
| 5149 | .poll = perf_poll, |
| 5150 | .unlocked_ioctl = perf_ioctl, |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 5151 | .compat_ioctl = perf_compat_ioctl, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5152 | .mmap = perf_mmap, |
| 5153 | .fasync = perf_fasync, |
| 5154 | }; |
| 5155 | |
| 5156 | /* |
| 5157 | * Perf event wakeup |
| 5158 | * |
| 5159 | * If there's data, ensure we set the poll() state and publish everything |
| 5160 | * to user-space before waking everybody up. |
| 5161 | */ |
| 5162 | |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 5163 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
| 5164 | { |
| 5165 | /* only the parent has fasync state */ |
| 5166 | if (event->parent) |
| 5167 | event = event->parent; |
| 5168 | return &event->fasync; |
| 5169 | } |
| 5170 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5171 | void perf_event_wakeup(struct perf_event *event) |
| 5172 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 5173 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5174 | |
| 5175 | if (event->pending_kill) { |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 5176 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5177 | event->pending_kill = 0; |
| 5178 | } |
| 5179 | } |
| 5180 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 5181 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5182 | { |
| 5183 | struct perf_event *event = container_of(entry, |
| 5184 | struct perf_event, pending); |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 5185 | int rctx; |
| 5186 | |
| 5187 | rctx = perf_swevent_get_recursion_context(); |
| 5188 | /* |
| 5189 | * If we 'fail' here, that's OK, it means recursion is already disabled |
| 5190 | * and we won't recurse 'further'. |
| 5191 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5192 | |
| 5193 | if (event->pending_disable) { |
| 5194 | event->pending_disable = 0; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 5195 | perf_event_disable_local(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5196 | } |
| 5197 | |
| 5198 | if (event->pending_wakeup) { |
| 5199 | event->pending_wakeup = 0; |
| 5200 | perf_event_wakeup(event); |
| 5201 | } |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 5202 | |
| 5203 | if (rctx >= 0) |
| 5204 | perf_swevent_put_recursion_context(rctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5205 | } |
| 5206 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5207 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5208 | * We assume there is only KVM supporting the callbacks. |
| 5209 | * Later on, we might change it to a list if there is |
| 5210 | * another virtualization implementation supporting the callbacks. |
| 5211 | */ |
| 5212 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 5213 | |
| 5214 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 5215 | { |
| 5216 | perf_guest_cbs = cbs; |
| 5217 | return 0; |
| 5218 | } |
| 5219 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 5220 | |
| 5221 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 5222 | { |
| 5223 | perf_guest_cbs = NULL; |
| 5224 | return 0; |
| 5225 | } |
| 5226 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 5227 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5228 | static void |
| 5229 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 5230 | struct pt_regs *regs, u64 mask) |
| 5231 | { |
| 5232 | int bit; |
| 5233 | |
| 5234 | for_each_set_bit(bit, (const unsigned long *) &mask, |
| 5235 | sizeof(mask) * BITS_PER_BYTE) { |
| 5236 | u64 val; |
| 5237 | |
| 5238 | val = perf_reg_value(regs, bit); |
| 5239 | perf_output_put(handle, val); |
| 5240 | } |
| 5241 | } |
| 5242 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5243 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5244 | struct pt_regs *regs, |
| 5245 | struct pt_regs *regs_user_copy) |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5246 | { |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5247 | if (user_mode(regs)) { |
| 5248 | regs_user->abi = perf_reg_abi(current); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5249 | regs_user->regs = regs; |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5250 | } else if (current->mm) { |
| 5251 | perf_get_regs_user(regs_user, regs, regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5252 | } else { |
| 5253 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; |
| 5254 | regs_user->regs = NULL; |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5255 | } |
| 5256 | } |
| 5257 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5258 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
| 5259 | struct pt_regs *regs) |
| 5260 | { |
| 5261 | regs_intr->regs = regs; |
| 5262 | regs_intr->abi = perf_reg_abi(current); |
| 5263 | } |
| 5264 | |
| 5265 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5266 | /* |
| 5267 | * Get remaining task size from user stack pointer. |
| 5268 | * |
| 5269 | * It'd be better to take stack vma map and limit this more |
| 5270 | * precisly, but there's no way to get it safely under interrupt, |
| 5271 | * so using TASK_SIZE as limit. |
| 5272 | */ |
| 5273 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 5274 | { |
| 5275 | unsigned long addr = perf_user_stack_pointer(regs); |
| 5276 | |
| 5277 | if (!addr || addr >= TASK_SIZE) |
| 5278 | return 0; |
| 5279 | |
| 5280 | return TASK_SIZE - addr; |
| 5281 | } |
| 5282 | |
| 5283 | static u16 |
| 5284 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 5285 | struct pt_regs *regs) |
| 5286 | { |
| 5287 | u64 task_size; |
| 5288 | |
| 5289 | /* No regs, no stack pointer, no dump. */ |
| 5290 | if (!regs) |
| 5291 | return 0; |
| 5292 | |
| 5293 | /* |
| 5294 | * Check if we fit in with the requested stack size into the: |
| 5295 | * - TASK_SIZE |
| 5296 | * If we don't, we limit the size to the TASK_SIZE. |
| 5297 | * |
| 5298 | * - remaining sample size |
| 5299 | * If we don't, we customize the stack size to |
| 5300 | * fit in to the remaining sample size. |
| 5301 | */ |
| 5302 | |
| 5303 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 5304 | stack_size = min(stack_size, (u16) task_size); |
| 5305 | |
| 5306 | /* Current header size plus static size and dynamic size. */ |
| 5307 | header_size += 2 * sizeof(u64); |
| 5308 | |
| 5309 | /* Do we fit in with the current stack dump size? */ |
| 5310 | if ((u16) (header_size + stack_size) < header_size) { |
| 5311 | /* |
| 5312 | * If we overflow the maximum size for the sample, |
| 5313 | * we customize the stack dump size to fit in. |
| 5314 | */ |
| 5315 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 5316 | stack_size = round_up(stack_size, sizeof(u64)); |
| 5317 | } |
| 5318 | |
| 5319 | return stack_size; |
| 5320 | } |
| 5321 | |
| 5322 | static void |
| 5323 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 5324 | struct pt_regs *regs) |
| 5325 | { |
| 5326 | /* Case of a kernel thread, nothing to dump */ |
| 5327 | if (!regs) { |
| 5328 | u64 size = 0; |
| 5329 | perf_output_put(handle, size); |
| 5330 | } else { |
| 5331 | unsigned long sp; |
| 5332 | unsigned int rem; |
| 5333 | u64 dyn_size; |
| 5334 | |
| 5335 | /* |
| 5336 | * We dump: |
| 5337 | * static size |
| 5338 | * - the size requested by user or the best one we can fit |
| 5339 | * in to the sample max size |
| 5340 | * data |
| 5341 | * - user stack dump data |
| 5342 | * dynamic size |
| 5343 | * - the actual dumped size |
| 5344 | */ |
| 5345 | |
| 5346 | /* Static size. */ |
| 5347 | perf_output_put(handle, dump_size); |
| 5348 | |
| 5349 | /* Data. */ |
| 5350 | sp = perf_user_stack_pointer(regs); |
| 5351 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 5352 | dyn_size = dump_size - rem; |
| 5353 | |
| 5354 | perf_output_skip(handle, rem); |
| 5355 | |
| 5356 | /* Dynamic size. */ |
| 5357 | perf_output_put(handle, dyn_size); |
| 5358 | } |
| 5359 | } |
| 5360 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5361 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 5362 | struct perf_sample_data *data, |
| 5363 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5364 | { |
| 5365 | u64 sample_type = event->attr.sample_type; |
| 5366 | |
| 5367 | data->type = sample_type; |
| 5368 | header->size += event->id_header_size; |
| 5369 | |
| 5370 | if (sample_type & PERF_SAMPLE_TID) { |
| 5371 | /* namespace issues */ |
| 5372 | data->tid_entry.pid = perf_event_pid(event, current); |
| 5373 | data->tid_entry.tid = perf_event_tid(event, current); |
| 5374 | } |
| 5375 | |
| 5376 | if (sample_type & PERF_SAMPLE_TIME) |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 5377 | data->time = perf_event_clock(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5378 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5379 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5380 | data->id = primary_event_id(event); |
| 5381 | |
| 5382 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5383 | data->stream_id = event->id; |
| 5384 | |
| 5385 | if (sample_type & PERF_SAMPLE_CPU) { |
| 5386 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 5387 | data->cpu_entry.reserved = 0; |
| 5388 | } |
| 5389 | } |
| 5390 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5391 | void perf_event_header__init_id(struct perf_event_header *header, |
| 5392 | struct perf_sample_data *data, |
| 5393 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5394 | { |
| 5395 | if (event->attr.sample_id_all) |
| 5396 | __perf_event_header__init_id(header, data, event); |
| 5397 | } |
| 5398 | |
| 5399 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 5400 | struct perf_sample_data *data) |
| 5401 | { |
| 5402 | u64 sample_type = data->type; |
| 5403 | |
| 5404 | if (sample_type & PERF_SAMPLE_TID) |
| 5405 | perf_output_put(handle, data->tid_entry); |
| 5406 | |
| 5407 | if (sample_type & PERF_SAMPLE_TIME) |
| 5408 | perf_output_put(handle, data->time); |
| 5409 | |
| 5410 | if (sample_type & PERF_SAMPLE_ID) |
| 5411 | perf_output_put(handle, data->id); |
| 5412 | |
| 5413 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5414 | perf_output_put(handle, data->stream_id); |
| 5415 | |
| 5416 | if (sample_type & PERF_SAMPLE_CPU) |
| 5417 | perf_output_put(handle, data->cpu_entry); |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5418 | |
| 5419 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 5420 | perf_output_put(handle, data->id); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5421 | } |
| 5422 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5423 | void perf_event__output_id_sample(struct perf_event *event, |
| 5424 | struct perf_output_handle *handle, |
| 5425 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5426 | { |
| 5427 | if (event->attr.sample_id_all) |
| 5428 | __perf_event__output_id_sample(handle, sample); |
| 5429 | } |
| 5430 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5431 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5432 | struct perf_event *event, |
| 5433 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5434 | { |
| 5435 | u64 read_format = event->attr.read_format; |
| 5436 | u64 values[4]; |
| 5437 | int n = 0; |
| 5438 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5439 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5440 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5441 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5442 | atomic64_read(&event->child_total_time_enabled); |
| 5443 | } |
| 5444 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5445 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5446 | atomic64_read(&event->child_total_time_running); |
| 5447 | } |
| 5448 | if (read_format & PERF_FORMAT_ID) |
| 5449 | values[n++] = primary_event_id(event); |
| 5450 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5451 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | /* |
| 5455 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 5456 | */ |
| 5457 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5458 | struct perf_event *event, |
| 5459 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5460 | { |
| 5461 | struct perf_event *leader = event->group_leader, *sub; |
| 5462 | u64 read_format = event->attr.read_format; |
| 5463 | u64 values[5]; |
| 5464 | int n = 0; |
| 5465 | |
| 5466 | values[n++] = 1 + leader->nr_siblings; |
| 5467 | |
| 5468 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5469 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5470 | |
| 5471 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5472 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5473 | |
| 5474 | if (leader != event) |
| 5475 | leader->pmu->read(leader); |
| 5476 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5477 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5478 | if (read_format & PERF_FORMAT_ID) |
| 5479 | values[n++] = primary_event_id(leader); |
| 5480 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5481 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5482 | |
| 5483 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 5484 | n = 0; |
| 5485 | |
Jiri Olsa | 6f5ab00 | 2012-10-15 20:13:45 +0200 | [diff] [blame] | 5486 | if ((sub != event) && |
| 5487 | (sub->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5488 | sub->pmu->read(sub); |
| 5489 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5490 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5491 | if (read_format & PERF_FORMAT_ID) |
| 5492 | values[n++] = primary_event_id(sub); |
| 5493 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5494 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5495 | } |
| 5496 | } |
| 5497 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5498 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 5499 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 5500 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5501 | static void perf_output_read(struct perf_output_handle *handle, |
| 5502 | struct perf_event *event) |
| 5503 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 5504 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5505 | u64 read_format = event->attr.read_format; |
| 5506 | |
| 5507 | /* |
| 5508 | * compute total_time_enabled, total_time_running |
| 5509 | * based on snapshot values taken when the event |
| 5510 | * was last scheduled in. |
| 5511 | * |
| 5512 | * we cannot simply called update_context_time() |
| 5513 | * because of locking issue as we are called in |
| 5514 | * NMI context |
| 5515 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 5516 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 5517 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5518 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5519 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5520 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5521 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5522 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | void perf_output_sample(struct perf_output_handle *handle, |
| 5526 | struct perf_event_header *header, |
| 5527 | struct perf_sample_data *data, |
| 5528 | struct perf_event *event) |
| 5529 | { |
| 5530 | u64 sample_type = data->type; |
| 5531 | |
| 5532 | perf_output_put(handle, *header); |
| 5533 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5534 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 5535 | perf_output_put(handle, data->id); |
| 5536 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5537 | if (sample_type & PERF_SAMPLE_IP) |
| 5538 | perf_output_put(handle, data->ip); |
| 5539 | |
| 5540 | if (sample_type & PERF_SAMPLE_TID) |
| 5541 | perf_output_put(handle, data->tid_entry); |
| 5542 | |
| 5543 | if (sample_type & PERF_SAMPLE_TIME) |
| 5544 | perf_output_put(handle, data->time); |
| 5545 | |
| 5546 | if (sample_type & PERF_SAMPLE_ADDR) |
| 5547 | perf_output_put(handle, data->addr); |
| 5548 | |
| 5549 | if (sample_type & PERF_SAMPLE_ID) |
| 5550 | perf_output_put(handle, data->id); |
| 5551 | |
| 5552 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5553 | perf_output_put(handle, data->stream_id); |
| 5554 | |
| 5555 | if (sample_type & PERF_SAMPLE_CPU) |
| 5556 | perf_output_put(handle, data->cpu_entry); |
| 5557 | |
| 5558 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 5559 | perf_output_put(handle, data->period); |
| 5560 | |
| 5561 | if (sample_type & PERF_SAMPLE_READ) |
| 5562 | perf_output_read(handle, event); |
| 5563 | |
| 5564 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5565 | if (data->callchain) { |
| 5566 | int size = 1; |
| 5567 | |
| 5568 | if (data->callchain) |
| 5569 | size += data->callchain->nr; |
| 5570 | |
| 5571 | size *= sizeof(u64); |
| 5572 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5573 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5574 | } else { |
| 5575 | u64 nr = 0; |
| 5576 | perf_output_put(handle, nr); |
| 5577 | } |
| 5578 | } |
| 5579 | |
| 5580 | if (sample_type & PERF_SAMPLE_RAW) { |
| 5581 | if (data->raw) { |
Alexei Starovoitov | fa128e6 | 2015-10-20 20:02:33 -0700 | [diff] [blame] | 5582 | u32 raw_size = data->raw->size; |
| 5583 | u32 real_size = round_up(raw_size + sizeof(u32), |
| 5584 | sizeof(u64)) - sizeof(u32); |
| 5585 | u64 zero = 0; |
| 5586 | |
| 5587 | perf_output_put(handle, real_size); |
| 5588 | __output_copy(handle, data->raw->data, raw_size); |
| 5589 | if (real_size - raw_size) |
| 5590 | __output_copy(handle, &zero, real_size - raw_size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5591 | } else { |
| 5592 | struct { |
| 5593 | u32 size; |
| 5594 | u32 data; |
| 5595 | } raw = { |
| 5596 | .size = sizeof(u32), |
| 5597 | .data = 0, |
| 5598 | }; |
| 5599 | perf_output_put(handle, raw); |
| 5600 | } |
| 5601 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5602 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5603 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5604 | if (data->br_stack) { |
| 5605 | size_t size; |
| 5606 | |
| 5607 | size = data->br_stack->nr |
| 5608 | * sizeof(struct perf_branch_entry); |
| 5609 | |
| 5610 | perf_output_put(handle, data->br_stack->nr); |
| 5611 | perf_output_copy(handle, data->br_stack->entries, size); |
| 5612 | } else { |
| 5613 | /* |
| 5614 | * we always store at least the value of nr |
| 5615 | */ |
| 5616 | u64 nr = 0; |
| 5617 | perf_output_put(handle, nr); |
| 5618 | } |
| 5619 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5620 | |
| 5621 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5622 | u64 abi = data->regs_user.abi; |
| 5623 | |
| 5624 | /* |
| 5625 | * If there are no regs to dump, notice it through |
| 5626 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5627 | */ |
| 5628 | perf_output_put(handle, abi); |
| 5629 | |
| 5630 | if (abi) { |
| 5631 | u64 mask = event->attr.sample_regs_user; |
| 5632 | perf_output_sample_regs(handle, |
| 5633 | data->regs_user.regs, |
| 5634 | mask); |
| 5635 | } |
| 5636 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5637 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5638 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5639 | perf_output_sample_ustack(handle, |
| 5640 | data->stack_user_size, |
| 5641 | data->regs_user.regs); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5642 | } |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 5643 | |
| 5644 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 5645 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 5646 | |
| 5647 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 5648 | perf_output_put(handle, data->data_src.val); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5649 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 5650 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 5651 | perf_output_put(handle, data->txn); |
| 5652 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5653 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5654 | u64 abi = data->regs_intr.abi; |
| 5655 | /* |
| 5656 | * If there are no regs to dump, notice it through |
| 5657 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5658 | */ |
| 5659 | perf_output_put(handle, abi); |
| 5660 | |
| 5661 | if (abi) { |
| 5662 | u64 mask = event->attr.sample_regs_intr; |
| 5663 | |
| 5664 | perf_output_sample_regs(handle, |
| 5665 | data->regs_intr.regs, |
| 5666 | mask); |
| 5667 | } |
| 5668 | } |
| 5669 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5670 | if (!event->attr.watermark) { |
| 5671 | int wakeup_events = event->attr.wakeup_events; |
| 5672 | |
| 5673 | if (wakeup_events) { |
| 5674 | struct ring_buffer *rb = handle->rb; |
| 5675 | int events = local_inc_return(&rb->events); |
| 5676 | |
| 5677 | if (events >= wakeup_events) { |
| 5678 | local_sub(wakeup_events, &rb->events); |
| 5679 | local_inc(&rb->wakeup); |
| 5680 | } |
| 5681 | } |
| 5682 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5683 | } |
| 5684 | |
| 5685 | void perf_prepare_sample(struct perf_event_header *header, |
| 5686 | struct perf_sample_data *data, |
| 5687 | struct perf_event *event, |
| 5688 | struct pt_regs *regs) |
| 5689 | { |
| 5690 | u64 sample_type = event->attr.sample_type; |
| 5691 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5692 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5693 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5694 | |
| 5695 | header->misc = 0; |
| 5696 | header->misc |= perf_misc_flags(regs); |
| 5697 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5698 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5699 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5700 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5701 | data->ip = perf_instruction_pointer(regs); |
| 5702 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5703 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5704 | int size = 1; |
| 5705 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5706 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5707 | |
| 5708 | if (data->callchain) |
| 5709 | size += data->callchain->nr; |
| 5710 | |
| 5711 | header->size += size * sizeof(u64); |
| 5712 | } |
| 5713 | |
| 5714 | if (sample_type & PERF_SAMPLE_RAW) { |
| 5715 | int size = sizeof(u32); |
| 5716 | |
| 5717 | if (data->raw) |
| 5718 | size += data->raw->size; |
| 5719 | else |
| 5720 | size += sizeof(u32); |
| 5721 | |
Alexei Starovoitov | fa128e6 | 2015-10-20 20:02:33 -0700 | [diff] [blame] | 5722 | header->size += round_up(size, sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5723 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5724 | |
| 5725 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5726 | int size = sizeof(u64); /* nr */ |
| 5727 | if (data->br_stack) { |
| 5728 | size += data->br_stack->nr |
| 5729 | * sizeof(struct perf_branch_entry); |
| 5730 | } |
| 5731 | header->size += size; |
| 5732 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5733 | |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5734 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5735 | perf_sample_regs_user(&data->regs_user, regs, |
| 5736 | &data->regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5737 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5738 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5739 | /* regs dump ABI info */ |
| 5740 | int size = sizeof(u64); |
| 5741 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5742 | if (data->regs_user.regs) { |
| 5743 | u64 mask = event->attr.sample_regs_user; |
| 5744 | size += hweight64(mask) * sizeof(u64); |
| 5745 | } |
| 5746 | |
| 5747 | header->size += size; |
| 5748 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5749 | |
| 5750 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 5751 | /* |
| 5752 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 5753 | * processed as the last one or have additional check added |
| 5754 | * in case new sample type is added, because we could eat |
| 5755 | * up the rest of the sample size. |
| 5756 | */ |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5757 | u16 stack_size = event->attr.sample_stack_user; |
| 5758 | u16 size = sizeof(u64); |
| 5759 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5760 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5761 | data->regs_user.regs); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5762 | |
| 5763 | /* |
| 5764 | * If there is something to dump, add space for the dump |
| 5765 | * itself and for the field that tells the dynamic size, |
| 5766 | * which is how many have been actually dumped. |
| 5767 | */ |
| 5768 | if (stack_size) |
| 5769 | size += sizeof(u64) + stack_size; |
| 5770 | |
| 5771 | data->stack_user_size = stack_size; |
| 5772 | header->size += size; |
| 5773 | } |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5774 | |
| 5775 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5776 | /* regs dump ABI info */ |
| 5777 | int size = sizeof(u64); |
| 5778 | |
| 5779 | perf_sample_regs_intr(&data->regs_intr, regs); |
| 5780 | |
| 5781 | if (data->regs_intr.regs) { |
| 5782 | u64 mask = event->attr.sample_regs_intr; |
| 5783 | |
| 5784 | size += hweight64(mask) * sizeof(u64); |
| 5785 | } |
| 5786 | |
| 5787 | header->size += size; |
| 5788 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5789 | } |
| 5790 | |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 5791 | static void __always_inline |
| 5792 | __perf_event_output(struct perf_event *event, |
| 5793 | struct perf_sample_data *data, |
| 5794 | struct pt_regs *regs, |
| 5795 | int (*output_begin)(struct perf_output_handle *, |
| 5796 | struct perf_event *, |
| 5797 | unsigned int)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5798 | { |
| 5799 | struct perf_output_handle handle; |
| 5800 | struct perf_event_header header; |
| 5801 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5802 | /* protect the callchain buffers */ |
| 5803 | rcu_read_lock(); |
| 5804 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5805 | perf_prepare_sample(&header, data, event, regs); |
| 5806 | |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 5807 | if (output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5808 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5809 | |
| 5810 | perf_output_sample(&handle, &header, data, event); |
| 5811 | |
| 5812 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5813 | |
| 5814 | exit: |
| 5815 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5816 | } |
| 5817 | |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 5818 | void |
| 5819 | perf_event_output_forward(struct perf_event *event, |
| 5820 | struct perf_sample_data *data, |
| 5821 | struct pt_regs *regs) |
| 5822 | { |
| 5823 | __perf_event_output(event, data, regs, perf_output_begin_forward); |
| 5824 | } |
| 5825 | |
| 5826 | void |
| 5827 | perf_event_output_backward(struct perf_event *event, |
| 5828 | struct perf_sample_data *data, |
| 5829 | struct pt_regs *regs) |
| 5830 | { |
| 5831 | __perf_event_output(event, data, regs, perf_output_begin_backward); |
| 5832 | } |
| 5833 | |
| 5834 | void |
| 5835 | perf_event_output(struct perf_event *event, |
| 5836 | struct perf_sample_data *data, |
| 5837 | struct pt_regs *regs) |
| 5838 | { |
| 5839 | __perf_event_output(event, data, regs, perf_output_begin); |
| 5840 | } |
| 5841 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5842 | /* |
| 5843 | * read event_id |
| 5844 | */ |
| 5845 | |
| 5846 | struct perf_read_event { |
| 5847 | struct perf_event_header header; |
| 5848 | |
| 5849 | u32 pid; |
| 5850 | u32 tid; |
| 5851 | }; |
| 5852 | |
| 5853 | static void |
| 5854 | perf_event_read_event(struct perf_event *event, |
| 5855 | struct task_struct *task) |
| 5856 | { |
| 5857 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5858 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5859 | struct perf_read_event read_event = { |
| 5860 | .header = { |
| 5861 | .type = PERF_RECORD_READ, |
| 5862 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5863 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5864 | }, |
| 5865 | .pid = perf_event_pid(event, task), |
| 5866 | .tid = perf_event_tid(event, task), |
| 5867 | }; |
| 5868 | int ret; |
| 5869 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5870 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5871 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5872 | if (ret) |
| 5873 | return; |
| 5874 | |
| 5875 | perf_output_put(&handle, read_event); |
| 5876 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5877 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5878 | |
| 5879 | perf_output_end(&handle); |
| 5880 | } |
| 5881 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5882 | typedef void (perf_iterate_f)(struct perf_event *event, void *data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5883 | |
| 5884 | static void |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5885 | perf_iterate_ctx(struct perf_event_context *ctx, |
| 5886 | perf_iterate_f output, |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 5887 | void *data, bool all) |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5888 | { |
| 5889 | struct perf_event *event; |
| 5890 | |
| 5891 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 5892 | if (!all) { |
| 5893 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 5894 | continue; |
| 5895 | if (!event_filter_match(event)) |
| 5896 | continue; |
| 5897 | } |
| 5898 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5899 | output(event, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5900 | } |
| 5901 | } |
| 5902 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5903 | static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 5904 | { |
| 5905 | struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); |
| 5906 | struct perf_event *event; |
| 5907 | |
| 5908 | list_for_each_entry_rcu(event, &pel->list, sb_list) { |
| 5909 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 5910 | continue; |
| 5911 | if (!event_filter_match(event)) |
| 5912 | continue; |
| 5913 | output(event, data); |
| 5914 | } |
| 5915 | } |
| 5916 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5917 | /* |
| 5918 | * Iterate all events that need to receive side-band events. |
| 5919 | * |
| 5920 | * For new callers; ensure that account_pmu_sb_event() includes |
| 5921 | * your event, otherwise it might not get delivered. |
| 5922 | */ |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 5923 | static void |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5924 | perf_iterate_sb(perf_iterate_f output, void *data, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5925 | struct perf_event_context *task_ctx) |
| 5926 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5927 | struct perf_event_context *ctx; |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5928 | int ctxn; |
| 5929 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5930 | rcu_read_lock(); |
| 5931 | preempt_disable(); |
| 5932 | |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 5933 | /* |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5934 | * If we have task_ctx != NULL we only notify the task context itself. |
| 5935 | * The task_ctx is set only for EXIT events before releasing task |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 5936 | * context. |
| 5937 | */ |
| 5938 | if (task_ctx) { |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5939 | perf_iterate_ctx(task_ctx, output, data, false); |
| 5940 | goto done; |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 5941 | } |
| 5942 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5943 | perf_iterate_sb_cpu(output, data); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 5944 | |
| 5945 | for_each_task_context_nr(ctxn) { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5946 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 5947 | if (ctx) |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5948 | perf_iterate_ctx(ctx, output, data, false); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5949 | } |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 5950 | done: |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 5951 | preempt_enable(); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5952 | rcu_read_unlock(); |
| 5953 | } |
| 5954 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 5955 | /* |
| 5956 | * Clear all file-based filters at exec, they'll have to be |
| 5957 | * re-instated when/if these objects are mmapped again. |
| 5958 | */ |
| 5959 | static void perf_event_addr_filters_exec(struct perf_event *event, void *data) |
| 5960 | { |
| 5961 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 5962 | struct perf_addr_filter *filter; |
| 5963 | unsigned int restart = 0, count = 0; |
| 5964 | unsigned long flags; |
| 5965 | |
| 5966 | if (!has_addr_filter(event)) |
| 5967 | return; |
| 5968 | |
| 5969 | raw_spin_lock_irqsave(&ifh->lock, flags); |
| 5970 | list_for_each_entry(filter, &ifh->list, entry) { |
| 5971 | if (filter->inode) { |
| 5972 | event->addr_filters_offs[count] = 0; |
| 5973 | restart++; |
| 5974 | } |
| 5975 | |
| 5976 | count++; |
| 5977 | } |
| 5978 | |
| 5979 | if (restart) |
| 5980 | event->addr_filters_gen++; |
| 5981 | raw_spin_unlock_irqrestore(&ifh->lock, flags); |
| 5982 | |
| 5983 | if (restart) |
| 5984 | perf_event_restart(event); |
| 5985 | } |
| 5986 | |
| 5987 | void perf_event_exec(void) |
| 5988 | { |
| 5989 | struct perf_event_context *ctx; |
| 5990 | int ctxn; |
| 5991 | |
| 5992 | rcu_read_lock(); |
| 5993 | for_each_task_context_nr(ctxn) { |
| 5994 | ctx = current->perf_event_ctxp[ctxn]; |
| 5995 | if (!ctx) |
| 5996 | continue; |
| 5997 | |
| 5998 | perf_event_enable_on_exec(ctxn); |
| 5999 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 6000 | perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6001 | true); |
| 6002 | } |
| 6003 | rcu_read_unlock(); |
| 6004 | } |
| 6005 | |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6006 | struct remote_output { |
| 6007 | struct ring_buffer *rb; |
| 6008 | int err; |
| 6009 | }; |
| 6010 | |
| 6011 | static void __perf_event_output_stop(struct perf_event *event, void *data) |
| 6012 | { |
| 6013 | struct perf_event *parent = event->parent; |
| 6014 | struct remote_output *ro = data; |
| 6015 | struct ring_buffer *rb = ro->rb; |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6016 | struct stop_event_data sd = { |
| 6017 | .event = event, |
| 6018 | }; |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6019 | |
| 6020 | if (!has_aux(event)) |
| 6021 | return; |
| 6022 | |
| 6023 | if (!parent) |
| 6024 | parent = event; |
| 6025 | |
| 6026 | /* |
| 6027 | * In case of inheritance, it will be the parent that links to the |
| 6028 | * ring-buffer, but it will be the child that's actually using it: |
| 6029 | */ |
| 6030 | if (rcu_dereference(parent->rb) == rb) |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6031 | ro->err = __perf_event_stop(&sd); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6032 | } |
| 6033 | |
| 6034 | static int __perf_pmu_output_stop(void *info) |
| 6035 | { |
| 6036 | struct perf_event *event = info; |
| 6037 | struct pmu *pmu = event->pmu; |
| 6038 | struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 6039 | struct remote_output ro = { |
| 6040 | .rb = event->rb, |
| 6041 | }; |
| 6042 | |
| 6043 | rcu_read_lock(); |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 6044 | perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6045 | if (cpuctx->task_ctx) |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 6046 | perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, |
Alexander Shishkin | b73e4fe | 2016-04-27 18:44:45 +0300 | [diff] [blame] | 6047 | &ro, false); |
Alexander Shishkin | 95ff4ca | 2015-12-02 18:41:11 +0200 | [diff] [blame] | 6048 | rcu_read_unlock(); |
| 6049 | |
| 6050 | return ro.err; |
| 6051 | } |
| 6052 | |
| 6053 | static void perf_pmu_output_stop(struct perf_event *event) |
| 6054 | { |
| 6055 | struct perf_event *iter; |
| 6056 | int err, cpu; |
| 6057 | |
| 6058 | restart: |
| 6059 | rcu_read_lock(); |
| 6060 | list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { |
| 6061 | /* |
| 6062 | * For per-CPU events, we need to make sure that neither they |
| 6063 | * nor their children are running; for cpu==-1 events it's |
| 6064 | * sufficient to stop the event itself if it's active, since |
| 6065 | * it can't have children. |
| 6066 | */ |
| 6067 | cpu = iter->cpu; |
| 6068 | if (cpu == -1) |
| 6069 | cpu = READ_ONCE(iter->oncpu); |
| 6070 | |
| 6071 | if (cpu == -1) |
| 6072 | continue; |
| 6073 | |
| 6074 | err = cpu_function_call(cpu, __perf_pmu_output_stop, event); |
| 6075 | if (err == -EAGAIN) { |
| 6076 | rcu_read_unlock(); |
| 6077 | goto restart; |
| 6078 | } |
| 6079 | } |
| 6080 | rcu_read_unlock(); |
| 6081 | } |
| 6082 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6083 | /* |
| 6084 | * task tracking -- fork/exit |
| 6085 | * |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6086 | * 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] | 6087 | */ |
| 6088 | |
| 6089 | struct perf_task_event { |
| 6090 | struct task_struct *task; |
| 6091 | struct perf_event_context *task_ctx; |
| 6092 | |
| 6093 | struct { |
| 6094 | struct perf_event_header header; |
| 6095 | |
| 6096 | u32 pid; |
| 6097 | u32 ppid; |
| 6098 | u32 tid; |
| 6099 | u32 ptid; |
| 6100 | u64 time; |
| 6101 | } event_id; |
| 6102 | }; |
| 6103 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6104 | static int perf_event_task_match(struct perf_event *event) |
| 6105 | { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6106 | return event->attr.comm || event->attr.mmap || |
| 6107 | event->attr.mmap2 || event->attr.mmap_data || |
| 6108 | event->attr.task; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6109 | } |
| 6110 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6111 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6112 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6113 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6114 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6115 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6116 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6117 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6118 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 6119 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6120 | if (!perf_event_task_match(event)) |
| 6121 | return; |
| 6122 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6123 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6124 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6125 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6126 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 6127 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6128 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6129 | |
| 6130 | task_event->event_id.pid = perf_event_pid(event, task); |
| 6131 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 6132 | |
| 6133 | task_event->event_id.tid = perf_event_tid(event, task); |
| 6134 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 6135 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6136 | task_event->event_id.time = perf_event_clock(event); |
| 6137 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6138 | perf_output_put(&handle, task_event->event_id); |
| 6139 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6140 | perf_event__output_id_sample(event, &handle, &sample); |
| 6141 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6142 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6143 | out: |
| 6144 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6145 | } |
| 6146 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6147 | static void perf_event_task(struct task_struct *task, |
| 6148 | struct perf_event_context *task_ctx, |
| 6149 | int new) |
| 6150 | { |
| 6151 | struct perf_task_event task_event; |
| 6152 | |
| 6153 | if (!atomic_read(&nr_comm_events) && |
| 6154 | !atomic_read(&nr_mmap_events) && |
| 6155 | !atomic_read(&nr_task_events)) |
| 6156 | return; |
| 6157 | |
| 6158 | task_event = (struct perf_task_event){ |
| 6159 | .task = task, |
| 6160 | .task_ctx = task_ctx, |
| 6161 | .event_id = { |
| 6162 | .header = { |
| 6163 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 6164 | .misc = 0, |
| 6165 | .size = sizeof(task_event.event_id), |
| 6166 | }, |
| 6167 | /* .pid */ |
| 6168 | /* .ppid */ |
| 6169 | /* .tid */ |
| 6170 | /* .ptid */ |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6171 | /* .time */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6172 | }, |
| 6173 | }; |
| 6174 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 6175 | perf_iterate_sb(perf_event_task_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6176 | &task_event, |
| 6177 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6178 | } |
| 6179 | |
| 6180 | void perf_event_fork(struct task_struct *task) |
| 6181 | { |
| 6182 | perf_event_task(task, NULL, 1); |
| 6183 | } |
| 6184 | |
| 6185 | /* |
| 6186 | * comm tracking |
| 6187 | */ |
| 6188 | |
| 6189 | struct perf_comm_event { |
| 6190 | struct task_struct *task; |
| 6191 | char *comm; |
| 6192 | int comm_size; |
| 6193 | |
| 6194 | struct { |
| 6195 | struct perf_event_header header; |
| 6196 | |
| 6197 | u32 pid; |
| 6198 | u32 tid; |
| 6199 | } event_id; |
| 6200 | }; |
| 6201 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6202 | static int perf_event_comm_match(struct perf_event *event) |
| 6203 | { |
| 6204 | return event->attr.comm; |
| 6205 | } |
| 6206 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6207 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6208 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6209 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6210 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6211 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6212 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6213 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6214 | int ret; |
| 6215 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6216 | if (!perf_event_comm_match(event)) |
| 6217 | return; |
| 6218 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6219 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 6220 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6221 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6222 | |
| 6223 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6224 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6225 | |
| 6226 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 6227 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 6228 | |
| 6229 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6230 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6231 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6232 | |
| 6233 | perf_event__output_id_sample(event, &handle, &sample); |
| 6234 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6235 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6236 | out: |
| 6237 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6238 | } |
| 6239 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6240 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 6241 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6242 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6243 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6244 | |
| 6245 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 6246 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6247 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 6248 | |
| 6249 | comm_event->comm = comm; |
| 6250 | comm_event->comm_size = size; |
| 6251 | |
| 6252 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 6253 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 6254 | perf_iterate_sb(perf_event_comm_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6255 | comm_event, |
| 6256 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6257 | } |
| 6258 | |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 6259 | void perf_event_comm(struct task_struct *task, bool exec) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6260 | { |
| 6261 | struct perf_comm_event comm_event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6262 | |
| 6263 | if (!atomic_read(&nr_comm_events)) |
| 6264 | return; |
| 6265 | |
| 6266 | comm_event = (struct perf_comm_event){ |
| 6267 | .task = task, |
| 6268 | /* .comm */ |
| 6269 | /* .comm_size */ |
| 6270 | .event_id = { |
| 6271 | .header = { |
| 6272 | .type = PERF_RECORD_COMM, |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 6273 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6274 | /* .size */ |
| 6275 | }, |
| 6276 | /* .pid */ |
| 6277 | /* .tid */ |
| 6278 | }, |
| 6279 | }; |
| 6280 | |
| 6281 | perf_event_comm_event(&comm_event); |
| 6282 | } |
| 6283 | |
| 6284 | /* |
| 6285 | * mmap tracking |
| 6286 | */ |
| 6287 | |
| 6288 | struct perf_mmap_event { |
| 6289 | struct vm_area_struct *vma; |
| 6290 | |
| 6291 | const char *file_name; |
| 6292 | int file_size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6293 | int maj, min; |
| 6294 | u64 ino; |
| 6295 | u64 ino_generation; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6296 | u32 prot, flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6297 | |
| 6298 | struct { |
| 6299 | struct perf_event_header header; |
| 6300 | |
| 6301 | u32 pid; |
| 6302 | u32 tid; |
| 6303 | u64 start; |
| 6304 | u64 len; |
| 6305 | u64 pgoff; |
| 6306 | } event_id; |
| 6307 | }; |
| 6308 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6309 | static int perf_event_mmap_match(struct perf_event *event, |
| 6310 | void *data) |
| 6311 | { |
| 6312 | struct perf_mmap_event *mmap_event = data; |
| 6313 | struct vm_area_struct *vma = mmap_event->vma; |
| 6314 | int executable = vma->vm_flags & VM_EXEC; |
| 6315 | |
| 6316 | return (!executable && event->attr.mmap_data) || |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6317 | (executable && (event->attr.mmap || event->attr.mmap2)); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6318 | } |
| 6319 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6320 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6321 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6322 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6323 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6324 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6325 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6326 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6327 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6328 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6329 | if (!perf_event_mmap_match(event, data)) |
| 6330 | return; |
| 6331 | |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6332 | if (event->attr.mmap2) { |
| 6333 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; |
| 6334 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); |
| 6335 | mmap_event->event_id.header.size += sizeof(mmap_event->min); |
| 6336 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); |
Arnaldo Carvalho de Melo | d008d52 | 2013-09-10 10:24:05 -0300 | [diff] [blame] | 6337 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6338 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
| 6339 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6340 | } |
| 6341 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6342 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 6343 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6344 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6345 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6346 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6347 | |
| 6348 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 6349 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 6350 | |
| 6351 | perf_output_put(&handle, mmap_event->event_id); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6352 | |
| 6353 | if (event->attr.mmap2) { |
| 6354 | perf_output_put(&handle, mmap_event->maj); |
| 6355 | perf_output_put(&handle, mmap_event->min); |
| 6356 | perf_output_put(&handle, mmap_event->ino); |
| 6357 | perf_output_put(&handle, mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6358 | perf_output_put(&handle, mmap_event->prot); |
| 6359 | perf_output_put(&handle, mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6360 | } |
| 6361 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6362 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6363 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6364 | |
| 6365 | perf_event__output_id_sample(event, &handle, &sample); |
| 6366 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6367 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6368 | out: |
| 6369 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6370 | } |
| 6371 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6372 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 6373 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6374 | struct vm_area_struct *vma = mmap_event->vma; |
| 6375 | struct file *file = vma->vm_file; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6376 | int maj = 0, min = 0; |
| 6377 | u64 ino = 0, gen = 0; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6378 | u32 prot = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6379 | unsigned int size; |
| 6380 | char tmp[16]; |
| 6381 | char *buf = NULL; |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6382 | char *name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6383 | |
| 6384 | if (file) { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6385 | struct inode *inode; |
| 6386 | dev_t dev; |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 6387 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6388 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6389 | if (!buf) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6390 | name = "//enomem"; |
| 6391 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6392 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6393 | /* |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 6394 | * d_path() works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6395 | * need to add enough zero bytes after the string to handle |
| 6396 | * the 64bit alignment we do later. |
| 6397 | */ |
Miklos Szeredi | 9bf39ab | 2015-06-19 10:29:13 +0200 | [diff] [blame] | 6398 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6399 | if (IS_ERR(name)) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6400 | name = "//toolong"; |
| 6401 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6402 | } |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6403 | inode = file_inode(vma->vm_file); |
| 6404 | dev = inode->i_sb->s_dev; |
| 6405 | ino = inode->i_ino; |
| 6406 | gen = inode->i_generation; |
| 6407 | maj = MAJOR(dev); |
| 6408 | min = MINOR(dev); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6409 | |
| 6410 | if (vma->vm_flags & VM_READ) |
| 6411 | prot |= PROT_READ; |
| 6412 | if (vma->vm_flags & VM_WRITE) |
| 6413 | prot |= PROT_WRITE; |
| 6414 | if (vma->vm_flags & VM_EXEC) |
| 6415 | prot |= PROT_EXEC; |
| 6416 | |
| 6417 | if (vma->vm_flags & VM_MAYSHARE) |
| 6418 | flags = MAP_SHARED; |
| 6419 | else |
| 6420 | flags = MAP_PRIVATE; |
| 6421 | |
| 6422 | if (vma->vm_flags & VM_DENYWRITE) |
| 6423 | flags |= MAP_DENYWRITE; |
| 6424 | if (vma->vm_flags & VM_MAYEXEC) |
| 6425 | flags |= MAP_EXECUTABLE; |
| 6426 | if (vma->vm_flags & VM_LOCKED) |
| 6427 | flags |= MAP_LOCKED; |
| 6428 | if (vma->vm_flags & VM_HUGETLB) |
| 6429 | flags |= MAP_HUGETLB; |
| 6430 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6431 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6432 | } else { |
Jiri Olsa | fbe26ab | 2014-07-14 17:57:19 +0200 | [diff] [blame] | 6433 | if (vma->vm_ops && vma->vm_ops->name) { |
| 6434 | name = (char *) vma->vm_ops->name(vma); |
| 6435 | if (name) |
| 6436 | goto cpy_name; |
| 6437 | } |
| 6438 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6439 | name = (char *)arch_vma_name(vma); |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6440 | if (name) |
| 6441 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6442 | |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 6443 | if (vma->vm_start <= vma->vm_mm->start_brk && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6444 | vma->vm_end >= vma->vm_mm->brk) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6445 | name = "[heap]"; |
| 6446 | goto cpy_name; |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 6447 | } |
| 6448 | if (vma->vm_start <= vma->vm_mm->start_stack && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6449 | vma->vm_end >= vma->vm_mm->start_stack) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6450 | name = "[stack]"; |
| 6451 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6452 | } |
| 6453 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6454 | name = "//anon"; |
| 6455 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6456 | } |
| 6457 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6458 | cpy_name: |
| 6459 | strlcpy(tmp, name, sizeof(tmp)); |
| 6460 | name = tmp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6461 | got_name: |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6462 | /* |
| 6463 | * Since our buffer works in 8 byte units we need to align our string |
| 6464 | * size to a multiple of 8. However, we must guarantee the tail end is |
| 6465 | * zero'd out to avoid leaking random bits to userspace. |
| 6466 | */ |
| 6467 | size = strlen(name)+1; |
| 6468 | while (!IS_ALIGNED(size, sizeof(u64))) |
| 6469 | name[size++] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6470 | |
| 6471 | mmap_event->file_name = name; |
| 6472 | mmap_event->file_size = size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6473 | mmap_event->maj = maj; |
| 6474 | mmap_event->min = min; |
| 6475 | mmap_event->ino = ino; |
| 6476 | mmap_event->ino_generation = gen; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6477 | mmap_event->prot = prot; |
| 6478 | mmap_event->flags = flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6479 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 6480 | if (!(vma->vm_flags & VM_EXEC)) |
| 6481 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 6482 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6483 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 6484 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 6485 | perf_iterate_sb(perf_event_mmap_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6486 | mmap_event, |
| 6487 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6488 | |
| 6489 | kfree(buf); |
| 6490 | } |
| 6491 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6492 | /* |
| 6493 | * Whether this @filter depends on a dynamic object which is not loaded |
| 6494 | * yet or its load addresses are not known. |
| 6495 | */ |
| 6496 | static bool perf_addr_filter_needs_mmap(struct perf_addr_filter *filter) |
| 6497 | { |
| 6498 | return filter->filter && filter->inode; |
| 6499 | } |
| 6500 | |
| 6501 | /* |
| 6502 | * Check whether inode and address range match filter criteria. |
| 6503 | */ |
| 6504 | static bool perf_addr_filter_match(struct perf_addr_filter *filter, |
| 6505 | struct file *file, unsigned long offset, |
| 6506 | unsigned long size) |
| 6507 | { |
| 6508 | if (filter->inode != file->f_inode) |
| 6509 | return false; |
| 6510 | |
| 6511 | if (filter->offset > offset + size) |
| 6512 | return false; |
| 6513 | |
| 6514 | if (filter->offset + filter->size < offset) |
| 6515 | return false; |
| 6516 | |
| 6517 | return true; |
| 6518 | } |
| 6519 | |
| 6520 | static void __perf_addr_filters_adjust(struct perf_event *event, void *data) |
| 6521 | { |
| 6522 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 6523 | struct vm_area_struct *vma = data; |
| 6524 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags; |
| 6525 | struct file *file = vma->vm_file; |
| 6526 | struct perf_addr_filter *filter; |
| 6527 | unsigned int restart = 0, count = 0; |
| 6528 | |
| 6529 | if (!has_addr_filter(event)) |
| 6530 | return; |
| 6531 | |
| 6532 | if (!file) |
| 6533 | return; |
| 6534 | |
| 6535 | raw_spin_lock_irqsave(&ifh->lock, flags); |
| 6536 | list_for_each_entry(filter, &ifh->list, entry) { |
| 6537 | if (perf_addr_filter_match(filter, file, off, |
| 6538 | vma->vm_end - vma->vm_start)) { |
| 6539 | event->addr_filters_offs[count] = vma->vm_start; |
| 6540 | restart++; |
| 6541 | } |
| 6542 | |
| 6543 | count++; |
| 6544 | } |
| 6545 | |
| 6546 | if (restart) |
| 6547 | event->addr_filters_gen++; |
| 6548 | raw_spin_unlock_irqrestore(&ifh->lock, flags); |
| 6549 | |
| 6550 | if (restart) |
| 6551 | perf_event_restart(event); |
| 6552 | } |
| 6553 | |
| 6554 | /* |
| 6555 | * Adjust all task's events' filters to the new vma |
| 6556 | */ |
| 6557 | static void perf_addr_filters_adjust(struct vm_area_struct *vma) |
| 6558 | { |
| 6559 | struct perf_event_context *ctx; |
| 6560 | int ctxn; |
| 6561 | |
| 6562 | rcu_read_lock(); |
| 6563 | for_each_task_context_nr(ctxn) { |
| 6564 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 6565 | if (!ctx) |
| 6566 | continue; |
| 6567 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 6568 | perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6569 | } |
| 6570 | rcu_read_unlock(); |
| 6571 | } |
| 6572 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 6573 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6574 | { |
| 6575 | struct perf_mmap_event mmap_event; |
| 6576 | |
| 6577 | if (!atomic_read(&nr_mmap_events)) |
| 6578 | return; |
| 6579 | |
| 6580 | mmap_event = (struct perf_mmap_event){ |
| 6581 | .vma = vma, |
| 6582 | /* .file_name */ |
| 6583 | /* .file_size */ |
| 6584 | .event_id = { |
| 6585 | .header = { |
| 6586 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 6587 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6588 | /* .size */ |
| 6589 | }, |
| 6590 | /* .pid */ |
| 6591 | /* .tid */ |
| 6592 | .start = vma->vm_start, |
| 6593 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 6594 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6595 | }, |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6596 | /* .maj (attr_mmap2 only) */ |
| 6597 | /* .min (attr_mmap2 only) */ |
| 6598 | /* .ino (attr_mmap2 only) */ |
| 6599 | /* .ino_generation (attr_mmap2 only) */ |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6600 | /* .prot (attr_mmap2 only) */ |
| 6601 | /* .flags (attr_mmap2 only) */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6602 | }; |
| 6603 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 6604 | perf_addr_filters_adjust(vma); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6605 | perf_event_mmap_event(&mmap_event); |
| 6606 | } |
| 6607 | |
Alexander Shishkin | 68db7e9 | 2015-01-14 14:18:15 +0200 | [diff] [blame] | 6608 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
| 6609 | unsigned long size, u64 flags) |
| 6610 | { |
| 6611 | struct perf_output_handle handle; |
| 6612 | struct perf_sample_data sample; |
| 6613 | struct perf_aux_event { |
| 6614 | struct perf_event_header header; |
| 6615 | u64 offset; |
| 6616 | u64 size; |
| 6617 | u64 flags; |
| 6618 | } rec = { |
| 6619 | .header = { |
| 6620 | .type = PERF_RECORD_AUX, |
| 6621 | .misc = 0, |
| 6622 | .size = sizeof(rec), |
| 6623 | }, |
| 6624 | .offset = head, |
| 6625 | .size = size, |
| 6626 | .flags = flags, |
| 6627 | }; |
| 6628 | int ret; |
| 6629 | |
| 6630 | perf_event_header__init_id(&rec.header, &sample, event); |
| 6631 | ret = perf_output_begin(&handle, event, rec.header.size); |
| 6632 | |
| 6633 | if (ret) |
| 6634 | return; |
| 6635 | |
| 6636 | perf_output_put(&handle, rec); |
| 6637 | perf_event__output_id_sample(event, &handle, &sample); |
| 6638 | |
| 6639 | perf_output_end(&handle); |
| 6640 | } |
| 6641 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6642 | /* |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 6643 | * Lost/dropped samples logging |
| 6644 | */ |
| 6645 | void perf_log_lost_samples(struct perf_event *event, u64 lost) |
| 6646 | { |
| 6647 | struct perf_output_handle handle; |
| 6648 | struct perf_sample_data sample; |
| 6649 | int ret; |
| 6650 | |
| 6651 | struct { |
| 6652 | struct perf_event_header header; |
| 6653 | u64 lost; |
| 6654 | } lost_samples_event = { |
| 6655 | .header = { |
| 6656 | .type = PERF_RECORD_LOST_SAMPLES, |
| 6657 | .misc = 0, |
| 6658 | .size = sizeof(lost_samples_event), |
| 6659 | }, |
| 6660 | .lost = lost, |
| 6661 | }; |
| 6662 | |
| 6663 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); |
| 6664 | |
| 6665 | ret = perf_output_begin(&handle, event, |
| 6666 | lost_samples_event.header.size); |
| 6667 | if (ret) |
| 6668 | return; |
| 6669 | |
| 6670 | perf_output_put(&handle, lost_samples_event); |
| 6671 | perf_event__output_id_sample(event, &handle, &sample); |
| 6672 | perf_output_end(&handle); |
| 6673 | } |
| 6674 | |
| 6675 | /* |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 6676 | * context_switch tracking |
| 6677 | */ |
| 6678 | |
| 6679 | struct perf_switch_event { |
| 6680 | struct task_struct *task; |
| 6681 | struct task_struct *next_prev; |
| 6682 | |
| 6683 | struct { |
| 6684 | struct perf_event_header header; |
| 6685 | u32 next_prev_pid; |
| 6686 | u32 next_prev_tid; |
| 6687 | } event_id; |
| 6688 | }; |
| 6689 | |
| 6690 | static int perf_event_switch_match(struct perf_event *event) |
| 6691 | { |
| 6692 | return event->attr.context_switch; |
| 6693 | } |
| 6694 | |
| 6695 | static void perf_event_switch_output(struct perf_event *event, void *data) |
| 6696 | { |
| 6697 | struct perf_switch_event *se = data; |
| 6698 | struct perf_output_handle handle; |
| 6699 | struct perf_sample_data sample; |
| 6700 | int ret; |
| 6701 | |
| 6702 | if (!perf_event_switch_match(event)) |
| 6703 | return; |
| 6704 | |
| 6705 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ |
| 6706 | if (event->ctx->task) { |
| 6707 | se->event_id.header.type = PERF_RECORD_SWITCH; |
| 6708 | se->event_id.header.size = sizeof(se->event_id.header); |
| 6709 | } else { |
| 6710 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; |
| 6711 | se->event_id.header.size = sizeof(se->event_id); |
| 6712 | se->event_id.next_prev_pid = |
| 6713 | perf_event_pid(event, se->next_prev); |
| 6714 | se->event_id.next_prev_tid = |
| 6715 | perf_event_tid(event, se->next_prev); |
| 6716 | } |
| 6717 | |
| 6718 | perf_event_header__init_id(&se->event_id.header, &sample, event); |
| 6719 | |
| 6720 | ret = perf_output_begin(&handle, event, se->event_id.header.size); |
| 6721 | if (ret) |
| 6722 | return; |
| 6723 | |
| 6724 | if (event->ctx->task) |
| 6725 | perf_output_put(&handle, se->event_id.header); |
| 6726 | else |
| 6727 | perf_output_put(&handle, se->event_id); |
| 6728 | |
| 6729 | perf_event__output_id_sample(event, &handle, &sample); |
| 6730 | |
| 6731 | perf_output_end(&handle); |
| 6732 | } |
| 6733 | |
| 6734 | static void perf_event_switch(struct task_struct *task, |
| 6735 | struct task_struct *next_prev, bool sched_in) |
| 6736 | { |
| 6737 | struct perf_switch_event switch_event; |
| 6738 | |
| 6739 | /* N.B. caller checks nr_switch_events != 0 */ |
| 6740 | |
| 6741 | switch_event = (struct perf_switch_event){ |
| 6742 | .task = task, |
| 6743 | .next_prev = next_prev, |
| 6744 | .event_id = { |
| 6745 | .header = { |
| 6746 | /* .type */ |
| 6747 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, |
| 6748 | /* .size */ |
| 6749 | }, |
| 6750 | /* .next_prev_pid */ |
| 6751 | /* .next_prev_tid */ |
| 6752 | }, |
| 6753 | }; |
| 6754 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 6755 | perf_iterate_sb(perf_event_switch_output, |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 6756 | &switch_event, |
| 6757 | NULL); |
| 6758 | } |
| 6759 | |
| 6760 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6761 | * IRQ throttle logging |
| 6762 | */ |
| 6763 | |
| 6764 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 6765 | { |
| 6766 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6767 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6768 | int ret; |
| 6769 | |
| 6770 | struct { |
| 6771 | struct perf_event_header header; |
| 6772 | u64 time; |
| 6773 | u64 id; |
| 6774 | u64 stream_id; |
| 6775 | } throttle_event = { |
| 6776 | .header = { |
| 6777 | .type = PERF_RECORD_THROTTLE, |
| 6778 | .misc = 0, |
| 6779 | .size = sizeof(throttle_event), |
| 6780 | }, |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6781 | .time = perf_event_clock(event), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6782 | .id = primary_event_id(event), |
| 6783 | .stream_id = event->id, |
| 6784 | }; |
| 6785 | |
| 6786 | if (enable) |
| 6787 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 6788 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6789 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 6790 | |
| 6791 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6792 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6793 | if (ret) |
| 6794 | return; |
| 6795 | |
| 6796 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6797 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6798 | perf_output_end(&handle); |
| 6799 | } |
| 6800 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 6801 | static void perf_log_itrace_start(struct perf_event *event) |
| 6802 | { |
| 6803 | struct perf_output_handle handle; |
| 6804 | struct perf_sample_data sample; |
| 6805 | struct perf_aux_event { |
| 6806 | struct perf_event_header header; |
| 6807 | u32 pid; |
| 6808 | u32 tid; |
| 6809 | } rec; |
| 6810 | int ret; |
| 6811 | |
| 6812 | if (event->parent) |
| 6813 | event = event->parent; |
| 6814 | |
| 6815 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || |
| 6816 | event->hw.itrace_started) |
| 6817 | return; |
| 6818 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 6819 | rec.header.type = PERF_RECORD_ITRACE_START; |
| 6820 | rec.header.misc = 0; |
| 6821 | rec.header.size = sizeof(rec); |
| 6822 | rec.pid = perf_event_pid(event, current); |
| 6823 | rec.tid = perf_event_tid(event, current); |
| 6824 | |
| 6825 | perf_event_header__init_id(&rec.header, &sample, event); |
| 6826 | ret = perf_output_begin(&handle, event, rec.header.size); |
| 6827 | |
| 6828 | if (ret) |
| 6829 | return; |
| 6830 | |
| 6831 | perf_output_put(&handle, rec); |
| 6832 | perf_event__output_id_sample(event, &handle, &sample); |
| 6833 | |
| 6834 | perf_output_end(&handle); |
| 6835 | } |
| 6836 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6837 | /* |
| 6838 | * Generic event overflow handling, sampling. |
| 6839 | */ |
| 6840 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6841 | static int __perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6842 | int throttle, struct perf_sample_data *data, |
| 6843 | struct pt_regs *regs) |
| 6844 | { |
| 6845 | int events = atomic_read(&event->event_limit); |
| 6846 | struct hw_perf_event *hwc = &event->hw; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 6847 | u64 seq; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6848 | int ret = 0; |
| 6849 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 6850 | /* |
| 6851 | * Non-sampling counters might still use the PMI to fold short |
| 6852 | * hardware counters, ignore those. |
| 6853 | */ |
| 6854 | if (unlikely(!is_sampling_event(event))) |
| 6855 | return 0; |
| 6856 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 6857 | seq = __this_cpu_read(perf_throttled_seq); |
| 6858 | if (seq != hwc->interrupts_seq) { |
| 6859 | hwc->interrupts_seq = seq; |
| 6860 | hwc->interrupts = 1; |
| 6861 | } else { |
| 6862 | hwc->interrupts++; |
| 6863 | if (unlikely(throttle |
| 6864 | && hwc->interrupts >= max_samples_per_tick)) { |
| 6865 | __this_cpu_inc(perf_throttled_count); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 6866 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 6867 | hwc->interrupts = MAX_INTERRUPTS; |
| 6868 | perf_log_throttle(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6869 | ret = 1; |
| 6870 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 6871 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6872 | |
| 6873 | if (event->attr.freq) { |
| 6874 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 6875 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6876 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 6877 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6878 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 6879 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 6880 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | /* |
| 6884 | * XXX event_limit might not quite work as expected on inherited |
| 6885 | * events |
| 6886 | */ |
| 6887 | |
| 6888 | event->pending_kill = POLL_IN; |
| 6889 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 6890 | ret = 1; |
| 6891 | event->pending_kill = POLL_HUP; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6892 | event->pending_disable = 1; |
| 6893 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6894 | } |
| 6895 | |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 6896 | event->overflow_handler(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 6897 | |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 6898 | if (*perf_event_fasync(event) && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6899 | event->pending_wakeup = 1; |
| 6900 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 6901 | } |
| 6902 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6903 | return ret; |
| 6904 | } |
| 6905 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6906 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6907 | struct perf_sample_data *data, |
| 6908 | struct pt_regs *regs) |
| 6909 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6910 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6911 | } |
| 6912 | |
| 6913 | /* |
| 6914 | * Generic software event infrastructure |
| 6915 | */ |
| 6916 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6917 | struct swevent_htable { |
| 6918 | struct swevent_hlist *swevent_hlist; |
| 6919 | struct mutex hlist_mutex; |
| 6920 | int hlist_refcount; |
| 6921 | |
| 6922 | /* Recursion avoidance in each contexts */ |
| 6923 | int recursion[PERF_NR_CONTEXTS]; |
| 6924 | }; |
| 6925 | |
| 6926 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 6927 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6928 | /* |
| 6929 | * We directly increment event->count and keep a second value in |
| 6930 | * event->hw.period_left to count intervals. This period event |
| 6931 | * is kept in the range [-sample_period, 0] so that we can use the |
| 6932 | * sign as trigger. |
| 6933 | */ |
| 6934 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 6935 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6936 | { |
| 6937 | struct hw_perf_event *hwc = &event->hw; |
| 6938 | u64 period = hwc->last_period; |
| 6939 | u64 nr, offset; |
| 6940 | s64 old, val; |
| 6941 | |
| 6942 | hwc->last_period = hwc->sample_period; |
| 6943 | |
| 6944 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6945 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6946 | if (val < 0) |
| 6947 | return 0; |
| 6948 | |
| 6949 | nr = div64_u64(period + val, period); |
| 6950 | offset = nr * period; |
| 6951 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6952 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6953 | goto again; |
| 6954 | |
| 6955 | return nr; |
| 6956 | } |
| 6957 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6958 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6959 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6960 | struct pt_regs *regs) |
| 6961 | { |
| 6962 | struct hw_perf_event *hwc = &event->hw; |
| 6963 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6964 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6965 | if (!overflow) |
| 6966 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6967 | |
| 6968 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 6969 | return; |
| 6970 | |
| 6971 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6972 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6973 | data, regs)) { |
| 6974 | /* |
| 6975 | * We inhibit the overflow from happening when |
| 6976 | * hwc->interrupts == MAX_INTERRUPTS. |
| 6977 | */ |
| 6978 | break; |
| 6979 | } |
| 6980 | throttle = 1; |
| 6981 | } |
| 6982 | } |
| 6983 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6984 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6985 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6986 | struct pt_regs *regs) |
| 6987 | { |
| 6988 | struct hw_perf_event *hwc = &event->hw; |
| 6989 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6990 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6991 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6992 | if (!regs) |
| 6993 | return; |
| 6994 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6995 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6996 | return; |
| 6997 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 6998 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 6999 | data->period = nr; |
| 7000 | return perf_swevent_overflow(event, 1, data, regs); |
| 7001 | } else |
| 7002 | data->period = event->hw.last_period; |
| 7003 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 7004 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7005 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 7006 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 7007 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 7008 | return; |
| 7009 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7010 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7011 | } |
| 7012 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7013 | static int perf_exclude_event(struct perf_event *event, |
| 7014 | struct pt_regs *regs) |
| 7015 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7016 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 7017 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7018 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7019 | if (regs) { |
| 7020 | if (event->attr.exclude_user && user_mode(regs)) |
| 7021 | return 1; |
| 7022 | |
| 7023 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 7024 | return 1; |
| 7025 | } |
| 7026 | |
| 7027 | return 0; |
| 7028 | } |
| 7029 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7030 | static int perf_swevent_match(struct perf_event *event, |
| 7031 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7032 | u32 event_id, |
| 7033 | struct perf_sample_data *data, |
| 7034 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7035 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7036 | if (event->attr.type != type) |
| 7037 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7038 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7039 | if (event->attr.config != event_id) |
| 7040 | return 0; |
| 7041 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7042 | if (perf_exclude_event(event, regs)) |
| 7043 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7044 | |
| 7045 | return 1; |
| 7046 | } |
| 7047 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7048 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7049 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7050 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7051 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7052 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 7053 | } |
| 7054 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7055 | static inline struct hlist_head * |
| 7056 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7057 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7058 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7059 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7060 | return &hlist->heads[hash]; |
| 7061 | } |
| 7062 | |
| 7063 | /* For the read side: events when they trigger */ |
| 7064 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7065 | 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] | 7066 | { |
| 7067 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7068 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7069 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7070 | if (!hlist) |
| 7071 | return NULL; |
| 7072 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7073 | return __find_swevent_head(hlist, type, event_id); |
| 7074 | } |
| 7075 | |
| 7076 | /* For the event head insertion and removal in the hlist */ |
| 7077 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7078 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7079 | { |
| 7080 | struct swevent_hlist *hlist; |
| 7081 | u32 event_id = event->attr.config; |
| 7082 | u64 type = event->attr.type; |
| 7083 | |
| 7084 | /* |
| 7085 | * Event scheduling is always serialized against hlist allocation |
| 7086 | * and release. Which makes the protected version suitable here. |
| 7087 | * The context lock guarantees that. |
| 7088 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7089 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7090 | lockdep_is_held(&event->ctx->lock)); |
| 7091 | if (!hlist) |
| 7092 | return NULL; |
| 7093 | |
| 7094 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7095 | } |
| 7096 | |
| 7097 | 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] | 7098 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7099 | struct perf_sample_data *data, |
| 7100 | struct pt_regs *regs) |
| 7101 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7102 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7103 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7104 | struct hlist_head *head; |
| 7105 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7106 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7107 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7108 | if (!head) |
| 7109 | goto end; |
| 7110 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7111 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7112 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7113 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7114 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7115 | end: |
| 7116 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7117 | } |
| 7118 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7119 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
| 7120 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7121 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7122 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7123 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 7124 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7125 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7126 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 7127 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7128 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 7129 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7130 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7131 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 7132 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7133 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 7134 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7135 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7136 | 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] | 7137 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 7138 | struct perf_sample_data data; |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7139 | |
| 7140 | if (WARN_ON_ONCE(!regs)) |
| 7141 | return; |
| 7142 | |
| 7143 | perf_sample_data_init(&data, addr, 0); |
| 7144 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
| 7145 | } |
| 7146 | |
| 7147 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
| 7148 | { |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7149 | int rctx; |
| 7150 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7151 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7152 | rctx = perf_swevent_get_recursion_context(); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7153 | if (unlikely(rctx < 0)) |
| 7154 | goto fail; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7155 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7156 | ___perf_sw_event(event_id, nr, regs, addr); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 7157 | |
| 7158 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 7159 | fail: |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7160 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7161 | } |
| 7162 | |
| 7163 | static void perf_swevent_read(struct perf_event *event) |
| 7164 | { |
| 7165 | } |
| 7166 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7167 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7168 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 7169 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7170 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7171 | struct hlist_head *head; |
| 7172 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 7173 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7174 | hwc->last_period = hwc->sample_period; |
| 7175 | perf_swevent_set_period(event); |
| 7176 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7177 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7178 | hwc->state = !(flags & PERF_EF_START); |
| 7179 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7180 | head = find_swevent_head(swhash, event); |
Peter Zijlstra | 12ca6ad | 2015-12-15 13:49:05 +0100 | [diff] [blame] | 7181 | if (WARN_ON_ONCE(!head)) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7182 | return -EINVAL; |
| 7183 | |
| 7184 | hlist_add_head_rcu(&event->hlist_entry, head); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 7185 | perf_event_update_userpage(event); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7186 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7187 | return 0; |
| 7188 | } |
| 7189 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7190 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7191 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7192 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7193 | } |
| 7194 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7195 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7196 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7197 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7198 | } |
| 7199 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7200 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7201 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7202 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 7203 | } |
| 7204 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7205 | /* Deref the hlist from the update side */ |
| 7206 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7207 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7208 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7209 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 7210 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7211 | } |
| 7212 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7213 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7214 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7215 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7216 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 7217 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7218 | return; |
| 7219 | |
Andreea-Cristina Bernat | 70691d4 | 2014-08-22 16:26:05 +0300 | [diff] [blame] | 7220 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 7221 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7222 | } |
| 7223 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7224 | static void swevent_hlist_put_cpu(int cpu) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7225 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7226 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7227 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7228 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7229 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7230 | if (!--swhash->hlist_refcount) |
| 7231 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7232 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7233 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7234 | } |
| 7235 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7236 | static void swevent_hlist_put(void) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7237 | { |
| 7238 | int cpu; |
| 7239 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7240 | for_each_possible_cpu(cpu) |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7241 | swevent_hlist_put_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7242 | } |
| 7243 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7244 | static int swevent_hlist_get_cpu(int cpu) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7245 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7246 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7247 | int err = 0; |
| 7248 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7249 | mutex_lock(&swhash->hlist_mutex); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7250 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7251 | struct swevent_hlist *hlist; |
| 7252 | |
| 7253 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 7254 | if (!hlist) { |
| 7255 | err = -ENOMEM; |
| 7256 | goto exit; |
| 7257 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7258 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7259 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7260 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 7261 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 7262 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7263 | |
| 7264 | return err; |
| 7265 | } |
| 7266 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7267 | static int swevent_hlist_get(void) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7268 | { |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7269 | int err, cpu, failed_cpu; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7270 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7271 | get_online_cpus(); |
| 7272 | for_each_possible_cpu(cpu) { |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7273 | err = swevent_hlist_get_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7274 | if (err) { |
| 7275 | failed_cpu = cpu; |
| 7276 | goto fail; |
| 7277 | } |
| 7278 | } |
| 7279 | put_online_cpus(); |
| 7280 | |
| 7281 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 7282 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7283 | for_each_possible_cpu(cpu) { |
| 7284 | if (cpu == failed_cpu) |
| 7285 | break; |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7286 | swevent_hlist_put_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7287 | } |
| 7288 | |
| 7289 | put_online_cpus(); |
| 7290 | return err; |
| 7291 | } |
| 7292 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 7293 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7294 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7295 | static void sw_perf_event_destroy(struct perf_event *event) |
| 7296 | { |
| 7297 | u64 event_id = event->attr.config; |
| 7298 | |
| 7299 | WARN_ON(event->parent); |
| 7300 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 7301 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7302 | swevent_hlist_put(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7303 | } |
| 7304 | |
| 7305 | static int perf_swevent_init(struct perf_event *event) |
| 7306 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 7307 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7308 | |
| 7309 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 7310 | return -ENOENT; |
| 7311 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 7312 | /* |
| 7313 | * no branch sampling for software events |
| 7314 | */ |
| 7315 | if (has_branch_stack(event)) |
| 7316 | return -EOPNOTSUPP; |
| 7317 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7318 | switch (event_id) { |
| 7319 | case PERF_COUNT_SW_CPU_CLOCK: |
| 7320 | case PERF_COUNT_SW_TASK_CLOCK: |
| 7321 | return -ENOENT; |
| 7322 | |
| 7323 | default: |
| 7324 | break; |
| 7325 | } |
| 7326 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 7327 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7328 | return -ENOENT; |
| 7329 | |
| 7330 | if (!event->parent) { |
| 7331 | int err; |
| 7332 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 7333 | err = swevent_hlist_get(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7334 | if (err) |
| 7335 | return err; |
| 7336 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 7337 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7338 | event->destroy = sw_perf_event_destroy; |
| 7339 | } |
| 7340 | |
| 7341 | return 0; |
| 7342 | } |
| 7343 | |
| 7344 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7345 | .task_ctx_nr = perf_sw_context, |
| 7346 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 7347 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 7348 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7349 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7350 | .add = perf_swevent_add, |
| 7351 | .del = perf_swevent_del, |
| 7352 | .start = perf_swevent_start, |
| 7353 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7354 | .read = perf_swevent_read, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7355 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7356 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7357 | #ifdef CONFIG_EVENT_TRACING |
| 7358 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7359 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7360 | struct perf_sample_data *data) |
| 7361 | { |
| 7362 | void *record = data->raw->data; |
| 7363 | |
Peter Zijlstra | b71b437 | 2015-11-02 10:50:51 +0100 | [diff] [blame] | 7364 | /* only top level events have filters set */ |
| 7365 | if (event->parent) |
| 7366 | event = event->parent; |
| 7367 | |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 7368 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 7369 | return 1; |
| 7370 | return 0; |
| 7371 | } |
| 7372 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7373 | static int perf_tp_event_match(struct perf_event *event, |
| 7374 | struct perf_sample_data *data, |
| 7375 | struct pt_regs *regs) |
| 7376 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 7377 | if (event->hw.state & PERF_HES_STOPPED) |
| 7378 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 7379 | /* |
| 7380 | * All tracepoints are from kernel-space. |
| 7381 | */ |
| 7382 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7383 | return 0; |
| 7384 | |
| 7385 | if (!perf_tp_filter_match(event, data)) |
| 7386 | return 0; |
| 7387 | |
| 7388 | return 1; |
| 7389 | } |
| 7390 | |
| 7391 | void perf_tp_event(u64 addr, u64 count, void *record, int entry_size, |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 7392 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 7393 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7394 | { |
| 7395 | struct perf_sample_data data; |
| 7396 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7397 | |
| 7398 | struct perf_raw_record raw = { |
| 7399 | .size = entry_size, |
| 7400 | .data = record, |
| 7401 | }; |
| 7402 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7403 | perf_sample_data_init(&data, addr, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7404 | data.raw = &raw; |
| 7405 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7406 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7407 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7408 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7409 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 7410 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 7411 | /* |
| 7412 | * If we got specified a target task, also iterate its context and |
| 7413 | * deliver this event there too. |
| 7414 | */ |
| 7415 | if (task && task != current) { |
| 7416 | struct perf_event_context *ctx; |
| 7417 | struct trace_entry *entry = record; |
| 7418 | |
| 7419 | rcu_read_lock(); |
| 7420 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 7421 | if (!ctx) |
| 7422 | goto unlock; |
| 7423 | |
| 7424 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 7425 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7426 | continue; |
| 7427 | if (event->attr.config != entry->type) |
| 7428 | continue; |
| 7429 | if (perf_tp_event_match(event, &data, regs)) |
| 7430 | perf_swevent_event(event, count, &data, regs); |
| 7431 | } |
| 7432 | unlock: |
| 7433 | rcu_read_unlock(); |
| 7434 | } |
| 7435 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 7436 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7437 | } |
| 7438 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 7439 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7440 | static void tp_perf_event_destroy(struct perf_event *event) |
| 7441 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7442 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7443 | } |
| 7444 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7445 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7446 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7447 | int err; |
| 7448 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7449 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7450 | return -ENOENT; |
| 7451 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 7452 | /* |
| 7453 | * no branch sampling for tracepoint events |
| 7454 | */ |
| 7455 | if (has_branch_stack(event)) |
| 7456 | return -EOPNOTSUPP; |
| 7457 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7458 | err = perf_trace_init(event); |
| 7459 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7460 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7461 | |
| 7462 | event->destroy = tp_perf_event_destroy; |
| 7463 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7464 | return 0; |
| 7465 | } |
| 7466 | |
| 7467 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7468 | .task_ctx_nr = perf_sw_context, |
| 7469 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7470 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7471 | .add = perf_trace_add, |
| 7472 | .del = perf_trace_del, |
| 7473 | .start = perf_swevent_start, |
| 7474 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7475 | .read = perf_swevent_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7476 | }; |
| 7477 | |
| 7478 | static inline void perf_tp_register(void) |
| 7479 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7480 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7481 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7482 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7483 | static void perf_event_free_filter(struct perf_event *event) |
| 7484 | { |
| 7485 | ftrace_profile_free_filter(event); |
| 7486 | } |
| 7487 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7488 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
| 7489 | { |
| 7490 | struct bpf_prog *prog; |
| 7491 | |
| 7492 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7493 | return -EINVAL; |
| 7494 | |
| 7495 | if (event->tp_event->prog) |
| 7496 | return -EEXIST; |
| 7497 | |
Wang Nan | 04a22fa | 2015-07-01 02:13:50 +0000 | [diff] [blame] | 7498 | if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE)) |
| 7499 | /* bpf programs can only be attached to u/kprobes */ |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7500 | return -EINVAL; |
| 7501 | |
| 7502 | prog = bpf_prog_get(prog_fd); |
| 7503 | if (IS_ERR(prog)) |
| 7504 | return PTR_ERR(prog); |
| 7505 | |
Linus Torvalds | 6c373ca | 2015-04-15 09:00:47 -0700 | [diff] [blame] | 7506 | if (prog->type != BPF_PROG_TYPE_KPROBE) { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7507 | /* valid fd, but invalid bpf program type */ |
| 7508 | bpf_prog_put(prog); |
| 7509 | return -EINVAL; |
| 7510 | } |
| 7511 | |
| 7512 | event->tp_event->prog = prog; |
| 7513 | |
| 7514 | return 0; |
| 7515 | } |
| 7516 | |
| 7517 | static void perf_event_free_bpf_prog(struct perf_event *event) |
| 7518 | { |
| 7519 | struct bpf_prog *prog; |
| 7520 | |
| 7521 | if (!event->tp_event) |
| 7522 | return; |
| 7523 | |
| 7524 | prog = event->tp_event->prog; |
| 7525 | if (prog) { |
| 7526 | event->tp_event->prog = NULL; |
| 7527 | bpf_prog_put(prog); |
| 7528 | } |
| 7529 | } |
| 7530 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7531 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7532 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7533 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7534 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7535 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7536 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7537 | static void perf_event_free_filter(struct perf_event *event) |
| 7538 | { |
| 7539 | } |
| 7540 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7541 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
| 7542 | { |
| 7543 | return -ENOENT; |
| 7544 | } |
| 7545 | |
| 7546 | static void perf_event_free_bpf_prog(struct perf_event *event) |
| 7547 | { |
| 7548 | } |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 7549 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7550 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7551 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7552 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7553 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7554 | struct perf_sample_data sample; |
| 7555 | struct pt_regs *regs = data; |
| 7556 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7557 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7558 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7559 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7560 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7561 | } |
| 7562 | #endif |
| 7563 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 7564 | /* |
| 7565 | * Allocate a new address filter |
| 7566 | */ |
| 7567 | static struct perf_addr_filter * |
| 7568 | perf_addr_filter_new(struct perf_event *event, struct list_head *filters) |
| 7569 | { |
| 7570 | int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); |
| 7571 | struct perf_addr_filter *filter; |
| 7572 | |
| 7573 | filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); |
| 7574 | if (!filter) |
| 7575 | return NULL; |
| 7576 | |
| 7577 | INIT_LIST_HEAD(&filter->entry); |
| 7578 | list_add_tail(&filter->entry, filters); |
| 7579 | |
| 7580 | return filter; |
| 7581 | } |
| 7582 | |
| 7583 | static void free_filters_list(struct list_head *filters) |
| 7584 | { |
| 7585 | struct perf_addr_filter *filter, *iter; |
| 7586 | |
| 7587 | list_for_each_entry_safe(filter, iter, filters, entry) { |
| 7588 | if (filter->inode) |
| 7589 | iput(filter->inode); |
| 7590 | list_del(&filter->entry); |
| 7591 | kfree(filter); |
| 7592 | } |
| 7593 | } |
| 7594 | |
| 7595 | /* |
| 7596 | * Free existing address filters and optionally install new ones |
| 7597 | */ |
| 7598 | static void perf_addr_filters_splice(struct perf_event *event, |
| 7599 | struct list_head *head) |
| 7600 | { |
| 7601 | unsigned long flags; |
| 7602 | LIST_HEAD(list); |
| 7603 | |
| 7604 | if (!has_addr_filter(event)) |
| 7605 | return; |
| 7606 | |
| 7607 | /* don't bother with children, they don't have their own filters */ |
| 7608 | if (event->parent) |
| 7609 | return; |
| 7610 | |
| 7611 | raw_spin_lock_irqsave(&event->addr_filters.lock, flags); |
| 7612 | |
| 7613 | list_splice_init(&event->addr_filters.list, &list); |
| 7614 | if (head) |
| 7615 | list_splice(head, &event->addr_filters.list); |
| 7616 | |
| 7617 | raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); |
| 7618 | |
| 7619 | free_filters_list(&list); |
| 7620 | } |
| 7621 | |
| 7622 | /* |
| 7623 | * Scan through mm's vmas and see if one of them matches the |
| 7624 | * @filter; if so, adjust filter's address range. |
| 7625 | * Called with mm::mmap_sem down for reading. |
| 7626 | */ |
| 7627 | static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter, |
| 7628 | struct mm_struct *mm) |
| 7629 | { |
| 7630 | struct vm_area_struct *vma; |
| 7631 | |
| 7632 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 7633 | struct file *file = vma->vm_file; |
| 7634 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; |
| 7635 | unsigned long vma_size = vma->vm_end - vma->vm_start; |
| 7636 | |
| 7637 | if (!file) |
| 7638 | continue; |
| 7639 | |
| 7640 | if (!perf_addr_filter_match(filter, file, off, vma_size)) |
| 7641 | continue; |
| 7642 | |
| 7643 | return vma->vm_start; |
| 7644 | } |
| 7645 | |
| 7646 | return 0; |
| 7647 | } |
| 7648 | |
| 7649 | /* |
| 7650 | * Update event's address range filters based on the |
| 7651 | * task's existing mappings, if any. |
| 7652 | */ |
| 7653 | static void perf_event_addr_filters_apply(struct perf_event *event) |
| 7654 | { |
| 7655 | struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); |
| 7656 | struct task_struct *task = READ_ONCE(event->ctx->task); |
| 7657 | struct perf_addr_filter *filter; |
| 7658 | struct mm_struct *mm = NULL; |
| 7659 | unsigned int count = 0; |
| 7660 | unsigned long flags; |
| 7661 | |
| 7662 | /* |
| 7663 | * We may observe TASK_TOMBSTONE, which means that the event tear-down |
| 7664 | * will stop on the parent's child_mutex that our caller is also holding |
| 7665 | */ |
| 7666 | if (task == TASK_TOMBSTONE) |
| 7667 | return; |
| 7668 | |
| 7669 | mm = get_task_mm(event->ctx->task); |
| 7670 | if (!mm) |
| 7671 | goto restart; |
| 7672 | |
| 7673 | down_read(&mm->mmap_sem); |
| 7674 | |
| 7675 | raw_spin_lock_irqsave(&ifh->lock, flags); |
| 7676 | list_for_each_entry(filter, &ifh->list, entry) { |
| 7677 | event->addr_filters_offs[count] = 0; |
| 7678 | |
| 7679 | if (perf_addr_filter_needs_mmap(filter)) |
| 7680 | event->addr_filters_offs[count] = |
| 7681 | perf_addr_filter_apply(filter, mm); |
| 7682 | |
| 7683 | count++; |
| 7684 | } |
| 7685 | |
| 7686 | event->addr_filters_gen++; |
| 7687 | raw_spin_unlock_irqrestore(&ifh->lock, flags); |
| 7688 | |
| 7689 | up_read(&mm->mmap_sem); |
| 7690 | |
| 7691 | mmput(mm); |
| 7692 | |
| 7693 | restart: |
| 7694 | perf_event_restart(event); |
| 7695 | } |
| 7696 | |
| 7697 | /* |
| 7698 | * Address range filtering: limiting the data to certain |
| 7699 | * instruction address ranges. Filters are ioctl()ed to us from |
| 7700 | * userspace as ascii strings. |
| 7701 | * |
| 7702 | * Filter string format: |
| 7703 | * |
| 7704 | * ACTION RANGE_SPEC |
| 7705 | * where ACTION is one of the |
| 7706 | * * "filter": limit the trace to this region |
| 7707 | * * "start": start tracing from this address |
| 7708 | * * "stop": stop tracing at this address/region; |
| 7709 | * RANGE_SPEC is |
| 7710 | * * for kernel addresses: <start address>[/<size>] |
| 7711 | * * for object files: <start address>[/<size>]@</path/to/object/file> |
| 7712 | * |
| 7713 | * if <size> is not specified, the range is treated as a single address. |
| 7714 | */ |
| 7715 | enum { |
| 7716 | IF_ACT_FILTER, |
| 7717 | IF_ACT_START, |
| 7718 | IF_ACT_STOP, |
| 7719 | IF_SRC_FILE, |
| 7720 | IF_SRC_KERNEL, |
| 7721 | IF_SRC_FILEADDR, |
| 7722 | IF_SRC_KERNELADDR, |
| 7723 | }; |
| 7724 | |
| 7725 | enum { |
| 7726 | IF_STATE_ACTION = 0, |
| 7727 | IF_STATE_SOURCE, |
| 7728 | IF_STATE_END, |
| 7729 | }; |
| 7730 | |
| 7731 | static const match_table_t if_tokens = { |
| 7732 | { IF_ACT_FILTER, "filter" }, |
| 7733 | { IF_ACT_START, "start" }, |
| 7734 | { IF_ACT_STOP, "stop" }, |
| 7735 | { IF_SRC_FILE, "%u/%u@%s" }, |
| 7736 | { IF_SRC_KERNEL, "%u/%u" }, |
| 7737 | { IF_SRC_FILEADDR, "%u@%s" }, |
| 7738 | { IF_SRC_KERNELADDR, "%u" }, |
| 7739 | }; |
| 7740 | |
| 7741 | /* |
| 7742 | * Address filter string parser |
| 7743 | */ |
| 7744 | static int |
| 7745 | perf_event_parse_addr_filter(struct perf_event *event, char *fstr, |
| 7746 | struct list_head *filters) |
| 7747 | { |
| 7748 | struct perf_addr_filter *filter = NULL; |
| 7749 | char *start, *orig, *filename = NULL; |
| 7750 | struct path path; |
| 7751 | substring_t args[MAX_OPT_ARGS]; |
| 7752 | int state = IF_STATE_ACTION, token; |
| 7753 | unsigned int kernel = 0; |
| 7754 | int ret = -EINVAL; |
| 7755 | |
| 7756 | orig = fstr = kstrdup(fstr, GFP_KERNEL); |
| 7757 | if (!fstr) |
| 7758 | return -ENOMEM; |
| 7759 | |
| 7760 | while ((start = strsep(&fstr, " ,\n")) != NULL) { |
| 7761 | ret = -EINVAL; |
| 7762 | |
| 7763 | if (!*start) |
| 7764 | continue; |
| 7765 | |
| 7766 | /* filter definition begins */ |
| 7767 | if (state == IF_STATE_ACTION) { |
| 7768 | filter = perf_addr_filter_new(event, filters); |
| 7769 | if (!filter) |
| 7770 | goto fail; |
| 7771 | } |
| 7772 | |
| 7773 | token = match_token(start, if_tokens, args); |
| 7774 | switch (token) { |
| 7775 | case IF_ACT_FILTER: |
| 7776 | case IF_ACT_START: |
| 7777 | filter->filter = 1; |
| 7778 | |
| 7779 | case IF_ACT_STOP: |
| 7780 | if (state != IF_STATE_ACTION) |
| 7781 | goto fail; |
| 7782 | |
| 7783 | state = IF_STATE_SOURCE; |
| 7784 | break; |
| 7785 | |
| 7786 | case IF_SRC_KERNELADDR: |
| 7787 | case IF_SRC_KERNEL: |
| 7788 | kernel = 1; |
| 7789 | |
| 7790 | case IF_SRC_FILEADDR: |
| 7791 | case IF_SRC_FILE: |
| 7792 | if (state != IF_STATE_SOURCE) |
| 7793 | goto fail; |
| 7794 | |
| 7795 | if (token == IF_SRC_FILE || token == IF_SRC_KERNEL) |
| 7796 | filter->range = 1; |
| 7797 | |
| 7798 | *args[0].to = 0; |
| 7799 | ret = kstrtoul(args[0].from, 0, &filter->offset); |
| 7800 | if (ret) |
| 7801 | goto fail; |
| 7802 | |
| 7803 | if (filter->range) { |
| 7804 | *args[1].to = 0; |
| 7805 | ret = kstrtoul(args[1].from, 0, &filter->size); |
| 7806 | if (ret) |
| 7807 | goto fail; |
| 7808 | } |
| 7809 | |
| 7810 | if (token == IF_SRC_FILE) { |
| 7811 | filename = match_strdup(&args[2]); |
| 7812 | if (!filename) { |
| 7813 | ret = -ENOMEM; |
| 7814 | goto fail; |
| 7815 | } |
| 7816 | } |
| 7817 | |
| 7818 | state = IF_STATE_END; |
| 7819 | break; |
| 7820 | |
| 7821 | default: |
| 7822 | goto fail; |
| 7823 | } |
| 7824 | |
| 7825 | /* |
| 7826 | * Filter definition is fully parsed, validate and install it. |
| 7827 | * Make sure that it doesn't contradict itself or the event's |
| 7828 | * attribute. |
| 7829 | */ |
| 7830 | if (state == IF_STATE_END) { |
| 7831 | if (kernel && event->attr.exclude_kernel) |
| 7832 | goto fail; |
| 7833 | |
| 7834 | if (!kernel) { |
| 7835 | if (!filename) |
| 7836 | goto fail; |
| 7837 | |
| 7838 | /* look up the path and grab its inode */ |
| 7839 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); |
| 7840 | if (ret) |
| 7841 | goto fail_free_name; |
| 7842 | |
| 7843 | filter->inode = igrab(d_inode(path.dentry)); |
| 7844 | path_put(&path); |
| 7845 | kfree(filename); |
| 7846 | filename = NULL; |
| 7847 | |
| 7848 | ret = -EINVAL; |
| 7849 | if (!filter->inode || |
| 7850 | !S_ISREG(filter->inode->i_mode)) |
| 7851 | /* free_filters_list() will iput() */ |
| 7852 | goto fail; |
| 7853 | } |
| 7854 | |
| 7855 | /* ready to consume more filters */ |
| 7856 | state = IF_STATE_ACTION; |
| 7857 | filter = NULL; |
| 7858 | } |
| 7859 | } |
| 7860 | |
| 7861 | if (state != IF_STATE_ACTION) |
| 7862 | goto fail; |
| 7863 | |
| 7864 | kfree(orig); |
| 7865 | |
| 7866 | return 0; |
| 7867 | |
| 7868 | fail_free_name: |
| 7869 | kfree(filename); |
| 7870 | fail: |
| 7871 | free_filters_list(filters); |
| 7872 | kfree(orig); |
| 7873 | |
| 7874 | return ret; |
| 7875 | } |
| 7876 | |
| 7877 | static int |
| 7878 | perf_event_set_addr_filter(struct perf_event *event, char *filter_str) |
| 7879 | { |
| 7880 | LIST_HEAD(filters); |
| 7881 | int ret; |
| 7882 | |
| 7883 | /* |
| 7884 | * Since this is called in perf_ioctl() path, we're already holding |
| 7885 | * ctx::mutex. |
| 7886 | */ |
| 7887 | lockdep_assert_held(&event->ctx->mutex); |
| 7888 | |
| 7889 | if (WARN_ON_ONCE(event->parent)) |
| 7890 | return -EINVAL; |
| 7891 | |
| 7892 | /* |
| 7893 | * For now, we only support filtering in per-task events; doing so |
| 7894 | * for CPU-wide events requires additional context switching trickery, |
| 7895 | * since same object code will be mapped at different virtual |
| 7896 | * addresses in different processes. |
| 7897 | */ |
| 7898 | if (!event->ctx->task) |
| 7899 | return -EOPNOTSUPP; |
| 7900 | |
| 7901 | ret = perf_event_parse_addr_filter(event, filter_str, &filters); |
| 7902 | if (ret) |
| 7903 | return ret; |
| 7904 | |
| 7905 | ret = event->pmu->addr_filters_validate(&filters); |
| 7906 | if (ret) { |
| 7907 | free_filters_list(&filters); |
| 7908 | return ret; |
| 7909 | } |
| 7910 | |
| 7911 | /* remove existing filters, if any */ |
| 7912 | perf_addr_filters_splice(event, &filters); |
| 7913 | |
| 7914 | /* install new filters */ |
| 7915 | perf_event_for_each_child(event, perf_event_addr_filters_apply); |
| 7916 | |
| 7917 | return ret; |
| 7918 | } |
| 7919 | |
Alexander Shishkin | c796bbb | 2016-04-27 18:44:42 +0300 | [diff] [blame] | 7920 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 7921 | { |
| 7922 | char *filter_str; |
| 7923 | int ret = -EINVAL; |
| 7924 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 7925 | if ((event->attr.type != PERF_TYPE_TRACEPOINT || |
| 7926 | !IS_ENABLED(CONFIG_EVENT_TRACING)) && |
| 7927 | !has_addr_filter(event)) |
Alexander Shishkin | c796bbb | 2016-04-27 18:44:42 +0300 | [diff] [blame] | 7928 | return -EINVAL; |
| 7929 | |
| 7930 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 7931 | if (IS_ERR(filter_str)) |
| 7932 | return PTR_ERR(filter_str); |
| 7933 | |
| 7934 | if (IS_ENABLED(CONFIG_EVENT_TRACING) && |
| 7935 | event->attr.type == PERF_TYPE_TRACEPOINT) |
| 7936 | ret = ftrace_profile_set_filter(event, event->attr.config, |
| 7937 | filter_str); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 7938 | else if (has_addr_filter(event)) |
| 7939 | ret = perf_event_set_addr_filter(event, filter_str); |
Alexander Shishkin | c796bbb | 2016-04-27 18:44:42 +0300 | [diff] [blame] | 7940 | |
| 7941 | kfree(filter_str); |
| 7942 | return ret; |
| 7943 | } |
| 7944 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7945 | /* |
| 7946 | * hrtimer based swevent callback |
| 7947 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7948 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7949 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7950 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7951 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 7952 | struct perf_sample_data data; |
| 7953 | struct pt_regs *regs; |
| 7954 | struct perf_event *event; |
| 7955 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7956 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7957 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 7958 | |
| 7959 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 7960 | return HRTIMER_NORESTART; |
| 7961 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7962 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7963 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7964 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7965 | regs = get_irq_regs(); |
| 7966 | |
| 7967 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 7968 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 7969 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7970 | ret = HRTIMER_NORESTART; |
| 7971 | } |
| 7972 | |
| 7973 | period = max_t(u64, 10000, event->hw.sample_period); |
| 7974 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 7975 | |
| 7976 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7977 | } |
| 7978 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7979 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7980 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7981 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 7982 | s64 period; |
| 7983 | |
| 7984 | if (!is_sampling_event(event)) |
| 7985 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7986 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 7987 | period = local64_read(&hwc->period_left); |
| 7988 | if (period) { |
| 7989 | if (period < 0) |
| 7990 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 7991 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 7992 | local64_set(&hwc->period_left, 0); |
| 7993 | } else { |
| 7994 | period = max_t(u64, 10000, hwc->sample_period); |
| 7995 | } |
Thomas Gleixner | 3497d20 | 2015-04-14 21:09:03 +0000 | [diff] [blame] | 7996 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
| 7997 | HRTIMER_MODE_REL_PINNED); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7998 | } |
| 7999 | |
| 8000 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 8001 | { |
| 8002 | struct hw_perf_event *hwc = &event->hw; |
| 8003 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 8004 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8005 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 8006 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8007 | |
| 8008 | hrtimer_cancel(&hwc->hrtimer); |
| 8009 | } |
| 8010 | } |
| 8011 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8012 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 8013 | { |
| 8014 | struct hw_perf_event *hwc = &event->hw; |
| 8015 | |
| 8016 | if (!is_sampling_event(event)) |
| 8017 | return; |
| 8018 | |
| 8019 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 8020 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 8021 | |
| 8022 | /* |
| 8023 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 8024 | * mapping and avoid the whole period adjust feedback stuff. |
| 8025 | */ |
| 8026 | if (event->attr.freq) { |
| 8027 | long freq = event->attr.sample_freq; |
| 8028 | |
| 8029 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 8030 | hwc->sample_period = event->attr.sample_period; |
| 8031 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 8032 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8033 | event->attr.freq = 0; |
| 8034 | } |
| 8035 | } |
| 8036 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8037 | /* |
| 8038 | * Software event: cpu wall time clock |
| 8039 | */ |
| 8040 | |
| 8041 | static void cpu_clock_event_update(struct perf_event *event) |
| 8042 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8043 | s64 prev; |
| 8044 | u64 now; |
| 8045 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8046 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8047 | prev = local64_xchg(&event->hw.prev_count, now); |
| 8048 | local64_add(now - prev, &event->count); |
| 8049 | } |
| 8050 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8051 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8052 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8053 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8054 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8055 | } |
| 8056 | |
| 8057 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 8058 | { |
| 8059 | perf_swevent_cancel_hrtimer(event); |
| 8060 | cpu_clock_event_update(event); |
| 8061 | } |
| 8062 | |
| 8063 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 8064 | { |
| 8065 | if (flags & PERF_EF_START) |
| 8066 | cpu_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 8067 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8068 | |
| 8069 | return 0; |
| 8070 | } |
| 8071 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8072 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8073 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8074 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8075 | } |
| 8076 | |
| 8077 | static void cpu_clock_event_read(struct perf_event *event) |
| 8078 | { |
| 8079 | cpu_clock_event_update(event); |
| 8080 | } |
| 8081 | |
| 8082 | static int cpu_clock_event_init(struct perf_event *event) |
| 8083 | { |
| 8084 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 8085 | return -ENOENT; |
| 8086 | |
| 8087 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 8088 | return -ENOENT; |
| 8089 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 8090 | /* |
| 8091 | * no branch sampling for software events |
| 8092 | */ |
| 8093 | if (has_branch_stack(event)) |
| 8094 | return -EOPNOTSUPP; |
| 8095 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8096 | perf_swevent_init_hrtimer(event); |
| 8097 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8098 | return 0; |
| 8099 | } |
| 8100 | |
| 8101 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8102 | .task_ctx_nr = perf_sw_context, |
| 8103 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8104 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 8105 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8106 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8107 | .add = cpu_clock_event_add, |
| 8108 | .del = cpu_clock_event_del, |
| 8109 | .start = cpu_clock_event_start, |
| 8110 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8111 | .read = cpu_clock_event_read, |
| 8112 | }; |
| 8113 | |
| 8114 | /* |
| 8115 | * Software event: task time clock |
| 8116 | */ |
| 8117 | |
| 8118 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 8119 | { |
| 8120 | u64 prev; |
| 8121 | s64 delta; |
| 8122 | |
| 8123 | prev = local64_xchg(&event->hw.prev_count, now); |
| 8124 | delta = now - prev; |
| 8125 | local64_add(delta, &event->count); |
| 8126 | } |
| 8127 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8128 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8129 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8130 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8131 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8132 | } |
| 8133 | |
| 8134 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 8135 | { |
| 8136 | perf_swevent_cancel_hrtimer(event); |
| 8137 | task_clock_event_update(event, event->ctx->time); |
| 8138 | } |
| 8139 | |
| 8140 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 8141 | { |
| 8142 | if (flags & PERF_EF_START) |
| 8143 | task_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 8144 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8145 | |
| 8146 | return 0; |
| 8147 | } |
| 8148 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8149 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8150 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8151 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8152 | } |
| 8153 | |
| 8154 | static void task_clock_event_read(struct perf_event *event) |
| 8155 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 8156 | u64 now = perf_clock(); |
| 8157 | u64 delta = now - event->ctx->timestamp; |
| 8158 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8159 | |
| 8160 | task_clock_event_update(event, time); |
| 8161 | } |
| 8162 | |
| 8163 | static int task_clock_event_init(struct perf_event *event) |
| 8164 | { |
| 8165 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 8166 | return -ENOENT; |
| 8167 | |
| 8168 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 8169 | return -ENOENT; |
| 8170 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 8171 | /* |
| 8172 | * no branch sampling for software events |
| 8173 | */ |
| 8174 | if (has_branch_stack(event)) |
| 8175 | return -EOPNOTSUPP; |
| 8176 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 8177 | perf_swevent_init_hrtimer(event); |
| 8178 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8179 | return 0; |
| 8180 | } |
| 8181 | |
| 8182 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8183 | .task_ctx_nr = perf_sw_context, |
| 8184 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8185 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 8186 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8187 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 8188 | .add = task_clock_event_add, |
| 8189 | .del = task_clock_event_del, |
| 8190 | .start = task_clock_event_start, |
| 8191 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8192 | .read = task_clock_event_read, |
| 8193 | }; |
| 8194 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8195 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 8196 | { |
| 8197 | } |
| 8198 | |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8199 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
| 8200 | { |
| 8201 | } |
| 8202 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8203 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 8204 | { |
| 8205 | return 0; |
| 8206 | } |
| 8207 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 8208 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8209 | |
| 8210 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8211 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8212 | __this_cpu_write(nop_txn_flags, flags); |
| 8213 | |
| 8214 | if (flags & ~PERF_PMU_TXN_ADD) |
| 8215 | return; |
| 8216 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8217 | perf_pmu_disable(pmu); |
| 8218 | } |
| 8219 | |
| 8220 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 8221 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8222 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
| 8223 | |
| 8224 | __this_cpu_write(nop_txn_flags, 0); |
| 8225 | |
| 8226 | if (flags & ~PERF_PMU_TXN_ADD) |
| 8227 | return 0; |
| 8228 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8229 | perf_pmu_enable(pmu); |
| 8230 | return 0; |
| 8231 | } |
| 8232 | |
| 8233 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 8234 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8235 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
| 8236 | |
| 8237 | __this_cpu_write(nop_txn_flags, 0); |
| 8238 | |
| 8239 | if (flags & ~PERF_PMU_TXN_ADD) |
| 8240 | return; |
| 8241 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8242 | perf_pmu_enable(pmu); |
| 8243 | } |
| 8244 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 8245 | static int perf_event_idx_default(struct perf_event *event) |
| 8246 | { |
Peter Zijlstra | c719f56 | 2014-10-21 11:10:21 +0200 | [diff] [blame] | 8247 | return 0; |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 8248 | } |
| 8249 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8250 | /* |
| 8251 | * Ensures all contexts with the same task_ctx_nr have the same |
| 8252 | * pmu_cpu_context too. |
| 8253 | */ |
Mark Rutland | 9e31704 | 2014-02-10 17:44:18 +0000 | [diff] [blame] | 8254 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8255 | { |
| 8256 | struct pmu *pmu; |
| 8257 | |
| 8258 | if (ctxn < 0) |
| 8259 | return NULL; |
| 8260 | |
| 8261 | list_for_each_entry(pmu, &pmus, entry) { |
| 8262 | if (pmu->task_ctx_nr == ctxn) |
| 8263 | return pmu->pmu_cpu_context; |
| 8264 | } |
| 8265 | |
| 8266 | return NULL; |
| 8267 | } |
| 8268 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8269 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8270 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8271 | int cpu; |
| 8272 | |
| 8273 | for_each_possible_cpu(cpu) { |
| 8274 | struct perf_cpu_context *cpuctx; |
| 8275 | |
| 8276 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 8277 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 8278 | if (cpuctx->unique_pmu == old_pmu) |
| 8279 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8280 | } |
| 8281 | } |
| 8282 | |
| 8283 | static void free_pmu_context(struct pmu *pmu) |
| 8284 | { |
| 8285 | struct pmu *i; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8286 | |
| 8287 | mutex_lock(&pmus_lock); |
| 8288 | /* |
| 8289 | * Like a real lame refcount. |
| 8290 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8291 | list_for_each_entry(i, &pmus, entry) { |
| 8292 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 8293 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8294 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8295 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8296 | } |
| 8297 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8298 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8299 | out: |
| 8300 | mutex_unlock(&pmus_lock); |
| 8301 | } |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8302 | |
| 8303 | /* |
| 8304 | * Let userspace know that this PMU supports address range filtering: |
| 8305 | */ |
| 8306 | static ssize_t nr_addr_filters_show(struct device *dev, |
| 8307 | struct device_attribute *attr, |
| 8308 | char *page) |
| 8309 | { |
| 8310 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8311 | |
| 8312 | return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); |
| 8313 | } |
| 8314 | DEVICE_ATTR_RO(nr_addr_filters); |
| 8315 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8316 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8317 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8318 | static ssize_t |
| 8319 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 8320 | { |
| 8321 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8322 | |
| 8323 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 8324 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8325 | static DEVICE_ATTR_RO(type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8326 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8327 | static ssize_t |
| 8328 | perf_event_mux_interval_ms_show(struct device *dev, |
| 8329 | struct device_attribute *attr, |
| 8330 | char *page) |
| 8331 | { |
| 8332 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8333 | |
| 8334 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 8335 | } |
| 8336 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8337 | static DEFINE_MUTEX(mux_interval_mutex); |
| 8338 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8339 | static ssize_t |
| 8340 | perf_event_mux_interval_ms_store(struct device *dev, |
| 8341 | struct device_attribute *attr, |
| 8342 | const char *buf, size_t count) |
| 8343 | { |
| 8344 | struct pmu *pmu = dev_get_drvdata(dev); |
| 8345 | int timer, cpu, ret; |
| 8346 | |
| 8347 | ret = kstrtoint(buf, 0, &timer); |
| 8348 | if (ret) |
| 8349 | return ret; |
| 8350 | |
| 8351 | if (timer < 1) |
| 8352 | return -EINVAL; |
| 8353 | |
| 8354 | /* same value, noting to do */ |
| 8355 | if (timer == pmu->hrtimer_interval_ms) |
| 8356 | return count; |
| 8357 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8358 | mutex_lock(&mux_interval_mutex); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8359 | pmu->hrtimer_interval_ms = timer; |
| 8360 | |
| 8361 | /* update all cpuctx for this PMU */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8362 | get_online_cpus(); |
| 8363 | for_each_online_cpu(cpu) { |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8364 | struct perf_cpu_context *cpuctx; |
| 8365 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 8366 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 8367 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8368 | cpu_function_call(cpu, |
| 8369 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8370 | } |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8371 | put_online_cpus(); |
| 8372 | mutex_unlock(&mux_interval_mutex); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8373 | |
| 8374 | return count; |
| 8375 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8376 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 8377 | |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8378 | static struct attribute *pmu_dev_attrs[] = { |
| 8379 | &dev_attr_type.attr, |
| 8380 | &dev_attr_perf_event_mux_interval_ms.attr, |
| 8381 | NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8382 | }; |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8383 | ATTRIBUTE_GROUPS(pmu_dev); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8384 | |
| 8385 | static int pmu_bus_running; |
| 8386 | static struct bus_type pmu_bus = { |
| 8387 | .name = "event_source", |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 8388 | .dev_groups = pmu_dev_groups, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8389 | }; |
| 8390 | |
| 8391 | static void pmu_dev_release(struct device *dev) |
| 8392 | { |
| 8393 | kfree(dev); |
| 8394 | } |
| 8395 | |
| 8396 | static int pmu_dev_alloc(struct pmu *pmu) |
| 8397 | { |
| 8398 | int ret = -ENOMEM; |
| 8399 | |
| 8400 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 8401 | if (!pmu->dev) |
| 8402 | goto out; |
| 8403 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 8404 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8405 | device_initialize(pmu->dev); |
| 8406 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 8407 | if (ret) |
| 8408 | goto free_dev; |
| 8409 | |
| 8410 | dev_set_drvdata(pmu->dev, pmu); |
| 8411 | pmu->dev->bus = &pmu_bus; |
| 8412 | pmu->dev->release = pmu_dev_release; |
| 8413 | ret = device_add(pmu->dev); |
| 8414 | if (ret) |
| 8415 | goto free_dev; |
| 8416 | |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8417 | /* For PMUs with address filters, throw in an extra attribute: */ |
| 8418 | if (pmu->nr_addr_filters) |
| 8419 | ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters); |
| 8420 | |
| 8421 | if (ret) |
| 8422 | goto del_dev; |
| 8423 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8424 | out: |
| 8425 | return ret; |
| 8426 | |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8427 | del_dev: |
| 8428 | device_del(pmu->dev); |
| 8429 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8430 | free_dev: |
| 8431 | put_device(pmu->dev); |
| 8432 | goto out; |
| 8433 | } |
| 8434 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 8435 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 8436 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 8437 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 8438 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8439 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8440 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8441 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8442 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8443 | ret = -ENOMEM; |
| 8444 | pmu->pmu_disable_count = alloc_percpu(int); |
| 8445 | if (!pmu->pmu_disable_count) |
| 8446 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8447 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8448 | pmu->type = -1; |
| 8449 | if (!name) |
| 8450 | goto skip_type; |
| 8451 | pmu->name = name; |
| 8452 | |
| 8453 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 8454 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 8455 | if (type < 0) { |
| 8456 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8457 | goto free_pdc; |
| 8458 | } |
| 8459 | } |
| 8460 | pmu->type = type; |
| 8461 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8462 | if (pmu_bus_running) { |
| 8463 | ret = pmu_dev_alloc(pmu); |
| 8464 | if (ret) |
| 8465 | goto free_idr; |
| 8466 | } |
| 8467 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8468 | skip_type: |
Peter Zijlstra | 2665784 | 2016-03-22 22:09:18 +0100 | [diff] [blame] | 8469 | if (pmu->task_ctx_nr == perf_hw_context) { |
| 8470 | static int hw_context_taken = 0; |
| 8471 | |
Mark Rutland | 5101ef2 | 2016-04-26 11:33:46 +0100 | [diff] [blame] | 8472 | /* |
| 8473 | * Other than systems with heterogeneous CPUs, it never makes |
| 8474 | * sense for two PMUs to share perf_hw_context. PMUs which are |
| 8475 | * uncore must use perf_invalid_context. |
| 8476 | */ |
| 8477 | if (WARN_ON_ONCE(hw_context_taken && |
| 8478 | !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS))) |
Peter Zijlstra | 2665784 | 2016-03-22 22:09:18 +0100 | [diff] [blame] | 8479 | pmu->task_ctx_nr = perf_invalid_context; |
| 8480 | |
| 8481 | hw_context_taken = 1; |
| 8482 | } |
| 8483 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8484 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 8485 | if (pmu->pmu_cpu_context) |
| 8486 | goto got_cpu_context; |
| 8487 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 8488 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8489 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 8490 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8491 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8492 | |
| 8493 | for_each_possible_cpu(cpu) { |
| 8494 | struct perf_cpu_context *cpuctx; |
| 8495 | |
| 8496 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 8497 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 8498 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 8499 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8500 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 8501 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 8502 | __perf_mux_hrtimer_init(cpuctx, cpu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 8503 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 8504 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8505 | } |
| 8506 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8507 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8508 | if (!pmu->start_txn) { |
| 8509 | if (pmu->pmu_enable) { |
| 8510 | /* |
| 8511 | * If we have pmu_enable/pmu_disable calls, install |
| 8512 | * transaction stubs that use that to try and batch |
| 8513 | * hardware accesses. |
| 8514 | */ |
| 8515 | pmu->start_txn = perf_pmu_start_txn; |
| 8516 | pmu->commit_txn = perf_pmu_commit_txn; |
| 8517 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 8518 | } else { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 8519 | pmu->start_txn = perf_pmu_nop_txn; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 8520 | pmu->commit_txn = perf_pmu_nop_int; |
| 8521 | pmu->cancel_txn = perf_pmu_nop_void; |
| 8522 | } |
| 8523 | } |
| 8524 | |
| 8525 | if (!pmu->pmu_enable) { |
| 8526 | pmu->pmu_enable = perf_pmu_nop_void; |
| 8527 | pmu->pmu_disable = perf_pmu_nop_void; |
| 8528 | } |
| 8529 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 8530 | if (!pmu->event_idx) |
| 8531 | pmu->event_idx = perf_event_idx_default; |
| 8532 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8533 | list_add_rcu(&pmu->entry, &pmus); |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8534 | atomic_set(&pmu->exclusive_cnt, 0); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8535 | ret = 0; |
| 8536 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8537 | mutex_unlock(&pmus_lock); |
| 8538 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8539 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8540 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8541 | free_dev: |
| 8542 | device_del(pmu->dev); |
| 8543 | put_device(pmu->dev); |
| 8544 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8545 | free_idr: |
| 8546 | if (pmu->type >= PERF_TYPE_MAX) |
| 8547 | idr_remove(&pmu_idr, pmu->type); |
| 8548 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 8549 | free_pdc: |
| 8550 | free_percpu(pmu->pmu_disable_count); |
| 8551 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8552 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 8553 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8554 | |
| 8555 | void perf_pmu_unregister(struct pmu *pmu) |
| 8556 | { |
| 8557 | mutex_lock(&pmus_lock); |
| 8558 | list_del_rcu(&pmu->entry); |
| 8559 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8560 | |
| 8561 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 8562 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 8563 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8564 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8565 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 8566 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8567 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 8568 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8569 | if (pmu->type >= PERF_TYPE_MAX) |
| 8570 | idr_remove(&pmu_idr, pmu->type); |
Alexander Shishkin | 6e855cd | 2016-04-27 18:44:48 +0300 | [diff] [blame] | 8571 | if (pmu->nr_addr_filters) |
| 8572 | device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 8573 | device_del(pmu->dev); |
| 8574 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 8575 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8576 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 8577 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8578 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8579 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
| 8580 | { |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8581 | struct perf_event_context *ctx = NULL; |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8582 | int ret; |
| 8583 | |
| 8584 | if (!try_module_get(pmu->module)) |
| 8585 | return -ENODEV; |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8586 | |
| 8587 | if (event->group_leader != event) { |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 8588 | /* |
| 8589 | * This ctx->mutex can nest when we're called through |
| 8590 | * inheritance. See the perf_event_ctx_lock_nested() comment. |
| 8591 | */ |
| 8592 | ctx = perf_event_ctx_lock_nested(event->group_leader, |
| 8593 | SINGLE_DEPTH_NESTING); |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8594 | BUG_ON(!ctx); |
| 8595 | } |
| 8596 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8597 | event->pmu = pmu; |
| 8598 | ret = pmu->event_init(event); |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 8599 | |
| 8600 | if (ctx) |
| 8601 | perf_event_ctx_unlock(event->group_leader, ctx); |
| 8602 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8603 | if (ret) |
| 8604 | module_put(pmu->module); |
| 8605 | |
| 8606 | return ret; |
| 8607 | } |
| 8608 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 8609 | static struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8610 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 8611 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8612 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 8613 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8614 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8615 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8616 | |
| 8617 | rcu_read_lock(); |
| 8618 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 8619 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 8620 | if (pmu) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8621 | ret = perf_try_init_event(pmu, event); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 8622 | if (ret) |
| 8623 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8624 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 8625 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 8626 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8627 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 8628 | ret = perf_try_init_event(pmu, event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8629 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 8630 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 8631 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8632 | if (ret != -ENOENT) { |
| 8633 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 8634 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8635 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8636 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 8637 | pmu = ERR_PTR(-ENOENT); |
| 8638 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8639 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8640 | |
| 8641 | return pmu; |
| 8642 | } |
| 8643 | |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 8644 | static void attach_sb_event(struct perf_event *event) |
| 8645 | { |
| 8646 | struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); |
| 8647 | |
| 8648 | raw_spin_lock(&pel->lock); |
| 8649 | list_add_rcu(&event->sb_list, &pel->list); |
| 8650 | raw_spin_unlock(&pel->lock); |
| 8651 | } |
| 8652 | |
Peter Zijlstra | aab5b71 | 2016-05-12 17:26:46 +0200 | [diff] [blame^] | 8653 | /* |
| 8654 | * We keep a list of all !task (and therefore per-cpu) events |
| 8655 | * that need to receive side-band records. |
| 8656 | * |
| 8657 | * This avoids having to scan all the various PMU per-cpu contexts |
| 8658 | * looking for them. |
| 8659 | */ |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 8660 | static void account_pmu_sb_event(struct perf_event *event) |
| 8661 | { |
| 8662 | struct perf_event_attr *attr = &event->attr; |
| 8663 | |
| 8664 | if (event->parent) |
| 8665 | return; |
| 8666 | |
| 8667 | if (event->attach_state & PERF_ATTACH_TASK) |
| 8668 | return; |
| 8669 | |
| 8670 | if (attr->mmap || attr->mmap_data || attr->mmap2 || |
| 8671 | attr->comm || attr->comm_exec || |
| 8672 | attr->task || |
| 8673 | attr->context_switch) |
| 8674 | attach_sb_event(event); |
| 8675 | } |
| 8676 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8677 | static void account_event_cpu(struct perf_event *event, int cpu) |
| 8678 | { |
| 8679 | if (event->parent) |
| 8680 | return; |
| 8681 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8682 | if (is_cgroup_event(event)) |
| 8683 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); |
| 8684 | } |
| 8685 | |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 8686 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
| 8687 | static void account_freq_event_nohz(void) |
| 8688 | { |
| 8689 | #ifdef CONFIG_NO_HZ_FULL |
| 8690 | /* Lock so we don't race with concurrent unaccount */ |
| 8691 | spin_lock(&nr_freq_lock); |
| 8692 | if (atomic_inc_return(&nr_freq_events) == 1) |
| 8693 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); |
| 8694 | spin_unlock(&nr_freq_lock); |
| 8695 | #endif |
| 8696 | } |
| 8697 | |
| 8698 | static void account_freq_event(void) |
| 8699 | { |
| 8700 | if (tick_nohz_full_enabled()) |
| 8701 | account_freq_event_nohz(); |
| 8702 | else |
| 8703 | atomic_inc(&nr_freq_events); |
| 8704 | } |
| 8705 | |
| 8706 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8707 | static void account_event(struct perf_event *event) |
| 8708 | { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8709 | bool inc = false; |
| 8710 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8711 | if (event->parent) |
| 8712 | return; |
| 8713 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8714 | if (event->attach_state & PERF_ATTACH_TASK) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8715 | inc = true; |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8716 | if (event->attr.mmap || event->attr.mmap_data) |
| 8717 | atomic_inc(&nr_mmap_events); |
| 8718 | if (event->attr.comm) |
| 8719 | atomic_inc(&nr_comm_events); |
| 8720 | if (event->attr.task) |
| 8721 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 8722 | if (event->attr.freq) |
| 8723 | account_freq_event(); |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 8724 | if (event->attr.context_switch) { |
| 8725 | atomic_inc(&nr_switch_events); |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8726 | inc = true; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 8727 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8728 | if (has_branch_stack(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8729 | inc = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8730 | if (is_cgroup_event(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 8731 | inc = true; |
| 8732 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 8733 | if (inc) { |
| 8734 | if (atomic_inc_not_zero(&perf_sched_count)) |
| 8735 | goto enabled; |
| 8736 | |
| 8737 | mutex_lock(&perf_sched_mutex); |
| 8738 | if (!atomic_read(&perf_sched_count)) { |
| 8739 | static_branch_enable(&perf_sched_events); |
| 8740 | /* |
| 8741 | * Guarantee that all CPUs observe they key change and |
| 8742 | * call the perf scheduling hooks before proceeding to |
| 8743 | * install events that need them. |
| 8744 | */ |
| 8745 | synchronize_sched(); |
| 8746 | } |
| 8747 | /* |
| 8748 | * Now that we have waited for the sync_sched(), allow further |
| 8749 | * increments to by-pass the mutex. |
| 8750 | */ |
| 8751 | atomic_inc(&perf_sched_count); |
| 8752 | mutex_unlock(&perf_sched_mutex); |
| 8753 | } |
| 8754 | enabled: |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8755 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 8756 | account_event_cpu(event, event->cpu); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 8757 | |
| 8758 | account_pmu_sb_event(event); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 8759 | } |
| 8760 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8761 | /* |
| 8762 | * Allocate and initialize a event structure |
| 8763 | */ |
| 8764 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8765 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8766 | struct task_struct *task, |
| 8767 | struct perf_event *group_leader, |
| 8768 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8769 | perf_overflow_handler_t overflow_handler, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8770 | void *context, int cgroup_fd) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8771 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 8772 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8773 | struct perf_event *event; |
| 8774 | struct hw_perf_event *hwc; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8775 | long err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8776 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 8777 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 8778 | if (!task || cpu != -1) |
| 8779 | return ERR_PTR(-EINVAL); |
| 8780 | } |
| 8781 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8782 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8783 | if (!event) |
| 8784 | return ERR_PTR(-ENOMEM); |
| 8785 | |
| 8786 | /* |
| 8787 | * Single events are their own group leaders, with an |
| 8788 | * empty sibling list: |
| 8789 | */ |
| 8790 | if (!group_leader) |
| 8791 | group_leader = event; |
| 8792 | |
| 8793 | mutex_init(&event->child_mutex); |
| 8794 | INIT_LIST_HEAD(&event->child_list); |
| 8795 | |
| 8796 | INIT_LIST_HEAD(&event->group_entry); |
| 8797 | INIT_LIST_HEAD(&event->event_entry); |
| 8798 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 8799 | INIT_LIST_HEAD(&event->rb_entry); |
Stephane Eranian | 71ad88e | 2013-11-12 17:58:48 +0100 | [diff] [blame] | 8800 | INIT_LIST_HEAD(&event->active_entry); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8801 | INIT_LIST_HEAD(&event->addr_filters.list); |
Stephane Eranian | f3ae75d | 2014-01-08 11:15:52 +0100 | [diff] [blame] | 8802 | INIT_HLIST_NODE(&event->hlist_entry); |
| 8803 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 8804 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8805 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 8806 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8807 | |
| 8808 | mutex_init(&event->mmap_mutex); |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8809 | raw_spin_lock_init(&event->addr_filters.lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8810 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 8811 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8812 | event->cpu = cpu; |
| 8813 | event->attr = *attr; |
| 8814 | event->group_leader = group_leader; |
| 8815 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8816 | event->oncpu = -1; |
| 8817 | |
| 8818 | event->parent = parent_event; |
| 8819 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 8820 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8821 | event->id = atomic64_inc_return(&perf_event_id); |
| 8822 | |
| 8823 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 8824 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8825 | if (task) { |
| 8826 | event->attach_state = PERF_ATTACH_TASK; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8827 | /* |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 8828 | * XXX pmu::event_init needs to know what task to account to |
| 8829 | * and we cannot use the ctx information because we need the |
| 8830 | * pmu before we get a ctx. |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8831 | */ |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 8832 | event->hw.target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 8833 | } |
| 8834 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8835 | event->clock = &local_clock; |
| 8836 | if (parent_event) |
| 8837 | event->clock = parent_event->clock; |
| 8838 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8839 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 8840 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8841 | context = parent_event->overflow_handler_context; |
| 8842 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 8843 | |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 8844 | if (overflow_handler) { |
| 8845 | event->overflow_handler = overflow_handler; |
| 8846 | event->overflow_handler_context = context; |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 8847 | } else if (is_write_backward(event)){ |
| 8848 | event->overflow_handler = perf_event_output_backward; |
| 8849 | event->overflow_handler_context = NULL; |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 8850 | } else { |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 8851 | event->overflow_handler = perf_event_output_forward; |
Wang Nan | 1879445 | 2016-03-28 06:41:30 +0000 | [diff] [blame] | 8852 | event->overflow_handler_context = NULL; |
| 8853 | } |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 8854 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 8855 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8856 | |
| 8857 | pmu = NULL; |
| 8858 | |
| 8859 | hwc = &event->hw; |
| 8860 | hwc->sample_period = attr->sample_period; |
| 8861 | if (attr->freq && attr->sample_freq) |
| 8862 | hwc->sample_period = 1; |
| 8863 | hwc->last_period = hwc->sample_period; |
| 8864 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 8865 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8866 | |
| 8867 | /* |
| 8868 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 8869 | */ |
| 8870 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8871 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8872 | |
Yan, Zheng | a46a230 | 2014-11-04 21:56:06 -0500 | [diff] [blame] | 8873 | if (!has_branch_stack(event)) |
| 8874 | event->attr.branch_sample_type = 0; |
| 8875 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8876 | if (cgroup_fd != -1) { |
| 8877 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); |
| 8878 | if (err) |
| 8879 | goto err_ns; |
| 8880 | } |
| 8881 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8882 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8883 | if (!pmu) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8884 | goto err_ns; |
| 8885 | else if (IS_ERR(pmu)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8886 | err = PTR_ERR(pmu); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8887 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8888 | } |
| 8889 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8890 | err = exclusive_event_init(event); |
| 8891 | if (err) |
| 8892 | goto err_pmu; |
| 8893 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8894 | if (has_addr_filter(event)) { |
| 8895 | event->addr_filters_offs = kcalloc(pmu->nr_addr_filters, |
| 8896 | sizeof(unsigned long), |
| 8897 | GFP_KERNEL); |
| 8898 | if (!event->addr_filters_offs) |
| 8899 | goto err_per_task; |
| 8900 | |
| 8901 | /* force hw sync on the address filters */ |
| 8902 | event->addr_filters_gen = 1; |
| 8903 | } |
| 8904 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8905 | if (!event->parent) { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 8906 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
Arnaldo Carvalho de Melo | 97c79a3 | 2016-04-28 13:16:33 -0300 | [diff] [blame] | 8907 | err = get_callchain_buffers(attr->sample_max_stack); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8908 | if (err) |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8909 | goto err_addr_filters; |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 8910 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8911 | } |
| 8912 | |
Alexander Shishkin | 927a557 | 2016-03-02 13:24:14 +0200 | [diff] [blame] | 8913 | /* symmetric to unaccount_event() in _free_event() */ |
| 8914 | account_event(event); |
| 8915 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8916 | return event; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8917 | |
Alexander Shishkin | 375637b | 2016-04-27 18:44:46 +0300 | [diff] [blame] | 8918 | err_addr_filters: |
| 8919 | kfree(event->addr_filters_offs); |
| 8920 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8921 | err_per_task: |
| 8922 | exclusive_event_destroy(event); |
| 8923 | |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8924 | err_pmu: |
| 8925 | if (event->destroy) |
| 8926 | event->destroy(event); |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 8927 | module_put(pmu->module); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8928 | err_ns: |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8929 | if (is_cgroup_event(event)) |
| 8930 | perf_detach_cgroup(event); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8931 | if (event->ns) |
| 8932 | put_pid_ns(event->ns); |
| 8933 | kfree(event); |
| 8934 | |
| 8935 | return ERR_PTR(err); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8936 | } |
| 8937 | |
| 8938 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 8939 | struct perf_event_attr *attr) |
| 8940 | { |
| 8941 | u32 size; |
| 8942 | int ret; |
| 8943 | |
| 8944 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 8945 | return -EFAULT; |
| 8946 | |
| 8947 | /* |
| 8948 | * zero the full structure, so that a short copy will be nice. |
| 8949 | */ |
| 8950 | memset(attr, 0, sizeof(*attr)); |
| 8951 | |
| 8952 | ret = get_user(size, &uattr->size); |
| 8953 | if (ret) |
| 8954 | return ret; |
| 8955 | |
| 8956 | if (size > PAGE_SIZE) /* silly large */ |
| 8957 | goto err_size; |
| 8958 | |
| 8959 | if (!size) /* abi compat */ |
| 8960 | size = PERF_ATTR_SIZE_VER0; |
| 8961 | |
| 8962 | if (size < PERF_ATTR_SIZE_VER0) |
| 8963 | goto err_size; |
| 8964 | |
| 8965 | /* |
| 8966 | * If we're handed a bigger struct than we know of, |
| 8967 | * ensure all the unknown bits are 0 - i.e. new |
| 8968 | * user-space does not rely on any kernel feature |
| 8969 | * extensions we dont know about yet. |
| 8970 | */ |
| 8971 | if (size > sizeof(*attr)) { |
| 8972 | unsigned char __user *addr; |
| 8973 | unsigned char __user *end; |
| 8974 | unsigned char val; |
| 8975 | |
| 8976 | addr = (void __user *)uattr + sizeof(*attr); |
| 8977 | end = (void __user *)uattr + size; |
| 8978 | |
| 8979 | for (; addr < end; addr++) { |
| 8980 | ret = get_user(val, addr); |
| 8981 | if (ret) |
| 8982 | return ret; |
| 8983 | if (val) |
| 8984 | goto err_size; |
| 8985 | } |
| 8986 | size = sizeof(*attr); |
| 8987 | } |
| 8988 | |
| 8989 | ret = copy_from_user(attr, uattr, size); |
| 8990 | if (ret) |
| 8991 | return -EFAULT; |
| 8992 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 8993 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8994 | return -EINVAL; |
| 8995 | |
| 8996 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 8997 | return -EINVAL; |
| 8998 | |
| 8999 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 9000 | return -EINVAL; |
| 9001 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 9002 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 9003 | u64 mask = attr->branch_sample_type; |
| 9004 | |
| 9005 | /* only using defined bits */ |
| 9006 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 9007 | return -EINVAL; |
| 9008 | |
| 9009 | /* at least one branch bit must be set */ |
| 9010 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 9011 | return -EINVAL; |
| 9012 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 9013 | /* propagate priv level, when not set for branch */ |
| 9014 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 9015 | |
| 9016 | /* exclude_kernel checked on syscall entry */ |
| 9017 | if (!attr->exclude_kernel) |
| 9018 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 9019 | |
| 9020 | if (!attr->exclude_user) |
| 9021 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 9022 | |
| 9023 | if (!attr->exclude_hv) |
| 9024 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 9025 | /* |
| 9026 | * adjust user setting (for HW filter setup) |
| 9027 | */ |
| 9028 | attr->branch_sample_type = mask; |
| 9029 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 9030 | /* privileged levels capture (kernel, hv): check permissions */ |
| 9031 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 9032 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 9033 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 9034 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 9035 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 9036 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 9037 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 9038 | if (ret) |
| 9039 | return ret; |
| 9040 | } |
| 9041 | |
| 9042 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 9043 | if (!arch_perf_have_user_stack_dump()) |
| 9044 | return -ENOSYS; |
| 9045 | |
| 9046 | /* |
| 9047 | * We have __u32 type for the size, but so far |
| 9048 | * we can only use __u16 as maximum due to the |
| 9049 | * __u16 sample size limit. |
| 9050 | */ |
| 9051 | if (attr->sample_stack_user >= USHRT_MAX) |
| 9052 | ret = -EINVAL; |
| 9053 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
| 9054 | ret = -EINVAL; |
| 9055 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 9056 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 9057 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
| 9058 | ret = perf_reg_validate(attr->sample_regs_intr); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9059 | out: |
| 9060 | return ret; |
| 9061 | |
| 9062 | err_size: |
| 9063 | put_user(sizeof(*attr), &uattr->size); |
| 9064 | ret = -E2BIG; |
| 9065 | goto out; |
| 9066 | } |
| 9067 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9068 | static int |
| 9069 | 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] | 9070 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 9071 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9072 | int ret = -EINVAL; |
| 9073 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9074 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9075 | goto set; |
| 9076 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9077 | /* don't allow circular references */ |
| 9078 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9079 | goto out; |
| 9080 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 9081 | /* |
| 9082 | * Don't allow cross-cpu buffers |
| 9083 | */ |
| 9084 | if (output_event->cpu != event->cpu) |
| 9085 | goto out; |
| 9086 | |
| 9087 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 9088 | * 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] | 9089 | */ |
| 9090 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 9091 | goto out; |
| 9092 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9093 | /* |
| 9094 | * Mixing clocks in the same buffer is trouble you don't need. |
| 9095 | */ |
| 9096 | if (output_event->clock != event->clock) |
| 9097 | goto out; |
| 9098 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 9099 | /* |
Wang Nan | 9ecda41 | 2016-04-05 14:11:18 +0000 | [diff] [blame] | 9100 | * Either writing ring buffer from beginning or from end. |
| 9101 | * Mixing is not allowed. |
| 9102 | */ |
| 9103 | if (is_write_backward(output_event) != is_write_backward(event)) |
| 9104 | goto out; |
| 9105 | |
| 9106 | /* |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 9107 | * If both events generate aux data, they must be on the same PMU |
| 9108 | */ |
| 9109 | if (has_aux(event) && has_aux(output_event) && |
| 9110 | event->pmu != output_event->pmu) |
| 9111 | goto out; |
| 9112 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9113 | set: |
| 9114 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9115 | /* Can't redirect output if we've got an active mmap() */ |
| 9116 | if (atomic_read(&event->mmap_count)) |
| 9117 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9118 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9119 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 9120 | /* get the rb we want to redirect to */ |
| 9121 | rb = ring_buffer_get(output_event); |
| 9122 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9123 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9124 | } |
| 9125 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 9126 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 9127 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9128 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9129 | unlock: |
| 9130 | mutex_unlock(&event->mmap_mutex); |
| 9131 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9132 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9133 | return ret; |
| 9134 | } |
| 9135 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9136 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
| 9137 | { |
| 9138 | if (b < a) |
| 9139 | swap(a, b); |
| 9140 | |
| 9141 | mutex_lock(a); |
| 9142 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); |
| 9143 | } |
| 9144 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9145 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
| 9146 | { |
| 9147 | bool nmi_safe = false; |
| 9148 | |
| 9149 | switch (clk_id) { |
| 9150 | case CLOCK_MONOTONIC: |
| 9151 | event->clock = &ktime_get_mono_fast_ns; |
| 9152 | nmi_safe = true; |
| 9153 | break; |
| 9154 | |
| 9155 | case CLOCK_MONOTONIC_RAW: |
| 9156 | event->clock = &ktime_get_raw_fast_ns; |
| 9157 | nmi_safe = true; |
| 9158 | break; |
| 9159 | |
| 9160 | case CLOCK_REALTIME: |
| 9161 | event->clock = &ktime_get_real_ns; |
| 9162 | break; |
| 9163 | |
| 9164 | case CLOCK_BOOTTIME: |
| 9165 | event->clock = &ktime_get_boot_ns; |
| 9166 | break; |
| 9167 | |
| 9168 | case CLOCK_TAI: |
| 9169 | event->clock = &ktime_get_tai_ns; |
| 9170 | break; |
| 9171 | |
| 9172 | default: |
| 9173 | return -EINVAL; |
| 9174 | } |
| 9175 | |
| 9176 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) |
| 9177 | return -EINVAL; |
| 9178 | |
| 9179 | return 0; |
| 9180 | } |
| 9181 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9182 | /** |
| 9183 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 9184 | * |
| 9185 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 9186 | * @pid: target pid |
| 9187 | * @cpu: target cpu |
| 9188 | * @group_fd: group leader event fd |
| 9189 | */ |
| 9190 | SYSCALL_DEFINE5(perf_event_open, |
| 9191 | struct perf_event_attr __user *, attr_uptr, |
| 9192 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 9193 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9194 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 9195 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9196 | struct perf_event_attr attr; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9197 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9198 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9199 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 9200 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9201 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9202 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9203 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9204 | int err; |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 9205 | int f_flags = O_RDWR; |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9206 | int cgroup_fd = -1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9207 | |
| 9208 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9209 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9210 | return -EINVAL; |
| 9211 | |
| 9212 | err = perf_copy_attr(attr_uptr, &attr); |
| 9213 | if (err) |
| 9214 | return err; |
| 9215 | |
| 9216 | if (!attr.exclude_kernel) { |
| 9217 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 9218 | return -EACCES; |
| 9219 | } |
| 9220 | |
| 9221 | if (attr.freq) { |
| 9222 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 9223 | return -EINVAL; |
Peter Zijlstra | 0819b2e | 2014-05-15 20:23:48 +0200 | [diff] [blame] | 9224 | } else { |
| 9225 | if (attr.sample_period & (1ULL << 63)) |
| 9226 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9227 | } |
| 9228 | |
Arnaldo Carvalho de Melo | 97c79a3 | 2016-04-28 13:16:33 -0300 | [diff] [blame] | 9229 | if (!attr.sample_max_stack) |
| 9230 | attr.sample_max_stack = sysctl_perf_event_max_stack; |
| 9231 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9232 | /* |
| 9233 | * In cgroup mode, the pid argument is used to pass the fd |
| 9234 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 9235 | * designates the cpu on which to monitor threads from that |
| 9236 | * cgroup. |
| 9237 | */ |
| 9238 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 9239 | return -EINVAL; |
| 9240 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 9241 | if (flags & PERF_FLAG_FD_CLOEXEC) |
| 9242 | f_flags |= O_CLOEXEC; |
| 9243 | |
| 9244 | event_fd = get_unused_fd_flags(f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9245 | if (event_fd < 0) |
| 9246 | return event_fd; |
| 9247 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9248 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9249 | err = perf_fget_light(group_fd, &group); |
| 9250 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 9251 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9252 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9253 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 9254 | output_event = group_leader; |
| 9255 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 9256 | group_leader = NULL; |
| 9257 | } |
| 9258 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9259 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 9260 | task = find_lively_task_by_vpid(pid); |
| 9261 | if (IS_ERR(task)) { |
| 9262 | err = PTR_ERR(task); |
| 9263 | goto err_group_fd; |
| 9264 | } |
| 9265 | } |
| 9266 | |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 9267 | if (task && group_leader && |
| 9268 | group_leader->attr.inherit != attr.inherit) { |
| 9269 | err = -EINVAL; |
| 9270 | goto err_task; |
| 9271 | } |
| 9272 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 9273 | get_online_cpus(); |
| 9274 | |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9275 | if (task) { |
| 9276 | err = mutex_lock_interruptible(&task->signal->cred_guard_mutex); |
| 9277 | if (err) |
| 9278 | goto err_cpus; |
| 9279 | |
| 9280 | /* |
| 9281 | * Reuse ptrace permission checks for now. |
| 9282 | * |
| 9283 | * We must hold cred_guard_mutex across this and any potential |
| 9284 | * perf_install_in_context() call for this new event to |
| 9285 | * serialize against exec() altering our credentials (and the |
| 9286 | * perf_event_exit_task() that could imply). |
| 9287 | */ |
| 9288 | err = -EACCES; |
| 9289 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) |
| 9290 | goto err_cred; |
| 9291 | } |
| 9292 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9293 | if (flags & PERF_FLAG_PID_CGROUP) |
| 9294 | cgroup_fd = pid; |
| 9295 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9296 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9297 | NULL, NULL, cgroup_fd); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 9298 | if (IS_ERR(event)) { |
| 9299 | err = PTR_ERR(event); |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9300 | goto err_cred; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 9301 | } |
| 9302 | |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 9303 | if (is_sampling_event(event)) { |
| 9304 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { |
| 9305 | err = -ENOTSUPP; |
| 9306 | goto err_alloc; |
| 9307 | } |
| 9308 | } |
| 9309 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9310 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9311 | * Special case software events and allow them to be part of |
| 9312 | * any hardware group. |
| 9313 | */ |
| 9314 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9315 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9316 | if (attr.use_clockid) { |
| 9317 | err = perf_event_set_clock(event, attr.clockid); |
| 9318 | if (err) |
| 9319 | goto err_alloc; |
| 9320 | } |
| 9321 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9322 | if (group_leader && |
| 9323 | (is_software_event(event) != is_software_event(group_leader))) { |
| 9324 | if (is_software_event(event)) { |
| 9325 | /* |
| 9326 | * If event and group_leader are not both a software |
| 9327 | * event, and event is, then group leader is not. |
| 9328 | * |
| 9329 | * Allow the addition of software events to !software |
| 9330 | * groups, this is safe because software events never |
| 9331 | * fail to schedule. |
| 9332 | */ |
| 9333 | pmu = group_leader->pmu; |
| 9334 | } else if (is_software_event(group_leader) && |
| 9335 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 9336 | /* |
| 9337 | * In case the group is a pure software group, and we |
| 9338 | * try to add a hardware event, move the whole group to |
| 9339 | * the hardware context. |
| 9340 | */ |
| 9341 | move_group = 1; |
| 9342 | } |
| 9343 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9344 | |
| 9345 | /* |
| 9346 | * Get the target context (task or percpu): |
| 9347 | */ |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 9348 | ctx = find_get_context(pmu, task, event); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9349 | if (IS_ERR(ctx)) { |
| 9350 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 9351 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9352 | } |
| 9353 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9354 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
| 9355 | err = -EBUSY; |
| 9356 | goto err_context; |
| 9357 | } |
| 9358 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9359 | /* |
| 9360 | * Look up the group leader (we will attach this event to it): |
| 9361 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9362 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9363 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9364 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9365 | /* |
| 9366 | * Do not allow a recursive hierarchy (this new sibling |
| 9367 | * becoming part of another group-sibling): |
| 9368 | */ |
| 9369 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9370 | goto err_context; |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 9371 | |
| 9372 | /* All events in a group should have the same clock */ |
| 9373 | if (group_leader->clock != event->clock) |
| 9374 | goto err_context; |
| 9375 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9376 | /* |
| 9377 | * Do not allow to attach to a group in a different |
| 9378 | * task or CPU context: |
| 9379 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9380 | if (move_group) { |
Peter Zijlstra | c3c87e7 | 2015-01-23 11:19:48 +0100 | [diff] [blame] | 9381 | /* |
| 9382 | * Make sure we're both on the same task, or both |
| 9383 | * per-cpu events. |
| 9384 | */ |
| 9385 | if (group_leader->ctx->task != ctx->task) |
| 9386 | goto err_context; |
| 9387 | |
| 9388 | /* |
| 9389 | * Make sure we're both events for the same CPU; |
| 9390 | * grouping events for different CPUs is broken; since |
| 9391 | * you can never concurrently schedule them anyhow. |
| 9392 | */ |
| 9393 | if (group_leader->cpu != event->cpu) |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9394 | goto err_context; |
| 9395 | } else { |
| 9396 | if (group_leader->ctx != ctx) |
| 9397 | goto err_context; |
| 9398 | } |
| 9399 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9400 | /* |
| 9401 | * Only a group leader can be exclusive or pinned |
| 9402 | */ |
| 9403 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9404 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9405 | } |
| 9406 | |
| 9407 | if (output_event) { |
| 9408 | err = perf_event_set_output(event, output_event); |
| 9409 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9410 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 9411 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9412 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 9413 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
| 9414 | f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9415 | if (IS_ERR(event_file)) { |
| 9416 | err = PTR_ERR(event_file); |
Alexander Shishkin | 201c2f8 | 2016-03-21 10:02:42 +0200 | [diff] [blame] | 9417 | event_file = NULL; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9418 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9419 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9420 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9421 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9422 | gctx = group_leader->ctx; |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9423 | mutex_lock_double(&gctx->mutex, &ctx->mutex); |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9424 | if (gctx->task == TASK_TOMBSTONE) { |
| 9425 | err = -ESRCH; |
| 9426 | goto err_locked; |
| 9427 | } |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9428 | } else { |
| 9429 | mutex_lock(&ctx->mutex); |
| 9430 | } |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9431 | |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9432 | if (ctx->task == TASK_TOMBSTONE) { |
| 9433 | err = -ESRCH; |
| 9434 | goto err_locked; |
| 9435 | } |
| 9436 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9437 | if (!perf_event_validate_size(event)) { |
| 9438 | err = -E2BIG; |
| 9439 | goto err_locked; |
| 9440 | } |
| 9441 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9442 | /* |
| 9443 | * Must be under the same ctx::mutex as perf_install_in_context(), |
| 9444 | * because we need to serialize with concurrent event creation. |
| 9445 | */ |
| 9446 | if (!exclusive_event_installable(event, ctx)) { |
| 9447 | /* exclusive and group stuff are assumed mutually exclusive */ |
| 9448 | WARN_ON_ONCE(move_group); |
| 9449 | |
| 9450 | err = -EBUSY; |
| 9451 | goto err_locked; |
| 9452 | } |
| 9453 | |
| 9454 | WARN_ON_ONCE(ctx->parent_ctx); |
| 9455 | |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9456 | /* |
| 9457 | * This is the point on no return; we cannot fail hereafter. This is |
| 9458 | * where we start modifying current state. |
| 9459 | */ |
| 9460 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9461 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9462 | /* |
| 9463 | * See perf_event_ctx_lock() for comments on the details |
| 9464 | * of swizzling perf_event::ctx. |
| 9465 | */ |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 9466 | perf_remove_from_context(group_leader, 0); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 9467 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9468 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 9469 | group_entry) { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 9470 | perf_remove_from_context(sibling, 0); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9471 | put_ctx(gctx); |
| 9472 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9473 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9474 | /* |
| 9475 | * Wait for everybody to stop referencing the events through |
| 9476 | * the old lists, before installing it on new lists. |
| 9477 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9478 | synchronize_rcu(); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9479 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9480 | /* |
| 9481 | * Install the group siblings before the group leader. |
| 9482 | * |
| 9483 | * Because a group leader will try and install the entire group |
| 9484 | * (through the sibling list, which is still in-tact), we can |
| 9485 | * end up with siblings installed in the wrong context. |
| 9486 | * |
| 9487 | * By installing siblings first we NO-OP because they're not |
| 9488 | * reachable through the group lists. |
| 9489 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9490 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 9491 | group_entry) { |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9492 | perf_event__state_init(sibling); |
Jiri Olsa | 9fc81d8 | 2014-12-10 21:23:51 +0100 | [diff] [blame] | 9493 | perf_install_in_context(ctx, sibling, sibling->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9494 | get_ctx(ctx); |
| 9495 | } |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9496 | |
| 9497 | /* |
| 9498 | * Removing from the context ends up with disabled |
| 9499 | * event. What we want here is event in the initial |
| 9500 | * startup state, ready to be add into new context. |
| 9501 | */ |
| 9502 | perf_event__state_init(group_leader); |
| 9503 | perf_install_in_context(ctx, group_leader, group_leader->cpu); |
| 9504 | get_ctx(ctx); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 9505 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9506 | /* |
| 9507 | * Now that all events are installed in @ctx, nothing |
| 9508 | * references @gctx anymore, so drop the last reference we have |
| 9509 | * on it. |
| 9510 | */ |
| 9511 | put_ctx(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9512 | } |
| 9513 | |
Peter Zijlstra | f73e22a | 2015-09-09 20:48:22 +0200 | [diff] [blame] | 9514 | /* |
| 9515 | * Precalculate sample_data sizes; do while holding ctx::mutex such |
| 9516 | * that we're serialized against further additions and before |
| 9517 | * perf_install_in_context() which is the point the event is active and |
| 9518 | * can use these values. |
| 9519 | */ |
| 9520 | perf_event__header_size(event); |
| 9521 | perf_event__id_header_size(event); |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9522 | |
Peter Zijlstra | 78cd2c7 | 2016-01-25 14:08:45 +0100 | [diff] [blame] | 9523 | event->owner = current; |
| 9524 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 9525 | perf_install_in_context(ctx, event, event->cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 9526 | perf_unpin_context(ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9527 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9528 | if (move_group) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9529 | mutex_unlock(&gctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9530 | mutex_unlock(&ctx->mutex); |
| 9531 | |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9532 | if (task) { |
| 9533 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 9534 | put_task_struct(task); |
| 9535 | } |
| 9536 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 9537 | put_online_cpus(); |
| 9538 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9539 | mutex_lock(¤t->perf_event_mutex); |
| 9540 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 9541 | mutex_unlock(¤t->perf_event_mutex); |
| 9542 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 9543 | /* |
| 9544 | * Drop the reference on the group_event after placing the |
| 9545 | * new event on the sibling_list. This ensures destruction |
| 9546 | * of the group leader will find the pointer to itself in |
| 9547 | * perf_group_detach(). |
| 9548 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9549 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9550 | fd_install(event_fd, event_file); |
| 9551 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9552 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 9553 | err_locked: |
| 9554 | if (move_group) |
| 9555 | mutex_unlock(&gctx->mutex); |
| 9556 | mutex_unlock(&ctx->mutex); |
| 9557 | /* err_file: */ |
| 9558 | fput(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9559 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 9560 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9561 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 9562 | err_alloc: |
Peter Zijlstra | 1300562 | 2016-02-24 18:45:41 +0100 | [diff] [blame] | 9563 | /* |
| 9564 | * If event_file is set, the fput() above will have called ->release() |
| 9565 | * and that will take care of freeing the event. |
| 9566 | */ |
| 9567 | if (!event_file) |
| 9568 | free_event(event); |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9569 | err_cred: |
| 9570 | if (task) |
| 9571 | mutex_unlock(&task->signal->cred_guard_mutex); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 9572 | err_cpus: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 9573 | put_online_cpus(); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 9574 | err_task: |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 9575 | if (task) |
| 9576 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 9577 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 9578 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 9579 | err_fd: |
| 9580 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9581 | return err; |
| 9582 | } |
| 9583 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9584 | /** |
| 9585 | * perf_event_create_kernel_counter |
| 9586 | * |
| 9587 | * @attr: attributes of the counter to create |
| 9588 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 9589 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9590 | */ |
| 9591 | struct perf_event * |
| 9592 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 9593 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9594 | perf_overflow_handler_t overflow_handler, |
| 9595 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9596 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9597 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9598 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9599 | int err; |
| 9600 | |
| 9601 | /* |
| 9602 | * Get the target context (task or percpu): |
| 9603 | */ |
| 9604 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9605 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9606 | overflow_handler, context, -1); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 9607 | if (IS_ERR(event)) { |
| 9608 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9609 | goto err; |
| 9610 | } |
| 9611 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 9612 | /* Mark owner so we could distinguish it from user events. */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9613 | event->owner = TASK_TOMBSTONE; |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 9614 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 9615 | ctx = find_get_context(event->pmu, task, event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9616 | if (IS_ERR(ctx)) { |
| 9617 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9618 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 9619 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9620 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9621 | WARN_ON_ONCE(ctx->parent_ctx); |
| 9622 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9623 | if (ctx->task == TASK_TOMBSTONE) { |
| 9624 | err = -ESRCH; |
| 9625 | goto err_unlock; |
| 9626 | } |
| 9627 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9628 | if (!exclusive_event_installable(event, ctx)) { |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9629 | err = -EBUSY; |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9630 | goto err_unlock; |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 9631 | } |
| 9632 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9633 | perf_install_in_context(ctx, event, cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 9634 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9635 | mutex_unlock(&ctx->mutex); |
| 9636 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9637 | return event; |
| 9638 | |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 9639 | err_unlock: |
| 9640 | mutex_unlock(&ctx->mutex); |
| 9641 | perf_unpin_context(ctx); |
| 9642 | put_ctx(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 9643 | err_free: |
| 9644 | free_event(event); |
| 9645 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 9646 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 9647 | } |
| 9648 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 9649 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9650 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 9651 | { |
| 9652 | struct perf_event_context *src_ctx; |
| 9653 | struct perf_event_context *dst_ctx; |
| 9654 | struct perf_event *event, *tmp; |
| 9655 | LIST_HEAD(events); |
| 9656 | |
| 9657 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 9658 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 9659 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9660 | /* |
| 9661 | * See perf_event_ctx_lock() for comments on the details |
| 9662 | * of swizzling perf_event::ctx. |
| 9663 | */ |
| 9664 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9665 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 9666 | event_entry) { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 9667 | perf_remove_from_context(event, 0); |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 9668 | unaccount_event_cpu(event, src_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9669 | put_ctx(src_ctx); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 9670 | list_add(&event->migrate_entry, &events); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9671 | } |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9672 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9673 | /* |
| 9674 | * Wait for the events to quiesce before re-instating them. |
| 9675 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9676 | synchronize_rcu(); |
| 9677 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 9678 | /* |
| 9679 | * Re-instate events in 2 passes. |
| 9680 | * |
| 9681 | * Skip over group leaders and only install siblings on this first |
| 9682 | * pass, siblings will not get enabled without a leader, however a |
| 9683 | * leader will enable its siblings, even if those are still on the old |
| 9684 | * context. |
| 9685 | */ |
| 9686 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 9687 | if (event->group_leader == event) |
| 9688 | continue; |
| 9689 | |
| 9690 | list_del(&event->migrate_entry); |
| 9691 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 9692 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 9693 | account_event_cpu(event, dst_cpu); |
| 9694 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 9695 | get_ctx(dst_ctx); |
| 9696 | } |
| 9697 | |
| 9698 | /* |
| 9699 | * Once all the siblings are setup properly, install the group leaders |
| 9700 | * to make it go. |
| 9701 | */ |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 9702 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 9703 | list_del(&event->migrate_entry); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9704 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 9705 | event->state = PERF_EVENT_STATE_INACTIVE; |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 9706 | account_event_cpu(event, dst_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9707 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 9708 | get_ctx(dst_ctx); |
| 9709 | } |
| 9710 | mutex_unlock(&dst_ctx->mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 9711 | mutex_unlock(&src_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 9712 | } |
| 9713 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 9714 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9715 | static void sync_child_event(struct perf_event *child_event, |
| 9716 | struct task_struct *child) |
| 9717 | { |
| 9718 | struct perf_event *parent_event = child_event->parent; |
| 9719 | u64 child_val; |
| 9720 | |
| 9721 | if (child_event->attr.inherit_stat) |
| 9722 | perf_event_read_event(child_event, child); |
| 9723 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 9724 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9725 | |
| 9726 | /* |
| 9727 | * Add back the child's count to the parent's count: |
| 9728 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 9729 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9730 | atomic64_add(child_event->total_time_enabled, |
| 9731 | &parent_event->child_total_time_enabled); |
| 9732 | atomic64_add(child_event->total_time_running, |
| 9733 | &parent_event->child_total_time_running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9734 | } |
| 9735 | |
| 9736 | static void |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9737 | perf_event_exit_event(struct perf_event *child_event, |
| 9738 | struct perf_event_context *child_ctx, |
| 9739 | struct task_struct *child) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9740 | { |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9741 | struct perf_event *parent_event = child_event->parent; |
| 9742 | |
Peter Zijlstra | 1903d50 | 2014-07-15 17:27:27 +0200 | [diff] [blame] | 9743 | /* |
| 9744 | * Do not destroy the 'original' grouping; because of the context |
| 9745 | * switch optimization the original events could've ended up in a |
| 9746 | * random child task. |
| 9747 | * |
| 9748 | * If we were to destroy the original group, all group related |
| 9749 | * operations would cease to function properly after this random |
| 9750 | * child dies. |
| 9751 | * |
| 9752 | * Do destroy all inherited groups, we don't care about those |
| 9753 | * and being thorough is better. |
| 9754 | */ |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 9755 | raw_spin_lock_irq(&child_ctx->lock); |
| 9756 | WARN_ON_ONCE(child_ctx->is_active); |
| 9757 | |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9758 | if (parent_event) |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 9759 | perf_group_detach(child_event); |
| 9760 | list_del_event(child_event, child_ctx); |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 9761 | child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */ |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 9762 | raw_spin_unlock_irq(&child_ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9763 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9764 | /* |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9765 | * Parent events are governed by their filedesc, retain them. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9766 | */ |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9767 | if (!parent_event) { |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 9768 | perf_event_wakeup(child_event); |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9769 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9770 | } |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 9771 | /* |
| 9772 | * Child events can be cleaned up. |
| 9773 | */ |
| 9774 | |
| 9775 | sync_child_event(child_event, child); |
| 9776 | |
| 9777 | /* |
| 9778 | * Remove this event from the parent's list |
| 9779 | */ |
| 9780 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 9781 | mutex_lock(&parent_event->child_mutex); |
| 9782 | list_del_init(&child_event->child_list); |
| 9783 | mutex_unlock(&parent_event->child_mutex); |
| 9784 | |
| 9785 | /* |
| 9786 | * Kick perf_poll() for is_event_hup(). |
| 9787 | */ |
| 9788 | perf_event_wakeup(parent_event); |
| 9789 | free_event(child_event); |
| 9790 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9791 | } |
| 9792 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9793 | 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] | 9794 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 9795 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9796 | struct perf_event *child_event, *next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9797 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9798 | WARN_ON_ONCE(child != current); |
| 9799 | |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9800 | child_ctx = perf_pin_task_context(child, ctxn); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9801 | if (!child_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9802 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9803 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9804 | /* |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9805 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
| 9806 | * ctx::mutex over the entire thing. This serializes against almost |
| 9807 | * everything that wants to access the ctx. |
| 9808 | * |
| 9809 | * The exception is sys_perf_event_open() / |
| 9810 | * perf_event_create_kernel_count() which does find_get_context() |
| 9811 | * without ctx::mutex (it cannot because of the move_group double mutex |
| 9812 | * lock thing). See the comments in perf_install_in_context(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9813 | */ |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9814 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9815 | |
| 9816 | /* |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9817 | * In a single ctx::lock section, de-schedule the events and detach the |
| 9818 | * context from the task such that we cannot ever get it scheduled back |
| 9819 | * in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9820 | */ |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9821 | raw_spin_lock_irq(&child_ctx->lock); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9822 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 9823 | |
| 9824 | /* |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9825 | * Now that the context is inactive, destroy the task <-> ctx relation |
| 9826 | * and mark the context dead. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9827 | */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 9828 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
| 9829 | put_ctx(child_ctx); /* cannot be last */ |
| 9830 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); |
| 9831 | put_task_struct(current); /* cannot be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9832 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 9833 | clone_ctx = unclone_ctx(child_ctx); |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 9834 | raw_spin_unlock_irq(&child_ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9835 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 9836 | if (clone_ctx) |
| 9837 | put_ctx(clone_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 9838 | |
| 9839 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9840 | * Report the task dead after unscheduling the events so that we |
| 9841 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 9842 | * get a few PERF_RECORD_READ events. |
| 9843 | */ |
| 9844 | perf_event_task(child, child_ctx, 0); |
| 9845 | |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 9846 | 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] | 9847 | perf_event_exit_event(child_event, child_ctx, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9848 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9849 | mutex_unlock(&child_ctx->mutex); |
| 9850 | |
| 9851 | put_ctx(child_ctx); |
| 9852 | } |
| 9853 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9854 | /* |
| 9855 | * When a child task exits, feed back event values to parent events. |
Peter Zijlstra | 79c9ce5 | 2016-04-26 11:36:53 +0200 | [diff] [blame] | 9856 | * |
| 9857 | * Can be called with cred_guard_mutex held when called from |
| 9858 | * install_exec_creds(). |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9859 | */ |
| 9860 | void perf_event_exit_task(struct task_struct *child) |
| 9861 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 9862 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9863 | int ctxn; |
| 9864 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 9865 | mutex_lock(&child->perf_event_mutex); |
| 9866 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 9867 | owner_entry) { |
| 9868 | list_del_init(&event->owner_entry); |
| 9869 | |
| 9870 | /* |
| 9871 | * Ensure the list deletion is visible before we clear |
| 9872 | * the owner, closes a race against perf_release() where |
| 9873 | * we need to serialize on the owner->perf_event_mutex. |
| 9874 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 9875 | smp_store_release(&event->owner, NULL); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 9876 | } |
| 9877 | mutex_unlock(&child->perf_event_mutex); |
| 9878 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9879 | for_each_task_context_nr(ctxn) |
| 9880 | perf_event_exit_task_context(child, ctxn); |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 9881 | |
| 9882 | /* |
| 9883 | * The perf_event_exit_task_context calls perf_event_task |
| 9884 | * with child's task_ctx, which generates EXIT events for |
| 9885 | * child contexts and sets child->perf_event_ctxp[] to NULL. |
| 9886 | * At this point we need to send EXIT events to cpu contexts. |
| 9887 | */ |
| 9888 | perf_event_task(child, NULL, 0); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9889 | } |
| 9890 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9891 | static void perf_free_event(struct perf_event *event, |
| 9892 | struct perf_event_context *ctx) |
| 9893 | { |
| 9894 | struct perf_event *parent = event->parent; |
| 9895 | |
| 9896 | if (WARN_ON_ONCE(!parent)) |
| 9897 | return; |
| 9898 | |
| 9899 | mutex_lock(&parent->child_mutex); |
| 9900 | list_del_init(&event->child_list); |
| 9901 | mutex_unlock(&parent->child_mutex); |
| 9902 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 9903 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9904 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9905 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 9906 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9907 | list_del_event(event, ctx); |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9908 | raw_spin_unlock_irq(&ctx->lock); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9909 | free_event(event); |
| 9910 | } |
| 9911 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9912 | /* |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9913 | * Free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9914 | * perf_event_init_task below, used by fork() in case of fail. |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9915 | * |
| 9916 | * Not all locks are strictly required, but take them anyway to be nice and |
| 9917 | * help out with the lockdep assertions. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9918 | */ |
| 9919 | void perf_event_free_task(struct task_struct *task) |
| 9920 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9921 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9922 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9923 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9924 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9925 | for_each_task_context_nr(ctxn) { |
| 9926 | ctx = task->perf_event_ctxp[ctxn]; |
| 9927 | if (!ctx) |
| 9928 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9929 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9930 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9931 | again: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9932 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 9933 | group_entry) |
| 9934 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9935 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9936 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 9937 | group_entry) |
| 9938 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9939 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9940 | if (!list_empty(&ctx->pinned_groups) || |
| 9941 | !list_empty(&ctx->flexible_groups)) |
| 9942 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9943 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9944 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9945 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9946 | put_ctx(ctx); |
| 9947 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9948 | } |
| 9949 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 9950 | void perf_event_delayed_put(struct task_struct *task) |
| 9951 | { |
| 9952 | int ctxn; |
| 9953 | |
| 9954 | for_each_task_context_nr(ctxn) |
| 9955 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 9956 | } |
| 9957 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9958 | struct file *perf_event_get(unsigned int fd) |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9959 | { |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9960 | struct file *file; |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9961 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9962 | file = fget_raw(fd); |
| 9963 | if (!file) |
| 9964 | return ERR_PTR(-EBADF); |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9965 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9966 | if (file->f_op != &perf_fops) { |
| 9967 | fput(file); |
| 9968 | return ERR_PTR(-EBADF); |
| 9969 | } |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9970 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9971 | return file; |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9972 | } |
| 9973 | |
| 9974 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) |
| 9975 | { |
| 9976 | if (!event) |
| 9977 | return ERR_PTR(-EINVAL); |
| 9978 | |
| 9979 | return &event->attr; |
| 9980 | } |
| 9981 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9982 | /* |
| 9983 | * inherit a event from parent task to child task: |
| 9984 | */ |
| 9985 | static struct perf_event * |
| 9986 | inherit_event(struct perf_event *parent_event, |
| 9987 | struct task_struct *parent, |
| 9988 | struct perf_event_context *parent_ctx, |
| 9989 | struct task_struct *child, |
| 9990 | struct perf_event *group_leader, |
| 9991 | struct perf_event_context *child_ctx) |
| 9992 | { |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 9993 | enum perf_event_active_state parent_state = parent_event->state; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9994 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 9995 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9996 | |
| 9997 | /* |
| 9998 | * Instead of creating recursive hierarchies of events, |
| 9999 | * we link inherited events back to the original parent, |
| 10000 | * which has a filp for sure, which we use as the reference |
| 10001 | * count: |
| 10002 | */ |
| 10003 | if (parent_event->parent) |
| 10004 | parent_event = parent_event->parent; |
| 10005 | |
| 10006 | child_event = perf_event_alloc(&parent_event->attr, |
| 10007 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 10008 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10009 | group_leader, parent_event, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 10010 | NULL, NULL, -1); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10011 | if (IS_ERR(child_event)) |
| 10012 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 10013 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 10014 | /* |
| 10015 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) |
| 10016 | * must be under the same lock in order to serialize against |
| 10017 | * perf_event_release_kernel(), such that either we must observe |
| 10018 | * is_orphaned_event() or they will observe us on the child_list. |
| 10019 | */ |
| 10020 | mutex_lock(&parent_event->child_mutex); |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 10021 | if (is_orphaned_event(parent_event) || |
| 10022 | !atomic_long_inc_not_zero(&parent_event->refcount)) { |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 10023 | mutex_unlock(&parent_event->child_mutex); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 10024 | free_event(child_event); |
| 10025 | return NULL; |
| 10026 | } |
| 10027 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10028 | get_ctx(child_ctx); |
| 10029 | |
| 10030 | /* |
| 10031 | * Make the child state follow the state of the parent event, |
| 10032 | * not its attr.disabled bit. We hold the parent's mutex, |
| 10033 | * so we won't race with perf_event_{en, dis}able_family. |
| 10034 | */ |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 10035 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10036 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 10037 | else |
| 10038 | child_event->state = PERF_EVENT_STATE_OFF; |
| 10039 | |
| 10040 | if (parent_event->attr.freq) { |
| 10041 | u64 sample_period = parent_event->hw.sample_period; |
| 10042 | struct hw_perf_event *hwc = &child_event->hw; |
| 10043 | |
| 10044 | hwc->sample_period = sample_period; |
| 10045 | hwc->last_period = sample_period; |
| 10046 | |
| 10047 | local64_set(&hwc->period_left, sample_period); |
| 10048 | } |
| 10049 | |
| 10050 | child_event->ctx = child_ctx; |
| 10051 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 10052 | child_event->overflow_handler_context |
| 10053 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10054 | |
| 10055 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 10056 | * Precalculate sample_data sizes |
| 10057 | */ |
| 10058 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 10059 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 10060 | |
| 10061 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10062 | * Link it up in the child's context: |
| 10063 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 10064 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10065 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 10066 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10067 | |
| 10068 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10069 | * Link this into the parent event's child list |
| 10070 | */ |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 10071 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 10072 | mutex_unlock(&parent_event->child_mutex); |
| 10073 | |
| 10074 | return child_event; |
| 10075 | } |
| 10076 | |
| 10077 | static int inherit_group(struct perf_event *parent_event, |
| 10078 | struct task_struct *parent, |
| 10079 | struct perf_event_context *parent_ctx, |
| 10080 | struct task_struct *child, |
| 10081 | struct perf_event_context *child_ctx) |
| 10082 | { |
| 10083 | struct perf_event *leader; |
| 10084 | struct perf_event *sub; |
| 10085 | struct perf_event *child_ctr; |
| 10086 | |
| 10087 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 10088 | child, NULL, child_ctx); |
| 10089 | if (IS_ERR(leader)) |
| 10090 | return PTR_ERR(leader); |
| 10091 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 10092 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 10093 | child, leader, child_ctx); |
| 10094 | if (IS_ERR(child_ctr)) |
| 10095 | return PTR_ERR(child_ctr); |
| 10096 | } |
| 10097 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10098 | } |
| 10099 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10100 | static int |
| 10101 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 10102 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10103 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10104 | int *inherited_all) |
| 10105 | { |
| 10106 | int ret; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10107 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10108 | |
| 10109 | if (!event->attr.inherit) { |
| 10110 | *inherited_all = 0; |
| 10111 | return 0; |
| 10112 | } |
| 10113 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 10114 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10115 | if (!child_ctx) { |
| 10116 | /* |
| 10117 | * This is executed from the parent task context, so |
| 10118 | * inherit events that have been marked for cloning. |
| 10119 | * First allocate and initialize a context for the |
| 10120 | * child. |
| 10121 | */ |
| 10122 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 10123 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10124 | if (!child_ctx) |
| 10125 | return -ENOMEM; |
| 10126 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10127 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10128 | } |
| 10129 | |
| 10130 | ret = inherit_group(event, parent, parent_ctx, |
| 10131 | child, child_ctx); |
| 10132 | |
| 10133 | if (ret) |
| 10134 | *inherited_all = 0; |
| 10135 | |
| 10136 | return ret; |
| 10137 | } |
| 10138 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10139 | /* |
| 10140 | * Initialize the perf_event context in task_struct |
| 10141 | */ |
Jiri Olsa | 985c8dc | 2014-06-24 10:20:24 +0200 | [diff] [blame] | 10142 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10143 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10144 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10145 | struct perf_event_context *cloned_ctx; |
| 10146 | struct perf_event *event; |
| 10147 | struct task_struct *parent = current; |
| 10148 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10149 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10150 | int ret = 0; |
| 10151 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10152 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10153 | return 0; |
| 10154 | |
| 10155 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10156 | * If the parent's context is a clone, pin it so it won't get |
| 10157 | * swapped under us. |
| 10158 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10159 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Peter Zijlstra | ffb4ef2 | 2014-05-05 19:12:20 +0200 | [diff] [blame] | 10160 | if (!parent_ctx) |
| 10161 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10162 | |
| 10163 | /* |
| 10164 | * No need to check if parent_ctx != NULL here; since we saw |
| 10165 | * it non-NULL earlier, the only reason for it to become NULL |
| 10166 | * is if we exit, and since we're currently in the middle of |
| 10167 | * a fork we can't be exiting at the same time. |
| 10168 | */ |
| 10169 | |
| 10170 | /* |
| 10171 | * Lock the parent list. No need to lock the child - not PID |
| 10172 | * hashed yet and not running, so nobody can access it. |
| 10173 | */ |
| 10174 | mutex_lock(&parent_ctx->mutex); |
| 10175 | |
| 10176 | /* |
| 10177 | * We dont have to disable NMIs - we are only looking at |
| 10178 | * the list, not manipulating it: |
| 10179 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10180 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10181 | ret = inherit_task_group(event, parent, parent_ctx, |
| 10182 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10183 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10184 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10185 | } |
| 10186 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10187 | /* |
| 10188 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 10189 | * to allocations, but we need to prevent rotation because |
| 10190 | * rotate_ctx() will change the list from interrupt context. |
| 10191 | */ |
| 10192 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 10193 | parent_ctx->rotate_disable = 1; |
| 10194 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 10195 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10196 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10197 | ret = inherit_task_group(event, parent, parent_ctx, |
| 10198 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10199 | if (ret) |
| 10200 | break; |
| 10201 | } |
| 10202 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10203 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 10204 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 10205 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10206 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 10207 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 10208 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10209 | /* |
| 10210 | * Mark the child context as a clone of the parent |
| 10211 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 10212 | * |
| 10213 | * Note that if the parent is a clone, the holding of |
| 10214 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10215 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 10216 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10217 | if (cloned_ctx) { |
| 10218 | child_ctx->parent_ctx = cloned_ctx; |
| 10219 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 10220 | } else { |
| 10221 | child_ctx->parent_ctx = parent_ctx; |
| 10222 | child_ctx->parent_gen = parent_ctx->generation; |
| 10223 | } |
| 10224 | get_ctx(child_ctx->parent_ctx); |
| 10225 | } |
| 10226 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 10227 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10228 | mutex_unlock(&parent_ctx->mutex); |
| 10229 | |
| 10230 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 10231 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10232 | |
| 10233 | return ret; |
| 10234 | } |
| 10235 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10236 | /* |
| 10237 | * Initialize the perf_event context in task_struct |
| 10238 | */ |
| 10239 | int perf_event_init_task(struct task_struct *child) |
| 10240 | { |
| 10241 | int ctxn, ret; |
| 10242 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 10243 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 10244 | mutex_init(&child->perf_event_mutex); |
| 10245 | INIT_LIST_HEAD(&child->perf_event_list); |
| 10246 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10247 | for_each_task_context_nr(ctxn) { |
| 10248 | ret = perf_event_init_context(child, ctxn); |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 10249 | if (ret) { |
| 10250 | perf_event_free_task(child); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10251 | return ret; |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 10252 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 10253 | } |
| 10254 | |
| 10255 | return 0; |
| 10256 | } |
| 10257 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10258 | static void __init perf_event_init_all_cpus(void) |
| 10259 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10260 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10261 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10262 | |
| 10263 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10264 | swhash = &per_cpu(swevent_htable, cpu); |
| 10265 | mutex_init(&swhash->hlist_mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 10266 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
Kan Liang | f2fb6be | 2016-03-23 11:24:37 -0700 | [diff] [blame] | 10267 | |
| 10268 | INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); |
| 10269 | raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10270 | } |
| 10271 | } |
| 10272 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 10273 | static void perf_event_init_cpu(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10274 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10275 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10276 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10277 | mutex_lock(&swhash->hlist_mutex); |
Thomas Gleixner | 059fcd8 | 2016-02-09 20:11:34 +0000 | [diff] [blame] | 10278 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 10279 | struct swevent_hlist *hlist; |
| 10280 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10281 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 10282 | WARN_ON(!hlist); |
| 10283 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 10284 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 10285 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10286 | } |
| 10287 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 10288 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10289 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10290 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10291 | struct perf_event_context *ctx = __info; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 10292 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 10293 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10294 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 10295 | raw_spin_lock(&ctx->lock); |
| 10296 | list_for_each_entry(event, &ctx->event_list, event_entry) |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 10297 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 10298 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10299 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10300 | |
| 10301 | static void perf_event_exit_cpu_context(int cpu) |
| 10302 | { |
| 10303 | struct perf_event_context *ctx; |
| 10304 | struct pmu *pmu; |
| 10305 | int idx; |
| 10306 | |
| 10307 | idx = srcu_read_lock(&pmus_srcu); |
| 10308 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 10309 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10310 | |
| 10311 | mutex_lock(&ctx->mutex); |
| 10312 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 10313 | mutex_unlock(&ctx->mutex); |
| 10314 | } |
| 10315 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 10316 | } |
| 10317 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10318 | static void perf_event_exit_cpu(int cpu) |
| 10319 | { |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 10320 | perf_event_exit_cpu_context(cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10321 | } |
| 10322 | #else |
| 10323 | static inline void perf_event_exit_cpu(int cpu) { } |
| 10324 | #endif |
| 10325 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 10326 | static int |
| 10327 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 10328 | { |
| 10329 | int cpu; |
| 10330 | |
| 10331 | for_each_online_cpu(cpu) |
| 10332 | perf_event_exit_cpu(cpu); |
| 10333 | |
| 10334 | return NOTIFY_OK; |
| 10335 | } |
| 10336 | |
| 10337 | /* |
| 10338 | * Run the perf reboot notifier at the very last possible moment so that |
| 10339 | * the generic watchdog code runs as long as possible. |
| 10340 | */ |
| 10341 | static struct notifier_block perf_reboot_notifier = { |
| 10342 | .notifier_call = perf_reboot, |
| 10343 | .priority = INT_MIN, |
| 10344 | }; |
| 10345 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 10346 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10347 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 10348 | { |
| 10349 | unsigned int cpu = (long)hcpu; |
| 10350 | |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 10351 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10352 | |
| 10353 | case CPU_UP_PREPARE: |
Peter Zijlstra | 1dcaac1 | 2016-03-08 17:56:05 +0100 | [diff] [blame] | 10354 | /* |
| 10355 | * This must be done before the CPU comes alive, because the |
| 10356 | * moment we can run tasks we can encounter (software) events. |
| 10357 | * |
| 10358 | * Specifically, someone can have inherited events on kthreadd |
| 10359 | * or a pre-existing worker thread that gets re-bound. |
| 10360 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10361 | perf_event_init_cpu(cpu); |
| 10362 | break; |
| 10363 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10364 | case CPU_DOWN_PREPARE: |
Peter Zijlstra | 1dcaac1 | 2016-03-08 17:56:05 +0100 | [diff] [blame] | 10365 | /* |
| 10366 | * This must be done before the CPU dies because after that an |
| 10367 | * active event might want to IPI the CPU and that'll not work |
| 10368 | * so great for dead CPUs. |
| 10369 | * |
| 10370 | * XXX smp_call_function_single() return -ENXIO without a warn |
| 10371 | * so we could possibly deal with this. |
| 10372 | * |
| 10373 | * This is safe against new events arriving because |
| 10374 | * sys_perf_event_open() serializes against hotplug using |
| 10375 | * get_online_cpus(). |
| 10376 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10377 | perf_event_exit_cpu(cpu); |
| 10378 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10379 | default: |
| 10380 | break; |
| 10381 | } |
| 10382 | |
| 10383 | return NOTIFY_OK; |
| 10384 | } |
| 10385 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10386 | void __init perf_event_init(void) |
| 10387 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 10388 | int ret; |
| 10389 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 10390 | idr_init(&pmu_idr); |
| 10391 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 10392 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 10393 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 10394 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 10395 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 10396 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 10397 | perf_tp_register(); |
| 10398 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 10399 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 10400 | |
| 10401 | ret = init_hw_breakpoint(); |
| 10402 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 10403 | |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 10404 | /* |
| 10405 | * Build time assertion that we keep the data_head at the intended |
| 10406 | * location. IOW, validation we got the __reserved[] size right. |
| 10407 | */ |
| 10408 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 10409 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10410 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 10411 | |
Cody P Schafer | fd979c0 | 2015-01-30 13:45:57 -0800 | [diff] [blame] | 10412 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
| 10413 | char *page) |
| 10414 | { |
| 10415 | struct perf_pmu_events_attr *pmu_attr = |
| 10416 | container_of(attr, struct perf_pmu_events_attr, attr); |
| 10417 | |
| 10418 | if (pmu_attr->event_str) |
| 10419 | return sprintf(page, "%s\n", pmu_attr->event_str); |
| 10420 | |
| 10421 | return 0; |
| 10422 | } |
Thomas Gleixner | 675965b | 2016-02-22 22:19:27 +0000 | [diff] [blame] | 10423 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
Cody P Schafer | fd979c0 | 2015-01-30 13:45:57 -0800 | [diff] [blame] | 10424 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 10425 | static int __init perf_event_sysfs_init(void) |
| 10426 | { |
| 10427 | struct pmu *pmu; |
| 10428 | int ret; |
| 10429 | |
| 10430 | mutex_lock(&pmus_lock); |
| 10431 | |
| 10432 | ret = bus_register(&pmu_bus); |
| 10433 | if (ret) |
| 10434 | goto unlock; |
| 10435 | |
| 10436 | list_for_each_entry(pmu, &pmus, entry) { |
| 10437 | if (!pmu->name || pmu->type < 0) |
| 10438 | continue; |
| 10439 | |
| 10440 | ret = pmu_dev_alloc(pmu); |
| 10441 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 10442 | } |
| 10443 | pmu_bus_running = 1; |
| 10444 | ret = 0; |
| 10445 | |
| 10446 | unlock: |
| 10447 | mutex_unlock(&pmus_lock); |
| 10448 | |
| 10449 | return ret; |
| 10450 | } |
| 10451 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10452 | |
| 10453 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 10454 | static struct cgroup_subsys_state * |
| 10455 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10456 | { |
| 10457 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10458 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 10459 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10460 | if (!jc) |
| 10461 | return ERR_PTR(-ENOMEM); |
| 10462 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10463 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 10464 | if (!jc->info) { |
| 10465 | kfree(jc); |
| 10466 | return ERR_PTR(-ENOMEM); |
| 10467 | } |
| 10468 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10469 | return &jc->css; |
| 10470 | } |
| 10471 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 10472 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10473 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 10474 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
| 10475 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10476 | free_percpu(jc->info); |
| 10477 | kfree(jc); |
| 10478 | } |
| 10479 | |
| 10480 | static int __perf_cgroup_move(void *info) |
| 10481 | { |
| 10482 | struct task_struct *task = info; |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 10483 | rcu_read_lock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10484 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 10485 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10486 | return 0; |
| 10487 | } |
| 10488 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 10489 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10490 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10491 | struct task_struct *task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 10492 | struct cgroup_subsys_state *css; |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10493 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 10494 | cgroup_taskset_for_each(task, css, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10495 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10496 | } |
| 10497 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 10498 | struct cgroup_subsys perf_event_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 10499 | .css_alloc = perf_cgroup_css_alloc, |
| 10500 | .css_free = perf_cgroup_css_free, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10501 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 10502 | }; |
| 10503 | #endif /* CONFIG_CGROUP_PERF */ |