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> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 47 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 48 | #include "internal.h" |
| 49 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 50 | #include <asm/irq_regs.h> |
| 51 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 52 | typedef int (*remote_function_f)(void *); |
| 53 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 54 | struct remote_function_call { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 55 | struct task_struct *p; |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 56 | remote_function_f func; |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 57 | void *info; |
| 58 | int ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static void remote_function(void *data) |
| 62 | { |
| 63 | struct remote_function_call *tfc = data; |
| 64 | struct task_struct *p = tfc->p; |
| 65 | |
| 66 | if (p) { |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 67 | /* -EAGAIN */ |
| 68 | if (task_cpu(p) != smp_processor_id()) |
| 69 | return; |
| 70 | |
| 71 | /* |
| 72 | * Now that we're on right CPU with IRQs disabled, we can test |
| 73 | * if we hit the right task without races. |
| 74 | */ |
| 75 | |
| 76 | tfc->ret = -ESRCH; /* No such (running) process */ |
| 77 | if (p != current) |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
| 81 | tfc->ret = tfc->func(tfc->info); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * task_function_call - call a function on the cpu on which a task runs |
| 86 | * @p: the task to evaluate |
| 87 | * @func: the function to be called |
| 88 | * @info: the function call argument |
| 89 | * |
| 90 | * Calls the function @func when the task is currently running. This might |
| 91 | * be on the current CPU, which just calls the function directly |
| 92 | * |
| 93 | * returns: @func return value, or |
| 94 | * -ESRCH - when the process isn't running |
| 95 | * -EAGAIN - when the process moved away |
| 96 | */ |
| 97 | static int |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 98 | 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] | 99 | { |
| 100 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 101 | .p = p, |
| 102 | .func = func, |
| 103 | .info = info, |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 104 | .ret = -EAGAIN, |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 105 | }; |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 106 | int ret; |
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 | do { |
| 109 | ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1); |
| 110 | if (!ret) |
| 111 | ret = data.ret; |
| 112 | } while (ret == -EAGAIN); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 113 | |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 114 | return ret; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * cpu_function_call - call a function on the cpu |
| 119 | * @func: the function to be called |
| 120 | * @info: the function call argument |
| 121 | * |
| 122 | * Calls the function @func on the remote cpu. |
| 123 | * |
| 124 | * returns: @func return value or -ENXIO when the cpu is offline |
| 125 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 126 | 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] | 127 | { |
| 128 | struct remote_function_call data = { |
Ingo Molnar | e7e7ee2 | 2011-05-04 08:42:29 +0200 | [diff] [blame] | 129 | .p = NULL, |
| 130 | .func = func, |
| 131 | .info = info, |
| 132 | .ret = -ENXIO, /* No such CPU */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | smp_call_function_single(cpu, remote_function, &data, 1); |
| 136 | |
| 137 | return data.ret; |
| 138 | } |
| 139 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 140 | static inline struct perf_cpu_context * |
| 141 | __get_cpu_context(struct perf_event_context *ctx) |
| 142 | { |
| 143 | return this_cpu_ptr(ctx->pmu->pmu_cpu_context); |
| 144 | } |
| 145 | |
| 146 | static void perf_ctx_lock(struct perf_cpu_context *cpuctx, |
| 147 | struct perf_event_context *ctx) |
| 148 | { |
| 149 | raw_spin_lock(&cpuctx->ctx.lock); |
| 150 | if (ctx) |
| 151 | raw_spin_lock(&ctx->lock); |
| 152 | } |
| 153 | |
| 154 | static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, |
| 155 | struct perf_event_context *ctx) |
| 156 | { |
| 157 | if (ctx) |
| 158 | raw_spin_unlock(&ctx->lock); |
| 159 | raw_spin_unlock(&cpuctx->ctx.lock); |
| 160 | } |
| 161 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 162 | #define TASK_TOMBSTONE ((void *)-1L) |
| 163 | |
| 164 | static bool is_kernel_event(struct perf_event *event) |
| 165 | { |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 166 | return READ_ONCE(event->owner) == TASK_TOMBSTONE; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 169 | /* |
| 170 | * On task ctx scheduling... |
| 171 | * |
| 172 | * When !ctx->nr_events a task context will not be scheduled. This means |
| 173 | * we can disable the scheduler hooks (for performance) without leaving |
| 174 | * pending task ctx state. |
| 175 | * |
| 176 | * This however results in two special cases: |
| 177 | * |
| 178 | * - removing the last event from a task ctx; this is relatively straight |
| 179 | * forward and is done in __perf_remove_from_context. |
| 180 | * |
| 181 | * - adding the first event to a task ctx; this is tricky because we cannot |
| 182 | * rely on ctx->is_active and therefore cannot use event_function_call(). |
| 183 | * See perf_install_in_context(). |
| 184 | * |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 185 | * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. |
| 186 | */ |
| 187 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 188 | typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, |
| 189 | struct perf_event_context *, void *); |
| 190 | |
| 191 | struct event_function_struct { |
| 192 | struct perf_event *event; |
| 193 | event_f func; |
| 194 | void *data; |
| 195 | }; |
| 196 | |
| 197 | static int event_function(void *info) |
| 198 | { |
| 199 | struct event_function_struct *efs = info; |
| 200 | struct perf_event *event = efs->event; |
| 201 | struct perf_event_context *ctx = event->ctx; |
| 202 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 203 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 204 | int ret = 0; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 205 | |
| 206 | WARN_ON_ONCE(!irqs_disabled()); |
| 207 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 208 | perf_ctx_lock(cpuctx, task_ctx); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 209 | /* |
| 210 | * Since we do the IPI call without holding ctx->lock things can have |
| 211 | * changed, double check we hit the task we set out to hit. |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 212 | */ |
| 213 | if (ctx->task) { |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 214 | if (ctx->task != current) { |
Peter Zijlstra | 0da4cf3 | 2016-02-24 18:45:51 +0100 | [diff] [blame] | 215 | ret = -ESRCH; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 216 | goto unlock; |
| 217 | } |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 218 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 219 | /* |
| 220 | * We only use event_function_call() on established contexts, |
| 221 | * and event_function() is only ever called when active (or |
| 222 | * rather, we'll have bailed in task_function_call() or the |
| 223 | * above ctx->task != current test), therefore we must have |
| 224 | * ctx->is_active here. |
| 225 | */ |
| 226 | WARN_ON_ONCE(!ctx->is_active); |
| 227 | /* |
| 228 | * And since we have ctx->is_active, cpuctx->task_ctx must |
| 229 | * match. |
| 230 | */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 231 | WARN_ON_ONCE(task_ctx != ctx); |
| 232 | } else { |
| 233 | WARN_ON_ONCE(&cpuctx->ctx != ctx); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 234 | } |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 235 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 236 | efs->func(event, cpuctx, ctx, efs->data); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 237 | unlock: |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 238 | perf_ctx_unlock(cpuctx, task_ctx); |
| 239 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 240 | return ret; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void event_function_local(struct perf_event *event, event_f func, void *data) |
| 244 | { |
| 245 | struct event_function_struct efs = { |
| 246 | .event = event, |
| 247 | .func = func, |
| 248 | .data = data, |
| 249 | }; |
| 250 | |
| 251 | int ret = event_function(&efs); |
| 252 | WARN_ON_ONCE(ret); |
| 253 | } |
| 254 | |
| 255 | 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] | 256 | { |
| 257 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 258 | struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 259 | struct event_function_struct efs = { |
| 260 | .event = event, |
| 261 | .func = func, |
| 262 | .data = data, |
| 263 | }; |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 264 | |
Peter Zijlstra | c97f473 | 2016-01-14 10:51:03 +0100 | [diff] [blame] | 265 | if (!event->parent) { |
| 266 | /* |
| 267 | * If this is a !child event, we must hold ctx::mutex to |
| 268 | * stabilize the the event->ctx relation. See |
| 269 | * perf_event_ctx_lock(). |
| 270 | */ |
| 271 | lockdep_assert_held(&ctx->mutex); |
| 272 | } |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 273 | |
| 274 | if (!task) { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 275 | cpu_function_call(event->cpu, event_function, &efs); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 279 | if (task == TASK_TOMBSTONE) |
| 280 | return; |
| 281 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 282 | again: |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 283 | if (!task_function_call(task, event_function, &efs)) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 284 | return; |
| 285 | |
| 286 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 287 | /* |
| 288 | * Reload the task pointer, it might have been changed by |
| 289 | * a concurrent perf_event_context_sched_out(). |
| 290 | */ |
| 291 | task = ctx->task; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 292 | if (task == TASK_TOMBSTONE) { |
| 293 | raw_spin_unlock_irq(&ctx->lock); |
| 294 | return; |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 295 | } |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 296 | if (ctx->is_active) { |
| 297 | raw_spin_unlock_irq(&ctx->lock); |
| 298 | goto again; |
| 299 | } |
| 300 | func(event, NULL, ctx, data); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 301 | raw_spin_unlock_irq(&ctx->lock); |
| 302 | } |
| 303 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 304 | #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ |
| 305 | PERF_FLAG_FD_OUTPUT |\ |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 306 | PERF_FLAG_PID_CGROUP |\ |
| 307 | PERF_FLAG_FD_CLOEXEC) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 308 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 309 | /* |
| 310 | * branch priv levels that need permission checks |
| 311 | */ |
| 312 | #define PERF_SAMPLE_BRANCH_PERM_PLM \ |
| 313 | (PERF_SAMPLE_BRANCH_KERNEL |\ |
| 314 | PERF_SAMPLE_BRANCH_HV) |
| 315 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 316 | enum event_type_t { |
| 317 | EVENT_FLEXIBLE = 0x1, |
| 318 | EVENT_PINNED = 0x2, |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 319 | EVENT_TIME = 0x4, |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 320 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
| 321 | }; |
| 322 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 323 | /* |
| 324 | * perf_sched_events : >0 events exist |
| 325 | * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu |
| 326 | */ |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 327 | |
| 328 | static void perf_sched_delayed(struct work_struct *work); |
| 329 | DEFINE_STATIC_KEY_FALSE(perf_sched_events); |
| 330 | static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); |
| 331 | static DEFINE_MUTEX(perf_sched_mutex); |
| 332 | static atomic_t perf_sched_count; |
| 333 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 334 | static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 335 | static DEFINE_PER_CPU(int, perf_sched_cb_usages); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 336 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 337 | static atomic_t nr_mmap_events __read_mostly; |
| 338 | static atomic_t nr_comm_events __read_mostly; |
| 339 | static atomic_t nr_task_events __read_mostly; |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 340 | static atomic_t nr_freq_events __read_mostly; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 341 | static atomic_t nr_switch_events __read_mostly; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 342 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 343 | static LIST_HEAD(pmus); |
| 344 | static DEFINE_MUTEX(pmus_lock); |
| 345 | static struct srcu_struct pmus_srcu; |
| 346 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 347 | /* |
| 348 | * perf event paranoia level: |
| 349 | * -1 - not paranoid at all |
| 350 | * 0 - disallow raw tracepoint access for unpriv |
| 351 | * 1 - disallow cpu events for unpriv |
| 352 | * 2 - disallow kernel profiling for unpriv |
| 353 | */ |
| 354 | int sysctl_perf_event_paranoid __read_mostly = 1; |
| 355 | |
Frederic Weisbecker | 2044338 | 2011-03-31 03:33:29 +0200 | [diff] [blame] | 356 | /* Minimum for 512 kiB + 1 user control page */ |
| 357 | 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] | 358 | |
| 359 | /* |
| 360 | * max perf event sample rate |
| 361 | */ |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 362 | #define DEFAULT_MAX_SAMPLE_RATE 100000 |
| 363 | #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) |
| 364 | #define DEFAULT_CPU_TIME_MAX_PERCENT 25 |
| 365 | |
| 366 | int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; |
| 367 | |
| 368 | static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); |
| 369 | static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; |
| 370 | |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 371 | static int perf_sample_allowed_ns __read_mostly = |
| 372 | DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 373 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 374 | static void update_perf_cpu_limits(void) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 375 | { |
| 376 | u64 tmp = perf_sample_period_ns; |
| 377 | |
| 378 | tmp *= sysctl_perf_cpu_time_max_percent; |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 379 | tmp = div_u64(tmp, 100); |
| 380 | if (!tmp) |
| 381 | tmp = 1; |
| 382 | |
| 383 | WRITE_ONCE(perf_sample_allowed_ns, tmp); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 384 | } |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 385 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 386 | static int perf_rotate_context(struct perf_cpu_context *cpuctx); |
| 387 | |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 388 | int perf_proc_update_handler(struct ctl_table *table, int write, |
| 389 | void __user *buffer, size_t *lenp, |
| 390 | loff_t *ppos) |
| 391 | { |
Knut Petersen | 723478c | 2013-09-25 14:29:37 +0200 | [diff] [blame] | 392 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 393 | |
| 394 | if (ret || !write) |
| 395 | return ret; |
| 396 | |
| 397 | 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] | 398 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 399 | update_perf_cpu_limits(); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 400 | |
| 401 | return 0; |
| 402 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 403 | |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 404 | int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; |
| 405 | |
| 406 | int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, |
| 407 | void __user *buffer, size_t *lenp, |
| 408 | loff_t *ppos) |
| 409 | { |
| 410 | int ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 411 | |
| 412 | if (ret || !write) |
| 413 | return ret; |
| 414 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 415 | if (sysctl_perf_cpu_time_max_percent == 100) { |
| 416 | printk(KERN_WARNING |
| 417 | "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); |
| 418 | WRITE_ONCE(perf_sample_allowed_ns, 0); |
| 419 | } else { |
| 420 | update_perf_cpu_limits(); |
| 421 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * perf samples are done in some very critical code paths (NMIs). |
| 428 | * If they take too much CPU time, the system can lock up and not |
| 429 | * get any real work done. This will drop the sample rate when |
| 430 | * we detect that events are taking too long. |
| 431 | */ |
| 432 | #define NR_ACCUMULATED_SAMPLES 128 |
Peter Zijlstra | d9494cb | 2013-10-17 15:36:19 +0200 | [diff] [blame] | 433 | static DEFINE_PER_CPU(u64, running_sample_length); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 434 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 435 | static u64 __report_avg; |
| 436 | static u64 __report_allowed; |
| 437 | |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 438 | static void perf_duration_warn(struct irq_work *w) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 439 | { |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 440 | printk_ratelimited(KERN_WARNING |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 441 | "perf: interrupt took too long (%lld > %lld), lowering " |
| 442 | "kernel.perf_event_max_sample_rate to %d\n", |
| 443 | __report_avg, __report_allowed, |
| 444 | sysctl_perf_event_sample_rate); |
Peter Zijlstra | 6a02ad66 | 2014-02-03 18:11:08 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); |
| 448 | |
| 449 | void perf_sample_event_took(u64 sample_len_ns) |
| 450 | { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 451 | u64 max_len = READ_ONCE(perf_sample_allowed_ns); |
| 452 | u64 running_len; |
| 453 | u64 avg_len; |
| 454 | u32 max; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 455 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 456 | if (max_len == 0) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 457 | return; |
| 458 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 459 | /* Decay the counter by 1 average sample. */ |
| 460 | running_len = __this_cpu_read(running_sample_length); |
| 461 | running_len -= running_len/NR_ACCUMULATED_SAMPLES; |
| 462 | running_len += sample_len_ns; |
| 463 | __this_cpu_write(running_sample_length, running_len); |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 464 | |
| 465 | /* |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 466 | * Note: this will be biased artifically low until we have |
| 467 | * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 468 | * from having to maintain a count. |
| 469 | */ |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 470 | avg_len = running_len/NR_ACCUMULATED_SAMPLES; |
| 471 | if (avg_len <= max_len) |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 472 | return; |
| 473 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 474 | __report_avg = avg_len; |
| 475 | __report_allowed = max_len; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 476 | |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 477 | /* |
| 478 | * Compute a throttle threshold 25% below the current duration. |
| 479 | */ |
| 480 | avg_len += avg_len / 4; |
| 481 | max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; |
| 482 | if (avg_len < max) |
| 483 | max /= (u32)avg_len; |
| 484 | else |
| 485 | max = 1; |
| 486 | |
| 487 | WRITE_ONCE(perf_sample_allowed_ns, avg_len); |
| 488 | WRITE_ONCE(max_samples_per_tick, max); |
| 489 | |
| 490 | sysctl_perf_event_sample_rate = max * HZ; |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 491 | perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; |
| 492 | |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 493 | if (!irq_work_queue(&perf_duration_work)) { |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 494 | early_printk("perf: interrupt took too long (%lld > %lld), lowering " |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 495 | "kernel.perf_event_max_sample_rate to %d\n", |
Peter Zijlstra | 91a612e | 2016-03-17 15:17:35 +0100 | [diff] [blame^] | 496 | __report_avg, __report_allowed, |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 497 | sysctl_perf_event_sample_rate); |
| 498 | } |
Dave Hansen | 14c63f1 | 2013-06-21 08:51:36 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 501 | static atomic64_t perf_event_id; |
| 502 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 503 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 504 | enum event_type_t event_type); |
| 505 | |
| 506 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 507 | enum event_type_t event_type, |
| 508 | struct task_struct *task); |
| 509 | |
| 510 | static void update_context_time(struct perf_event_context *ctx); |
| 511 | static u64 perf_event_time(struct perf_event *event); |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 512 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 513 | void __weak perf_event_print_debug(void) { } |
| 514 | |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 515 | extern __weak const char *perf_pmu_name(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 516 | { |
Matt Fleming | 84c7991 | 2010-10-03 21:41:13 +0100 | [diff] [blame] | 517 | return "pmu"; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Stephane Eranian | 0b3fcf1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 520 | static inline u64 perf_clock(void) |
| 521 | { |
| 522 | return local_clock(); |
| 523 | } |
| 524 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 525 | static inline u64 perf_event_clock(struct perf_event *event) |
| 526 | { |
| 527 | return event->clock(); |
| 528 | } |
| 529 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 530 | #ifdef CONFIG_CGROUP_PERF |
| 531 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 532 | static inline bool |
| 533 | perf_cgroup_match(struct perf_event *event) |
| 534 | { |
| 535 | struct perf_event_context *ctx = event->ctx; |
| 536 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 537 | |
Tejun Heo | ef824fa | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 538 | /* @event doesn't care about cgroup */ |
| 539 | if (!event->cgrp) |
| 540 | return true; |
| 541 | |
| 542 | /* wants specific cgroup scope but @cpuctx isn't associated with any */ |
| 543 | if (!cpuctx->cgrp) |
| 544 | return false; |
| 545 | |
| 546 | /* |
| 547 | * Cgroup scoping is recursive. An event enabled for a cgroup is |
| 548 | * also enabled for all its descendant cgroups. If @cpuctx's |
| 549 | * cgroup is a descendant of @event's (the test covers identity |
| 550 | * case), it's a match. |
| 551 | */ |
| 552 | return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, |
| 553 | event->cgrp->css.cgroup); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 556 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 557 | { |
Zefan Li | 4e2ba65 | 2014-09-19 16:53:14 +0800 | [diff] [blame] | 558 | css_put(&event->cgrp->css); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 559 | event->cgrp = NULL; |
| 560 | } |
| 561 | |
| 562 | static inline int is_cgroup_event(struct perf_event *event) |
| 563 | { |
| 564 | return event->cgrp != NULL; |
| 565 | } |
| 566 | |
| 567 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 568 | { |
| 569 | struct perf_cgroup_info *t; |
| 570 | |
| 571 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 572 | return t->time; |
| 573 | } |
| 574 | |
| 575 | static inline void __update_cgrp_time(struct perf_cgroup *cgrp) |
| 576 | { |
| 577 | struct perf_cgroup_info *info; |
| 578 | u64 now; |
| 579 | |
| 580 | now = perf_clock(); |
| 581 | |
| 582 | info = this_cpu_ptr(cgrp->info); |
| 583 | |
| 584 | info->time += now - info->timestamp; |
| 585 | info->timestamp = now; |
| 586 | } |
| 587 | |
| 588 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 589 | { |
| 590 | struct perf_cgroup *cgrp_out = cpuctx->cgrp; |
| 591 | if (cgrp_out) |
| 592 | __update_cgrp_time(cgrp_out); |
| 593 | } |
| 594 | |
| 595 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 596 | { |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 597 | struct perf_cgroup *cgrp; |
| 598 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 599 | /* |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 600 | * ensure we access cgroup data only when needed and |
| 601 | * when we know the cgroup is pinned (css_get) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 602 | */ |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 603 | if (!is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 604 | return; |
| 605 | |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 606 | cgrp = perf_cgroup_from_task(current, event->ctx); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 607 | /* |
| 608 | * Do not update time when cgroup is not active |
| 609 | */ |
| 610 | if (cgrp == event->cgrp) |
| 611 | __update_cgrp_time(event->cgrp); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 615 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 616 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 617 | { |
| 618 | struct perf_cgroup *cgrp; |
| 619 | struct perf_cgroup_info *info; |
| 620 | |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 621 | /* |
| 622 | * ctx->lock held by caller |
| 623 | * ensure we do not access cgroup data |
| 624 | * unless we have the cgroup pinned (css_get) |
| 625 | */ |
| 626 | if (!task || !ctx->nr_cgroups) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 627 | return; |
| 628 | |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 629 | cgrp = perf_cgroup_from_task(task, ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 630 | info = this_cpu_ptr(cgrp->info); |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 631 | info->timestamp = ctx->timestamp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ |
| 635 | #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ |
| 636 | |
| 637 | /* |
| 638 | * reschedule events based on the cgroup constraint of task. |
| 639 | * |
| 640 | * mode SWOUT : schedule out everything |
| 641 | * mode SWIN : schedule in based on cgroup for next |
| 642 | */ |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 643 | static void perf_cgroup_switch(struct task_struct *task, int mode) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 644 | { |
| 645 | struct perf_cpu_context *cpuctx; |
| 646 | struct pmu *pmu; |
| 647 | unsigned long flags; |
| 648 | |
| 649 | /* |
| 650 | * disable interrupts to avoid geting nr_cgroup |
| 651 | * changes via __perf_event_disable(). Also |
| 652 | * avoids preemption. |
| 653 | */ |
| 654 | local_irq_save(flags); |
| 655 | |
| 656 | /* |
| 657 | * we reschedule only in the presence of cgroup |
| 658 | * constrained events. |
| 659 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 660 | |
| 661 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 662 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 663 | if (cpuctx->unique_pmu != pmu) |
| 664 | continue; /* ensure we process each cpuctx once */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 665 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 666 | /* |
| 667 | * perf_cgroup_events says at least one |
| 668 | * context on this CPU has cgroup events. |
| 669 | * |
| 670 | * ctx->nr_cgroups reports the number of cgroup |
| 671 | * events for a context. |
| 672 | */ |
| 673 | if (cpuctx->ctx.nr_cgroups > 0) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 674 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 675 | perf_pmu_disable(cpuctx->ctx.pmu); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 676 | |
| 677 | if (mode & PERF_CGROUP_SWOUT) { |
| 678 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 679 | /* |
| 680 | * must not be done before ctxswout due |
| 681 | * to event_filter_match() in event_sched_out() |
| 682 | */ |
| 683 | cpuctx->cgrp = NULL; |
| 684 | } |
| 685 | |
| 686 | if (mode & PERF_CGROUP_SWIN) { |
Stephane Eranian | e566b76 | 2011-04-06 02:54:54 +0200 | [diff] [blame] | 687 | WARN_ON_ONCE(cpuctx->cgrp); |
Peter Zijlstra | 95cf59e | 2012-10-02 15:41:23 +0200 | [diff] [blame] | 688 | /* |
| 689 | * set cgrp before ctxsw in to allow |
| 690 | * event_filter_match() to not have to pass |
| 691 | * task around |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 692 | * we pass the cpuctx->ctx to perf_cgroup_from_task() |
| 693 | * because cgorup events are only per-cpu |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 694 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 695 | cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 696 | cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); |
| 697 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 698 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 699 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 700 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 701 | } |
| 702 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 703 | local_irq_restore(flags); |
| 704 | } |
| 705 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 706 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 707 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 708 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 709 | struct perf_cgroup *cgrp1; |
| 710 | struct perf_cgroup *cgrp2 = NULL; |
| 711 | |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 712 | rcu_read_lock(); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 713 | /* |
| 714 | * we come here when we know perf_cgroup_events > 0 |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 715 | * we do not need to pass the ctx here because we know |
| 716 | * we are holding the rcu lock |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 717 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 718 | cgrp1 = perf_cgroup_from_task(task, NULL); |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 719 | cgrp2 = perf_cgroup_from_task(next, NULL); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 720 | |
| 721 | /* |
| 722 | * only schedule out current cgroup events if we know |
| 723 | * that we are switching to a different cgroup. Otherwise, |
| 724 | * do no touch the cgroup events. |
| 725 | */ |
| 726 | if (cgrp1 != cgrp2) |
| 727 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 728 | |
| 729 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 732 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 733 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 734 | { |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 735 | struct perf_cgroup *cgrp1; |
| 736 | struct perf_cgroup *cgrp2 = NULL; |
| 737 | |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 738 | rcu_read_lock(); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 739 | /* |
| 740 | * we come here when we know perf_cgroup_events > 0 |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 741 | * we do not need to pass the ctx here because we know |
| 742 | * we are holding the rcu lock |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 743 | */ |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 744 | cgrp1 = perf_cgroup_from_task(task, NULL); |
Stephane Eranian | 614e4c4 | 2015-11-12 11:00:04 +0100 | [diff] [blame] | 745 | cgrp2 = perf_cgroup_from_task(prev, NULL); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 746 | |
| 747 | /* |
| 748 | * only need to schedule in cgroup events if we are changing |
| 749 | * cgroup during ctxsw. Cgroup events were not scheduled |
| 750 | * out of ctxsw out if that was not the case. |
| 751 | */ |
| 752 | if (cgrp1 != cgrp2) |
| 753 | perf_cgroup_switch(task, PERF_CGROUP_SWIN); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 754 | |
| 755 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static inline int perf_cgroup_connect(int fd, struct perf_event *event, |
| 759 | struct perf_event_attr *attr, |
| 760 | struct perf_event *group_leader) |
| 761 | { |
| 762 | struct perf_cgroup *cgrp; |
| 763 | struct cgroup_subsys_state *css; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 764 | struct fd f = fdget(fd); |
| 765 | int ret = 0; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 766 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 767 | if (!f.file) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 768 | return -EBADF; |
| 769 | |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 770 | css = css_tryget_online_from_dir(f.file->f_path.dentry, |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 771 | &perf_event_cgrp_subsys); |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 772 | if (IS_ERR(css)) { |
| 773 | ret = PTR_ERR(css); |
| 774 | goto out; |
| 775 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 776 | |
| 777 | cgrp = container_of(css, struct perf_cgroup, css); |
| 778 | event->cgrp = cgrp; |
| 779 | |
| 780 | /* |
| 781 | * all events in a group must monitor |
| 782 | * the same cgroup because a task belongs |
| 783 | * to only one perf cgroup at a time |
| 784 | */ |
| 785 | if (group_leader && group_leader->cgrp != cgrp) { |
| 786 | perf_detach_cgroup(event); |
| 787 | ret = -EINVAL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 788 | } |
Li Zefan | 3db272c | 2011-03-03 14:25:37 +0800 | [diff] [blame] | 789 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 790 | fdput(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | static inline void |
| 795 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 796 | { |
| 797 | struct perf_cgroup_info *t; |
| 798 | t = per_cpu_ptr(event->cgrp->info, event->cpu); |
| 799 | event->shadow_ctx_time = now - t->timestamp; |
| 800 | } |
| 801 | |
| 802 | static inline void |
| 803 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 804 | { |
| 805 | /* |
| 806 | * when the current task's perf cgroup does not match |
| 807 | * the event's, we need to remember to call the |
| 808 | * perf_mark_enable() function the first time a task with |
| 809 | * a matching perf cgroup is scheduled in. |
| 810 | */ |
| 811 | if (is_cgroup_event(event) && !perf_cgroup_match(event)) |
| 812 | event->cgrp_defer_enabled = 1; |
| 813 | } |
| 814 | |
| 815 | static inline void |
| 816 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 817 | struct perf_event_context *ctx) |
| 818 | { |
| 819 | struct perf_event *sub; |
| 820 | u64 tstamp = perf_event_time(event); |
| 821 | |
| 822 | if (!event->cgrp_defer_enabled) |
| 823 | return; |
| 824 | |
| 825 | event->cgrp_defer_enabled = 0; |
| 826 | |
| 827 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
| 828 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 829 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
| 830 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
| 831 | sub->cgrp_defer_enabled = 0; |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | #else /* !CONFIG_CGROUP_PERF */ |
| 836 | |
| 837 | static inline bool |
| 838 | perf_cgroup_match(struct perf_event *event) |
| 839 | { |
| 840 | return true; |
| 841 | } |
| 842 | |
| 843 | static inline void perf_detach_cgroup(struct perf_event *event) |
| 844 | {} |
| 845 | |
| 846 | static inline int is_cgroup_event(struct perf_event *event) |
| 847 | { |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) |
| 852 | { |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static inline void update_cgrp_time_from_event(struct perf_event *event) |
| 857 | { |
| 858 | } |
| 859 | |
| 860 | static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) |
| 861 | { |
| 862 | } |
| 863 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 864 | static inline void perf_cgroup_sched_out(struct task_struct *task, |
| 865 | struct task_struct *next) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 866 | { |
| 867 | } |
| 868 | |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 869 | static inline void perf_cgroup_sched_in(struct task_struct *prev, |
| 870 | struct task_struct *task) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 871 | { |
| 872 | } |
| 873 | |
| 874 | static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, |
| 875 | struct perf_event_attr *attr, |
| 876 | struct perf_event *group_leader) |
| 877 | { |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | |
| 881 | static inline void |
Stephane Eranian | 3f7cce3 | 2011-02-18 14:40:01 +0200 | [diff] [blame] | 882 | perf_cgroup_set_timestamp(struct task_struct *task, |
| 883 | struct perf_event_context *ctx) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 884 | { |
| 885 | } |
| 886 | |
| 887 | void |
| 888 | perf_cgroup_switch(struct task_struct *task, struct task_struct *next) |
| 889 | { |
| 890 | } |
| 891 | |
| 892 | static inline void |
| 893 | perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) |
| 894 | { |
| 895 | } |
| 896 | |
| 897 | static inline u64 perf_cgroup_event_time(struct perf_event *event) |
| 898 | { |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | static inline void |
| 903 | perf_cgroup_defer_enabled(struct perf_event *event) |
| 904 | { |
| 905 | } |
| 906 | |
| 907 | static inline void |
| 908 | perf_cgroup_mark_enabled(struct perf_event *event, |
| 909 | struct perf_event_context *ctx) |
| 910 | { |
| 911 | } |
| 912 | #endif |
| 913 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 914 | /* |
| 915 | * set default to be dependent on timer tick just |
| 916 | * like original code |
| 917 | */ |
| 918 | #define PERF_CPU_HRTIMER (1000 / HZ) |
| 919 | /* |
| 920 | * function must be called with interrupts disbled |
| 921 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 922 | static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 923 | { |
| 924 | struct perf_cpu_context *cpuctx; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 925 | int rotations = 0; |
| 926 | |
| 927 | WARN_ON(!irqs_disabled()); |
| 928 | |
| 929 | cpuctx = container_of(hr, struct perf_cpu_context, hrtimer); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 930 | rotations = perf_rotate_context(cpuctx); |
| 931 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 932 | raw_spin_lock(&cpuctx->hrtimer_lock); |
| 933 | if (rotations) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 934 | hrtimer_forward_now(hr, cpuctx->hrtimer_interval); |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 935 | else |
| 936 | cpuctx->hrtimer_active = 0; |
| 937 | raw_spin_unlock(&cpuctx->hrtimer_lock); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 938 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 939 | return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 940 | } |
| 941 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 942 | 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] | 943 | { |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 944 | struct hrtimer *timer = &cpuctx->hrtimer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 945 | struct pmu *pmu = cpuctx->ctx.pmu; |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 946 | u64 interval; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 947 | |
| 948 | /* no multiplexing needed for SW PMU */ |
| 949 | if (pmu->task_ctx_nr == perf_sw_context) |
| 950 | return; |
| 951 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 952 | /* |
| 953 | * check default is sane, if not set then force to |
| 954 | * default interval (1/tick) |
| 955 | */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 956 | interval = pmu->hrtimer_interval_ms; |
| 957 | if (interval < 1) |
| 958 | interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 959 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 960 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 961 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 962 | raw_spin_lock_init(&cpuctx->hrtimer_lock); |
| 963 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 964 | timer->function = perf_mux_hrtimer_handler; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 965 | } |
| 966 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 967 | static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx) |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 968 | { |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 969 | struct hrtimer *timer = &cpuctx->hrtimer; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 970 | struct pmu *pmu = cpuctx->ctx.pmu; |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 971 | unsigned long flags; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 972 | |
| 973 | /* not for SW PMU */ |
| 974 | if (pmu->task_ctx_nr == perf_sw_context) |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 975 | return 0; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 976 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 977 | raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags); |
| 978 | if (!cpuctx->hrtimer_active) { |
| 979 | cpuctx->hrtimer_active = 1; |
| 980 | hrtimer_forward_now(timer, cpuctx->hrtimer_interval); |
| 981 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); |
| 982 | } |
| 983 | raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 984 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 985 | return 0; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 986 | } |
| 987 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 988 | void perf_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 989 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 990 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 991 | if (!(*count)++) |
| 992 | pmu->pmu_disable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 993 | } |
| 994 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 995 | void perf_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 996 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 997 | int *count = this_cpu_ptr(pmu->pmu_disable_count); |
| 998 | if (!--(*count)) |
| 999 | pmu->pmu_enable(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1002 | static DEFINE_PER_CPU(struct list_head, active_ctx_list); |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1003 | |
| 1004 | /* |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1005 | * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and |
| 1006 | * perf_event_task_tick() are fully serialized because they're strictly cpu |
| 1007 | * affine and perf_event_ctx{activate,deactivate} are called with IRQs |
| 1008 | * disabled, while perf_event_task_tick is called from IRQ context. |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1009 | */ |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1010 | static void perf_event_ctx_activate(struct perf_event_context *ctx) |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1011 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1012 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 1013 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 1014 | WARN_ON(!irqs_disabled()); |
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 | WARN_ON(!list_empty(&ctx->active_ctx_list)); |
| 1017 | |
| 1018 | list_add(&ctx->active_ctx_list, head); |
| 1019 | } |
| 1020 | |
| 1021 | static void perf_event_ctx_deactivate(struct perf_event_context *ctx) |
| 1022 | { |
| 1023 | WARN_ON(!irqs_disabled()); |
| 1024 | |
| 1025 | WARN_ON(list_empty(&ctx->active_ctx_list)); |
| 1026 | |
| 1027 | list_del_init(&ctx->active_ctx_list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | static void get_ctx(struct perf_event_context *ctx) |
| 1031 | { |
| 1032 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 1033 | } |
| 1034 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 1035 | static void free_ctx(struct rcu_head *head) |
| 1036 | { |
| 1037 | struct perf_event_context *ctx; |
| 1038 | |
| 1039 | ctx = container_of(head, struct perf_event_context, rcu_head); |
| 1040 | kfree(ctx->task_ctx_data); |
| 1041 | kfree(ctx); |
| 1042 | } |
| 1043 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1044 | static void put_ctx(struct perf_event_context *ctx) |
| 1045 | { |
| 1046 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 1047 | if (ctx->parent_ctx) |
| 1048 | put_ctx(ctx->parent_ctx); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1049 | if (ctx->task && ctx->task != TASK_TOMBSTONE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1050 | put_task_struct(ctx->task); |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 1051 | call_rcu(&ctx->rcu_head, free_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1055 | /* |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1056 | * Because of perf_event::ctx migration in sys_perf_event_open::move_group and |
| 1057 | * perf_pmu_migrate_context() we need some magic. |
| 1058 | * |
| 1059 | * Those places that change perf_event::ctx will hold both |
| 1060 | * perf_event_ctx::mutex of the 'old' and 'new' ctx value. |
| 1061 | * |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 1062 | * Lock ordering is by mutex address. There are two other sites where |
| 1063 | * perf_event_context::mutex nests and those are: |
| 1064 | * |
| 1065 | * - perf_event_exit_task_context() [ child , 0 ] |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 1066 | * perf_event_exit_event() |
| 1067 | * put_event() [ parent, 1 ] |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 1068 | * |
| 1069 | * - perf_event_init_context() [ parent, 0 ] |
| 1070 | * inherit_task_group() |
| 1071 | * inherit_group() |
| 1072 | * inherit_event() |
| 1073 | * perf_event_alloc() |
| 1074 | * perf_init_event() |
| 1075 | * perf_try_init_event() [ child , 1 ] |
| 1076 | * |
| 1077 | * While it appears there is an obvious deadlock here -- the parent and child |
| 1078 | * nesting levels are inverted between the two. This is in fact safe because |
| 1079 | * life-time rules separate them. That is an exiting task cannot fork, and a |
| 1080 | * spawning task cannot (yet) exit. |
| 1081 | * |
| 1082 | * But remember that that these are parent<->child context relations, and |
| 1083 | * migration does not affect children, therefore these two orderings should not |
| 1084 | * interact. |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1085 | * |
| 1086 | * The change in perf_event::ctx does not affect children (as claimed above) |
| 1087 | * because the sys_perf_event_open() case will install a new event and break |
| 1088 | * the ctx parent<->child relation, and perf_pmu_migrate_context() is only |
| 1089 | * concerned with cpuctx and that doesn't have children. |
| 1090 | * |
| 1091 | * The places that change perf_event::ctx will issue: |
| 1092 | * |
| 1093 | * perf_remove_from_context(); |
| 1094 | * synchronize_rcu(); |
| 1095 | * perf_install_in_context(); |
| 1096 | * |
| 1097 | * to affect the change. The remove_from_context() + synchronize_rcu() should |
| 1098 | * quiesce the event, after which we can install it in the new location. This |
| 1099 | * means that only external vectors (perf_fops, prctl) can perturb the event |
| 1100 | * while in transit. Therefore all such accessors should also acquire |
| 1101 | * perf_event_context::mutex to serialize against this. |
| 1102 | * |
| 1103 | * However; because event->ctx can change while we're waiting to acquire |
| 1104 | * ctx->mutex we must be careful and use the below perf_event_ctx_lock() |
| 1105 | * function. |
| 1106 | * |
| 1107 | * Lock order: |
| 1108 | * task_struct::perf_event_mutex |
| 1109 | * perf_event_context::mutex |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1110 | * perf_event::child_mutex; |
Peter Zijlstra | 07c4a77 | 2016-01-26 12:15:37 +0100 | [diff] [blame] | 1111 | * perf_event_context::lock |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1112 | * perf_event::mmap_mutex |
| 1113 | * mmap_sem |
| 1114 | */ |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1115 | static struct perf_event_context * |
| 1116 | perf_event_ctx_lock_nested(struct perf_event *event, int nesting) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1117 | { |
| 1118 | struct perf_event_context *ctx; |
| 1119 | |
| 1120 | again: |
| 1121 | rcu_read_lock(); |
| 1122 | ctx = ACCESS_ONCE(event->ctx); |
| 1123 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
| 1124 | rcu_read_unlock(); |
| 1125 | goto again; |
| 1126 | } |
| 1127 | rcu_read_unlock(); |
| 1128 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1129 | mutex_lock_nested(&ctx->mutex, nesting); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1130 | if (event->ctx != ctx) { |
| 1131 | mutex_unlock(&ctx->mutex); |
| 1132 | put_ctx(ctx); |
| 1133 | goto again; |
| 1134 | } |
| 1135 | |
| 1136 | return ctx; |
| 1137 | } |
| 1138 | |
Peter Zijlstra | a83fe28 | 2015-01-29 14:44:34 +0100 | [diff] [blame] | 1139 | static inline struct perf_event_context * |
| 1140 | perf_event_ctx_lock(struct perf_event *event) |
| 1141 | { |
| 1142 | return perf_event_ctx_lock_nested(event, 0); |
| 1143 | } |
| 1144 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1145 | static void perf_event_ctx_unlock(struct perf_event *event, |
| 1146 | struct perf_event_context *ctx) |
| 1147 | { |
| 1148 | mutex_unlock(&ctx->mutex); |
| 1149 | put_ctx(ctx); |
| 1150 | } |
| 1151 | |
| 1152 | /* |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1153 | * This must be done under the ctx->lock, such as to serialize against |
| 1154 | * context_equiv(), therefore we cannot call put_ctx() since that might end up |
| 1155 | * calling scheduler related locks and ctx->lock nests inside those. |
| 1156 | */ |
| 1157 | static __must_check struct perf_event_context * |
| 1158 | unclone_ctx(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1159 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1160 | struct perf_event_context *parent_ctx = ctx->parent_ctx; |
| 1161 | |
| 1162 | lockdep_assert_held(&ctx->lock); |
| 1163 | |
| 1164 | if (parent_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1165 | ctx->parent_ctx = NULL; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1166 | ctx->generation++; |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 1167 | |
| 1168 | return parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1171 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 1172 | { |
| 1173 | /* |
| 1174 | * only top level events have the pid namespace they were created in |
| 1175 | */ |
| 1176 | if (event->parent) |
| 1177 | event = event->parent; |
| 1178 | |
| 1179 | return task_tgid_nr_ns(p, event->ns); |
| 1180 | } |
| 1181 | |
| 1182 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 1183 | { |
| 1184 | /* |
| 1185 | * only top level events have the pid namespace they were created in |
| 1186 | */ |
| 1187 | if (event->parent) |
| 1188 | event = event->parent; |
| 1189 | |
| 1190 | return task_pid_nr_ns(p, event->ns); |
| 1191 | } |
| 1192 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1193 | /* |
| 1194 | * If we inherit events we want to return the parent event id |
| 1195 | * to userspace. |
| 1196 | */ |
| 1197 | static u64 primary_event_id(struct perf_event *event) |
| 1198 | { |
| 1199 | u64 id = event->id; |
| 1200 | |
| 1201 | if (event->parent) |
| 1202 | id = event->parent->id; |
| 1203 | |
| 1204 | return id; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * Get the perf_event_context for a task and lock it. |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1209 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1210 | * This has to cope with with the fact that until it is locked, |
| 1211 | * the context could get moved to another task. |
| 1212 | */ |
| 1213 | static struct perf_event_context * |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1214 | 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] | 1215 | { |
| 1216 | struct perf_event_context *ctx; |
| 1217 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1218 | retry: |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1219 | /* |
| 1220 | * One of the few rules of preemptible RCU is that one cannot do |
| 1221 | * rcu_read_unlock() while holding a scheduler (or nested) lock when |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1222 | * part of the read side critical section was irqs-enabled -- see |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1223 | * rcu_read_unlock_special(). |
| 1224 | * |
| 1225 | * 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] | 1226 | * side critical section has interrupts disabled. |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1227 | */ |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1228 | local_irq_save(*flags); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1229 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1230 | ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1231 | if (ctx) { |
| 1232 | /* |
| 1233 | * If this context is a clone of another, it might |
| 1234 | * get swapped for another underneath us by |
| 1235 | * perf_event_task_sched_out, though the |
| 1236 | * rcu_read_lock() protects us from any context |
| 1237 | * getting freed. Lock the context and check if it |
| 1238 | * got swapped before we could get the lock, and retry |
| 1239 | * if so. If we locked the right context, then it |
| 1240 | * can't get swapped on us any more. |
| 1241 | */ |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1242 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1243 | if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1244 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | 058ebd0 | 2013-07-12 11:08:33 +0200 | [diff] [blame] | 1245 | rcu_read_unlock(); |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1246 | local_irq_restore(*flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1247 | goto retry; |
| 1248 | } |
| 1249 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 1250 | if (ctx->task == TASK_TOMBSTONE || |
| 1251 | !atomic_inc_not_zero(&ctx->refcount)) { |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1252 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1253 | ctx = NULL; |
Peter Zijlstra | 828b6f0 | 2016-01-27 21:59:04 +0100 | [diff] [blame] | 1254 | } else { |
| 1255 | WARN_ON_ONCE(ctx->task != task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | rcu_read_unlock(); |
Paul E. McKenney | 2fd5907 | 2015-11-04 05:48:38 -0800 | [diff] [blame] | 1259 | if (!ctx) |
| 1260 | local_irq_restore(*flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1261 | return ctx; |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Get the context for a task and increment its pin_count so it |
| 1266 | * can't get swapped to another task. This also increments its |
| 1267 | * reference count so that the context can't get freed. |
| 1268 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1269 | static struct perf_event_context * |
| 1270 | perf_pin_task_context(struct task_struct *task, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1271 | { |
| 1272 | struct perf_event_context *ctx; |
| 1273 | unsigned long flags; |
| 1274 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 1275 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1276 | if (ctx) { |
| 1277 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1278 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1279 | } |
| 1280 | return ctx; |
| 1281 | } |
| 1282 | |
| 1283 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 1284 | { |
| 1285 | unsigned long flags; |
| 1286 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1287 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1288 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1289 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1290 | } |
| 1291 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1292 | /* |
| 1293 | * Update the record of the current time in a context. |
| 1294 | */ |
| 1295 | static void update_context_time(struct perf_event_context *ctx) |
| 1296 | { |
| 1297 | u64 now = perf_clock(); |
| 1298 | |
| 1299 | ctx->time += now - ctx->timestamp; |
| 1300 | ctx->timestamp = now; |
| 1301 | } |
| 1302 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1303 | static u64 perf_event_time(struct perf_event *event) |
| 1304 | { |
| 1305 | struct perf_event_context *ctx = event->ctx; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1306 | |
| 1307 | if (is_cgroup_event(event)) |
| 1308 | return perf_cgroup_event_time(event); |
| 1309 | |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1310 | return ctx ? ctx->time : 0; |
| 1311 | } |
| 1312 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1313 | /* |
| 1314 | * Update the total_time_enabled and total_time_running fields for a event. |
| 1315 | */ |
| 1316 | static void update_event_times(struct perf_event *event) |
| 1317 | { |
| 1318 | struct perf_event_context *ctx = event->ctx; |
| 1319 | u64 run_end; |
| 1320 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 1321 | lockdep_assert_held(&ctx->lock); |
| 1322 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1323 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 1324 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 1325 | return; |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 1326 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1327 | /* |
| 1328 | * in cgroup mode, time_enabled represents |
| 1329 | * the time the event was enabled AND active |
| 1330 | * tasks were in the monitored cgroup. This is |
| 1331 | * independent of the activity of the context as |
| 1332 | * there may be a mix of cgroup and non-cgroup events. |
| 1333 | * |
| 1334 | * That is why we treat cgroup events differently |
| 1335 | * here. |
| 1336 | */ |
| 1337 | if (is_cgroup_event(event)) |
Namhyung Kim | 46cd6a7f | 2012-01-20 10:12:46 +0900 | [diff] [blame] | 1338 | run_end = perf_cgroup_event_time(event); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1339 | else if (ctx->is_active) |
| 1340 | run_end = ctx->time; |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 1341 | else |
| 1342 | run_end = event->tstamp_stopped; |
| 1343 | |
| 1344 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1345 | |
| 1346 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 1347 | run_end = event->tstamp_stopped; |
| 1348 | else |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1349 | run_end = perf_event_time(event); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1350 | |
| 1351 | event->total_time_running = run_end - event->tstamp_running; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1352 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 1353 | } |
| 1354 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1355 | /* |
| 1356 | * Update total_time_enabled and total_time_running for all events in a group. |
| 1357 | */ |
| 1358 | static void update_group_times(struct perf_event *leader) |
| 1359 | { |
| 1360 | struct perf_event *event; |
| 1361 | |
| 1362 | update_event_times(leader); |
| 1363 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 1364 | update_event_times(event); |
| 1365 | } |
| 1366 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1367 | static struct list_head * |
| 1368 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 1369 | { |
| 1370 | if (event->attr.pinned) |
| 1371 | return &ctx->pinned_groups; |
| 1372 | else |
| 1373 | return &ctx->flexible_groups; |
| 1374 | } |
| 1375 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1376 | /* |
| 1377 | * Add a event from the lists for its context. |
| 1378 | * Must be called with ctx->mutex and ctx->lock held. |
| 1379 | */ |
| 1380 | static void |
| 1381 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1382 | { |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 1383 | lockdep_assert_held(&ctx->lock); |
| 1384 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1385 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 1386 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1387 | |
| 1388 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1389 | * If we're a stand alone event or group leader, we go to the context |
| 1390 | * list, group events are kept attached to the group so that |
| 1391 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1392 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1393 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1394 | struct list_head *list; |
| 1395 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1396 | if (is_software_event(event)) |
| 1397 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 1398 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1399 | list = ctx_group_list(event, ctx); |
| 1400 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1401 | } |
| 1402 | |
Peter Zijlstra | 0830937 | 2011-03-03 11:31:20 +0100 | [diff] [blame] | 1403 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1404 | ctx->nr_cgroups++; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1405 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1406 | list_add_rcu(&event->event_entry, &ctx->event_list); |
| 1407 | ctx->nr_events++; |
| 1408 | if (event->attr.inherit_stat) |
| 1409 | ctx->nr_stat++; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1410 | |
| 1411 | ctx->generation++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1412 | } |
| 1413 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1414 | /* |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 1415 | * Initialize event state based on the perf_event_attr::disabled. |
| 1416 | */ |
| 1417 | static inline void perf_event__state_init(struct perf_event *event) |
| 1418 | { |
| 1419 | event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : |
| 1420 | PERF_EVENT_STATE_INACTIVE; |
| 1421 | } |
| 1422 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1423 | 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] | 1424 | { |
| 1425 | int entry = sizeof(u64); /* value */ |
| 1426 | int size = 0; |
| 1427 | int nr = 1; |
| 1428 | |
| 1429 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1430 | size += sizeof(u64); |
| 1431 | |
| 1432 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1433 | size += sizeof(u64); |
| 1434 | |
| 1435 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 1436 | entry += sizeof(u64); |
| 1437 | |
| 1438 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1439 | nr += nr_siblings; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1440 | size += sizeof(u64); |
| 1441 | } |
| 1442 | |
| 1443 | size += entry * nr; |
| 1444 | event->read_size = size; |
| 1445 | } |
| 1446 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1447 | 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] | 1448 | { |
| 1449 | struct perf_sample_data *data; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1450 | u16 size = 0; |
| 1451 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1452 | if (sample_type & PERF_SAMPLE_IP) |
| 1453 | size += sizeof(data->ip); |
| 1454 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1455 | if (sample_type & PERF_SAMPLE_ADDR) |
| 1456 | size += sizeof(data->addr); |
| 1457 | |
| 1458 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 1459 | size += sizeof(data->period); |
| 1460 | |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 1461 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1462 | size += sizeof(data->weight); |
| 1463 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1464 | if (sample_type & PERF_SAMPLE_READ) |
| 1465 | size += event->read_size; |
| 1466 | |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 1467 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1468 | size += sizeof(data->data_src.val); |
| 1469 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 1470 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1471 | size += sizeof(data->txn); |
| 1472 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1473 | event->header_size = size; |
| 1474 | } |
| 1475 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1476 | /* |
| 1477 | * Called at perf_event creation and when events are attached/detached from a |
| 1478 | * group. |
| 1479 | */ |
| 1480 | static void perf_event__header_size(struct perf_event *event) |
| 1481 | { |
| 1482 | __perf_event_read_size(event, |
| 1483 | event->group_leader->nr_siblings); |
| 1484 | __perf_event_header_size(event, event->attr.sample_type); |
| 1485 | } |
| 1486 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1487 | static void perf_event__id_header_size(struct perf_event *event) |
| 1488 | { |
| 1489 | struct perf_sample_data *data; |
| 1490 | u64 sample_type = event->attr.sample_type; |
| 1491 | u16 size = 0; |
| 1492 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1493 | if (sample_type & PERF_SAMPLE_TID) |
| 1494 | size += sizeof(data->tid_entry); |
| 1495 | |
| 1496 | if (sample_type & PERF_SAMPLE_TIME) |
| 1497 | size += sizeof(data->time); |
| 1498 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 1499 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1500 | size += sizeof(data->id); |
| 1501 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1502 | if (sample_type & PERF_SAMPLE_ID) |
| 1503 | size += sizeof(data->id); |
| 1504 | |
| 1505 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1506 | size += sizeof(data->stream_id); |
| 1507 | |
| 1508 | if (sample_type & PERF_SAMPLE_CPU) |
| 1509 | size += sizeof(data->cpu_entry); |
| 1510 | |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 1511 | event->id_header_size = size; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1512 | } |
| 1513 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 1514 | static bool perf_event_validate_size(struct perf_event *event) |
| 1515 | { |
| 1516 | /* |
| 1517 | * The values computed here will be over-written when we actually |
| 1518 | * attach the event. |
| 1519 | */ |
| 1520 | __perf_event_read_size(event, event->group_leader->nr_siblings + 1); |
| 1521 | __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ); |
| 1522 | perf_event__id_header_size(event); |
| 1523 | |
| 1524 | /* |
| 1525 | * Sum the lot; should not exceed the 64k limit we have on records. |
| 1526 | * Conservative limit to allow for callchains and other variable fields. |
| 1527 | */ |
| 1528 | if (event->read_size + event->header_size + |
| 1529 | event->id_header_size + sizeof(struct perf_event_header) >= 16*1024) |
| 1530 | return false; |
| 1531 | |
| 1532 | return true; |
| 1533 | } |
| 1534 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1535 | static void perf_group_attach(struct perf_event *event) |
| 1536 | { |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1537 | struct perf_event *group_leader = event->group_leader, *pos; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1538 | |
Peter Zijlstra | 74c3337 | 2010-10-15 11:40:29 +0200 | [diff] [blame] | 1539 | /* |
| 1540 | * We can have double attach due to group movement in perf_event_open. |
| 1541 | */ |
| 1542 | if (event->attach_state & PERF_ATTACH_GROUP) |
| 1543 | return; |
| 1544 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1545 | event->attach_state |= PERF_ATTACH_GROUP; |
| 1546 | |
| 1547 | if (group_leader == event) |
| 1548 | return; |
| 1549 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1550 | WARN_ON_ONCE(group_leader->ctx != event->ctx); |
| 1551 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1552 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 1553 | !is_software_event(event)) |
| 1554 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 1555 | |
| 1556 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 1557 | group_leader->nr_siblings++; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1558 | |
| 1559 | perf_event__header_size(group_leader); |
| 1560 | |
| 1561 | list_for_each_entry(pos, &group_leader->sibling_list, group_entry) |
| 1562 | perf_event__header_size(pos); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1565 | /* |
| 1566 | * Remove a event from the lists for its context. |
| 1567 | * Must be called with ctx->mutex and ctx->lock held. |
| 1568 | */ |
| 1569 | static void |
| 1570 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 1571 | { |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1572 | struct perf_cpu_context *cpuctx; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1573 | |
| 1574 | WARN_ON_ONCE(event->ctx != ctx); |
| 1575 | lockdep_assert_held(&ctx->lock); |
| 1576 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1577 | /* |
| 1578 | * We can have double detach due to exit/hot-unplug + close. |
| 1579 | */ |
| 1580 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1581 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1582 | |
| 1583 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 1584 | |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1585 | if (is_cgroup_event(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1586 | ctx->nr_cgroups--; |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 1587 | /* |
| 1588 | * Because cgroup events are always per-cpu events, this will |
| 1589 | * always be called from the right CPU. |
| 1590 | */ |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1591 | cpuctx = __get_cpu_context(ctx); |
| 1592 | /* |
Peter Zijlstra | 70a0165 | 2016-01-08 09:29:16 +0100 | [diff] [blame] | 1593 | * If there are no more cgroup events then clear cgrp to avoid |
| 1594 | * stale pointer in update_cgrp_time_from_cpuctx(). |
Stephane Eranian | 68cacd2 | 2011-03-23 16:03:06 +0100 | [diff] [blame] | 1595 | */ |
| 1596 | if (!ctx->nr_cgroups) |
| 1597 | cpuctx->cgrp = NULL; |
| 1598 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1599 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1600 | ctx->nr_events--; |
| 1601 | if (event->attr.inherit_stat) |
| 1602 | ctx->nr_stat--; |
| 1603 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1604 | list_del_rcu(&event->event_entry); |
| 1605 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1606 | if (event->group_leader == event) |
| 1607 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1608 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 1609 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 1610 | |
| 1611 | /* |
| 1612 | * If event was in error state, then keep it |
| 1613 | * that way, otherwise bogus counts will be |
| 1614 | * returned on read(). The only way to get out |
| 1615 | * of error state is by explicit re-enabling |
| 1616 | * of the event |
| 1617 | */ |
| 1618 | if (event->state > PERF_EVENT_STATE_OFF) |
| 1619 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 1620 | |
| 1621 | ctx->generation++; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1624 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 1625 | { |
| 1626 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1627 | struct list_head *list = NULL; |
| 1628 | |
| 1629 | /* |
| 1630 | * We can have double detach due to exit/hot-unplug + close. |
| 1631 | */ |
| 1632 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 1633 | return; |
| 1634 | |
| 1635 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 1636 | |
| 1637 | /* |
| 1638 | * If this is a sibling, remove it from its group. |
| 1639 | */ |
| 1640 | if (event->group_leader != event) { |
| 1641 | list_del_init(&event->group_entry); |
| 1642 | event->group_leader->nr_siblings--; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1643 | goto out; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | if (!list_empty(&event->group_entry)) |
| 1647 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 1648 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1649 | /* |
| 1650 | * If this was a group event with sibling events then |
| 1651 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1652 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1653 | */ |
| 1654 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 1655 | if (list) |
| 1656 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1657 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* Inherit group flags from the previous leader */ |
| 1660 | sibling->group_flags = event->group_flags; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1661 | |
| 1662 | WARN_ON_ONCE(sibling->ctx != event->ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1663 | } |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 1664 | |
| 1665 | out: |
| 1666 | perf_event__header_size(event->group_leader); |
| 1667 | |
| 1668 | list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) |
| 1669 | perf_event__header_size(tmp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1670 | } |
| 1671 | |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1672 | static bool is_orphaned_event(struct perf_event *event) |
| 1673 | { |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 1674 | return event->state == PERF_EVENT_STATE_DEAD; |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 1675 | } |
| 1676 | |
Mark Rutland | 66eb579 | 2015-05-13 17:12:23 +0100 | [diff] [blame] | 1677 | static inline int pmu_filter_match(struct perf_event *event) |
| 1678 | { |
| 1679 | struct pmu *pmu = event->pmu; |
| 1680 | return pmu->filter_match ? pmu->filter_match(event) : 1; |
| 1681 | } |
| 1682 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1683 | static inline int |
| 1684 | event_filter_match(struct perf_event *event) |
| 1685 | { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1686 | return (event->cpu == -1 || event->cpu == smp_processor_id()) |
Mark Rutland | 66eb579 | 2015-05-13 17:12:23 +0100 | [diff] [blame] | 1687 | && perf_cgroup_match(event) && pmu_filter_match(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1688 | } |
| 1689 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1690 | static void |
| 1691 | event_sched_out(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1692 | struct perf_cpu_context *cpuctx, |
| 1693 | struct perf_event_context *ctx) |
| 1694 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1695 | u64 tstamp = perf_event_time(event); |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1696 | u64 delta; |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 1697 | |
| 1698 | WARN_ON_ONCE(event->ctx != ctx); |
| 1699 | lockdep_assert_held(&ctx->lock); |
| 1700 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1701 | /* |
| 1702 | * An event which could not be activated because of |
| 1703 | * filter mismatch still needs to have its timings |
| 1704 | * maintained, otherwise bogus information is return |
| 1705 | * via read() for time_enabled, time_running: |
| 1706 | */ |
| 1707 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 1708 | && !event_filter_match(event)) { |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1709 | delta = tstamp - event->tstamp_stopped; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1710 | event->tstamp_running += delta; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1711 | event->tstamp_stopped = tstamp; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1712 | } |
| 1713 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1714 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1715 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1716 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1717 | perf_pmu_disable(event->pmu); |
| 1718 | |
Peter Zijlstra | 28a967c | 2016-02-24 18:45:46 +0100 | [diff] [blame] | 1719 | event->tstamp_stopped = tstamp; |
| 1720 | event->pmu->del(event, 0); |
| 1721 | event->oncpu = -1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1722 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1723 | if (event->pending_disable) { |
| 1724 | event->pending_disable = 0; |
| 1725 | event->state = PERF_EVENT_STATE_OFF; |
| 1726 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1727 | |
| 1728 | if (!is_software_event(event)) |
| 1729 | cpuctx->active_oncpu--; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1730 | if (!--ctx->nr_active) |
| 1731 | perf_event_ctx_deactivate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1732 | if (event->attr.freq && event->attr.sample_freq) |
| 1733 | ctx->nr_freq--; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1734 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1735 | cpuctx->exclusive = 0; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1736 | |
| 1737 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | static void |
| 1741 | group_sched_out(struct perf_event *group_event, |
| 1742 | struct perf_cpu_context *cpuctx, |
| 1743 | struct perf_event_context *ctx) |
| 1744 | { |
| 1745 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1746 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1747 | |
| 1748 | event_sched_out(group_event, cpuctx, ctx); |
| 1749 | |
| 1750 | /* |
| 1751 | * Schedule out siblings (if any): |
| 1752 | */ |
| 1753 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 1754 | event_sched_out(event, cpuctx, ctx); |
| 1755 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 1756 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1757 | cpuctx->exclusive = 0; |
| 1758 | } |
| 1759 | |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1760 | #define DETACH_GROUP 0x01UL |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 1761 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1762 | /* |
| 1763 | * Cross CPU call to remove a performance event |
| 1764 | * |
| 1765 | * We disable the event on the hardware level first. After that we |
| 1766 | * remove it from the context list. |
| 1767 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1768 | static void |
| 1769 | __perf_remove_from_context(struct perf_event *event, |
| 1770 | struct perf_cpu_context *cpuctx, |
| 1771 | struct perf_event_context *ctx, |
| 1772 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1773 | { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1774 | unsigned long flags = (unsigned long)info; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1775 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1776 | event_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1777 | if (flags & DETACH_GROUP) |
Peter Zijlstra | 46ce0fe | 2014-05-02 16:56:01 +0200 | [diff] [blame] | 1778 | perf_group_detach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1779 | list_del_event(event, ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1780 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 1781 | if (!ctx->nr_events && ctx->is_active) { |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 1782 | ctx->is_active = 0; |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 1783 | if (ctx->task) { |
| 1784 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 1785 | cpuctx->task_ctx = NULL; |
| 1786 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1787 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1788 | } |
| 1789 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1790 | /* |
| 1791 | * Remove the event from a task's (or a CPU's) list of events. |
| 1792 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1793 | * If event->ctx is a cloned context, callers must make sure that |
| 1794 | * every task struct that event->ctx->task could possibly point to |
| 1795 | * remains valid. This is OK when called from perf_release since |
| 1796 | * that only calls us on the top-level context, which can't be a clone. |
| 1797 | * When called from perf_event_exit_task, it's OK because the |
| 1798 | * context has been detached from its task. |
| 1799 | */ |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1800 | 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] | 1801 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1802 | lockdep_assert_held(&event->ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1803 | |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 1804 | event_function_call(event, __perf_remove_from_context, (void *)flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1805 | } |
| 1806 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1807 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1808 | * Cross CPU call to disable a performance event |
| 1809 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1810 | static void __perf_event_disable(struct perf_event *event, |
| 1811 | struct perf_cpu_context *cpuctx, |
| 1812 | struct perf_event_context *ctx, |
| 1813 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1814 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1815 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 1816 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1817 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1818 | update_context_time(ctx); |
| 1819 | update_cgrp_time_from_event(event); |
| 1820 | update_group_times(event); |
| 1821 | if (event == event->group_leader) |
| 1822 | group_sched_out(event, cpuctx, ctx); |
| 1823 | else |
| 1824 | event_sched_out(event, cpuctx, ctx); |
| 1825 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1826 | } |
| 1827 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1828 | /* |
| 1829 | * Disable a event. |
| 1830 | * |
| 1831 | * If event->ctx is a cloned context, callers must make sure that |
| 1832 | * every task struct that event->ctx->task could possibly point to |
| 1833 | * remains valid. This condition is satisifed when called through |
| 1834 | * perf_event_for_each_child or perf_event_for_each because they |
| 1835 | * hold the top-level event's child_mutex, so any descendant that |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 1836 | * goes to exit will block in perf_event_exit_event(). |
| 1837 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1838 | * When called from perf_pending_event it's OK because event->ctx |
| 1839 | * is the current context on this CPU and preemption is disabled, |
| 1840 | * hence we can't get into perf_event_task_sched_out for this context. |
| 1841 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1842 | static void _perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1843 | { |
| 1844 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1845 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1846 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1847 | if (event->state <= PERF_EVENT_STATE_OFF) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1848 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1849 | return; |
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_unlock_irq(&ctx->lock); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 1852 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 1853 | event_function_call(event, __perf_event_disable, NULL); |
| 1854 | } |
| 1855 | |
| 1856 | void perf_event_disable_local(struct perf_event *event) |
| 1857 | { |
| 1858 | event_function_local(event, __perf_event_disable, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1859 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 1860 | |
| 1861 | /* |
| 1862 | * Strictly speaking kernel users cannot create groups and therefore this |
| 1863 | * interface does not need the perf_event_ctx_lock() magic. |
| 1864 | */ |
| 1865 | void perf_event_disable(struct perf_event *event) |
| 1866 | { |
| 1867 | struct perf_event_context *ctx; |
| 1868 | |
| 1869 | ctx = perf_event_ctx_lock(event); |
| 1870 | _perf_event_disable(event); |
| 1871 | perf_event_ctx_unlock(event, ctx); |
| 1872 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 1873 | EXPORT_SYMBOL_GPL(perf_event_disable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1874 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1875 | static void perf_set_shadow_time(struct perf_event *event, |
| 1876 | struct perf_event_context *ctx, |
| 1877 | u64 tstamp) |
| 1878 | { |
| 1879 | /* |
| 1880 | * use the correct time source for the time snapshot |
| 1881 | * |
| 1882 | * We could get by without this by leveraging the |
| 1883 | * fact that to get to this function, the caller |
| 1884 | * has most likely already called update_context_time() |
| 1885 | * and update_cgrp_time_xx() and thus both timestamp |
| 1886 | * are identical (or very close). Given that tstamp is, |
| 1887 | * already adjusted for cgroup, we could say that: |
| 1888 | * tstamp - ctx->timestamp |
| 1889 | * is equivalent to |
| 1890 | * tstamp - cgrp->timestamp. |
| 1891 | * |
| 1892 | * Then, in perf_output_read(), the calculation would |
| 1893 | * work with no changes because: |
| 1894 | * - event is guaranteed scheduled in |
| 1895 | * - no scheduled out in between |
| 1896 | * - thus the timestamp would be the same |
| 1897 | * |
| 1898 | * But this is a bit hairy. |
| 1899 | * |
| 1900 | * So instead, we have an explicit cgroup call to remain |
| 1901 | * within the time time source all along. We believe it |
| 1902 | * is cleaner and simpler to understand. |
| 1903 | */ |
| 1904 | if (is_cgroup_event(event)) |
| 1905 | perf_cgroup_set_shadow_time(event, tstamp); |
| 1906 | else |
| 1907 | event->shadow_ctx_time = tstamp - ctx->timestamp; |
| 1908 | } |
| 1909 | |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1910 | #define MAX_INTERRUPTS (~0ULL) |
| 1911 | |
| 1912 | static void perf_log_throttle(struct perf_event *event, int enable); |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 1913 | static void perf_log_itrace_start(struct perf_event *event); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1914 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1915 | static int |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1916 | event_sched_in(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1917 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1918 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1919 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1920 | u64 tstamp = perf_event_time(event); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1921 | int ret = 0; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 1922 | |
Peter Zijlstra | 6334241 | 2014-05-05 11:49:16 +0200 | [diff] [blame] | 1923 | lockdep_assert_held(&ctx->lock); |
| 1924 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1925 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 1926 | return 0; |
| 1927 | |
| 1928 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1929 | event->oncpu = smp_processor_id(); |
Peter Zijlstra | 4fe757d | 2011-02-15 22:26:07 +0100 | [diff] [blame] | 1930 | |
| 1931 | /* |
| 1932 | * Unthrottle events, since we scheduled we might have missed several |
| 1933 | * ticks already, also for a heavily scheduling task there is little |
| 1934 | * guarantee it'll get a tick in a timely manner. |
| 1935 | */ |
| 1936 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { |
| 1937 | perf_log_throttle(event, 1); |
| 1938 | event->hw.interrupts = 0; |
| 1939 | } |
| 1940 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1941 | /* |
| 1942 | * The new state must be visible before we turn it on in the hardware: |
| 1943 | */ |
| 1944 | smp_wmb(); |
| 1945 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1946 | perf_pmu_disable(event->pmu); |
| 1947 | |
Shaohua Li | 72f669c | 2015-02-05 15:55:31 -0800 | [diff] [blame] | 1948 | perf_set_shadow_time(event, ctx, tstamp); |
| 1949 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 1950 | perf_log_itrace_start(event); |
| 1951 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1952 | if (event->pmu->add(event, PERF_EF_START)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1953 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1954 | event->oncpu = -1; |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1955 | ret = -EAGAIN; |
| 1956 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1957 | } |
| 1958 | |
Peter Zijlstra | 00a2916 | 2015-07-27 10:35:07 +0200 | [diff] [blame] | 1959 | event->tstamp_running += tstamp - event->tstamp_stopped; |
| 1960 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1961 | if (!is_software_event(event)) |
| 1962 | cpuctx->active_oncpu++; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 1963 | if (!ctx->nr_active++) |
| 1964 | perf_event_ctx_activate(ctx); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 1965 | if (event->attr.freq && event->attr.sample_freq) |
| 1966 | ctx->nr_freq++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1967 | |
| 1968 | if (event->attr.exclusive) |
| 1969 | cpuctx->exclusive = 1; |
| 1970 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 1971 | out: |
| 1972 | perf_pmu_enable(event->pmu); |
| 1973 | |
| 1974 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | static int |
| 1978 | group_sched_in(struct perf_event *group_event, |
| 1979 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1980 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1981 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1982 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 4a23459 | 2014-02-24 12:43:31 +0100 | [diff] [blame] | 1983 | struct pmu *pmu = ctx->pmu; |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1984 | u64 now = ctx->time; |
| 1985 | bool simulate = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1986 | |
| 1987 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 1988 | return 0; |
| 1989 | |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 1990 | pmu->start_txn(pmu, PERF_PMU_TXN_ADD); |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 1991 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 1992 | if (event_sched_in(group_event, cpuctx, ctx)) { |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 1993 | pmu->cancel_txn(pmu); |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 1994 | perf_mux_hrtimer_restart(cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1995 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1996 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1997 | |
| 1998 | /* |
| 1999 | * Schedule in siblings as one group (if any): |
| 2000 | */ |
| 2001 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2002 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2003 | partial_group = event; |
| 2004 | goto group_error; |
| 2005 | } |
| 2006 | } |
| 2007 | |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2008 | if (!pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 2009 | return 0; |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2010 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2011 | group_error: |
| 2012 | /* |
| 2013 | * Groups can be scheduled in as one unit only, so undo any |
| 2014 | * partial group before returning: |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2015 | * The events up to the failed event are scheduled out normally, |
| 2016 | * tstamp_stopped will be updated. |
| 2017 | * |
| 2018 | * The failed events and the remaining siblings need to have |
| 2019 | * their timings updated as if they had gone thru event_sched_in() |
| 2020 | * and event_sched_out(). This is required to get consistent timings |
| 2021 | * across the group. This also takes care of the case where the group |
| 2022 | * could never be scheduled by ensuring tstamp_stopped is set to mark |
| 2023 | * the time the event was actually stopped, such that time delta |
| 2024 | * calculation in update_event_times() is correct. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2025 | */ |
| 2026 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 2027 | if (event == partial_group) |
Stephane Eranian | d7842da | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2028 | simulate = true; |
| 2029 | |
| 2030 | if (simulate) { |
| 2031 | event->tstamp_running += now - event->tstamp_stopped; |
| 2032 | event->tstamp_stopped = now; |
| 2033 | } else { |
| 2034 | event_sched_out(event, cpuctx, ctx); |
| 2035 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2036 | } |
Stephane Eranian | 9ffcfa6 | 2010-10-20 15:25:01 +0200 | [diff] [blame] | 2037 | event_sched_out(group_event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2038 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 2039 | pmu->cancel_txn(pmu); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 2040 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 2041 | perf_mux_hrtimer_restart(cpuctx); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 2042 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2043 | return -EAGAIN; |
| 2044 | } |
| 2045 | |
| 2046 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2047 | * Work out whether we can put this event group on the CPU now. |
| 2048 | */ |
| 2049 | static int group_can_go_on(struct perf_event *event, |
| 2050 | struct perf_cpu_context *cpuctx, |
| 2051 | int can_add_hw) |
| 2052 | { |
| 2053 | /* |
| 2054 | * Groups consisting entirely of software events can always go on. |
| 2055 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 2056 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2057 | return 1; |
| 2058 | /* |
| 2059 | * If an exclusive group is already on, no other hardware |
| 2060 | * events can go on. |
| 2061 | */ |
| 2062 | if (cpuctx->exclusive) |
| 2063 | return 0; |
| 2064 | /* |
| 2065 | * If this group is exclusive and there are already |
| 2066 | * events on the CPU, it can't go on. |
| 2067 | */ |
| 2068 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 2069 | return 0; |
| 2070 | /* |
| 2071 | * Otherwise, try to add it if all previous groups were able |
| 2072 | * to go on. |
| 2073 | */ |
| 2074 | return can_add_hw; |
| 2075 | } |
| 2076 | |
| 2077 | static void add_event_to_ctx(struct perf_event *event, |
| 2078 | struct perf_event_context *ctx) |
| 2079 | { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2080 | u64 tstamp = perf_event_time(event); |
| 2081 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2082 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 2083 | perf_group_attach(event); |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2084 | event->tstamp_enabled = tstamp; |
| 2085 | event->tstamp_running = tstamp; |
| 2086 | event->tstamp_stopped = tstamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2087 | } |
| 2088 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2089 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2090 | struct perf_cpu_context *cpuctx, |
| 2091 | enum event_type_t event_type); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2092 | static void |
| 2093 | ctx_sched_in(struct perf_event_context *ctx, |
| 2094 | struct perf_cpu_context *cpuctx, |
| 2095 | enum event_type_t event_type, |
| 2096 | struct task_struct *task); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2097 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2098 | static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2099 | struct perf_event_context *ctx) |
| 2100 | { |
| 2101 | if (!cpuctx->task_ctx) |
| 2102 | return; |
| 2103 | |
| 2104 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 2105 | return; |
| 2106 | |
| 2107 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
| 2108 | } |
| 2109 | |
Peter Zijlstra | dce5855 | 2011-04-09 21:17:46 +0200 | [diff] [blame] | 2110 | static void perf_event_sched_in(struct perf_cpu_context *cpuctx, |
| 2111 | struct perf_event_context *ctx, |
| 2112 | struct task_struct *task) |
| 2113 | { |
| 2114 | cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); |
| 2115 | if (ctx) |
| 2116 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); |
| 2117 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); |
| 2118 | if (ctx) |
| 2119 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); |
| 2120 | } |
| 2121 | |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 2122 | static void ctx_resched(struct perf_cpu_context *cpuctx, |
| 2123 | struct perf_event_context *task_ctx) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 2124 | { |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 2125 | perf_pmu_disable(cpuctx->ctx.pmu); |
| 2126 | if (task_ctx) |
| 2127 | task_ctx_sched_out(cpuctx, task_ctx); |
| 2128 | cpu_ctx_sched_out(cpuctx, EVENT_ALL); |
| 2129 | perf_event_sched_in(cpuctx, task_ctx, current); |
| 2130 | perf_pmu_enable(cpuctx->ctx.pmu); |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2133 | /* |
| 2134 | * Cross CPU call to install and enable a performance event |
| 2135 | * |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2136 | * Very similar to remote_function() + event_function() but cannot assume that |
| 2137 | * things like ctx->is_active and cpuctx->task_ctx are set. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2138 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2139 | static int __perf_install_in_context(void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2140 | { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2141 | struct perf_event *event = info; |
| 2142 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2143 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2144 | struct perf_event_context *task_ctx = cpuctx->task_ctx; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2145 | bool activate = true; |
| 2146 | int ret = 0; |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2147 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2148 | raw_spin_lock(&cpuctx->ctx.lock); |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2149 | if (ctx->task) { |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2150 | raw_spin_lock(&ctx->lock); |
| 2151 | task_ctx = ctx; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2152 | |
| 2153 | /* If we're on the wrong CPU, try again */ |
| 2154 | if (task_cpu(ctx->task) != smp_processor_id()) { |
| 2155 | ret = -ESRCH; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2156 | goto unlock; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2157 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2158 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2159 | /* |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2160 | * If we're on the right CPU, see if the task we target is |
| 2161 | * current, if not we don't have to activate the ctx, a future |
| 2162 | * context switch will do that for us. |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2163 | */ |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2164 | if (ctx->task != current) |
| 2165 | activate = false; |
| 2166 | else |
| 2167 | WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx); |
| 2168 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2169 | } else if (task_ctx) { |
| 2170 | raw_spin_lock(&task_ctx->lock); |
Peter Zijlstra | b58f6b0 | 2011-06-07 00:23:28 +0200 | [diff] [blame] | 2171 | } |
| 2172 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2173 | if (activate) { |
| 2174 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
| 2175 | add_event_to_ctx(event, ctx); |
| 2176 | ctx_resched(cpuctx, task_ctx); |
| 2177 | } else { |
| 2178 | add_event_to_ctx(event, ctx); |
| 2179 | } |
| 2180 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2181 | unlock: |
Peter Zijlstra | 2c29ef0 | 2011-04-09 21:17:44 +0200 | [diff] [blame] | 2182 | perf_ctx_unlock(cpuctx, task_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2183 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2184 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | /* |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2188 | * Attach a performance event to a context. |
| 2189 | * |
| 2190 | * Very similar to event_function_call, see comment there. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2191 | */ |
| 2192 | static void |
| 2193 | perf_install_in_context(struct perf_event_context *ctx, |
| 2194 | struct perf_event *event, |
| 2195 | int cpu) |
| 2196 | { |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2197 | struct task_struct *task = READ_ONCE(ctx->task); |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2198 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2199 | lockdep_assert_held(&ctx->mutex); |
| 2200 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2201 | event->ctx = ctx; |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 2202 | if (event->cpu != -1) |
| 2203 | event->cpu = cpu; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 2204 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2205 | if (!task) { |
| 2206 | cpu_function_call(cpu, __perf_install_in_context, event); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2207 | return; |
| 2208 | } |
Peter Zijlstra | 6f932e5 | 2016-02-24 18:45:43 +0100 | [diff] [blame] | 2209 | |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2210 | /* |
| 2211 | * Should not happen, we validate the ctx is still alive before calling. |
| 2212 | */ |
| 2213 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) |
| 2214 | return; |
| 2215 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2216 | /* |
| 2217 | * Installing events is tricky because we cannot rely on ctx->is_active |
| 2218 | * 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] | 2219 | */ |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2220 | again: |
| 2221 | /* |
| 2222 | * Cannot use task_function_call() because we need to run on the task's |
| 2223 | * CPU regardless of whether its current or not. |
| 2224 | */ |
| 2225 | if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event)) |
| 2226 | return; |
| 2227 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2228 | raw_spin_lock_irq(&ctx->lock); |
| 2229 | task = ctx->task; |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2230 | if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { |
| 2231 | /* |
| 2232 | * Cannot happen because we already checked above (which also |
| 2233 | * cannot happen), and we hold ctx->mutex, which serializes us |
| 2234 | * against perf_event_exit_task_context(). |
| 2235 | */ |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2236 | raw_spin_unlock_irq(&ctx->lock); |
| 2237 | return; |
| 2238 | } |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2239 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | a096309 | 2016-02-24 18:45:50 +0100 | [diff] [blame] | 2240 | /* |
| 2241 | * Since !ctx->is_active doesn't mean anything, we must IPI |
| 2242 | * unconditionally. |
| 2243 | */ |
| 2244 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | /* |
| 2248 | * Put a event into inactive state and update time fields. |
| 2249 | * Enabling the leader of a group effectively enables all |
| 2250 | * the group members that aren't explicitly disabled, so we |
| 2251 | * have to update their ->tstamp_enabled also. |
| 2252 | * Note: this works for group members as well as group leaders |
| 2253 | * since the non-leader members' sibling_lists will be empty. |
| 2254 | */ |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2255 | static void __perf_event_mark_enabled(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2256 | { |
| 2257 | struct perf_event *sub; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2258 | u64 tstamp = perf_event_time(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2259 | |
| 2260 | event->state = PERF_EVENT_STATE_INACTIVE; |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2261 | event->tstamp_enabled = tstamp - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2262 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
Stephane Eranian | 4158755 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2263 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) |
| 2264 | sub->tstamp_enabled = tstamp - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2265 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | /* |
| 2269 | * Cross CPU call to enable a performance event |
| 2270 | */ |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2271 | static void __perf_event_enable(struct perf_event *event, |
| 2272 | struct perf_cpu_context *cpuctx, |
| 2273 | struct perf_event_context *ctx, |
| 2274 | void *info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2275 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2276 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2277 | struct perf_event_context *task_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2278 | |
Peter Zijlstra | 6e801e01 | 2016-01-26 12:17:08 +0100 | [diff] [blame] | 2279 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
| 2280 | event->state <= PERF_EVENT_STATE_ERROR) |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2281 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2282 | |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2283 | if (ctx->is_active) |
| 2284 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
| 2285 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 2286 | __perf_event_mark_enabled(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2287 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2288 | if (!ctx->is_active) |
| 2289 | return; |
| 2290 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2291 | if (!event_filter_match(event)) { |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2292 | if (is_cgroup_event(event)) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2293 | perf_cgroup_defer_enabled(event); |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2294 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2295 | return; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2296 | } |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 2297 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2298 | /* |
| 2299 | * If the event is in a group and isn't the group leader, |
| 2300 | * then don't put it on unless the group is on. |
| 2301 | */ |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2302 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { |
| 2303 | ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2304 | return; |
Peter Zijlstra | bd2afa4 | 2016-02-24 18:45:49 +0100 | [diff] [blame] | 2305 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2306 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2307 | task_ctx = cpuctx->task_ctx; |
| 2308 | if (ctx->task) |
| 2309 | WARN_ON_ONCE(task_ctx != ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2310 | |
Peter Zijlstra | aee7dbc | 2016-01-08 10:45:11 +0100 | [diff] [blame] | 2311 | ctx_resched(cpuctx, task_ctx); |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2314 | /* |
| 2315 | * Enable a event. |
| 2316 | * |
| 2317 | * If event->ctx is a cloned context, callers must make sure that |
| 2318 | * every task struct that event->ctx->task could possibly point to |
| 2319 | * remains valid. This condition is satisfied when called through |
| 2320 | * perf_event_for_each_child or perf_event_for_each as described |
| 2321 | * for perf_event_disable. |
| 2322 | */ |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2323 | static void _perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2324 | { |
| 2325 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2326 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2327 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 6e801e01 | 2016-01-26 12:17:08 +0100 | [diff] [blame] | 2328 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
| 2329 | event->state < PERF_EVENT_STATE_ERROR) { |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2330 | raw_spin_unlock_irq(&ctx->lock); |
| 2331 | return; |
| 2332 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2333 | |
| 2334 | /* |
| 2335 | * If the event is in error state, clear that first. |
Peter Zijlstra | 7b64801 | 2015-12-03 18:35:21 +0100 | [diff] [blame] | 2336 | * |
| 2337 | * That way, if we see the event in error state below, we know that it |
| 2338 | * has gone back into error state, as distinct from the task having |
| 2339 | * been scheduled away before the cross-call arrived. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2340 | */ |
| 2341 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2342 | event->state = PERF_EVENT_STATE_OFF; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2343 | raw_spin_unlock_irq(&ctx->lock); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2344 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 2345 | event_function_call(event, __perf_event_enable, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2346 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2347 | |
| 2348 | /* |
| 2349 | * See perf_event_disable(); |
| 2350 | */ |
| 2351 | void perf_event_enable(struct perf_event *event) |
| 2352 | { |
| 2353 | struct perf_event_context *ctx; |
| 2354 | |
| 2355 | ctx = perf_event_ctx_lock(event); |
| 2356 | _perf_event_enable(event); |
| 2357 | perf_event_ctx_unlock(event, ctx); |
| 2358 | } |
Robert Richter | dcfce4a | 2011-10-11 17:11:08 +0200 | [diff] [blame] | 2359 | EXPORT_SYMBOL_GPL(perf_event_enable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2360 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2361 | static int _perf_event_refresh(struct perf_event *event, int refresh) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2362 | { |
| 2363 | /* |
| 2364 | * not supported on inherited events |
| 2365 | */ |
Franck Bui-Huu | 2e939d1 | 2010-11-23 16:21:44 +0100 | [diff] [blame] | 2366 | if (event->attr.inherit || !is_sampling_event(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2367 | return -EINVAL; |
| 2368 | |
| 2369 | atomic_add(refresh, &event->event_limit); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2370 | _perf_event_enable(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2371 | |
| 2372 | return 0; |
| 2373 | } |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 2374 | |
| 2375 | /* |
| 2376 | * See perf_event_disable() |
| 2377 | */ |
| 2378 | int perf_event_refresh(struct perf_event *event, int refresh) |
| 2379 | { |
| 2380 | struct perf_event_context *ctx; |
| 2381 | int ret; |
| 2382 | |
| 2383 | ctx = perf_event_ctx_lock(event); |
| 2384 | ret = _perf_event_refresh(event, refresh); |
| 2385 | perf_event_ctx_unlock(event, ctx); |
| 2386 | |
| 2387 | return ret; |
| 2388 | } |
Avi Kivity | 26ca5c1 | 2011-06-29 18:42:37 +0300 | [diff] [blame] | 2389 | EXPORT_SYMBOL_GPL(perf_event_refresh); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2390 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2391 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 2392 | struct perf_cpu_context *cpuctx, |
| 2393 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2394 | { |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2395 | int is_active = ctx->is_active; |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2396 | struct perf_event *event; |
| 2397 | |
| 2398 | lockdep_assert_held(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2399 | |
Peter Zijlstra | 39a4364 | 2016-01-11 12:46:35 +0100 | [diff] [blame] | 2400 | if (likely(!ctx->nr_events)) { |
| 2401 | /* |
| 2402 | * See __perf_remove_from_context(). |
| 2403 | */ |
| 2404 | WARN_ON_ONCE(ctx->is_active); |
| 2405 | if (ctx->task) |
| 2406 | WARN_ON_ONCE(cpuctx->task_ctx); |
| 2407 | return; |
| 2408 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2409 | |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2410 | ctx->is_active &= ~event_type; |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2411 | if (!(ctx->is_active & EVENT_ALL)) |
| 2412 | ctx->is_active = 0; |
| 2413 | |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2414 | if (ctx->task) { |
| 2415 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 2416 | if (!ctx->is_active) |
| 2417 | cpuctx->task_ctx = NULL; |
| 2418 | } |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2419 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2420 | is_active ^= ctx->is_active; /* changed bits */ |
| 2421 | |
| 2422 | if (is_active & EVENT_TIME) { |
| 2423 | /* update (and stop) ctx time */ |
| 2424 | update_context_time(ctx); |
| 2425 | update_cgrp_time_from_cpuctx(cpuctx); |
| 2426 | } |
| 2427 | |
| 2428 | if (!ctx->nr_active || !(is_active & EVENT_ALL)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2429 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2430 | |
Peter Zijlstra | 075e0b0 | 2011-04-09 21:17:40 +0200 | [diff] [blame] | 2431 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2432 | if (is_active & EVENT_PINNED) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2433 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 2434 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2435 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2436 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2437 | if (is_active & EVENT_FLEXIBLE) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2438 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2439 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2440 | } |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2441 | perf_pmu_enable(ctx->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | /* |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2445 | * Test whether two contexts are equivalent, i.e. whether they have both been |
| 2446 | * cloned from the same version of the same context. |
| 2447 | * |
| 2448 | * Equivalence is measured using a generation number in the context that is |
| 2449 | * incremented on each modification to it; see unclone_ctx(), list_add_event() |
| 2450 | * and list_del_event(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2451 | */ |
| 2452 | static int context_equiv(struct perf_event_context *ctx1, |
| 2453 | struct perf_event_context *ctx2) |
| 2454 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 2455 | lockdep_assert_held(&ctx1->lock); |
| 2456 | lockdep_assert_held(&ctx2->lock); |
| 2457 | |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2458 | /* Pinning disables the swap optimization */ |
| 2459 | if (ctx1->pin_count || ctx2->pin_count) |
| 2460 | return 0; |
| 2461 | |
| 2462 | /* If ctx1 is the parent of ctx2 */ |
| 2463 | if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) |
| 2464 | return 1; |
| 2465 | |
| 2466 | /* If ctx2 is the parent of ctx1 */ |
| 2467 | if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) |
| 2468 | return 1; |
| 2469 | |
| 2470 | /* |
| 2471 | * If ctx1 and ctx2 have the same parent; we flatten the parent |
| 2472 | * hierarchy, see perf_event_init_context(). |
| 2473 | */ |
| 2474 | if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && |
| 2475 | ctx1->parent_gen == ctx2->parent_gen) |
| 2476 | return 1; |
| 2477 | |
| 2478 | /* Unmatched */ |
| 2479 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2480 | } |
| 2481 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2482 | static void __perf_event_sync_stat(struct perf_event *event, |
| 2483 | struct perf_event *next_event) |
| 2484 | { |
| 2485 | u64 value; |
| 2486 | |
| 2487 | if (!event->attr.inherit_stat) |
| 2488 | return; |
| 2489 | |
| 2490 | /* |
| 2491 | * Update the event value, we cannot use perf_event_read() |
| 2492 | * because we're in the middle of a context switch and have IRQs |
| 2493 | * disabled, which upsets smp_call_function_single(), however |
| 2494 | * we know the event must be on the current CPU, therefore we |
| 2495 | * don't need to use it. |
| 2496 | */ |
| 2497 | switch (event->state) { |
| 2498 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 2499 | event->pmu->read(event); |
| 2500 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2501 | |
| 2502 | case PERF_EVENT_STATE_INACTIVE: |
| 2503 | update_event_times(event); |
| 2504 | break; |
| 2505 | |
| 2506 | default: |
| 2507 | break; |
| 2508 | } |
| 2509 | |
| 2510 | /* |
| 2511 | * In order to keep per-task stats reliable we need to flip the event |
| 2512 | * values when we flip the contexts. |
| 2513 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2514 | value = local64_read(&next_event->count); |
| 2515 | value = local64_xchg(&event->count, value); |
| 2516 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2517 | |
| 2518 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 2519 | swap(event->total_time_running, next_event->total_time_running); |
| 2520 | |
| 2521 | /* |
| 2522 | * Since we swizzled the values, update the user visible data too. |
| 2523 | */ |
| 2524 | perf_event_update_userpage(event); |
| 2525 | perf_event_update_userpage(next_event); |
| 2526 | } |
| 2527 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2528 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 2529 | struct perf_event_context *next_ctx) |
| 2530 | { |
| 2531 | struct perf_event *event, *next_event; |
| 2532 | |
| 2533 | if (!ctx->nr_stat) |
| 2534 | return; |
| 2535 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 2536 | update_context_time(ctx); |
| 2537 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2538 | event = list_first_entry(&ctx->event_list, |
| 2539 | struct perf_event, event_entry); |
| 2540 | |
| 2541 | next_event = list_first_entry(&next_ctx->event_list, |
| 2542 | struct perf_event, event_entry); |
| 2543 | |
| 2544 | while (&event->event_entry != &ctx->event_list && |
| 2545 | &next_event->event_entry != &next_ctx->event_list) { |
| 2546 | |
| 2547 | __perf_event_sync_stat(event, next_event); |
| 2548 | |
| 2549 | event = list_next_entry(event, event_entry); |
| 2550 | next_event = list_next_entry(next_event, event_entry); |
| 2551 | } |
| 2552 | } |
| 2553 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2554 | static void perf_event_context_sched_out(struct task_struct *task, int ctxn, |
| 2555 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2556 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2557 | struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2558 | struct perf_event_context *next_ctx; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2559 | struct perf_event_context *parent, *next_parent; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2560 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2561 | int do_switch = 1; |
| 2562 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2563 | if (likely(!ctx)) |
| 2564 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2565 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2566 | cpuctx = __get_cpu_context(ctx); |
| 2567 | if (!cpuctx->task_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2568 | return; |
| 2569 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2570 | rcu_read_lock(); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2571 | next_ctx = next->perf_event_ctxp[ctxn]; |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2572 | if (!next_ctx) |
| 2573 | goto unlock; |
| 2574 | |
| 2575 | parent = rcu_dereference(ctx->parent_ctx); |
| 2576 | next_parent = rcu_dereference(next_ctx->parent_ctx); |
| 2577 | |
| 2578 | /* If neither context have a parent context; they cannot be clones. */ |
Jiri Olsa | 802c8a6 | 2014-09-12 13:18:28 +0200 | [diff] [blame] | 2579 | if (!parent && !next_parent) |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2580 | goto unlock; |
| 2581 | |
| 2582 | if (next_parent == ctx || next_ctx == parent || next_parent == parent) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2583 | /* |
| 2584 | * Looks like the two contexts are clones, so we might be |
| 2585 | * able to optimize the context switch. We lock both |
| 2586 | * contexts and check that they are clones under the |
| 2587 | * lock (including re-checking that neither has been |
| 2588 | * uncloned in the meantime). It doesn't matter which |
| 2589 | * order we take the locks because no other cpu could |
| 2590 | * be trying to lock both of these tasks. |
| 2591 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2592 | raw_spin_lock(&ctx->lock); |
| 2593 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2594 | if (context_equiv(ctx, next_ctx)) { |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2595 | WRITE_ONCE(ctx->task, next); |
| 2596 | WRITE_ONCE(next_ctx->task, task); |
Yan, Zheng | 5a158c3 | 2014-11-04 21:56:02 -0500 | [diff] [blame] | 2597 | |
| 2598 | swap(ctx->task_ctx_data, next_ctx->task_ctx_data); |
| 2599 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 2600 | /* |
| 2601 | * RCU_INIT_POINTER here is safe because we've not |
| 2602 | * modified the ctx and the above modification of |
| 2603 | * ctx->task and ctx->task_ctx_data are immaterial |
| 2604 | * since those values are always verified under |
| 2605 | * ctx->lock which we're now holding. |
| 2606 | */ |
| 2607 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx); |
| 2608 | RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx); |
| 2609 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2610 | do_switch = 0; |
| 2611 | |
| 2612 | perf_event_sync_stat(ctx, next_ctx); |
| 2613 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2614 | raw_spin_unlock(&next_ctx->lock); |
| 2615 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2616 | } |
Peter Zijlstra | 5a3126d | 2013-10-07 17:12:48 +0200 | [diff] [blame] | 2617 | unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2618 | rcu_read_unlock(); |
| 2619 | |
| 2620 | if (do_switch) { |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2621 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 8833d0e | 2016-01-08 10:02:37 +0100 | [diff] [blame] | 2622 | task_ctx_sched_out(cpuctx, ctx); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2623 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2624 | } |
| 2625 | } |
| 2626 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2627 | void perf_sched_cb_dec(struct pmu *pmu) |
| 2628 | { |
| 2629 | this_cpu_dec(perf_sched_cb_usages); |
| 2630 | } |
| 2631 | |
| 2632 | void perf_sched_cb_inc(struct pmu *pmu) |
| 2633 | { |
| 2634 | this_cpu_inc(perf_sched_cb_usages); |
| 2635 | } |
| 2636 | |
| 2637 | /* |
| 2638 | * This function provides the context switch callback to the lower code |
| 2639 | * layer. It is invoked ONLY when the context switch callback is enabled. |
| 2640 | */ |
| 2641 | static void perf_pmu_sched_task(struct task_struct *prev, |
| 2642 | struct task_struct *next, |
| 2643 | bool sched_in) |
| 2644 | { |
| 2645 | struct perf_cpu_context *cpuctx; |
| 2646 | struct pmu *pmu; |
| 2647 | unsigned long flags; |
| 2648 | |
| 2649 | if (prev == next) |
| 2650 | return; |
| 2651 | |
| 2652 | local_irq_save(flags); |
| 2653 | |
| 2654 | rcu_read_lock(); |
| 2655 | |
| 2656 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 2657 | if (pmu->sched_task) { |
| 2658 | cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); |
| 2659 | |
| 2660 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
| 2661 | |
| 2662 | perf_pmu_disable(pmu); |
| 2663 | |
| 2664 | pmu->sched_task(cpuctx->task_ctx, sched_in); |
| 2665 | |
| 2666 | perf_pmu_enable(pmu); |
| 2667 | |
| 2668 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
| 2669 | } |
| 2670 | } |
| 2671 | |
| 2672 | rcu_read_unlock(); |
| 2673 | |
| 2674 | local_irq_restore(flags); |
| 2675 | } |
| 2676 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 2677 | static void perf_event_switch(struct task_struct *task, |
| 2678 | struct task_struct *next_prev, bool sched_in); |
| 2679 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2680 | #define for_each_task_context_nr(ctxn) \ |
| 2681 | for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) |
| 2682 | |
| 2683 | /* |
| 2684 | * Called from scheduler to remove the events of the current task, |
| 2685 | * with interrupts disabled. |
| 2686 | * |
| 2687 | * We stop each event and update the event value in event->count. |
| 2688 | * |
| 2689 | * This does not protect us against NMI, but disable() |
| 2690 | * sets the disabled bit in the control field of event _before_ |
| 2691 | * accessing the event control register. If a NMI hits, then it will |
| 2692 | * not restart the event. |
| 2693 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2694 | void __perf_event_task_sched_out(struct task_struct *task, |
| 2695 | struct task_struct *next) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2696 | { |
| 2697 | int ctxn; |
| 2698 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2699 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 2700 | perf_pmu_sched_task(task, next, false); |
| 2701 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 2702 | if (atomic_read(&nr_switch_events)) |
| 2703 | perf_event_switch(task, next, false); |
| 2704 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2705 | for_each_task_context_nr(ctxn) |
| 2706 | perf_event_context_sched_out(task, ctxn, next); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2707 | |
| 2708 | /* |
| 2709 | * if cgroup events exist on this CPU, then we need |
| 2710 | * to check if we have to switch out PMU state. |
| 2711 | * cgroup event are system-wide mode only |
| 2712 | */ |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 2713 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 2714 | perf_cgroup_sched_out(task, next); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2715 | } |
| 2716 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2717 | /* |
| 2718 | * Called with IRQs disabled |
| 2719 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2720 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 2721 | enum event_type_t event_type) |
| 2722 | { |
| 2723 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2727 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2728 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2729 | { |
| 2730 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2731 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2732 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 2733 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2734 | continue; |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2735 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2736 | continue; |
| 2737 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2738 | /* may need to reset tstamp_enabled */ |
| 2739 | if (is_cgroup_event(event)) |
| 2740 | perf_cgroup_mark_enabled(event, ctx); |
| 2741 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 2742 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2743 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2744 | |
| 2745 | /* |
| 2746 | * If this pinned group hasn't been scheduled, |
| 2747 | * put it in error state. |
| 2748 | */ |
| 2749 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 2750 | update_group_times(event); |
| 2751 | event->state = PERF_EVENT_STATE_ERROR; |
| 2752 | } |
| 2753 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | static void |
| 2757 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2758 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2759 | { |
| 2760 | struct perf_event *event; |
| 2761 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2762 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 2763 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 2764 | /* Ignore events in OFF or ERROR state */ |
| 2765 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2766 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2767 | /* |
| 2768 | * Listen to the 'cpu' scheduling filter constraint |
| 2769 | * of events: |
| 2770 | */ |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 2771 | if (!event_filter_match(event)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2772 | continue; |
| 2773 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2774 | /* may need to reset tstamp_enabled */ |
| 2775 | if (is_cgroup_event(event)) |
| 2776 | perf_cgroup_mark_enabled(event, ctx); |
| 2777 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2778 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2779 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2780 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2781 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2782 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | static void |
| 2786 | ctx_sched_in(struct perf_event_context *ctx, |
| 2787 | struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2788 | enum event_type_t event_type, |
| 2789 | struct task_struct *task) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2790 | { |
Peter Zijlstra | db24d33 | 2011-04-09 21:17:45 +0200 | [diff] [blame] | 2791 | int is_active = ctx->is_active; |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2792 | u64 now; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2793 | |
Peter Zijlstra | c994d61 | 2016-01-08 09:20:23 +0100 | [diff] [blame] | 2794 | lockdep_assert_held(&ctx->lock); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2795 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2796 | if (likely(!ctx->nr_events)) |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2797 | return; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2798 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2799 | ctx->is_active |= (event_type | EVENT_TIME); |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2800 | if (ctx->task) { |
| 2801 | if (!is_active) |
| 2802 | cpuctx->task_ctx = ctx; |
| 2803 | else |
| 2804 | WARN_ON_ONCE(cpuctx->task_ctx != ctx); |
| 2805 | } |
| 2806 | |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2807 | is_active ^= ctx->is_active; /* changed bits */ |
| 2808 | |
| 2809 | if (is_active & EVENT_TIME) { |
| 2810 | /* start ctx time */ |
| 2811 | now = perf_clock(); |
| 2812 | ctx->timestamp = now; |
| 2813 | perf_cgroup_set_timestamp(task, ctx); |
| 2814 | } |
| 2815 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2816 | /* |
| 2817 | * First go through the list and put on any pinned groups |
| 2818 | * in order to give them the best chance of going on. |
| 2819 | */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2820 | if (is_active & EVENT_PINNED) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2821 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 2822 | |
| 2823 | /* Then walk through the lower prio flexible groups */ |
Peter Zijlstra | 3cbaa59 | 2016-02-24 18:45:47 +0100 | [diff] [blame] | 2824 | if (is_active & EVENT_FLEXIBLE) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 2825 | ctx_flexible_sched_in(ctx, cpuctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2826 | } |
| 2827 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2828 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2829 | enum event_type_t event_type, |
| 2830 | struct task_struct *task) |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2831 | { |
| 2832 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 2833 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2834 | ctx_sched_in(ctx, cpuctx, event_type, task); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2835 | } |
| 2836 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2837 | static void perf_event_context_sched_in(struct perf_event_context *ctx, |
| 2838 | struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2839 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2840 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2841 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 2842 | cpuctx = __get_cpu_context(ctx); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2843 | if (cpuctx->task_ctx == ctx) |
| 2844 | return; |
| 2845 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2846 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 2847 | perf_pmu_disable(ctx->pmu); |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 2848 | /* |
| 2849 | * We want to keep the following priority order: |
| 2850 | * cpu pinned (that don't need to move), task pinned, |
| 2851 | * cpu flexible, task flexible. |
| 2852 | */ |
| 2853 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | 63e30d3 | 2016-01-08 11:39:10 +0100 | [diff] [blame] | 2854 | perf_event_sched_in(cpuctx, ctx, task); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 2855 | perf_pmu_enable(ctx->pmu); |
| 2856 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2857 | } |
| 2858 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2859 | /* |
| 2860 | * Called from scheduler to add the events of the current task |
| 2861 | * with interrupts disabled. |
| 2862 | * |
| 2863 | * We restore the event value and then enable it. |
| 2864 | * |
| 2865 | * This does not protect us against NMI, but enable() |
| 2866 | * sets the enabled bit in the control field of event _before_ |
| 2867 | * accessing the event control register. If a NMI hits, then it will |
| 2868 | * keep the event running. |
| 2869 | */ |
Jiri Olsa | ab0cce5 | 2012-05-23 13:13:02 +0200 | [diff] [blame] | 2870 | void __perf_event_task_sched_in(struct task_struct *prev, |
| 2871 | struct task_struct *task) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2872 | { |
| 2873 | struct perf_event_context *ctx; |
| 2874 | int ctxn; |
| 2875 | |
Peter Zijlstra | 7e41d17 | 2016-01-08 09:21:40 +0100 | [diff] [blame] | 2876 | /* |
| 2877 | * If cgroup events exist on this CPU, then we need to check if we have |
| 2878 | * to switch in PMU state; cgroup event are system-wide mode only. |
| 2879 | * |
| 2880 | * Since cgroup events are CPU events, we must schedule these in before |
| 2881 | * we schedule in the task events. |
| 2882 | */ |
| 2883 | if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) |
| 2884 | perf_cgroup_sched_in(prev, task); |
| 2885 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2886 | for_each_task_context_nr(ctxn) { |
| 2887 | ctx = task->perf_event_ctxp[ctxn]; |
| 2888 | if (likely(!ctx)) |
| 2889 | continue; |
| 2890 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2891 | perf_event_context_sched_in(ctx, task); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 2892 | } |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 2893 | |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 2894 | if (atomic_read(&nr_switch_events)) |
| 2895 | perf_event_switch(task, prev, true); |
| 2896 | |
Yan, Zheng | ba53250 | 2014-11-04 21:55:58 -0500 | [diff] [blame] | 2897 | if (__this_cpu_read(perf_sched_cb_usages)) |
| 2898 | perf_pmu_sched_task(prev, task, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2901 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 2902 | { |
| 2903 | u64 frequency = event->attr.sample_freq; |
| 2904 | u64 sec = NSEC_PER_SEC; |
| 2905 | u64 divisor, dividend; |
| 2906 | |
| 2907 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 2908 | |
| 2909 | count_fls = fls64(count); |
| 2910 | nsec_fls = fls64(nsec); |
| 2911 | frequency_fls = fls64(frequency); |
| 2912 | sec_fls = 30; |
| 2913 | |
| 2914 | /* |
| 2915 | * We got @count in @nsec, with a target of sample_freq HZ |
| 2916 | * the target period becomes: |
| 2917 | * |
| 2918 | * @count * 10^9 |
| 2919 | * period = ------------------- |
| 2920 | * @nsec * sample_freq |
| 2921 | * |
| 2922 | */ |
| 2923 | |
| 2924 | /* |
| 2925 | * Reduce accuracy by one bit such that @a and @b converge |
| 2926 | * to a similar magnitude. |
| 2927 | */ |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 2928 | #define REDUCE_FLS(a, b) \ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2929 | do { \ |
| 2930 | if (a##_fls > b##_fls) { \ |
| 2931 | a >>= 1; \ |
| 2932 | a##_fls--; \ |
| 2933 | } else { \ |
| 2934 | b >>= 1; \ |
| 2935 | b##_fls--; \ |
| 2936 | } \ |
| 2937 | } while (0) |
| 2938 | |
| 2939 | /* |
| 2940 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 2941 | * the other, so that finally we can do a u64/u64 division. |
| 2942 | */ |
| 2943 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 2944 | REDUCE_FLS(nsec, frequency); |
| 2945 | REDUCE_FLS(sec, count); |
| 2946 | } |
| 2947 | |
| 2948 | if (count_fls + sec_fls > 64) { |
| 2949 | divisor = nsec * frequency; |
| 2950 | |
| 2951 | while (count_fls + sec_fls > 64) { |
| 2952 | REDUCE_FLS(count, sec); |
| 2953 | divisor >>= 1; |
| 2954 | } |
| 2955 | |
| 2956 | dividend = count * sec; |
| 2957 | } else { |
| 2958 | dividend = count * sec; |
| 2959 | |
| 2960 | while (nsec_fls + frequency_fls > 64) { |
| 2961 | REDUCE_FLS(nsec, frequency); |
| 2962 | dividend >>= 1; |
| 2963 | } |
| 2964 | |
| 2965 | divisor = nsec * frequency; |
| 2966 | } |
| 2967 | |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2968 | if (!divisor) |
| 2969 | return dividend; |
| 2970 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2971 | return div64_u64(dividend, divisor); |
| 2972 | } |
| 2973 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 2974 | static DEFINE_PER_CPU(int, perf_throttled_count); |
| 2975 | static DEFINE_PER_CPU(u64, perf_throttled_seq); |
| 2976 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2977 | 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] | 2978 | { |
| 2979 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91ad | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 2980 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2981 | s64 delta; |
| 2982 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2983 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2984 | |
| 2985 | delta = (s64)(period - hwc->sample_period); |
| 2986 | delta = (delta + 7) / 8; /* low pass filter */ |
| 2987 | |
| 2988 | sample_period = hwc->sample_period + delta; |
| 2989 | |
| 2990 | if (!sample_period) |
| 2991 | sample_period = 1; |
| 2992 | |
| 2993 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 2994 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2995 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 2996 | if (disable) |
| 2997 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 2998 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2999 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3000 | |
| 3001 | if (disable) |
| 3002 | event->pmu->start(event, PERF_EF_RELOAD); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3003 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3004 | } |
| 3005 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3006 | /* |
| 3007 | * combine freq adjustment with unthrottling to avoid two passes over the |
| 3008 | * events. At the same time, make sure, having freq events does not change |
| 3009 | * the rate of unthrottling as that would introduce bias. |
| 3010 | */ |
| 3011 | static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, |
| 3012 | int needs_unthr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3013 | { |
| 3014 | struct perf_event *event; |
| 3015 | struct hw_perf_event *hwc; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3016 | u64 now, period = TICK_NSEC; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3017 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3018 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3019 | /* |
| 3020 | * only need to iterate over all events iff: |
| 3021 | * - context have events in frequency mode (needs freq adjust) |
| 3022 | * - there are events to unthrottle on this cpu |
| 3023 | */ |
| 3024 | if (!(ctx->nr_freq || needs_unthr)) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3025 | return; |
| 3026 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3027 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3028 | perf_pmu_disable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3029 | |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 3030 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3031 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 3032 | continue; |
| 3033 | |
Stephane Eranian | 5632ab1 | 2011-01-03 18:20:01 +0200 | [diff] [blame] | 3034 | if (!event_filter_match(event)) |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 3035 | continue; |
| 3036 | |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3037 | perf_pmu_disable(event->pmu); |
| 3038 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3039 | hwc = &event->hw; |
| 3040 | |
Jiri Olsa | ae23bff | 2013-08-24 16:45:54 +0200 | [diff] [blame] | 3041 | if (hwc->interrupts == MAX_INTERRUPTS) { |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3042 | hwc->interrupts = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3043 | perf_log_throttle(event, 1); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 3044 | event->pmu->start(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | if (!event->attr.freq || !event->attr.sample_freq) |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3048 | goto next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3049 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3050 | /* |
| 3051 | * stop the event and update event->count |
| 3052 | */ |
| 3053 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 3054 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 3055 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3056 | delta = now - hwc->freq_count_stamp; |
| 3057 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3058 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3059 | /* |
| 3060 | * restart the event |
| 3061 | * reload only if value has changed |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3062 | * we have stopped the event so tell that |
| 3063 | * to perf_adjust_period() to avoid stopping it |
| 3064 | * twice. |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3065 | */ |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 3066 | if (delta > 0) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3067 | perf_adjust_period(event, period, delta, false); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3068 | |
| 3069 | event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); |
Alexander Shishkin | 4437727 | 2013-12-16 14:17:36 +0200 | [diff] [blame] | 3070 | next: |
| 3071 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3072 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3073 | |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 3074 | perf_pmu_enable(ctx->pmu); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3075 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | /* |
| 3079 | * Round-robin a context's events: |
| 3080 | */ |
| 3081 | static void rotate_ctx(struct perf_event_context *ctx) |
| 3082 | { |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 3083 | /* |
| 3084 | * Rotate the first entry last of non-pinned groups. Rotation might be |
| 3085 | * disabled by the inheritance code. |
| 3086 | */ |
| 3087 | if (!ctx->rotate_disable) |
| 3088 | list_rotate_left(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3089 | } |
| 3090 | |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3091 | static int perf_rotate_context(struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3092 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3093 | struct perf_event_context *ctx = NULL; |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3094 | int rotate = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3095 | |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3096 | if (cpuctx->ctx.nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3097 | if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 3098 | rotate = 1; |
| 3099 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3100 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3101 | ctx = cpuctx->task_ctx; |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3102 | if (ctx && ctx->nr_events) { |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3103 | if (ctx->nr_events != ctx->nr_active) |
| 3104 | rotate = 1; |
| 3105 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3106 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3107 | if (!rotate) |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3108 | goto done; |
| 3109 | |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 3110 | perf_ctx_lock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | 1b9a644 | 2010-09-07 18:32:22 +0200 | [diff] [blame] | 3111 | perf_pmu_disable(cpuctx->ctx.pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3112 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3113 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 3114 | if (ctx) |
| 3115 | ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 3116 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3117 | rotate_ctx(&cpuctx->ctx); |
| 3118 | if (ctx) |
| 3119 | rotate_ctx(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3120 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3121 | perf_event_sched_in(cpuctx, ctx, current); |
Peter Zijlstra | 0f5a260 | 2011-11-16 14:38:16 +0100 | [diff] [blame] | 3122 | |
| 3123 | perf_pmu_enable(cpuctx->ctx.pmu); |
| 3124 | perf_ctx_unlock(cpuctx, cpuctx->task_ctx); |
Peter Zijlstra | b5ab4cd | 2010-09-06 16:32:21 +0200 | [diff] [blame] | 3125 | done: |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 3126 | |
| 3127 | return rotate; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | void perf_event_task_tick(void) |
| 3131 | { |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3132 | struct list_head *head = this_cpu_ptr(&active_ctx_list); |
| 3133 | struct perf_event_context *ctx, *tmp; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3134 | int throttled; |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 3135 | |
| 3136 | WARN_ON(!irqs_disabled()); |
| 3137 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3138 | __this_cpu_inc(perf_throttled_seq); |
| 3139 | throttled = __this_cpu_xchg(perf_throttled_count, 0); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3140 | tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3141 | |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3142 | list_for_each_entry_safe(ctx, tmp, head, active_ctx_list) |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 3143 | perf_adjust_freq_unthr_context(ctx, throttled); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3144 | } |
| 3145 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3146 | static int event_enable_on_exec(struct perf_event *event, |
| 3147 | struct perf_event_context *ctx) |
| 3148 | { |
| 3149 | if (!event->attr.enable_on_exec) |
| 3150 | return 0; |
| 3151 | |
| 3152 | event->attr.enable_on_exec = 0; |
| 3153 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 3154 | return 0; |
| 3155 | |
Peter Zijlstra | 1d9b482 | 2011-11-23 12:34:20 +0100 | [diff] [blame] | 3156 | __perf_event_mark_enabled(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3157 | |
| 3158 | return 1; |
| 3159 | } |
| 3160 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3161 | /* |
| 3162 | * Enable all of a task's events that have been marked enable-on-exec. |
| 3163 | * This expects task == current. |
| 3164 | */ |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3165 | static void perf_event_enable_on_exec(int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3166 | { |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3167 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3168 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3169 | struct perf_event *event; |
| 3170 | unsigned long flags; |
| 3171 | int enabled = 0; |
| 3172 | |
| 3173 | local_irq_save(flags); |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3174 | ctx = current->perf_event_ctxp[ctxn]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3175 | if (!ctx || !ctx->nr_events) |
| 3176 | goto out; |
| 3177 | |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3178 | cpuctx = __get_cpu_context(ctx); |
| 3179 | perf_ctx_lock(cpuctx, ctx); |
Peter Zijlstra | 7fce250 | 2016-02-24 18:45:48 +0100 | [diff] [blame] | 3180 | ctx_sched_out(ctx, cpuctx, EVENT_TIME); |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3181 | list_for_each_entry(event, &ctx->event_list, event_entry) |
| 3182 | enabled |= event_enable_on_exec(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3183 | |
| 3184 | /* |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3185 | * Unclone and reschedule this context if we enabled any event. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3186 | */ |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3187 | if (enabled) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3188 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | 3e34950 | 2016-01-08 10:01:18 +0100 | [diff] [blame] | 3189 | ctx_resched(cpuctx, ctx); |
| 3190 | } |
| 3191 | perf_ctx_unlock(cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3192 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3193 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3194 | local_irq_restore(flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3195 | |
| 3196 | if (clone_ctx) |
| 3197 | put_ctx(clone_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3198 | } |
| 3199 | |
Peter Zijlstra | e041e32 | 2014-05-21 17:32:19 +0200 | [diff] [blame] | 3200 | void perf_event_exec(void) |
| 3201 | { |
Peter Zijlstra | e041e32 | 2014-05-21 17:32:19 +0200 | [diff] [blame] | 3202 | int ctxn; |
| 3203 | |
| 3204 | rcu_read_lock(); |
Peter Zijlstra | c127449 | 2015-12-10 20:57:40 +0100 | [diff] [blame] | 3205 | for_each_task_context_nr(ctxn) |
| 3206 | perf_event_enable_on_exec(ctxn); |
Peter Zijlstra | e041e32 | 2014-05-21 17:32:19 +0200 | [diff] [blame] | 3207 | rcu_read_unlock(); |
| 3208 | } |
| 3209 | |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3210 | struct perf_read_data { |
| 3211 | struct perf_event *event; |
| 3212 | bool group; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3213 | int ret; |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3214 | }; |
| 3215 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3216 | /* |
| 3217 | * Cross CPU call to read the hardware event |
| 3218 | */ |
| 3219 | static void __perf_event_read(void *info) |
| 3220 | { |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3221 | struct perf_read_data *data = info; |
| 3222 | struct perf_event *sub, *event = data->event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3223 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3224 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3225 | struct pmu *pmu = event->pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3226 | |
| 3227 | /* |
| 3228 | * If this is a task context, we need to check whether it is |
| 3229 | * the current task context of this cpu. If not it has been |
| 3230 | * scheduled out before the smp call arrived. In that case |
| 3231 | * event->count would have been updated to a recent sample |
| 3232 | * when the event was scheduled out. |
| 3233 | */ |
| 3234 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 3235 | return; |
| 3236 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3237 | raw_spin_lock(&ctx->lock); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3238 | if (ctx->is_active) { |
Peter Zijlstra | 542e72f | 2011-01-26 15:38:35 +0100 | [diff] [blame] | 3239 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3240 | update_cgrp_time_from_event(event); |
| 3241 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3242 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3243 | update_event_times(event); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3244 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3245 | goto unlock; |
| 3246 | |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3247 | if (!data->group) { |
| 3248 | pmu->read(event); |
| 3249 | data->ret = 0; |
| 3250 | goto unlock; |
| 3251 | } |
| 3252 | |
| 3253 | pmu->start_txn(pmu, PERF_PMU_TXN_READ); |
| 3254 | |
| 3255 | pmu->read(event); |
| 3256 | |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3257 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 3258 | update_event_times(sub); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3259 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
| 3260 | /* |
| 3261 | * Use sibling's PMU rather than @event's since |
| 3262 | * sibling could be on different (eg: software) PMU. |
| 3263 | */ |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3264 | sub->pmu->read(sub); |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3265 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3266 | } |
Sukadev Bhattiprolu | 4a00c16 | 2015-09-03 20:07:51 -0700 | [diff] [blame] | 3267 | |
| 3268 | data->ret = pmu->commit_txn(pmu); |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3269 | |
| 3270 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3271 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3272 | } |
| 3273 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3274 | static inline u64 perf_event_count(struct perf_event *event) |
| 3275 | { |
Matt Fleming | eacd3ec | 2015-01-23 18:45:41 +0000 | [diff] [blame] | 3276 | if (event->pmu->count) |
| 3277 | return event->pmu->count(event); |
| 3278 | |
| 3279 | return __perf_event_count(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3280 | } |
| 3281 | |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 3282 | /* |
| 3283 | * NMI-safe method to read a local event, that is an event that |
| 3284 | * is: |
| 3285 | * - either for the current task, or for this CPU |
| 3286 | * - does not have inherit set, for inherited task events |
| 3287 | * will not be local and we cannot read them atomically |
| 3288 | * - must not have a pmu::count method |
| 3289 | */ |
| 3290 | u64 perf_event_read_local(struct perf_event *event) |
| 3291 | { |
| 3292 | unsigned long flags; |
| 3293 | u64 val; |
| 3294 | |
| 3295 | /* |
| 3296 | * Disabling interrupts avoids all counter scheduling (context |
| 3297 | * switches, timer based rotation and IPIs). |
| 3298 | */ |
| 3299 | local_irq_save(flags); |
| 3300 | |
| 3301 | /* If this is a per-task event, it must be for current */ |
| 3302 | WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) && |
| 3303 | event->hw.target != current); |
| 3304 | |
| 3305 | /* If this is a per-CPU event, it must be for this CPU */ |
| 3306 | WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) && |
| 3307 | event->cpu != smp_processor_id()); |
| 3308 | |
| 3309 | /* |
| 3310 | * It must not be an event with inherit set, we cannot read |
| 3311 | * all child counters from atomic context. |
| 3312 | */ |
| 3313 | WARN_ON_ONCE(event->attr.inherit); |
| 3314 | |
| 3315 | /* |
| 3316 | * It must not have a pmu::count method, those are not |
| 3317 | * NMI safe. |
| 3318 | */ |
| 3319 | WARN_ON_ONCE(event->pmu->count); |
| 3320 | |
| 3321 | /* |
| 3322 | * If the event is currently on this CPU, its either a per-task event, |
| 3323 | * or local to this CPU. Furthermore it means its ACTIVE (otherwise |
| 3324 | * oncpu == -1). |
| 3325 | */ |
| 3326 | if (event->oncpu == smp_processor_id()) |
| 3327 | event->pmu->read(event); |
| 3328 | |
| 3329 | val = local64_read(&event->count); |
| 3330 | local_irq_restore(flags); |
| 3331 | |
| 3332 | return val; |
| 3333 | } |
| 3334 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3335 | static int perf_event_read(struct perf_event *event, bool group) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3336 | { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3337 | int ret = 0; |
| 3338 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3339 | /* |
| 3340 | * If event is enabled and currently active on a CPU, update the |
| 3341 | * value in the event structure: |
| 3342 | */ |
| 3343 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3344 | struct perf_read_data data = { |
| 3345 | .event = event, |
| 3346 | .group = group, |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3347 | .ret = 0, |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3348 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3349 | smp_call_function_single(event->oncpu, |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3350 | __perf_event_read, &data, 1); |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3351 | ret = data.ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3352 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 3353 | struct perf_event_context *ctx = event->ctx; |
| 3354 | unsigned long flags; |
| 3355 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3356 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3357 | /* |
| 3358 | * may read while context is not active |
| 3359 | * (e.g., thread is blocked), in that case |
| 3360 | * we cannot update context time |
| 3361 | */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3362 | if (ctx->is_active) { |
Stephane Eranian | c530ccd | 2010-10-15 15:26:01 +0200 | [diff] [blame] | 3363 | update_context_time(ctx); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3364 | update_cgrp_time_from_event(event); |
| 3365 | } |
Peter Zijlstra | 0492d4c | 2015-09-03 20:07:48 -0700 | [diff] [blame] | 3366 | if (group) |
| 3367 | update_group_times(event); |
| 3368 | else |
| 3369 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3370 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3371 | } |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3372 | |
| 3373 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | /* |
| 3377 | * Initialize the perf_event context in a task_struct: |
| 3378 | */ |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3379 | static void __perf_event_init_context(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3380 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3381 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3382 | mutex_init(&ctx->mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 3383 | INIT_LIST_HEAD(&ctx->active_ctx_list); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 3384 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 3385 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3386 | INIT_LIST_HEAD(&ctx->event_list); |
| 3387 | atomic_set(&ctx->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3388 | } |
| 3389 | |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3390 | static struct perf_event_context * |
| 3391 | alloc_perf_context(struct pmu *pmu, struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3392 | { |
| 3393 | struct perf_event_context *ctx; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3394 | |
| 3395 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
| 3396 | if (!ctx) |
| 3397 | return NULL; |
| 3398 | |
| 3399 | __perf_event_init_context(ctx); |
| 3400 | if (task) { |
| 3401 | ctx->task = task; |
| 3402 | get_task_struct(task); |
| 3403 | } |
| 3404 | ctx->pmu = pmu; |
| 3405 | |
| 3406 | return ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3409 | static struct task_struct * |
| 3410 | find_lively_task_by_vpid(pid_t vpid) |
| 3411 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3412 | struct task_struct *task; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3413 | int err; |
| 3414 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3415 | rcu_read_lock(); |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3416 | if (!vpid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3417 | task = current; |
| 3418 | else |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3419 | task = find_task_by_vpid(vpid); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3420 | if (task) |
| 3421 | get_task_struct(task); |
| 3422 | rcu_read_unlock(); |
| 3423 | |
| 3424 | if (!task) |
| 3425 | return ERR_PTR(-ESRCH); |
| 3426 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3427 | /* Reuse ptrace permission checks for now. */ |
| 3428 | err = -EACCES; |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 3429 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3430 | goto errout; |
| 3431 | |
Matt Helsley | 2ebd4ff | 2010-09-13 13:01:19 -0700 | [diff] [blame] | 3432 | return task; |
| 3433 | errout: |
| 3434 | put_task_struct(task); |
| 3435 | return ERR_PTR(err); |
| 3436 | |
| 3437 | } |
| 3438 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3439 | /* |
| 3440 | * Returns a matching context with refcount and pincount. |
| 3441 | */ |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3442 | static struct perf_event_context * |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3443 | find_get_context(struct pmu *pmu, struct task_struct *task, |
| 3444 | struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3445 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3446 | struct perf_event_context *ctx, *clone_ctx = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3447 | struct perf_cpu_context *cpuctx; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3448 | void *task_ctx_data = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3449 | unsigned long flags; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3450 | int ctxn, err; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3451 | int cpu = event->cpu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3452 | |
Oleg Nesterov | 22a4ec7 | 2011-01-18 17:10:08 +0100 | [diff] [blame] | 3453 | if (!task) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3454 | /* Must be root to operate on a CPU event: */ |
| 3455 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 3456 | return ERR_PTR(-EACCES); |
| 3457 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3458 | /* |
| 3459 | * We could be clever and allow to attach a event to an |
| 3460 | * offline CPU and activate it when the CPU comes up, but |
| 3461 | * that's for later. |
| 3462 | */ |
| 3463 | if (!cpu_online(cpu)) |
| 3464 | return ERR_PTR(-ENODEV); |
| 3465 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 3466 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3467 | ctx = &cpuctx->ctx; |
| 3468 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3469 | ++ctx->pin_count; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3470 | |
| 3471 | return ctx; |
| 3472 | } |
| 3473 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3474 | err = -EINVAL; |
| 3475 | ctxn = pmu->task_ctx_nr; |
| 3476 | if (ctxn < 0) |
| 3477 | goto errout; |
| 3478 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3479 | if (event->attach_state & PERF_ATTACH_TASK_DATA) { |
| 3480 | task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL); |
| 3481 | if (!task_ctx_data) { |
| 3482 | err = -ENOMEM; |
| 3483 | goto errout; |
| 3484 | } |
| 3485 | } |
| 3486 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3487 | retry: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 3488 | ctx = perf_lock_task_context(task, ctxn, &flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3489 | if (ctx) { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3490 | clone_ctx = unclone_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3491 | ++ctx->pin_count; |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3492 | |
| 3493 | if (task_ctx_data && !ctx->task_ctx_data) { |
| 3494 | ctx->task_ctx_data = task_ctx_data; |
| 3495 | task_ctx_data = NULL; |
| 3496 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 3497 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 3498 | |
| 3499 | if (clone_ctx) |
| 3500 | put_ctx(clone_ctx); |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3501 | } else { |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3502 | ctx = alloc_perf_context(pmu, task); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3503 | err = -ENOMEM; |
| 3504 | if (!ctx) |
| 3505 | goto errout; |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 3506 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3507 | if (task_ctx_data) { |
| 3508 | ctx->task_ctx_data = task_ctx_data; |
| 3509 | task_ctx_data = NULL; |
| 3510 | } |
| 3511 | |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3512 | err = 0; |
| 3513 | mutex_lock(&task->perf_event_mutex); |
| 3514 | /* |
| 3515 | * If it has already passed perf_event_exit_task(). |
| 3516 | * we must see PF_EXITING, it takes this mutex too. |
| 3517 | */ |
| 3518 | if (task->flags & PF_EXITING) |
| 3519 | err = -ESRCH; |
| 3520 | else if (task->perf_event_ctxp[ctxn]) |
| 3521 | err = -EAGAIN; |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3522 | else { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3523 | get_ctx(ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3524 | ++ctx->pin_count; |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3525 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 3526 | } |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3527 | mutex_unlock(&task->perf_event_mutex); |
| 3528 | |
| 3529 | if (unlikely(err)) { |
Peter Zijlstra | 9137fb2 | 2011-04-09 21:17:41 +0200 | [diff] [blame] | 3530 | put_ctx(ctx); |
Oleg Nesterov | dbe08d8 | 2011-01-19 19:22:07 +0100 | [diff] [blame] | 3531 | |
| 3532 | if (err == -EAGAIN) |
| 3533 | goto retry; |
| 3534 | goto errout; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3535 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3536 | } |
| 3537 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3538 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3539 | return ctx; |
| 3540 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3541 | errout: |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 3542 | kfree(task_ctx_data); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3543 | return ERR_PTR(err); |
| 3544 | } |
| 3545 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3546 | static void perf_event_free_filter(struct perf_event *event); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 3547 | static void perf_event_free_bpf_prog(struct perf_event *event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3548 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3549 | static void free_event_rcu(struct rcu_head *head) |
| 3550 | { |
| 3551 | struct perf_event *event; |
| 3552 | |
| 3553 | event = container_of(head, struct perf_event, rcu_head); |
| 3554 | if (event->ns) |
| 3555 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 3556 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3557 | kfree(event); |
| 3558 | } |
| 3559 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3560 | static void ring_buffer_attach(struct perf_event *event, |
| 3561 | struct ring_buffer *rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3562 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3563 | static void unaccount_event_cpu(struct perf_event *event, int cpu) |
| 3564 | { |
| 3565 | if (event->parent) |
| 3566 | return; |
| 3567 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3568 | if (is_cgroup_event(event)) |
| 3569 | atomic_dec(&per_cpu(perf_cgroup_events, cpu)); |
| 3570 | } |
| 3571 | |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3572 | #ifdef CONFIG_NO_HZ_FULL |
| 3573 | static DEFINE_SPINLOCK(nr_freq_lock); |
| 3574 | #endif |
| 3575 | |
| 3576 | static void unaccount_freq_event_nohz(void) |
| 3577 | { |
| 3578 | #ifdef CONFIG_NO_HZ_FULL |
| 3579 | spin_lock(&nr_freq_lock); |
| 3580 | if (atomic_dec_and_test(&nr_freq_events)) |
| 3581 | tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); |
| 3582 | spin_unlock(&nr_freq_lock); |
| 3583 | #endif |
| 3584 | } |
| 3585 | |
| 3586 | static void unaccount_freq_event(void) |
| 3587 | { |
| 3588 | if (tick_nohz_full_enabled()) |
| 3589 | unaccount_freq_event_nohz(); |
| 3590 | else |
| 3591 | atomic_dec(&nr_freq_events); |
| 3592 | } |
| 3593 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3594 | static void unaccount_event(struct perf_event *event) |
| 3595 | { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3596 | bool dec = false; |
| 3597 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3598 | if (event->parent) |
| 3599 | return; |
| 3600 | |
| 3601 | if (event->attach_state & PERF_ATTACH_TASK) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3602 | dec = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3603 | if (event->attr.mmap || event->attr.mmap_data) |
| 3604 | atomic_dec(&nr_mmap_events); |
| 3605 | if (event->attr.comm) |
| 3606 | atomic_dec(&nr_comm_events); |
| 3607 | if (event->attr.task) |
| 3608 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 948b26b | 2013-08-02 18:29:55 +0200 | [diff] [blame] | 3609 | if (event->attr.freq) |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 3610 | unaccount_freq_event(); |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3611 | if (event->attr.context_switch) { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3612 | dec = true; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 3613 | atomic_dec(&nr_switch_events); |
| 3614 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3615 | if (is_cgroup_event(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3616 | dec = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3617 | if (has_branch_stack(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 3618 | dec = true; |
| 3619 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 3620 | if (dec) { |
| 3621 | if (!atomic_add_unless(&perf_sched_count, -1, 1)) |
| 3622 | schedule_delayed_work(&perf_sched_work, HZ); |
| 3623 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3624 | |
| 3625 | unaccount_event_cpu(event, event->cpu); |
| 3626 | } |
| 3627 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 3628 | static void perf_sched_delayed(struct work_struct *work) |
| 3629 | { |
| 3630 | mutex_lock(&perf_sched_mutex); |
| 3631 | if (atomic_dec_and_test(&perf_sched_count)) |
| 3632 | static_branch_disable(&perf_sched_events); |
| 3633 | mutex_unlock(&perf_sched_mutex); |
| 3634 | } |
| 3635 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 3636 | /* |
| 3637 | * The following implement mutual exclusion of events on "exclusive" pmus |
| 3638 | * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled |
| 3639 | * at a time, so we disallow creating events that might conflict, namely: |
| 3640 | * |
| 3641 | * 1) cpu-wide events in the presence of per-task events, |
| 3642 | * 2) per-task events in the presence of cpu-wide events, |
| 3643 | * 3) two matching events on the same context. |
| 3644 | * |
| 3645 | * 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] | 3646 | * _free_event()), the latter -- before the first perf_install_in_context(). |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 3647 | */ |
| 3648 | static int exclusive_event_init(struct perf_event *event) |
| 3649 | { |
| 3650 | struct pmu *pmu = event->pmu; |
| 3651 | |
| 3652 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3653 | return 0; |
| 3654 | |
| 3655 | /* |
| 3656 | * Prevent co-existence of per-task and cpu-wide events on the |
| 3657 | * same exclusive pmu. |
| 3658 | * |
| 3659 | * Negative pmu::exclusive_cnt means there are cpu-wide |
| 3660 | * events on this "exclusive" pmu, positive means there are |
| 3661 | * per-task events. |
| 3662 | * |
| 3663 | * Since this is called in perf_event_alloc() path, event::ctx |
| 3664 | * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK |
| 3665 | * to mean "per-task event", because unlike other attach states it |
| 3666 | * never gets cleared. |
| 3667 | */ |
| 3668 | if (event->attach_state & PERF_ATTACH_TASK) { |
| 3669 | if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) |
| 3670 | return -EBUSY; |
| 3671 | } else { |
| 3672 | if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) |
| 3673 | return -EBUSY; |
| 3674 | } |
| 3675 | |
| 3676 | return 0; |
| 3677 | } |
| 3678 | |
| 3679 | static void exclusive_event_destroy(struct perf_event *event) |
| 3680 | { |
| 3681 | struct pmu *pmu = event->pmu; |
| 3682 | |
| 3683 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3684 | return; |
| 3685 | |
| 3686 | /* see comment in exclusive_event_init() */ |
| 3687 | if (event->attach_state & PERF_ATTACH_TASK) |
| 3688 | atomic_dec(&pmu->exclusive_cnt); |
| 3689 | else |
| 3690 | atomic_inc(&pmu->exclusive_cnt); |
| 3691 | } |
| 3692 | |
| 3693 | static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) |
| 3694 | { |
| 3695 | if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && |
| 3696 | (e1->cpu == e2->cpu || |
| 3697 | e1->cpu == -1 || |
| 3698 | e2->cpu == -1)) |
| 3699 | return true; |
| 3700 | return false; |
| 3701 | } |
| 3702 | |
| 3703 | /* Called under the same ctx::mutex as perf_install_in_context() */ |
| 3704 | static bool exclusive_event_installable(struct perf_event *event, |
| 3705 | struct perf_event_context *ctx) |
| 3706 | { |
| 3707 | struct perf_event *iter_event; |
| 3708 | struct pmu *pmu = event->pmu; |
| 3709 | |
| 3710 | if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE)) |
| 3711 | return true; |
| 3712 | |
| 3713 | list_for_each_entry(iter_event, &ctx->event_list, event_entry) { |
| 3714 | if (exclusive_event_match(iter_event, event)) |
| 3715 | return false; |
| 3716 | } |
| 3717 | |
| 3718 | return true; |
| 3719 | } |
| 3720 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3721 | static void _free_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3722 | { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 3723 | irq_work_sync(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3724 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 3725 | unaccount_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3726 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 3727 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3728 | /* |
| 3729 | * Can happen when we close an event with re-directed output. |
| 3730 | * |
| 3731 | * Since we have a 0 refcount, perf_mmap_close() will skip |
| 3732 | * over us; possibly making our ring_buffer_put() the last. |
| 3733 | */ |
| 3734 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 3735 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 3736 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3737 | } |
| 3738 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 3739 | if (is_cgroup_event(event)) |
| 3740 | perf_detach_cgroup(event); |
| 3741 | |
Peter Zijlstra | a0733e6 | 2016-01-26 12:14:40 +0100 | [diff] [blame] | 3742 | if (!event->parent) { |
| 3743 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 3744 | put_callchain_buffers(); |
| 3745 | } |
| 3746 | |
| 3747 | perf_event_free_bpf_prog(event); |
| 3748 | |
| 3749 | if (event->destroy) |
| 3750 | event->destroy(event); |
| 3751 | |
| 3752 | if (event->ctx) |
| 3753 | put_ctx(event->ctx); |
| 3754 | |
| 3755 | if (event->pmu) { |
| 3756 | exclusive_event_destroy(event); |
| 3757 | module_put(event->pmu->module); |
| 3758 | } |
| 3759 | |
| 3760 | call_rcu(&event->rcu_head, free_event_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3761 | } |
| 3762 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3763 | /* |
| 3764 | * Used to free events which have a known refcount of 1, such as in error paths |
| 3765 | * where the event isn't exposed yet and inherited events. |
| 3766 | */ |
| 3767 | static void free_event(struct perf_event *event) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3768 | { |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3769 | if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, |
| 3770 | "unexpected event refcount: %ld; ptr=%p\n", |
| 3771 | atomic_long_read(&event->refcount), event)) { |
| 3772 | /* leak to avoid use-after-free */ |
| 3773 | return; |
| 3774 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3775 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3776 | _free_event(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3777 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3778 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3779 | /* |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3780 | * Remove user event from the owner task. |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3781 | */ |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3782 | static void perf_remove_from_owner(struct perf_event *event) |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3783 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3784 | struct task_struct *owner; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3785 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3786 | rcu_read_lock(); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3787 | /* |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3788 | * Matches the smp_store_release() in perf_event_exit_task(). If we |
| 3789 | * observe !owner it means the list deletion is complete and we can |
| 3790 | * indeed free this event, otherwise we need to serialize on |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3791 | * owner->perf_event_mutex. |
| 3792 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3793 | owner = lockless_dereference(event->owner); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3794 | if (owner) { |
| 3795 | /* |
| 3796 | * Since delayed_put_task_struct() also drops the last |
| 3797 | * task reference we can safely take a new reference |
| 3798 | * while holding the rcu_read_lock(). |
| 3799 | */ |
| 3800 | get_task_struct(owner); |
| 3801 | } |
| 3802 | rcu_read_unlock(); |
| 3803 | |
| 3804 | if (owner) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3805 | /* |
| 3806 | * If we're here through perf_event_exit_task() we're already |
| 3807 | * holding ctx->mutex which would be an inversion wrt. the |
| 3808 | * normal lock order. |
| 3809 | * |
| 3810 | * However we can safely take this lock because its the child |
| 3811 | * ctx->mutex. |
| 3812 | */ |
| 3813 | mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); |
| 3814 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3815 | /* |
| 3816 | * We have to re-check the event->owner field, if it is cleared |
| 3817 | * we raced with perf_event_exit_task(), acquiring the mutex |
| 3818 | * ensured they're done, and we can proceed with freeing the |
| 3819 | * event. |
| 3820 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3821 | if (event->owner) { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3822 | list_del_init(&event->owner_entry); |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 3823 | smp_store_release(&event->owner, NULL); |
| 3824 | } |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3825 | mutex_unlock(&owner->perf_event_mutex); |
| 3826 | put_task_struct(owner); |
| 3827 | } |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3828 | } |
| 3829 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3830 | static void put_event(struct perf_event *event) |
| 3831 | { |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 3832 | if (!atomic_long_dec_and_test(&event->refcount)) |
| 3833 | return; |
| 3834 | |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3835 | _free_event(event); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3836 | } |
| 3837 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3838 | /* |
| 3839 | * Kill an event dead; while event:refcount will preserve the event |
| 3840 | * object, it will not preserve its functionality. Once the last 'user' |
| 3841 | * gives up the object, we'll destroy the thing. |
| 3842 | */ |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3843 | int perf_event_release_kernel(struct perf_event *event) |
| 3844 | { |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 3845 | struct perf_event_context *ctx = event->ctx; |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3846 | struct perf_event *child, *tmp; |
| 3847 | |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 3848 | /* |
| 3849 | * If we got here through err_file: fput(event_file); we will not have |
| 3850 | * attached to a context yet. |
| 3851 | */ |
| 3852 | if (!ctx) { |
| 3853 | WARN_ON_ONCE(event->attach_state & |
| 3854 | (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); |
| 3855 | goto no_ctx; |
| 3856 | } |
| 3857 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3858 | if (!is_kernel_event(event)) |
| 3859 | perf_remove_from_owner(event); |
| 3860 | |
Peter Zijlstra | 5fa7c8e | 2016-01-26 15:25:15 +0100 | [diff] [blame] | 3861 | ctx = perf_event_ctx_lock(event); |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3862 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3863 | perf_remove_from_context(event, DETACH_GROUP); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 3864 | |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3865 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 3866 | /* |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3867 | * Mark this even as STATE_DEAD, there is no external reference to it |
| 3868 | * anymore. |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3869 | * |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3870 | * Anybody acquiring event->child_mutex after the below loop _must_ |
| 3871 | * also see this, most importantly inherit_event() which will avoid |
| 3872 | * placing more children on the list. |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3873 | * |
| 3874 | * Thus this guarantees that we will in fact observe and kill _ALL_ |
| 3875 | * child events. |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 3876 | */ |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 3877 | event->state = PERF_EVENT_STATE_DEAD; |
| 3878 | raw_spin_unlock_irq(&ctx->lock); |
| 3879 | |
| 3880 | perf_event_ctx_unlock(event, ctx); |
Peter Zijlstra | 60beda8 | 2016-01-26 14:55:02 +0100 | [diff] [blame] | 3881 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3882 | again: |
| 3883 | mutex_lock(&event->child_mutex); |
| 3884 | list_for_each_entry(child, &event->child_list, child_list) { |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3885 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3886 | /* |
| 3887 | * Cannot change, child events are not migrated, see the |
| 3888 | * comment with perf_event_ctx_lock_nested(). |
| 3889 | */ |
| 3890 | ctx = lockless_dereference(child->ctx); |
| 3891 | /* |
| 3892 | * Since child_mutex nests inside ctx::mutex, we must jump |
| 3893 | * through hoops. We start by grabbing a reference on the ctx. |
| 3894 | * |
| 3895 | * Since the event cannot get freed while we hold the |
| 3896 | * child_mutex, the context must also exist and have a !0 |
| 3897 | * reference count. |
| 3898 | */ |
| 3899 | get_ctx(ctx); |
| 3900 | |
| 3901 | /* |
| 3902 | * Now that we have a ctx ref, we can drop child_mutex, and |
| 3903 | * acquire ctx::mutex without fear of it going away. Then we |
| 3904 | * can re-acquire child_mutex. |
| 3905 | */ |
| 3906 | mutex_unlock(&event->child_mutex); |
| 3907 | mutex_lock(&ctx->mutex); |
| 3908 | mutex_lock(&event->child_mutex); |
| 3909 | |
| 3910 | /* |
| 3911 | * Now that we hold ctx::mutex and child_mutex, revalidate our |
| 3912 | * state, if child is still the first entry, it didn't get freed |
| 3913 | * and we can continue doing so. |
| 3914 | */ |
| 3915 | tmp = list_first_entry_or_null(&event->child_list, |
| 3916 | struct perf_event, child_list); |
| 3917 | if (tmp == child) { |
| 3918 | perf_remove_from_context(child, DETACH_GROUP); |
| 3919 | list_del(&child->child_list); |
| 3920 | free_event(child); |
| 3921 | /* |
| 3922 | * This matches the refcount bump in inherit_event(); |
| 3923 | * this can't be the last reference. |
| 3924 | */ |
| 3925 | put_event(event); |
| 3926 | } |
| 3927 | |
| 3928 | mutex_unlock(&event->child_mutex); |
| 3929 | mutex_unlock(&ctx->mutex); |
| 3930 | put_ctx(ctx); |
| 3931 | goto again; |
| 3932 | } |
| 3933 | mutex_unlock(&event->child_mutex); |
| 3934 | |
Peter Zijlstra | a4f4bb6 | 2016-02-24 18:45:42 +0100 | [diff] [blame] | 3935 | no_ctx: |
| 3936 | put_event(event); /* Must be the 'last' reference */ |
Peter Zijlstra | 683ede4 | 2014-05-05 12:11:24 +0200 | [diff] [blame] | 3937 | return 0; |
| 3938 | } |
| 3939 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 3940 | |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 3941 | /* |
| 3942 | * Called when the last reference to the file is gone. |
| 3943 | */ |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3944 | static int perf_release(struct inode *inode, struct file *file) |
| 3945 | { |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 3946 | perf_event_release_kernel(file->private_data); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 3947 | return 0; |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 3948 | } |
| 3949 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3950 | 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] | 3951 | { |
| 3952 | struct perf_event *child; |
| 3953 | u64 total = 0; |
| 3954 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3955 | *enabled = 0; |
| 3956 | *running = 0; |
| 3957 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3958 | mutex_lock(&event->child_mutex); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 3959 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3960 | (void)perf_event_read(event, false); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 3961 | total += perf_event_count(event); |
| 3962 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3963 | *enabled += event->total_time_enabled + |
| 3964 | atomic64_read(&event->child_total_time_enabled); |
| 3965 | *running += event->total_time_running + |
| 3966 | atomic64_read(&event->child_total_time_running); |
| 3967 | |
| 3968 | list_for_each_entry(child, &event->child_list, child_list) { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3969 | (void)perf_event_read(child, false); |
Sukadev Bhattiprolu | 01add3e | 2015-09-03 20:07:46 -0700 | [diff] [blame] | 3970 | total += perf_event_count(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 3971 | *enabled += child->total_time_enabled; |
| 3972 | *running += child->total_time_running; |
| 3973 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 3974 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3975 | |
| 3976 | return total; |
| 3977 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 3978 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3979 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3980 | static int __perf_read_group_add(struct perf_event *leader, |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 3981 | u64 read_format, u64 *values) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3982 | { |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 3983 | struct perf_event *sub; |
| 3984 | int n = 1; /* skip @nr */ |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3985 | int ret; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 3986 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 3987 | ret = perf_event_read(leader, true); |
| 3988 | if (ret) |
| 3989 | return ret; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 3990 | |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 3991 | /* |
| 3992 | * Since we co-schedule groups, {enabled,running} times of siblings |
| 3993 | * will be identical to those of the leader, so we only publish one |
| 3994 | * set. |
| 3995 | */ |
| 3996 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
| 3997 | values[n++] += leader->total_time_enabled + |
| 3998 | atomic64_read(&leader->child_total_time_enabled); |
| 3999 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4000 | |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4001 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
| 4002 | values[n++] += leader->total_time_running + |
| 4003 | atomic64_read(&leader->child_total_time_running); |
| 4004 | } |
| 4005 | |
| 4006 | /* |
| 4007 | * Write {count,id} tuples for every sibling. |
| 4008 | */ |
| 4009 | values[n++] += perf_event_count(leader); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4010 | if (read_format & PERF_FORMAT_ID) |
| 4011 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4012 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4013 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4014 | values[n++] += perf_event_count(sub); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4015 | if (read_format & PERF_FORMAT_ID) |
| 4016 | values[n++] = primary_event_id(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4017 | } |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4018 | |
| 4019 | return 0; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4020 | } |
| 4021 | |
| 4022 | static int perf_read_group(struct perf_event *event, |
| 4023 | u64 read_format, char __user *buf) |
| 4024 | { |
| 4025 | struct perf_event *leader = event->group_leader, *child; |
| 4026 | struct perf_event_context *ctx = leader->ctx; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4027 | int ret; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4028 | u64 *values; |
| 4029 | |
| 4030 | lockdep_assert_held(&ctx->mutex); |
| 4031 | |
| 4032 | values = kzalloc(event->read_size, GFP_KERNEL); |
| 4033 | if (!values) |
| 4034 | return -ENOMEM; |
| 4035 | |
| 4036 | values[0] = 1 + leader->nr_siblings; |
| 4037 | |
| 4038 | /* |
| 4039 | * By locking the child_mutex of the leader we effectively |
| 4040 | * lock the child list of all siblings.. XXX explain how. |
| 4041 | */ |
| 4042 | mutex_lock(&leader->child_mutex); |
| 4043 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4044 | ret = __perf_read_group_add(leader, read_format, values); |
| 4045 | if (ret) |
| 4046 | goto unlock; |
| 4047 | |
| 4048 | list_for_each_entry(child, &leader->child_list, child_list) { |
| 4049 | ret = __perf_read_group_add(child, read_format, values); |
| 4050 | if (ret) |
| 4051 | goto unlock; |
| 4052 | } |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4053 | |
| 4054 | mutex_unlock(&leader->child_mutex); |
| 4055 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4056 | ret = event->read_size; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4057 | if (copy_to_user(buf, values, event->read_size)) |
| 4058 | ret = -EFAULT; |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4059 | goto out; |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4060 | |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4061 | unlock: |
| 4062 | mutex_unlock(&leader->child_mutex); |
| 4063 | out: |
Peter Zijlstra | fa8c269 | 2015-09-03 20:07:49 -0700 | [diff] [blame] | 4064 | kfree(values); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 4065 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4066 | } |
| 4067 | |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4068 | static int perf_read_one(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4069 | u64 read_format, char __user *buf) |
| 4070 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4071 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4072 | u64 values[4]; |
| 4073 | int n = 0; |
| 4074 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 4075 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 4076 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 4077 | values[n++] = enabled; |
| 4078 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 4079 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4080 | if (read_format & PERF_FORMAT_ID) |
| 4081 | values[n++] = primary_event_id(event); |
| 4082 | |
| 4083 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 4084 | return -EFAULT; |
| 4085 | |
| 4086 | return n * sizeof(u64); |
| 4087 | } |
| 4088 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4089 | static bool is_event_hup(struct perf_event *event) |
| 4090 | { |
| 4091 | bool no_children; |
| 4092 | |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 4093 | if (event->state > PERF_EVENT_STATE_EXIT) |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4094 | return false; |
| 4095 | |
| 4096 | mutex_lock(&event->child_mutex); |
| 4097 | no_children = list_empty(&event->child_list); |
| 4098 | mutex_unlock(&event->child_mutex); |
| 4099 | return no_children; |
| 4100 | } |
| 4101 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4102 | /* |
| 4103 | * Read the performance event - simple non blocking version for now |
| 4104 | */ |
| 4105 | static ssize_t |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4106 | __perf_read(struct perf_event *event, char __user *buf, size_t count) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4107 | { |
| 4108 | u64 read_format = event->attr.read_format; |
| 4109 | int ret; |
| 4110 | |
| 4111 | /* |
| 4112 | * Return end-of-file for a read on a event that is in |
| 4113 | * error state (i.e. because it was pinned but it couldn't be |
| 4114 | * scheduled on to the CPU at some point). |
| 4115 | */ |
| 4116 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 4117 | return 0; |
| 4118 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 4119 | if (count < event->read_size) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4120 | return -ENOSPC; |
| 4121 | |
| 4122 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4123 | if (read_format & PERF_FORMAT_GROUP) |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4124 | ret = perf_read_group(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4125 | else |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4126 | ret = perf_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4127 | |
| 4128 | return ret; |
| 4129 | } |
| 4130 | |
| 4131 | static ssize_t |
| 4132 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 4133 | { |
| 4134 | struct perf_event *event = file->private_data; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4135 | struct perf_event_context *ctx; |
| 4136 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4137 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4138 | ctx = perf_event_ctx_lock(event); |
Peter Zijlstra (Intel) | b15f495 | 2015-09-03 20:07:47 -0700 | [diff] [blame] | 4139 | ret = __perf_read(event, buf, count); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4140 | perf_event_ctx_unlock(event, ctx); |
| 4141 | |
| 4142 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 4146 | { |
| 4147 | struct perf_event *event = file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4148 | struct ring_buffer *rb; |
Jiri Olsa | 61b6768 | 2014-08-13 19:39:56 +0200 | [diff] [blame] | 4149 | unsigned int events = POLLHUP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4150 | |
Sebastian Andrzej Siewior | e708d7a | 2014-08-04 15:31:08 +0200 | [diff] [blame] | 4151 | poll_wait(file, &event->waitq, wait); |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 4152 | |
Jiri Olsa | dc63398 | 2014-09-12 13:18:26 +0200 | [diff] [blame] | 4153 | if (is_event_hup(event)) |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 4154 | return events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4155 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4156 | /* |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4157 | * Pin the event->rb by taking event->mmap_mutex; otherwise |
| 4158 | * 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] | 4159 | */ |
| 4160 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4161 | rb = event->rb; |
| 4162 | if (rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4163 | events = atomic_xchg(&rb->poll, 0); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4164 | mutex_unlock(&event->mmap_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4165 | return events; |
| 4166 | } |
| 4167 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4168 | static void _perf_event_reset(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4169 | { |
Sukadev Bhattiprolu | 7d88962 | 2015-09-03 20:07:50 -0700 | [diff] [blame] | 4170 | (void)perf_event_read(event, false); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4171 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4172 | perf_event_update_userpage(event); |
| 4173 | } |
| 4174 | |
| 4175 | /* |
| 4176 | * Holding the top-level event's child_mutex means that any |
| 4177 | * descendant process that has inherited this event will block |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 4178 | * 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] | 4179 | * task existence requirements of perf_event_enable/disable. |
| 4180 | */ |
| 4181 | static void perf_event_for_each_child(struct perf_event *event, |
| 4182 | void (*func)(struct perf_event *)) |
| 4183 | { |
| 4184 | struct perf_event *child; |
| 4185 | |
| 4186 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4187 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4188 | mutex_lock(&event->child_mutex); |
| 4189 | func(event); |
| 4190 | list_for_each_entry(child, &event->child_list, child_list) |
| 4191 | func(child); |
| 4192 | mutex_unlock(&event->child_mutex); |
| 4193 | } |
| 4194 | |
| 4195 | static void perf_event_for_each(struct perf_event *event, |
| 4196 | void (*func)(struct perf_event *)) |
| 4197 | { |
| 4198 | struct perf_event_context *ctx = event->ctx; |
| 4199 | struct perf_event *sibling; |
| 4200 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4201 | lockdep_assert_held(&ctx->mutex); |
| 4202 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4203 | event = event->group_leader; |
| 4204 | |
| 4205 | perf_event_for_each_child(event, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4206 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
Michael Ellerman | 724b6da | 2012-04-11 11:54:13 +1000 | [diff] [blame] | 4207 | perf_event_for_each_child(sibling, func); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4208 | } |
| 4209 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4210 | static void __perf_event_period(struct perf_event *event, |
| 4211 | struct perf_cpu_context *cpuctx, |
| 4212 | struct perf_event_context *ctx, |
| 4213 | void *info) |
Peter Zijlstra | 0017960 | 2015-11-30 16:26:35 +0100 | [diff] [blame] | 4214 | { |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4215 | u64 value = *((u64 *)info); |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4216 | bool active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4217 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4218 | if (event->attr.freq) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4219 | event->attr.sample_freq = value; |
| 4220 | } else { |
| 4221 | event->attr.sample_period = value; |
| 4222 | event->hw.sample_period = value; |
| 4223 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 4224 | |
| 4225 | active = (event->state == PERF_EVENT_STATE_ACTIVE); |
| 4226 | if (active) { |
| 4227 | perf_pmu_disable(ctx->pmu); |
Peter Zijlstra | 1e02cd4 | 2016-03-10 15:39:24 +0100 | [diff] [blame] | 4228 | /* |
| 4229 | * We could be throttled; unthrottle now to avoid the tick |
| 4230 | * trying to unthrottle while we already re-started the event. |
| 4231 | */ |
| 4232 | if (event->hw.interrupts == MAX_INTERRUPTS) { |
| 4233 | event->hw.interrupts = 0; |
| 4234 | perf_log_throttle(event, 1); |
| 4235 | } |
Peter Zijlstra | bad7192 | 2013-11-27 13:54:38 +0000 | [diff] [blame] | 4236 | event->pmu->stop(event, PERF_EF_UPDATE); |
| 4237 | } |
| 4238 | |
| 4239 | local64_set(&event->hw.period_left, 0); |
| 4240 | |
| 4241 | if (active) { |
| 4242 | event->pmu->start(event, PERF_EF_RELOAD); |
| 4243 | perf_pmu_enable(ctx->pmu); |
| 4244 | } |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 4248 | { |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4249 | u64 value; |
| 4250 | |
| 4251 | if (!is_sampling_event(event)) |
| 4252 | return -EINVAL; |
| 4253 | |
| 4254 | if (copy_from_user(&value, arg, sizeof(value))) |
| 4255 | return -EFAULT; |
| 4256 | |
| 4257 | if (!value) |
| 4258 | return -EINVAL; |
| 4259 | |
| 4260 | if (event->attr.freq && value > sysctl_perf_event_sample_rate) |
| 4261 | return -EINVAL; |
| 4262 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 4263 | event_function_call(event, __perf_event_period, &value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4264 | |
Peter Zijlstra | c7999c6 | 2015-08-04 19:22:49 +0200 | [diff] [blame] | 4265 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4266 | } |
| 4267 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4268 | static const struct file_operations perf_fops; |
| 4269 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4270 | static inline int perf_fget_light(int fd, struct fd *p) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4271 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4272 | struct fd f = fdget(fd); |
| 4273 | if (!f.file) |
| 4274 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4275 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4276 | if (f.file->f_op != &perf_fops) { |
| 4277 | fdput(f); |
| 4278 | return -EBADF; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4279 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4280 | *p = f; |
| 4281 | return 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | static int perf_event_set_output(struct perf_event *event, |
| 4285 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4286 | 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] | 4287 | 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] | 4288 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4289 | 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] | 4290 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4291 | void (*func)(struct perf_event *); |
| 4292 | u32 flags = arg; |
| 4293 | |
| 4294 | switch (cmd) { |
| 4295 | case PERF_EVENT_IOC_ENABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4296 | func = _perf_event_enable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4297 | break; |
| 4298 | case PERF_EVENT_IOC_DISABLE: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4299 | func = _perf_event_disable; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4300 | break; |
| 4301 | case PERF_EVENT_IOC_RESET: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4302 | func = _perf_event_reset; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4303 | break; |
| 4304 | |
| 4305 | case PERF_EVENT_IOC_REFRESH: |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4306 | return _perf_event_refresh(event, arg); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4307 | |
| 4308 | case PERF_EVENT_IOC_PERIOD: |
| 4309 | return perf_event_period(event, (u64 __user *)arg); |
| 4310 | |
Jiri Olsa | cf4957f | 2012-10-24 13:37:58 +0200 | [diff] [blame] | 4311 | case PERF_EVENT_IOC_ID: |
| 4312 | { |
| 4313 | u64 id = primary_event_id(event); |
| 4314 | |
| 4315 | if (copy_to_user((void __user *)arg, &id, sizeof(id))) |
| 4316 | return -EFAULT; |
| 4317 | return 0; |
| 4318 | } |
| 4319 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4320 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4321 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4322 | int ret; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4323 | if (arg != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 4324 | struct perf_event *output_event; |
| 4325 | struct fd output; |
| 4326 | ret = perf_fget_light(arg, &output); |
| 4327 | if (ret) |
| 4328 | return ret; |
| 4329 | output_event = output.file->private_data; |
| 4330 | ret = perf_event_set_output(event, output_event); |
| 4331 | fdput(output); |
| 4332 | } else { |
| 4333 | ret = perf_event_set_output(event, NULL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4334 | } |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4335 | return ret; |
| 4336 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4337 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4338 | case PERF_EVENT_IOC_SET_FILTER: |
| 4339 | return perf_event_set_filter(event, (void __user *)arg); |
| 4340 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 4341 | case PERF_EVENT_IOC_SET_BPF: |
| 4342 | return perf_event_set_bpf_prog(event, arg); |
| 4343 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4344 | default: |
| 4345 | return -ENOTTY; |
| 4346 | } |
| 4347 | |
| 4348 | if (flags & PERF_IOC_FLAG_GROUP) |
| 4349 | perf_event_for_each(event, func); |
| 4350 | else |
| 4351 | perf_event_for_each_child(event, func); |
| 4352 | |
| 4353 | return 0; |
| 4354 | } |
| 4355 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4356 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 4357 | { |
| 4358 | struct perf_event *event = file->private_data; |
| 4359 | struct perf_event_context *ctx; |
| 4360 | long ret; |
| 4361 | |
| 4362 | ctx = perf_event_ctx_lock(event); |
| 4363 | ret = _perf_ioctl(event, cmd, arg); |
| 4364 | perf_event_ctx_unlock(event, ctx); |
| 4365 | |
| 4366 | return ret; |
| 4367 | } |
| 4368 | |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4369 | #ifdef CONFIG_COMPAT |
| 4370 | static long perf_compat_ioctl(struct file *file, unsigned int cmd, |
| 4371 | unsigned long arg) |
| 4372 | { |
| 4373 | switch (_IOC_NR(cmd)) { |
| 4374 | case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): |
| 4375 | case _IOC_NR(PERF_EVENT_IOC_ID): |
| 4376 | /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ |
| 4377 | if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { |
| 4378 | cmd &= ~IOCSIZE_MASK; |
| 4379 | cmd |= sizeof(void *) << IOCSIZE_SHIFT; |
| 4380 | } |
| 4381 | break; |
| 4382 | } |
| 4383 | return perf_ioctl(file, cmd, arg); |
| 4384 | } |
| 4385 | #else |
| 4386 | # define perf_compat_ioctl NULL |
| 4387 | #endif |
| 4388 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4389 | int perf_event_task_enable(void) |
| 4390 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4391 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4392 | struct perf_event *event; |
| 4393 | |
| 4394 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4395 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4396 | ctx = perf_event_ctx_lock(event); |
| 4397 | perf_event_for_each_child(event, _perf_event_enable); |
| 4398 | perf_event_ctx_unlock(event, ctx); |
| 4399 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4400 | mutex_unlock(¤t->perf_event_mutex); |
| 4401 | |
| 4402 | return 0; |
| 4403 | } |
| 4404 | |
| 4405 | int perf_event_task_disable(void) |
| 4406 | { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4407 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4408 | struct perf_event *event; |
| 4409 | |
| 4410 | mutex_lock(¤t->perf_event_mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 4411 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { |
| 4412 | ctx = perf_event_ctx_lock(event); |
| 4413 | perf_event_for_each_child(event, _perf_event_disable); |
| 4414 | perf_event_ctx_unlock(event, ctx); |
| 4415 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4416 | mutex_unlock(¤t->perf_event_mutex); |
| 4417 | |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4421 | static int perf_event_index(struct perf_event *event) |
| 4422 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 4423 | if (event->hw.state & PERF_HES_STOPPED) |
| 4424 | return 0; |
| 4425 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4426 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 4427 | return 0; |
| 4428 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 4429 | return event->pmu->event_idx(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4430 | } |
| 4431 | |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4432 | static void calc_timer_values(struct perf_event *event, |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4433 | u64 *now, |
Eric B Munson | 7f310a5 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4434 | u64 *enabled, |
| 4435 | u64 *running) |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4436 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4437 | u64 ctx_time; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4438 | |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4439 | *now = perf_clock(); |
| 4440 | ctx_time = event->shadow_ctx_time + *now; |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 4441 | *enabled = ctx_time - event->tstamp_enabled; |
| 4442 | *running = ctx_time - event->tstamp_running; |
| 4443 | } |
| 4444 | |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4445 | static void perf_event_init_userpage(struct perf_event *event) |
| 4446 | { |
| 4447 | struct perf_event_mmap_page *userpg; |
| 4448 | struct ring_buffer *rb; |
| 4449 | |
| 4450 | rcu_read_lock(); |
| 4451 | rb = rcu_dereference(event->rb); |
| 4452 | if (!rb) |
| 4453 | goto unlock; |
| 4454 | |
| 4455 | userpg = rb->user_page; |
| 4456 | |
| 4457 | /* Allow new userspace to detect that bit 0 is deprecated */ |
| 4458 | userpg->cap_bit0_is_deprecated = 1; |
| 4459 | userpg->size = offsetof(struct perf_event_mmap_page, __reserved); |
Alexander Shishkin | e8c6dea | 2015-01-14 14:18:10 +0200 | [diff] [blame] | 4460 | userpg->data_offset = PAGE_SIZE; |
| 4461 | userpg->data_size = perf_data_size(rb); |
Peter Zijlstra | fa731587 | 2013-09-19 10:16:42 +0200 | [diff] [blame] | 4462 | |
| 4463 | unlock: |
| 4464 | rcu_read_unlock(); |
| 4465 | } |
| 4466 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4467 | void __weak arch_perf_update_userpage( |
| 4468 | struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4469 | { |
| 4470 | } |
| 4471 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4472 | /* |
| 4473 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 4474 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 4475 | * code calls this from NMI context. |
| 4476 | */ |
| 4477 | void perf_event_update_userpage(struct perf_event *event) |
| 4478 | { |
| 4479 | struct perf_event_mmap_page *userpg; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4480 | struct ring_buffer *rb; |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4481 | u64 enabled, running, now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4482 | |
| 4483 | rcu_read_lock(); |
Peter Zijlstra | 5ec4c59 | 2013-08-02 21:16:30 +0200 | [diff] [blame] | 4484 | rb = rcu_dereference(event->rb); |
| 4485 | if (!rb) |
| 4486 | goto unlock; |
| 4487 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4488 | /* |
| 4489 | * compute total_time_enabled, total_time_running |
| 4490 | * based on snapshot values taken when the event |
| 4491 | * was last scheduled in. |
| 4492 | * |
| 4493 | * we cannot simply called update_context_time() |
| 4494 | * because of locking issue as we can be called in |
| 4495 | * NMI context |
| 4496 | */ |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4497 | calc_timer_values(event, &now, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4498 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4499 | userpg = rb->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4500 | /* |
| 4501 | * Disable preemption so as to not let the corresponding user-space |
| 4502 | * spin too long if we get preempted. |
| 4503 | */ |
| 4504 | preempt_disable(); |
| 4505 | ++userpg->lock; |
| 4506 | barrier(); |
| 4507 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 4508 | userpg->offset = perf_event_count(event); |
Peter Zijlstra | 365a403 | 2011-11-21 20:58:59 +0100 | [diff] [blame] | 4509 | if (userpg->index) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4510 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4511 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4512 | userpg->time_enabled = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4513 | atomic64_read(&event->child_total_time_enabled); |
| 4514 | |
Eric B Munson | 0d64120 | 2011-06-24 12:26:26 -0400 | [diff] [blame] | 4515 | userpg->time_running = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4516 | atomic64_read(&event->child_total_time_running); |
| 4517 | |
Andy Lutomirski | c1317ec | 2014-10-24 15:58:11 -0700 | [diff] [blame] | 4518 | arch_perf_update_userpage(event, userpg, now); |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 4519 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4520 | barrier(); |
| 4521 | ++userpg->lock; |
| 4522 | preempt_enable(); |
| 4523 | unlock: |
| 4524 | rcu_read_unlock(); |
| 4525 | } |
| 4526 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4527 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 4528 | { |
| 4529 | struct perf_event *event = vma->vm_file->private_data; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4530 | struct ring_buffer *rb; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4531 | int ret = VM_FAULT_SIGBUS; |
| 4532 | |
| 4533 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 4534 | if (vmf->pgoff == 0) |
| 4535 | ret = 0; |
| 4536 | return ret; |
| 4537 | } |
| 4538 | |
| 4539 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4540 | rb = rcu_dereference(event->rb); |
| 4541 | if (!rb) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4542 | goto unlock; |
| 4543 | |
| 4544 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 4545 | goto unlock; |
| 4546 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4547 | vmf->page = perf_mmap_to_page(rb, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4548 | if (!vmf->page) |
| 4549 | goto unlock; |
| 4550 | |
| 4551 | get_page(vmf->page); |
| 4552 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 4553 | vmf->page->index = vmf->pgoff; |
| 4554 | |
| 4555 | ret = 0; |
| 4556 | unlock: |
| 4557 | rcu_read_unlock(); |
| 4558 | |
| 4559 | return ret; |
| 4560 | } |
| 4561 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4562 | static void ring_buffer_attach(struct perf_event *event, |
| 4563 | struct ring_buffer *rb) |
| 4564 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4565 | struct ring_buffer *old_rb = NULL; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4566 | unsigned long flags; |
| 4567 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4568 | if (event->rb) { |
| 4569 | /* |
| 4570 | * Should be impossible, we set this when removing |
| 4571 | * event->rb_entry and wait/clear when adding event->rb_entry. |
| 4572 | */ |
| 4573 | WARN_ON_ONCE(event->rcu_pending); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4574 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4575 | old_rb = event->rb; |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4576 | spin_lock_irqsave(&old_rb->event_lock, flags); |
| 4577 | list_del_rcu(&event->rb_entry); |
| 4578 | spin_unlock_irqrestore(&old_rb->event_lock, flags); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4579 | |
Oleg Nesterov | 2f993cf | 2015-05-30 22:04:25 +0200 | [diff] [blame] | 4580 | event->rcu_batches = get_state_synchronize_rcu(); |
| 4581 | event->rcu_pending = 1; |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4582 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4583 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4584 | if (rb) { |
Oleg Nesterov | 2f993cf | 2015-05-30 22:04:25 +0200 | [diff] [blame] | 4585 | if (event->rcu_pending) { |
| 4586 | cond_synchronize_rcu(event->rcu_batches); |
| 4587 | event->rcu_pending = 0; |
| 4588 | } |
| 4589 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4590 | spin_lock_irqsave(&rb->event_lock, flags); |
| 4591 | list_add_rcu(&event->rb_entry, &rb->event_list); |
| 4592 | spin_unlock_irqrestore(&rb->event_lock, flags); |
| 4593 | } |
| 4594 | |
| 4595 | rcu_assign_pointer(event->rb, rb); |
| 4596 | |
| 4597 | if (old_rb) { |
| 4598 | ring_buffer_put(old_rb); |
| 4599 | /* |
| 4600 | * Since we detached before setting the new rb, so that we |
| 4601 | * could attach the new rb, we could have missed a wakeup. |
| 4602 | * Provide it now. |
| 4603 | */ |
| 4604 | wake_up_all(&event->waitq); |
| 4605 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | static void ring_buffer_wakeup(struct perf_event *event) |
| 4609 | { |
| 4610 | struct ring_buffer *rb; |
| 4611 | |
| 4612 | rcu_read_lock(); |
| 4613 | rb = rcu_dereference(event->rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4614 | if (rb) { |
| 4615 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) |
| 4616 | wake_up_all(&event->waitq); |
| 4617 | } |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4618 | rcu_read_unlock(); |
| 4619 | } |
| 4620 | |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 4621 | struct ring_buffer *ring_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4622 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4623 | struct ring_buffer *rb; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4624 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4625 | rcu_read_lock(); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4626 | rb = rcu_dereference(event->rb); |
| 4627 | if (rb) { |
| 4628 | if (!atomic_inc_not_zero(&rb->refcount)) |
| 4629 | rb = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4630 | } |
| 4631 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4632 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4633 | return rb; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4634 | } |
| 4635 | |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 4636 | void ring_buffer_put(struct ring_buffer *rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4637 | { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4638 | if (!atomic_dec_and_test(&rb->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4639 | return; |
| 4640 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4641 | WARN_ON_ONCE(!list_empty(&rb->event_list)); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 4642 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4643 | call_rcu(&rb->rcu_head, rb_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 4647 | { |
| 4648 | struct perf_event *event = vma->vm_file->private_data; |
| 4649 | |
| 4650 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4651 | atomic_inc(&event->rb->mmap_count); |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4652 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4653 | if (vma->vm_pgoff) |
| 4654 | atomic_inc(&event->rb->aux_mmap_count); |
| 4655 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4656 | if (event->pmu->event_mapped) |
| 4657 | event->pmu->event_mapped(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4658 | } |
| 4659 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4660 | /* |
| 4661 | * A buffer can be mmap()ed multiple times; either directly through the same |
| 4662 | * event, or through other events by use of perf_event_set_output(). |
| 4663 | * |
| 4664 | * In order to undo the VM accounting done by perf_mmap() we need to destroy |
| 4665 | * the buffer here, where we still have a VM context. This means we need |
| 4666 | * to detach all events redirecting to us. |
| 4667 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4668 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 4669 | { |
| 4670 | struct perf_event *event = vma->vm_file->private_data; |
| 4671 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4672 | struct ring_buffer *rb = ring_buffer_get(event); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4673 | struct user_struct *mmap_user = rb->mmap_user; |
| 4674 | int mmap_locked = rb->mmap_locked; |
| 4675 | unsigned long size = perf_data_size(rb); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4676 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4677 | if (event->pmu->event_unmapped) |
| 4678 | event->pmu->event_unmapped(event); |
| 4679 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4680 | /* |
| 4681 | * rb->aux_mmap_count will always drop before rb->mmap_count and |
| 4682 | * event->mmap_count, so it is ok to use event->mmap_mutex to |
| 4683 | * serialize with perf_mmap here. |
| 4684 | */ |
| 4685 | if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && |
| 4686 | atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) { |
| 4687 | atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm); |
| 4688 | vma->vm_mm->pinned_vm -= rb->aux_mmap_locked; |
| 4689 | |
| 4690 | rb_free_aux(rb); |
| 4691 | mutex_unlock(&event->mmap_mutex); |
| 4692 | } |
| 4693 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4694 | atomic_dec(&rb->mmap_count); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4695 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4696 | if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4697 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4698 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4699 | ring_buffer_attach(event, NULL); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4700 | mutex_unlock(&event->mmap_mutex); |
| 4701 | |
| 4702 | /* 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] | 4703 | if (atomic_read(&rb->mmap_count)) |
| 4704 | goto out_put; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4705 | |
| 4706 | /* |
| 4707 | * No other mmap()s, detach from all other events that might redirect |
| 4708 | * into the now unreachable buffer. Somewhat complicated by the |
| 4709 | * fact that rb::event_lock otherwise nests inside mmap_mutex. |
| 4710 | */ |
| 4711 | again: |
| 4712 | rcu_read_lock(); |
| 4713 | list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { |
| 4714 | if (!atomic_long_inc_not_zero(&event->refcount)) { |
| 4715 | /* |
| 4716 | * This event is en-route to free_event() which will |
| 4717 | * detach it and remove it from the list. |
| 4718 | */ |
| 4719 | continue; |
| 4720 | } |
| 4721 | rcu_read_unlock(); |
| 4722 | |
| 4723 | mutex_lock(&event->mmap_mutex); |
| 4724 | /* |
| 4725 | * Check we didn't race with perf_event_set_output() which can |
| 4726 | * swizzle the rb from under us while we were waiting to |
| 4727 | * acquire mmap_mutex. |
| 4728 | * |
| 4729 | * If we find a different rb; ignore this event, a next |
| 4730 | * iteration will no longer find it on the list. We have to |
| 4731 | * still restart the iteration to make sure we're not now |
| 4732 | * iterating the wrong list. |
| 4733 | */ |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4734 | if (event->rb == rb) |
| 4735 | ring_buffer_attach(event, NULL); |
| 4736 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4737 | mutex_unlock(&event->mmap_mutex); |
| 4738 | put_event(event); |
| 4739 | |
| 4740 | /* |
| 4741 | * Restart the iteration; either we're on the wrong list or |
| 4742 | * destroyed its integrity by doing a deletion. |
| 4743 | */ |
| 4744 | goto again; |
| 4745 | } |
| 4746 | rcu_read_unlock(); |
| 4747 | |
| 4748 | /* |
| 4749 | * It could be there's still a few 0-ref events on the list; they'll |
| 4750 | * get cleaned up by free_event() -- they'll also still have their |
| 4751 | * ref on the rb and will free it whenever they are done with it. |
| 4752 | * |
| 4753 | * Aside from that, this buffer is 'fully' detached and unmapped, |
| 4754 | * undo the VM accounting. |
| 4755 | */ |
| 4756 | |
| 4757 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm); |
| 4758 | vma->vm_mm->pinned_vm -= mmap_locked; |
| 4759 | free_uid(mmap_user); |
| 4760 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 4761 | out_put: |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4762 | ring_buffer_put(rb); /* could be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4763 | } |
| 4764 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 4765 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4766 | .open = perf_mmap_open, |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4767 | .close = perf_mmap_close, /* non mergable */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4768 | .fault = perf_mmap_fault, |
| 4769 | .page_mkwrite = perf_mmap_fault, |
| 4770 | }; |
| 4771 | |
| 4772 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 4773 | { |
| 4774 | struct perf_event *event = file->private_data; |
| 4775 | unsigned long user_locked, user_lock_limit; |
| 4776 | struct user_struct *user = current_user(); |
| 4777 | unsigned long locked, lock_limit; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4778 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4779 | unsigned long vma_size; |
| 4780 | unsigned long nr_pages; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4781 | long user_extra = 0, extra = 0; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4782 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4783 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4784 | /* |
| 4785 | * Don't allow mmap() of inherited per-task counters. This would |
| 4786 | * create a performance issue due to all children writing to the |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4787 | * same rb. |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 4788 | */ |
| 4789 | if (event->cpu == -1 && event->attr.inherit) |
| 4790 | return -EINVAL; |
| 4791 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4792 | if (!(vma->vm_flags & VM_SHARED)) |
| 4793 | return -EINVAL; |
| 4794 | |
| 4795 | vma_size = vma->vm_end - vma->vm_start; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4796 | |
| 4797 | if (vma->vm_pgoff == 0) { |
| 4798 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 4799 | } else { |
| 4800 | /* |
| 4801 | * AUX area mapping: if rb->aux_nr_pages != 0, it's already |
| 4802 | * mapped, all subsequent mappings should have the same size |
| 4803 | * and offset. Must be above the normal perf buffer. |
| 4804 | */ |
| 4805 | u64 aux_offset, aux_size; |
| 4806 | |
| 4807 | if (!event->rb) |
| 4808 | return -EINVAL; |
| 4809 | |
| 4810 | nr_pages = vma_size / PAGE_SIZE; |
| 4811 | |
| 4812 | mutex_lock(&event->mmap_mutex); |
| 4813 | ret = -EINVAL; |
| 4814 | |
| 4815 | rb = event->rb; |
| 4816 | if (!rb) |
| 4817 | goto aux_unlock; |
| 4818 | |
| 4819 | aux_offset = ACCESS_ONCE(rb->user_page->aux_offset); |
| 4820 | aux_size = ACCESS_ONCE(rb->user_page->aux_size); |
| 4821 | |
| 4822 | if (aux_offset < perf_data_size(rb) + PAGE_SIZE) |
| 4823 | goto aux_unlock; |
| 4824 | |
| 4825 | if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) |
| 4826 | goto aux_unlock; |
| 4827 | |
| 4828 | /* already mapped with a different offset */ |
| 4829 | if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) |
| 4830 | goto aux_unlock; |
| 4831 | |
| 4832 | if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) |
| 4833 | goto aux_unlock; |
| 4834 | |
| 4835 | /* already mapped with a different size */ |
| 4836 | if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) |
| 4837 | goto aux_unlock; |
| 4838 | |
| 4839 | if (!is_power_of_2(nr_pages)) |
| 4840 | goto aux_unlock; |
| 4841 | |
| 4842 | if (!atomic_inc_not_zero(&rb->mmap_count)) |
| 4843 | goto aux_unlock; |
| 4844 | |
| 4845 | if (rb_has_aux(rb)) { |
| 4846 | atomic_inc(&rb->aux_mmap_count); |
| 4847 | ret = 0; |
| 4848 | goto unlock; |
| 4849 | } |
| 4850 | |
| 4851 | atomic_set(&rb->aux_mmap_count, 1); |
| 4852 | user_extra = nr_pages; |
| 4853 | |
| 4854 | goto accounting; |
| 4855 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4856 | |
| 4857 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4858 | * 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] | 4859 | * can do bitmasks instead of modulo. |
| 4860 | */ |
Kan Liang | 2ed1131 | 2015-03-02 02:14:26 -0500 | [diff] [blame] | 4861 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4862 | return -EINVAL; |
| 4863 | |
| 4864 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 4865 | return -EINVAL; |
| 4866 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4867 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4868 | again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4869 | mutex_lock(&event->mmap_mutex); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4870 | if (event->rb) { |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4871 | if (event->rb->nr_pages != nr_pages) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4872 | ret = -EINVAL; |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4873 | goto unlock; |
| 4874 | } |
| 4875 | |
| 4876 | if (!atomic_inc_not_zero(&event->rb->mmap_count)) { |
| 4877 | /* |
| 4878 | * Raced against perf_mmap_close() through |
| 4879 | * perf_event_set_output(). Try again, hope for better |
| 4880 | * luck. |
| 4881 | */ |
| 4882 | mutex_unlock(&event->mmap_mutex); |
| 4883 | goto again; |
| 4884 | } |
| 4885 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4886 | goto unlock; |
| 4887 | } |
| 4888 | |
| 4889 | user_extra = nr_pages + 1; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4890 | |
| 4891 | accounting: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4892 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 4893 | |
| 4894 | /* |
| 4895 | * Increase the limit linearly with more CPUs: |
| 4896 | */ |
| 4897 | user_lock_limit *= num_online_cpus(); |
| 4898 | |
| 4899 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 4900 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4901 | if (user_locked > user_lock_limit) |
| 4902 | extra = user_locked - user_lock_limit; |
| 4903 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 4904 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4905 | lock_limit >>= PAGE_SHIFT; |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 4906 | locked = vma->vm_mm->pinned_vm + extra; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4907 | |
| 4908 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 4909 | !capable(CAP_IPC_LOCK)) { |
| 4910 | ret = -EPERM; |
| 4911 | goto unlock; |
| 4912 | } |
| 4913 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4914 | WARN_ON(!rb && event->rb); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 4915 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4916 | if (vma->vm_flags & VM_WRITE) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4917 | flags |= RING_BUFFER_WRITABLE; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 4918 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 4919 | if (!rb) { |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4920 | rb = rb_alloc(nr_pages, |
| 4921 | event->attr.watermark ? event->attr.wakeup_watermark : 0, |
| 4922 | event->cpu, flags); |
| 4923 | |
| 4924 | if (!rb) { |
| 4925 | ret = -ENOMEM; |
| 4926 | goto unlock; |
| 4927 | } |
| 4928 | |
| 4929 | atomic_set(&rb->mmap_count, 1); |
| 4930 | rb->mmap_user = get_current_user(); |
| 4931 | rb->mmap_locked = extra; |
| 4932 | |
| 4933 | ring_buffer_attach(event, rb); |
| 4934 | |
| 4935 | perf_event_init_userpage(event); |
| 4936 | perf_event_update_userpage(event); |
| 4937 | } else { |
Alexander Shishkin | 1a59413 | 2015-01-14 14:18:18 +0200 | [diff] [blame] | 4938 | ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, |
| 4939 | event->attr.aux_watermark, flags); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4940 | if (!ret) |
| 4941 | rb->aux_mmap_locked = extra; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4942 | } |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4943 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4944 | unlock: |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4945 | if (!ret) { |
| 4946 | atomic_long_add(user_extra, &user->locked_vm); |
| 4947 | vma->vm_mm->pinned_vm += extra; |
| 4948 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 4949 | atomic_inc(&event->mmap_count); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 4950 | } else if (rb) { |
| 4951 | atomic_dec(&rb->mmap_count); |
| 4952 | } |
| 4953 | aux_unlock: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4954 | mutex_unlock(&event->mmap_mutex); |
| 4955 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 4956 | /* |
| 4957 | * Since pinned accounting is per vm we cannot allow fork() to copy our |
| 4958 | * vma. |
| 4959 | */ |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 4960 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4961 | vma->vm_ops = &perf_mmap_vmops; |
| 4962 | |
Andy Lutomirski | 1e0fb9e | 2014-10-24 15:58:10 -0700 | [diff] [blame] | 4963 | if (event->pmu->event_mapped) |
| 4964 | event->pmu->event_mapped(event); |
| 4965 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4966 | return ret; |
| 4967 | } |
| 4968 | |
| 4969 | static int perf_fasync(int fd, struct file *filp, int on) |
| 4970 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4971 | struct inode *inode = file_inode(filp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4972 | struct perf_event *event = filp->private_data; |
| 4973 | int retval; |
| 4974 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4975 | inode_lock(inode); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4976 | retval = fasync_helper(fd, filp, on, &event->fasync); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4977 | inode_unlock(inode); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4978 | |
| 4979 | if (retval < 0) |
| 4980 | return retval; |
| 4981 | |
| 4982 | return 0; |
| 4983 | } |
| 4984 | |
| 4985 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 4986 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4987 | .release = perf_release, |
| 4988 | .read = perf_read, |
| 4989 | .poll = perf_poll, |
| 4990 | .unlocked_ioctl = perf_ioctl, |
Pawel Moll | b3f2078 | 2014-06-13 16:03:32 +0100 | [diff] [blame] | 4991 | .compat_ioctl = perf_compat_ioctl, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4992 | .mmap = perf_mmap, |
| 4993 | .fasync = perf_fasync, |
| 4994 | }; |
| 4995 | |
| 4996 | /* |
| 4997 | * Perf event wakeup |
| 4998 | * |
| 4999 | * If there's data, ensure we set the poll() state and publish everything |
| 5000 | * to user-space before waking everybody up. |
| 5001 | */ |
| 5002 | |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 5003 | static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) |
| 5004 | { |
| 5005 | /* only the parent has fasync state */ |
| 5006 | if (event->parent) |
| 5007 | event = event->parent; |
| 5008 | return &event->fasync; |
| 5009 | } |
| 5010 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5011 | void perf_event_wakeup(struct perf_event *event) |
| 5012 | { |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 5013 | ring_buffer_wakeup(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5014 | |
| 5015 | if (event->pending_kill) { |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 5016 | kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5017 | event->pending_kill = 0; |
| 5018 | } |
| 5019 | } |
| 5020 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 5021 | static void perf_pending_event(struct irq_work *entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5022 | { |
| 5023 | struct perf_event *event = container_of(entry, |
| 5024 | struct perf_event, pending); |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 5025 | int rctx; |
| 5026 | |
| 5027 | rctx = perf_swevent_get_recursion_context(); |
| 5028 | /* |
| 5029 | * If we 'fail' here, that's OK, it means recursion is already disabled |
| 5030 | * and we won't recurse 'further'. |
| 5031 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5032 | |
| 5033 | if (event->pending_disable) { |
| 5034 | event->pending_disable = 0; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 5035 | perf_event_disable_local(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5036 | } |
| 5037 | |
| 5038 | if (event->pending_wakeup) { |
| 5039 | event->pending_wakeup = 0; |
| 5040 | perf_event_wakeup(event); |
| 5041 | } |
Peter Zijlstra | d525211 | 2015-02-19 18:03:11 +0100 | [diff] [blame] | 5042 | |
| 5043 | if (rctx >= 0) |
| 5044 | perf_swevent_put_recursion_context(rctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5045 | } |
| 5046 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5047 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 5048 | * We assume there is only KVM supporting the callbacks. |
| 5049 | * Later on, we might change it to a list if there is |
| 5050 | * another virtualization implementation supporting the callbacks. |
| 5051 | */ |
| 5052 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 5053 | |
| 5054 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 5055 | { |
| 5056 | perf_guest_cbs = cbs; |
| 5057 | return 0; |
| 5058 | } |
| 5059 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 5060 | |
| 5061 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 5062 | { |
| 5063 | perf_guest_cbs = NULL; |
| 5064 | return 0; |
| 5065 | } |
| 5066 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 5067 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5068 | static void |
| 5069 | perf_output_sample_regs(struct perf_output_handle *handle, |
| 5070 | struct pt_regs *regs, u64 mask) |
| 5071 | { |
| 5072 | int bit; |
| 5073 | |
| 5074 | for_each_set_bit(bit, (const unsigned long *) &mask, |
| 5075 | sizeof(mask) * BITS_PER_BYTE) { |
| 5076 | u64 val; |
| 5077 | |
| 5078 | val = perf_reg_value(regs, bit); |
| 5079 | perf_output_put(handle, val); |
| 5080 | } |
| 5081 | } |
| 5082 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5083 | static void perf_sample_regs_user(struct perf_regs *regs_user, |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5084 | struct pt_regs *regs, |
| 5085 | struct pt_regs *regs_user_copy) |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5086 | { |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5087 | if (user_mode(regs)) { |
| 5088 | regs_user->abi = perf_reg_abi(current); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5089 | regs_user->regs = regs; |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5090 | } else if (current->mm) { |
| 5091 | perf_get_regs_user(regs_user, regs, regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5092 | } else { |
| 5093 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; |
| 5094 | regs_user->regs = NULL; |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5095 | } |
| 5096 | } |
| 5097 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5098 | static void perf_sample_regs_intr(struct perf_regs *regs_intr, |
| 5099 | struct pt_regs *regs) |
| 5100 | { |
| 5101 | regs_intr->regs = regs; |
| 5102 | regs_intr->abi = perf_reg_abi(current); |
| 5103 | } |
| 5104 | |
| 5105 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5106 | /* |
| 5107 | * Get remaining task size from user stack pointer. |
| 5108 | * |
| 5109 | * It'd be better to take stack vma map and limit this more |
| 5110 | * precisly, but there's no way to get it safely under interrupt, |
| 5111 | * so using TASK_SIZE as limit. |
| 5112 | */ |
| 5113 | static u64 perf_ustack_task_size(struct pt_regs *regs) |
| 5114 | { |
| 5115 | unsigned long addr = perf_user_stack_pointer(regs); |
| 5116 | |
| 5117 | if (!addr || addr >= TASK_SIZE) |
| 5118 | return 0; |
| 5119 | |
| 5120 | return TASK_SIZE - addr; |
| 5121 | } |
| 5122 | |
| 5123 | static u16 |
| 5124 | perf_sample_ustack_size(u16 stack_size, u16 header_size, |
| 5125 | struct pt_regs *regs) |
| 5126 | { |
| 5127 | u64 task_size; |
| 5128 | |
| 5129 | /* No regs, no stack pointer, no dump. */ |
| 5130 | if (!regs) |
| 5131 | return 0; |
| 5132 | |
| 5133 | /* |
| 5134 | * Check if we fit in with the requested stack size into the: |
| 5135 | * - TASK_SIZE |
| 5136 | * If we don't, we limit the size to the TASK_SIZE. |
| 5137 | * |
| 5138 | * - remaining sample size |
| 5139 | * If we don't, we customize the stack size to |
| 5140 | * fit in to the remaining sample size. |
| 5141 | */ |
| 5142 | |
| 5143 | task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); |
| 5144 | stack_size = min(stack_size, (u16) task_size); |
| 5145 | |
| 5146 | /* Current header size plus static size and dynamic size. */ |
| 5147 | header_size += 2 * sizeof(u64); |
| 5148 | |
| 5149 | /* Do we fit in with the current stack dump size? */ |
| 5150 | if ((u16) (header_size + stack_size) < header_size) { |
| 5151 | /* |
| 5152 | * If we overflow the maximum size for the sample, |
| 5153 | * we customize the stack dump size to fit in. |
| 5154 | */ |
| 5155 | stack_size = USHRT_MAX - header_size - sizeof(u64); |
| 5156 | stack_size = round_up(stack_size, sizeof(u64)); |
| 5157 | } |
| 5158 | |
| 5159 | return stack_size; |
| 5160 | } |
| 5161 | |
| 5162 | static void |
| 5163 | perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, |
| 5164 | struct pt_regs *regs) |
| 5165 | { |
| 5166 | /* Case of a kernel thread, nothing to dump */ |
| 5167 | if (!regs) { |
| 5168 | u64 size = 0; |
| 5169 | perf_output_put(handle, size); |
| 5170 | } else { |
| 5171 | unsigned long sp; |
| 5172 | unsigned int rem; |
| 5173 | u64 dyn_size; |
| 5174 | |
| 5175 | /* |
| 5176 | * We dump: |
| 5177 | * static size |
| 5178 | * - the size requested by user or the best one we can fit |
| 5179 | * in to the sample max size |
| 5180 | * data |
| 5181 | * - user stack dump data |
| 5182 | * dynamic size |
| 5183 | * - the actual dumped size |
| 5184 | */ |
| 5185 | |
| 5186 | /* Static size. */ |
| 5187 | perf_output_put(handle, dump_size); |
| 5188 | |
| 5189 | /* Data. */ |
| 5190 | sp = perf_user_stack_pointer(regs); |
| 5191 | rem = __output_copy_user(handle, (void *) sp, dump_size); |
| 5192 | dyn_size = dump_size - rem; |
| 5193 | |
| 5194 | perf_output_skip(handle, rem); |
| 5195 | |
| 5196 | /* Dynamic size. */ |
| 5197 | perf_output_put(handle, dyn_size); |
| 5198 | } |
| 5199 | } |
| 5200 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5201 | static void __perf_event_header__init_id(struct perf_event_header *header, |
| 5202 | struct perf_sample_data *data, |
| 5203 | struct perf_event *event) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5204 | { |
| 5205 | u64 sample_type = event->attr.sample_type; |
| 5206 | |
| 5207 | data->type = sample_type; |
| 5208 | header->size += event->id_header_size; |
| 5209 | |
| 5210 | if (sample_type & PERF_SAMPLE_TID) { |
| 5211 | /* namespace issues */ |
| 5212 | data->tid_entry.pid = perf_event_pid(event, current); |
| 5213 | data->tid_entry.tid = perf_event_tid(event, current); |
| 5214 | } |
| 5215 | |
| 5216 | if (sample_type & PERF_SAMPLE_TIME) |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 5217 | data->time = perf_event_clock(event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5218 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5219 | if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5220 | data->id = primary_event_id(event); |
| 5221 | |
| 5222 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5223 | data->stream_id = event->id; |
| 5224 | |
| 5225 | if (sample_type & PERF_SAMPLE_CPU) { |
| 5226 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 5227 | data->cpu_entry.reserved = 0; |
| 5228 | } |
| 5229 | } |
| 5230 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5231 | void perf_event_header__init_id(struct perf_event_header *header, |
| 5232 | struct perf_sample_data *data, |
| 5233 | struct perf_event *event) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5234 | { |
| 5235 | if (event->attr.sample_id_all) |
| 5236 | __perf_event_header__init_id(header, data, event); |
| 5237 | } |
| 5238 | |
| 5239 | static void __perf_event__output_id_sample(struct perf_output_handle *handle, |
| 5240 | struct perf_sample_data *data) |
| 5241 | { |
| 5242 | u64 sample_type = data->type; |
| 5243 | |
| 5244 | if (sample_type & PERF_SAMPLE_TID) |
| 5245 | perf_output_put(handle, data->tid_entry); |
| 5246 | |
| 5247 | if (sample_type & PERF_SAMPLE_TIME) |
| 5248 | perf_output_put(handle, data->time); |
| 5249 | |
| 5250 | if (sample_type & PERF_SAMPLE_ID) |
| 5251 | perf_output_put(handle, data->id); |
| 5252 | |
| 5253 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5254 | perf_output_put(handle, data->stream_id); |
| 5255 | |
| 5256 | if (sample_type & PERF_SAMPLE_CPU) |
| 5257 | perf_output_put(handle, data->cpu_entry); |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5258 | |
| 5259 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 5260 | perf_output_put(handle, data->id); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5261 | } |
| 5262 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5263 | void perf_event__output_id_sample(struct perf_event *event, |
| 5264 | struct perf_output_handle *handle, |
| 5265 | struct perf_sample_data *sample) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5266 | { |
| 5267 | if (event->attr.sample_id_all) |
| 5268 | __perf_event__output_id_sample(handle, sample); |
| 5269 | } |
| 5270 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5271 | static void perf_output_read_one(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5272 | struct perf_event *event, |
| 5273 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5274 | { |
| 5275 | u64 read_format = event->attr.read_format; |
| 5276 | u64 values[4]; |
| 5277 | int n = 0; |
| 5278 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5279 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5280 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5281 | values[n++] = enabled + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5282 | atomic64_read(&event->child_total_time_enabled); |
| 5283 | } |
| 5284 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5285 | values[n++] = running + |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5286 | atomic64_read(&event->child_total_time_running); |
| 5287 | } |
| 5288 | if (read_format & PERF_FORMAT_ID) |
| 5289 | values[n++] = primary_event_id(event); |
| 5290 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5291 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5292 | } |
| 5293 | |
| 5294 | /* |
| 5295 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 5296 | */ |
| 5297 | static void perf_output_read_group(struct perf_output_handle *handle, |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5298 | struct perf_event *event, |
| 5299 | u64 enabled, u64 running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5300 | { |
| 5301 | struct perf_event *leader = event->group_leader, *sub; |
| 5302 | u64 read_format = event->attr.read_format; |
| 5303 | u64 values[5]; |
| 5304 | int n = 0; |
| 5305 | |
| 5306 | values[n++] = 1 + leader->nr_siblings; |
| 5307 | |
| 5308 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5309 | values[n++] = enabled; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5310 | |
| 5311 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5312 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5313 | |
| 5314 | if (leader != event) |
| 5315 | leader->pmu->read(leader); |
| 5316 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5317 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5318 | if (read_format & PERF_FORMAT_ID) |
| 5319 | values[n++] = primary_event_id(leader); |
| 5320 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5321 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5322 | |
| 5323 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 5324 | n = 0; |
| 5325 | |
Jiri Olsa | 6f5ab00 | 2012-10-15 20:13:45 +0200 | [diff] [blame] | 5326 | if ((sub != event) && |
| 5327 | (sub->state == PERF_EVENT_STATE_ACTIVE)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5328 | sub->pmu->read(sub); |
| 5329 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5330 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5331 | if (read_format & PERF_FORMAT_ID) |
| 5332 | values[n++] = primary_event_id(sub); |
| 5333 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5334 | __output_copy(handle, values, n * sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5335 | } |
| 5336 | } |
| 5337 | |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5338 | #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ |
| 5339 | PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 5340 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5341 | static void perf_output_read(struct perf_output_handle *handle, |
| 5342 | struct perf_event *event) |
| 5343 | { |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 5344 | u64 enabled = 0, running = 0, now; |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5345 | u64 read_format = event->attr.read_format; |
| 5346 | |
| 5347 | /* |
| 5348 | * compute total_time_enabled, total_time_running |
| 5349 | * based on snapshot values taken when the event |
| 5350 | * was last scheduled in. |
| 5351 | * |
| 5352 | * we cannot simply called update_context_time() |
| 5353 | * because of locking issue as we are called in |
| 5354 | * NMI context |
| 5355 | */ |
Eric B Munson | c479429 | 2011-06-23 16:34:38 -0400 | [diff] [blame] | 5356 | if (read_format & PERF_FORMAT_TOTAL_TIMES) |
Peter Zijlstra | e3f3541 | 2011-11-21 11:43:53 +0100 | [diff] [blame] | 5357 | calc_timer_values(event, &now, &enabled, &running); |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5358 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5359 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5360 | perf_output_read_group(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5361 | else |
Stephane Eranian | eed0152 | 2010-10-26 16:08:01 +0200 | [diff] [blame] | 5362 | perf_output_read_one(handle, event, enabled, running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | void perf_output_sample(struct perf_output_handle *handle, |
| 5366 | struct perf_event_header *header, |
| 5367 | struct perf_sample_data *data, |
| 5368 | struct perf_event *event) |
| 5369 | { |
| 5370 | u64 sample_type = data->type; |
| 5371 | |
| 5372 | perf_output_put(handle, *header); |
| 5373 | |
Adrian Hunter | ff3d527 | 2013-08-27 11:23:07 +0300 | [diff] [blame] | 5374 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 5375 | perf_output_put(handle, data->id); |
| 5376 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5377 | if (sample_type & PERF_SAMPLE_IP) |
| 5378 | perf_output_put(handle, data->ip); |
| 5379 | |
| 5380 | if (sample_type & PERF_SAMPLE_TID) |
| 5381 | perf_output_put(handle, data->tid_entry); |
| 5382 | |
| 5383 | if (sample_type & PERF_SAMPLE_TIME) |
| 5384 | perf_output_put(handle, data->time); |
| 5385 | |
| 5386 | if (sample_type & PERF_SAMPLE_ADDR) |
| 5387 | perf_output_put(handle, data->addr); |
| 5388 | |
| 5389 | if (sample_type & PERF_SAMPLE_ID) |
| 5390 | perf_output_put(handle, data->id); |
| 5391 | |
| 5392 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 5393 | perf_output_put(handle, data->stream_id); |
| 5394 | |
| 5395 | if (sample_type & PERF_SAMPLE_CPU) |
| 5396 | perf_output_put(handle, data->cpu_entry); |
| 5397 | |
| 5398 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 5399 | perf_output_put(handle, data->period); |
| 5400 | |
| 5401 | if (sample_type & PERF_SAMPLE_READ) |
| 5402 | perf_output_read(handle, event); |
| 5403 | |
| 5404 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5405 | if (data->callchain) { |
| 5406 | int size = 1; |
| 5407 | |
| 5408 | if (data->callchain) |
| 5409 | size += data->callchain->nr; |
| 5410 | |
| 5411 | size *= sizeof(u64); |
| 5412 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5413 | __output_copy(handle, data->callchain, size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5414 | } else { |
| 5415 | u64 nr = 0; |
| 5416 | perf_output_put(handle, nr); |
| 5417 | } |
| 5418 | } |
| 5419 | |
| 5420 | if (sample_type & PERF_SAMPLE_RAW) { |
| 5421 | if (data->raw) { |
Alexei Starovoitov | fa128e6 | 2015-10-20 20:02:33 -0700 | [diff] [blame] | 5422 | u32 raw_size = data->raw->size; |
| 5423 | u32 real_size = round_up(raw_size + sizeof(u32), |
| 5424 | sizeof(u64)) - sizeof(u32); |
| 5425 | u64 zero = 0; |
| 5426 | |
| 5427 | perf_output_put(handle, real_size); |
| 5428 | __output_copy(handle, data->raw->data, raw_size); |
| 5429 | if (real_size - raw_size) |
| 5430 | __output_copy(handle, &zero, real_size - raw_size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5431 | } else { |
| 5432 | struct { |
| 5433 | u32 size; |
| 5434 | u32 data; |
| 5435 | } raw = { |
| 5436 | .size = sizeof(u32), |
| 5437 | .data = 0, |
| 5438 | }; |
| 5439 | perf_output_put(handle, raw); |
| 5440 | } |
| 5441 | } |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5442 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5443 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5444 | if (data->br_stack) { |
| 5445 | size_t size; |
| 5446 | |
| 5447 | size = data->br_stack->nr |
| 5448 | * sizeof(struct perf_branch_entry); |
| 5449 | |
| 5450 | perf_output_put(handle, data->br_stack->nr); |
| 5451 | perf_output_copy(handle, data->br_stack->entries, size); |
| 5452 | } else { |
| 5453 | /* |
| 5454 | * we always store at least the value of nr |
| 5455 | */ |
| 5456 | u64 nr = 0; |
| 5457 | perf_output_put(handle, nr); |
| 5458 | } |
| 5459 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5460 | |
| 5461 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5462 | u64 abi = data->regs_user.abi; |
| 5463 | |
| 5464 | /* |
| 5465 | * If there are no regs to dump, notice it through |
| 5466 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5467 | */ |
| 5468 | perf_output_put(handle, abi); |
| 5469 | |
| 5470 | if (abi) { |
| 5471 | u64 mask = event->attr.sample_regs_user; |
| 5472 | perf_output_sample_regs(handle, |
| 5473 | data->regs_user.regs, |
| 5474 | mask); |
| 5475 | } |
| 5476 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5477 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5478 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5479 | perf_output_sample_ustack(handle, |
| 5480 | data->stack_user_size, |
| 5481 | data->regs_user.regs); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5482 | } |
Andi Kleen | c3feedf | 2013-01-24 16:10:28 +0100 | [diff] [blame] | 5483 | |
| 5484 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 5485 | perf_output_put(handle, data->weight); |
Stephane Eranian | d6be9ad | 2013-01-24 16:10:31 +0100 | [diff] [blame] | 5486 | |
| 5487 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 5488 | perf_output_put(handle, data->data_src.val); |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5489 | |
Andi Kleen | fdfbbd0 | 2013-09-20 07:40:39 -0700 | [diff] [blame] | 5490 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 5491 | perf_output_put(handle, data->txn); |
| 5492 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5493 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5494 | u64 abi = data->regs_intr.abi; |
| 5495 | /* |
| 5496 | * If there are no regs to dump, notice it through |
| 5497 | * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). |
| 5498 | */ |
| 5499 | perf_output_put(handle, abi); |
| 5500 | |
| 5501 | if (abi) { |
| 5502 | u64 mask = event->attr.sample_regs_intr; |
| 5503 | |
| 5504 | perf_output_sample_regs(handle, |
| 5505 | data->regs_intr.regs, |
| 5506 | mask); |
| 5507 | } |
| 5508 | } |
| 5509 | |
Peter Zijlstra | a5cdd40 | 2013-07-16 17:09:07 +0200 | [diff] [blame] | 5510 | if (!event->attr.watermark) { |
| 5511 | int wakeup_events = event->attr.wakeup_events; |
| 5512 | |
| 5513 | if (wakeup_events) { |
| 5514 | struct ring_buffer *rb = handle->rb; |
| 5515 | int events = local_inc_return(&rb->events); |
| 5516 | |
| 5517 | if (events >= wakeup_events) { |
| 5518 | local_sub(wakeup_events, &rb->events); |
| 5519 | local_inc(&rb->wakeup); |
| 5520 | } |
| 5521 | } |
| 5522 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | void perf_prepare_sample(struct perf_event_header *header, |
| 5526 | struct perf_sample_data *data, |
| 5527 | struct perf_event *event, |
| 5528 | struct pt_regs *regs) |
| 5529 | { |
| 5530 | u64 sample_type = event->attr.sample_type; |
| 5531 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5532 | header->type = PERF_RECORD_SAMPLE; |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5533 | header->size = sizeof(*header) + event->header_size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5534 | |
| 5535 | header->misc = 0; |
| 5536 | header->misc |= perf_misc_flags(regs); |
| 5537 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5538 | __perf_event_header__init_id(header, data, event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 5539 | |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5540 | if (sample_type & PERF_SAMPLE_IP) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5541 | data->ip = perf_instruction_pointer(regs); |
| 5542 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5543 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5544 | int size = 1; |
| 5545 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 5546 | data->callchain = perf_callchain(event, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5547 | |
| 5548 | if (data->callchain) |
| 5549 | size += data->callchain->nr; |
| 5550 | |
| 5551 | header->size += size * sizeof(u64); |
| 5552 | } |
| 5553 | |
| 5554 | if (sample_type & PERF_SAMPLE_RAW) { |
| 5555 | int size = sizeof(u32); |
| 5556 | |
| 5557 | if (data->raw) |
| 5558 | size += data->raw->size; |
| 5559 | else |
| 5560 | size += sizeof(u32); |
| 5561 | |
Alexei Starovoitov | fa128e6 | 2015-10-20 20:02:33 -0700 | [diff] [blame] | 5562 | header->size += round_up(size, sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5563 | } |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 5564 | |
| 5565 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 5566 | int size = sizeof(u64); /* nr */ |
| 5567 | if (data->br_stack) { |
| 5568 | size += data->br_stack->nr |
| 5569 | * sizeof(struct perf_branch_entry); |
| 5570 | } |
| 5571 | header->size += size; |
| 5572 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5573 | |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5574 | if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER)) |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 5575 | perf_sample_regs_user(&data->regs_user, regs, |
| 5576 | &data->regs_user_copy); |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5577 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5578 | if (sample_type & PERF_SAMPLE_REGS_USER) { |
| 5579 | /* regs dump ABI info */ |
| 5580 | int size = sizeof(u64); |
| 5581 | |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 5582 | if (data->regs_user.regs) { |
| 5583 | u64 mask = event->attr.sample_regs_user; |
| 5584 | size += hweight64(mask) * sizeof(u64); |
| 5585 | } |
| 5586 | |
| 5587 | header->size += size; |
| 5588 | } |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5589 | |
| 5590 | if (sample_type & PERF_SAMPLE_STACK_USER) { |
| 5591 | /* |
| 5592 | * Either we need PERF_SAMPLE_STACK_USER bit to be allways |
| 5593 | * processed as the last one or have additional check added |
| 5594 | * in case new sample type is added, because we could eat |
| 5595 | * up the rest of the sample size. |
| 5596 | */ |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5597 | u16 stack_size = event->attr.sample_stack_user; |
| 5598 | u16 size = sizeof(u64); |
| 5599 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5600 | stack_size = perf_sample_ustack_size(stack_size, header->size, |
Peter Zijlstra | 2565711 | 2014-09-24 13:48:42 +0200 | [diff] [blame] | 5601 | data->regs_user.regs); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 5602 | |
| 5603 | /* |
| 5604 | * If there is something to dump, add space for the dump |
| 5605 | * itself and for the field that tells the dynamic size, |
| 5606 | * which is how many have been actually dumped. |
| 5607 | */ |
| 5608 | if (stack_size) |
| 5609 | size += sizeof(u64) + stack_size; |
| 5610 | |
| 5611 | data->stack_user_size = stack_size; |
| 5612 | header->size += size; |
| 5613 | } |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 5614 | |
| 5615 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 5616 | /* regs dump ABI info */ |
| 5617 | int size = sizeof(u64); |
| 5618 | |
| 5619 | perf_sample_regs_intr(&data->regs_intr, regs); |
| 5620 | |
| 5621 | if (data->regs_intr.regs) { |
| 5622 | u64 mask = event->attr.sample_regs_intr; |
| 5623 | |
| 5624 | size += hweight64(mask) * sizeof(u64); |
| 5625 | } |
| 5626 | |
| 5627 | header->size += size; |
| 5628 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5629 | } |
| 5630 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 5631 | void perf_event_output(struct perf_event *event, |
| 5632 | struct perf_sample_data *data, |
| 5633 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5634 | { |
| 5635 | struct perf_output_handle handle; |
| 5636 | struct perf_event_header header; |
| 5637 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5638 | /* protect the callchain buffers */ |
| 5639 | rcu_read_lock(); |
| 5640 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5641 | perf_prepare_sample(&header, data, event, regs); |
| 5642 | |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5643 | if (perf_output_begin(&handle, event, header.size)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5644 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5645 | |
| 5646 | perf_output_sample(&handle, &header, data, event); |
| 5647 | |
| 5648 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5649 | |
| 5650 | exit: |
| 5651 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5652 | } |
| 5653 | |
| 5654 | /* |
| 5655 | * read event_id |
| 5656 | */ |
| 5657 | |
| 5658 | struct perf_read_event { |
| 5659 | struct perf_event_header header; |
| 5660 | |
| 5661 | u32 pid; |
| 5662 | u32 tid; |
| 5663 | }; |
| 5664 | |
| 5665 | static void |
| 5666 | perf_event_read_event(struct perf_event *event, |
| 5667 | struct task_struct *task) |
| 5668 | { |
| 5669 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5670 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5671 | struct perf_read_event read_event = { |
| 5672 | .header = { |
| 5673 | .type = PERF_RECORD_READ, |
| 5674 | .misc = 0, |
Arnaldo Carvalho de Melo | c320c7b | 2010-10-20 12:50:11 -0200 | [diff] [blame] | 5675 | .size = sizeof(read_event) + event->read_size, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5676 | }, |
| 5677 | .pid = perf_event_pid(event, task), |
| 5678 | .tid = perf_event_tid(event, task), |
| 5679 | }; |
| 5680 | int ret; |
| 5681 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5682 | perf_event_header__init_id(&read_event.header, &sample, event); |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5683 | ret = perf_output_begin(&handle, event, read_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5684 | if (ret) |
| 5685 | return; |
| 5686 | |
| 5687 | perf_output_put(&handle, read_event); |
| 5688 | perf_output_read(&handle, event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5689 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5690 | |
| 5691 | perf_output_end(&handle); |
| 5692 | } |
| 5693 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5694 | typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data); |
| 5695 | |
| 5696 | static void |
| 5697 | perf_event_aux_ctx(struct perf_event_context *ctx, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5698 | perf_event_aux_output_cb output, |
| 5699 | void *data) |
| 5700 | { |
| 5701 | struct perf_event *event; |
| 5702 | |
| 5703 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 5704 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
| 5705 | continue; |
| 5706 | if (!event_filter_match(event)) |
| 5707 | continue; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5708 | output(event, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5709 | } |
| 5710 | } |
| 5711 | |
| 5712 | static void |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 5713 | perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data, |
| 5714 | struct perf_event_context *task_ctx) |
| 5715 | { |
| 5716 | rcu_read_lock(); |
| 5717 | preempt_disable(); |
| 5718 | perf_event_aux_ctx(task_ctx, output, data); |
| 5719 | preempt_enable(); |
| 5720 | rcu_read_unlock(); |
| 5721 | } |
| 5722 | |
| 5723 | static void |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5724 | perf_event_aux(perf_event_aux_output_cb output, void *data, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5725 | struct perf_event_context *task_ctx) |
| 5726 | { |
| 5727 | struct perf_cpu_context *cpuctx; |
| 5728 | struct perf_event_context *ctx; |
| 5729 | struct pmu *pmu; |
| 5730 | int ctxn; |
| 5731 | |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 5732 | /* |
| 5733 | * If we have task_ctx != NULL we only notify |
| 5734 | * the task context itself. The task_ctx is set |
| 5735 | * only for EXIT events before releasing task |
| 5736 | * context. |
| 5737 | */ |
| 5738 | if (task_ctx) { |
| 5739 | perf_event_aux_task_ctx(output, data, task_ctx); |
| 5740 | return; |
| 5741 | } |
| 5742 | |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5743 | rcu_read_lock(); |
| 5744 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 5745 | cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); |
| 5746 | if (cpuctx->unique_pmu != pmu) |
| 5747 | goto next; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5748 | perf_event_aux_ctx(&cpuctx->ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5749 | ctxn = pmu->task_ctx_nr; |
| 5750 | if (ctxn < 0) |
| 5751 | goto next; |
| 5752 | ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); |
| 5753 | if (ctx) |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5754 | perf_event_aux_ctx(ctx, output, data); |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5755 | next: |
| 5756 | put_cpu_ptr(pmu->pmu_cpu_context); |
| 5757 | } |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5758 | rcu_read_unlock(); |
| 5759 | } |
| 5760 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5761 | /* |
| 5762 | * task tracking -- fork/exit |
| 5763 | * |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5764 | * 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] | 5765 | */ |
| 5766 | |
| 5767 | struct perf_task_event { |
| 5768 | struct task_struct *task; |
| 5769 | struct perf_event_context *task_ctx; |
| 5770 | |
| 5771 | struct { |
| 5772 | struct perf_event_header header; |
| 5773 | |
| 5774 | u32 pid; |
| 5775 | u32 ppid; |
| 5776 | u32 tid; |
| 5777 | u32 ptid; |
| 5778 | u64 time; |
| 5779 | } event_id; |
| 5780 | }; |
| 5781 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5782 | static int perf_event_task_match(struct perf_event *event) |
| 5783 | { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5784 | return event->attr.comm || event->attr.mmap || |
| 5785 | event->attr.mmap2 || event->attr.mmap_data || |
| 5786 | event->attr.task; |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5787 | } |
| 5788 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5789 | static void perf_event_task_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5790 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5791 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5792 | struct perf_task_event *task_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5793 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5794 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5795 | struct task_struct *task = task_event->task; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5796 | int ret, size = task_event->event_id.header.size; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 5797 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5798 | if (!perf_event_task_match(event)) |
| 5799 | return; |
| 5800 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5801 | perf_event_header__init_id(&task_event->event_id.header, &sample, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5802 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5803 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5804 | task_event->event_id.header.size); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 5805 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5806 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5807 | |
| 5808 | task_event->event_id.pid = perf_event_pid(event, task); |
| 5809 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 5810 | |
| 5811 | task_event->event_id.tid = perf_event_tid(event, task); |
| 5812 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 5813 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 5814 | task_event->event_id.time = perf_event_clock(event); |
| 5815 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5816 | perf_output_put(&handle, task_event->event_id); |
| 5817 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5818 | perf_event__output_id_sample(event, &handle, &sample); |
| 5819 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5820 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5821 | out: |
| 5822 | task_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5823 | } |
| 5824 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5825 | static void perf_event_task(struct task_struct *task, |
| 5826 | struct perf_event_context *task_ctx, |
| 5827 | int new) |
| 5828 | { |
| 5829 | struct perf_task_event task_event; |
| 5830 | |
| 5831 | if (!atomic_read(&nr_comm_events) && |
| 5832 | !atomic_read(&nr_mmap_events) && |
| 5833 | !atomic_read(&nr_task_events)) |
| 5834 | return; |
| 5835 | |
| 5836 | task_event = (struct perf_task_event){ |
| 5837 | .task = task, |
| 5838 | .task_ctx = task_ctx, |
| 5839 | .event_id = { |
| 5840 | .header = { |
| 5841 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 5842 | .misc = 0, |
| 5843 | .size = sizeof(task_event.event_id), |
| 5844 | }, |
| 5845 | /* .pid */ |
| 5846 | /* .ppid */ |
| 5847 | /* .tid */ |
| 5848 | /* .ptid */ |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 5849 | /* .time */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5850 | }, |
| 5851 | }; |
| 5852 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5853 | perf_event_aux(perf_event_task_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5854 | &task_event, |
| 5855 | task_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | void perf_event_fork(struct task_struct *task) |
| 5859 | { |
| 5860 | perf_event_task(task, NULL, 1); |
| 5861 | } |
| 5862 | |
| 5863 | /* |
| 5864 | * comm tracking |
| 5865 | */ |
| 5866 | |
| 5867 | struct perf_comm_event { |
| 5868 | struct task_struct *task; |
| 5869 | char *comm; |
| 5870 | int comm_size; |
| 5871 | |
| 5872 | struct { |
| 5873 | struct perf_event_header header; |
| 5874 | |
| 5875 | u32 pid; |
| 5876 | u32 tid; |
| 5877 | } event_id; |
| 5878 | }; |
| 5879 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5880 | static int perf_event_comm_match(struct perf_event *event) |
| 5881 | { |
| 5882 | return event->attr.comm; |
| 5883 | } |
| 5884 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5885 | static void perf_event_comm_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5886 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5887 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5888 | struct perf_comm_event *comm_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5889 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5890 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5891 | int size = comm_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5892 | int ret; |
| 5893 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5894 | if (!perf_event_comm_match(event)) |
| 5895 | return; |
| 5896 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5897 | perf_event_header__init_id(&comm_event->event_id.header, &sample, event); |
| 5898 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 5899 | comm_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5900 | |
| 5901 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5902 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5903 | |
| 5904 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 5905 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 5906 | |
| 5907 | perf_output_put(&handle, comm_event->event_id); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 5908 | __output_copy(&handle, comm_event->comm, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5909 | comm_event->comm_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5910 | |
| 5911 | perf_event__output_id_sample(event, &handle, &sample); |
| 5912 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5913 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 5914 | out: |
| 5915 | comm_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5916 | } |
| 5917 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5918 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 5919 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5920 | char comm[TASK_COMM_LEN]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5921 | unsigned int size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5922 | |
| 5923 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 5924 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5925 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 5926 | |
| 5927 | comm_event->comm = comm; |
| 5928 | comm_event->comm_size = size; |
| 5929 | |
| 5930 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 5931 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5932 | perf_event_aux(perf_event_comm_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5933 | comm_event, |
| 5934 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5935 | } |
| 5936 | |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5937 | void perf_event_comm(struct task_struct *task, bool exec) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5938 | { |
| 5939 | struct perf_comm_event comm_event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5940 | |
| 5941 | if (!atomic_read(&nr_comm_events)) |
| 5942 | return; |
| 5943 | |
| 5944 | comm_event = (struct perf_comm_event){ |
| 5945 | .task = task, |
| 5946 | /* .comm */ |
| 5947 | /* .comm_size */ |
| 5948 | .event_id = { |
| 5949 | .header = { |
| 5950 | .type = PERF_RECORD_COMM, |
Adrian Hunter | 82b8977 | 2014-05-28 11:45:04 +0300 | [diff] [blame] | 5951 | .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5952 | /* .size */ |
| 5953 | }, |
| 5954 | /* .pid */ |
| 5955 | /* .tid */ |
| 5956 | }, |
| 5957 | }; |
| 5958 | |
| 5959 | perf_event_comm_event(&comm_event); |
| 5960 | } |
| 5961 | |
| 5962 | /* |
| 5963 | * mmap tracking |
| 5964 | */ |
| 5965 | |
| 5966 | struct perf_mmap_event { |
| 5967 | struct vm_area_struct *vma; |
| 5968 | |
| 5969 | const char *file_name; |
| 5970 | int file_size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5971 | int maj, min; |
| 5972 | u64 ino; |
| 5973 | u64 ino_generation; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 5974 | u32 prot, flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5975 | |
| 5976 | struct { |
| 5977 | struct perf_event_header header; |
| 5978 | |
| 5979 | u32 pid; |
| 5980 | u32 tid; |
| 5981 | u64 start; |
| 5982 | u64 len; |
| 5983 | u64 pgoff; |
| 5984 | } event_id; |
| 5985 | }; |
| 5986 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5987 | static int perf_event_mmap_match(struct perf_event *event, |
| 5988 | void *data) |
| 5989 | { |
| 5990 | struct perf_mmap_event *mmap_event = data; |
| 5991 | struct vm_area_struct *vma = mmap_event->vma; |
| 5992 | int executable = vma->vm_flags & VM_EXEC; |
| 5993 | |
| 5994 | return (!executable && event->attr.mmap_data) || |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 5995 | (executable && (event->attr.mmap || event->attr.mmap2)); |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 5996 | } |
| 5997 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5998 | static void perf_event_mmap_output(struct perf_event *event, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 5999 | void *data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6000 | { |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6001 | struct perf_mmap_event *mmap_event = data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6002 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6003 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6004 | int size = mmap_event->event_id.header.size; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6005 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6006 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6007 | if (!perf_event_mmap_match(event, data)) |
| 6008 | return; |
| 6009 | |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6010 | if (event->attr.mmap2) { |
| 6011 | mmap_event->event_id.header.type = PERF_RECORD_MMAP2; |
| 6012 | mmap_event->event_id.header.size += sizeof(mmap_event->maj); |
| 6013 | mmap_event->event_id.header.size += sizeof(mmap_event->min); |
| 6014 | mmap_event->event_id.header.size += sizeof(mmap_event->ino); |
Arnaldo Carvalho de Melo | d008d52 | 2013-09-10 10:24:05 -0300 | [diff] [blame] | 6015 | mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6016 | mmap_event->event_id.header.size += sizeof(mmap_event->prot); |
| 6017 | mmap_event->event_id.header.size += sizeof(mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6018 | } |
| 6019 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6020 | perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); |
| 6021 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6022 | mmap_event->event_id.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6023 | if (ret) |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6024 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6025 | |
| 6026 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 6027 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 6028 | |
| 6029 | perf_output_put(&handle, mmap_event->event_id); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6030 | |
| 6031 | if (event->attr.mmap2) { |
| 6032 | perf_output_put(&handle, mmap_event->maj); |
| 6033 | perf_output_put(&handle, mmap_event->min); |
| 6034 | perf_output_put(&handle, mmap_event->ino); |
| 6035 | perf_output_put(&handle, mmap_event->ino_generation); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6036 | perf_output_put(&handle, mmap_event->prot); |
| 6037 | perf_output_put(&handle, mmap_event->flags); |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6038 | } |
| 6039 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 6040 | __output_copy(&handle, mmap_event->file_name, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6041 | mmap_event->file_size); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6042 | |
| 6043 | perf_event__output_id_sample(event, &handle, &sample); |
| 6044 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6045 | perf_output_end(&handle); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6046 | out: |
| 6047 | mmap_event->event_id.header.size = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6048 | } |
| 6049 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6050 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 6051 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6052 | struct vm_area_struct *vma = mmap_event->vma; |
| 6053 | struct file *file = vma->vm_file; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6054 | int maj = 0, min = 0; |
| 6055 | u64 ino = 0, gen = 0; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6056 | u32 prot = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6057 | unsigned int size; |
| 6058 | char tmp[16]; |
| 6059 | char *buf = NULL; |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6060 | char *name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6061 | |
| 6062 | if (file) { |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6063 | struct inode *inode; |
| 6064 | dev_t dev; |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 6065 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6066 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6067 | if (!buf) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6068 | name = "//enomem"; |
| 6069 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6070 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6071 | /* |
Oleg Nesterov | 3ea2f2b | 2013-10-16 22:10:04 +0200 | [diff] [blame] | 6072 | * d_path() works from the end of the rb backwards, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6073 | * need to add enough zero bytes after the string to handle |
| 6074 | * the 64bit alignment we do later. |
| 6075 | */ |
Miklos Szeredi | 9bf39ab | 2015-06-19 10:29:13 +0200 | [diff] [blame] | 6076 | name = file_path(file, buf, PATH_MAX - sizeof(u64)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6077 | if (IS_ERR(name)) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6078 | name = "//toolong"; |
| 6079 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6080 | } |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6081 | inode = file_inode(vma->vm_file); |
| 6082 | dev = inode->i_sb->s_dev; |
| 6083 | ino = inode->i_ino; |
| 6084 | gen = inode->i_generation; |
| 6085 | maj = MAJOR(dev); |
| 6086 | min = MINOR(dev); |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6087 | |
| 6088 | if (vma->vm_flags & VM_READ) |
| 6089 | prot |= PROT_READ; |
| 6090 | if (vma->vm_flags & VM_WRITE) |
| 6091 | prot |= PROT_WRITE; |
| 6092 | if (vma->vm_flags & VM_EXEC) |
| 6093 | prot |= PROT_EXEC; |
| 6094 | |
| 6095 | if (vma->vm_flags & VM_MAYSHARE) |
| 6096 | flags = MAP_SHARED; |
| 6097 | else |
| 6098 | flags = MAP_PRIVATE; |
| 6099 | |
| 6100 | if (vma->vm_flags & VM_DENYWRITE) |
| 6101 | flags |= MAP_DENYWRITE; |
| 6102 | if (vma->vm_flags & VM_MAYEXEC) |
| 6103 | flags |= MAP_EXECUTABLE; |
| 6104 | if (vma->vm_flags & VM_LOCKED) |
| 6105 | flags |= MAP_LOCKED; |
| 6106 | if (vma->vm_flags & VM_HUGETLB) |
| 6107 | flags |= MAP_HUGETLB; |
| 6108 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6109 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6110 | } else { |
Jiri Olsa | fbe26ab | 2014-07-14 17:57:19 +0200 | [diff] [blame] | 6111 | if (vma->vm_ops && vma->vm_ops->name) { |
| 6112 | name = (char *) vma->vm_ops->name(vma); |
| 6113 | if (name) |
| 6114 | goto cpy_name; |
| 6115 | } |
| 6116 | |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6117 | name = (char *)arch_vma_name(vma); |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6118 | if (name) |
| 6119 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6120 | |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 6121 | if (vma->vm_start <= vma->vm_mm->start_brk && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6122 | vma->vm_end >= vma->vm_mm->brk) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6123 | name = "[heap]"; |
| 6124 | goto cpy_name; |
Oleg Nesterov | 32c5fb7 | 2013-10-16 22:09:45 +0200 | [diff] [blame] | 6125 | } |
| 6126 | if (vma->vm_start <= vma->vm_mm->start_stack && |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6127 | vma->vm_end >= vma->vm_mm->start_stack) { |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6128 | name = "[stack]"; |
| 6129 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6130 | } |
| 6131 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6132 | name = "//anon"; |
| 6133 | goto cpy_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6134 | } |
| 6135 | |
Oleg Nesterov | c7e548b | 2013-10-17 20:24:17 +0200 | [diff] [blame] | 6136 | cpy_name: |
| 6137 | strlcpy(tmp, name, sizeof(tmp)); |
| 6138 | name = tmp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6139 | got_name: |
Peter Zijlstra | 2c42cfbf | 2013-10-17 00:06:46 +0200 | [diff] [blame] | 6140 | /* |
| 6141 | * Since our buffer works in 8 byte units we need to align our string |
| 6142 | * size to a multiple of 8. However, we must guarantee the tail end is |
| 6143 | * zero'd out to avoid leaking random bits to userspace. |
| 6144 | */ |
| 6145 | size = strlen(name)+1; |
| 6146 | while (!IS_ALIGNED(size, sizeof(u64))) |
| 6147 | name[size++] = '\0'; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6148 | |
| 6149 | mmap_event->file_name = name; |
| 6150 | mmap_event->file_size = size; |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6151 | mmap_event->maj = maj; |
| 6152 | mmap_event->min = min; |
| 6153 | mmap_event->ino = ino; |
| 6154 | mmap_event->ino_generation = gen; |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6155 | mmap_event->prot = prot; |
| 6156 | mmap_event->flags = flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6157 | |
Stephane Eranian | 2fe8542 | 2013-01-24 16:10:39 +0100 | [diff] [blame] | 6158 | if (!(vma->vm_flags & VM_EXEC)) |
| 6159 | mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 6160 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6161 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 6162 | |
Jiri Olsa | 6751684 | 2013-07-09 18:56:31 +0200 | [diff] [blame] | 6163 | perf_event_aux(perf_event_mmap_output, |
Jiri Olsa | 52d857a | 2013-05-06 18:27:18 +0200 | [diff] [blame] | 6164 | mmap_event, |
| 6165 | NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6166 | |
| 6167 | kfree(buf); |
| 6168 | } |
| 6169 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 6170 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6171 | { |
| 6172 | struct perf_mmap_event mmap_event; |
| 6173 | |
| 6174 | if (!atomic_read(&nr_mmap_events)) |
| 6175 | return; |
| 6176 | |
| 6177 | mmap_event = (struct perf_mmap_event){ |
| 6178 | .vma = vma, |
| 6179 | /* .file_name */ |
| 6180 | /* .file_size */ |
| 6181 | .event_id = { |
| 6182 | .header = { |
| 6183 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 6184 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6185 | /* .size */ |
| 6186 | }, |
| 6187 | /* .pid */ |
| 6188 | /* .tid */ |
| 6189 | .start = vma->vm_start, |
| 6190 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 6191 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6192 | }, |
Stephane Eranian | 13d7a24 | 2013-08-21 12:10:24 +0200 | [diff] [blame] | 6193 | /* .maj (attr_mmap2 only) */ |
| 6194 | /* .min (attr_mmap2 only) */ |
| 6195 | /* .ino (attr_mmap2 only) */ |
| 6196 | /* .ino_generation (attr_mmap2 only) */ |
Peter Zijlstra | f972eb6 | 2014-05-19 15:13:47 -0400 | [diff] [blame] | 6197 | /* .prot (attr_mmap2 only) */ |
| 6198 | /* .flags (attr_mmap2 only) */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6199 | }; |
| 6200 | |
| 6201 | perf_event_mmap_event(&mmap_event); |
| 6202 | } |
| 6203 | |
Alexander Shishkin | 68db7e9 | 2015-01-14 14:18:15 +0200 | [diff] [blame] | 6204 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
| 6205 | unsigned long size, u64 flags) |
| 6206 | { |
| 6207 | struct perf_output_handle handle; |
| 6208 | struct perf_sample_data sample; |
| 6209 | struct perf_aux_event { |
| 6210 | struct perf_event_header header; |
| 6211 | u64 offset; |
| 6212 | u64 size; |
| 6213 | u64 flags; |
| 6214 | } rec = { |
| 6215 | .header = { |
| 6216 | .type = PERF_RECORD_AUX, |
| 6217 | .misc = 0, |
| 6218 | .size = sizeof(rec), |
| 6219 | }, |
| 6220 | .offset = head, |
| 6221 | .size = size, |
| 6222 | .flags = flags, |
| 6223 | }; |
| 6224 | int ret; |
| 6225 | |
| 6226 | perf_event_header__init_id(&rec.header, &sample, event); |
| 6227 | ret = perf_output_begin(&handle, event, rec.header.size); |
| 6228 | |
| 6229 | if (ret) |
| 6230 | return; |
| 6231 | |
| 6232 | perf_output_put(&handle, rec); |
| 6233 | perf_event__output_id_sample(event, &handle, &sample); |
| 6234 | |
| 6235 | perf_output_end(&handle); |
| 6236 | } |
| 6237 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6238 | /* |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 6239 | * Lost/dropped samples logging |
| 6240 | */ |
| 6241 | void perf_log_lost_samples(struct perf_event *event, u64 lost) |
| 6242 | { |
| 6243 | struct perf_output_handle handle; |
| 6244 | struct perf_sample_data sample; |
| 6245 | int ret; |
| 6246 | |
| 6247 | struct { |
| 6248 | struct perf_event_header header; |
| 6249 | u64 lost; |
| 6250 | } lost_samples_event = { |
| 6251 | .header = { |
| 6252 | .type = PERF_RECORD_LOST_SAMPLES, |
| 6253 | .misc = 0, |
| 6254 | .size = sizeof(lost_samples_event), |
| 6255 | }, |
| 6256 | .lost = lost, |
| 6257 | }; |
| 6258 | |
| 6259 | perf_event_header__init_id(&lost_samples_event.header, &sample, event); |
| 6260 | |
| 6261 | ret = perf_output_begin(&handle, event, |
| 6262 | lost_samples_event.header.size); |
| 6263 | if (ret) |
| 6264 | return; |
| 6265 | |
| 6266 | perf_output_put(&handle, lost_samples_event); |
| 6267 | perf_event__output_id_sample(event, &handle, &sample); |
| 6268 | perf_output_end(&handle); |
| 6269 | } |
| 6270 | |
| 6271 | /* |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 6272 | * context_switch tracking |
| 6273 | */ |
| 6274 | |
| 6275 | struct perf_switch_event { |
| 6276 | struct task_struct *task; |
| 6277 | struct task_struct *next_prev; |
| 6278 | |
| 6279 | struct { |
| 6280 | struct perf_event_header header; |
| 6281 | u32 next_prev_pid; |
| 6282 | u32 next_prev_tid; |
| 6283 | } event_id; |
| 6284 | }; |
| 6285 | |
| 6286 | static int perf_event_switch_match(struct perf_event *event) |
| 6287 | { |
| 6288 | return event->attr.context_switch; |
| 6289 | } |
| 6290 | |
| 6291 | static void perf_event_switch_output(struct perf_event *event, void *data) |
| 6292 | { |
| 6293 | struct perf_switch_event *se = data; |
| 6294 | struct perf_output_handle handle; |
| 6295 | struct perf_sample_data sample; |
| 6296 | int ret; |
| 6297 | |
| 6298 | if (!perf_event_switch_match(event)) |
| 6299 | return; |
| 6300 | |
| 6301 | /* Only CPU-wide events are allowed to see next/prev pid/tid */ |
| 6302 | if (event->ctx->task) { |
| 6303 | se->event_id.header.type = PERF_RECORD_SWITCH; |
| 6304 | se->event_id.header.size = sizeof(se->event_id.header); |
| 6305 | } else { |
| 6306 | se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; |
| 6307 | se->event_id.header.size = sizeof(se->event_id); |
| 6308 | se->event_id.next_prev_pid = |
| 6309 | perf_event_pid(event, se->next_prev); |
| 6310 | se->event_id.next_prev_tid = |
| 6311 | perf_event_tid(event, se->next_prev); |
| 6312 | } |
| 6313 | |
| 6314 | perf_event_header__init_id(&se->event_id.header, &sample, event); |
| 6315 | |
| 6316 | ret = perf_output_begin(&handle, event, se->event_id.header.size); |
| 6317 | if (ret) |
| 6318 | return; |
| 6319 | |
| 6320 | if (event->ctx->task) |
| 6321 | perf_output_put(&handle, se->event_id.header); |
| 6322 | else |
| 6323 | perf_output_put(&handle, se->event_id); |
| 6324 | |
| 6325 | perf_event__output_id_sample(event, &handle, &sample); |
| 6326 | |
| 6327 | perf_output_end(&handle); |
| 6328 | } |
| 6329 | |
| 6330 | static void perf_event_switch(struct task_struct *task, |
| 6331 | struct task_struct *next_prev, bool sched_in) |
| 6332 | { |
| 6333 | struct perf_switch_event switch_event; |
| 6334 | |
| 6335 | /* N.B. caller checks nr_switch_events != 0 */ |
| 6336 | |
| 6337 | switch_event = (struct perf_switch_event){ |
| 6338 | .task = task, |
| 6339 | .next_prev = next_prev, |
| 6340 | .event_id = { |
| 6341 | .header = { |
| 6342 | /* .type */ |
| 6343 | .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, |
| 6344 | /* .size */ |
| 6345 | }, |
| 6346 | /* .next_prev_pid */ |
| 6347 | /* .next_prev_tid */ |
| 6348 | }, |
| 6349 | }; |
| 6350 | |
| 6351 | perf_event_aux(perf_event_switch_output, |
| 6352 | &switch_event, |
| 6353 | NULL); |
| 6354 | } |
| 6355 | |
| 6356 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6357 | * IRQ throttle logging |
| 6358 | */ |
| 6359 | |
| 6360 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 6361 | { |
| 6362 | struct perf_output_handle handle; |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6363 | struct perf_sample_data sample; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6364 | int ret; |
| 6365 | |
| 6366 | struct { |
| 6367 | struct perf_event_header header; |
| 6368 | u64 time; |
| 6369 | u64 id; |
| 6370 | u64 stream_id; |
| 6371 | } throttle_event = { |
| 6372 | .header = { |
| 6373 | .type = PERF_RECORD_THROTTLE, |
| 6374 | .misc = 0, |
| 6375 | .size = sizeof(throttle_event), |
| 6376 | }, |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6377 | .time = perf_event_clock(event), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6378 | .id = primary_event_id(event), |
| 6379 | .stream_id = event->id, |
| 6380 | }; |
| 6381 | |
| 6382 | if (enable) |
| 6383 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 6384 | |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6385 | perf_event_header__init_id(&throttle_event.header, &sample, event); |
| 6386 | |
| 6387 | ret = perf_output_begin(&handle, event, |
Peter Zijlstra | a7ac67e | 2011-06-27 16:47:16 +0200 | [diff] [blame] | 6388 | throttle_event.header.size); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6389 | if (ret) |
| 6390 | return; |
| 6391 | |
| 6392 | perf_output_put(&handle, throttle_event); |
Arnaldo Carvalho de Melo | c980d10 | 2010-12-04 23:02:20 -0200 | [diff] [blame] | 6393 | perf_event__output_id_sample(event, &handle, &sample); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6394 | perf_output_end(&handle); |
| 6395 | } |
| 6396 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 6397 | static void perf_log_itrace_start(struct perf_event *event) |
| 6398 | { |
| 6399 | struct perf_output_handle handle; |
| 6400 | struct perf_sample_data sample; |
| 6401 | struct perf_aux_event { |
| 6402 | struct perf_event_header header; |
| 6403 | u32 pid; |
| 6404 | u32 tid; |
| 6405 | } rec; |
| 6406 | int ret; |
| 6407 | |
| 6408 | if (event->parent) |
| 6409 | event = event->parent; |
| 6410 | |
| 6411 | if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || |
| 6412 | event->hw.itrace_started) |
| 6413 | return; |
| 6414 | |
Alexander Shishkin | ec0d772 | 2015-01-14 14:18:23 +0200 | [diff] [blame] | 6415 | rec.header.type = PERF_RECORD_ITRACE_START; |
| 6416 | rec.header.misc = 0; |
| 6417 | rec.header.size = sizeof(rec); |
| 6418 | rec.pid = perf_event_pid(event, current); |
| 6419 | rec.tid = perf_event_tid(event, current); |
| 6420 | |
| 6421 | perf_event_header__init_id(&rec.header, &sample, event); |
| 6422 | ret = perf_output_begin(&handle, event, rec.header.size); |
| 6423 | |
| 6424 | if (ret) |
| 6425 | return; |
| 6426 | |
| 6427 | perf_output_put(&handle, rec); |
| 6428 | perf_event__output_id_sample(event, &handle, &sample); |
| 6429 | |
| 6430 | perf_output_end(&handle); |
| 6431 | } |
| 6432 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6433 | /* |
| 6434 | * Generic event overflow handling, sampling. |
| 6435 | */ |
| 6436 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6437 | static int __perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6438 | int throttle, struct perf_sample_data *data, |
| 6439 | struct pt_regs *regs) |
| 6440 | { |
| 6441 | int events = atomic_read(&event->event_limit); |
| 6442 | struct hw_perf_event *hwc = &event->hw; |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 6443 | u64 seq; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6444 | int ret = 0; |
| 6445 | |
Peter Zijlstra | 9639882 | 2010-11-24 18:55:29 +0100 | [diff] [blame] | 6446 | /* |
| 6447 | * Non-sampling counters might still use the PMI to fold short |
| 6448 | * hardware counters, ignore those. |
| 6449 | */ |
| 6450 | if (unlikely(!is_sampling_event(event))) |
| 6451 | return 0; |
| 6452 | |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 6453 | seq = __this_cpu_read(perf_throttled_seq); |
| 6454 | if (seq != hwc->interrupts_seq) { |
| 6455 | hwc->interrupts_seq = seq; |
| 6456 | hwc->interrupts = 1; |
| 6457 | } else { |
| 6458 | hwc->interrupts++; |
| 6459 | if (unlikely(throttle |
| 6460 | && hwc->interrupts >= max_samples_per_tick)) { |
| 6461 | __this_cpu_inc(perf_throttled_count); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 6462 | tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); |
Peter Zijlstra | 163ec43 | 2011-02-16 11:22:34 +0100 | [diff] [blame] | 6463 | hwc->interrupts = MAX_INTERRUPTS; |
| 6464 | perf_log_throttle(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6465 | ret = 1; |
| 6466 | } |
Stephane Eranian | e050e3f | 2012-01-26 17:03:19 +0100 | [diff] [blame] | 6467 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6468 | |
| 6469 | if (event->attr.freq) { |
| 6470 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 6471 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6472 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 6473 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6474 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 6475 | if (delta > 0 && delta < 2*TICK_NSEC) |
Stephane Eranian | f39d47f | 2012-02-07 14:39:57 +0100 | [diff] [blame] | 6476 | perf_adjust_period(event, delta, hwc->last_period, true); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | /* |
| 6480 | * XXX event_limit might not quite work as expected on inherited |
| 6481 | * events |
| 6482 | */ |
| 6483 | |
| 6484 | event->pending_kill = POLL_IN; |
| 6485 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 6486 | ret = 1; |
| 6487 | event->pending_kill = POLL_HUP; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6488 | event->pending_disable = 1; |
| 6489 | irq_work_queue(&event->pending); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6490 | } |
| 6491 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 6492 | if (event->overflow_handler) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6493 | event->overflow_handler(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 6494 | else |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6495 | perf_event_output(event, data, regs); |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 6496 | |
Peter Zijlstra | fed66e2cd | 2015-06-11 10:32:01 +0200 | [diff] [blame] | 6497 | if (*perf_event_fasync(event) && event->pending_kill) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6498 | event->pending_wakeup = 1; |
| 6499 | irq_work_queue(&event->pending); |
Peter Zijlstra | f506b3d | 2011-05-26 17:02:53 +0200 | [diff] [blame] | 6500 | } |
| 6501 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6502 | return ret; |
| 6503 | } |
| 6504 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6505 | int perf_event_overflow(struct perf_event *event, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6506 | struct perf_sample_data *data, |
| 6507 | struct pt_regs *regs) |
| 6508 | { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6509 | return __perf_event_overflow(event, 1, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6510 | } |
| 6511 | |
| 6512 | /* |
| 6513 | * Generic software event infrastructure |
| 6514 | */ |
| 6515 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6516 | struct swevent_htable { |
| 6517 | struct swevent_hlist *swevent_hlist; |
| 6518 | struct mutex hlist_mutex; |
| 6519 | int hlist_refcount; |
| 6520 | |
| 6521 | /* Recursion avoidance in each contexts */ |
| 6522 | int recursion[PERF_NR_CONTEXTS]; |
| 6523 | }; |
| 6524 | |
| 6525 | static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); |
| 6526 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6527 | /* |
| 6528 | * We directly increment event->count and keep a second value in |
| 6529 | * event->hw.period_left to count intervals. This period event |
| 6530 | * is kept in the range [-sample_period, 0] so that we can use the |
| 6531 | * sign as trigger. |
| 6532 | */ |
| 6533 | |
Jiri Olsa | ab57384 | 2013-05-01 17:25:44 +0200 | [diff] [blame] | 6534 | u64 perf_swevent_set_period(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6535 | { |
| 6536 | struct hw_perf_event *hwc = &event->hw; |
| 6537 | u64 period = hwc->last_period; |
| 6538 | u64 nr, offset; |
| 6539 | s64 old, val; |
| 6540 | |
| 6541 | hwc->last_period = hwc->sample_period; |
| 6542 | |
| 6543 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6544 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6545 | if (val < 0) |
| 6546 | return 0; |
| 6547 | |
| 6548 | nr = div64_u64(period + val, period); |
| 6549 | offset = nr * period; |
| 6550 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6551 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6552 | goto again; |
| 6553 | |
| 6554 | return nr; |
| 6555 | } |
| 6556 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6557 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6558 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6559 | struct pt_regs *regs) |
| 6560 | { |
| 6561 | struct hw_perf_event *hwc = &event->hw; |
| 6562 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6563 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6564 | if (!overflow) |
| 6565 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6566 | |
| 6567 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 6568 | return; |
| 6569 | |
| 6570 | for (; overflow; overflow--) { |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6571 | if (__perf_event_overflow(event, throttle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6572 | data, regs)) { |
| 6573 | /* |
| 6574 | * We inhibit the overflow from happening when |
| 6575 | * hwc->interrupts == MAX_INTERRUPTS. |
| 6576 | */ |
| 6577 | break; |
| 6578 | } |
| 6579 | throttle = 1; |
| 6580 | } |
| 6581 | } |
| 6582 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6583 | static void perf_swevent_event(struct perf_event *event, u64 nr, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6584 | struct perf_sample_data *data, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6585 | struct pt_regs *regs) |
| 6586 | { |
| 6587 | struct hw_perf_event *hwc = &event->hw; |
| 6588 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6589 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6590 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6591 | if (!regs) |
| 6592 | return; |
| 6593 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6594 | if (!is_sampling_event(event)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6595 | return; |
| 6596 | |
Andrew Vagin | 5d81e5c | 2011-11-07 15:54:12 +0300 | [diff] [blame] | 6597 | if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { |
| 6598 | data->period = nr; |
| 6599 | return perf_swevent_overflow(event, 1, data, regs); |
| 6600 | } else |
| 6601 | data->period = event->hw.last_period; |
| 6602 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6603 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6604 | return perf_swevent_overflow(event, 1, data, regs); |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6605 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 6606 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 6607 | return; |
| 6608 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6609 | perf_swevent_overflow(event, 0, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6610 | } |
| 6611 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6612 | static int perf_exclude_event(struct perf_event *event, |
| 6613 | struct pt_regs *regs) |
| 6614 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6615 | if (event->hw.state & PERF_HES_STOPPED) |
Frederic Weisbecker | 91b2f48 | 2011-03-07 21:27:08 +0100 | [diff] [blame] | 6616 | return 1; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6617 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6618 | if (regs) { |
| 6619 | if (event->attr.exclude_user && user_mode(regs)) |
| 6620 | return 1; |
| 6621 | |
| 6622 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 6623 | return 1; |
| 6624 | } |
| 6625 | |
| 6626 | return 0; |
| 6627 | } |
| 6628 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6629 | static int perf_swevent_match(struct perf_event *event, |
| 6630 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6631 | u32 event_id, |
| 6632 | struct perf_sample_data *data, |
| 6633 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6634 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6635 | if (event->attr.type != type) |
| 6636 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6637 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6638 | if (event->attr.config != event_id) |
| 6639 | return 0; |
| 6640 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 6641 | if (perf_exclude_event(event, regs)) |
| 6642 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6643 | |
| 6644 | return 1; |
| 6645 | } |
| 6646 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6647 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6648 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6649 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6650 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6651 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 6652 | } |
| 6653 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6654 | static inline struct hlist_head * |
| 6655 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6656 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6657 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6658 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6659 | return &hlist->heads[hash]; |
| 6660 | } |
| 6661 | |
| 6662 | /* For the read side: events when they trigger */ |
| 6663 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6664 | 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] | 6665 | { |
| 6666 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6667 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6668 | hlist = rcu_dereference(swhash->swevent_hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6669 | if (!hlist) |
| 6670 | return NULL; |
| 6671 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6672 | return __find_swevent_head(hlist, type, event_id); |
| 6673 | } |
| 6674 | |
| 6675 | /* For the event head insertion and removal in the hlist */ |
| 6676 | static inline struct hlist_head * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6677 | find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6678 | { |
| 6679 | struct swevent_hlist *hlist; |
| 6680 | u32 event_id = event->attr.config; |
| 6681 | u64 type = event->attr.type; |
| 6682 | |
| 6683 | /* |
| 6684 | * Event scheduling is always serialized against hlist allocation |
| 6685 | * and release. Which makes the protected version suitable here. |
| 6686 | * The context lock guarantees that. |
| 6687 | */ |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6688 | hlist = rcu_dereference_protected(swhash->swevent_hlist, |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6689 | lockdep_is_held(&event->ctx->lock)); |
| 6690 | if (!hlist) |
| 6691 | return NULL; |
| 6692 | |
| 6693 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6694 | } |
| 6695 | |
| 6696 | 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] | 6697 | u64 nr, |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6698 | struct perf_sample_data *data, |
| 6699 | struct pt_regs *regs) |
| 6700 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6701 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6702 | struct perf_event *event; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6703 | struct hlist_head *head; |
| 6704 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6705 | rcu_read_lock(); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6706 | head = find_swevent_head_rcu(swhash, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6707 | if (!head) |
| 6708 | goto end; |
| 6709 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6710 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 6711 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 6712 | perf_swevent_event(event, nr, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6713 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6714 | end: |
| 6715 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6716 | } |
| 6717 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6718 | DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); |
| 6719 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6720 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6721 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6722 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 6723 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6724 | return get_recursion_context(swhash->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6725 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 6726 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6727 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 6728 | inline void perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6729 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6730 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 6731 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6732 | put_recursion_context(swhash->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 6733 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6734 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6735 | 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] | 6736 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 6737 | struct perf_sample_data data; |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6738 | |
| 6739 | if (WARN_ON_ONCE(!regs)) |
| 6740 | return; |
| 6741 | |
| 6742 | perf_sample_data_init(&data, addr, 0); |
| 6743 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); |
| 6744 | } |
| 6745 | |
| 6746 | void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) |
| 6747 | { |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6748 | int rctx; |
| 6749 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6750 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6751 | rctx = perf_swevent_get_recursion_context(); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6752 | if (unlikely(rctx < 0)) |
| 6753 | goto fail; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6754 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6755 | ___perf_sw_event(event_id, nr, regs, addr); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 6756 | |
| 6757 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 6758 | fail: |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6759 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6760 | } |
| 6761 | |
| 6762 | static void perf_swevent_read(struct perf_event *event) |
| 6763 | { |
| 6764 | } |
| 6765 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6766 | static int perf_swevent_add(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6767 | { |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 6768 | struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6769 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6770 | struct hlist_head *head; |
| 6771 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 6772 | if (is_sampling_event(event)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6773 | hwc->last_period = hwc->sample_period; |
| 6774 | perf_swevent_set_period(event); |
| 6775 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6776 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6777 | hwc->state = !(flags & PERF_EF_START); |
| 6778 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6779 | head = find_swevent_head(swhash, event); |
Peter Zijlstra | 12ca6ad | 2015-12-15 13:49:05 +0100 | [diff] [blame] | 6780 | if (WARN_ON_ONCE(!head)) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6781 | return -EINVAL; |
| 6782 | |
| 6783 | hlist_add_head_rcu(&event->hlist_entry, head); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 6784 | perf_event_update_userpage(event); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6785 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6786 | return 0; |
| 6787 | } |
| 6788 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6789 | static void perf_swevent_del(struct perf_event *event, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6790 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6791 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6792 | } |
| 6793 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6794 | static void perf_swevent_start(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6795 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6796 | event->hw.state = 0; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6797 | } |
| 6798 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6799 | static void perf_swevent_stop(struct perf_event *event, int flags) |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6800 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6801 | event->hw.state = PERF_HES_STOPPED; |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 6802 | } |
| 6803 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6804 | /* Deref the hlist from the update side */ |
| 6805 | static inline struct swevent_hlist * |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6806 | swevent_hlist_deref(struct swevent_htable *swhash) |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6807 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6808 | return rcu_dereference_protected(swhash->swevent_hlist, |
| 6809 | lockdep_is_held(&swhash->hlist_mutex)); |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6810 | } |
| 6811 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6812 | static void swevent_hlist_release(struct swevent_htable *swhash) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6813 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6814 | struct swevent_hlist *hlist = swevent_hlist_deref(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6815 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 6816 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6817 | return; |
| 6818 | |
Andreea-Cristina Bernat | 70691d4 | 2014-08-22 16:26:05 +0300 | [diff] [blame] | 6819 | RCU_INIT_POINTER(swhash->swevent_hlist, NULL); |
Lai Jiangshan | fa4bbc4 | 2011-03-18 12:08:29 +0800 | [diff] [blame] | 6820 | kfree_rcu(hlist, rcu_head); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6821 | } |
| 6822 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6823 | static void swevent_hlist_put_cpu(int cpu) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6824 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6825 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6826 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6827 | mutex_lock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6828 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6829 | if (!--swhash->hlist_refcount) |
| 6830 | swevent_hlist_release(swhash); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6831 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6832 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6833 | } |
| 6834 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6835 | static void swevent_hlist_put(void) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6836 | { |
| 6837 | int cpu; |
| 6838 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6839 | for_each_possible_cpu(cpu) |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6840 | swevent_hlist_put_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6841 | } |
| 6842 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6843 | static int swevent_hlist_get_cpu(int cpu) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6844 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6845 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6846 | int err = 0; |
| 6847 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6848 | mutex_lock(&swhash->hlist_mutex); |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6849 | if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6850 | struct swevent_hlist *hlist; |
| 6851 | |
| 6852 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 6853 | if (!hlist) { |
| 6854 | err = -ENOMEM; |
| 6855 | goto exit; |
| 6856 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6857 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6858 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6859 | swhash->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 6860 | exit: |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 6861 | mutex_unlock(&swhash->hlist_mutex); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6862 | |
| 6863 | return err; |
| 6864 | } |
| 6865 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6866 | static int swevent_hlist_get(void) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6867 | { |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6868 | int err, cpu, failed_cpu; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6869 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6870 | get_online_cpus(); |
| 6871 | for_each_possible_cpu(cpu) { |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6872 | err = swevent_hlist_get_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6873 | if (err) { |
| 6874 | failed_cpu = cpu; |
| 6875 | goto fail; |
| 6876 | } |
| 6877 | } |
| 6878 | put_online_cpus(); |
| 6879 | |
| 6880 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 6881 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6882 | for_each_possible_cpu(cpu) { |
| 6883 | if (cpu == failed_cpu) |
| 6884 | break; |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6885 | swevent_hlist_put_cpu(cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 6886 | } |
| 6887 | |
| 6888 | put_online_cpus(); |
| 6889 | return err; |
| 6890 | } |
| 6891 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6892 | struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6893 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6894 | static void sw_perf_event_destroy(struct perf_event *event) |
| 6895 | { |
| 6896 | u64 event_id = event->attr.config; |
| 6897 | |
| 6898 | WARN_ON(event->parent); |
| 6899 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6900 | static_key_slow_dec(&perf_swevent_enabled[event_id]); |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6901 | swevent_hlist_put(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | static int perf_swevent_init(struct perf_event *event) |
| 6905 | { |
Tommi Rantala | 8176cce | 2013-04-13 22:49:14 +0300 | [diff] [blame] | 6906 | u64 event_id = event->attr.config; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6907 | |
| 6908 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 6909 | return -ENOENT; |
| 6910 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 6911 | /* |
| 6912 | * no branch sampling for software events |
| 6913 | */ |
| 6914 | if (has_branch_stack(event)) |
| 6915 | return -EOPNOTSUPP; |
| 6916 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6917 | switch (event_id) { |
| 6918 | case PERF_COUNT_SW_CPU_CLOCK: |
| 6919 | case PERF_COUNT_SW_TASK_CLOCK: |
| 6920 | return -ENOENT; |
| 6921 | |
| 6922 | default: |
| 6923 | break; |
| 6924 | } |
| 6925 | |
Dan Carpenter | ce67783 | 2010-10-24 21:50:42 +0200 | [diff] [blame] | 6926 | if (event_id >= PERF_COUNT_SW_MAX) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6927 | return -ENOENT; |
| 6928 | |
| 6929 | if (!event->parent) { |
| 6930 | int err; |
| 6931 | |
Thomas Gleixner | 3b364d7 | 2016-02-09 20:11:40 +0000 | [diff] [blame] | 6932 | err = swevent_hlist_get(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6933 | if (err) |
| 6934 | return err; |
| 6935 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 6936 | static_key_slow_inc(&perf_swevent_enabled[event_id]); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6937 | event->destroy = sw_perf_event_destroy; |
| 6938 | } |
| 6939 | |
| 6940 | return 0; |
| 6941 | } |
| 6942 | |
| 6943 | static struct pmu perf_swevent = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 6944 | .task_ctx_nr = perf_sw_context, |
| 6945 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 6946 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 6947 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6948 | .event_init = perf_swevent_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 6949 | .add = perf_swevent_add, |
| 6950 | .del = perf_swevent_del, |
| 6951 | .start = perf_swevent_start, |
| 6952 | .stop = perf_swevent_stop, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6953 | .read = perf_swevent_read, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6954 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6955 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 6956 | #ifdef CONFIG_EVENT_TRACING |
| 6957 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6958 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6959 | struct perf_sample_data *data) |
| 6960 | { |
| 6961 | void *record = data->raw->data; |
| 6962 | |
Peter Zijlstra | b71b437 | 2015-11-02 10:50:51 +0100 | [diff] [blame] | 6963 | /* only top level events have filters set */ |
| 6964 | if (event->parent) |
| 6965 | event = event->parent; |
| 6966 | |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 6967 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 6968 | return 1; |
| 6969 | return 0; |
| 6970 | } |
| 6971 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6972 | static int perf_tp_event_match(struct perf_event *event, |
| 6973 | struct perf_sample_data *data, |
| 6974 | struct pt_regs *regs) |
| 6975 | { |
Frederic Weisbecker | a0f7d0f | 2011-03-07 21:27:09 +0100 | [diff] [blame] | 6976 | if (event->hw.state & PERF_HES_STOPPED) |
| 6977 | return 0; |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 6978 | /* |
| 6979 | * All tracepoints are from kernel-space. |
| 6980 | */ |
| 6981 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6982 | return 0; |
| 6983 | |
| 6984 | if (!perf_tp_filter_match(event, data)) |
| 6985 | return 0; |
| 6986 | |
| 6987 | return 1; |
| 6988 | } |
| 6989 | |
| 6990 | 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] | 6991 | struct pt_regs *regs, struct hlist_head *head, int rctx, |
| 6992 | struct task_struct *task) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6993 | { |
| 6994 | struct perf_sample_data data; |
| 6995 | struct perf_event *event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 6996 | |
| 6997 | struct perf_raw_record raw = { |
| 6998 | .size = entry_size, |
| 6999 | .data = record, |
| 7000 | }; |
| 7001 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7002 | perf_sample_data_init(&data, addr, 0); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7003 | data.raw = &raw; |
| 7004 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7005 | hlist_for_each_entry_rcu(event, head, hlist_entry) { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7006 | if (perf_tp_event_match(event, &data, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7007 | perf_swevent_event(event, count, &data, regs); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7008 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 7009 | |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 7010 | /* |
| 7011 | * If we got specified a target task, also iterate its context and |
| 7012 | * deliver this event there too. |
| 7013 | */ |
| 7014 | if (task && task != current) { |
| 7015 | struct perf_event_context *ctx; |
| 7016 | struct trace_entry *entry = record; |
| 7017 | |
| 7018 | rcu_read_lock(); |
| 7019 | ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); |
| 7020 | if (!ctx) |
| 7021 | goto unlock; |
| 7022 | |
| 7023 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 7024 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7025 | continue; |
| 7026 | if (event->attr.config != entry->type) |
| 7027 | continue; |
| 7028 | if (perf_tp_event_match(event, &data, regs)) |
| 7029 | perf_swevent_event(event, count, &data, regs); |
| 7030 | } |
| 7031 | unlock: |
| 7032 | rcu_read_unlock(); |
| 7033 | } |
| 7034 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 7035 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7036 | } |
| 7037 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 7038 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7039 | static void tp_perf_event_destroy(struct perf_event *event) |
| 7040 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7041 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7042 | } |
| 7043 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7044 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7045 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7046 | int err; |
| 7047 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7048 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7049 | return -ENOENT; |
| 7050 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 7051 | /* |
| 7052 | * no branch sampling for tracepoint events |
| 7053 | */ |
| 7054 | if (has_branch_stack(event)) |
| 7055 | return -EOPNOTSUPP; |
| 7056 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 7057 | err = perf_trace_init(event); |
| 7058 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7059 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7060 | |
| 7061 | event->destroy = tp_perf_event_destroy; |
| 7062 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7063 | return 0; |
| 7064 | } |
| 7065 | |
| 7066 | static struct pmu perf_tracepoint = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7067 | .task_ctx_nr = perf_sw_context, |
| 7068 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7069 | .event_init = perf_tp_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7070 | .add = perf_trace_add, |
| 7071 | .del = perf_trace_del, |
| 7072 | .start = perf_swevent_start, |
| 7073 | .stop = perf_swevent_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7074 | .read = perf_swevent_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7075 | }; |
| 7076 | |
| 7077 | static inline void perf_tp_register(void) |
| 7078 | { |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7079 | perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7080 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7081 | |
| 7082 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 7083 | { |
| 7084 | char *filter_str; |
| 7085 | int ret; |
| 7086 | |
| 7087 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7088 | return -EINVAL; |
| 7089 | |
| 7090 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 7091 | if (IS_ERR(filter_str)) |
| 7092 | return PTR_ERR(filter_str); |
| 7093 | |
| 7094 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); |
| 7095 | |
| 7096 | kfree(filter_str); |
| 7097 | return ret; |
| 7098 | } |
| 7099 | |
| 7100 | static void perf_event_free_filter(struct perf_event *event) |
| 7101 | { |
| 7102 | ftrace_profile_free_filter(event); |
| 7103 | } |
| 7104 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7105 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
| 7106 | { |
| 7107 | struct bpf_prog *prog; |
| 7108 | |
| 7109 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 7110 | return -EINVAL; |
| 7111 | |
| 7112 | if (event->tp_event->prog) |
| 7113 | return -EEXIST; |
| 7114 | |
Wang Nan | 04a22fa | 2015-07-01 02:13:50 +0000 | [diff] [blame] | 7115 | if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE)) |
| 7116 | /* bpf programs can only be attached to u/kprobes */ |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7117 | return -EINVAL; |
| 7118 | |
| 7119 | prog = bpf_prog_get(prog_fd); |
| 7120 | if (IS_ERR(prog)) |
| 7121 | return PTR_ERR(prog); |
| 7122 | |
Linus Torvalds | 6c373ca | 2015-04-15 09:00:47 -0700 | [diff] [blame] | 7123 | if (prog->type != BPF_PROG_TYPE_KPROBE) { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7124 | /* valid fd, but invalid bpf program type */ |
| 7125 | bpf_prog_put(prog); |
| 7126 | return -EINVAL; |
| 7127 | } |
| 7128 | |
| 7129 | event->tp_event->prog = prog; |
| 7130 | |
| 7131 | return 0; |
| 7132 | } |
| 7133 | |
| 7134 | static void perf_event_free_bpf_prog(struct perf_event *event) |
| 7135 | { |
| 7136 | struct bpf_prog *prog; |
| 7137 | |
| 7138 | if (!event->tp_event) |
| 7139 | return; |
| 7140 | |
| 7141 | prog = event->tp_event->prog; |
| 7142 | if (prog) { |
| 7143 | event->tp_event->prog = NULL; |
| 7144 | bpf_prog_put(prog); |
| 7145 | } |
| 7146 | } |
| 7147 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7148 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7149 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7150 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7151 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7152 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 7153 | |
| 7154 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 7155 | { |
| 7156 | return -ENOENT; |
| 7157 | } |
| 7158 | |
| 7159 | static void perf_event_free_filter(struct perf_event *event) |
| 7160 | { |
| 7161 | } |
| 7162 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 7163 | static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) |
| 7164 | { |
| 7165 | return -ENOENT; |
| 7166 | } |
| 7167 | |
| 7168 | static void perf_event_free_bpf_prog(struct perf_event *event) |
| 7169 | { |
| 7170 | } |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 7171 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7172 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7173 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7174 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7175 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7176 | struct perf_sample_data sample; |
| 7177 | struct pt_regs *regs = data; |
| 7178 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7179 | perf_sample_data_init(&sample, bp->attr.bp_addr, 0); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 7180 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7181 | if (!bp->hw.state && !perf_exclude_event(bp, regs)) |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 7182 | perf_swevent_event(bp, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 7183 | } |
| 7184 | #endif |
| 7185 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7186 | /* |
| 7187 | * hrtimer based swevent callback |
| 7188 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7189 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7190 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7191 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7192 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 7193 | struct perf_sample_data data; |
| 7194 | struct pt_regs *regs; |
| 7195 | struct perf_event *event; |
| 7196 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7197 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7198 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 7199 | |
| 7200 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 7201 | return HRTIMER_NORESTART; |
| 7202 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7203 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7204 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 7205 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7206 | regs = get_irq_regs(); |
| 7207 | |
| 7208 | if (regs && !perf_exclude_event(event, regs)) { |
Paul E. McKenney | 77aeeeb | 2011-11-10 16:02:52 -0800 | [diff] [blame] | 7209 | if (!(event->attr.exclude_idle && is_idle_task(current))) |
Robert Richter | 33b07b8 | 2012-04-05 18:24:43 +0200 | [diff] [blame] | 7210 | if (__perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7211 | ret = HRTIMER_NORESTART; |
| 7212 | } |
| 7213 | |
| 7214 | period = max_t(u64, 10000, event->hw.sample_period); |
| 7215 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 7216 | |
| 7217 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7218 | } |
| 7219 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7220 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7221 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7222 | struct hw_perf_event *hwc = &event->hw; |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 7223 | s64 period; |
| 7224 | |
| 7225 | if (!is_sampling_event(event)) |
| 7226 | return; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7227 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 7228 | period = local64_read(&hwc->period_left); |
| 7229 | if (period) { |
| 7230 | if (period < 0) |
| 7231 | period = 10000; |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 7232 | |
Franck Bui-Huu | 5d508e8 | 2010-11-23 16:21:45 +0100 | [diff] [blame] | 7233 | local64_set(&hwc->period_left, 0); |
| 7234 | } else { |
| 7235 | period = max_t(u64, 10000, hwc->sample_period); |
| 7236 | } |
Thomas Gleixner | 3497d20 | 2015-04-14 21:09:03 +0000 | [diff] [blame] | 7237 | hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), |
| 7238 | HRTIMER_MODE_REL_PINNED); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7239 | } |
| 7240 | |
| 7241 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 7242 | { |
| 7243 | struct hw_perf_event *hwc = &event->hw; |
| 7244 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 7245 | if (is_sampling_event(event)) { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7246 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
Peter Zijlstra | fa407f3 | 2010-06-24 12:35:12 +0200 | [diff] [blame] | 7247 | local64_set(&hwc->period_left, ktime_to_ns(remaining)); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7248 | |
| 7249 | hrtimer_cancel(&hwc->hrtimer); |
| 7250 | } |
| 7251 | } |
| 7252 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 7253 | static void perf_swevent_init_hrtimer(struct perf_event *event) |
| 7254 | { |
| 7255 | struct hw_perf_event *hwc = &event->hw; |
| 7256 | |
| 7257 | if (!is_sampling_event(event)) |
| 7258 | return; |
| 7259 | |
| 7260 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 7261 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 7262 | |
| 7263 | /* |
| 7264 | * Since hrtimers have a fixed rate, we can do a static freq->period |
| 7265 | * mapping and avoid the whole period adjust feedback stuff. |
| 7266 | */ |
| 7267 | if (event->attr.freq) { |
| 7268 | long freq = event->attr.sample_freq; |
| 7269 | |
| 7270 | event->attr.sample_period = NSEC_PER_SEC / freq; |
| 7271 | hwc->sample_period = event->attr.sample_period; |
| 7272 | local64_set(&hwc->period_left, hwc->sample_period); |
Namhyung Kim | 778141e | 2013-03-18 11:41:46 +0900 | [diff] [blame] | 7273 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 7274 | event->attr.freq = 0; |
| 7275 | } |
| 7276 | } |
| 7277 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7278 | /* |
| 7279 | * Software event: cpu wall time clock |
| 7280 | */ |
| 7281 | |
| 7282 | static void cpu_clock_event_update(struct perf_event *event) |
| 7283 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7284 | s64 prev; |
| 7285 | u64 now; |
| 7286 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7287 | now = local_clock(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7288 | prev = local64_xchg(&event->hw.prev_count, now); |
| 7289 | local64_add(now - prev, &event->count); |
| 7290 | } |
| 7291 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7292 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7293 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7294 | local64_set(&event->hw.prev_count, local_clock()); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7295 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7296 | } |
| 7297 | |
| 7298 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 7299 | { |
| 7300 | perf_swevent_cancel_hrtimer(event); |
| 7301 | cpu_clock_event_update(event); |
| 7302 | } |
| 7303 | |
| 7304 | static int cpu_clock_event_add(struct perf_event *event, int flags) |
| 7305 | { |
| 7306 | if (flags & PERF_EF_START) |
| 7307 | cpu_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 7308 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7309 | |
| 7310 | return 0; |
| 7311 | } |
| 7312 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7313 | static void cpu_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7314 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7315 | cpu_clock_event_stop(event, flags); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7316 | } |
| 7317 | |
| 7318 | static void cpu_clock_event_read(struct perf_event *event) |
| 7319 | { |
| 7320 | cpu_clock_event_update(event); |
| 7321 | } |
| 7322 | |
| 7323 | static int cpu_clock_event_init(struct perf_event *event) |
| 7324 | { |
| 7325 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 7326 | return -ENOENT; |
| 7327 | |
| 7328 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 7329 | return -ENOENT; |
| 7330 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 7331 | /* |
| 7332 | * no branch sampling for software events |
| 7333 | */ |
| 7334 | if (has_branch_stack(event)) |
| 7335 | return -EOPNOTSUPP; |
| 7336 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 7337 | perf_swevent_init_hrtimer(event); |
| 7338 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7339 | return 0; |
| 7340 | } |
| 7341 | |
| 7342 | static struct pmu perf_cpu_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7343 | .task_ctx_nr = perf_sw_context, |
| 7344 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 7345 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 7346 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7347 | .event_init = cpu_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7348 | .add = cpu_clock_event_add, |
| 7349 | .del = cpu_clock_event_del, |
| 7350 | .start = cpu_clock_event_start, |
| 7351 | .stop = cpu_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7352 | .read = cpu_clock_event_read, |
| 7353 | }; |
| 7354 | |
| 7355 | /* |
| 7356 | * Software event: task time clock |
| 7357 | */ |
| 7358 | |
| 7359 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 7360 | { |
| 7361 | u64 prev; |
| 7362 | s64 delta; |
| 7363 | |
| 7364 | prev = local64_xchg(&event->hw.prev_count, now); |
| 7365 | delta = now - prev; |
| 7366 | local64_add(delta, &event->count); |
| 7367 | } |
| 7368 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7369 | static void task_clock_event_start(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7370 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7371 | local64_set(&event->hw.prev_count, event->ctx->time); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7372 | perf_swevent_start_hrtimer(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7373 | } |
| 7374 | |
| 7375 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 7376 | { |
| 7377 | perf_swevent_cancel_hrtimer(event); |
| 7378 | task_clock_event_update(event, event->ctx->time); |
| 7379 | } |
| 7380 | |
| 7381 | static int task_clock_event_add(struct perf_event *event, int flags) |
| 7382 | { |
| 7383 | if (flags & PERF_EF_START) |
| 7384 | task_clock_event_start(event, flags); |
Shaohua Li | 6a694a6 | 2015-02-05 15:55:32 -0800 | [diff] [blame] | 7385 | perf_event_update_userpage(event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7386 | |
| 7387 | return 0; |
| 7388 | } |
| 7389 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7390 | static void task_clock_event_del(struct perf_event *event, int flags) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7391 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7392 | task_clock_event_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7393 | } |
| 7394 | |
| 7395 | static void task_clock_event_read(struct perf_event *event) |
| 7396 | { |
Peter Zijlstra | 768a06e | 2011-02-22 16:52:24 +0100 | [diff] [blame] | 7397 | u64 now = perf_clock(); |
| 7398 | u64 delta = now - event->ctx->timestamp; |
| 7399 | u64 time = event->ctx->time + delta; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7400 | |
| 7401 | task_clock_event_update(event, time); |
| 7402 | } |
| 7403 | |
| 7404 | static int task_clock_event_init(struct perf_event *event) |
| 7405 | { |
| 7406 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 7407 | return -ENOENT; |
| 7408 | |
| 7409 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 7410 | return -ENOENT; |
| 7411 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 7412 | /* |
| 7413 | * no branch sampling for software events |
| 7414 | */ |
| 7415 | if (has_branch_stack(event)) |
| 7416 | return -EOPNOTSUPP; |
| 7417 | |
Peter Zijlstra | ba3dd36 | 2011-02-15 12:41:46 +0100 | [diff] [blame] | 7418 | perf_swevent_init_hrtimer(event); |
| 7419 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7420 | return 0; |
| 7421 | } |
| 7422 | |
| 7423 | static struct pmu perf_task_clock = { |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 7424 | .task_ctx_nr = perf_sw_context, |
| 7425 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 7426 | .capabilities = PERF_PMU_CAP_NO_NMI, |
| 7427 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7428 | .event_init = task_clock_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 7429 | .add = task_clock_event_add, |
| 7430 | .del = task_clock_event_del, |
| 7431 | .start = task_clock_event_start, |
| 7432 | .stop = task_clock_event_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7433 | .read = task_clock_event_read, |
| 7434 | }; |
| 7435 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 7436 | static void perf_pmu_nop_void(struct pmu *pmu) |
| 7437 | { |
| 7438 | } |
| 7439 | |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 7440 | static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) |
| 7441 | { |
| 7442 | } |
| 7443 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 7444 | static int perf_pmu_nop_int(struct pmu *pmu) |
| 7445 | { |
| 7446 | return 0; |
| 7447 | } |
| 7448 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 7449 | static DEFINE_PER_CPU(unsigned int, nop_txn_flags); |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 7450 | |
| 7451 | static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 7452 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 7453 | __this_cpu_write(nop_txn_flags, flags); |
| 7454 | |
| 7455 | if (flags & ~PERF_PMU_TXN_ADD) |
| 7456 | return; |
| 7457 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 7458 | perf_pmu_disable(pmu); |
| 7459 | } |
| 7460 | |
| 7461 | static int perf_pmu_commit_txn(struct pmu *pmu) |
| 7462 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 7463 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
| 7464 | |
| 7465 | __this_cpu_write(nop_txn_flags, 0); |
| 7466 | |
| 7467 | if (flags & ~PERF_PMU_TXN_ADD) |
| 7468 | return 0; |
| 7469 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 7470 | perf_pmu_enable(pmu); |
| 7471 | return 0; |
| 7472 | } |
| 7473 | |
| 7474 | static void perf_pmu_cancel_txn(struct pmu *pmu) |
| 7475 | { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 7476 | unsigned int flags = __this_cpu_read(nop_txn_flags); |
| 7477 | |
| 7478 | __this_cpu_write(nop_txn_flags, 0); |
| 7479 | |
| 7480 | if (flags & ~PERF_PMU_TXN_ADD) |
| 7481 | return; |
| 7482 | |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 7483 | perf_pmu_enable(pmu); |
| 7484 | } |
| 7485 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 7486 | static int perf_event_idx_default(struct perf_event *event) |
| 7487 | { |
Peter Zijlstra | c719f56 | 2014-10-21 11:10:21 +0200 | [diff] [blame] | 7488 | return 0; |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 7489 | } |
| 7490 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7491 | /* |
| 7492 | * Ensures all contexts with the same task_ctx_nr have the same |
| 7493 | * pmu_cpu_context too. |
| 7494 | */ |
Mark Rutland | 9e31704 | 2014-02-10 17:44:18 +0000 | [diff] [blame] | 7495 | static struct perf_cpu_context __percpu *find_pmu_context(int ctxn) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7496 | { |
| 7497 | struct pmu *pmu; |
| 7498 | |
| 7499 | if (ctxn < 0) |
| 7500 | return NULL; |
| 7501 | |
| 7502 | list_for_each_entry(pmu, &pmus, entry) { |
| 7503 | if (pmu->task_ctx_nr == ctxn) |
| 7504 | return pmu->pmu_cpu_context; |
| 7505 | } |
| 7506 | |
| 7507 | return NULL; |
| 7508 | } |
| 7509 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 7510 | static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7511 | { |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 7512 | int cpu; |
| 7513 | |
| 7514 | for_each_possible_cpu(cpu) { |
| 7515 | struct perf_cpu_context *cpuctx; |
| 7516 | |
| 7517 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 7518 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 7519 | if (cpuctx->unique_pmu == old_pmu) |
| 7520 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 7521 | } |
| 7522 | } |
| 7523 | |
| 7524 | static void free_pmu_context(struct pmu *pmu) |
| 7525 | { |
| 7526 | struct pmu *i; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7527 | |
| 7528 | mutex_lock(&pmus_lock); |
| 7529 | /* |
| 7530 | * Like a real lame refcount. |
| 7531 | */ |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 7532 | list_for_each_entry(i, &pmus, entry) { |
| 7533 | if (i->pmu_cpu_context == pmu->pmu_cpu_context) { |
| 7534 | update_pmu_context(i, pmu); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7535 | goto out; |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 7536 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7537 | } |
| 7538 | |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 7539 | free_percpu(pmu->pmu_cpu_context); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7540 | out: |
| 7541 | mutex_unlock(&pmus_lock); |
| 7542 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7543 | static struct idr pmu_idr; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7544 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7545 | static ssize_t |
| 7546 | type_show(struct device *dev, struct device_attribute *attr, char *page) |
| 7547 | { |
| 7548 | struct pmu *pmu = dev_get_drvdata(dev); |
| 7549 | |
| 7550 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); |
| 7551 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 7552 | static DEVICE_ATTR_RO(type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7553 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 7554 | static ssize_t |
| 7555 | perf_event_mux_interval_ms_show(struct device *dev, |
| 7556 | struct device_attribute *attr, |
| 7557 | char *page) |
| 7558 | { |
| 7559 | struct pmu *pmu = dev_get_drvdata(dev); |
| 7560 | |
| 7561 | return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms); |
| 7562 | } |
| 7563 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 7564 | static DEFINE_MUTEX(mux_interval_mutex); |
| 7565 | |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 7566 | static ssize_t |
| 7567 | perf_event_mux_interval_ms_store(struct device *dev, |
| 7568 | struct device_attribute *attr, |
| 7569 | const char *buf, size_t count) |
| 7570 | { |
| 7571 | struct pmu *pmu = dev_get_drvdata(dev); |
| 7572 | int timer, cpu, ret; |
| 7573 | |
| 7574 | ret = kstrtoint(buf, 0, &timer); |
| 7575 | if (ret) |
| 7576 | return ret; |
| 7577 | |
| 7578 | if (timer < 1) |
| 7579 | return -EINVAL; |
| 7580 | |
| 7581 | /* same value, noting to do */ |
| 7582 | if (timer == pmu->hrtimer_interval_ms) |
| 7583 | return count; |
| 7584 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 7585 | mutex_lock(&mux_interval_mutex); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 7586 | pmu->hrtimer_interval_ms = timer; |
| 7587 | |
| 7588 | /* update all cpuctx for this PMU */ |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 7589 | get_online_cpus(); |
| 7590 | for_each_online_cpu(cpu) { |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 7591 | struct perf_cpu_context *cpuctx; |
| 7592 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
| 7593 | cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); |
| 7594 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 7595 | cpu_function_call(cpu, |
| 7596 | (remote_function_f)perf_mux_hrtimer_restart, cpuctx); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 7597 | } |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 7598 | put_online_cpus(); |
| 7599 | mutex_unlock(&mux_interval_mutex); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 7600 | |
| 7601 | return count; |
| 7602 | } |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 7603 | static DEVICE_ATTR_RW(perf_event_mux_interval_ms); |
Stephane Eranian | 62b8563 | 2013-04-03 14:21:34 +0200 | [diff] [blame] | 7604 | |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 7605 | static struct attribute *pmu_dev_attrs[] = { |
| 7606 | &dev_attr_type.attr, |
| 7607 | &dev_attr_perf_event_mux_interval_ms.attr, |
| 7608 | NULL, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7609 | }; |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 7610 | ATTRIBUTE_GROUPS(pmu_dev); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7611 | |
| 7612 | static int pmu_bus_running; |
| 7613 | static struct bus_type pmu_bus = { |
| 7614 | .name = "event_source", |
Greg Kroah-Hartman | 90826ca | 2013-08-23 14:24:40 -0700 | [diff] [blame] | 7615 | .dev_groups = pmu_dev_groups, |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7616 | }; |
| 7617 | |
| 7618 | static void pmu_dev_release(struct device *dev) |
| 7619 | { |
| 7620 | kfree(dev); |
| 7621 | } |
| 7622 | |
| 7623 | static int pmu_dev_alloc(struct pmu *pmu) |
| 7624 | { |
| 7625 | int ret = -ENOMEM; |
| 7626 | |
| 7627 | pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 7628 | if (!pmu->dev) |
| 7629 | goto out; |
| 7630 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 7631 | pmu->dev->groups = pmu->attr_groups; |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7632 | device_initialize(pmu->dev); |
| 7633 | ret = dev_set_name(pmu->dev, "%s", pmu->name); |
| 7634 | if (ret) |
| 7635 | goto free_dev; |
| 7636 | |
| 7637 | dev_set_drvdata(pmu->dev, pmu); |
| 7638 | pmu->dev->bus = &pmu_bus; |
| 7639 | pmu->dev->release = pmu_dev_release; |
| 7640 | ret = device_add(pmu->dev); |
| 7641 | if (ret) |
| 7642 | goto free_dev; |
| 7643 | |
| 7644 | out: |
| 7645 | return ret; |
| 7646 | |
| 7647 | free_dev: |
| 7648 | put_device(pmu->dev); |
| 7649 | goto out; |
| 7650 | } |
| 7651 | |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 7652 | static struct lock_class_key cpuctx_mutex; |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 7653 | static struct lock_class_key cpuctx_lock; |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 7654 | |
Mischa Jonker | 03d8e80 | 2013-06-04 11:45:48 +0200 | [diff] [blame] | 7655 | int perf_pmu_register(struct pmu *pmu, const char *name, int type) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7656 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7657 | int cpu, ret; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 7658 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7659 | mutex_lock(&pmus_lock); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 7660 | ret = -ENOMEM; |
| 7661 | pmu->pmu_disable_count = alloc_percpu(int); |
| 7662 | if (!pmu->pmu_disable_count) |
| 7663 | goto unlock; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 7664 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7665 | pmu->type = -1; |
| 7666 | if (!name) |
| 7667 | goto skip_type; |
| 7668 | pmu->name = name; |
| 7669 | |
| 7670 | if (type < 0) { |
Tejun Heo | 0e9c3be | 2013-02-27 17:04:55 -0800 | [diff] [blame] | 7671 | type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); |
| 7672 | if (type < 0) { |
| 7673 | ret = type; |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7674 | goto free_pdc; |
| 7675 | } |
| 7676 | } |
| 7677 | pmu->type = type; |
| 7678 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7679 | if (pmu_bus_running) { |
| 7680 | ret = pmu_dev_alloc(pmu); |
| 7681 | if (ret) |
| 7682 | goto free_idr; |
| 7683 | } |
| 7684 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7685 | skip_type: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7686 | pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); |
| 7687 | if (pmu->pmu_cpu_context) |
| 7688 | goto got_cpu_context; |
| 7689 | |
Wei Yongjun | c481420 | 2013-04-12 11:05:54 +0800 | [diff] [blame] | 7690 | ret = -ENOMEM; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7691 | pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); |
| 7692 | if (!pmu->pmu_cpu_context) |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7693 | goto free_dev; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7694 | |
| 7695 | for_each_possible_cpu(cpu) { |
| 7696 | struct perf_cpu_context *cpuctx; |
| 7697 | |
| 7698 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
Peter Zijlstra | eb18447 | 2010-09-07 15:55:13 +0200 | [diff] [blame] | 7699 | __perf_event_init_context(&cpuctx->ctx); |
Peter Zijlstra | 547e9fd | 2011-01-19 12:51:39 +0100 | [diff] [blame] | 7700 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); |
Peter Zijlstra | facc430 | 2011-04-09 21:17:42 +0200 | [diff] [blame] | 7701 | lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7702 | cpuctx->ctx.pmu = pmu; |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 7703 | |
Peter Zijlstra | 272325c | 2015-04-15 11:41:58 +0200 | [diff] [blame] | 7704 | __perf_mux_hrtimer_init(cpuctx, cpu); |
Stephane Eranian | 9e63020 | 2013-04-03 14:21:33 +0200 | [diff] [blame] | 7705 | |
Peter Zijlstra | 3f1f332 | 2012-10-02 15:38:52 +0200 | [diff] [blame] | 7706 | cpuctx->unique_pmu = pmu; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7707 | } |
| 7708 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 7709 | got_cpu_context: |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 7710 | if (!pmu->start_txn) { |
| 7711 | if (pmu->pmu_enable) { |
| 7712 | /* |
| 7713 | * If we have pmu_enable/pmu_disable calls, install |
| 7714 | * transaction stubs that use that to try and batch |
| 7715 | * hardware accesses. |
| 7716 | */ |
| 7717 | pmu->start_txn = perf_pmu_start_txn; |
| 7718 | pmu->commit_txn = perf_pmu_commit_txn; |
| 7719 | pmu->cancel_txn = perf_pmu_cancel_txn; |
| 7720 | } else { |
Sukadev Bhattiprolu | fbbe070 | 2015-09-03 20:07:45 -0700 | [diff] [blame] | 7721 | pmu->start_txn = perf_pmu_nop_txn; |
Peter Zijlstra | ad5133b | 2010-06-15 12:22:39 +0200 | [diff] [blame] | 7722 | pmu->commit_txn = perf_pmu_nop_int; |
| 7723 | pmu->cancel_txn = perf_pmu_nop_void; |
| 7724 | } |
| 7725 | } |
| 7726 | |
| 7727 | if (!pmu->pmu_enable) { |
| 7728 | pmu->pmu_enable = perf_pmu_nop_void; |
| 7729 | pmu->pmu_disable = perf_pmu_nop_void; |
| 7730 | } |
| 7731 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 7732 | if (!pmu->event_idx) |
| 7733 | pmu->event_idx = perf_event_idx_default; |
| 7734 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7735 | list_add_rcu(&pmu->entry, &pmus); |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 7736 | atomic_set(&pmu->exclusive_cnt, 0); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 7737 | ret = 0; |
| 7738 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7739 | mutex_unlock(&pmus_lock); |
| 7740 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 7741 | return ret; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7742 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7743 | free_dev: |
| 7744 | device_del(pmu->dev); |
| 7745 | put_device(pmu->dev); |
| 7746 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7747 | free_idr: |
| 7748 | if (pmu->type >= PERF_TYPE_MAX) |
| 7749 | idr_remove(&pmu_idr, pmu->type); |
| 7750 | |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 7751 | free_pdc: |
| 7752 | free_percpu(pmu->pmu_disable_count); |
| 7753 | goto unlock; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7754 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 7755 | EXPORT_SYMBOL_GPL(perf_pmu_register); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7756 | |
| 7757 | void perf_pmu_unregister(struct pmu *pmu) |
| 7758 | { |
| 7759 | mutex_lock(&pmus_lock); |
| 7760 | list_del_rcu(&pmu->entry); |
| 7761 | mutex_unlock(&pmus_lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7762 | |
| 7763 | /* |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 7764 | * We dereference the pmu list under both SRCU and regular RCU, so |
| 7765 | * synchronize against both of those. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7766 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7767 | synchronize_srcu(&pmus_srcu); |
Peter Zijlstra | cde8e88 | 2010-09-13 11:06:55 +0200 | [diff] [blame] | 7768 | synchronize_rcu(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7769 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 7770 | free_percpu(pmu->pmu_disable_count); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7771 | if (pmu->type >= PERF_TYPE_MAX) |
| 7772 | idr_remove(&pmu_idr, pmu->type); |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 7773 | device_del(pmu->dev); |
| 7774 | put_device(pmu->dev); |
Peter Zijlstra | 5167695 | 2010-12-07 14:18:20 +0100 | [diff] [blame] | 7775 | free_pmu_context(pmu); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7776 | } |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 7777 | EXPORT_SYMBOL_GPL(perf_pmu_unregister); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7778 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7779 | static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) |
| 7780 | { |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 7781 | struct perf_event_context *ctx = NULL; |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7782 | int ret; |
| 7783 | |
| 7784 | if (!try_module_get(pmu->module)) |
| 7785 | return -ENODEV; |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 7786 | |
| 7787 | if (event->group_leader != event) { |
Peter Zijlstra | 8b10c5e | 2015-05-01 16:08:46 +0200 | [diff] [blame] | 7788 | /* |
| 7789 | * This ctx->mutex can nest when we're called through |
| 7790 | * inheritance. See the perf_event_ctx_lock_nested() comment. |
| 7791 | */ |
| 7792 | ctx = perf_event_ctx_lock_nested(event->group_leader, |
| 7793 | SINGLE_DEPTH_NESTING); |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 7794 | BUG_ON(!ctx); |
| 7795 | } |
| 7796 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7797 | event->pmu = pmu; |
| 7798 | ret = pmu->event_init(event); |
Peter Zijlstra | ccd41c8 | 2015-02-25 15:56:04 +0100 | [diff] [blame] | 7799 | |
| 7800 | if (ctx) |
| 7801 | perf_event_ctx_unlock(event->group_leader, ctx); |
| 7802 | |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7803 | if (ret) |
| 7804 | module_put(pmu->module); |
| 7805 | |
| 7806 | return ret; |
| 7807 | } |
| 7808 | |
Geliang Tang | 18ab2cd | 2015-09-27 23:25:50 +0800 | [diff] [blame] | 7809 | static struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7810 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 7811 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7812 | int idx; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7813 | int ret; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7814 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7815 | idx = srcu_read_lock(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7816 | |
| 7817 | rcu_read_lock(); |
| 7818 | pmu = idr_find(&pmu_idr, event->attr.type); |
| 7819 | rcu_read_unlock(); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7820 | if (pmu) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7821 | ret = perf_try_init_event(pmu, event); |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7822 | if (ret) |
| 7823 | pmu = ERR_PTR(ret); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7824 | goto unlock; |
Lin Ming | 940c5b2 | 2011-02-27 21:13:31 +0800 | [diff] [blame] | 7825 | } |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 7826 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7827 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Mark Rutland | cc34b98 | 2015-01-07 14:56:51 +0000 | [diff] [blame] | 7828 | ret = perf_try_init_event(pmu, event); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7829 | if (!ret) |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 7830 | goto unlock; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 7831 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7832 | if (ret != -ENOENT) { |
| 7833 | pmu = ERR_PTR(ret); |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 7834 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7835 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7836 | } |
Peter Zijlstra | e5f4d33 | 2010-09-10 17:38:06 +0200 | [diff] [blame] | 7837 | pmu = ERR_PTR(-ENOENT); |
| 7838 | unlock: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 7839 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7840 | |
| 7841 | return pmu; |
| 7842 | } |
| 7843 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7844 | static void account_event_cpu(struct perf_event *event, int cpu) |
| 7845 | { |
| 7846 | if (event->parent) |
| 7847 | return; |
| 7848 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7849 | if (is_cgroup_event(event)) |
| 7850 | atomic_inc(&per_cpu(perf_cgroup_events, cpu)); |
| 7851 | } |
| 7852 | |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 7853 | /* Freq events need the tick to stay alive (see perf_event_task_tick). */ |
| 7854 | static void account_freq_event_nohz(void) |
| 7855 | { |
| 7856 | #ifdef CONFIG_NO_HZ_FULL |
| 7857 | /* Lock so we don't race with concurrent unaccount */ |
| 7858 | spin_lock(&nr_freq_lock); |
| 7859 | if (atomic_inc_return(&nr_freq_events) == 1) |
| 7860 | tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); |
| 7861 | spin_unlock(&nr_freq_lock); |
| 7862 | #endif |
| 7863 | } |
| 7864 | |
| 7865 | static void account_freq_event(void) |
| 7866 | { |
| 7867 | if (tick_nohz_full_enabled()) |
| 7868 | account_freq_event_nohz(); |
| 7869 | else |
| 7870 | atomic_inc(&nr_freq_events); |
| 7871 | } |
| 7872 | |
| 7873 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7874 | static void account_event(struct perf_event *event) |
| 7875 | { |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 7876 | bool inc = false; |
| 7877 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7878 | if (event->parent) |
| 7879 | return; |
| 7880 | |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7881 | if (event->attach_state & PERF_ATTACH_TASK) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 7882 | inc = true; |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7883 | if (event->attr.mmap || event->attr.mmap_data) |
| 7884 | atomic_inc(&nr_mmap_events); |
| 7885 | if (event->attr.comm) |
| 7886 | atomic_inc(&nr_comm_events); |
| 7887 | if (event->attr.task) |
| 7888 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 555e0c1 | 2015-07-16 17:42:29 +0200 | [diff] [blame] | 7889 | if (event->attr.freq) |
| 7890 | account_freq_event(); |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 7891 | if (event->attr.context_switch) { |
| 7892 | atomic_inc(&nr_switch_events); |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 7893 | inc = true; |
Adrian Hunter | 45ac140 | 2015-07-21 12:44:02 +0300 | [diff] [blame] | 7894 | } |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7895 | if (has_branch_stack(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 7896 | inc = true; |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7897 | if (is_cgroup_event(event)) |
Peter Zijlstra | 25432ae | 2016-01-08 11:05:09 +0100 | [diff] [blame] | 7898 | inc = true; |
| 7899 | |
Peter Zijlstra | 9107c89 | 2016-02-24 18:45:45 +0100 | [diff] [blame] | 7900 | if (inc) { |
| 7901 | if (atomic_inc_not_zero(&perf_sched_count)) |
| 7902 | goto enabled; |
| 7903 | |
| 7904 | mutex_lock(&perf_sched_mutex); |
| 7905 | if (!atomic_read(&perf_sched_count)) { |
| 7906 | static_branch_enable(&perf_sched_events); |
| 7907 | /* |
| 7908 | * Guarantee that all CPUs observe they key change and |
| 7909 | * call the perf scheduling hooks before proceeding to |
| 7910 | * install events that need them. |
| 7911 | */ |
| 7912 | synchronize_sched(); |
| 7913 | } |
| 7914 | /* |
| 7915 | * Now that we have waited for the sync_sched(), allow further |
| 7916 | * increments to by-pass the mutex. |
| 7917 | */ |
| 7918 | atomic_inc(&perf_sched_count); |
| 7919 | mutex_unlock(&perf_sched_mutex); |
| 7920 | } |
| 7921 | enabled: |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7922 | |
Frederic Weisbecker | 4beb31f | 2013-07-23 02:31:02 +0200 | [diff] [blame] | 7923 | account_event_cpu(event, event->cpu); |
Frederic Weisbecker | 766d6c0 | 2013-07-23 02:31:01 +0200 | [diff] [blame] | 7924 | } |
| 7925 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7926 | /* |
| 7927 | * Allocate and initialize a event structure |
| 7928 | */ |
| 7929 | static struct perf_event * |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7930 | perf_event_alloc(struct perf_event_attr *attr, int cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7931 | struct task_struct *task, |
| 7932 | struct perf_event *group_leader, |
| 7933 | struct perf_event *parent_event, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 7934 | perf_overflow_handler_t overflow_handler, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 7935 | void *context, int cgroup_fd) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7936 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 7937 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7938 | struct perf_event *event; |
| 7939 | struct hw_perf_event *hwc; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 7940 | long err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7941 | |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 7942 | if ((unsigned)cpu >= nr_cpu_ids) { |
| 7943 | if (!task || cpu != -1) |
| 7944 | return ERR_PTR(-EINVAL); |
| 7945 | } |
| 7946 | |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 7947 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7948 | if (!event) |
| 7949 | return ERR_PTR(-ENOMEM); |
| 7950 | |
| 7951 | /* |
| 7952 | * Single events are their own group leaders, with an |
| 7953 | * empty sibling list: |
| 7954 | */ |
| 7955 | if (!group_leader) |
| 7956 | group_leader = event; |
| 7957 | |
| 7958 | mutex_init(&event->child_mutex); |
| 7959 | INIT_LIST_HEAD(&event->child_list); |
| 7960 | |
| 7961 | INIT_LIST_HEAD(&event->group_entry); |
| 7962 | INIT_LIST_HEAD(&event->event_entry); |
| 7963 | INIT_LIST_HEAD(&event->sibling_list); |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 7964 | INIT_LIST_HEAD(&event->rb_entry); |
Stephane Eranian | 71ad88e | 2013-11-12 17:58:48 +0100 | [diff] [blame] | 7965 | INIT_LIST_HEAD(&event->active_entry); |
Stephane Eranian | f3ae75d | 2014-01-08 11:15:52 +0100 | [diff] [blame] | 7966 | INIT_HLIST_NODE(&event->hlist_entry); |
| 7967 | |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 7968 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7969 | init_waitqueue_head(&event->waitq); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 7970 | init_irq_work(&event->pending, perf_pending_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7971 | |
| 7972 | mutex_init(&event->mmap_mutex); |
| 7973 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 7974 | atomic_long_set(&event->refcount, 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7975 | event->cpu = cpu; |
| 7976 | event->attr = *attr; |
| 7977 | event->group_leader = group_leader; |
| 7978 | event->pmu = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7979 | event->oncpu = -1; |
| 7980 | |
| 7981 | event->parent = parent_event; |
| 7982 | |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 7983 | event->ns = get_pid_ns(task_active_pid_ns(current)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 7984 | event->id = atomic64_inc_return(&perf_event_id); |
| 7985 | |
| 7986 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 7987 | |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7988 | if (task) { |
| 7989 | event->attach_state = PERF_ATTACH_TASK; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7990 | /* |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 7991 | * XXX pmu::event_init needs to know what task to account to |
| 7992 | * and we cannot use the ctx information because we need the |
| 7993 | * pmu before we get a ctx. |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7994 | */ |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 7995 | event->hw.target = task; |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 7996 | } |
| 7997 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 7998 | event->clock = &local_clock; |
| 7999 | if (parent_event) |
| 8000 | event->clock = parent_event->clock; |
| 8001 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8002 | if (!overflow_handler && parent_event) { |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 8003 | overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8004 | context = parent_event->overflow_handler_context; |
| 8005 | } |
Oleg Nesterov | 66832eb | 2011-01-18 17:10:32 +0100 | [diff] [blame] | 8006 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 8007 | event->overflow_handler = overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8008 | event->overflow_handler_context = context; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 8009 | |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 8010 | perf_event__state_init(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8011 | |
| 8012 | pmu = NULL; |
| 8013 | |
| 8014 | hwc = &event->hw; |
| 8015 | hwc->sample_period = attr->sample_period; |
| 8016 | if (attr->freq && attr->sample_freq) |
| 8017 | hwc->sample_period = 1; |
| 8018 | hwc->last_period = hwc->sample_period; |
| 8019 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 8020 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8021 | |
| 8022 | /* |
| 8023 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 8024 | */ |
| 8025 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8026 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8027 | |
Yan, Zheng | a46a230 | 2014-11-04 21:56:06 -0500 | [diff] [blame] | 8028 | if (!has_branch_stack(event)) |
| 8029 | event->attr.branch_sample_type = 0; |
| 8030 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8031 | if (cgroup_fd != -1) { |
| 8032 | err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); |
| 8033 | if (err) |
| 8034 | goto err_ns; |
| 8035 | } |
| 8036 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 8037 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8038 | if (!pmu) |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8039 | goto err_ns; |
| 8040 | else if (IS_ERR(pmu)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8041 | err = PTR_ERR(pmu); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8042 | goto err_ns; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8043 | } |
| 8044 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8045 | err = exclusive_event_init(event); |
| 8046 | if (err) |
| 8047 | goto err_pmu; |
| 8048 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8049 | if (!event->parent) { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 8050 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 8051 | err = get_callchain_buffers(); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8052 | if (err) |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8053 | goto err_per_task; |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 8054 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8055 | } |
| 8056 | |
Alexander Shishkin | 927a557 | 2016-03-02 13:24:14 +0200 | [diff] [blame] | 8057 | /* symmetric to unaccount_event() in _free_event() */ |
| 8058 | account_event(event); |
| 8059 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8060 | return event; |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8061 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8062 | err_per_task: |
| 8063 | exclusive_event_destroy(event); |
| 8064 | |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8065 | err_pmu: |
| 8066 | if (event->destroy) |
| 8067 | event->destroy(event); |
Yan, Zheng | c464c76 | 2014-03-18 16:56:41 +0800 | [diff] [blame] | 8068 | module_put(pmu->module); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8069 | err_ns: |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8070 | if (is_cgroup_event(event)) |
| 8071 | perf_detach_cgroup(event); |
Frederic Weisbecker | 90983b1 | 2013-07-23 02:31:00 +0200 | [diff] [blame] | 8072 | if (event->ns) |
| 8073 | put_pid_ns(event->ns); |
| 8074 | kfree(event); |
| 8075 | |
| 8076 | return ERR_PTR(err); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8077 | } |
| 8078 | |
| 8079 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 8080 | struct perf_event_attr *attr) |
| 8081 | { |
| 8082 | u32 size; |
| 8083 | int ret; |
| 8084 | |
| 8085 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 8086 | return -EFAULT; |
| 8087 | |
| 8088 | /* |
| 8089 | * zero the full structure, so that a short copy will be nice. |
| 8090 | */ |
| 8091 | memset(attr, 0, sizeof(*attr)); |
| 8092 | |
| 8093 | ret = get_user(size, &uattr->size); |
| 8094 | if (ret) |
| 8095 | return ret; |
| 8096 | |
| 8097 | if (size > PAGE_SIZE) /* silly large */ |
| 8098 | goto err_size; |
| 8099 | |
| 8100 | if (!size) /* abi compat */ |
| 8101 | size = PERF_ATTR_SIZE_VER0; |
| 8102 | |
| 8103 | if (size < PERF_ATTR_SIZE_VER0) |
| 8104 | goto err_size; |
| 8105 | |
| 8106 | /* |
| 8107 | * If we're handed a bigger struct than we know of, |
| 8108 | * ensure all the unknown bits are 0 - i.e. new |
| 8109 | * user-space does not rely on any kernel feature |
| 8110 | * extensions we dont know about yet. |
| 8111 | */ |
| 8112 | if (size > sizeof(*attr)) { |
| 8113 | unsigned char __user *addr; |
| 8114 | unsigned char __user *end; |
| 8115 | unsigned char val; |
| 8116 | |
| 8117 | addr = (void __user *)uattr + sizeof(*attr); |
| 8118 | end = (void __user *)uattr + size; |
| 8119 | |
| 8120 | for (; addr < end; addr++) { |
| 8121 | ret = get_user(val, addr); |
| 8122 | if (ret) |
| 8123 | return ret; |
| 8124 | if (val) |
| 8125 | goto err_size; |
| 8126 | } |
| 8127 | size = sizeof(*attr); |
| 8128 | } |
| 8129 | |
| 8130 | ret = copy_from_user(attr, uattr, size); |
| 8131 | if (ret) |
| 8132 | return -EFAULT; |
| 8133 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 8134 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8135 | return -EINVAL; |
| 8136 | |
| 8137 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 8138 | return -EINVAL; |
| 8139 | |
| 8140 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 8141 | return -EINVAL; |
| 8142 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 8143 | if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 8144 | u64 mask = attr->branch_sample_type; |
| 8145 | |
| 8146 | /* only using defined bits */ |
| 8147 | if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) |
| 8148 | return -EINVAL; |
| 8149 | |
| 8150 | /* at least one branch bit must be set */ |
| 8151 | if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) |
| 8152 | return -EINVAL; |
| 8153 | |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 8154 | /* propagate priv level, when not set for branch */ |
| 8155 | if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { |
| 8156 | |
| 8157 | /* exclude_kernel checked on syscall entry */ |
| 8158 | if (!attr->exclude_kernel) |
| 8159 | mask |= PERF_SAMPLE_BRANCH_KERNEL; |
| 8160 | |
| 8161 | if (!attr->exclude_user) |
| 8162 | mask |= PERF_SAMPLE_BRANCH_USER; |
| 8163 | |
| 8164 | if (!attr->exclude_hv) |
| 8165 | mask |= PERF_SAMPLE_BRANCH_HV; |
| 8166 | /* |
| 8167 | * adjust user setting (for HW filter setup) |
| 8168 | */ |
| 8169 | attr->branch_sample_type = mask; |
| 8170 | } |
Stephane Eranian | e712209 | 2013-06-06 11:02:04 +0200 | [diff] [blame] | 8171 | /* privileged levels capture (kernel, hv): check permissions */ |
| 8172 | if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) |
Stephane Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 8173 | && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 8174 | return -EACCES; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 8175 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 8176 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 8177 | if (attr->sample_type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 8178 | ret = perf_reg_validate(attr->sample_regs_user); |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 8179 | if (ret) |
| 8180 | return ret; |
| 8181 | } |
| 8182 | |
| 8183 | if (attr->sample_type & PERF_SAMPLE_STACK_USER) { |
| 8184 | if (!arch_perf_have_user_stack_dump()) |
| 8185 | return -ENOSYS; |
| 8186 | |
| 8187 | /* |
| 8188 | * We have __u32 type for the size, but so far |
| 8189 | * we can only use __u16 as maximum due to the |
| 8190 | * __u16 sample size limit. |
| 8191 | */ |
| 8192 | if (attr->sample_stack_user >= USHRT_MAX) |
| 8193 | ret = -EINVAL; |
| 8194 | else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) |
| 8195 | ret = -EINVAL; |
| 8196 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 8197 | |
Stephane Eranian | 60e2364 | 2014-09-24 13:48:37 +0200 | [diff] [blame] | 8198 | if (attr->sample_type & PERF_SAMPLE_REGS_INTR) |
| 8199 | ret = perf_reg_validate(attr->sample_regs_intr); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8200 | out: |
| 8201 | return ret; |
| 8202 | |
| 8203 | err_size: |
| 8204 | put_user(sizeof(*attr), &uattr->size); |
| 8205 | ret = -E2BIG; |
| 8206 | goto out; |
| 8207 | } |
| 8208 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8209 | static int |
| 8210 | 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] | 8211 | { |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 8212 | struct ring_buffer *rb = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8213 | int ret = -EINVAL; |
| 8214 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8215 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8216 | goto set; |
| 8217 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8218 | /* don't allow circular references */ |
| 8219 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8220 | goto out; |
| 8221 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 8222 | /* |
| 8223 | * Don't allow cross-cpu buffers |
| 8224 | */ |
| 8225 | if (output_event->cpu != event->cpu) |
| 8226 | goto out; |
| 8227 | |
| 8228 | /* |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 8229 | * 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] | 8230 | */ |
| 8231 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 8232 | goto out; |
| 8233 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8234 | /* |
| 8235 | * Mixing clocks in the same buffer is trouble you don't need. |
| 8236 | */ |
| 8237 | if (output_event->clock != event->clock) |
| 8238 | goto out; |
| 8239 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 8240 | /* |
| 8241 | * If both events generate aux data, they must be on the same PMU |
| 8242 | */ |
| 8243 | if (has_aux(event) && has_aux(output_event) && |
| 8244 | event->pmu != output_event->pmu) |
| 8245 | goto out; |
| 8246 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8247 | set: |
| 8248 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8249 | /* Can't redirect output if we've got an active mmap() */ |
| 8250 | if (atomic_read(&event->mmap_count)) |
| 8251 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8252 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8253 | if (output_event) { |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 8254 | /* get the rb we want to redirect to */ |
| 8255 | rb = ring_buffer_get(output_event); |
| 8256 | if (!rb) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8257 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8258 | } |
| 8259 | |
Peter Zijlstra | b69cf53 | 2014-03-14 10:50:33 +0100 | [diff] [blame] | 8260 | ring_buffer_attach(event, rb); |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 8261 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8262 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8263 | unlock: |
| 8264 | mutex_unlock(&event->mmap_mutex); |
| 8265 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8266 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8267 | return ret; |
| 8268 | } |
| 8269 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8270 | static void mutex_lock_double(struct mutex *a, struct mutex *b) |
| 8271 | { |
| 8272 | if (b < a) |
| 8273 | swap(a, b); |
| 8274 | |
| 8275 | mutex_lock(a); |
| 8276 | mutex_lock_nested(b, SINGLE_DEPTH_NESTING); |
| 8277 | } |
| 8278 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8279 | static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) |
| 8280 | { |
| 8281 | bool nmi_safe = false; |
| 8282 | |
| 8283 | switch (clk_id) { |
| 8284 | case CLOCK_MONOTONIC: |
| 8285 | event->clock = &ktime_get_mono_fast_ns; |
| 8286 | nmi_safe = true; |
| 8287 | break; |
| 8288 | |
| 8289 | case CLOCK_MONOTONIC_RAW: |
| 8290 | event->clock = &ktime_get_raw_fast_ns; |
| 8291 | nmi_safe = true; |
| 8292 | break; |
| 8293 | |
| 8294 | case CLOCK_REALTIME: |
| 8295 | event->clock = &ktime_get_real_ns; |
| 8296 | break; |
| 8297 | |
| 8298 | case CLOCK_BOOTTIME: |
| 8299 | event->clock = &ktime_get_boot_ns; |
| 8300 | break; |
| 8301 | |
| 8302 | case CLOCK_TAI: |
| 8303 | event->clock = &ktime_get_tai_ns; |
| 8304 | break; |
| 8305 | |
| 8306 | default: |
| 8307 | return -EINVAL; |
| 8308 | } |
| 8309 | |
| 8310 | if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) |
| 8311 | return -EINVAL; |
| 8312 | |
| 8313 | return 0; |
| 8314 | } |
| 8315 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8316 | /** |
| 8317 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 8318 | * |
| 8319 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 8320 | * @pid: target pid |
| 8321 | * @cpu: target cpu |
| 8322 | * @group_fd: group leader event fd |
| 8323 | */ |
| 8324 | SYSCALL_DEFINE5(perf_event_open, |
| 8325 | struct perf_event_attr __user *, attr_uptr, |
| 8326 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 8327 | { |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8328 | struct perf_event *group_leader = NULL, *output_event = NULL; |
| 8329 | struct perf_event *event, *sibling; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8330 | struct perf_event_attr attr; |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8331 | struct perf_event_context *ctx, *uninitialized_var(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8332 | struct file *event_file = NULL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 8333 | struct fd group = {NULL, 0}; |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 8334 | struct task_struct *task = NULL; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8335 | struct pmu *pmu; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 8336 | int event_fd; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8337 | int move_group = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8338 | int err; |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 8339 | int f_flags = O_RDWR; |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8340 | int cgroup_fd = -1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8341 | |
| 8342 | /* for future expandability... */ |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8343 | if (flags & ~PERF_FLAG_ALL) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8344 | return -EINVAL; |
| 8345 | |
| 8346 | err = perf_copy_attr(attr_uptr, &attr); |
| 8347 | if (err) |
| 8348 | return err; |
| 8349 | |
| 8350 | if (!attr.exclude_kernel) { |
| 8351 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 8352 | return -EACCES; |
| 8353 | } |
| 8354 | |
| 8355 | if (attr.freq) { |
| 8356 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 8357 | return -EINVAL; |
Peter Zijlstra | 0819b2e | 2014-05-15 20:23:48 +0200 | [diff] [blame] | 8358 | } else { |
| 8359 | if (attr.sample_period & (1ULL << 63)) |
| 8360 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8361 | } |
| 8362 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8363 | /* |
| 8364 | * In cgroup mode, the pid argument is used to pass the fd |
| 8365 | * opened to the cgroup directory in cgroupfs. The cpu argument |
| 8366 | * designates the cpu on which to monitor threads from that |
| 8367 | * cgroup. |
| 8368 | */ |
| 8369 | if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) |
| 8370 | return -EINVAL; |
| 8371 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 8372 | if (flags & PERF_FLAG_FD_CLOEXEC) |
| 8373 | f_flags |= O_CLOEXEC; |
| 8374 | |
| 8375 | event_fd = get_unused_fd_flags(f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 8376 | if (event_fd < 0) |
| 8377 | return event_fd; |
| 8378 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8379 | if (group_fd != -1) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 8380 | err = perf_fget_light(group_fd, &group); |
| 8381 | if (err) |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 8382 | goto err_fd; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 8383 | group_leader = group.file->private_data; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8384 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 8385 | output_event = group_leader; |
| 8386 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 8387 | group_leader = NULL; |
| 8388 | } |
| 8389 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 8390 | if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 8391 | task = find_lively_task_by_vpid(pid); |
| 8392 | if (IS_ERR(task)) { |
| 8393 | err = PTR_ERR(task); |
| 8394 | goto err_group_fd; |
| 8395 | } |
| 8396 | } |
| 8397 | |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 8398 | if (task && group_leader && |
| 8399 | group_leader->attr.inherit != attr.inherit) { |
| 8400 | err = -EINVAL; |
| 8401 | goto err_task; |
| 8402 | } |
| 8403 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 8404 | get_online_cpus(); |
| 8405 | |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8406 | if (flags & PERF_FLAG_PID_CGROUP) |
| 8407 | cgroup_fd = pid; |
| 8408 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8409 | event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8410 | NULL, NULL, cgroup_fd); |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 8411 | if (IS_ERR(event)) { |
| 8412 | err = PTR_ERR(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 8413 | goto err_cpus; |
Stephane Eranian | d14b12d | 2010-09-17 11:28:47 +0200 | [diff] [blame] | 8414 | } |
| 8415 | |
Vince Weaver | 53b2533 | 2014-05-16 17:12:12 -0400 | [diff] [blame] | 8416 | if (is_sampling_event(event)) { |
| 8417 | if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { |
| 8418 | err = -ENOTSUPP; |
| 8419 | goto err_alloc; |
| 8420 | } |
| 8421 | } |
| 8422 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8423 | /* |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8424 | * Special case software events and allow them to be part of |
| 8425 | * any hardware group. |
| 8426 | */ |
| 8427 | pmu = event->pmu; |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8428 | |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8429 | if (attr.use_clockid) { |
| 8430 | err = perf_event_set_clock(event, attr.clockid); |
| 8431 | if (err) |
| 8432 | goto err_alloc; |
| 8433 | } |
| 8434 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8435 | if (group_leader && |
| 8436 | (is_software_event(event) != is_software_event(group_leader))) { |
| 8437 | if (is_software_event(event)) { |
| 8438 | /* |
| 8439 | * If event and group_leader are not both a software |
| 8440 | * event, and event is, then group leader is not. |
| 8441 | * |
| 8442 | * Allow the addition of software events to !software |
| 8443 | * groups, this is safe because software events never |
| 8444 | * fail to schedule. |
| 8445 | */ |
| 8446 | pmu = group_leader->pmu; |
| 8447 | } else if (is_software_event(group_leader) && |
| 8448 | (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { |
| 8449 | /* |
| 8450 | * In case the group is a pure software group, and we |
| 8451 | * try to add a hardware event, move the whole group to |
| 8452 | * the hardware context. |
| 8453 | */ |
| 8454 | move_group = 1; |
| 8455 | } |
| 8456 | } |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8457 | |
| 8458 | /* |
| 8459 | * Get the target context (task or percpu): |
| 8460 | */ |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 8461 | ctx = find_get_context(pmu, task, event); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8462 | if (IS_ERR(ctx)) { |
| 8463 | err = PTR_ERR(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 8464 | goto err_alloc; |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8465 | } |
| 8466 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8467 | if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) { |
| 8468 | err = -EBUSY; |
| 8469 | goto err_context; |
| 8470 | } |
| 8471 | |
Peter Zijlstra | fd1edb3 | 2011-03-28 13:13:56 +0200 | [diff] [blame] | 8472 | if (task) { |
| 8473 | put_task_struct(task); |
| 8474 | task = NULL; |
| 8475 | } |
| 8476 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8477 | /* |
| 8478 | * Look up the group leader (we will attach this event to it): |
| 8479 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8480 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8481 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8482 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8483 | /* |
| 8484 | * Do not allow a recursive hierarchy (this new sibling |
| 8485 | * becoming part of another group-sibling): |
| 8486 | */ |
| 8487 | if (group_leader->group_leader != group_leader) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8488 | goto err_context; |
Peter Zijlstra | 34f4392 | 2015-02-20 14:05:38 +0100 | [diff] [blame] | 8489 | |
| 8490 | /* All events in a group should have the same clock */ |
| 8491 | if (group_leader->clock != event->clock) |
| 8492 | goto err_context; |
| 8493 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8494 | /* |
| 8495 | * Do not allow to attach to a group in a different |
| 8496 | * task or CPU context: |
| 8497 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8498 | if (move_group) { |
Peter Zijlstra | c3c87e7 | 2015-01-23 11:19:48 +0100 | [diff] [blame] | 8499 | /* |
| 8500 | * Make sure we're both on the same task, or both |
| 8501 | * per-cpu events. |
| 8502 | */ |
| 8503 | if (group_leader->ctx->task != ctx->task) |
| 8504 | goto err_context; |
| 8505 | |
| 8506 | /* |
| 8507 | * Make sure we're both events for the same CPU; |
| 8508 | * grouping events for different CPUs is broken; since |
| 8509 | * you can never concurrently schedule them anyhow. |
| 8510 | */ |
| 8511 | if (group_leader->cpu != event->cpu) |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8512 | goto err_context; |
| 8513 | } else { |
| 8514 | if (group_leader->ctx != ctx) |
| 8515 | goto err_context; |
| 8516 | } |
| 8517 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8518 | /* |
| 8519 | * Only a group leader can be exclusive or pinned |
| 8520 | */ |
| 8521 | if (attr.exclusive || attr.pinned) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8522 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8523 | } |
| 8524 | |
| 8525 | if (output_event) { |
| 8526 | err = perf_event_set_output(event, output_event); |
| 8527 | if (err) |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8528 | goto err_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 8529 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8530 | |
Yann Droneaud | a21b0b3 | 2014-01-05 21:36:33 +0100 | [diff] [blame] | 8531 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, |
| 8532 | f_flags); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 8533 | if (IS_ERR(event_file)) { |
| 8534 | err = PTR_ERR(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8535 | goto err_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 8536 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8537 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8538 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8539 | gctx = group_leader->ctx; |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 8540 | mutex_lock_double(&gctx->mutex, &ctx->mutex); |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 8541 | if (gctx->task == TASK_TOMBSTONE) { |
| 8542 | err = -ESRCH; |
| 8543 | goto err_locked; |
| 8544 | } |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 8545 | } else { |
| 8546 | mutex_lock(&ctx->mutex); |
| 8547 | } |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8548 | |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 8549 | if (ctx->task == TASK_TOMBSTONE) { |
| 8550 | err = -ESRCH; |
| 8551 | goto err_locked; |
| 8552 | } |
| 8553 | |
Peter Zijlstra | a723968 | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 8554 | if (!perf_event_validate_size(event)) { |
| 8555 | err = -E2BIG; |
| 8556 | goto err_locked; |
| 8557 | } |
| 8558 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 8559 | /* |
| 8560 | * Must be under the same ctx::mutex as perf_install_in_context(), |
| 8561 | * because we need to serialize with concurrent event creation. |
| 8562 | */ |
| 8563 | if (!exclusive_event_installable(event, ctx)) { |
| 8564 | /* exclusive and group stuff are assumed mutually exclusive */ |
| 8565 | WARN_ON_ONCE(move_group); |
| 8566 | |
| 8567 | err = -EBUSY; |
| 8568 | goto err_locked; |
| 8569 | } |
| 8570 | |
| 8571 | WARN_ON_ONCE(ctx->parent_ctx); |
| 8572 | |
| 8573 | if (move_group) { |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8574 | /* |
| 8575 | * See perf_event_ctx_lock() for comments on the details |
| 8576 | * of swizzling perf_event::ctx. |
| 8577 | */ |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 8578 | perf_remove_from_context(group_leader, 0); |
Jiri Olsa | 0231bb5 | 2013-02-01 11:23:45 +0100 | [diff] [blame] | 8579 | |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8580 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 8581 | group_entry) { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 8582 | perf_remove_from_context(sibling, 0); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8583 | put_ctx(gctx); |
| 8584 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8585 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8586 | /* |
| 8587 | * Wait for everybody to stop referencing the events through |
| 8588 | * the old lists, before installing it on new lists. |
| 8589 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8590 | synchronize_rcu(); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8591 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 8592 | /* |
| 8593 | * Install the group siblings before the group leader. |
| 8594 | * |
| 8595 | * Because a group leader will try and install the entire group |
| 8596 | * (through the sibling list, which is still in-tact), we can |
| 8597 | * end up with siblings installed in the wrong context. |
| 8598 | * |
| 8599 | * By installing siblings first we NO-OP because they're not |
| 8600 | * reachable through the group lists. |
| 8601 | */ |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8602 | list_for_each_entry(sibling, &group_leader->sibling_list, |
| 8603 | group_entry) { |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 8604 | perf_event__state_init(sibling); |
Jiri Olsa | 9fc81d8 | 2014-12-10 21:23:51 +0100 | [diff] [blame] | 8605 | perf_install_in_context(ctx, sibling, sibling->cpu); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8606 | get_ctx(ctx); |
| 8607 | } |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 8608 | |
| 8609 | /* |
| 8610 | * Removing from the context ends up with disabled |
| 8611 | * event. What we want here is event in the initial |
| 8612 | * startup state, ready to be add into new context. |
| 8613 | */ |
| 8614 | perf_event__state_init(group_leader); |
| 8615 | perf_install_in_context(ctx, group_leader, group_leader->cpu); |
| 8616 | get_ctx(ctx); |
Peter Zijlstra | b04243e | 2010-09-17 11:28:48 +0200 | [diff] [blame] | 8617 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 8618 | /* |
| 8619 | * Now that all events are installed in @ctx, nothing |
| 8620 | * references @gctx anymore, so drop the last reference we have |
| 8621 | * on it. |
| 8622 | */ |
| 8623 | put_ctx(gctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8624 | } |
| 8625 | |
Peter Zijlstra | f73e22a | 2015-09-09 20:48:22 +0200 | [diff] [blame] | 8626 | /* |
| 8627 | * Precalculate sample_data sizes; do while holding ctx::mutex such |
| 8628 | * that we're serialized against further additions and before |
| 8629 | * perf_install_in_context() which is the point the event is active and |
| 8630 | * can use these values. |
| 8631 | */ |
| 8632 | perf_event__header_size(event); |
| 8633 | perf_event__id_header_size(event); |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8634 | |
Peter Zijlstra | 78cd2c7 | 2016-01-25 14:08:45 +0100 | [diff] [blame] | 8635 | event->owner = current; |
| 8636 | |
Yan, Zheng | e2d37cd | 2012-06-15 14:31:32 +0800 | [diff] [blame] | 8637 | perf_install_in_context(ctx, event, event->cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 8638 | perf_unpin_context(ctx); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8639 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 8640 | if (move_group) |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8641 | mutex_unlock(&gctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8642 | mutex_unlock(&ctx->mutex); |
| 8643 | |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 8644 | put_online_cpus(); |
| 8645 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8646 | mutex_lock(¤t->perf_event_mutex); |
| 8647 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 8648 | mutex_unlock(¤t->perf_event_mutex); |
| 8649 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 8650 | /* |
| 8651 | * Drop the reference on the group_event after placing the |
| 8652 | * new event on the sibling_list. This ensures destruction |
| 8653 | * of the group leader will find the pointer to itself in |
| 8654 | * perf_group_detach(). |
| 8655 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 8656 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 8657 | fd_install(event_fd, event_file); |
| 8658 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8659 | |
Peter Zijlstra | f55fc2a | 2015-09-09 19:06:33 +0200 | [diff] [blame] | 8660 | err_locked: |
| 8661 | if (move_group) |
| 8662 | mutex_unlock(&gctx->mutex); |
| 8663 | mutex_unlock(&ctx->mutex); |
| 8664 | /* err_file: */ |
| 8665 | fput(event_file); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8666 | err_context: |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 8667 | perf_unpin_context(ctx); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 8668 | put_ctx(ctx); |
Peter Zijlstra | c6be5a5 | 2010-10-14 16:59:46 +0200 | [diff] [blame] | 8669 | err_alloc: |
Peter Zijlstra | 1300562 | 2016-02-24 18:45:41 +0100 | [diff] [blame] | 8670 | /* |
| 8671 | * If event_file is set, the fput() above will have called ->release() |
| 8672 | * and that will take care of freeing the event. |
| 8673 | */ |
| 8674 | if (!event_file) |
| 8675 | free_event(event); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 8676 | err_cpus: |
Yan, Zheng | fbfc623 | 2012-06-15 14:31:31 +0800 | [diff] [blame] | 8677 | put_online_cpus(); |
Peter Zijlstra | 1f4ee50 | 2014-05-06 09:59:34 +0200 | [diff] [blame] | 8678 | err_task: |
Peter Zijlstra | e7d0bc0 | 2010-10-14 16:54:51 +0200 | [diff] [blame] | 8679 | if (task) |
| 8680 | put_task_struct(task); |
Peter Zijlstra | 89a1e18 | 2010-09-07 17:34:50 +0200 | [diff] [blame] | 8681 | err_group_fd: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 8682 | fdput(group); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 8683 | err_fd: |
| 8684 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8685 | return err; |
| 8686 | } |
| 8687 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8688 | /** |
| 8689 | * perf_event_create_kernel_counter |
| 8690 | * |
| 8691 | * @attr: attributes of the counter to create |
| 8692 | * @cpu: cpu in which the counter is bound |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 8693 | * @task: task to profile (NULL for percpu) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8694 | */ |
| 8695 | struct perf_event * |
| 8696 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Matt Helsley | 38a81da | 2010-09-13 13:01:20 -0700 | [diff] [blame] | 8697 | struct task_struct *task, |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8698 | perf_overflow_handler_t overflow_handler, |
| 8699 | void *context) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8700 | { |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8701 | struct perf_event_context *ctx; |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8702 | struct perf_event *event; |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8703 | int err; |
| 8704 | |
| 8705 | /* |
| 8706 | * Get the target context (task or percpu): |
| 8707 | */ |
| 8708 | |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 8709 | event = perf_event_alloc(attr, cpu, task, NULL, NULL, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 8710 | overflow_handler, context, -1); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 8711 | if (IS_ERR(event)) { |
| 8712 | err = PTR_ERR(event); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8713 | goto err; |
| 8714 | } |
| 8715 | |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 8716 | /* Mark owner so we could distinguish it from user events. */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 8717 | event->owner = TASK_TOMBSTONE; |
Jiri Olsa | f869776 | 2014-08-01 14:33:01 +0200 | [diff] [blame] | 8718 | |
Yan, Zheng | 4af57ef | 2014-11-04 21:56:01 -0500 | [diff] [blame] | 8719 | ctx = find_get_context(event->pmu, task, event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8720 | if (IS_ERR(ctx)) { |
| 8721 | err = PTR_ERR(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8722 | goto err_free; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 8723 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8724 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8725 | WARN_ON_ONCE(ctx->parent_ctx); |
| 8726 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 8727 | if (ctx->task == TASK_TOMBSTONE) { |
| 8728 | err = -ESRCH; |
| 8729 | goto err_unlock; |
| 8730 | } |
| 8731 | |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8732 | if (!exclusive_event_installable(event, ctx)) { |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8733 | err = -EBUSY; |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 8734 | goto err_unlock; |
Alexander Shishkin | bed5b25 | 2015-01-30 12:31:06 +0200 | [diff] [blame] | 8735 | } |
| 8736 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8737 | perf_install_in_context(ctx, event, cpu); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 8738 | perf_unpin_context(ctx); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8739 | mutex_unlock(&ctx->mutex); |
| 8740 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8741 | return event; |
| 8742 | |
Peter Zijlstra | 84c4e62 | 2016-02-24 18:45:40 +0100 | [diff] [blame] | 8743 | err_unlock: |
| 8744 | mutex_unlock(&ctx->mutex); |
| 8745 | perf_unpin_context(ctx); |
| 8746 | put_ctx(ctx); |
Peter Zijlstra | c3f00c7 | 2010-08-18 14:37:15 +0200 | [diff] [blame] | 8747 | err_free: |
| 8748 | free_event(event); |
| 8749 | err: |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 8750 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 8751 | } |
| 8752 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 8753 | |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8754 | void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) |
| 8755 | { |
| 8756 | struct perf_event_context *src_ctx; |
| 8757 | struct perf_event_context *dst_ctx; |
| 8758 | struct perf_event *event, *tmp; |
| 8759 | LIST_HEAD(events); |
| 8760 | |
| 8761 | src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; |
| 8762 | dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; |
| 8763 | |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8764 | /* |
| 8765 | * See perf_event_ctx_lock() for comments on the details |
| 8766 | * of swizzling perf_event::ctx. |
| 8767 | */ |
| 8768 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8769 | list_for_each_entry_safe(event, tmp, &src_ctx->event_list, |
| 8770 | event_entry) { |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 8771 | perf_remove_from_context(event, 0); |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 8772 | unaccount_event_cpu(event, src_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8773 | put_ctx(src_ctx); |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 8774 | list_add(&event->migrate_entry, &events); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8775 | } |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8776 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 8777 | /* |
| 8778 | * Wait for the events to quiesce before re-instating them. |
| 8779 | */ |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8780 | synchronize_rcu(); |
| 8781 | |
Peter Zijlstra (Intel) | 8f95b43 | 2015-01-27 11:53:12 +0100 | [diff] [blame] | 8782 | /* |
| 8783 | * Re-instate events in 2 passes. |
| 8784 | * |
| 8785 | * Skip over group leaders and only install siblings on this first |
| 8786 | * pass, siblings will not get enabled without a leader, however a |
| 8787 | * leader will enable its siblings, even if those are still on the old |
| 8788 | * context. |
| 8789 | */ |
| 8790 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 8791 | if (event->group_leader == event) |
| 8792 | continue; |
| 8793 | |
| 8794 | list_del(&event->migrate_entry); |
| 8795 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 8796 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 8797 | account_event_cpu(event, dst_cpu); |
| 8798 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 8799 | get_ctx(dst_ctx); |
| 8800 | } |
| 8801 | |
| 8802 | /* |
| 8803 | * Once all the siblings are setup properly, install the group leaders |
| 8804 | * to make it go. |
| 8805 | */ |
Peter Zijlstra | 9886167 | 2013-10-03 16:02:23 +0200 | [diff] [blame] | 8806 | list_for_each_entry_safe(event, tmp, &events, migrate_entry) { |
| 8807 | list_del(&event->migrate_entry); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8808 | if (event->state >= PERF_EVENT_STATE_OFF) |
| 8809 | event->state = PERF_EVENT_STATE_INACTIVE; |
Frederic Weisbecker | 9a545de | 2013-07-23 02:31:03 +0200 | [diff] [blame] | 8810 | account_event_cpu(event, dst_cpu); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8811 | perf_install_in_context(dst_ctx, event, dst_cpu); |
| 8812 | get_ctx(dst_ctx); |
| 8813 | } |
| 8814 | mutex_unlock(&dst_ctx->mutex); |
Peter Zijlstra | f63a8da | 2015-01-23 12:24:14 +0100 | [diff] [blame] | 8815 | mutex_unlock(&src_ctx->mutex); |
Yan, Zheng | 0cda4c0 | 2012-06-15 14:31:33 +0800 | [diff] [blame] | 8816 | } |
| 8817 | EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); |
| 8818 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8819 | static void sync_child_event(struct perf_event *child_event, |
| 8820 | struct task_struct *child) |
| 8821 | { |
| 8822 | struct perf_event *parent_event = child_event->parent; |
| 8823 | u64 child_val; |
| 8824 | |
| 8825 | if (child_event->attr.inherit_stat) |
| 8826 | perf_event_read_event(child_event, child); |
| 8827 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 8828 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8829 | |
| 8830 | /* |
| 8831 | * Add back the child's count to the parent's count: |
| 8832 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 8833 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8834 | atomic64_add(child_event->total_time_enabled, |
| 8835 | &parent_event->child_total_time_enabled); |
| 8836 | atomic64_add(child_event->total_time_running, |
| 8837 | &parent_event->child_total_time_running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8838 | } |
| 8839 | |
| 8840 | static void |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 8841 | perf_event_exit_event(struct perf_event *child_event, |
| 8842 | struct perf_event_context *child_ctx, |
| 8843 | struct task_struct *child) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8844 | { |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 8845 | struct perf_event *parent_event = child_event->parent; |
| 8846 | |
Peter Zijlstra | 1903d50 | 2014-07-15 17:27:27 +0200 | [diff] [blame] | 8847 | /* |
| 8848 | * Do not destroy the 'original' grouping; because of the context |
| 8849 | * switch optimization the original events could've ended up in a |
| 8850 | * random child task. |
| 8851 | * |
| 8852 | * If we were to destroy the original group, all group related |
| 8853 | * operations would cease to function properly after this random |
| 8854 | * child dies. |
| 8855 | * |
| 8856 | * Do destroy all inherited groups, we don't care about those |
| 8857 | * and being thorough is better. |
| 8858 | */ |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 8859 | raw_spin_lock_irq(&child_ctx->lock); |
| 8860 | WARN_ON_ONCE(child_ctx->is_active); |
| 8861 | |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 8862 | if (parent_event) |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 8863 | perf_group_detach(child_event); |
| 8864 | list_del_event(child_event, child_ctx); |
Peter Zijlstra | a69b0ca | 2016-02-24 18:45:44 +0100 | [diff] [blame] | 8865 | child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */ |
Peter Zijlstra | 32132a3 | 2016-01-11 15:40:59 +0100 | [diff] [blame] | 8866 | raw_spin_unlock_irq(&child_ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8867 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8868 | /* |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 8869 | * Parent events are governed by their filedesc, retain them. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8870 | */ |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 8871 | if (!parent_event) { |
Jiri Olsa | 179033b | 2014-08-07 11:48:26 -0400 | [diff] [blame] | 8872 | perf_event_wakeup(child_event); |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 8873 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8874 | } |
Peter Zijlstra | 8ba289b | 2016-01-26 13:06:56 +0100 | [diff] [blame] | 8875 | /* |
| 8876 | * Child events can be cleaned up. |
| 8877 | */ |
| 8878 | |
| 8879 | sync_child_event(child_event, child); |
| 8880 | |
| 8881 | /* |
| 8882 | * Remove this event from the parent's list |
| 8883 | */ |
| 8884 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 8885 | mutex_lock(&parent_event->child_mutex); |
| 8886 | list_del_init(&child_event->child_list); |
| 8887 | mutex_unlock(&parent_event->child_mutex); |
| 8888 | |
| 8889 | /* |
| 8890 | * Kick perf_poll() for is_event_hup(). |
| 8891 | */ |
| 8892 | perf_event_wakeup(parent_event); |
| 8893 | free_event(child_event); |
| 8894 | put_event(parent_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8895 | } |
| 8896 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8897 | 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] | 8898 | { |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 8899 | struct perf_event_context *child_ctx, *clone_ctx = NULL; |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 8900 | struct perf_event *child_event, *next; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8901 | |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 8902 | WARN_ON_ONCE(child != current); |
| 8903 | |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 8904 | child_ctx = perf_pin_task_context(child, ctxn); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 8905 | if (!child_ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8906 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8907 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8908 | /* |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 8909 | * In order to reduce the amount of tricky in ctx tear-down, we hold |
| 8910 | * ctx::mutex over the entire thing. This serializes against almost |
| 8911 | * everything that wants to access the ctx. |
| 8912 | * |
| 8913 | * The exception is sys_perf_event_open() / |
| 8914 | * perf_event_create_kernel_count() which does find_get_context() |
| 8915 | * without ctx::mutex (it cannot because of the move_group double mutex |
| 8916 | * lock thing). See the comments in perf_install_in_context(). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8917 | */ |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 8918 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8919 | |
| 8920 | /* |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 8921 | * In a single ctx::lock section, de-schedule the events and detach the |
| 8922 | * context from the task such that we cannot ever get it scheduled back |
| 8923 | * in. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8924 | */ |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 8925 | raw_spin_lock_irq(&child_ctx->lock); |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 8926 | task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 8927 | |
| 8928 | /* |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 8929 | * Now that the context is inactive, destroy the task <-> ctx relation |
| 8930 | * and mark the context dead. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8931 | */ |
Peter Zijlstra | 63b6da3 | 2016-01-14 16:05:37 +0100 | [diff] [blame] | 8932 | RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL); |
| 8933 | put_ctx(child_ctx); /* cannot be last */ |
| 8934 | WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); |
| 8935 | put_task_struct(current); /* cannot be last */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8936 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 8937 | clone_ctx = unclone_ctx(child_ctx); |
Peter Zijlstra | 6a3351b | 2016-01-25 14:09:54 +0100 | [diff] [blame] | 8938 | raw_spin_unlock_irq(&child_ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8939 | |
Peter Zijlstra | 211de6e | 2014-09-30 19:23:08 +0200 | [diff] [blame] | 8940 | if (clone_ctx) |
| 8941 | put_ctx(clone_ctx); |
Peter Zijlstra | 4a1c0f2 | 2014-06-23 16:12:42 +0200 | [diff] [blame] | 8942 | |
| 8943 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8944 | * Report the task dead after unscheduling the events so that we |
| 8945 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 8946 | * get a few PERF_RECORD_READ events. |
| 8947 | */ |
| 8948 | perf_event_task(child, child_ctx, 0); |
| 8949 | |
Peter Zijlstra | ebf905f | 2014-05-29 19:00:24 +0200 | [diff] [blame] | 8950 | 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] | 8951 | perf_event_exit_event(child_event, child_ctx, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8952 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8953 | mutex_unlock(&child_ctx->mutex); |
| 8954 | |
| 8955 | put_ctx(child_ctx); |
| 8956 | } |
| 8957 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8958 | /* |
| 8959 | * When a child task exits, feed back event values to parent events. |
| 8960 | */ |
| 8961 | void perf_event_exit_task(struct task_struct *child) |
| 8962 | { |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 8963 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8964 | int ctxn; |
| 8965 | |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 8966 | mutex_lock(&child->perf_event_mutex); |
| 8967 | list_for_each_entry_safe(event, tmp, &child->perf_event_list, |
| 8968 | owner_entry) { |
| 8969 | list_del_init(&event->owner_entry); |
| 8970 | |
| 8971 | /* |
| 8972 | * Ensure the list deletion is visible before we clear |
| 8973 | * the owner, closes a race against perf_release() where |
| 8974 | * we need to serialize on the owner->perf_event_mutex. |
| 8975 | */ |
Peter Zijlstra | f47c02c | 2016-01-26 12:30:14 +0100 | [diff] [blame] | 8976 | smp_store_release(&event->owner, NULL); |
Peter Zijlstra | 8882135 | 2010-11-09 19:01:43 +0100 | [diff] [blame] | 8977 | } |
| 8978 | mutex_unlock(&child->perf_event_mutex); |
| 8979 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8980 | for_each_task_context_nr(ctxn) |
| 8981 | perf_event_exit_task_context(child, ctxn); |
Jiri Olsa | 4e93ad6 | 2015-11-04 16:00:05 +0100 | [diff] [blame] | 8982 | |
| 8983 | /* |
| 8984 | * The perf_event_exit_task_context calls perf_event_task |
| 8985 | * with child's task_ctx, which generates EXIT events for |
| 8986 | * child contexts and sets child->perf_event_ctxp[] to NULL. |
| 8987 | * At this point we need to send EXIT events to cpu contexts. |
| 8988 | */ |
| 8989 | perf_event_task(child, NULL, 0); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 8990 | } |
| 8991 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 8992 | static void perf_free_event(struct perf_event *event, |
| 8993 | struct perf_event_context *ctx) |
| 8994 | { |
| 8995 | struct perf_event *parent = event->parent; |
| 8996 | |
| 8997 | if (WARN_ON_ONCE(!parent)) |
| 8998 | return; |
| 8999 | |
| 9000 | mutex_lock(&parent->child_mutex); |
| 9001 | list_del_init(&event->child_list); |
| 9002 | mutex_unlock(&parent->child_mutex); |
| 9003 | |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 9004 | put_event(parent); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9005 | |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9006 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 9007 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9008 | list_del_event(event, ctx); |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9009 | raw_spin_unlock_irq(&ctx->lock); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9010 | free_event(event); |
| 9011 | } |
| 9012 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9013 | /* |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9014 | * Free an unexposed, unused context as created by inheritance by |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9015 | * perf_event_init_task below, used by fork() in case of fail. |
Peter Zijlstra | 652884f | 2015-01-23 11:20:10 +0100 | [diff] [blame] | 9016 | * |
| 9017 | * Not all locks are strictly required, but take them anyway to be nice and |
| 9018 | * help out with the lockdep assertions. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9019 | */ |
| 9020 | void perf_event_free_task(struct task_struct *task) |
| 9021 | { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9022 | struct perf_event_context *ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9023 | struct perf_event *event, *tmp; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9024 | int ctxn; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9025 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9026 | for_each_task_context_nr(ctxn) { |
| 9027 | ctx = task->perf_event_ctxp[ctxn]; |
| 9028 | if (!ctx) |
| 9029 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9030 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9031 | mutex_lock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9032 | again: |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9033 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 9034 | group_entry) |
| 9035 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9036 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9037 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 9038 | group_entry) |
| 9039 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9040 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9041 | if (!list_empty(&ctx->pinned_groups) || |
| 9042 | !list_empty(&ctx->flexible_groups)) |
| 9043 | goto again; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9044 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9045 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9046 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9047 | put_ctx(ctx); |
| 9048 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9049 | } |
| 9050 | |
Peter Zijlstra | 4e231c7 | 2010-09-09 21:01:59 +0200 | [diff] [blame] | 9051 | void perf_event_delayed_put(struct task_struct *task) |
| 9052 | { |
| 9053 | int ctxn; |
| 9054 | |
| 9055 | for_each_task_context_nr(ctxn) |
| 9056 | WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); |
| 9057 | } |
| 9058 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9059 | struct file *perf_event_get(unsigned int fd) |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9060 | { |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9061 | struct file *file; |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9062 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9063 | file = fget_raw(fd); |
| 9064 | if (!file) |
| 9065 | return ERR_PTR(-EBADF); |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9066 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9067 | if (file->f_op != &perf_fops) { |
| 9068 | fput(file); |
| 9069 | return ERR_PTR(-EBADF); |
| 9070 | } |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9071 | |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 9072 | return file; |
Kaixu Xia | ffe8690 | 2015-08-06 07:02:32 +0000 | [diff] [blame] | 9073 | } |
| 9074 | |
| 9075 | const struct perf_event_attr *perf_event_attrs(struct perf_event *event) |
| 9076 | { |
| 9077 | if (!event) |
| 9078 | return ERR_PTR(-EINVAL); |
| 9079 | |
| 9080 | return &event->attr; |
| 9081 | } |
| 9082 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9083 | /* |
| 9084 | * inherit a event from parent task to child task: |
| 9085 | */ |
| 9086 | static struct perf_event * |
| 9087 | inherit_event(struct perf_event *parent_event, |
| 9088 | struct task_struct *parent, |
| 9089 | struct perf_event_context *parent_ctx, |
| 9090 | struct task_struct *child, |
| 9091 | struct perf_event *group_leader, |
| 9092 | struct perf_event_context *child_ctx) |
| 9093 | { |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 9094 | enum perf_event_active_state parent_state = parent_event->state; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9095 | struct perf_event *child_event; |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 9096 | unsigned long flags; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9097 | |
| 9098 | /* |
| 9099 | * Instead of creating recursive hierarchies of events, |
| 9100 | * we link inherited events back to the original parent, |
| 9101 | * which has a filp for sure, which we use as the reference |
| 9102 | * count: |
| 9103 | */ |
| 9104 | if (parent_event->parent) |
| 9105 | parent_event = parent_event->parent; |
| 9106 | |
| 9107 | child_event = perf_event_alloc(&parent_event->attr, |
| 9108 | parent_event->cpu, |
Peter Zijlstra | d580ff8 | 2010-10-14 17:43:23 +0200 | [diff] [blame] | 9109 | child, |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9110 | group_leader, parent_event, |
Matt Fleming | 79dff51 | 2015-01-23 18:45:42 +0000 | [diff] [blame] | 9111 | NULL, NULL, -1); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9112 | if (IS_ERR(child_event)) |
| 9113 | return child_event; |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 9114 | |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 9115 | /* |
| 9116 | * is_orphaned_event() and list_add_tail(&parent_event->child_list) |
| 9117 | * must be under the same lock in order to serialize against |
| 9118 | * perf_event_release_kernel(), such that either we must observe |
| 9119 | * is_orphaned_event() or they will observe us on the child_list. |
| 9120 | */ |
| 9121 | mutex_lock(&parent_event->child_mutex); |
Jiri Olsa | fadfe7b | 2014-08-01 14:33:02 +0200 | [diff] [blame] | 9122 | if (is_orphaned_event(parent_event) || |
| 9123 | !atomic_long_inc_not_zero(&parent_event->refcount)) { |
Peter Zijlstra | c6e5b73 | 2016-01-15 16:07:41 +0200 | [diff] [blame] | 9124 | mutex_unlock(&parent_event->child_mutex); |
Al Viro | a6fa941 | 2012-08-20 14:59:25 +0100 | [diff] [blame] | 9125 | free_event(child_event); |
| 9126 | return NULL; |
| 9127 | } |
| 9128 | |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9129 | get_ctx(child_ctx); |
| 9130 | |
| 9131 | /* |
| 9132 | * Make the child state follow the state of the parent event, |
| 9133 | * not its attr.disabled bit. We hold the parent's mutex, |
| 9134 | * so we won't race with perf_event_{en, dis}able_family. |
| 9135 | */ |
Jiri Olsa | 1929def | 2014-09-12 13:18:27 +0200 | [diff] [blame] | 9136 | if (parent_state >= PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9137 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 9138 | else |
| 9139 | child_event->state = PERF_EVENT_STATE_OFF; |
| 9140 | |
| 9141 | if (parent_event->attr.freq) { |
| 9142 | u64 sample_period = parent_event->hw.sample_period; |
| 9143 | struct hw_perf_event *hwc = &child_event->hw; |
| 9144 | |
| 9145 | hwc->sample_period = sample_period; |
| 9146 | hwc->last_period = sample_period; |
| 9147 | |
| 9148 | local64_set(&hwc->period_left, sample_period); |
| 9149 | } |
| 9150 | |
| 9151 | child_event->ctx = child_ctx; |
| 9152 | child_event->overflow_handler = parent_event->overflow_handler; |
Avi Kivity | 4dc0da8 | 2011-06-29 18:42:35 +0300 | [diff] [blame] | 9153 | child_event->overflow_handler_context |
| 9154 | = parent_event->overflow_handler_context; |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9155 | |
| 9156 | /* |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 9157 | * Precalculate sample_data sizes |
| 9158 | */ |
| 9159 | perf_event__header_size(child_event); |
Arnaldo Carvalho de Melo | 6844c09 | 2010-12-03 16:36:35 -0200 | [diff] [blame] | 9160 | perf_event__id_header_size(child_event); |
Thomas Gleixner | 614b678 | 2010-12-03 16:24:32 -0200 | [diff] [blame] | 9161 | |
| 9162 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9163 | * Link it up in the child's context: |
| 9164 | */ |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 9165 | raw_spin_lock_irqsave(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9166 | add_event_to_ctx(child_event, child_ctx); |
Peter Zijlstra | cee010e | 2010-09-10 12:51:54 +0200 | [diff] [blame] | 9167 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9168 | |
| 9169 | /* |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9170 | * Link this into the parent event's child list |
| 9171 | */ |
Peter Zijlstra | 97dee4f | 2010-09-07 15:35:33 +0200 | [diff] [blame] | 9172 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 9173 | mutex_unlock(&parent_event->child_mutex); |
| 9174 | |
| 9175 | return child_event; |
| 9176 | } |
| 9177 | |
| 9178 | static int inherit_group(struct perf_event *parent_event, |
| 9179 | struct task_struct *parent, |
| 9180 | struct perf_event_context *parent_ctx, |
| 9181 | struct task_struct *child, |
| 9182 | struct perf_event_context *child_ctx) |
| 9183 | { |
| 9184 | struct perf_event *leader; |
| 9185 | struct perf_event *sub; |
| 9186 | struct perf_event *child_ctr; |
| 9187 | |
| 9188 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 9189 | child, NULL, child_ctx); |
| 9190 | if (IS_ERR(leader)) |
| 9191 | return PTR_ERR(leader); |
| 9192 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 9193 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 9194 | child, leader, child_ctx); |
| 9195 | if (IS_ERR(child_ctr)) |
| 9196 | return PTR_ERR(child_ctr); |
| 9197 | } |
| 9198 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9199 | } |
| 9200 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9201 | static int |
| 9202 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 9203 | struct perf_event_context *parent_ctx, |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9204 | struct task_struct *child, int ctxn, |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9205 | int *inherited_all) |
| 9206 | { |
| 9207 | int ret; |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9208 | struct perf_event_context *child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9209 | |
| 9210 | if (!event->attr.inherit) { |
| 9211 | *inherited_all = 0; |
| 9212 | return 0; |
| 9213 | } |
| 9214 | |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 9215 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9216 | if (!child_ctx) { |
| 9217 | /* |
| 9218 | * This is executed from the parent task context, so |
| 9219 | * inherit events that have been marked for cloning. |
| 9220 | * First allocate and initialize a context for the |
| 9221 | * child. |
| 9222 | */ |
| 9223 | |
Jiri Olsa | 734df5a | 2013-07-09 17:44:10 +0200 | [diff] [blame] | 9224 | child_ctx = alloc_perf_context(parent_ctx->pmu, child); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9225 | if (!child_ctx) |
| 9226 | return -ENOMEM; |
| 9227 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9228 | child->perf_event_ctxp[ctxn] = child_ctx; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9229 | } |
| 9230 | |
| 9231 | ret = inherit_group(event, parent, parent_ctx, |
| 9232 | child, child_ctx); |
| 9233 | |
| 9234 | if (ret) |
| 9235 | *inherited_all = 0; |
| 9236 | |
| 9237 | return ret; |
| 9238 | } |
| 9239 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9240 | /* |
| 9241 | * Initialize the perf_event context in task_struct |
| 9242 | */ |
Jiri Olsa | 985c8dc | 2014-06-24 10:20:24 +0200 | [diff] [blame] | 9243 | static int perf_event_init_context(struct task_struct *child, int ctxn) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9244 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9245 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9246 | struct perf_event_context *cloned_ctx; |
| 9247 | struct perf_event *event; |
| 9248 | struct task_struct *parent = current; |
| 9249 | int inherited_all = 1; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 9250 | unsigned long flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9251 | int ret = 0; |
| 9252 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9253 | if (likely(!parent->perf_event_ctxp[ctxn])) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9254 | return 0; |
| 9255 | |
| 9256 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9257 | * If the parent's context is a clone, pin it so it won't get |
| 9258 | * swapped under us. |
| 9259 | */ |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9260 | parent_ctx = perf_pin_task_context(parent, ctxn); |
Peter Zijlstra | ffb4ef2 | 2014-05-05 19:12:20 +0200 | [diff] [blame] | 9261 | if (!parent_ctx) |
| 9262 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9263 | |
| 9264 | /* |
| 9265 | * No need to check if parent_ctx != NULL here; since we saw |
| 9266 | * it non-NULL earlier, the only reason for it to become NULL |
| 9267 | * is if we exit, and since we're currently in the middle of |
| 9268 | * a fork we can't be exiting at the same time. |
| 9269 | */ |
| 9270 | |
| 9271 | /* |
| 9272 | * Lock the parent list. No need to lock the child - not PID |
| 9273 | * hashed yet and not running, so nobody can access it. |
| 9274 | */ |
| 9275 | mutex_lock(&parent_ctx->mutex); |
| 9276 | |
| 9277 | /* |
| 9278 | * We dont have to disable NMIs - we are only looking at |
| 9279 | * the list, not manipulating it: |
| 9280 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9281 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9282 | ret = inherit_task_group(event, parent, parent_ctx, |
| 9283 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9284 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9285 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9286 | } |
| 9287 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 9288 | /* |
| 9289 | * We can't hold ctx->lock when iterating the ->flexible_group list due |
| 9290 | * to allocations, but we need to prevent rotation because |
| 9291 | * rotate_ctx() will change the list from interrupt context. |
| 9292 | */ |
| 9293 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 9294 | parent_ctx->rotate_disable = 1; |
| 9295 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 9296 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9297 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9298 | ret = inherit_task_group(event, parent, parent_ctx, |
| 9299 | child, ctxn, &inherited_all); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9300 | if (ret) |
| 9301 | break; |
| 9302 | } |
| 9303 | |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 9304 | raw_spin_lock_irqsave(&parent_ctx->lock, flags); |
| 9305 | parent_ctx->rotate_disable = 0; |
Thomas Gleixner | dddd337 | 2010-11-24 10:05:55 +0100 | [diff] [blame] | 9306 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9307 | child_ctx = child->perf_event_ctxp[ctxn]; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 9308 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 9309 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9310 | /* |
| 9311 | * Mark the child context as a clone of the parent |
| 9312 | * context, or of whatever the parent is a clone of. |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 9313 | * |
| 9314 | * Note that if the parent is a clone, the holding of |
| 9315 | * parent_ctx->lock avoids it from being uncloned. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9316 | */ |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 9317 | cloned_ctx = parent_ctx->parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9318 | if (cloned_ctx) { |
| 9319 | child_ctx->parent_ctx = cloned_ctx; |
| 9320 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 9321 | } else { |
| 9322 | child_ctx->parent_ctx = parent_ctx; |
| 9323 | child_ctx->parent_gen = parent_ctx->generation; |
| 9324 | } |
| 9325 | get_ctx(child_ctx->parent_ctx); |
| 9326 | } |
| 9327 | |
Peter Zijlstra | c5ed514 | 2011-01-17 13:45:37 +0100 | [diff] [blame] | 9328 | raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9329 | mutex_unlock(&parent_ctx->mutex); |
| 9330 | |
| 9331 | perf_unpin_context(parent_ctx); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 9332 | put_ctx(parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9333 | |
| 9334 | return ret; |
| 9335 | } |
| 9336 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9337 | /* |
| 9338 | * Initialize the perf_event context in task_struct |
| 9339 | */ |
| 9340 | int perf_event_init_task(struct task_struct *child) |
| 9341 | { |
| 9342 | int ctxn, ret; |
| 9343 | |
Oleg Nesterov | 8550d7c | 2011-01-19 19:22:28 +0100 | [diff] [blame] | 9344 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); |
| 9345 | mutex_init(&child->perf_event_mutex); |
| 9346 | INIT_LIST_HEAD(&child->perf_event_list); |
| 9347 | |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9348 | for_each_task_context_nr(ctxn) { |
| 9349 | ret = perf_event_init_context(child, ctxn); |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 9350 | if (ret) { |
| 9351 | perf_event_free_task(child); |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9352 | return ret; |
Peter Zijlstra | 6c72e350 | 2014-10-02 16:17:02 -0700 | [diff] [blame] | 9353 | } |
Peter Zijlstra | 8dc85d547 | 2010-09-02 16:50:03 +0200 | [diff] [blame] | 9354 | } |
| 9355 | |
| 9356 | return 0; |
| 9357 | } |
| 9358 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 9359 | static void __init perf_event_init_all_cpus(void) |
| 9360 | { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 9361 | struct swevent_htable *swhash; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 9362 | int cpu; |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 9363 | |
| 9364 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 9365 | swhash = &per_cpu(swevent_htable, cpu); |
| 9366 | mutex_init(&swhash->hlist_mutex); |
Mark Rutland | 2fde4f9 | 2015-01-07 15:01:54 +0000 | [diff] [blame] | 9367 | INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu)); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 9368 | } |
| 9369 | } |
| 9370 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 9371 | static void perf_event_init_cpu(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9372 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 9373 | struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9374 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 9375 | mutex_lock(&swhash->hlist_mutex); |
Thomas Gleixner | 059fcd8 | 2016-02-09 20:11:34 +0000 | [diff] [blame] | 9376 | if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 9377 | struct swevent_hlist *hlist; |
| 9378 | |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 9379 | hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); |
| 9380 | WARN_ON(!hlist); |
| 9381 | rcu_assign_pointer(swhash->swevent_hlist, hlist); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 9382 | } |
Peter Zijlstra | b28ab83 | 2010-09-06 14:48:15 +0200 | [diff] [blame] | 9383 | mutex_unlock(&swhash->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9384 | } |
| 9385 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 9386 | #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 9387 | static void __perf_event_exit_context(void *__info) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9388 | { |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 9389 | struct perf_event_context *ctx = __info; |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 9390 | struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); |
| 9391 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9392 | |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 9393 | raw_spin_lock(&ctx->lock); |
| 9394 | list_for_each_entry(event, &ctx->event_list, event_entry) |
Peter Zijlstra | 45a0e07 | 2016-01-26 13:09:48 +0100 | [diff] [blame] | 9395 | __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); |
Peter Zijlstra | fae3fde | 2016-01-11 15:00:50 +0100 | [diff] [blame] | 9396 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9397 | } |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 9398 | |
| 9399 | static void perf_event_exit_cpu_context(int cpu) |
| 9400 | { |
| 9401 | struct perf_event_context *ctx; |
| 9402 | struct pmu *pmu; |
| 9403 | int idx; |
| 9404 | |
| 9405 | idx = srcu_read_lock(&pmus_srcu); |
| 9406 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
Peter Zijlstra | 917bdd1 | 2010-09-17 11:28:49 +0200 | [diff] [blame] | 9407 | ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 9408 | |
| 9409 | mutex_lock(&ctx->mutex); |
| 9410 | smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); |
| 9411 | mutex_unlock(&ctx->mutex); |
| 9412 | } |
| 9413 | srcu_read_unlock(&pmus_srcu, idx); |
Peter Zijlstra | 108b02c | 2010-09-06 14:32:03 +0200 | [diff] [blame] | 9414 | } |
| 9415 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9416 | static void perf_event_exit_cpu(int cpu) |
| 9417 | { |
Peter Zijlstra | e3703f8 | 2014-02-24 12:06:12 +0100 | [diff] [blame] | 9418 | perf_event_exit_cpu_context(cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9419 | } |
| 9420 | #else |
| 9421 | static inline void perf_event_exit_cpu(int cpu) { } |
| 9422 | #endif |
| 9423 | |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 9424 | static int |
| 9425 | perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) |
| 9426 | { |
| 9427 | int cpu; |
| 9428 | |
| 9429 | for_each_online_cpu(cpu) |
| 9430 | perf_event_exit_cpu(cpu); |
| 9431 | |
| 9432 | return NOTIFY_OK; |
| 9433 | } |
| 9434 | |
| 9435 | /* |
| 9436 | * Run the perf reboot notifier at the very last possible moment so that |
| 9437 | * the generic watchdog code runs as long as possible. |
| 9438 | */ |
| 9439 | static struct notifier_block perf_reboot_notifier = { |
| 9440 | .notifier_call = perf_reboot, |
| 9441 | .priority = INT_MIN, |
| 9442 | }; |
| 9443 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 9444 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9445 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 9446 | { |
| 9447 | unsigned int cpu = (long)hcpu; |
| 9448 | |
Linus Torvalds | 4536e4d | 2011-11-03 07:44:04 -0700 | [diff] [blame] | 9449 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9450 | |
| 9451 | case CPU_UP_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9452 | perf_event_init_cpu(cpu); |
| 9453 | break; |
| 9454 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9455 | case CPU_DOWN_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9456 | perf_event_exit_cpu(cpu); |
| 9457 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9458 | default: |
| 9459 | break; |
| 9460 | } |
| 9461 | |
| 9462 | return NOTIFY_OK; |
| 9463 | } |
| 9464 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9465 | void __init perf_event_init(void) |
| 9466 | { |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 9467 | int ret; |
| 9468 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 9469 | idr_init(&pmu_idr); |
| 9470 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 9471 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 9472 | init_srcu_struct(&pmus_srcu); |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 9473 | perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); |
| 9474 | perf_pmu_register(&perf_cpu_clock, NULL, -1); |
| 9475 | perf_pmu_register(&perf_task_clock, NULL, -1); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 9476 | perf_tp_register(); |
| 9477 | perf_cpu_notifier(perf_cpu_notify); |
Peter Zijlstra | c277443 | 2010-12-08 15:29:02 +0100 | [diff] [blame] | 9478 | register_reboot_notifier(&perf_reboot_notifier); |
Jason Wessel | 3c502e7 | 2010-11-04 17:33:01 -0500 | [diff] [blame] | 9479 | |
| 9480 | ret = init_hw_breakpoint(); |
| 9481 | WARN(ret, "hw_breakpoint initialization failed with: %d", ret); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 9482 | |
Jiri Olsa | b01c3a0 | 2012-03-23 15:41:20 +0100 | [diff] [blame] | 9483 | /* |
| 9484 | * Build time assertion that we keep the data_head at the intended |
| 9485 | * location. IOW, validation we got the __reserved[] size right. |
| 9486 | */ |
| 9487 | BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) |
| 9488 | != 1024); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 9489 | } |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 9490 | |
Cody P Schafer | fd979c0 | 2015-01-30 13:45:57 -0800 | [diff] [blame] | 9491 | ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, |
| 9492 | char *page) |
| 9493 | { |
| 9494 | struct perf_pmu_events_attr *pmu_attr = |
| 9495 | container_of(attr, struct perf_pmu_events_attr, attr); |
| 9496 | |
| 9497 | if (pmu_attr->event_str) |
| 9498 | return sprintf(page, "%s\n", pmu_attr->event_str); |
| 9499 | |
| 9500 | return 0; |
| 9501 | } |
Thomas Gleixner | 675965b | 2016-02-22 22:19:27 +0000 | [diff] [blame] | 9502 | EXPORT_SYMBOL_GPL(perf_event_sysfs_show); |
Cody P Schafer | fd979c0 | 2015-01-30 13:45:57 -0800 | [diff] [blame] | 9503 | |
Peter Zijlstra | abe4340 | 2010-11-17 23:17:37 +0100 | [diff] [blame] | 9504 | static int __init perf_event_sysfs_init(void) |
| 9505 | { |
| 9506 | struct pmu *pmu; |
| 9507 | int ret; |
| 9508 | |
| 9509 | mutex_lock(&pmus_lock); |
| 9510 | |
| 9511 | ret = bus_register(&pmu_bus); |
| 9512 | if (ret) |
| 9513 | goto unlock; |
| 9514 | |
| 9515 | list_for_each_entry(pmu, &pmus, entry) { |
| 9516 | if (!pmu->name || pmu->type < 0) |
| 9517 | continue; |
| 9518 | |
| 9519 | ret = pmu_dev_alloc(pmu); |
| 9520 | WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); |
| 9521 | } |
| 9522 | pmu_bus_running = 1; |
| 9523 | ret = 0; |
| 9524 | |
| 9525 | unlock: |
| 9526 | mutex_unlock(&pmus_lock); |
| 9527 | |
| 9528 | return ret; |
| 9529 | } |
| 9530 | device_initcall(perf_event_sysfs_init); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9531 | |
| 9532 | #ifdef CONFIG_CGROUP_PERF |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9533 | static struct cgroup_subsys_state * |
| 9534 | perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9535 | { |
| 9536 | struct perf_cgroup *jc; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9537 | |
Li Zefan | 1b15d05 | 2011-03-03 14:26:06 +0800 | [diff] [blame] | 9538 | jc = kzalloc(sizeof(*jc), GFP_KERNEL); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9539 | if (!jc) |
| 9540 | return ERR_PTR(-ENOMEM); |
| 9541 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9542 | jc->info = alloc_percpu(struct perf_cgroup_info); |
| 9543 | if (!jc->info) { |
| 9544 | kfree(jc); |
| 9545 | return ERR_PTR(-ENOMEM); |
| 9546 | } |
| 9547 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9548 | return &jc->css; |
| 9549 | } |
| 9550 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9551 | static void perf_cgroup_css_free(struct cgroup_subsys_state *css) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9552 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9553 | struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); |
| 9554 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9555 | free_percpu(jc->info); |
| 9556 | kfree(jc); |
| 9557 | } |
| 9558 | |
| 9559 | static int __perf_cgroup_move(void *info) |
| 9560 | { |
| 9561 | struct task_struct *task = info; |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 9562 | rcu_read_lock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9563 | perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); |
Stephane Eranian | ddaaf4e | 2015-11-12 11:00:03 +0100 | [diff] [blame] | 9564 | rcu_read_unlock(); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9565 | return 0; |
| 9566 | } |
| 9567 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 9568 | static void perf_cgroup_attach(struct cgroup_taskset *tset) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9569 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9570 | struct task_struct *task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 9571 | struct cgroup_subsys_state *css; |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9572 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 9573 | cgroup_taskset_for_each(task, css, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9574 | task_function_call(task, __perf_cgroup_move, task); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9575 | } |
| 9576 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 9577 | struct cgroup_subsys perf_event_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 9578 | .css_alloc = perf_cgroup_css_alloc, |
| 9579 | .css_free = perf_cgroup_css_free, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9580 | .attach = perf_cgroup_attach, |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 9581 | }; |
| 9582 | #endif /* CONFIG_CGROUP_PERF */ |