blob: ede107cc583601d411888bb880571e2356fb5033 [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
Peter Zijlstra90eec102015-11-16 11:08:45 +01006 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#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 Fleming39bed6c2015-01-23 18:45:40 +000037#include <linux/cgroup.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038#include <linux/perf_event.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040039#include <linux/trace_events.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050040#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020041#include <linux/mm_types.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010044#include <linux/compat.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070045#include <linux/bpf.h>
46#include <linux/filter.h>
Alexander Shishkin375637b2016-04-27 18:44:46 +030047#include <linux/namei.h>
48#include <linux/parser.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020049
Frederic Weisbecker76369132011-05-19 19:55:04 +020050#include "internal.h"
51
Ingo Molnarcdd6c482009-09-21 12:02:48 +020052#include <asm/irq_regs.h>
53
Peter Zijlstra272325c2015-04-15 11:41:58 +020054typedef int (*remote_function_f)(void *);
55
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010056struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020057 struct task_struct *p;
Peter Zijlstra272325c2015-04-15 11:41:58 +020058 remote_function_f func;
Ingo Molnare7e7ee22011-05-04 08:42:29 +020059 void *info;
60 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010061};
62
63static void remote_function(void *data)
64{
65 struct remote_function_call *tfc = data;
66 struct task_struct *p = tfc->p;
67
68 if (p) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +010069 /* -EAGAIN */
70 if (task_cpu(p) != smp_processor_id())
71 return;
72
73 /*
74 * Now that we're on right CPU with IRQs disabled, we can test
75 * if we hit the right task without races.
76 */
77
78 tfc->ret = -ESRCH; /* No such (running) process */
79 if (p != current)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010080 return;
81 }
82
83 tfc->ret = tfc->func(tfc->info);
84}
85
86/**
87 * task_function_call - call a function on the cpu on which a task runs
88 * @p: the task to evaluate
89 * @func: the function to be called
90 * @info: the function call argument
91 *
92 * Calls the function @func when the task is currently running. This might
93 * be on the current CPU, which just calls the function directly
94 *
95 * returns: @func return value, or
96 * -ESRCH - when the process isn't running
97 * -EAGAIN - when the process moved away
98 */
99static int
Peter Zijlstra272325c2015-04-15 11:41:58 +0200100task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100101{
102 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200103 .p = p,
104 .func = func,
105 .info = info,
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100106 .ret = -EAGAIN,
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100107 };
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100108 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100109
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100110 do {
111 ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1);
112 if (!ret)
113 ret = data.ret;
114 } while (ret == -EAGAIN);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100115
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100116 return ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100117}
118
119/**
120 * cpu_function_call - call a function on the cpu
121 * @func: the function to be called
122 * @info: the function call argument
123 *
124 * Calls the function @func on the remote cpu.
125 *
126 * returns: @func return value or -ENXIO when the cpu is offline
127 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200128static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100129{
130 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200131 .p = NULL,
132 .func = func,
133 .info = info,
134 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100135 };
136
137 smp_call_function_single(cpu, remote_function, &data, 1);
138
139 return data.ret;
140}
141
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100142static inline struct perf_cpu_context *
143__get_cpu_context(struct perf_event_context *ctx)
144{
145 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
146}
147
148static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
149 struct perf_event_context *ctx)
150{
151 raw_spin_lock(&cpuctx->ctx.lock);
152 if (ctx)
153 raw_spin_lock(&ctx->lock);
154}
155
156static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
157 struct perf_event_context *ctx)
158{
159 if (ctx)
160 raw_spin_unlock(&ctx->lock);
161 raw_spin_unlock(&cpuctx->ctx.lock);
162}
163
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100164#define TASK_TOMBSTONE ((void *)-1L)
165
166static bool is_kernel_event(struct perf_event *event)
167{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +0100168 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100169}
170
Peter Zijlstra39a43642016-01-11 12:46:35 +0100171/*
172 * On task ctx scheduling...
173 *
174 * When !ctx->nr_events a task context will not be scheduled. This means
175 * we can disable the scheduler hooks (for performance) without leaving
176 * pending task ctx state.
177 *
178 * This however results in two special cases:
179 *
180 * - removing the last event from a task ctx; this is relatively straight
181 * forward and is done in __perf_remove_from_context.
182 *
183 * - adding the first event to a task ctx; this is tricky because we cannot
184 * rely on ctx->is_active and therefore cannot use event_function_call().
185 * See perf_install_in_context().
186 *
Peter Zijlstra39a43642016-01-11 12:46:35 +0100187 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
188 */
189
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100190typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
191 struct perf_event_context *, void *);
192
193struct event_function_struct {
194 struct perf_event *event;
195 event_f func;
196 void *data;
197};
198
199static int event_function(void *info)
200{
201 struct event_function_struct *efs = info;
202 struct perf_event *event = efs->event;
203 struct perf_event_context *ctx = event->ctx;
204 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
205 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100206 int ret = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100207
208 WARN_ON_ONCE(!irqs_disabled());
209
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100210 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100211 /*
212 * Since we do the IPI call without holding ctx->lock things can have
213 * changed, double check we hit the task we set out to hit.
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100214 */
215 if (ctx->task) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100216 if (ctx->task != current) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100217 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100218 goto unlock;
219 }
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100220
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100221 /*
222 * We only use event_function_call() on established contexts,
223 * and event_function() is only ever called when active (or
224 * rather, we'll have bailed in task_function_call() or the
225 * above ctx->task != current test), therefore we must have
226 * ctx->is_active here.
227 */
228 WARN_ON_ONCE(!ctx->is_active);
229 /*
230 * And since we have ctx->is_active, cpuctx->task_ctx must
231 * match.
232 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100233 WARN_ON_ONCE(task_ctx != ctx);
234 } else {
235 WARN_ON_ONCE(&cpuctx->ctx != ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100236 }
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100237
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100238 efs->func(event, cpuctx, ctx, efs->data);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100239unlock:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100240 perf_ctx_unlock(cpuctx, task_ctx);
241
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100242 return ret;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100243}
244
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100245static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100246{
247 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100248 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100249 struct event_function_struct efs = {
250 .event = event,
251 .func = func,
252 .data = data,
253 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100254
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100255 if (!event->parent) {
256 /*
257 * If this is a !child event, we must hold ctx::mutex to
258 * stabilize the the event->ctx relation. See
259 * perf_event_ctx_lock().
260 */
261 lockdep_assert_held(&ctx->mutex);
262 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100263
264 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100265 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100266 return;
267 }
268
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100269 if (task == TASK_TOMBSTONE)
270 return;
271
Peter Zijlstraa0963092016-02-24 18:45:50 +0100272again:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100273 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100274 return;
275
276 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100277 /*
278 * Reload the task pointer, it might have been changed by
279 * a concurrent perf_event_context_sched_out().
280 */
281 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +0100282 if (task == TASK_TOMBSTONE) {
283 raw_spin_unlock_irq(&ctx->lock);
284 return;
Peter Zijlstra00179602015-11-30 16:26:35 +0100285 }
Peter Zijlstraa0963092016-02-24 18:45:50 +0100286 if (ctx->is_active) {
287 raw_spin_unlock_irq(&ctx->lock);
288 goto again;
289 }
290 func(event, NULL, ctx, data);
Peter Zijlstra00179602015-11-30 16:26:35 +0100291 raw_spin_unlock_irq(&ctx->lock);
292}
293
Peter Zijlstracca20942016-08-16 13:33:26 +0200294/*
295 * Similar to event_function_call() + event_function(), but hard assumes IRQs
296 * are already disabled and we're on the right CPU.
297 */
298static void event_function_local(struct perf_event *event, event_f func, void *data)
299{
300 struct perf_event_context *ctx = event->ctx;
301 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
302 struct task_struct *task = READ_ONCE(ctx->task);
303 struct perf_event_context *task_ctx = NULL;
304
305 WARN_ON_ONCE(!irqs_disabled());
306
307 if (task) {
308 if (task == TASK_TOMBSTONE)
309 return;
310
311 task_ctx = ctx;
312 }
313
314 perf_ctx_lock(cpuctx, task_ctx);
315
316 task = ctx->task;
317 if (task == TASK_TOMBSTONE)
318 goto unlock;
319
320 if (task) {
321 /*
322 * We must be either inactive or active and the right task,
323 * otherwise we're screwed, since we cannot IPI to somewhere
324 * else.
325 */
326 if (ctx->is_active) {
327 if (WARN_ON_ONCE(task != current))
328 goto unlock;
329
330 if (WARN_ON_ONCE(cpuctx->task_ctx != ctx))
331 goto unlock;
332 }
333 } else {
334 WARN_ON_ONCE(&cpuctx->ctx != ctx);
335 }
336
337 func(event, cpuctx, ctx, data);
338unlock:
339 perf_ctx_unlock(cpuctx, task_ctx);
340}
341
Stephane Eraniane5d13672011-02-14 11:20:01 +0200342#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
343 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100344 PERF_FLAG_PID_CGROUP |\
345 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200346
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100347/*
348 * branch priv levels that need permission checks
349 */
350#define PERF_SAMPLE_BRANCH_PERM_PLM \
351 (PERF_SAMPLE_BRANCH_KERNEL |\
352 PERF_SAMPLE_BRANCH_HV)
353
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200354enum event_type_t {
355 EVENT_FLEXIBLE = 0x1,
356 EVENT_PINNED = 0x2,
Peter Zijlstra3cbaa592016-02-24 18:45:47 +0100357 EVENT_TIME = 0x4,
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200358 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
359};
360
Stephane Eraniane5d13672011-02-14 11:20:01 +0200361/*
362 * perf_sched_events : >0 events exist
363 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
364 */
Peter Zijlstra9107c892016-02-24 18:45:45 +0100365
366static void perf_sched_delayed(struct work_struct *work);
367DEFINE_STATIC_KEY_FALSE(perf_sched_events);
368static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
369static DEFINE_MUTEX(perf_sched_mutex);
370static atomic_t perf_sched_count;
371
Stephane Eraniane5d13672011-02-14 11:20:01 +0200372static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500373static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Kan Liangf2fb6be2016-03-23 11:24:37 -0700374static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200375
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200376static atomic_t nr_mmap_events __read_mostly;
377static atomic_t nr_comm_events __read_mostly;
378static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200379static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300380static atomic_t nr_switch_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200381
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200382static LIST_HEAD(pmus);
383static DEFINE_MUTEX(pmus_lock);
384static struct srcu_struct pmus_srcu;
385
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200386/*
387 * perf event paranoia level:
388 * -1 - not paranoid at all
389 * 0 - disallow raw tracepoint access for unpriv
390 * 1 - disallow cpu events for unpriv
391 * 2 - disallow kernel profiling for unpriv
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -0700392 * 3 - disallow all unpriv perf event use
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200393 */
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -0700394#ifdef CONFIG_SECURITY_PERF_EVENTS_RESTRICT
395int sysctl_perf_event_paranoid __read_mostly = 3;
396#else
Andy Lutomirski01610282016-05-09 15:48:51 -0700397int sysctl_perf_event_paranoid __read_mostly = 2;
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -0700398#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200399
Frederic Weisbecker20443382011-03-31 03:33:29 +0200400/* Minimum for 512 kiB + 1 user control page */
401int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200402
403/*
404 * max perf event sample rate
405 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700406#define DEFAULT_MAX_SAMPLE_RATE 100000
407#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
408#define DEFAULT_CPU_TIME_MAX_PERCENT 25
409
410int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
411
412static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
413static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
414
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200415static int perf_sample_allowed_ns __read_mostly =
416 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700417
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800418static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700419{
420 u64 tmp = perf_sample_period_ns;
421
422 tmp *= sysctl_perf_cpu_time_max_percent;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100423 tmp = div_u64(tmp, 100);
424 if (!tmp)
425 tmp = 1;
426
427 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700428}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100429
Stephane Eranian9e630202013-04-03 14:21:33 +0200430static int perf_rotate_context(struct perf_cpu_context *cpuctx);
431
Peter Zijlstra163ec432011-02-16 11:22:34 +0100432int perf_proc_update_handler(struct ctl_table *table, int write,
433 void __user *buffer, size_t *lenp,
434 loff_t *ppos)
435{
Knut Petersen723478c2013-09-25 14:29:37 +0200436 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100437
438 if (ret || !write)
439 return ret;
440
Kan Liangab7fdef2016-05-03 00:26:06 -0700441 /*
442 * If throttling is disabled don't allow the write:
443 */
444 if (sysctl_perf_cpu_time_max_percent == 100 ||
445 sysctl_perf_cpu_time_max_percent == 0)
446 return -EINVAL;
447
Peter Zijlstra163ec432011-02-16 11:22:34 +0100448 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700449 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
450 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100451
452 return 0;
453}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200454
Dave Hansen14c63f12013-06-21 08:51:36 -0700455int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
456
457int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
458 void __user *buffer, size_t *lenp,
459 loff_t *ppos)
460{
461 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
462
463 if (ret || !write)
464 return ret;
465
Peter Zijlstrab303e7c2016-04-04 09:57:40 +0200466 if (sysctl_perf_cpu_time_max_percent == 100 ||
467 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100468 printk(KERN_WARNING
469 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
470 WRITE_ONCE(perf_sample_allowed_ns, 0);
471 } else {
472 update_perf_cpu_limits();
473 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700474
475 return 0;
476}
477
478/*
479 * perf samples are done in some very critical code paths (NMIs).
480 * If they take too much CPU time, the system can lock up and not
481 * get any real work done. This will drop the sample rate when
482 * we detect that events are taking too long.
483 */
484#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200485static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700486
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100487static u64 __report_avg;
488static u64 __report_allowed;
489
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100490static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700491{
David Ahern0d87d7e2016-08-01 13:49:29 -0700492 printk_ratelimited(KERN_INFO
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100493 "perf: interrupt took too long (%lld > %lld), lowering "
494 "kernel.perf_event_max_sample_rate to %d\n",
495 __report_avg, __report_allowed,
496 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100497}
498
499static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
500
501void perf_sample_event_took(u64 sample_len_ns)
502{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100503 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
504 u64 running_len;
505 u64 avg_len;
506 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700507
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100508 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700509 return;
510
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100511 /* Decay the counter by 1 average sample. */
512 running_len = __this_cpu_read(running_sample_length);
513 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
514 running_len += sample_len_ns;
515 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700516
517 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100518 * Note: this will be biased artifically low until we have
519 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700520 * from having to maintain a count.
521 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100522 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
523 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700524 return;
525
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100526 __report_avg = avg_len;
527 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700528
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100529 /*
530 * Compute a throttle threshold 25% below the current duration.
531 */
532 avg_len += avg_len / 4;
533 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
534 if (avg_len < max)
535 max /= (u32)avg_len;
536 else
537 max = 1;
538
539 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
540 WRITE_ONCE(max_samples_per_tick, max);
541
542 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700543 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
544
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100545 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100546 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100547 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100548 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100549 sysctl_perf_event_sample_rate);
550 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700551}
552
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200553static atomic64_t perf_event_id;
554
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200555static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
556 enum event_type_t event_type);
557
558static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200559 enum event_type_t event_type,
560 struct task_struct *task);
561
562static void update_context_time(struct perf_event_context *ctx);
563static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200564
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200565void __weak perf_event_print_debug(void) { }
566
Matt Fleming84c79912010-10-03 21:41:13 +0100567extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200568{
Matt Fleming84c79912010-10-03 21:41:13 +0100569 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200570}
571
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200572static inline u64 perf_clock(void)
573{
574 return local_clock();
575}
576
Peter Zijlstra34f43922015-02-20 14:05:38 +0100577static inline u64 perf_event_clock(struct perf_event *event)
578{
579 return event->clock();
580}
581
Stephane Eraniane5d13672011-02-14 11:20:01 +0200582#ifdef CONFIG_CGROUP_PERF
583
Stephane Eraniane5d13672011-02-14 11:20:01 +0200584static inline bool
585perf_cgroup_match(struct perf_event *event)
586{
587 struct perf_event_context *ctx = event->ctx;
588 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
589
Tejun Heoef824fa2013-04-08 19:00:38 -0700590 /* @event doesn't care about cgroup */
591 if (!event->cgrp)
592 return true;
593
594 /* wants specific cgroup scope but @cpuctx isn't associated with any */
595 if (!cpuctx->cgrp)
596 return false;
597
598 /*
599 * Cgroup scoping is recursive. An event enabled for a cgroup is
600 * also enabled for all its descendant cgroups. If @cpuctx's
601 * cgroup is a descendant of @event's (the test covers identity
602 * case), it's a match.
603 */
604 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
605 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200606}
607
Stephane Eraniane5d13672011-02-14 11:20:01 +0200608static inline void perf_detach_cgroup(struct perf_event *event)
609{
Zefan Li4e2ba652014-09-19 16:53:14 +0800610 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200611 event->cgrp = NULL;
612}
613
614static inline int is_cgroup_event(struct perf_event *event)
615{
616 return event->cgrp != NULL;
617}
618
619static inline u64 perf_cgroup_event_time(struct perf_event *event)
620{
621 struct perf_cgroup_info *t;
622
623 t = per_cpu_ptr(event->cgrp->info, event->cpu);
624 return t->time;
625}
626
627static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
628{
629 struct perf_cgroup_info *info;
630 u64 now;
631
632 now = perf_clock();
633
634 info = this_cpu_ptr(cgrp->info);
635
636 info->time += now - info->timestamp;
637 info->timestamp = now;
638}
639
640static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
641{
642 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
643 if (cgrp_out)
644 __update_cgrp_time(cgrp_out);
645}
646
647static inline void update_cgrp_time_from_event(struct perf_event *event)
648{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200649 struct perf_cgroup *cgrp;
650
Stephane Eraniane5d13672011-02-14 11:20:01 +0200651 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200652 * ensure we access cgroup data only when needed and
653 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200654 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200655 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200656 return;
657
Stephane Eranian614e4c42015-11-12 11:00:04 +0100658 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200659 /*
660 * Do not update time when cgroup is not active
661 */
662 if (cgrp == event->cgrp)
663 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200664}
665
666static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200667perf_cgroup_set_timestamp(struct task_struct *task,
668 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200669{
670 struct perf_cgroup *cgrp;
671 struct perf_cgroup_info *info;
672
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200673 /*
674 * ctx->lock held by caller
675 * ensure we do not access cgroup data
676 * unless we have the cgroup pinned (css_get)
677 */
678 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200679 return;
680
Stephane Eranian614e4c42015-11-12 11:00:04 +0100681 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200682 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200683 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200684}
685
686#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
687#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
688
689/*
690 * reschedule events based on the cgroup constraint of task.
691 *
692 * mode SWOUT : schedule out everything
693 * mode SWIN : schedule in based on cgroup for next
694 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800695static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200696{
697 struct perf_cpu_context *cpuctx;
698 struct pmu *pmu;
699 unsigned long flags;
700
701 /*
702 * disable interrupts to avoid geting nr_cgroup
703 * changes via __perf_event_disable(). Also
704 * avoids preemption.
705 */
706 local_irq_save(flags);
707
708 /*
709 * we reschedule only in the presence of cgroup
710 * constrained events.
711 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200712
713 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200714 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200715 if (cpuctx->unique_pmu != pmu)
716 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200717
Stephane Eraniane5d13672011-02-14 11:20:01 +0200718 /*
719 * perf_cgroup_events says at least one
720 * context on this CPU has cgroup events.
721 *
722 * ctx->nr_cgroups reports the number of cgroup
723 * events for a context.
724 */
725 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200726 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
727 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200728
729 if (mode & PERF_CGROUP_SWOUT) {
730 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
731 /*
732 * must not be done before ctxswout due
733 * to event_filter_match() in event_sched_out()
734 */
735 cpuctx->cgrp = NULL;
736 }
737
738 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200739 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200740 /*
741 * set cgrp before ctxsw in to allow
742 * event_filter_match() to not have to pass
743 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100744 * we pass the cpuctx->ctx to perf_cgroup_from_task()
745 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200746 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100747 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200748 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
749 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200750 perf_pmu_enable(cpuctx->ctx.pmu);
751 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200752 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200753 }
754
Stephane Eraniane5d13672011-02-14 11:20:01 +0200755 local_irq_restore(flags);
756}
757
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200758static inline void perf_cgroup_sched_out(struct task_struct *task,
759 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200760{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200761 struct perf_cgroup *cgrp1;
762 struct perf_cgroup *cgrp2 = NULL;
763
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100764 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200765 /*
766 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100767 * we do not need to pass the ctx here because we know
768 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200769 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100770 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100771 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200772
773 /*
774 * only schedule out current cgroup events if we know
775 * that we are switching to a different cgroup. Otherwise,
776 * do no touch the cgroup events.
777 */
778 if (cgrp1 != cgrp2)
779 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100780
781 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200782}
783
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200784static inline void perf_cgroup_sched_in(struct task_struct *prev,
785 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200786{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200787 struct perf_cgroup *cgrp1;
788 struct perf_cgroup *cgrp2 = NULL;
789
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100790 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200791 /*
792 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100793 * we do not need to pass the ctx here because we know
794 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200795 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100796 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100797 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200798
799 /*
800 * only need to schedule in cgroup events if we are changing
801 * cgroup during ctxsw. Cgroup events were not scheduled
802 * out of ctxsw out if that was not the case.
803 */
804 if (cgrp1 != cgrp2)
805 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100806
807 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200808}
809
810static inline int perf_cgroup_connect(int fd, struct perf_event *event,
811 struct perf_event_attr *attr,
812 struct perf_event *group_leader)
813{
814 struct perf_cgroup *cgrp;
815 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400816 struct fd f = fdget(fd);
817 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200818
Al Viro2903ff02012-08-28 12:52:22 -0400819 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200820 return -EBADF;
821
Al Virob5830432014-10-31 01:22:04 -0400822 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400823 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800824 if (IS_ERR(css)) {
825 ret = PTR_ERR(css);
826 goto out;
827 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200828
829 cgrp = container_of(css, struct perf_cgroup, css);
830 event->cgrp = cgrp;
831
832 /*
833 * all events in a group must monitor
834 * the same cgroup because a task belongs
835 * to only one perf cgroup at a time
836 */
837 if (group_leader && group_leader->cgrp != cgrp) {
838 perf_detach_cgroup(event);
839 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200840 }
Li Zefan3db272c2011-03-03 14:25:37 +0800841out:
Al Viro2903ff02012-08-28 12:52:22 -0400842 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200843 return ret;
844}
845
846static inline void
847perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
848{
849 struct perf_cgroup_info *t;
850 t = per_cpu_ptr(event->cgrp->info, event->cpu);
851 event->shadow_ctx_time = now - t->timestamp;
852}
853
854static inline void
855perf_cgroup_defer_enabled(struct perf_event *event)
856{
857 /*
858 * when the current task's perf cgroup does not match
859 * the event's, we need to remember to call the
860 * perf_mark_enable() function the first time a task with
861 * a matching perf cgroup is scheduled in.
862 */
863 if (is_cgroup_event(event) && !perf_cgroup_match(event))
864 event->cgrp_defer_enabled = 1;
865}
866
867static inline void
868perf_cgroup_mark_enabled(struct perf_event *event,
869 struct perf_event_context *ctx)
870{
871 struct perf_event *sub;
872 u64 tstamp = perf_event_time(event);
873
874 if (!event->cgrp_defer_enabled)
875 return;
876
877 event->cgrp_defer_enabled = 0;
878
879 event->tstamp_enabled = tstamp - event->total_time_enabled;
880 list_for_each_entry(sub, &event->sibling_list, group_entry) {
881 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
882 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
883 sub->cgrp_defer_enabled = 0;
884 }
885 }
886}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700887
888/*
889 * Update cpuctx->cgrp so that it is set when first cgroup event is added and
890 * cleared when last cgroup event is removed.
891 */
892static inline void
893list_update_cgroup_event(struct perf_event *event,
894 struct perf_event_context *ctx, bool add)
895{
896 struct perf_cpu_context *cpuctx;
897
898 if (!is_cgroup_event(event))
899 return;
900
901 if (add && ctx->nr_cgroups++)
902 return;
903 else if (!add && --ctx->nr_cgroups)
904 return;
905 /*
906 * Because cgroup events are always per-cpu events,
907 * this will always be called from the right CPU.
908 */
909 cpuctx = __get_cpu_context(ctx);
David Carrillo-Cisneros864c2352016-11-01 11:52:58 -0700910
David Carrillo-Cisneros8fc31ce2016-12-04 00:46:17 -0800911 /*
912 * cpuctx->cgrp is NULL until a cgroup event is sched in or
913 * ctx->nr_cgroup == 0 .
914 */
915 if (add && perf_cgroup_from_task(current, ctx) == event->cgrp)
916 cpuctx->cgrp = event->cgrp;
917 else if (!add)
918 cpuctx->cgrp = NULL;
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700919}
920
Stephane Eraniane5d13672011-02-14 11:20:01 +0200921#else /* !CONFIG_CGROUP_PERF */
922
923static inline bool
924perf_cgroup_match(struct perf_event *event)
925{
926 return true;
927}
928
929static inline void perf_detach_cgroup(struct perf_event *event)
930{}
931
932static inline int is_cgroup_event(struct perf_event *event)
933{
934 return 0;
935}
936
937static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
938{
939 return 0;
940}
941
942static inline void update_cgrp_time_from_event(struct perf_event *event)
943{
944}
945
946static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
947{
948}
949
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200950static inline void perf_cgroup_sched_out(struct task_struct *task,
951 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200952{
953}
954
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200955static inline void perf_cgroup_sched_in(struct task_struct *prev,
956 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200957{
958}
959
960static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
961 struct perf_event_attr *attr,
962 struct perf_event *group_leader)
963{
964 return -EINVAL;
965}
966
967static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200968perf_cgroup_set_timestamp(struct task_struct *task,
969 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200970{
971}
972
973void
974perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
975{
976}
977
978static inline void
979perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
980{
981}
982
983static inline u64 perf_cgroup_event_time(struct perf_event *event)
984{
985 return 0;
986}
987
988static inline void
989perf_cgroup_defer_enabled(struct perf_event *event)
990{
991}
992
993static inline void
994perf_cgroup_mark_enabled(struct perf_event *event,
995 struct perf_event_context *ctx)
996{
997}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700998
999static inline void
1000list_update_cgroup_event(struct perf_event *event,
1001 struct perf_event_context *ctx, bool add)
1002{
1003}
1004
Stephane Eraniane5d13672011-02-14 11:20:01 +02001005#endif
1006
Stephane Eranian9e630202013-04-03 14:21:33 +02001007/*
1008 * set default to be dependent on timer tick just
1009 * like original code
1010 */
1011#define PERF_CPU_HRTIMER (1000 / HZ)
1012/*
1013 * function must be called with interrupts disbled
1014 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001015static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +02001016{
1017 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +02001018 int rotations = 0;
1019
1020 WARN_ON(!irqs_disabled());
1021
1022 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +02001023 rotations = perf_rotate_context(cpuctx);
1024
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001025 raw_spin_lock(&cpuctx->hrtimer_lock);
1026 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +02001027 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001028 else
1029 cpuctx->hrtimer_active = 0;
1030 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +02001031
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001032 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +02001033}
1034
Peter Zijlstra272325c2015-04-15 11:41:58 +02001035static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +02001036{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001037 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001038 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +02001039 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +02001040
1041 /* no multiplexing needed for SW PMU */
1042 if (pmu->task_ctx_nr == perf_sw_context)
1043 return;
1044
Stephane Eranian62b85632013-04-03 14:21:34 +02001045 /*
1046 * check default is sane, if not set then force to
1047 * default interval (1/tick)
1048 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001049 interval = pmu->hrtimer_interval_ms;
1050 if (interval < 1)
1051 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001052
Peter Zijlstra272325c2015-04-15 11:41:58 +02001053 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001054
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001055 raw_spin_lock_init(&cpuctx->hrtimer_lock);
1056 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001057 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001058}
1059
Peter Zijlstra272325c2015-04-15 11:41:58 +02001060static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001061{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001062 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001063 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001064 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001065
1066 /* not for SW PMU */
1067 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001068 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001069
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001070 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1071 if (!cpuctx->hrtimer_active) {
1072 cpuctx->hrtimer_active = 1;
1073 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
1074 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
1075 }
1076 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001077
Peter Zijlstra272325c2015-04-15 11:41:58 +02001078 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001079}
1080
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001081void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001082{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001083 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1084 if (!(*count)++)
1085 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001086}
1087
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001088void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001089{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001090 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1091 if (!--(*count))
1092 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001093}
1094
Mark Rutland2fde4f92015-01-07 15:01:54 +00001095static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001096
1097/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001098 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1099 * perf_event_task_tick() are fully serialized because they're strictly cpu
1100 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1101 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001102 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001103static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001104{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001105 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001106
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001107 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001108
Mark Rutland2fde4f92015-01-07 15:01:54 +00001109 WARN_ON(!list_empty(&ctx->active_ctx_list));
1110
1111 list_add(&ctx->active_ctx_list, head);
1112}
1113
1114static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1115{
1116 WARN_ON(!irqs_disabled());
1117
1118 WARN_ON(list_empty(&ctx->active_ctx_list));
1119
1120 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001121}
1122
1123static void get_ctx(struct perf_event_context *ctx)
1124{
1125 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1126}
1127
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001128static void free_ctx(struct rcu_head *head)
1129{
1130 struct perf_event_context *ctx;
1131
1132 ctx = container_of(head, struct perf_event_context, rcu_head);
1133 kfree(ctx->task_ctx_data);
1134 kfree(ctx);
1135}
1136
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001137static void put_ctx(struct perf_event_context *ctx)
1138{
1139 if (atomic_dec_and_test(&ctx->refcount)) {
1140 if (ctx->parent_ctx)
1141 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001142 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001144 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001145 }
1146}
1147
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001148/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001149 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1150 * perf_pmu_migrate_context() we need some magic.
1151 *
1152 * Those places that change perf_event::ctx will hold both
1153 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1154 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001155 * Lock ordering is by mutex address. There are two other sites where
1156 * perf_event_context::mutex nests and those are:
1157 *
1158 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001159 * perf_event_exit_event()
1160 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001161 *
1162 * - perf_event_init_context() [ parent, 0 ]
1163 * inherit_task_group()
1164 * inherit_group()
1165 * inherit_event()
1166 * perf_event_alloc()
1167 * perf_init_event()
1168 * perf_try_init_event() [ child , 1 ]
1169 *
1170 * While it appears there is an obvious deadlock here -- the parent and child
1171 * nesting levels are inverted between the two. This is in fact safe because
1172 * life-time rules separate them. That is an exiting task cannot fork, and a
1173 * spawning task cannot (yet) exit.
1174 *
1175 * But remember that that these are parent<->child context relations, and
1176 * migration does not affect children, therefore these two orderings should not
1177 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001178 *
1179 * The change in perf_event::ctx does not affect children (as claimed above)
1180 * because the sys_perf_event_open() case will install a new event and break
1181 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1182 * concerned with cpuctx and that doesn't have children.
1183 *
1184 * The places that change perf_event::ctx will issue:
1185 *
1186 * perf_remove_from_context();
1187 * synchronize_rcu();
1188 * perf_install_in_context();
1189 *
1190 * to affect the change. The remove_from_context() + synchronize_rcu() should
1191 * quiesce the event, after which we can install it in the new location. This
1192 * means that only external vectors (perf_fops, prctl) can perturb the event
1193 * while in transit. Therefore all such accessors should also acquire
1194 * perf_event_context::mutex to serialize against this.
1195 *
1196 * However; because event->ctx can change while we're waiting to acquire
1197 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1198 * function.
1199 *
1200 * Lock order:
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02001201 * cred_guard_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001202 * task_struct::perf_event_mutex
1203 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001204 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001205 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001206 * perf_event::mmap_mutex
1207 * mmap_sem
1208 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001209static struct perf_event_context *
1210perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001211{
1212 struct perf_event_context *ctx;
1213
1214again:
1215 rcu_read_lock();
1216 ctx = ACCESS_ONCE(event->ctx);
1217 if (!atomic_inc_not_zero(&ctx->refcount)) {
1218 rcu_read_unlock();
1219 goto again;
1220 }
1221 rcu_read_unlock();
1222
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001223 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001224 if (event->ctx != ctx) {
1225 mutex_unlock(&ctx->mutex);
1226 put_ctx(ctx);
1227 goto again;
1228 }
1229
1230 return ctx;
1231}
1232
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001233static inline struct perf_event_context *
1234perf_event_ctx_lock(struct perf_event *event)
1235{
1236 return perf_event_ctx_lock_nested(event, 0);
1237}
1238
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001239static void perf_event_ctx_unlock(struct perf_event *event,
1240 struct perf_event_context *ctx)
1241{
1242 mutex_unlock(&ctx->mutex);
1243 put_ctx(ctx);
1244}
1245
1246/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001247 * This must be done under the ctx->lock, such as to serialize against
1248 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1249 * calling scheduler related locks and ctx->lock nests inside those.
1250 */
1251static __must_check struct perf_event_context *
1252unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001254 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1255
1256 lockdep_assert_held(&ctx->lock);
1257
1258 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001259 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001260 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001261
1262 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001263}
1264
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001265static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1266{
1267 /*
1268 * only top level events have the pid namespace they were created in
1269 */
1270 if (event->parent)
1271 event = event->parent;
1272
1273 return task_tgid_nr_ns(p, event->ns);
1274}
1275
1276static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1277{
1278 /*
1279 * only top level events have the pid namespace they were created in
1280 */
1281 if (event->parent)
1282 event = event->parent;
1283
1284 return task_pid_nr_ns(p, event->ns);
1285}
1286
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001287/*
1288 * If we inherit events we want to return the parent event id
1289 * to userspace.
1290 */
1291static u64 primary_event_id(struct perf_event *event)
1292{
1293 u64 id = event->id;
1294
1295 if (event->parent)
1296 id = event->parent->id;
1297
1298 return id;
1299}
1300
1301/*
1302 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001303 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001304 * This has to cope with with the fact that until it is locked,
1305 * the context could get moved to another task.
1306 */
1307static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001308perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001309{
1310 struct perf_event_context *ctx;
1311
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001312retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001313 /*
1314 * One of the few rules of preemptible RCU is that one cannot do
1315 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001316 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001317 * rcu_read_unlock_special().
1318 *
1319 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001320 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001321 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001322 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001323 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001324 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001325 if (ctx) {
1326 /*
1327 * If this context is a clone of another, it might
1328 * get swapped for another underneath us by
1329 * perf_event_task_sched_out, though the
1330 * rcu_read_lock() protects us from any context
1331 * getting freed. Lock the context and check if it
1332 * got swapped before we could get the lock, and retry
1333 * if so. If we locked the right context, then it
1334 * can't get swapped on us any more.
1335 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001336 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001337 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001338 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001339 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001340 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001341 goto retry;
1342 }
1343
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001344 if (ctx->task == TASK_TOMBSTONE ||
1345 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001346 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001347 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001348 } else {
1349 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001350 }
1351 }
1352 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001353 if (!ctx)
1354 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001355 return ctx;
1356}
1357
1358/*
1359 * Get the context for a task and increment its pin_count so it
1360 * can't get swapped to another task. This also increments its
1361 * reference count so that the context can't get freed.
1362 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001363static struct perf_event_context *
1364perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001365{
1366 struct perf_event_context *ctx;
1367 unsigned long flags;
1368
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001369 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001370 if (ctx) {
1371 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001372 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001373 }
1374 return ctx;
1375}
1376
1377static void perf_unpin_context(struct perf_event_context *ctx)
1378{
1379 unsigned long flags;
1380
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001381 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001382 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001383 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001384}
1385
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001386/*
1387 * Update the record of the current time in a context.
1388 */
1389static void update_context_time(struct perf_event_context *ctx)
1390{
1391 u64 now = perf_clock();
1392
1393 ctx->time += now - ctx->timestamp;
1394 ctx->timestamp = now;
1395}
1396
Stephane Eranian41587552011-01-03 18:20:01 +02001397static u64 perf_event_time(struct perf_event *event)
1398{
1399 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001400
1401 if (is_cgroup_event(event))
1402 return perf_cgroup_event_time(event);
1403
Stephane Eranian41587552011-01-03 18:20:01 +02001404 return ctx ? ctx->time : 0;
1405}
1406
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001407/*
1408 * Update the total_time_enabled and total_time_running fields for a event.
1409 */
1410static void update_event_times(struct perf_event *event)
1411{
1412 struct perf_event_context *ctx = event->ctx;
1413 u64 run_end;
1414
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001415 lockdep_assert_held(&ctx->lock);
1416
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001417 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1418 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1419 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001420
Stephane Eraniane5d13672011-02-14 11:20:01 +02001421 /*
1422 * in cgroup mode, time_enabled represents
1423 * the time the event was enabled AND active
1424 * tasks were in the monitored cgroup. This is
1425 * independent of the activity of the context as
1426 * there may be a mix of cgroup and non-cgroup events.
1427 *
1428 * That is why we treat cgroup events differently
1429 * here.
1430 */
1431 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001432 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001433 else if (ctx->is_active)
1434 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001435 else
1436 run_end = event->tstamp_stopped;
1437
1438 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001439
1440 if (event->state == PERF_EVENT_STATE_INACTIVE)
1441 run_end = event->tstamp_stopped;
1442 else
Stephane Eranian41587552011-01-03 18:20:01 +02001443 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001444
1445 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001446
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001447}
1448
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001449/*
1450 * Update total_time_enabled and total_time_running for all events in a group.
1451 */
1452static void update_group_times(struct perf_event *leader)
1453{
1454 struct perf_event *event;
1455
1456 update_event_times(leader);
1457 list_for_each_entry(event, &leader->sibling_list, group_entry)
1458 update_event_times(event);
1459}
1460
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001461static struct list_head *
1462ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1463{
1464 if (event->attr.pinned)
1465 return &ctx->pinned_groups;
1466 else
1467 return &ctx->flexible_groups;
1468}
1469
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470/*
1471 * Add a event from the lists for its context.
1472 * Must be called with ctx->mutex and ctx->lock held.
1473 */
1474static void
1475list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1476{
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001477
Peter Zijlstrac994d612016-01-08 09:20:23 +01001478 lockdep_assert_held(&ctx->lock);
1479
Peter Zijlstra8a495422010-05-27 15:47:49 +02001480 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1481 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001482
1483 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001484 * If we're a stand alone event or group leader, we go to the context
1485 * list, group events are kept attached to the group so that
1486 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001488 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001489 struct list_head *list;
1490
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001491 event->group_caps = event->event_caps;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001492
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001493 list = ctx_group_list(event, ctx);
1494 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495 }
1496
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001497 list_update_cgroup_event(event, ctx, true);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001498
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001499 list_add_rcu(&event->event_entry, &ctx->event_list);
1500 ctx->nr_events++;
1501 if (event->attr.inherit_stat)
1502 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001503
1504 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001505}
1506
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001507/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001508 * Initialize event state based on the perf_event_attr::disabled.
1509 */
1510static inline void perf_event__state_init(struct perf_event *event)
1511{
1512 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1513 PERF_EVENT_STATE_INACTIVE;
1514}
1515
Peter Zijlstraa7239682015-09-09 19:06:33 +02001516static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001517{
1518 int entry = sizeof(u64); /* value */
1519 int size = 0;
1520 int nr = 1;
1521
1522 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1523 size += sizeof(u64);
1524
1525 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1526 size += sizeof(u64);
1527
1528 if (event->attr.read_format & PERF_FORMAT_ID)
1529 entry += sizeof(u64);
1530
1531 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001532 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001533 size += sizeof(u64);
1534 }
1535
1536 size += entry * nr;
1537 event->read_size = size;
1538}
1539
Peter Zijlstraa7239682015-09-09 19:06:33 +02001540static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001541{
1542 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001543 u16 size = 0;
1544
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001545 if (sample_type & PERF_SAMPLE_IP)
1546 size += sizeof(data->ip);
1547
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001548 if (sample_type & PERF_SAMPLE_ADDR)
1549 size += sizeof(data->addr);
1550
1551 if (sample_type & PERF_SAMPLE_PERIOD)
1552 size += sizeof(data->period);
1553
Andi Kleenc3feedf2013-01-24 16:10:28 +01001554 if (sample_type & PERF_SAMPLE_WEIGHT)
1555 size += sizeof(data->weight);
1556
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001557 if (sample_type & PERF_SAMPLE_READ)
1558 size += event->read_size;
1559
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001560 if (sample_type & PERF_SAMPLE_DATA_SRC)
1561 size += sizeof(data->data_src.val);
1562
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001563 if (sample_type & PERF_SAMPLE_TRANSACTION)
1564 size += sizeof(data->txn);
1565
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001566 event->header_size = size;
1567}
1568
Peter Zijlstraa7239682015-09-09 19:06:33 +02001569/*
1570 * Called at perf_event creation and when events are attached/detached from a
1571 * group.
1572 */
1573static void perf_event__header_size(struct perf_event *event)
1574{
1575 __perf_event_read_size(event,
1576 event->group_leader->nr_siblings);
1577 __perf_event_header_size(event, event->attr.sample_type);
1578}
1579
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001580static void perf_event__id_header_size(struct perf_event *event)
1581{
1582 struct perf_sample_data *data;
1583 u64 sample_type = event->attr.sample_type;
1584 u16 size = 0;
1585
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001586 if (sample_type & PERF_SAMPLE_TID)
1587 size += sizeof(data->tid_entry);
1588
1589 if (sample_type & PERF_SAMPLE_TIME)
1590 size += sizeof(data->time);
1591
Adrian Hunterff3d5272013-08-27 11:23:07 +03001592 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1593 size += sizeof(data->id);
1594
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001595 if (sample_type & PERF_SAMPLE_ID)
1596 size += sizeof(data->id);
1597
1598 if (sample_type & PERF_SAMPLE_STREAM_ID)
1599 size += sizeof(data->stream_id);
1600
1601 if (sample_type & PERF_SAMPLE_CPU)
1602 size += sizeof(data->cpu_entry);
1603
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001604 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001605}
1606
Peter Zijlstraa7239682015-09-09 19:06:33 +02001607static bool perf_event_validate_size(struct perf_event *event)
1608{
1609 /*
1610 * The values computed here will be over-written when we actually
1611 * attach the event.
1612 */
1613 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1614 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1615 perf_event__id_header_size(event);
1616
1617 /*
1618 * Sum the lot; should not exceed the 64k limit we have on records.
1619 * Conservative limit to allow for callchains and other variable fields.
1620 */
1621 if (event->read_size + event->header_size +
1622 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1623 return false;
1624
1625 return true;
1626}
1627
Peter Zijlstra8a495422010-05-27 15:47:49 +02001628static void perf_group_attach(struct perf_event *event)
1629{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001630 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001631
Peter Zijlstra74c33372010-10-15 11:40:29 +02001632 /*
1633 * We can have double attach due to group movement in perf_event_open.
1634 */
1635 if (event->attach_state & PERF_ATTACH_GROUP)
1636 return;
1637
Peter Zijlstra8a495422010-05-27 15:47:49 +02001638 event->attach_state |= PERF_ATTACH_GROUP;
1639
1640 if (group_leader == event)
1641 return;
1642
Peter Zijlstra652884f2015-01-23 11:20:10 +01001643 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1644
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001645 group_leader->group_caps &= event->event_caps;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001646
1647 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1648 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001649
1650 perf_event__header_size(group_leader);
1651
1652 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1653 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001654}
1655
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656/*
1657 * Remove a event from the lists for its context.
1658 * Must be called with ctx->mutex and ctx->lock held.
1659 */
1660static void
1661list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1662{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001663 WARN_ON_ONCE(event->ctx != ctx);
1664 lockdep_assert_held(&ctx->lock);
1665
Peter Zijlstra8a495422010-05-27 15:47:49 +02001666 /*
1667 * We can have double detach due to exit/hot-unplug + close.
1668 */
1669 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001670 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001671
1672 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1673
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001674 list_update_cgroup_event(event, ctx, false);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001675
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001676 ctx->nr_events--;
1677 if (event->attr.inherit_stat)
1678 ctx->nr_stat--;
1679
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001680 list_del_rcu(&event->event_entry);
1681
Peter Zijlstra8a495422010-05-27 15:47:49 +02001682 if (event->group_leader == event)
1683 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001684
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001685 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001686
1687 /*
1688 * If event was in error state, then keep it
1689 * that way, otherwise bogus counts will be
1690 * returned on read(). The only way to get out
1691 * of error state is by explicit re-enabling
1692 * of the event
1693 */
1694 if (event->state > PERF_EVENT_STATE_OFF)
1695 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001696
1697 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001698}
1699
Peter Zijlstra8a495422010-05-27 15:47:49 +02001700static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001701{
1702 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001703 struct list_head *list = NULL;
1704
1705 /*
1706 * We can have double detach due to exit/hot-unplug + close.
1707 */
1708 if (!(event->attach_state & PERF_ATTACH_GROUP))
1709 return;
1710
1711 event->attach_state &= ~PERF_ATTACH_GROUP;
1712
1713 /*
1714 * If this is a sibling, remove it from its group.
1715 */
1716 if (event->group_leader != event) {
1717 list_del_init(&event->group_entry);
1718 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001719 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001720 }
1721
1722 if (!list_empty(&event->group_entry))
1723 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001725 /*
1726 * If this was a group event with sibling events then
1727 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001728 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001729 */
1730 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001731 if (list)
1732 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001733 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001734
1735 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001736 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001737
1738 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001740
1741out:
1742 perf_event__header_size(event->group_leader);
1743
1744 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1745 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001746}
1747
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001748static bool is_orphaned_event(struct perf_event *event)
1749{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001750 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001751}
1752
Mark Rutland2c81a642016-06-14 16:10:41 +01001753static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01001754{
1755 struct pmu *pmu = event->pmu;
1756 return pmu->filter_match ? pmu->filter_match(event) : 1;
1757}
1758
Mark Rutland2c81a642016-06-14 16:10:41 +01001759/*
1760 * Check whether we should attempt to schedule an event group based on
1761 * PMU-specific filtering. An event group can consist of HW and SW events,
1762 * potentially with a SW leader, so we must check all the filters, to
1763 * determine whether a group is schedulable:
1764 */
1765static inline int pmu_filter_match(struct perf_event *event)
1766{
1767 struct perf_event *child;
1768
1769 if (!__pmu_filter_match(event))
1770 return 0;
1771
1772 list_for_each_entry(child, &event->sibling_list, group_entry) {
1773 if (!__pmu_filter_match(child))
1774 return 0;
1775 }
1776
1777 return 1;
1778}
1779
Stephane Eranianfa66f072010-08-26 16:40:01 +02001780static inline int
1781event_filter_match(struct perf_event *event)
1782{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001783 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
1784 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001785}
1786
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001787static void
1788event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789 struct perf_cpu_context *cpuctx,
1790 struct perf_event_context *ctx)
1791{
Stephane Eranian41587552011-01-03 18:20:01 +02001792 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001793 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001794
1795 WARN_ON_ONCE(event->ctx != ctx);
1796 lockdep_assert_held(&ctx->lock);
1797
Stephane Eranianfa66f072010-08-26 16:40:01 +02001798 /*
1799 * An event which could not be activated because of
1800 * filter mismatch still needs to have its timings
1801 * maintained, otherwise bogus information is return
1802 * via read() for time_enabled, time_running:
1803 */
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001804 if (event->state == PERF_EVENT_STATE_INACTIVE &&
1805 !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001806 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001807 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001808 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001809 }
1810
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001811 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001812 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001813
Alexander Shishkin44377272013-12-16 14:17:36 +02001814 perf_pmu_disable(event->pmu);
1815
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001816 event->tstamp_stopped = tstamp;
1817 event->pmu->del(event, 0);
1818 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001819 event->state = PERF_EVENT_STATE_INACTIVE;
1820 if (event->pending_disable) {
1821 event->pending_disable = 0;
1822 event->state = PERF_EVENT_STATE_OFF;
1823 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001824
1825 if (!is_software_event(event))
1826 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001827 if (!--ctx->nr_active)
1828 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001829 if (event->attr.freq && event->attr.sample_freq)
1830 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001831 if (event->attr.exclusive || !cpuctx->active_oncpu)
1832 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001833
1834 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001835}
1836
1837static void
1838group_sched_out(struct perf_event *group_event,
1839 struct perf_cpu_context *cpuctx,
1840 struct perf_event_context *ctx)
1841{
1842 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001843 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001844
Mark Rutland3f005e72016-07-26 18:12:21 +01001845 perf_pmu_disable(ctx->pmu);
1846
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001847 event_sched_out(group_event, cpuctx, ctx);
1848
1849 /*
1850 * Schedule out siblings (if any):
1851 */
1852 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1853 event_sched_out(event, cpuctx, ctx);
1854
Mark Rutland3f005e72016-07-26 18:12:21 +01001855 perf_pmu_enable(ctx->pmu);
1856
Stephane Eranianfa66f072010-08-26 16:40:01 +02001857 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858 cpuctx->exclusive = 0;
1859}
1860
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001861#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001862
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863/*
1864 * Cross CPU call to remove a performance event
1865 *
1866 * We disable the event on the hardware level first. After that we
1867 * remove it from the context list.
1868 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001869static void
1870__perf_remove_from_context(struct perf_event *event,
1871 struct perf_cpu_context *cpuctx,
1872 struct perf_event_context *ctx,
1873 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001874{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001875 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001877 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001878 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001879 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001880 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001881
Peter Zijlstra39a43642016-01-11 12:46:35 +01001882 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001883 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001884 if (ctx->task) {
1885 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1886 cpuctx->task_ctx = NULL;
1887 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001888 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889}
1890
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001891/*
1892 * Remove the event from a task's (or a CPU's) list of events.
1893 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 * If event->ctx is a cloned context, callers must make sure that
1895 * every task struct that event->ctx->task could possibly point to
1896 * remains valid. This is OK when called from perf_release since
1897 * that only calls us on the top-level context, which can't be a clone.
1898 * When called from perf_event_exit_task, it's OK because the
1899 * context has been detached from its task.
1900 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001901static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001903 lockdep_assert_held(&event->ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001904
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001905 event_function_call(event, __perf_remove_from_context, (void *)flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001906}
1907
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001908/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001909 * Cross CPU call to disable a performance event
1910 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001911static void __perf_event_disable(struct perf_event *event,
1912 struct perf_cpu_context *cpuctx,
1913 struct perf_event_context *ctx,
1914 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001915{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001916 if (event->state < PERF_EVENT_STATE_INACTIVE)
1917 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001919 update_context_time(ctx);
1920 update_cgrp_time_from_event(event);
1921 update_group_times(event);
1922 if (event == event->group_leader)
1923 group_sched_out(event, cpuctx, ctx);
1924 else
1925 event_sched_out(event, cpuctx, ctx);
1926 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001927}
1928
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001929/*
1930 * Disable a event.
1931 *
1932 * If event->ctx is a cloned context, callers must make sure that
1933 * every task struct that event->ctx->task could possibly point to
1934 * remains valid. This condition is satisifed when called through
1935 * perf_event_for_each_child or perf_event_for_each because they
1936 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001937 * goes to exit will block in perf_event_exit_event().
1938 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001939 * When called from perf_pending_event it's OK because event->ctx
1940 * is the current context on this CPU and preemption is disabled,
1941 * hence we can't get into perf_event_task_sched_out for this context.
1942 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001943static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001944{
1945 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001946
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001947 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001948 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001949 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001950 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001951 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001952 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001953
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001954 event_function_call(event, __perf_event_disable, NULL);
1955}
1956
1957void perf_event_disable_local(struct perf_event *event)
1958{
1959 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001960}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001961
1962/*
1963 * Strictly speaking kernel users cannot create groups and therefore this
1964 * interface does not need the perf_event_ctx_lock() magic.
1965 */
1966void perf_event_disable(struct perf_event *event)
1967{
1968 struct perf_event_context *ctx;
1969
1970 ctx = perf_event_ctx_lock(event);
1971 _perf_event_disable(event);
1972 perf_event_ctx_unlock(event, ctx);
1973}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001974EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001975
Jiri Olsa5aab90c2016-10-26 11:48:24 +02001976void perf_event_disable_inatomic(struct perf_event *event)
1977{
1978 event->pending_disable = 1;
1979 irq_work_queue(&event->pending);
1980}
1981
Stephane Eraniane5d13672011-02-14 11:20:01 +02001982static void perf_set_shadow_time(struct perf_event *event,
1983 struct perf_event_context *ctx,
1984 u64 tstamp)
1985{
1986 /*
1987 * use the correct time source for the time snapshot
1988 *
1989 * We could get by without this by leveraging the
1990 * fact that to get to this function, the caller
1991 * has most likely already called update_context_time()
1992 * and update_cgrp_time_xx() and thus both timestamp
1993 * are identical (or very close). Given that tstamp is,
1994 * already adjusted for cgroup, we could say that:
1995 * tstamp - ctx->timestamp
1996 * is equivalent to
1997 * tstamp - cgrp->timestamp.
1998 *
1999 * Then, in perf_output_read(), the calculation would
2000 * work with no changes because:
2001 * - event is guaranteed scheduled in
2002 * - no scheduled out in between
2003 * - thus the timestamp would be the same
2004 *
2005 * But this is a bit hairy.
2006 *
2007 * So instead, we have an explicit cgroup call to remain
2008 * within the time time source all along. We believe it
2009 * is cleaner and simpler to understand.
2010 */
2011 if (is_cgroup_event(event))
2012 perf_cgroup_set_shadow_time(event, tstamp);
2013 else
2014 event->shadow_ctx_time = tstamp - ctx->timestamp;
2015}
2016
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002017#define MAX_INTERRUPTS (~0ULL)
2018
2019static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002020static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002022static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002023event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002025 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002026{
Stephane Eranian41587552011-01-03 18:20:01 +02002027 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02002028 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002029
Peter Zijlstra63342412014-05-05 11:49:16 +02002030 lockdep_assert_held(&ctx->lock);
2031
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002032 if (event->state <= PERF_EVENT_STATE_OFF)
2033 return 0;
2034
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002035 WRITE_ONCE(event->oncpu, smp_processor_id());
2036 /*
2037 * Order event::oncpu write to happen before the ACTIVE state
2038 * is visible.
2039 */
2040 smp_wmb();
2041 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002042
2043 /*
2044 * Unthrottle events, since we scheduled we might have missed several
2045 * ticks already, also for a heavily scheduling task there is little
2046 * guarantee it'll get a tick in a timely manner.
2047 */
2048 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2049 perf_log_throttle(event, 1);
2050 event->hw.interrupts = 0;
2051 }
2052
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002053 /*
2054 * The new state must be visible before we turn it on in the hardware:
2055 */
2056 smp_wmb();
2057
Alexander Shishkin44377272013-12-16 14:17:36 +02002058 perf_pmu_disable(event->pmu);
2059
Shaohua Li72f669c2015-02-05 15:55:31 -08002060 perf_set_shadow_time(event, ctx, tstamp);
2061
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002062 perf_log_itrace_start(event);
2063
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002064 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002065 event->state = PERF_EVENT_STATE_INACTIVE;
2066 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002067 ret = -EAGAIN;
2068 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002069 }
2070
Peter Zijlstra00a29162015-07-27 10:35:07 +02002071 event->tstamp_running += tstamp - event->tstamp_stopped;
2072
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002073 if (!is_software_event(event))
2074 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002075 if (!ctx->nr_active++)
2076 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002077 if (event->attr.freq && event->attr.sample_freq)
2078 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002079
2080 if (event->attr.exclusive)
2081 cpuctx->exclusive = 1;
2082
Alexander Shishkin44377272013-12-16 14:17:36 +02002083out:
2084 perf_pmu_enable(event->pmu);
2085
2086 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002087}
2088
2089static int
2090group_sched_in(struct perf_event *group_event,
2091 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002092 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093{
Lin Ming6bde9b62010-04-23 13:56:00 +08002094 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002095 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002096 u64 now = ctx->time;
2097 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098
2099 if (group_event->state == PERF_EVENT_STATE_OFF)
2100 return 0;
2101
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002102 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002103
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002104 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002105 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002106 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002107 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002108 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002109
2110 /*
2111 * Schedule in siblings as one group (if any):
2112 */
2113 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002114 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002115 partial_group = event;
2116 goto group_error;
2117 }
2118 }
2119
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002120 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002121 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002122
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123group_error:
2124 /*
2125 * Groups can be scheduled in as one unit only, so undo any
2126 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002127 * The events up to the failed event are scheduled out normally,
2128 * tstamp_stopped will be updated.
2129 *
2130 * The failed events and the remaining siblings need to have
2131 * their timings updated as if they had gone thru event_sched_in()
2132 * and event_sched_out(). This is required to get consistent timings
2133 * across the group. This also takes care of the case where the group
2134 * could never be scheduled by ensuring tstamp_stopped is set to mark
2135 * the time the event was actually stopped, such that time delta
2136 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002137 */
2138 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2139 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002140 simulate = true;
2141
2142 if (simulate) {
2143 event->tstamp_running += now - event->tstamp_stopped;
2144 event->tstamp_stopped = now;
2145 } else {
2146 event_sched_out(event, cpuctx, ctx);
2147 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002148 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002149 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002150
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002151 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002152
Peter Zijlstra272325c2015-04-15 11:41:58 +02002153 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002154
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002155 return -EAGAIN;
2156}
2157
2158/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002159 * Work out whether we can put this event group on the CPU now.
2160 */
2161static int group_can_go_on(struct perf_event *event,
2162 struct perf_cpu_context *cpuctx,
2163 int can_add_hw)
2164{
2165 /*
2166 * Groups consisting entirely of software events can always go on.
2167 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002168 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002169 return 1;
2170 /*
2171 * If an exclusive group is already on, no other hardware
2172 * events can go on.
2173 */
2174 if (cpuctx->exclusive)
2175 return 0;
2176 /*
2177 * If this group is exclusive and there are already
2178 * events on the CPU, it can't go on.
2179 */
2180 if (event->attr.exclusive && cpuctx->active_oncpu)
2181 return 0;
2182 /*
2183 * Otherwise, try to add it if all previous groups were able
2184 * to go on.
2185 */
2186 return can_add_hw;
2187}
2188
2189static void add_event_to_ctx(struct perf_event *event,
2190 struct perf_event_context *ctx)
2191{
Stephane Eranian41587552011-01-03 18:20:01 +02002192 u64 tstamp = perf_event_time(event);
2193
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002194 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002195 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002196 event->tstamp_enabled = tstamp;
2197 event->tstamp_running = tstamp;
2198 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002199}
2200
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002201static void ctx_sched_out(struct perf_event_context *ctx,
2202 struct perf_cpu_context *cpuctx,
2203 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002204static void
2205ctx_sched_in(struct perf_event_context *ctx,
2206 struct perf_cpu_context *cpuctx,
2207 enum event_type_t event_type,
2208 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002209
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002210static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2211 struct perf_event_context *ctx)
2212{
2213 if (!cpuctx->task_ctx)
2214 return;
2215
2216 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2217 return;
2218
2219 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2220}
2221
Peter Zijlstradce58552011-04-09 21:17:46 +02002222static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2223 struct perf_event_context *ctx,
2224 struct task_struct *task)
2225{
2226 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2227 if (ctx)
2228 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2229 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2230 if (ctx)
2231 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2232}
2233
Peter Zijlstra3e349502016-01-08 10:01:18 +01002234static void ctx_resched(struct perf_cpu_context *cpuctx,
2235 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002236{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002237 perf_pmu_disable(cpuctx->ctx.pmu);
2238 if (task_ctx)
2239 task_ctx_sched_out(cpuctx, task_ctx);
2240 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2241 perf_event_sched_in(cpuctx, task_ctx, current);
2242 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002243}
2244
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002245/*
2246 * Cross CPU call to install and enable a performance event
2247 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002248 * Very similar to remote_function() + event_function() but cannot assume that
2249 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002250 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002251static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002252{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002253 struct perf_event *event = info;
2254 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002255 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002256 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002257 bool activate = true;
2258 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002259
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002260 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002261 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002262 raw_spin_lock(&ctx->lock);
2263 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002264
2265 /* If we're on the wrong CPU, try again */
2266 if (task_cpu(ctx->task) != smp_processor_id()) {
2267 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002268 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002269 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002270
Peter Zijlstra39a43642016-01-11 12:46:35 +01002271 /*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002272 * If we're on the right CPU, see if the task we target is
2273 * current, if not we don't have to activate the ctx, a future
2274 * context switch will do that for us.
Peter Zijlstra39a43642016-01-11 12:46:35 +01002275 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002276 if (ctx->task != current)
2277 activate = false;
2278 else
2279 WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2280
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002281 } else if (task_ctx) {
2282 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002283 }
2284
Peter Zijlstraa0963092016-02-24 18:45:50 +01002285 if (activate) {
2286 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2287 add_event_to_ctx(event, ctx);
2288 ctx_resched(cpuctx, task_ctx);
2289 } else {
2290 add_event_to_ctx(event, ctx);
2291 }
2292
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002293unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002294 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002295
Peter Zijlstraa0963092016-02-24 18:45:50 +01002296 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002297}
2298
2299/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002300 * Attach a performance event to a context.
2301 *
2302 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002303 */
2304static void
2305perf_install_in_context(struct perf_event_context *ctx,
2306 struct perf_event *event,
2307 int cpu)
2308{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002309 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002310
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002311 lockdep_assert_held(&ctx->mutex);
2312
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002313 if (event->cpu != -1)
2314 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002315
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002316 /*
2317 * Ensures that if we can observe event->ctx, both the event and ctx
2318 * will be 'complete'. See perf_iterate_sb_cpu().
2319 */
2320 smp_store_release(&event->ctx, ctx);
2321
Peter Zijlstraa0963092016-02-24 18:45:50 +01002322 if (!task) {
2323 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002324 return;
2325 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002326
Peter Zijlstraa0963092016-02-24 18:45:50 +01002327 /*
2328 * Should not happen, we validate the ctx is still alive before calling.
2329 */
2330 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2331 return;
2332
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002333 /*
2334 * Installing events is tricky because we cannot rely on ctx->is_active
2335 * to be set in case this is the nr_events 0 -> 1 transition.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002336 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002337again:
2338 /*
2339 * Cannot use task_function_call() because we need to run on the task's
2340 * CPU regardless of whether its current or not.
2341 */
2342 if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
2343 return;
2344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002345 raw_spin_lock_irq(&ctx->lock);
2346 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002347 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2348 /*
2349 * Cannot happen because we already checked above (which also
2350 * cannot happen), and we hold ctx->mutex, which serializes us
2351 * against perf_event_exit_task_context().
2352 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002353 raw_spin_unlock_irq(&ctx->lock);
2354 return;
2355 }
Peter Zijlstra39a43642016-01-11 12:46:35 +01002356 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstraa0963092016-02-24 18:45:50 +01002357 /*
2358 * Since !ctx->is_active doesn't mean anything, we must IPI
2359 * unconditionally.
2360 */
2361 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002362}
2363
2364/*
2365 * Put a event into inactive state and update time fields.
2366 * Enabling the leader of a group effectively enables all
2367 * the group members that aren't explicitly disabled, so we
2368 * have to update their ->tstamp_enabled also.
2369 * Note: this works for group members as well as group leaders
2370 * since the non-leader members' sibling_lists will be empty.
2371 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002372static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002373{
2374 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002375 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002376
2377 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002378 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002379 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002380 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2381 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002382 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002383}
2384
2385/*
2386 * Cross CPU call to enable a performance event
2387 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002388static void __perf_event_enable(struct perf_event *event,
2389 struct perf_cpu_context *cpuctx,
2390 struct perf_event_context *ctx,
2391 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002392{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002393 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002394 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002395
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002396 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2397 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002398 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002399
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002400 if (ctx->is_active)
2401 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2402
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002403 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002404
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002405 if (!ctx->is_active)
2406 return;
2407
Stephane Eraniane5d13672011-02-14 11:20:01 +02002408 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002409 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002410 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002411 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002412 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002413 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002414
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002415 /*
2416 * If the event is in a group and isn't the group leader,
2417 * then don't put it on unless the group is on.
2418 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002419 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2420 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002421 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002422 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002423
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002424 task_ctx = cpuctx->task_ctx;
2425 if (ctx->task)
2426 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002427
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002428 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002429}
2430
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002431/*
2432 * Enable a event.
2433 *
2434 * If event->ctx is a cloned context, callers must make sure that
2435 * every task struct that event->ctx->task could possibly point to
2436 * remains valid. This condition is satisfied when called through
2437 * perf_event_for_each_child or perf_event_for_each as described
2438 * for perf_event_disable.
2439 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002440static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002441{
2442 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002443
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002444 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002445 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2446 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002447 raw_spin_unlock_irq(&ctx->lock);
2448 return;
2449 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002450
2451 /*
2452 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002453 *
2454 * That way, if we see the event in error state below, we know that it
2455 * has gone back into error state, as distinct from the task having
2456 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002457 */
2458 if (event->state == PERF_EVENT_STATE_ERROR)
2459 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002460 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002461
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002462 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002463}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002464
2465/*
2466 * See perf_event_disable();
2467 */
2468void perf_event_enable(struct perf_event *event)
2469{
2470 struct perf_event_context *ctx;
2471
2472 ctx = perf_event_ctx_lock(event);
2473 _perf_event_enable(event);
2474 perf_event_ctx_unlock(event, ctx);
2475}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002476EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002477
Alexander Shishkin375637b2016-04-27 18:44:46 +03002478struct stop_event_data {
2479 struct perf_event *event;
2480 unsigned int restart;
2481};
2482
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002483static int __perf_event_stop(void *info)
2484{
Alexander Shishkin375637b2016-04-27 18:44:46 +03002485 struct stop_event_data *sd = info;
2486 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002487
Alexander Shishkin375637b2016-04-27 18:44:46 +03002488 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002489 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2490 return 0;
2491
2492 /* matches smp_wmb() in event_sched_in() */
2493 smp_rmb();
2494
2495 /*
2496 * There is a window with interrupts enabled before we get here,
2497 * so we need to check again lest we try to stop another CPU's event.
2498 */
2499 if (READ_ONCE(event->oncpu) != smp_processor_id())
2500 return -EAGAIN;
2501
2502 event->pmu->stop(event, PERF_EF_UPDATE);
2503
Alexander Shishkin375637b2016-04-27 18:44:46 +03002504 /*
2505 * May race with the actual stop (through perf_pmu_output_stop()),
2506 * but it is only used for events with AUX ring buffer, and such
2507 * events will refuse to restart because of rb::aux_mmap_count==0,
2508 * see comments in perf_aux_output_begin().
2509 *
2510 * Since this is happening on a event-local CPU, no trace is lost
2511 * while restarting.
2512 */
2513 if (sd->restart)
Will Deaconc9bbdd42016-08-15 11:42:45 +01002514 event->pmu->start(event, 0);
Alexander Shishkin375637b2016-04-27 18:44:46 +03002515
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002516 return 0;
2517}
2518
Alexander Shishkin767ae082016-09-06 16:23:49 +03002519static int perf_event_stop(struct perf_event *event, int restart)
Alexander Shishkin375637b2016-04-27 18:44:46 +03002520{
2521 struct stop_event_data sd = {
2522 .event = event,
Alexander Shishkin767ae082016-09-06 16:23:49 +03002523 .restart = restart,
Alexander Shishkin375637b2016-04-27 18:44:46 +03002524 };
2525 int ret = 0;
2526
2527 do {
2528 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2529 return 0;
2530
2531 /* matches smp_wmb() in event_sched_in() */
2532 smp_rmb();
2533
2534 /*
2535 * We only want to restart ACTIVE events, so if the event goes
2536 * inactive here (event->oncpu==-1), there's nothing more to do;
2537 * fall through with ret==-ENXIO.
2538 */
2539 ret = cpu_function_call(READ_ONCE(event->oncpu),
2540 __perf_event_stop, &sd);
2541 } while (ret == -EAGAIN);
2542
2543 return ret;
2544}
2545
2546/*
2547 * In order to contain the amount of racy and tricky in the address filter
2548 * configuration management, it is a two part process:
2549 *
2550 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2551 * we update the addresses of corresponding vmas in
2552 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2553 * (p2) when an event is scheduled in (pmu::add), it calls
2554 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2555 * if the generation has changed since the previous call.
2556 *
2557 * If (p1) happens while the event is active, we restart it to force (p2).
2558 *
2559 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2560 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2561 * ioctl;
2562 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2563 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2564 * for reading;
2565 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2566 * of exec.
2567 */
2568void perf_event_addr_filters_sync(struct perf_event *event)
2569{
2570 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2571
2572 if (!has_addr_filter(event))
2573 return;
2574
2575 raw_spin_lock(&ifh->lock);
2576 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2577 event->pmu->addr_filters_sync(event);
2578 event->hw.addr_filters_gen = event->addr_filters_gen;
2579 }
2580 raw_spin_unlock(&ifh->lock);
2581}
2582EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2583
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002584static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002585{
2586 /*
2587 * not supported on inherited events
2588 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002589 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002590 return -EINVAL;
2591
2592 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002593 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002594
2595 return 0;
2596}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002597
2598/*
2599 * See perf_event_disable()
2600 */
2601int perf_event_refresh(struct perf_event *event, int refresh)
2602{
2603 struct perf_event_context *ctx;
2604 int ret;
2605
2606 ctx = perf_event_ctx_lock(event);
2607 ret = _perf_event_refresh(event, refresh);
2608 perf_event_ctx_unlock(event, ctx);
2609
2610 return ret;
2611}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002612EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002613
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002614static void ctx_sched_out(struct perf_event_context *ctx,
2615 struct perf_cpu_context *cpuctx,
2616 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002617{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002618 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002619 struct perf_event *event;
2620
2621 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002622
Peter Zijlstra39a43642016-01-11 12:46:35 +01002623 if (likely(!ctx->nr_events)) {
2624 /*
2625 * See __perf_remove_from_context().
2626 */
2627 WARN_ON_ONCE(ctx->is_active);
2628 if (ctx->task)
2629 WARN_ON_ONCE(cpuctx->task_ctx);
2630 return;
2631 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002632
Peter Zijlstradb24d332011-04-09 21:17:45 +02002633 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002634 if (!(ctx->is_active & EVENT_ALL))
2635 ctx->is_active = 0;
2636
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002637 if (ctx->task) {
2638 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2639 if (!ctx->is_active)
2640 cpuctx->task_ctx = NULL;
2641 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002642
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002643 /*
2644 * Always update time if it was set; not only when it changes.
2645 * Otherwise we can 'forget' to update time for any but the last
2646 * context we sched out. For example:
2647 *
2648 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2649 * ctx_sched_out(.event_type = EVENT_PINNED)
2650 *
2651 * would only update time for the pinned events.
2652 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002653 if (is_active & EVENT_TIME) {
2654 /* update (and stop) ctx time */
2655 update_context_time(ctx);
2656 update_cgrp_time_from_cpuctx(cpuctx);
2657 }
2658
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002659 is_active ^= ctx->is_active; /* changed bits */
2660
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002661 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002662 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002663
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002664 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002665 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002666 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2667 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002668 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002669
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002670 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002671 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002672 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002673 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002674 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002675}
2676
2677/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002678 * Test whether two contexts are equivalent, i.e. whether they have both been
2679 * cloned from the same version of the same context.
2680 *
2681 * Equivalence is measured using a generation number in the context that is
2682 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2683 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002684 */
2685static int context_equiv(struct perf_event_context *ctx1,
2686 struct perf_event_context *ctx2)
2687{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002688 lockdep_assert_held(&ctx1->lock);
2689 lockdep_assert_held(&ctx2->lock);
2690
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002691 /* Pinning disables the swap optimization */
2692 if (ctx1->pin_count || ctx2->pin_count)
2693 return 0;
2694
2695 /* If ctx1 is the parent of ctx2 */
2696 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2697 return 1;
2698
2699 /* If ctx2 is the parent of ctx1 */
2700 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2701 return 1;
2702
2703 /*
2704 * If ctx1 and ctx2 have the same parent; we flatten the parent
2705 * hierarchy, see perf_event_init_context().
2706 */
2707 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2708 ctx1->parent_gen == ctx2->parent_gen)
2709 return 1;
2710
2711 /* Unmatched */
2712 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002713}
2714
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002715static void __perf_event_sync_stat(struct perf_event *event,
2716 struct perf_event *next_event)
2717{
2718 u64 value;
2719
2720 if (!event->attr.inherit_stat)
2721 return;
2722
2723 /*
2724 * Update the event value, we cannot use perf_event_read()
2725 * because we're in the middle of a context switch and have IRQs
2726 * disabled, which upsets smp_call_function_single(), however
2727 * we know the event must be on the current CPU, therefore we
2728 * don't need to use it.
2729 */
2730 switch (event->state) {
2731 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002732 event->pmu->read(event);
2733 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002734
2735 case PERF_EVENT_STATE_INACTIVE:
2736 update_event_times(event);
2737 break;
2738
2739 default:
2740 break;
2741 }
2742
2743 /*
2744 * In order to keep per-task stats reliable we need to flip the event
2745 * values when we flip the contexts.
2746 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002747 value = local64_read(&next_event->count);
2748 value = local64_xchg(&event->count, value);
2749 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002750
2751 swap(event->total_time_enabled, next_event->total_time_enabled);
2752 swap(event->total_time_running, next_event->total_time_running);
2753
2754 /*
2755 * Since we swizzled the values, update the user visible data too.
2756 */
2757 perf_event_update_userpage(event);
2758 perf_event_update_userpage(next_event);
2759}
2760
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002761static void perf_event_sync_stat(struct perf_event_context *ctx,
2762 struct perf_event_context *next_ctx)
2763{
2764 struct perf_event *event, *next_event;
2765
2766 if (!ctx->nr_stat)
2767 return;
2768
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002769 update_context_time(ctx);
2770
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002771 event = list_first_entry(&ctx->event_list,
2772 struct perf_event, event_entry);
2773
2774 next_event = list_first_entry(&next_ctx->event_list,
2775 struct perf_event, event_entry);
2776
2777 while (&event->event_entry != &ctx->event_list &&
2778 &next_event->event_entry != &next_ctx->event_list) {
2779
2780 __perf_event_sync_stat(event, next_event);
2781
2782 event = list_next_entry(event, event_entry);
2783 next_event = list_next_entry(next_event, event_entry);
2784 }
2785}
2786
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002787static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2788 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002789{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002790 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002791 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002792 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002793 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002794 int do_switch = 1;
2795
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002796 if (likely(!ctx))
2797 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002798
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002799 cpuctx = __get_cpu_context(ctx);
2800 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002801 return;
2802
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002803 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002804 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002805 if (!next_ctx)
2806 goto unlock;
2807
2808 parent = rcu_dereference(ctx->parent_ctx);
2809 next_parent = rcu_dereference(next_ctx->parent_ctx);
2810
2811 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002812 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002813 goto unlock;
2814
2815 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002816 /*
2817 * Looks like the two contexts are clones, so we might be
2818 * able to optimize the context switch. We lock both
2819 * contexts and check that they are clones under the
2820 * lock (including re-checking that neither has been
2821 * uncloned in the meantime). It doesn't matter which
2822 * order we take the locks because no other cpu could
2823 * be trying to lock both of these tasks.
2824 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002825 raw_spin_lock(&ctx->lock);
2826 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002827 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002828 WRITE_ONCE(ctx->task, next);
2829 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002830
2831 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2832
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002833 /*
2834 * RCU_INIT_POINTER here is safe because we've not
2835 * modified the ctx and the above modification of
2836 * ctx->task and ctx->task_ctx_data are immaterial
2837 * since those values are always verified under
2838 * ctx->lock which we're now holding.
2839 */
2840 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2841 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2842
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002843 do_switch = 0;
2844
2845 perf_event_sync_stat(ctx, next_ctx);
2846 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002847 raw_spin_unlock(&next_ctx->lock);
2848 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002850unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002851 rcu_read_unlock();
2852
2853 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002854 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002855 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002856 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002857 }
2858}
2859
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002860static DEFINE_PER_CPU(struct list_head, sched_cb_list);
2861
Yan, Zhengba532502014-11-04 21:55:58 -05002862void perf_sched_cb_dec(struct pmu *pmu)
2863{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002864 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2865
Yan, Zhengba532502014-11-04 21:55:58 -05002866 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002867
2868 if (!--cpuctx->sched_cb_usage)
2869 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05002870}
2871
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002872
Yan, Zhengba532502014-11-04 21:55:58 -05002873void perf_sched_cb_inc(struct pmu *pmu)
2874{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002875 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2876
2877 if (!cpuctx->sched_cb_usage++)
2878 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
2879
Yan, Zhengba532502014-11-04 21:55:58 -05002880 this_cpu_inc(perf_sched_cb_usages);
2881}
2882
2883/*
2884 * This function provides the context switch callback to the lower code
2885 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02002886 *
2887 * This callback is relevant even to per-cpu events; for example multi event
2888 * PEBS requires this to provide PID/TID information. This requires we flush
2889 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05002890 */
2891static void perf_pmu_sched_task(struct task_struct *prev,
2892 struct task_struct *next,
2893 bool sched_in)
2894{
2895 struct perf_cpu_context *cpuctx;
2896 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05002897
2898 if (prev == next)
2899 return;
2900
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002901 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
2902 pmu = cpuctx->unique_pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05002903
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002904 if (WARN_ON_ONCE(!pmu->sched_task))
2905 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05002906
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002907 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2908 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05002909
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002910 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05002911
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002912 perf_pmu_enable(pmu);
2913 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05002914 }
Yan, Zhengba532502014-11-04 21:55:58 -05002915}
2916
Adrian Hunter45ac1402015-07-21 12:44:02 +03002917static void perf_event_switch(struct task_struct *task,
2918 struct task_struct *next_prev, bool sched_in);
2919
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002920#define for_each_task_context_nr(ctxn) \
2921 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2922
2923/*
2924 * Called from scheduler to remove the events of the current task,
2925 * with interrupts disabled.
2926 *
2927 * We stop each event and update the event value in event->count.
2928 *
2929 * This does not protect us against NMI, but disable()
2930 * sets the disabled bit in the control field of event _before_
2931 * accessing the event control register. If a NMI hits, then it will
2932 * not restart the event.
2933 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002934void __perf_event_task_sched_out(struct task_struct *task,
2935 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002936{
2937 int ctxn;
2938
Yan, Zhengba532502014-11-04 21:55:58 -05002939 if (__this_cpu_read(perf_sched_cb_usages))
2940 perf_pmu_sched_task(task, next, false);
2941
Adrian Hunter45ac1402015-07-21 12:44:02 +03002942 if (atomic_read(&nr_switch_events))
2943 perf_event_switch(task, next, false);
2944
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002945 for_each_task_context_nr(ctxn)
2946 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002947
2948 /*
2949 * if cgroup events exist on this CPU, then we need
2950 * to check if we have to switch out PMU state.
2951 * cgroup event are system-wide mode only
2952 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002953 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002954 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002955}
2956
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002957/*
2958 * Called with IRQs disabled
2959 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002960static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2961 enum event_type_t event_type)
2962{
2963 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002964}
2965
2966static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002967ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002968 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002969{
2970 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002971
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002972 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2973 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002974 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002975 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002976 continue;
2977
Stephane Eraniane5d13672011-02-14 11:20:01 +02002978 /* may need to reset tstamp_enabled */
2979 if (is_cgroup_event(event))
2980 perf_cgroup_mark_enabled(event, ctx);
2981
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002982 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002983 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002984
2985 /*
2986 * If this pinned group hasn't been scheduled,
2987 * put it in error state.
2988 */
2989 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2990 update_group_times(event);
2991 event->state = PERF_EVENT_STATE_ERROR;
2992 }
2993 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002994}
2995
2996static void
2997ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002998 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002999{
3000 struct perf_event *event;
3001 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003002
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003003 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
3004 /* Ignore events in OFF or ERROR state */
3005 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003006 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007 /*
3008 * Listen to the 'cpu' scheduling filter constraint
3009 * of events:
3010 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02003011 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003012 continue;
3013
Stephane Eraniane5d13672011-02-14 11:20:01 +02003014 /* may need to reset tstamp_enabled */
3015 if (is_cgroup_event(event))
3016 perf_cgroup_mark_enabled(event, ctx);
3017
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003018 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01003019 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003020 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003021 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003022 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003023}
3024
3025static void
3026ctx_sched_in(struct perf_event_context *ctx,
3027 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003028 enum event_type_t event_type,
3029 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003030{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003031 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003032 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003033
Peter Zijlstrac994d612016-01-08 09:20:23 +01003034 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003035
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003036 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003037 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003038
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003039 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003040 if (ctx->task) {
3041 if (!is_active)
3042 cpuctx->task_ctx = ctx;
3043 else
3044 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3045 }
3046
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003047 is_active ^= ctx->is_active; /* changed bits */
3048
3049 if (is_active & EVENT_TIME) {
3050 /* start ctx time */
3051 now = perf_clock();
3052 ctx->timestamp = now;
3053 perf_cgroup_set_timestamp(task, ctx);
3054 }
3055
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003056 /*
3057 * First go through the list and put on any pinned groups
3058 * in order to give them the best chance of going on.
3059 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003060 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003061 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003062
3063 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003064 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003065 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003066}
3067
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003068static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003069 enum event_type_t event_type,
3070 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003071{
3072 struct perf_event_context *ctx = &cpuctx->ctx;
3073
Stephane Eraniane5d13672011-02-14 11:20:01 +02003074 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003075}
3076
Stephane Eraniane5d13672011-02-14 11:20:01 +02003077static void perf_event_context_sched_in(struct perf_event_context *ctx,
3078 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003079{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003080 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003081
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003082 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003083 if (cpuctx->task_ctx == ctx)
3084 return;
3085
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003086 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003087 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003088 /*
3089 * We want to keep the following priority order:
3090 * cpu pinned (that don't need to move), task pinned,
3091 * cpu flexible, task flexible.
3092 */
3093 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003094 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003095 perf_pmu_enable(ctx->pmu);
3096 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003097}
3098
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003099/*
3100 * Called from scheduler to add the events of the current task
3101 * with interrupts disabled.
3102 *
3103 * We restore the event value and then enable it.
3104 *
3105 * This does not protect us against NMI, but enable()
3106 * sets the enabled bit in the control field of event _before_
3107 * accessing the event control register. If a NMI hits, then it will
3108 * keep the event running.
3109 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003110void __perf_event_task_sched_in(struct task_struct *prev,
3111 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003112{
3113 struct perf_event_context *ctx;
3114 int ctxn;
3115
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003116 /*
3117 * If cgroup events exist on this CPU, then we need to check if we have
3118 * to switch in PMU state; cgroup event are system-wide mode only.
3119 *
3120 * Since cgroup events are CPU events, we must schedule these in before
3121 * we schedule in the task events.
3122 */
3123 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3124 perf_cgroup_sched_in(prev, task);
3125
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003126 for_each_task_context_nr(ctxn) {
3127 ctx = task->perf_event_ctxp[ctxn];
3128 if (likely(!ctx))
3129 continue;
3130
Stephane Eraniane5d13672011-02-14 11:20:01 +02003131 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003132 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003133
Adrian Hunter45ac1402015-07-21 12:44:02 +03003134 if (atomic_read(&nr_switch_events))
3135 perf_event_switch(task, prev, true);
3136
Yan, Zhengba532502014-11-04 21:55:58 -05003137 if (__this_cpu_read(perf_sched_cb_usages))
3138 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003139}
3140
Peter Zijlstraabd50712010-01-26 18:50:16 +01003141static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3142{
3143 u64 frequency = event->attr.sample_freq;
3144 u64 sec = NSEC_PER_SEC;
3145 u64 divisor, dividend;
3146
3147 int count_fls, nsec_fls, frequency_fls, sec_fls;
3148
3149 count_fls = fls64(count);
3150 nsec_fls = fls64(nsec);
3151 frequency_fls = fls64(frequency);
3152 sec_fls = 30;
3153
3154 /*
3155 * We got @count in @nsec, with a target of sample_freq HZ
3156 * the target period becomes:
3157 *
3158 * @count * 10^9
3159 * period = -------------------
3160 * @nsec * sample_freq
3161 *
3162 */
3163
3164 /*
3165 * Reduce accuracy by one bit such that @a and @b converge
3166 * to a similar magnitude.
3167 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003168#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003169do { \
3170 if (a##_fls > b##_fls) { \
3171 a >>= 1; \
3172 a##_fls--; \
3173 } else { \
3174 b >>= 1; \
3175 b##_fls--; \
3176 } \
3177} while (0)
3178
3179 /*
3180 * Reduce accuracy until either term fits in a u64, then proceed with
3181 * the other, so that finally we can do a u64/u64 division.
3182 */
3183 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3184 REDUCE_FLS(nsec, frequency);
3185 REDUCE_FLS(sec, count);
3186 }
3187
3188 if (count_fls + sec_fls > 64) {
3189 divisor = nsec * frequency;
3190
3191 while (count_fls + sec_fls > 64) {
3192 REDUCE_FLS(count, sec);
3193 divisor >>= 1;
3194 }
3195
3196 dividend = count * sec;
3197 } else {
3198 dividend = count * sec;
3199
3200 while (nsec_fls + frequency_fls > 64) {
3201 REDUCE_FLS(nsec, frequency);
3202 dividend >>= 1;
3203 }
3204
3205 divisor = nsec * frequency;
3206 }
3207
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003208 if (!divisor)
3209 return dividend;
3210
Peter Zijlstraabd50712010-01-26 18:50:16 +01003211 return div64_u64(dividend, divisor);
3212}
3213
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003214static DEFINE_PER_CPU(int, perf_throttled_count);
3215static DEFINE_PER_CPU(u64, perf_throttled_seq);
3216
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003217static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003218{
3219 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003220 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003221 s64 delta;
3222
Peter Zijlstraabd50712010-01-26 18:50:16 +01003223 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224
3225 delta = (s64)(period - hwc->sample_period);
3226 delta = (delta + 7) / 8; /* low pass filter */
3227
3228 sample_period = hwc->sample_period + delta;
3229
3230 if (!sample_period)
3231 sample_period = 1;
3232
3233 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003234
Peter Zijlstrae7850592010-05-21 14:43:08 +02003235 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003236 if (disable)
3237 event->pmu->stop(event, PERF_EF_UPDATE);
3238
Peter Zijlstrae7850592010-05-21 14:43:08 +02003239 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003240
3241 if (disable)
3242 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003243 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003244}
3245
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003246/*
3247 * combine freq adjustment with unthrottling to avoid two passes over the
3248 * events. At the same time, make sure, having freq events does not change
3249 * the rate of unthrottling as that would introduce bias.
3250 */
3251static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3252 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253{
3254 struct perf_event *event;
3255 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003256 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003257 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003258
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003259 /*
3260 * only need to iterate over all events iff:
3261 * - context have events in frequency mode (needs freq adjust)
3262 * - there are events to unthrottle on this cpu
3263 */
3264 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003265 return;
3266
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003267 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003268 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003269
Paul Mackerras03541f82009-10-14 16:58:03 +11003270 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003271 if (event->state != PERF_EVENT_STATE_ACTIVE)
3272 continue;
3273
Stephane Eranian5632ab12011-01-03 18:20:01 +02003274 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003275 continue;
3276
Alexander Shishkin44377272013-12-16 14:17:36 +02003277 perf_pmu_disable(event->pmu);
3278
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003279 hwc = &event->hw;
3280
Jiri Olsaae23bff2013-08-24 16:45:54 +02003281 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003282 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003283 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003284 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003285 }
3286
3287 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003288 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003289
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003290 /*
3291 * stop the event and update event->count
3292 */
3293 event->pmu->stop(event, PERF_EF_UPDATE);
3294
Peter Zijlstrae7850592010-05-21 14:43:08 +02003295 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003296 delta = now - hwc->freq_count_stamp;
3297 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003298
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003299 /*
3300 * restart the event
3301 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003302 * we have stopped the event so tell that
3303 * to perf_adjust_period() to avoid stopping it
3304 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003305 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003306 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003307 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003308
3309 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003310 next:
3311 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003312 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003313
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003314 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003315 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003316}
3317
3318/*
3319 * Round-robin a context's events:
3320 */
3321static void rotate_ctx(struct perf_event_context *ctx)
3322{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003323 /*
3324 * Rotate the first entry last of non-pinned groups. Rotation might be
3325 * disabled by the inheritance code.
3326 */
3327 if (!ctx->rotate_disable)
3328 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003329}
3330
Stephane Eranian9e630202013-04-03 14:21:33 +02003331static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003333 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003334 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003335
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003336 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003337 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3338 rotate = 1;
3339 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003341 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003342 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003343 if (ctx->nr_events != ctx->nr_active)
3344 rotate = 1;
3345 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003346
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003347 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003348 goto done;
3349
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003350 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003351 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003352
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003353 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3354 if (ctx)
3355 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003356
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003357 rotate_ctx(&cpuctx->ctx);
3358 if (ctx)
3359 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003360
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003361 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003362
3363 perf_pmu_enable(cpuctx->ctx.pmu);
3364 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003365done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003366
3367 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003368}
3369
3370void perf_event_task_tick(void)
3371{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003372 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3373 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003374 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003375
3376 WARN_ON(!irqs_disabled());
3377
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003378 __this_cpu_inc(perf_throttled_seq);
3379 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003380 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003381
Mark Rutland2fde4f92015-01-07 15:01:54 +00003382 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003383 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384}
3385
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003386static int event_enable_on_exec(struct perf_event *event,
3387 struct perf_event_context *ctx)
3388{
3389 if (!event->attr.enable_on_exec)
3390 return 0;
3391
3392 event->attr.enable_on_exec = 0;
3393 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3394 return 0;
3395
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003396 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003397
3398 return 1;
3399}
3400
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003401/*
3402 * Enable all of a task's events that have been marked enable-on-exec.
3403 * This expects task == current.
3404 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003405static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003406{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003407 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003408 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003409 struct perf_event *event;
3410 unsigned long flags;
3411 int enabled = 0;
3412
3413 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003414 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003415 if (!ctx || !ctx->nr_events)
3416 goto out;
3417
Peter Zijlstra3e349502016-01-08 10:01:18 +01003418 cpuctx = __get_cpu_context(ctx);
3419 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003420 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003421 list_for_each_entry(event, &ctx->event_list, event_entry)
3422 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003423
3424 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003425 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003426 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003427 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003428 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003429 ctx_resched(cpuctx, ctx);
3430 }
3431 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003432
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003433out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003434 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003435
3436 if (clone_ctx)
3437 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003438}
3439
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003440struct perf_read_data {
3441 struct perf_event *event;
3442 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003443 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003444};
3445
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003446static int find_cpu_to_read(struct perf_event *event, int local_cpu)
3447{
3448 int event_cpu = event->oncpu;
3449 u16 local_pkg, event_pkg;
3450
3451 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
3452 event_pkg = topology_physical_package_id(event_cpu);
3453 local_pkg = topology_physical_package_id(local_cpu);
3454
3455 if (event_pkg == local_pkg)
3456 return local_cpu;
3457 }
3458
3459 return event_cpu;
3460}
3461
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003462/*
3463 * Cross CPU call to read the hardware event
3464 */
3465static void __perf_event_read(void *info)
3466{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003467 struct perf_read_data *data = info;
3468 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003469 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003470 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003471 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003472
3473 /*
3474 * If this is a task context, we need to check whether it is
3475 * the current task context of this cpu. If not it has been
3476 * scheduled out before the smp call arrived. In that case
3477 * event->count would have been updated to a recent sample
3478 * when the event was scheduled out.
3479 */
3480 if (ctx->task && cpuctx->task_ctx != ctx)
3481 return;
3482
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003483 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003484 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003485 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003486 update_cgrp_time_from_event(event);
3487 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003488
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003489 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003490 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003491 goto unlock;
3492
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003493 if (!data->group) {
3494 pmu->read(event);
3495 data->ret = 0;
3496 goto unlock;
3497 }
3498
3499 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3500
3501 pmu->read(event);
3502
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003503 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3504 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003505 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3506 /*
3507 * Use sibling's PMU rather than @event's since
3508 * sibling could be on different (eg: software) PMU.
3509 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003510 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003511 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003512 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003513
3514 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003515
3516unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003517 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003518}
3519
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003520static inline u64 perf_event_count(struct perf_event *event)
3521{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003522 if (event->pmu->count)
3523 return event->pmu->count(event);
3524
3525 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003526}
3527
Kaixu Xiaffe86902015-08-06 07:02:32 +00003528/*
3529 * NMI-safe method to read a local event, that is an event that
3530 * is:
3531 * - either for the current task, or for this CPU
3532 * - does not have inherit set, for inherited task events
3533 * will not be local and we cannot read them atomically
3534 * - must not have a pmu::count method
3535 */
3536u64 perf_event_read_local(struct perf_event *event)
3537{
3538 unsigned long flags;
3539 u64 val;
3540
3541 /*
3542 * Disabling interrupts avoids all counter scheduling (context
3543 * switches, timer based rotation and IPIs).
3544 */
3545 local_irq_save(flags);
3546
3547 /* If this is a per-task event, it must be for current */
3548 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3549 event->hw.target != current);
3550
3551 /* If this is a per-CPU event, it must be for this CPU */
3552 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3553 event->cpu != smp_processor_id());
3554
3555 /*
3556 * It must not be an event with inherit set, we cannot read
3557 * all child counters from atomic context.
3558 */
3559 WARN_ON_ONCE(event->attr.inherit);
3560
3561 /*
3562 * It must not have a pmu::count method, those are not
3563 * NMI safe.
3564 */
3565 WARN_ON_ONCE(event->pmu->count);
3566
3567 /*
3568 * If the event is currently on this CPU, its either a per-task event,
3569 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3570 * oncpu == -1).
3571 */
3572 if (event->oncpu == smp_processor_id())
3573 event->pmu->read(event);
3574
3575 val = local64_read(&event->count);
3576 local_irq_restore(flags);
3577
3578 return val;
3579}
3580
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003581static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003582{
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003583 int ret = 0, cpu_to_read, local_cpu;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003584
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003585 /*
3586 * If event is enabled and currently active on a CPU, update the
3587 * value in the event structure:
3588 */
3589 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003590 struct perf_read_data data = {
3591 .event = event,
3592 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003593 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003594 };
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003595
3596 local_cpu = get_cpu();
3597 cpu_to_read = find_cpu_to_read(event, local_cpu);
3598 put_cpu();
3599
Peter Zijlstra58763142016-08-30 10:15:03 +02003600 /*
3601 * Purposely ignore the smp_call_function_single() return
3602 * value.
3603 *
3604 * If event->oncpu isn't a valid CPU it means the event got
3605 * scheduled out and that will have updated the event count.
3606 *
3607 * Therefore, either way, we'll have an up-to-date event count
3608 * after this.
3609 */
Ingo Molnar2cc53842016-09-05 12:09:59 +02003610 (void)smp_call_function_single(cpu_to_read, __perf_event_read, &data, 1);
Peter Zijlstra58763142016-08-30 10:15:03 +02003611 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003612 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003613 struct perf_event_context *ctx = event->ctx;
3614 unsigned long flags;
3615
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003616 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003617 /*
3618 * may read while context is not active
3619 * (e.g., thread is blocked), in that case
3620 * we cannot update context time
3621 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003622 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003623 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003624 update_cgrp_time_from_event(event);
3625 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003626 if (group)
3627 update_group_times(event);
3628 else
3629 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003630 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003631 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003632
3633 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003634}
3635
3636/*
3637 * Initialize the perf_event context in a task_struct:
3638 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003639static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003640{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003641 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003642 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003643 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003644 INIT_LIST_HEAD(&ctx->pinned_groups);
3645 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003646 INIT_LIST_HEAD(&ctx->event_list);
3647 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003648}
3649
Peter Zijlstraeb184472010-09-07 15:55:13 +02003650static struct perf_event_context *
3651alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003652{
3653 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003654
3655 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3656 if (!ctx)
3657 return NULL;
3658
3659 __perf_event_init_context(ctx);
3660 if (task) {
3661 ctx->task = task;
3662 get_task_struct(task);
3663 }
3664 ctx->pmu = pmu;
3665
3666 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003667}
3668
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003669static struct task_struct *
3670find_lively_task_by_vpid(pid_t vpid)
3671{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003672 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003673
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003674 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003675 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003676 task = current;
3677 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003678 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003679 if (task)
3680 get_task_struct(task);
3681 rcu_read_unlock();
3682
3683 if (!task)
3684 return ERR_PTR(-ESRCH);
3685
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003686 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003687}
3688
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003689/*
3690 * Returns a matching context with refcount and pincount.
3691 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003692static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003693find_get_context(struct pmu *pmu, struct task_struct *task,
3694 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003695{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003696 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003697 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003698 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003699 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003700 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003701 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003702
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003703 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003704 /* Must be root to operate on a CPU event: */
3705 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3706 return ERR_PTR(-EACCES);
3707
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003708 /*
3709 * We could be clever and allow to attach a event to an
3710 * offline CPU and activate it when the CPU comes up, but
3711 * that's for later.
3712 */
3713 if (!cpu_online(cpu))
3714 return ERR_PTR(-ENODEV);
3715
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003716 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717 ctx = &cpuctx->ctx;
3718 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003719 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003720
3721 return ctx;
3722 }
3723
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003724 err = -EINVAL;
3725 ctxn = pmu->task_ctx_nr;
3726 if (ctxn < 0)
3727 goto errout;
3728
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003729 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3730 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3731 if (!task_ctx_data) {
3732 err = -ENOMEM;
3733 goto errout;
3734 }
3735 }
3736
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003737retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003738 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003739 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003740 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003741 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003742
3743 if (task_ctx_data && !ctx->task_ctx_data) {
3744 ctx->task_ctx_data = task_ctx_data;
3745 task_ctx_data = NULL;
3746 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003747 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003748
3749 if (clone_ctx)
3750 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003751 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003752 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003753 err = -ENOMEM;
3754 if (!ctx)
3755 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003756
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003757 if (task_ctx_data) {
3758 ctx->task_ctx_data = task_ctx_data;
3759 task_ctx_data = NULL;
3760 }
3761
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003762 err = 0;
3763 mutex_lock(&task->perf_event_mutex);
3764 /*
3765 * If it has already passed perf_event_exit_task().
3766 * we must see PF_EXITING, it takes this mutex too.
3767 */
3768 if (task->flags & PF_EXITING)
3769 err = -ESRCH;
3770 else if (task->perf_event_ctxp[ctxn])
3771 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003772 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003773 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003774 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003775 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003776 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003777 mutex_unlock(&task->perf_event_mutex);
3778
3779 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003780 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003781
3782 if (err == -EAGAIN)
3783 goto retry;
3784 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003785 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003786 }
3787
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003788 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003789 return ctx;
3790
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003791errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003792 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003793 return ERR_PTR(err);
3794}
3795
Li Zefan6fb29152009-10-15 11:21:42 +08003796static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003797static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003798
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003799static void free_event_rcu(struct rcu_head *head)
3800{
3801 struct perf_event *event;
3802
3803 event = container_of(head, struct perf_event, rcu_head);
3804 if (event->ns)
3805 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003806 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003807 kfree(event);
3808}
3809
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003810static void ring_buffer_attach(struct perf_event *event,
3811 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003812
Kan Liangf2fb6be2016-03-23 11:24:37 -07003813static void detach_sb_event(struct perf_event *event)
3814{
3815 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
3816
3817 raw_spin_lock(&pel->lock);
3818 list_del_rcu(&event->sb_list);
3819 raw_spin_unlock(&pel->lock);
3820}
3821
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003822static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07003823{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003824 struct perf_event_attr *attr = &event->attr;
3825
Kan Liangf2fb6be2016-03-23 11:24:37 -07003826 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003827 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003828
3829 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003830 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003831
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003832 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
3833 attr->comm || attr->comm_exec ||
3834 attr->task ||
3835 attr->context_switch)
3836 return true;
3837 return false;
3838}
3839
3840static void unaccount_pmu_sb_event(struct perf_event *event)
3841{
3842 if (is_sb_event(event))
3843 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003844}
3845
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003846static void unaccount_event_cpu(struct perf_event *event, int cpu)
3847{
3848 if (event->parent)
3849 return;
3850
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003851 if (is_cgroup_event(event))
3852 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3853}
3854
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003855#ifdef CONFIG_NO_HZ_FULL
3856static DEFINE_SPINLOCK(nr_freq_lock);
3857#endif
3858
3859static void unaccount_freq_event_nohz(void)
3860{
3861#ifdef CONFIG_NO_HZ_FULL
3862 spin_lock(&nr_freq_lock);
3863 if (atomic_dec_and_test(&nr_freq_events))
3864 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3865 spin_unlock(&nr_freq_lock);
3866#endif
3867}
3868
3869static void unaccount_freq_event(void)
3870{
3871 if (tick_nohz_full_enabled())
3872 unaccount_freq_event_nohz();
3873 else
3874 atomic_dec(&nr_freq_events);
3875}
3876
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003877static void unaccount_event(struct perf_event *event)
3878{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003879 bool dec = false;
3880
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003881 if (event->parent)
3882 return;
3883
3884 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003885 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003886 if (event->attr.mmap || event->attr.mmap_data)
3887 atomic_dec(&nr_mmap_events);
3888 if (event->attr.comm)
3889 atomic_dec(&nr_comm_events);
3890 if (event->attr.task)
3891 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003892 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003893 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003894 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003895 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003896 atomic_dec(&nr_switch_events);
3897 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003898 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003899 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003900 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003901 dec = true;
3902
Peter Zijlstra9107c892016-02-24 18:45:45 +01003903 if (dec) {
3904 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3905 schedule_delayed_work(&perf_sched_work, HZ);
3906 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003907
3908 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003909
3910 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003911}
3912
Peter Zijlstra9107c892016-02-24 18:45:45 +01003913static void perf_sched_delayed(struct work_struct *work)
3914{
3915 mutex_lock(&perf_sched_mutex);
3916 if (atomic_dec_and_test(&perf_sched_count))
3917 static_branch_disable(&perf_sched_events);
3918 mutex_unlock(&perf_sched_mutex);
3919}
3920
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003921/*
3922 * The following implement mutual exclusion of events on "exclusive" pmus
3923 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3924 * at a time, so we disallow creating events that might conflict, namely:
3925 *
3926 * 1) cpu-wide events in the presence of per-task events,
3927 * 2) per-task events in the presence of cpu-wide events,
3928 * 3) two matching events on the same context.
3929 *
3930 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003931 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003932 */
3933static int exclusive_event_init(struct perf_event *event)
3934{
3935 struct pmu *pmu = event->pmu;
3936
3937 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3938 return 0;
3939
3940 /*
3941 * Prevent co-existence of per-task and cpu-wide events on the
3942 * same exclusive pmu.
3943 *
3944 * Negative pmu::exclusive_cnt means there are cpu-wide
3945 * events on this "exclusive" pmu, positive means there are
3946 * per-task events.
3947 *
3948 * Since this is called in perf_event_alloc() path, event::ctx
3949 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3950 * to mean "per-task event", because unlike other attach states it
3951 * never gets cleared.
3952 */
3953 if (event->attach_state & PERF_ATTACH_TASK) {
3954 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3955 return -EBUSY;
3956 } else {
3957 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3958 return -EBUSY;
3959 }
3960
3961 return 0;
3962}
3963
3964static void exclusive_event_destroy(struct perf_event *event)
3965{
3966 struct pmu *pmu = event->pmu;
3967
3968 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3969 return;
3970
3971 /* see comment in exclusive_event_init() */
3972 if (event->attach_state & PERF_ATTACH_TASK)
3973 atomic_dec(&pmu->exclusive_cnt);
3974 else
3975 atomic_inc(&pmu->exclusive_cnt);
3976}
3977
3978static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3979{
Alexander Shishkin3bf62152016-09-20 18:48:11 +03003980 if ((e1->pmu == e2->pmu) &&
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003981 (e1->cpu == e2->cpu ||
3982 e1->cpu == -1 ||
3983 e2->cpu == -1))
3984 return true;
3985 return false;
3986}
3987
3988/* Called under the same ctx::mutex as perf_install_in_context() */
3989static bool exclusive_event_installable(struct perf_event *event,
3990 struct perf_event_context *ctx)
3991{
3992 struct perf_event *iter_event;
3993 struct pmu *pmu = event->pmu;
3994
3995 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3996 return true;
3997
3998 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3999 if (exclusive_event_match(iter_event, event))
4000 return false;
4001 }
4002
4003 return true;
4004}
4005
Alexander Shishkin375637b2016-04-27 18:44:46 +03004006static void perf_addr_filters_splice(struct perf_event *event,
4007 struct list_head *head);
4008
Peter Zijlstra683ede42014-05-05 12:11:24 +02004009static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004010{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004011 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004012
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004013 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004014
Frederic Weisbecker76369132011-05-19 19:55:04 +02004015 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004016 /*
4017 * Can happen when we close an event with re-directed output.
4018 *
4019 * Since we have a 0 refcount, perf_mmap_close() will skip
4020 * over us; possibly making our ring_buffer_put() the last.
4021 */
4022 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004023 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004024 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004025 }
4026
Stephane Eraniane5d13672011-02-14 11:20:01 +02004027 if (is_cgroup_event(event))
4028 perf_detach_cgroup(event);
4029
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004030 if (!event->parent) {
4031 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
4032 put_callchain_buffers();
4033 }
4034
4035 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03004036 perf_addr_filters_splice(event, NULL);
4037 kfree(event->addr_filters_offs);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004038
4039 if (event->destroy)
4040 event->destroy(event);
4041
4042 if (event->ctx)
4043 put_ctx(event->ctx);
4044
Alexander Shishkin62a92c82016-06-07 15:44:15 +03004045 exclusive_event_destroy(event);
4046 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004047
4048 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004049}
4050
Peter Zijlstra683ede42014-05-05 12:11:24 +02004051/*
4052 * Used to free events which have a known refcount of 1, such as in error paths
4053 * where the event isn't exposed yet and inherited events.
4054 */
4055static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004056{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004057 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4058 "unexpected event refcount: %ld; ptr=%p\n",
4059 atomic_long_read(&event->refcount), event)) {
4060 /* leak to avoid use-after-free */
4061 return;
4062 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004063
Peter Zijlstra683ede42014-05-05 12:11:24 +02004064 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004065}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004066
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004067/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004068 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004069 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004070static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004071{
Peter Zijlstra88821352010-11-09 19:01:43 +01004072 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004073
Peter Zijlstra88821352010-11-09 19:01:43 +01004074 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004075 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004076 * Matches the smp_store_release() in perf_event_exit_task(). If we
4077 * observe !owner it means the list deletion is complete and we can
4078 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004079 * owner->perf_event_mutex.
4080 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004081 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004082 if (owner) {
4083 /*
4084 * Since delayed_put_task_struct() also drops the last
4085 * task reference we can safely take a new reference
4086 * while holding the rcu_read_lock().
4087 */
4088 get_task_struct(owner);
4089 }
4090 rcu_read_unlock();
4091
4092 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004093 /*
4094 * If we're here through perf_event_exit_task() we're already
4095 * holding ctx->mutex which would be an inversion wrt. the
4096 * normal lock order.
4097 *
4098 * However we can safely take this lock because its the child
4099 * ctx->mutex.
4100 */
4101 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4102
Peter Zijlstra88821352010-11-09 19:01:43 +01004103 /*
4104 * We have to re-check the event->owner field, if it is cleared
4105 * we raced with perf_event_exit_task(), acquiring the mutex
4106 * ensured they're done, and we can proceed with freeing the
4107 * event.
4108 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004109 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004110 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004111 smp_store_release(&event->owner, NULL);
4112 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004113 mutex_unlock(&owner->perf_event_mutex);
4114 put_task_struct(owner);
4115 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004116}
4117
Jiri Olsaf8697762014-08-01 14:33:01 +02004118static void put_event(struct perf_event *event)
4119{
Jiri Olsaf8697762014-08-01 14:33:01 +02004120 if (!atomic_long_dec_and_test(&event->refcount))
4121 return;
4122
Peter Zijlstra683ede42014-05-05 12:11:24 +02004123 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004124}
4125
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004126/*
4127 * Kill an event dead; while event:refcount will preserve the event
4128 * object, it will not preserve its functionality. Once the last 'user'
4129 * gives up the object, we'll destroy the thing.
4130 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004131int perf_event_release_kernel(struct perf_event *event)
4132{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004133 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004134 struct perf_event *child, *tmp;
4135
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004136 /*
4137 * If we got here through err_file: fput(event_file); we will not have
4138 * attached to a context yet.
4139 */
4140 if (!ctx) {
4141 WARN_ON_ONCE(event->attach_state &
4142 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4143 goto no_ctx;
4144 }
4145
Peter Zijlstra88821352010-11-09 19:01:43 +01004146 if (!is_kernel_event(event))
4147 perf_remove_from_owner(event);
4148
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004149 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004150 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004151 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004152
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004153 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004154 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004155 * Mark this even as STATE_DEAD, there is no external reference to it
4156 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004157 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004158 * Anybody acquiring event->child_mutex after the below loop _must_
4159 * also see this, most importantly inherit_event() which will avoid
4160 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004161 *
4162 * Thus this guarantees that we will in fact observe and kill _ALL_
4163 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004164 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004165 event->state = PERF_EVENT_STATE_DEAD;
4166 raw_spin_unlock_irq(&ctx->lock);
4167
4168 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004169
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004170again:
4171 mutex_lock(&event->child_mutex);
4172 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004173
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004174 /*
4175 * Cannot change, child events are not migrated, see the
4176 * comment with perf_event_ctx_lock_nested().
4177 */
4178 ctx = lockless_dereference(child->ctx);
4179 /*
4180 * Since child_mutex nests inside ctx::mutex, we must jump
4181 * through hoops. We start by grabbing a reference on the ctx.
4182 *
4183 * Since the event cannot get freed while we hold the
4184 * child_mutex, the context must also exist and have a !0
4185 * reference count.
4186 */
4187 get_ctx(ctx);
4188
4189 /*
4190 * Now that we have a ctx ref, we can drop child_mutex, and
4191 * acquire ctx::mutex without fear of it going away. Then we
4192 * can re-acquire child_mutex.
4193 */
4194 mutex_unlock(&event->child_mutex);
4195 mutex_lock(&ctx->mutex);
4196 mutex_lock(&event->child_mutex);
4197
4198 /*
4199 * Now that we hold ctx::mutex and child_mutex, revalidate our
4200 * state, if child is still the first entry, it didn't get freed
4201 * and we can continue doing so.
4202 */
4203 tmp = list_first_entry_or_null(&event->child_list,
4204 struct perf_event, child_list);
4205 if (tmp == child) {
4206 perf_remove_from_context(child, DETACH_GROUP);
4207 list_del(&child->child_list);
4208 free_event(child);
4209 /*
4210 * This matches the refcount bump in inherit_event();
4211 * this can't be the last reference.
4212 */
4213 put_event(event);
4214 }
4215
4216 mutex_unlock(&event->child_mutex);
4217 mutex_unlock(&ctx->mutex);
4218 put_ctx(ctx);
4219 goto again;
4220 }
4221 mutex_unlock(&event->child_mutex);
4222
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004223no_ctx:
4224 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004225 return 0;
4226}
4227EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4228
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004229/*
4230 * Called when the last reference to the file is gone.
4231 */
Al Viroa6fa9412012-08-20 14:59:25 +01004232static int perf_release(struct inode *inode, struct file *file)
4233{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004234 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004235 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004236}
4237
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004238u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004239{
4240 struct perf_event *child;
4241 u64 total = 0;
4242
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004243 *enabled = 0;
4244 *running = 0;
4245
Peter Zijlstra6f105812009-11-20 22:19:56 +01004246 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004247
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004248 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004249 total += perf_event_count(event);
4250
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004251 *enabled += event->total_time_enabled +
4252 atomic64_read(&event->child_total_time_enabled);
4253 *running += event->total_time_running +
4254 atomic64_read(&event->child_total_time_running);
4255
4256 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004257 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004258 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004259 *enabled += child->total_time_enabled;
4260 *running += child->total_time_running;
4261 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004262 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004263
4264 return total;
4265}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004266EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004267
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004268static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004269 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004270{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004271 struct perf_event *sub;
4272 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004273 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004274
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004275 ret = perf_event_read(leader, true);
4276 if (ret)
4277 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004278
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004279 /*
4280 * Since we co-schedule groups, {enabled,running} times of siblings
4281 * will be identical to those of the leader, so we only publish one
4282 * set.
4283 */
4284 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4285 values[n++] += leader->total_time_enabled +
4286 atomic64_read(&leader->child_total_time_enabled);
4287 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004288
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004289 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4290 values[n++] += leader->total_time_running +
4291 atomic64_read(&leader->child_total_time_running);
4292 }
4293
4294 /*
4295 * Write {count,id} tuples for every sibling.
4296 */
4297 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004298 if (read_format & PERF_FORMAT_ID)
4299 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004300
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004301 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004302 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004303 if (read_format & PERF_FORMAT_ID)
4304 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004305 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004306
4307 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004308}
4309
4310static int perf_read_group(struct perf_event *event,
4311 u64 read_format, char __user *buf)
4312{
4313 struct perf_event *leader = event->group_leader, *child;
4314 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004315 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004316 u64 *values;
4317
4318 lockdep_assert_held(&ctx->mutex);
4319
4320 values = kzalloc(event->read_size, GFP_KERNEL);
4321 if (!values)
4322 return -ENOMEM;
4323
4324 values[0] = 1 + leader->nr_siblings;
4325
4326 /*
4327 * By locking the child_mutex of the leader we effectively
4328 * lock the child list of all siblings.. XXX explain how.
4329 */
4330 mutex_lock(&leader->child_mutex);
4331
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004332 ret = __perf_read_group_add(leader, read_format, values);
4333 if (ret)
4334 goto unlock;
4335
4336 list_for_each_entry(child, &leader->child_list, child_list) {
4337 ret = __perf_read_group_add(child, read_format, values);
4338 if (ret)
4339 goto unlock;
4340 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004341
4342 mutex_unlock(&leader->child_mutex);
4343
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004344 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004345 if (copy_to_user(buf, values, event->read_size))
4346 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004347 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004348
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004349unlock:
4350 mutex_unlock(&leader->child_mutex);
4351out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004352 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004353 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004354}
4355
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004356static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004357 u64 read_format, char __user *buf)
4358{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004359 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004360 u64 values[4];
4361 int n = 0;
4362
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004363 values[n++] = perf_event_read_value(event, &enabled, &running);
4364 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4365 values[n++] = enabled;
4366 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4367 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368 if (read_format & PERF_FORMAT_ID)
4369 values[n++] = primary_event_id(event);
4370
4371 if (copy_to_user(buf, values, n * sizeof(u64)))
4372 return -EFAULT;
4373
4374 return n * sizeof(u64);
4375}
4376
Jiri Olsadc633982014-09-12 13:18:26 +02004377static bool is_event_hup(struct perf_event *event)
4378{
4379 bool no_children;
4380
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004381 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004382 return false;
4383
4384 mutex_lock(&event->child_mutex);
4385 no_children = list_empty(&event->child_list);
4386 mutex_unlock(&event->child_mutex);
4387 return no_children;
4388}
4389
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004390/*
4391 * Read the performance event - simple non blocking version for now
4392 */
4393static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004394__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004395{
4396 u64 read_format = event->attr.read_format;
4397 int ret;
4398
4399 /*
4400 * Return end-of-file for a read on a event that is in
4401 * error state (i.e. because it was pinned but it couldn't be
4402 * scheduled on to the CPU at some point).
4403 */
4404 if (event->state == PERF_EVENT_STATE_ERROR)
4405 return 0;
4406
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004407 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004408 return -ENOSPC;
4409
4410 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004411 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004412 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004413 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004414 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004415
4416 return ret;
4417}
4418
4419static ssize_t
4420perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4421{
4422 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004423 struct perf_event_context *ctx;
4424 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004425
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004426 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004427 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004428 perf_event_ctx_unlock(event, ctx);
4429
4430 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431}
4432
4433static unsigned int perf_poll(struct file *file, poll_table *wait)
4434{
4435 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004436 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004437 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004438
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004439 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004440
Jiri Olsadc633982014-09-12 13:18:26 +02004441 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004442 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004443
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004444 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004445 * Pin the event->rb by taking event->mmap_mutex; otherwise
4446 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004447 */
4448 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004449 rb = event->rb;
4450 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004451 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004452 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004453 return events;
4454}
4455
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004456static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004457{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004458 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004459 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004460 perf_event_update_userpage(event);
4461}
4462
4463/*
4464 * Holding the top-level event's child_mutex means that any
4465 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004466 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004467 * task existence requirements of perf_event_enable/disable.
4468 */
4469static void perf_event_for_each_child(struct perf_event *event,
4470 void (*func)(struct perf_event *))
4471{
4472 struct perf_event *child;
4473
4474 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004475
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004476 mutex_lock(&event->child_mutex);
4477 func(event);
4478 list_for_each_entry(child, &event->child_list, child_list)
4479 func(child);
4480 mutex_unlock(&event->child_mutex);
4481}
4482
4483static void perf_event_for_each(struct perf_event *event,
4484 void (*func)(struct perf_event *))
4485{
4486 struct perf_event_context *ctx = event->ctx;
4487 struct perf_event *sibling;
4488
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004489 lockdep_assert_held(&ctx->mutex);
4490
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004491 event = event->group_leader;
4492
4493 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004495 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004496}
4497
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004498static void __perf_event_period(struct perf_event *event,
4499 struct perf_cpu_context *cpuctx,
4500 struct perf_event_context *ctx,
4501 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004502{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004503 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004504 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004505
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004507 event->attr.sample_freq = value;
4508 } else {
4509 event->attr.sample_period = value;
4510 event->hw.sample_period = value;
4511 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004512
4513 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4514 if (active) {
4515 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004516 /*
4517 * We could be throttled; unthrottle now to avoid the tick
4518 * trying to unthrottle while we already re-started the event.
4519 */
4520 if (event->hw.interrupts == MAX_INTERRUPTS) {
4521 event->hw.interrupts = 0;
4522 perf_log_throttle(event, 1);
4523 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004524 event->pmu->stop(event, PERF_EF_UPDATE);
4525 }
4526
4527 local64_set(&event->hw.period_left, 0);
4528
4529 if (active) {
4530 event->pmu->start(event, PERF_EF_RELOAD);
4531 perf_pmu_enable(ctx->pmu);
4532 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004533}
4534
4535static int perf_event_period(struct perf_event *event, u64 __user *arg)
4536{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004537 u64 value;
4538
4539 if (!is_sampling_event(event))
4540 return -EINVAL;
4541
4542 if (copy_from_user(&value, arg, sizeof(value)))
4543 return -EFAULT;
4544
4545 if (!value)
4546 return -EINVAL;
4547
4548 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4549 return -EINVAL;
4550
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004551 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004552
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004553 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004554}
4555
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004556static const struct file_operations perf_fops;
4557
Al Viro2903ff02012-08-28 12:52:22 -04004558static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004559{
Al Viro2903ff02012-08-28 12:52:22 -04004560 struct fd f = fdget(fd);
4561 if (!f.file)
4562 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004563
Al Viro2903ff02012-08-28 12:52:22 -04004564 if (f.file->f_op != &perf_fops) {
4565 fdput(f);
4566 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004567 }
Al Viro2903ff02012-08-28 12:52:22 -04004568 *p = f;
4569 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004570}
4571
4572static int perf_event_set_output(struct perf_event *event,
4573 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004574static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004575static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004576
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004577static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004578{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004579 void (*func)(struct perf_event *);
4580 u32 flags = arg;
4581
4582 switch (cmd) {
4583 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004584 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004585 break;
4586 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004587 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004588 break;
4589 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004590 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004591 break;
4592
4593 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004594 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004595
4596 case PERF_EVENT_IOC_PERIOD:
4597 return perf_event_period(event, (u64 __user *)arg);
4598
Jiri Olsacf4957f2012-10-24 13:37:58 +02004599 case PERF_EVENT_IOC_ID:
4600 {
4601 u64 id = primary_event_id(event);
4602
4603 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4604 return -EFAULT;
4605 return 0;
4606 }
4607
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004608 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004609 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004610 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004611 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004612 struct perf_event *output_event;
4613 struct fd output;
4614 ret = perf_fget_light(arg, &output);
4615 if (ret)
4616 return ret;
4617 output_event = output.file->private_data;
4618 ret = perf_event_set_output(event, output_event);
4619 fdput(output);
4620 } else {
4621 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004622 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004623 return ret;
4624 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004625
Li Zefan6fb29152009-10-15 11:21:42 +08004626 case PERF_EVENT_IOC_SET_FILTER:
4627 return perf_event_set_filter(event, (void __user *)arg);
4628
Alexei Starovoitov25415172015-03-25 12:49:20 -07004629 case PERF_EVENT_IOC_SET_BPF:
4630 return perf_event_set_bpf_prog(event, arg);
4631
Wang Nan86e79722016-03-28 06:41:29 +00004632 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4633 struct ring_buffer *rb;
4634
4635 rcu_read_lock();
4636 rb = rcu_dereference(event->rb);
4637 if (!rb || !rb->nr_pages) {
4638 rcu_read_unlock();
4639 return -EINVAL;
4640 }
4641 rb_toggle_paused(rb, !!arg);
4642 rcu_read_unlock();
4643 return 0;
4644 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004645 default:
4646 return -ENOTTY;
4647 }
4648
4649 if (flags & PERF_IOC_FLAG_GROUP)
4650 perf_event_for_each(event, func);
4651 else
4652 perf_event_for_each_child(event, func);
4653
4654 return 0;
4655}
4656
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004657static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4658{
4659 struct perf_event *event = file->private_data;
4660 struct perf_event_context *ctx;
4661 long ret;
4662
4663 ctx = perf_event_ctx_lock(event);
4664 ret = _perf_ioctl(event, cmd, arg);
4665 perf_event_ctx_unlock(event, ctx);
4666
4667 return ret;
4668}
4669
Pawel Mollb3f20782014-06-13 16:03:32 +01004670#ifdef CONFIG_COMPAT
4671static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4672 unsigned long arg)
4673{
4674 switch (_IOC_NR(cmd)) {
4675 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4676 case _IOC_NR(PERF_EVENT_IOC_ID):
4677 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4678 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4679 cmd &= ~IOCSIZE_MASK;
4680 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4681 }
4682 break;
4683 }
4684 return perf_ioctl(file, cmd, arg);
4685}
4686#else
4687# define perf_compat_ioctl NULL
4688#endif
4689
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004690int perf_event_task_enable(void)
4691{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004692 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004693 struct perf_event *event;
4694
4695 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004696 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4697 ctx = perf_event_ctx_lock(event);
4698 perf_event_for_each_child(event, _perf_event_enable);
4699 perf_event_ctx_unlock(event, ctx);
4700 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004701 mutex_unlock(&current->perf_event_mutex);
4702
4703 return 0;
4704}
4705
4706int perf_event_task_disable(void)
4707{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004708 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004709 struct perf_event *event;
4710
4711 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004712 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4713 ctx = perf_event_ctx_lock(event);
4714 perf_event_for_each_child(event, _perf_event_disable);
4715 perf_event_ctx_unlock(event, ctx);
4716 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004717 mutex_unlock(&current->perf_event_mutex);
4718
4719 return 0;
4720}
4721
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004722static int perf_event_index(struct perf_event *event)
4723{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004724 if (event->hw.state & PERF_HES_STOPPED)
4725 return 0;
4726
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004727 if (event->state != PERF_EVENT_STATE_ACTIVE)
4728 return 0;
4729
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004730 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004731}
4732
Eric B Munsonc4794292011-06-23 16:34:38 -04004733static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004734 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004735 u64 *enabled,
4736 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004737{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004738 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004739
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004740 *now = perf_clock();
4741 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004742 *enabled = ctx_time - event->tstamp_enabled;
4743 *running = ctx_time - event->tstamp_running;
4744}
4745
Peter Zijlstrafa731582013-09-19 10:16:42 +02004746static void perf_event_init_userpage(struct perf_event *event)
4747{
4748 struct perf_event_mmap_page *userpg;
4749 struct ring_buffer *rb;
4750
4751 rcu_read_lock();
4752 rb = rcu_dereference(event->rb);
4753 if (!rb)
4754 goto unlock;
4755
4756 userpg = rb->user_page;
4757
4758 /* Allow new userspace to detect that bit 0 is deprecated */
4759 userpg->cap_bit0_is_deprecated = 1;
4760 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004761 userpg->data_offset = PAGE_SIZE;
4762 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004763
4764unlock:
4765 rcu_read_unlock();
4766}
4767
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004768void __weak arch_perf_update_userpage(
4769 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004770{
4771}
4772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004773/*
4774 * Callers need to ensure there can be no nesting of this function, otherwise
4775 * the seqlock logic goes bad. We can not serialize this because the arch
4776 * code calls this from NMI context.
4777 */
4778void perf_event_update_userpage(struct perf_event *event)
4779{
4780 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004781 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004782 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004783
4784 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004785 rb = rcu_dereference(event->rb);
4786 if (!rb)
4787 goto unlock;
4788
Eric B Munson0d641202011-06-24 12:26:26 -04004789 /*
4790 * compute total_time_enabled, total_time_running
4791 * based on snapshot values taken when the event
4792 * was last scheduled in.
4793 *
4794 * we cannot simply called update_context_time()
4795 * because of locking issue as we can be called in
4796 * NMI context
4797 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004798 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004799
Frederic Weisbecker76369132011-05-19 19:55:04 +02004800 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004801 /*
4802 * Disable preemption so as to not let the corresponding user-space
4803 * spin too long if we get preempted.
4804 */
4805 preempt_disable();
4806 ++userpg->lock;
4807 barrier();
4808 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004809 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004810 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004811 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004812
Eric B Munson0d641202011-06-24 12:26:26 -04004813 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004814 atomic64_read(&event->child_total_time_enabled);
4815
Eric B Munson0d641202011-06-24 12:26:26 -04004816 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004817 atomic64_read(&event->child_total_time_running);
4818
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004819 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004820
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004821 barrier();
4822 ++userpg->lock;
4823 preempt_enable();
4824unlock:
4825 rcu_read_unlock();
4826}
4827
Peter Zijlstra906010b2009-09-21 16:08:49 +02004828static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4829{
4830 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004831 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004832 int ret = VM_FAULT_SIGBUS;
4833
4834 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4835 if (vmf->pgoff == 0)
4836 ret = 0;
4837 return ret;
4838 }
4839
4840 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004841 rb = rcu_dereference(event->rb);
4842 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004843 goto unlock;
4844
4845 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4846 goto unlock;
4847
Frederic Weisbecker76369132011-05-19 19:55:04 +02004848 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004849 if (!vmf->page)
4850 goto unlock;
4851
4852 get_page(vmf->page);
4853 vmf->page->mapping = vma->vm_file->f_mapping;
4854 vmf->page->index = vmf->pgoff;
4855
4856 ret = 0;
4857unlock:
4858 rcu_read_unlock();
4859
4860 return ret;
4861}
4862
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004863static void ring_buffer_attach(struct perf_event *event,
4864 struct ring_buffer *rb)
4865{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004866 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004867 unsigned long flags;
4868
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004869 if (event->rb) {
4870 /*
4871 * Should be impossible, we set this when removing
4872 * event->rb_entry and wait/clear when adding event->rb_entry.
4873 */
4874 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004875
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004876 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004877 spin_lock_irqsave(&old_rb->event_lock, flags);
4878 list_del_rcu(&event->rb_entry);
4879 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004880
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004881 event->rcu_batches = get_state_synchronize_rcu();
4882 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004883 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004884
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004885 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004886 if (event->rcu_pending) {
4887 cond_synchronize_rcu(event->rcu_batches);
4888 event->rcu_pending = 0;
4889 }
4890
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004891 spin_lock_irqsave(&rb->event_lock, flags);
4892 list_add_rcu(&event->rb_entry, &rb->event_list);
4893 spin_unlock_irqrestore(&rb->event_lock, flags);
4894 }
4895
Alexander Shishkin767ae082016-09-06 16:23:49 +03004896 /*
4897 * Avoid racing with perf_mmap_close(AUX): stop the event
4898 * before swizzling the event::rb pointer; if it's getting
4899 * unmapped, its aux_mmap_count will be 0 and it won't
4900 * restart. See the comment in __perf_pmu_output_stop().
4901 *
4902 * Data will inevitably be lost when set_output is done in
4903 * mid-air, but then again, whoever does it like this is
4904 * not in for the data anyway.
4905 */
4906 if (has_aux(event))
4907 perf_event_stop(event, 0);
4908
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004909 rcu_assign_pointer(event->rb, rb);
4910
4911 if (old_rb) {
4912 ring_buffer_put(old_rb);
4913 /*
4914 * Since we detached before setting the new rb, so that we
4915 * could attach the new rb, we could have missed a wakeup.
4916 * Provide it now.
4917 */
4918 wake_up_all(&event->waitq);
4919 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004920}
4921
4922static void ring_buffer_wakeup(struct perf_event *event)
4923{
4924 struct ring_buffer *rb;
4925
4926 rcu_read_lock();
4927 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004928 if (rb) {
4929 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4930 wake_up_all(&event->waitq);
4931 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004932 rcu_read_unlock();
4933}
4934
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004935struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004936{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004937 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004938
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004939 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004940 rb = rcu_dereference(event->rb);
4941 if (rb) {
4942 if (!atomic_inc_not_zero(&rb->refcount))
4943 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004944 }
4945 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004946
Frederic Weisbecker76369132011-05-19 19:55:04 +02004947 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004948}
4949
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004950void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004951{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004952 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004953 return;
4954
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004955 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004956
Frederic Weisbecker76369132011-05-19 19:55:04 +02004957 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004958}
4959
4960static void perf_mmap_open(struct vm_area_struct *vma)
4961{
4962 struct perf_event *event = vma->vm_file->private_data;
4963
4964 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004965 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004966
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004967 if (vma->vm_pgoff)
4968 atomic_inc(&event->rb->aux_mmap_count);
4969
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004970 if (event->pmu->event_mapped)
4971 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004972}
4973
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004974static void perf_pmu_output_stop(struct perf_event *event);
4975
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004976/*
4977 * A buffer can be mmap()ed multiple times; either directly through the same
4978 * event, or through other events by use of perf_event_set_output().
4979 *
4980 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4981 * the buffer here, where we still have a VM context. This means we need
4982 * to detach all events redirecting to us.
4983 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004984static void perf_mmap_close(struct vm_area_struct *vma)
4985{
4986 struct perf_event *event = vma->vm_file->private_data;
4987
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004988 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004989 struct user_struct *mmap_user = rb->mmap_user;
4990 int mmap_locked = rb->mmap_locked;
4991 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004992
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004993 if (event->pmu->event_unmapped)
4994 event->pmu->event_unmapped(event);
4995
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004996 /*
4997 * rb->aux_mmap_count will always drop before rb->mmap_count and
4998 * event->mmap_count, so it is ok to use event->mmap_mutex to
4999 * serialize with perf_mmap here.
5000 */
5001 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5002 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005003 /*
5004 * Stop all AUX events that are writing to this buffer,
5005 * so that we can free its AUX pages and corresponding PMU
5006 * data. Note that after rb::aux_mmap_count dropped to zero,
5007 * they won't start any more (see perf_aux_output_begin()).
5008 */
5009 perf_pmu_output_stop(event);
5010
5011 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005012 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
5013 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
5014
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005015 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005016 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005017 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
5018
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005019 mutex_unlock(&event->mmap_mutex);
5020 }
5021
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005022 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005023
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005024 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005025 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005026
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005027 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005028 mutex_unlock(&event->mmap_mutex);
5029
5030 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005031 if (atomic_read(&rb->mmap_count))
5032 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005033
5034 /*
5035 * No other mmap()s, detach from all other events that might redirect
5036 * into the now unreachable buffer. Somewhat complicated by the
5037 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5038 */
5039again:
5040 rcu_read_lock();
5041 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5042 if (!atomic_long_inc_not_zero(&event->refcount)) {
5043 /*
5044 * This event is en-route to free_event() which will
5045 * detach it and remove it from the list.
5046 */
5047 continue;
5048 }
5049 rcu_read_unlock();
5050
5051 mutex_lock(&event->mmap_mutex);
5052 /*
5053 * Check we didn't race with perf_event_set_output() which can
5054 * swizzle the rb from under us while we were waiting to
5055 * acquire mmap_mutex.
5056 *
5057 * If we find a different rb; ignore this event, a next
5058 * iteration will no longer find it on the list. We have to
5059 * still restart the iteration to make sure we're not now
5060 * iterating the wrong list.
5061 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005062 if (event->rb == rb)
5063 ring_buffer_attach(event, NULL);
5064
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005065 mutex_unlock(&event->mmap_mutex);
5066 put_event(event);
5067
5068 /*
5069 * Restart the iteration; either we're on the wrong list or
5070 * destroyed its integrity by doing a deletion.
5071 */
5072 goto again;
5073 }
5074 rcu_read_unlock();
5075
5076 /*
5077 * It could be there's still a few 0-ref events on the list; they'll
5078 * get cleaned up by free_event() -- they'll also still have their
5079 * ref on the rb and will free it whenever they are done with it.
5080 *
5081 * Aside from that, this buffer is 'fully' detached and unmapped,
5082 * undo the VM accounting.
5083 */
5084
5085 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
5086 vma->vm_mm->pinned_vm -= mmap_locked;
5087 free_uid(mmap_user);
5088
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005089out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005090 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005091}
5092
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04005093static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005094 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005095 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005096 .fault = perf_mmap_fault,
5097 .page_mkwrite = perf_mmap_fault,
5098};
5099
5100static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5101{
5102 struct perf_event *event = file->private_data;
5103 unsigned long user_locked, user_lock_limit;
5104 struct user_struct *user = current_user();
5105 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005106 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005107 unsigned long vma_size;
5108 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005109 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005110 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005111
Peter Zijlstrac7920612010-05-18 10:33:24 +02005112 /*
5113 * Don't allow mmap() of inherited per-task counters. This would
5114 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005115 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005116 */
5117 if (event->cpu == -1 && event->attr.inherit)
5118 return -EINVAL;
5119
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005120 if (!(vma->vm_flags & VM_SHARED))
5121 return -EINVAL;
5122
5123 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005124
5125 if (vma->vm_pgoff == 0) {
5126 nr_pages = (vma_size / PAGE_SIZE) - 1;
5127 } else {
5128 /*
5129 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5130 * mapped, all subsequent mappings should have the same size
5131 * and offset. Must be above the normal perf buffer.
5132 */
5133 u64 aux_offset, aux_size;
5134
5135 if (!event->rb)
5136 return -EINVAL;
5137
5138 nr_pages = vma_size / PAGE_SIZE;
5139
5140 mutex_lock(&event->mmap_mutex);
5141 ret = -EINVAL;
5142
5143 rb = event->rb;
5144 if (!rb)
5145 goto aux_unlock;
5146
5147 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
5148 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
5149
5150 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5151 goto aux_unlock;
5152
5153 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5154 goto aux_unlock;
5155
5156 /* already mapped with a different offset */
5157 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5158 goto aux_unlock;
5159
5160 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5161 goto aux_unlock;
5162
5163 /* already mapped with a different size */
5164 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5165 goto aux_unlock;
5166
5167 if (!is_power_of_2(nr_pages))
5168 goto aux_unlock;
5169
5170 if (!atomic_inc_not_zero(&rb->mmap_count))
5171 goto aux_unlock;
5172
5173 if (rb_has_aux(rb)) {
5174 atomic_inc(&rb->aux_mmap_count);
5175 ret = 0;
5176 goto unlock;
5177 }
5178
5179 atomic_set(&rb->aux_mmap_count, 1);
5180 user_extra = nr_pages;
5181
5182 goto accounting;
5183 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184
5185 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005186 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005187 * can do bitmasks instead of modulo.
5188 */
Kan Liang2ed11312015-03-02 02:14:26 -05005189 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005190 return -EINVAL;
5191
5192 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5193 return -EINVAL;
5194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005195 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005196again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005197 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005198 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005199 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005200 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005201 goto unlock;
5202 }
5203
5204 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5205 /*
5206 * Raced against perf_mmap_close() through
5207 * perf_event_set_output(). Try again, hope for better
5208 * luck.
5209 */
5210 mutex_unlock(&event->mmap_mutex);
5211 goto again;
5212 }
5213
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005214 goto unlock;
5215 }
5216
5217 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005218
5219accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005220 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5221
5222 /*
5223 * Increase the limit linearly with more CPUs:
5224 */
5225 user_lock_limit *= num_online_cpus();
5226
5227 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5228
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005229 if (user_locked > user_lock_limit)
5230 extra = user_locked - user_lock_limit;
5231
Jiri Slaby78d7d402010-03-05 13:42:54 -08005232 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005234 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235
5236 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5237 !capable(CAP_IPC_LOCK)) {
5238 ret = -EPERM;
5239 goto unlock;
5240 }
5241
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005242 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005243
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005244 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005245 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005246
Frederic Weisbecker76369132011-05-19 19:55:04 +02005247 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005248 rb = rb_alloc(nr_pages,
5249 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5250 event->cpu, flags);
5251
5252 if (!rb) {
5253 ret = -ENOMEM;
5254 goto unlock;
5255 }
5256
5257 atomic_set(&rb->mmap_count, 1);
5258 rb->mmap_user = get_current_user();
5259 rb->mmap_locked = extra;
5260
5261 ring_buffer_attach(event, rb);
5262
5263 perf_event_init_userpage(event);
5264 perf_event_update_userpage(event);
5265 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005266 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5267 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005268 if (!ret)
5269 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005270 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005271
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005272unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005273 if (!ret) {
5274 atomic_long_add(user_extra, &user->locked_vm);
5275 vma->vm_mm->pinned_vm += extra;
5276
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005277 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005278 } else if (rb) {
5279 atomic_dec(&rb->mmap_count);
5280 }
5281aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005282 mutex_unlock(&event->mmap_mutex);
5283
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005284 /*
5285 * Since pinned accounting is per vm we cannot allow fork() to copy our
5286 * vma.
5287 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005288 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005289 vma->vm_ops = &perf_mmap_vmops;
5290
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005291 if (event->pmu->event_mapped)
5292 event->pmu->event_mapped(event);
5293
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005294 return ret;
5295}
5296
5297static int perf_fasync(int fd, struct file *filp, int on)
5298{
Al Viro496ad9a2013-01-23 17:07:38 -05005299 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005300 struct perf_event *event = filp->private_data;
5301 int retval;
5302
Al Viro59551022016-01-22 15:40:57 -05005303 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005304 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005305 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005306
5307 if (retval < 0)
5308 return retval;
5309
5310 return 0;
5311}
5312
5313static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005314 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005315 .release = perf_release,
5316 .read = perf_read,
5317 .poll = perf_poll,
5318 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005319 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005320 .mmap = perf_mmap,
5321 .fasync = perf_fasync,
5322};
5323
5324/*
5325 * Perf event wakeup
5326 *
5327 * If there's data, ensure we set the poll() state and publish everything
5328 * to user-space before waking everybody up.
5329 */
5330
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005331static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5332{
5333 /* only the parent has fasync state */
5334 if (event->parent)
5335 event = event->parent;
5336 return &event->fasync;
5337}
5338
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005339void perf_event_wakeup(struct perf_event *event)
5340{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005341 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005342
5343 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005344 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345 event->pending_kill = 0;
5346 }
5347}
5348
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005349static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005350{
5351 struct perf_event *event = container_of(entry,
5352 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005353 int rctx;
5354
5355 rctx = perf_swevent_get_recursion_context();
5356 /*
5357 * If we 'fail' here, that's OK, it means recursion is already disabled
5358 * and we won't recurse 'further'.
5359 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005360
5361 if (event->pending_disable) {
5362 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005363 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005364 }
5365
5366 if (event->pending_wakeup) {
5367 event->pending_wakeup = 0;
5368 perf_event_wakeup(event);
5369 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005370
5371 if (rctx >= 0)
5372 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005373}
5374
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005375/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005376 * We assume there is only KVM supporting the callbacks.
5377 * Later on, we might change it to a list if there is
5378 * another virtualization implementation supporting the callbacks.
5379 */
5380struct perf_guest_info_callbacks *perf_guest_cbs;
5381
5382int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5383{
5384 perf_guest_cbs = cbs;
5385 return 0;
5386}
5387EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5388
5389int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5390{
5391 perf_guest_cbs = NULL;
5392 return 0;
5393}
5394EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5395
Jiri Olsa40189942012-08-07 15:20:37 +02005396static void
5397perf_output_sample_regs(struct perf_output_handle *handle,
5398 struct pt_regs *regs, u64 mask)
5399{
5400 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305401 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02005402
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305403 bitmap_from_u64(_mask, mask);
5404 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02005405 u64 val;
5406
5407 val = perf_reg_value(regs, bit);
5408 perf_output_put(handle, val);
5409 }
5410}
5411
Stephane Eranian60e23642014-09-24 13:48:37 +02005412static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005413 struct pt_regs *regs,
5414 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005415{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005416 if (user_mode(regs)) {
5417 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005418 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005419 } else if (current->mm) {
5420 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005421 } else {
5422 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5423 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005424 }
5425}
5426
Stephane Eranian60e23642014-09-24 13:48:37 +02005427static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5428 struct pt_regs *regs)
5429{
5430 regs_intr->regs = regs;
5431 regs_intr->abi = perf_reg_abi(current);
5432}
5433
5434
Jiri Olsac5ebced2012-08-07 15:20:40 +02005435/*
5436 * Get remaining task size from user stack pointer.
5437 *
5438 * It'd be better to take stack vma map and limit this more
5439 * precisly, but there's no way to get it safely under interrupt,
5440 * so using TASK_SIZE as limit.
5441 */
5442static u64 perf_ustack_task_size(struct pt_regs *regs)
5443{
5444 unsigned long addr = perf_user_stack_pointer(regs);
5445
5446 if (!addr || addr >= TASK_SIZE)
5447 return 0;
5448
5449 return TASK_SIZE - addr;
5450}
5451
5452static u16
5453perf_sample_ustack_size(u16 stack_size, u16 header_size,
5454 struct pt_regs *regs)
5455{
5456 u64 task_size;
5457
5458 /* No regs, no stack pointer, no dump. */
5459 if (!regs)
5460 return 0;
5461
5462 /*
5463 * Check if we fit in with the requested stack size into the:
5464 * - TASK_SIZE
5465 * If we don't, we limit the size to the TASK_SIZE.
5466 *
5467 * - remaining sample size
5468 * If we don't, we customize the stack size to
5469 * fit in to the remaining sample size.
5470 */
5471
5472 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5473 stack_size = min(stack_size, (u16) task_size);
5474
5475 /* Current header size plus static size and dynamic size. */
5476 header_size += 2 * sizeof(u64);
5477
5478 /* Do we fit in with the current stack dump size? */
5479 if ((u16) (header_size + stack_size) < header_size) {
5480 /*
5481 * If we overflow the maximum size for the sample,
5482 * we customize the stack dump size to fit in.
5483 */
5484 stack_size = USHRT_MAX - header_size - sizeof(u64);
5485 stack_size = round_up(stack_size, sizeof(u64));
5486 }
5487
5488 return stack_size;
5489}
5490
5491static void
5492perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5493 struct pt_regs *regs)
5494{
5495 /* Case of a kernel thread, nothing to dump */
5496 if (!regs) {
5497 u64 size = 0;
5498 perf_output_put(handle, size);
5499 } else {
5500 unsigned long sp;
5501 unsigned int rem;
5502 u64 dyn_size;
5503
5504 /*
5505 * We dump:
5506 * static size
5507 * - the size requested by user or the best one we can fit
5508 * in to the sample max size
5509 * data
5510 * - user stack dump data
5511 * dynamic size
5512 * - the actual dumped size
5513 */
5514
5515 /* Static size. */
5516 perf_output_put(handle, dump_size);
5517
5518 /* Data. */
5519 sp = perf_user_stack_pointer(regs);
5520 rem = __output_copy_user(handle, (void *) sp, dump_size);
5521 dyn_size = dump_size - rem;
5522
5523 perf_output_skip(handle, rem);
5524
5525 /* Dynamic size. */
5526 perf_output_put(handle, dyn_size);
5527 }
5528}
5529
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005530static void __perf_event_header__init_id(struct perf_event_header *header,
5531 struct perf_sample_data *data,
5532 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005533{
5534 u64 sample_type = event->attr.sample_type;
5535
5536 data->type = sample_type;
5537 header->size += event->id_header_size;
5538
5539 if (sample_type & PERF_SAMPLE_TID) {
5540 /* namespace issues */
5541 data->tid_entry.pid = perf_event_pid(event, current);
5542 data->tid_entry.tid = perf_event_tid(event, current);
5543 }
5544
5545 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005546 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005547
Adrian Hunterff3d5272013-08-27 11:23:07 +03005548 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005549 data->id = primary_event_id(event);
5550
5551 if (sample_type & PERF_SAMPLE_STREAM_ID)
5552 data->stream_id = event->id;
5553
5554 if (sample_type & PERF_SAMPLE_CPU) {
5555 data->cpu_entry.cpu = raw_smp_processor_id();
5556 data->cpu_entry.reserved = 0;
5557 }
5558}
5559
Frederic Weisbecker76369132011-05-19 19:55:04 +02005560void perf_event_header__init_id(struct perf_event_header *header,
5561 struct perf_sample_data *data,
5562 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005563{
5564 if (event->attr.sample_id_all)
5565 __perf_event_header__init_id(header, data, event);
5566}
5567
5568static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5569 struct perf_sample_data *data)
5570{
5571 u64 sample_type = data->type;
5572
5573 if (sample_type & PERF_SAMPLE_TID)
5574 perf_output_put(handle, data->tid_entry);
5575
5576 if (sample_type & PERF_SAMPLE_TIME)
5577 perf_output_put(handle, data->time);
5578
5579 if (sample_type & PERF_SAMPLE_ID)
5580 perf_output_put(handle, data->id);
5581
5582 if (sample_type & PERF_SAMPLE_STREAM_ID)
5583 perf_output_put(handle, data->stream_id);
5584
5585 if (sample_type & PERF_SAMPLE_CPU)
5586 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005587
5588 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5589 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005590}
5591
Frederic Weisbecker76369132011-05-19 19:55:04 +02005592void perf_event__output_id_sample(struct perf_event *event,
5593 struct perf_output_handle *handle,
5594 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005595{
5596 if (event->attr.sample_id_all)
5597 __perf_event__output_id_sample(handle, sample);
5598}
5599
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005600static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005601 struct perf_event *event,
5602 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005603{
5604 u64 read_format = event->attr.read_format;
5605 u64 values[4];
5606 int n = 0;
5607
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005608 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005609 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005610 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005611 atomic64_read(&event->child_total_time_enabled);
5612 }
5613 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005614 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005615 atomic64_read(&event->child_total_time_running);
5616 }
5617 if (read_format & PERF_FORMAT_ID)
5618 values[n++] = primary_event_id(event);
5619
Frederic Weisbecker76369132011-05-19 19:55:04 +02005620 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005621}
5622
5623/*
5624 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5625 */
5626static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005627 struct perf_event *event,
5628 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005629{
5630 struct perf_event *leader = event->group_leader, *sub;
5631 u64 read_format = event->attr.read_format;
5632 u64 values[5];
5633 int n = 0;
5634
5635 values[n++] = 1 + leader->nr_siblings;
5636
5637 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005638 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005639
5640 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005641 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005642
5643 if (leader != event)
5644 leader->pmu->read(leader);
5645
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005646 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005647 if (read_format & PERF_FORMAT_ID)
5648 values[n++] = primary_event_id(leader);
5649
Frederic Weisbecker76369132011-05-19 19:55:04 +02005650 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005651
5652 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5653 n = 0;
5654
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005655 if ((sub != event) &&
5656 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005657 sub->pmu->read(sub);
5658
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005659 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005660 if (read_format & PERF_FORMAT_ID)
5661 values[n++] = primary_event_id(sub);
5662
Frederic Weisbecker76369132011-05-19 19:55:04 +02005663 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005664 }
5665}
5666
Stephane Eranianeed01522010-10-26 16:08:01 +02005667#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5668 PERF_FORMAT_TOTAL_TIME_RUNNING)
5669
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005670static void perf_output_read(struct perf_output_handle *handle,
5671 struct perf_event *event)
5672{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005673 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005674 u64 read_format = event->attr.read_format;
5675
5676 /*
5677 * compute total_time_enabled, total_time_running
5678 * based on snapshot values taken when the event
5679 * was last scheduled in.
5680 *
5681 * we cannot simply called update_context_time()
5682 * because of locking issue as we are called in
5683 * NMI context
5684 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005685 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005686 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005687
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005688 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005689 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005690 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005691 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005692}
5693
5694void perf_output_sample(struct perf_output_handle *handle,
5695 struct perf_event_header *header,
5696 struct perf_sample_data *data,
5697 struct perf_event *event)
5698{
5699 u64 sample_type = data->type;
5700
5701 perf_output_put(handle, *header);
5702
Adrian Hunterff3d5272013-08-27 11:23:07 +03005703 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5704 perf_output_put(handle, data->id);
5705
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005706 if (sample_type & PERF_SAMPLE_IP)
5707 perf_output_put(handle, data->ip);
5708
5709 if (sample_type & PERF_SAMPLE_TID)
5710 perf_output_put(handle, data->tid_entry);
5711
5712 if (sample_type & PERF_SAMPLE_TIME)
5713 perf_output_put(handle, data->time);
5714
5715 if (sample_type & PERF_SAMPLE_ADDR)
5716 perf_output_put(handle, data->addr);
5717
5718 if (sample_type & PERF_SAMPLE_ID)
5719 perf_output_put(handle, data->id);
5720
5721 if (sample_type & PERF_SAMPLE_STREAM_ID)
5722 perf_output_put(handle, data->stream_id);
5723
5724 if (sample_type & PERF_SAMPLE_CPU)
5725 perf_output_put(handle, data->cpu_entry);
5726
5727 if (sample_type & PERF_SAMPLE_PERIOD)
5728 perf_output_put(handle, data->period);
5729
5730 if (sample_type & PERF_SAMPLE_READ)
5731 perf_output_read(handle, event);
5732
5733 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5734 if (data->callchain) {
5735 int size = 1;
5736
5737 if (data->callchain)
5738 size += data->callchain->nr;
5739
5740 size *= sizeof(u64);
5741
Frederic Weisbecker76369132011-05-19 19:55:04 +02005742 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005743 } else {
5744 u64 nr = 0;
5745 perf_output_put(handle, nr);
5746 }
5747 }
5748
5749 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005750 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005751
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005752 if (raw) {
5753 struct perf_raw_frag *frag = &raw->frag;
5754
5755 perf_output_put(handle, raw->size);
5756 do {
5757 if (frag->copy) {
5758 __output_custom(handle, frag->copy,
5759 frag->data, frag->size);
5760 } else {
5761 __output_copy(handle, frag->data,
5762 frag->size);
5763 }
5764 if (perf_raw_frag_last(frag))
5765 break;
5766 frag = frag->next;
5767 } while (1);
5768 if (frag->pad)
5769 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005770 } else {
5771 struct {
5772 u32 size;
5773 u32 data;
5774 } raw = {
5775 .size = sizeof(u32),
5776 .data = 0,
5777 };
5778 perf_output_put(handle, raw);
5779 }
5780 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005781
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005782 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5783 if (data->br_stack) {
5784 size_t size;
5785
5786 size = data->br_stack->nr
5787 * sizeof(struct perf_branch_entry);
5788
5789 perf_output_put(handle, data->br_stack->nr);
5790 perf_output_copy(handle, data->br_stack->entries, size);
5791 } else {
5792 /*
5793 * we always store at least the value of nr
5794 */
5795 u64 nr = 0;
5796 perf_output_put(handle, nr);
5797 }
5798 }
Jiri Olsa40189942012-08-07 15:20:37 +02005799
5800 if (sample_type & PERF_SAMPLE_REGS_USER) {
5801 u64 abi = data->regs_user.abi;
5802
5803 /*
5804 * If there are no regs to dump, notice it through
5805 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5806 */
5807 perf_output_put(handle, abi);
5808
5809 if (abi) {
5810 u64 mask = event->attr.sample_regs_user;
5811 perf_output_sample_regs(handle,
5812 data->regs_user.regs,
5813 mask);
5814 }
5815 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005816
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005817 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005818 perf_output_sample_ustack(handle,
5819 data->stack_user_size,
5820 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005821 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005822
5823 if (sample_type & PERF_SAMPLE_WEIGHT)
5824 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005825
5826 if (sample_type & PERF_SAMPLE_DATA_SRC)
5827 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005828
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005829 if (sample_type & PERF_SAMPLE_TRANSACTION)
5830 perf_output_put(handle, data->txn);
5831
Stephane Eranian60e23642014-09-24 13:48:37 +02005832 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5833 u64 abi = data->regs_intr.abi;
5834 /*
5835 * If there are no regs to dump, notice it through
5836 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5837 */
5838 perf_output_put(handle, abi);
5839
5840 if (abi) {
5841 u64 mask = event->attr.sample_regs_intr;
5842
5843 perf_output_sample_regs(handle,
5844 data->regs_intr.regs,
5845 mask);
5846 }
5847 }
5848
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005849 if (!event->attr.watermark) {
5850 int wakeup_events = event->attr.wakeup_events;
5851
5852 if (wakeup_events) {
5853 struct ring_buffer *rb = handle->rb;
5854 int events = local_inc_return(&rb->events);
5855
5856 if (events >= wakeup_events) {
5857 local_sub(wakeup_events, &rb->events);
5858 local_inc(&rb->wakeup);
5859 }
5860 }
5861 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005862}
5863
5864void perf_prepare_sample(struct perf_event_header *header,
5865 struct perf_sample_data *data,
5866 struct perf_event *event,
5867 struct pt_regs *regs)
5868{
5869 u64 sample_type = event->attr.sample_type;
5870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005871 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005872 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005873
5874 header->misc = 0;
5875 header->misc |= perf_misc_flags(regs);
5876
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005877 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005878
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005879 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005880 data->ip = perf_instruction_pointer(regs);
5881
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005882 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5883 int size = 1;
5884
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005885 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005886
5887 if (data->callchain)
5888 size += data->callchain->nr;
5889
5890 header->size += size * sizeof(u64);
5891 }
5892
5893 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005894 struct perf_raw_record *raw = data->raw;
5895 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005896
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005897 if (raw) {
5898 struct perf_raw_frag *frag = &raw->frag;
5899 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005901 do {
5902 sum += frag->size;
5903 if (perf_raw_frag_last(frag))
5904 break;
5905 frag = frag->next;
5906 } while (1);
5907
5908 size = round_up(sum + sizeof(u32), sizeof(u64));
5909 raw->size = size - sizeof(u32);
5910 frag->pad = raw->size - sum;
5911 } else {
5912 size = sizeof(u64);
5913 }
5914
5915 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005916 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005917
5918 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5919 int size = sizeof(u64); /* nr */
5920 if (data->br_stack) {
5921 size += data->br_stack->nr
5922 * sizeof(struct perf_branch_entry);
5923 }
5924 header->size += size;
5925 }
Jiri Olsa40189942012-08-07 15:20:37 +02005926
Peter Zijlstra25657112014-09-24 13:48:42 +02005927 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005928 perf_sample_regs_user(&data->regs_user, regs,
5929 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005930
Jiri Olsa40189942012-08-07 15:20:37 +02005931 if (sample_type & PERF_SAMPLE_REGS_USER) {
5932 /* regs dump ABI info */
5933 int size = sizeof(u64);
5934
Jiri Olsa40189942012-08-07 15:20:37 +02005935 if (data->regs_user.regs) {
5936 u64 mask = event->attr.sample_regs_user;
5937 size += hweight64(mask) * sizeof(u64);
5938 }
5939
5940 header->size += size;
5941 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005942
5943 if (sample_type & PERF_SAMPLE_STACK_USER) {
5944 /*
5945 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5946 * processed as the last one or have additional check added
5947 * in case new sample type is added, because we could eat
5948 * up the rest of the sample size.
5949 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005950 u16 stack_size = event->attr.sample_stack_user;
5951 u16 size = sizeof(u64);
5952
Jiri Olsac5ebced2012-08-07 15:20:40 +02005953 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005954 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005955
5956 /*
5957 * If there is something to dump, add space for the dump
5958 * itself and for the field that tells the dynamic size,
5959 * which is how many have been actually dumped.
5960 */
5961 if (stack_size)
5962 size += sizeof(u64) + stack_size;
5963
5964 data->stack_user_size = stack_size;
5965 header->size += size;
5966 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005967
5968 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5969 /* regs dump ABI info */
5970 int size = sizeof(u64);
5971
5972 perf_sample_regs_intr(&data->regs_intr, regs);
5973
5974 if (data->regs_intr.regs) {
5975 u64 mask = event->attr.sample_regs_intr;
5976
5977 size += hweight64(mask) * sizeof(u64);
5978 }
5979
5980 header->size += size;
5981 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005982}
5983
Wang Nan9ecda412016-04-05 14:11:18 +00005984static void __always_inline
5985__perf_event_output(struct perf_event *event,
5986 struct perf_sample_data *data,
5987 struct pt_regs *regs,
5988 int (*output_begin)(struct perf_output_handle *,
5989 struct perf_event *,
5990 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005991{
5992 struct perf_output_handle handle;
5993 struct perf_event_header header;
5994
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005995 /* protect the callchain buffers */
5996 rcu_read_lock();
5997
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005998 perf_prepare_sample(&header, data, event, regs);
5999
Wang Nan9ecda412016-04-05 14:11:18 +00006000 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006001 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006002
6003 perf_output_sample(&handle, &header, data, event);
6004
6005 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006006
6007exit:
6008 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006009}
6010
Wang Nan9ecda412016-04-05 14:11:18 +00006011void
6012perf_event_output_forward(struct perf_event *event,
6013 struct perf_sample_data *data,
6014 struct pt_regs *regs)
6015{
6016 __perf_event_output(event, data, regs, perf_output_begin_forward);
6017}
6018
6019void
6020perf_event_output_backward(struct perf_event *event,
6021 struct perf_sample_data *data,
6022 struct pt_regs *regs)
6023{
6024 __perf_event_output(event, data, regs, perf_output_begin_backward);
6025}
6026
6027void
6028perf_event_output(struct perf_event *event,
6029 struct perf_sample_data *data,
6030 struct pt_regs *regs)
6031{
6032 __perf_event_output(event, data, regs, perf_output_begin);
6033}
6034
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006035/*
6036 * read event_id
6037 */
6038
6039struct perf_read_event {
6040 struct perf_event_header header;
6041
6042 u32 pid;
6043 u32 tid;
6044};
6045
6046static void
6047perf_event_read_event(struct perf_event *event,
6048 struct task_struct *task)
6049{
6050 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006051 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006052 struct perf_read_event read_event = {
6053 .header = {
6054 .type = PERF_RECORD_READ,
6055 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006056 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006057 },
6058 .pid = perf_event_pid(event, task),
6059 .tid = perf_event_tid(event, task),
6060 };
6061 int ret;
6062
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006063 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006064 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006065 if (ret)
6066 return;
6067
6068 perf_output_put(&handle, read_event);
6069 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006070 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006071
6072 perf_output_end(&handle);
6073}
6074
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006075typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006076
6077static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006078perf_iterate_ctx(struct perf_event_context *ctx,
6079 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006080 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02006081{
6082 struct perf_event *event;
6083
6084 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006085 if (!all) {
6086 if (event->state < PERF_EVENT_STATE_INACTIVE)
6087 continue;
6088 if (!event_filter_match(event))
6089 continue;
6090 }
6091
Jiri Olsa67516842013-07-09 18:56:31 +02006092 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006093 }
6094}
6095
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006096static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07006097{
6098 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
6099 struct perf_event *event;
6100
6101 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02006102 /*
6103 * Skip events that are not fully formed yet; ensure that
6104 * if we observe event->ctx, both event and ctx will be
6105 * complete enough. See perf_install_in_context().
6106 */
6107 if (!smp_load_acquire(&event->ctx))
6108 continue;
6109
Kan Liangf2fb6be2016-03-23 11:24:37 -07006110 if (event->state < PERF_EVENT_STATE_INACTIVE)
6111 continue;
6112 if (!event_filter_match(event))
6113 continue;
6114 output(event, data);
6115 }
6116}
6117
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006118/*
6119 * Iterate all events that need to receive side-band events.
6120 *
6121 * For new callers; ensure that account_pmu_sb_event() includes
6122 * your event, otherwise it might not get delivered.
6123 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006124static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006125perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006126 struct perf_event_context *task_ctx)
6127{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006128 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02006129 int ctxn;
6130
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006131 rcu_read_lock();
6132 preempt_disable();
6133
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006134 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006135 * If we have task_ctx != NULL we only notify the task context itself.
6136 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006137 * context.
6138 */
6139 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006140 perf_iterate_ctx(task_ctx, output, data, false);
6141 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006142 }
6143
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006144 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07006145
6146 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02006147 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6148 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006149 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006150 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006151done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07006152 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02006153 rcu_read_unlock();
6154}
6155
Alexander Shishkin375637b2016-04-27 18:44:46 +03006156/*
6157 * Clear all file-based filters at exec, they'll have to be
6158 * re-instated when/if these objects are mmapped again.
6159 */
6160static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
6161{
6162 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6163 struct perf_addr_filter *filter;
6164 unsigned int restart = 0, count = 0;
6165 unsigned long flags;
6166
6167 if (!has_addr_filter(event))
6168 return;
6169
6170 raw_spin_lock_irqsave(&ifh->lock, flags);
6171 list_for_each_entry(filter, &ifh->list, entry) {
6172 if (filter->inode) {
6173 event->addr_filters_offs[count] = 0;
6174 restart++;
6175 }
6176
6177 count++;
6178 }
6179
6180 if (restart)
6181 event->addr_filters_gen++;
6182 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6183
6184 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006185 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006186}
6187
6188void perf_event_exec(void)
6189{
6190 struct perf_event_context *ctx;
6191 int ctxn;
6192
6193 rcu_read_lock();
6194 for_each_task_context_nr(ctxn) {
6195 ctx = current->perf_event_ctxp[ctxn];
6196 if (!ctx)
6197 continue;
6198
6199 perf_event_enable_on_exec(ctxn);
6200
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006201 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03006202 true);
6203 }
6204 rcu_read_unlock();
6205}
6206
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006207struct remote_output {
6208 struct ring_buffer *rb;
6209 int err;
6210};
6211
6212static void __perf_event_output_stop(struct perf_event *event, void *data)
6213{
6214 struct perf_event *parent = event->parent;
6215 struct remote_output *ro = data;
6216 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03006217 struct stop_event_data sd = {
6218 .event = event,
6219 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006220
6221 if (!has_aux(event))
6222 return;
6223
6224 if (!parent)
6225 parent = event;
6226
6227 /*
6228 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03006229 * ring-buffer, but it will be the child that's actually using it.
6230 *
6231 * We are using event::rb to determine if the event should be stopped,
6232 * however this may race with ring_buffer_attach() (through set_output),
6233 * which will make us skip the event that actually needs to be stopped.
6234 * So ring_buffer_attach() has to stop an aux event before re-assigning
6235 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006236 */
6237 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006238 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006239}
6240
6241static int __perf_pmu_output_stop(void *info)
6242{
6243 struct perf_event *event = info;
6244 struct pmu *pmu = event->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01006245 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006246 struct remote_output ro = {
6247 .rb = event->rb,
6248 };
6249
6250 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006251 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006252 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006253 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006254 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006255 rcu_read_unlock();
6256
6257 return ro.err;
6258}
6259
6260static void perf_pmu_output_stop(struct perf_event *event)
6261{
6262 struct perf_event *iter;
6263 int err, cpu;
6264
6265restart:
6266 rcu_read_lock();
6267 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6268 /*
6269 * For per-CPU events, we need to make sure that neither they
6270 * nor their children are running; for cpu==-1 events it's
6271 * sufficient to stop the event itself if it's active, since
6272 * it can't have children.
6273 */
6274 cpu = iter->cpu;
6275 if (cpu == -1)
6276 cpu = READ_ONCE(iter->oncpu);
6277
6278 if (cpu == -1)
6279 continue;
6280
6281 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6282 if (err == -EAGAIN) {
6283 rcu_read_unlock();
6284 goto restart;
6285 }
6286 }
6287 rcu_read_unlock();
6288}
6289
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006290/*
6291 * task tracking -- fork/exit
6292 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006293 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006294 */
6295
6296struct perf_task_event {
6297 struct task_struct *task;
6298 struct perf_event_context *task_ctx;
6299
6300 struct {
6301 struct perf_event_header header;
6302
6303 u32 pid;
6304 u32 ppid;
6305 u32 tid;
6306 u32 ptid;
6307 u64 time;
6308 } event_id;
6309};
6310
Jiri Olsa67516842013-07-09 18:56:31 +02006311static int perf_event_task_match(struct perf_event *event)
6312{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006313 return event->attr.comm || event->attr.mmap ||
6314 event->attr.mmap2 || event->attr.mmap_data ||
6315 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006316}
6317
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006318static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006319 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006320{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006321 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006322 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006323 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006324 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006325 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006326
Jiri Olsa67516842013-07-09 18:56:31 +02006327 if (!perf_event_task_match(event))
6328 return;
6329
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006330 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006331
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006332 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006333 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006334 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006335 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006336
6337 task_event->event_id.pid = perf_event_pid(event, task);
6338 task_event->event_id.ppid = perf_event_pid(event, current);
6339
6340 task_event->event_id.tid = perf_event_tid(event, task);
6341 task_event->event_id.ptid = perf_event_tid(event, current);
6342
Peter Zijlstra34f43922015-02-20 14:05:38 +01006343 task_event->event_id.time = perf_event_clock(event);
6344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006345 perf_output_put(&handle, task_event->event_id);
6346
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006347 perf_event__output_id_sample(event, &handle, &sample);
6348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006349 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006350out:
6351 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006352}
6353
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006354static void perf_event_task(struct task_struct *task,
6355 struct perf_event_context *task_ctx,
6356 int new)
6357{
6358 struct perf_task_event task_event;
6359
6360 if (!atomic_read(&nr_comm_events) &&
6361 !atomic_read(&nr_mmap_events) &&
6362 !atomic_read(&nr_task_events))
6363 return;
6364
6365 task_event = (struct perf_task_event){
6366 .task = task,
6367 .task_ctx = task_ctx,
6368 .event_id = {
6369 .header = {
6370 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6371 .misc = 0,
6372 .size = sizeof(task_event.event_id),
6373 },
6374 /* .pid */
6375 /* .ppid */
6376 /* .tid */
6377 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006378 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006379 },
6380 };
6381
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006382 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006383 &task_event,
6384 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006385}
6386
6387void perf_event_fork(struct task_struct *task)
6388{
6389 perf_event_task(task, NULL, 1);
6390}
6391
6392/*
6393 * comm tracking
6394 */
6395
6396struct perf_comm_event {
6397 struct task_struct *task;
6398 char *comm;
6399 int comm_size;
6400
6401 struct {
6402 struct perf_event_header header;
6403
6404 u32 pid;
6405 u32 tid;
6406 } event_id;
6407};
6408
Jiri Olsa67516842013-07-09 18:56:31 +02006409static int perf_event_comm_match(struct perf_event *event)
6410{
6411 return event->attr.comm;
6412}
6413
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006414static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006415 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006416{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006417 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006418 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006419 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006420 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006421 int ret;
6422
Jiri Olsa67516842013-07-09 18:56:31 +02006423 if (!perf_event_comm_match(event))
6424 return;
6425
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006426 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6427 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006428 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006429
6430 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006431 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006432
6433 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6434 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6435
6436 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006437 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006438 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006439
6440 perf_event__output_id_sample(event, &handle, &sample);
6441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006442 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006443out:
6444 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006445}
6446
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006447static void perf_event_comm_event(struct perf_comm_event *comm_event)
6448{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006449 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006450 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006451
6452 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006453 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006454 size = ALIGN(strlen(comm)+1, sizeof(u64));
6455
6456 comm_event->comm = comm;
6457 comm_event->comm_size = size;
6458
6459 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006460
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006461 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006462 comm_event,
6463 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006464}
6465
Adrian Hunter82b89772014-05-28 11:45:04 +03006466void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006467{
6468 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006469
6470 if (!atomic_read(&nr_comm_events))
6471 return;
6472
6473 comm_event = (struct perf_comm_event){
6474 .task = task,
6475 /* .comm */
6476 /* .comm_size */
6477 .event_id = {
6478 .header = {
6479 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006480 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006481 /* .size */
6482 },
6483 /* .pid */
6484 /* .tid */
6485 },
6486 };
6487
6488 perf_event_comm_event(&comm_event);
6489}
6490
6491/*
6492 * mmap tracking
6493 */
6494
6495struct perf_mmap_event {
6496 struct vm_area_struct *vma;
6497
6498 const char *file_name;
6499 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006500 int maj, min;
6501 u64 ino;
6502 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006503 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006504
6505 struct {
6506 struct perf_event_header header;
6507
6508 u32 pid;
6509 u32 tid;
6510 u64 start;
6511 u64 len;
6512 u64 pgoff;
6513 } event_id;
6514};
6515
Jiri Olsa67516842013-07-09 18:56:31 +02006516static int perf_event_mmap_match(struct perf_event *event,
6517 void *data)
6518{
6519 struct perf_mmap_event *mmap_event = data;
6520 struct vm_area_struct *vma = mmap_event->vma;
6521 int executable = vma->vm_flags & VM_EXEC;
6522
6523 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006524 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006525}
6526
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006527static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006528 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006529{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006530 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006531 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006532 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006534 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006535
Jiri Olsa67516842013-07-09 18:56:31 +02006536 if (!perf_event_mmap_match(event, data))
6537 return;
6538
Stephane Eranian13d7a242013-08-21 12:10:24 +02006539 if (event->attr.mmap2) {
6540 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6541 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6542 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6543 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006544 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006545 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6546 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006547 }
6548
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006549 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6550 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006551 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006552 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006553 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006554
6555 mmap_event->event_id.pid = perf_event_pid(event, current);
6556 mmap_event->event_id.tid = perf_event_tid(event, current);
6557
6558 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006559
6560 if (event->attr.mmap2) {
6561 perf_output_put(&handle, mmap_event->maj);
6562 perf_output_put(&handle, mmap_event->min);
6563 perf_output_put(&handle, mmap_event->ino);
6564 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006565 perf_output_put(&handle, mmap_event->prot);
6566 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006567 }
6568
Frederic Weisbecker76369132011-05-19 19:55:04 +02006569 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006570 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006571
6572 perf_event__output_id_sample(event, &handle, &sample);
6573
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006574 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006575out:
6576 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006577}
6578
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006579static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6580{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006581 struct vm_area_struct *vma = mmap_event->vma;
6582 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006583 int maj = 0, min = 0;
6584 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006585 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006586 unsigned int size;
6587 char tmp[16];
6588 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006589 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006590
6591 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006592 struct inode *inode;
6593 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006594
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006595 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006596 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006597 name = "//enomem";
6598 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006599 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006600 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006601 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006602 * need to add enough zero bytes after the string to handle
6603 * the 64bit alignment we do later.
6604 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006605 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006606 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006607 name = "//toolong";
6608 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006609 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006610 inode = file_inode(vma->vm_file);
6611 dev = inode->i_sb->s_dev;
6612 ino = inode->i_ino;
6613 gen = inode->i_generation;
6614 maj = MAJOR(dev);
6615 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006616
6617 if (vma->vm_flags & VM_READ)
6618 prot |= PROT_READ;
6619 if (vma->vm_flags & VM_WRITE)
6620 prot |= PROT_WRITE;
6621 if (vma->vm_flags & VM_EXEC)
6622 prot |= PROT_EXEC;
6623
6624 if (vma->vm_flags & VM_MAYSHARE)
6625 flags = MAP_SHARED;
6626 else
6627 flags = MAP_PRIVATE;
6628
6629 if (vma->vm_flags & VM_DENYWRITE)
6630 flags |= MAP_DENYWRITE;
6631 if (vma->vm_flags & VM_MAYEXEC)
6632 flags |= MAP_EXECUTABLE;
6633 if (vma->vm_flags & VM_LOCKED)
6634 flags |= MAP_LOCKED;
6635 if (vma->vm_flags & VM_HUGETLB)
6636 flags |= MAP_HUGETLB;
6637
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006638 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006639 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006640 if (vma->vm_ops && vma->vm_ops->name) {
6641 name = (char *) vma->vm_ops->name(vma);
6642 if (name)
6643 goto cpy_name;
6644 }
6645
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006646 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006647 if (name)
6648 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006650 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006651 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006652 name = "[heap]";
6653 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006654 }
6655 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006656 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006657 name = "[stack]";
6658 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006659 }
6660
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006661 name = "//anon";
6662 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006663 }
6664
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006665cpy_name:
6666 strlcpy(tmp, name, sizeof(tmp));
6667 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006668got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006669 /*
6670 * Since our buffer works in 8 byte units we need to align our string
6671 * size to a multiple of 8. However, we must guarantee the tail end is
6672 * zero'd out to avoid leaking random bits to userspace.
6673 */
6674 size = strlen(name)+1;
6675 while (!IS_ALIGNED(size, sizeof(u64)))
6676 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006677
6678 mmap_event->file_name = name;
6679 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006680 mmap_event->maj = maj;
6681 mmap_event->min = min;
6682 mmap_event->ino = ino;
6683 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006684 mmap_event->prot = prot;
6685 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006686
Stephane Eranian2fe85422013-01-24 16:10:39 +01006687 if (!(vma->vm_flags & VM_EXEC))
6688 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6689
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006690 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6691
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006692 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006693 mmap_event,
6694 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006695
6696 kfree(buf);
6697}
6698
Alexander Shishkin375637b2016-04-27 18:44:46 +03006699/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03006700 * Check whether inode and address range match filter criteria.
6701 */
6702static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6703 struct file *file, unsigned long offset,
6704 unsigned long size)
6705{
6706 if (filter->inode != file->f_inode)
6707 return false;
6708
6709 if (filter->offset > offset + size)
6710 return false;
6711
6712 if (filter->offset + filter->size < offset)
6713 return false;
6714
6715 return true;
6716}
6717
6718static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6719{
6720 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6721 struct vm_area_struct *vma = data;
6722 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6723 struct file *file = vma->vm_file;
6724 struct perf_addr_filter *filter;
6725 unsigned int restart = 0, count = 0;
6726
6727 if (!has_addr_filter(event))
6728 return;
6729
6730 if (!file)
6731 return;
6732
6733 raw_spin_lock_irqsave(&ifh->lock, flags);
6734 list_for_each_entry(filter, &ifh->list, entry) {
6735 if (perf_addr_filter_match(filter, file, off,
6736 vma->vm_end - vma->vm_start)) {
6737 event->addr_filters_offs[count] = vma->vm_start;
6738 restart++;
6739 }
6740
6741 count++;
6742 }
6743
6744 if (restart)
6745 event->addr_filters_gen++;
6746 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6747
6748 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006749 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006750}
6751
6752/*
6753 * Adjust all task's events' filters to the new vma
6754 */
6755static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6756{
6757 struct perf_event_context *ctx;
6758 int ctxn;
6759
Mathieu Poirier12b40a22016-07-18 10:43:06 -06006760 /*
6761 * Data tracing isn't supported yet and as such there is no need
6762 * to keep track of anything that isn't related to executable code:
6763 */
6764 if (!(vma->vm_flags & VM_EXEC))
6765 return;
6766
Alexander Shishkin375637b2016-04-27 18:44:46 +03006767 rcu_read_lock();
6768 for_each_task_context_nr(ctxn) {
6769 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6770 if (!ctx)
6771 continue;
6772
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006773 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006774 }
6775 rcu_read_unlock();
6776}
6777
Eric B Munson3af9e852010-05-18 15:30:49 +01006778void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006779{
6780 struct perf_mmap_event mmap_event;
6781
6782 if (!atomic_read(&nr_mmap_events))
6783 return;
6784
6785 mmap_event = (struct perf_mmap_event){
6786 .vma = vma,
6787 /* .file_name */
6788 /* .file_size */
6789 .event_id = {
6790 .header = {
6791 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006792 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006793 /* .size */
6794 },
6795 /* .pid */
6796 /* .tid */
6797 .start = vma->vm_start,
6798 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006799 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006800 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006801 /* .maj (attr_mmap2 only) */
6802 /* .min (attr_mmap2 only) */
6803 /* .ino (attr_mmap2 only) */
6804 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006805 /* .prot (attr_mmap2 only) */
6806 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006807 };
6808
Alexander Shishkin375637b2016-04-27 18:44:46 +03006809 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006810 perf_event_mmap_event(&mmap_event);
6811}
6812
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006813void perf_event_aux_event(struct perf_event *event, unsigned long head,
6814 unsigned long size, u64 flags)
6815{
6816 struct perf_output_handle handle;
6817 struct perf_sample_data sample;
6818 struct perf_aux_event {
6819 struct perf_event_header header;
6820 u64 offset;
6821 u64 size;
6822 u64 flags;
6823 } rec = {
6824 .header = {
6825 .type = PERF_RECORD_AUX,
6826 .misc = 0,
6827 .size = sizeof(rec),
6828 },
6829 .offset = head,
6830 .size = size,
6831 .flags = flags,
6832 };
6833 int ret;
6834
6835 perf_event_header__init_id(&rec.header, &sample, event);
6836 ret = perf_output_begin(&handle, event, rec.header.size);
6837
6838 if (ret)
6839 return;
6840
6841 perf_output_put(&handle, rec);
6842 perf_event__output_id_sample(event, &handle, &sample);
6843
6844 perf_output_end(&handle);
6845}
6846
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006847/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006848 * Lost/dropped samples logging
6849 */
6850void perf_log_lost_samples(struct perf_event *event, u64 lost)
6851{
6852 struct perf_output_handle handle;
6853 struct perf_sample_data sample;
6854 int ret;
6855
6856 struct {
6857 struct perf_event_header header;
6858 u64 lost;
6859 } lost_samples_event = {
6860 .header = {
6861 .type = PERF_RECORD_LOST_SAMPLES,
6862 .misc = 0,
6863 .size = sizeof(lost_samples_event),
6864 },
6865 .lost = lost,
6866 };
6867
6868 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6869
6870 ret = perf_output_begin(&handle, event,
6871 lost_samples_event.header.size);
6872 if (ret)
6873 return;
6874
6875 perf_output_put(&handle, lost_samples_event);
6876 perf_event__output_id_sample(event, &handle, &sample);
6877 perf_output_end(&handle);
6878}
6879
6880/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006881 * context_switch tracking
6882 */
6883
6884struct perf_switch_event {
6885 struct task_struct *task;
6886 struct task_struct *next_prev;
6887
6888 struct {
6889 struct perf_event_header header;
6890 u32 next_prev_pid;
6891 u32 next_prev_tid;
6892 } event_id;
6893};
6894
6895static int perf_event_switch_match(struct perf_event *event)
6896{
6897 return event->attr.context_switch;
6898}
6899
6900static void perf_event_switch_output(struct perf_event *event, void *data)
6901{
6902 struct perf_switch_event *se = data;
6903 struct perf_output_handle handle;
6904 struct perf_sample_data sample;
6905 int ret;
6906
6907 if (!perf_event_switch_match(event))
6908 return;
6909
6910 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6911 if (event->ctx->task) {
6912 se->event_id.header.type = PERF_RECORD_SWITCH;
6913 se->event_id.header.size = sizeof(se->event_id.header);
6914 } else {
6915 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6916 se->event_id.header.size = sizeof(se->event_id);
6917 se->event_id.next_prev_pid =
6918 perf_event_pid(event, se->next_prev);
6919 se->event_id.next_prev_tid =
6920 perf_event_tid(event, se->next_prev);
6921 }
6922
6923 perf_event_header__init_id(&se->event_id.header, &sample, event);
6924
6925 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6926 if (ret)
6927 return;
6928
6929 if (event->ctx->task)
6930 perf_output_put(&handle, se->event_id.header);
6931 else
6932 perf_output_put(&handle, se->event_id);
6933
6934 perf_event__output_id_sample(event, &handle, &sample);
6935
6936 perf_output_end(&handle);
6937}
6938
6939static void perf_event_switch(struct task_struct *task,
6940 struct task_struct *next_prev, bool sched_in)
6941{
6942 struct perf_switch_event switch_event;
6943
6944 /* N.B. caller checks nr_switch_events != 0 */
6945
6946 switch_event = (struct perf_switch_event){
6947 .task = task,
6948 .next_prev = next_prev,
6949 .event_id = {
6950 .header = {
6951 /* .type */
6952 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6953 /* .size */
6954 },
6955 /* .next_prev_pid */
6956 /* .next_prev_tid */
6957 },
6958 };
6959
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006960 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03006961 &switch_event,
6962 NULL);
6963}
6964
6965/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006966 * IRQ throttle logging
6967 */
6968
6969static void perf_log_throttle(struct perf_event *event, int enable)
6970{
6971 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006972 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006973 int ret;
6974
6975 struct {
6976 struct perf_event_header header;
6977 u64 time;
6978 u64 id;
6979 u64 stream_id;
6980 } throttle_event = {
6981 .header = {
6982 .type = PERF_RECORD_THROTTLE,
6983 .misc = 0,
6984 .size = sizeof(throttle_event),
6985 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006986 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006987 .id = primary_event_id(event),
6988 .stream_id = event->id,
6989 };
6990
6991 if (enable)
6992 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6993
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006994 perf_event_header__init_id(&throttle_event.header, &sample, event);
6995
6996 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006997 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006998 if (ret)
6999 return;
7000
7001 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007002 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007003 perf_output_end(&handle);
7004}
7005
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007006static void perf_log_itrace_start(struct perf_event *event)
7007{
7008 struct perf_output_handle handle;
7009 struct perf_sample_data sample;
7010 struct perf_aux_event {
7011 struct perf_event_header header;
7012 u32 pid;
7013 u32 tid;
7014 } rec;
7015 int ret;
7016
7017 if (event->parent)
7018 event = event->parent;
7019
7020 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
7021 event->hw.itrace_started)
7022 return;
7023
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007024 rec.header.type = PERF_RECORD_ITRACE_START;
7025 rec.header.misc = 0;
7026 rec.header.size = sizeof(rec);
7027 rec.pid = perf_event_pid(event, current);
7028 rec.tid = perf_event_tid(event, current);
7029
7030 perf_event_header__init_id(&rec.header, &sample, event);
7031 ret = perf_output_begin(&handle, event, rec.header.size);
7032
7033 if (ret)
7034 return;
7035
7036 perf_output_put(&handle, rec);
7037 perf_event__output_id_sample(event, &handle, &sample);
7038
7039 perf_output_end(&handle);
7040}
7041
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007042/*
7043 * Generic event overflow handling, sampling.
7044 */
7045
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007046static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007047 int throttle, struct perf_sample_data *data,
7048 struct pt_regs *regs)
7049{
7050 int events = atomic_read(&event->event_limit);
7051 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007052 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007053 int ret = 0;
7054
Peter Zijlstra96398822010-11-24 18:55:29 +01007055 /*
7056 * Non-sampling counters might still use the PMI to fold short
7057 * hardware counters, ignore those.
7058 */
7059 if (unlikely(!is_sampling_event(event)))
7060 return 0;
7061
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007062 seq = __this_cpu_read(perf_throttled_seq);
7063 if (seq != hwc->interrupts_seq) {
7064 hwc->interrupts_seq = seq;
7065 hwc->interrupts = 1;
7066 } else {
7067 hwc->interrupts++;
7068 if (unlikely(throttle
7069 && hwc->interrupts >= max_samples_per_tick)) {
7070 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007071 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01007072 hwc->interrupts = MAX_INTERRUPTS;
7073 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007074 ret = 1;
7075 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007076 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007077
7078 if (event->attr.freq) {
7079 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01007080 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007081
Peter Zijlstraabd50712010-01-26 18:50:16 +01007082 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007083
Peter Zijlstraabd50712010-01-26 18:50:16 +01007084 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01007085 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007086 }
7087
7088 /*
7089 * XXX event_limit might not quite work as expected on inherited
7090 * events
7091 */
7092
7093 event->pending_kill = POLL_IN;
7094 if (events && atomic_dec_and_test(&event->event_limit)) {
7095 ret = 1;
7096 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02007097
7098 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007099 }
7100
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007101 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01007102
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02007103 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007104 event->pending_wakeup = 1;
7105 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02007106 }
7107
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007108 return ret;
7109}
7110
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007111int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007112 struct perf_sample_data *data,
7113 struct pt_regs *regs)
7114{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007115 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007116}
7117
7118/*
7119 * Generic software event infrastructure
7120 */
7121
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007122struct swevent_htable {
7123 struct swevent_hlist *swevent_hlist;
7124 struct mutex hlist_mutex;
7125 int hlist_refcount;
7126
7127 /* Recursion avoidance in each contexts */
7128 int recursion[PERF_NR_CONTEXTS];
7129};
7130
7131static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7132
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007133/*
7134 * We directly increment event->count and keep a second value in
7135 * event->hw.period_left to count intervals. This period event
7136 * is kept in the range [-sample_period, 0] so that we can use the
7137 * sign as trigger.
7138 */
7139
Jiri Olsaab573842013-05-01 17:25:44 +02007140u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007141{
7142 struct hw_perf_event *hwc = &event->hw;
7143 u64 period = hwc->last_period;
7144 u64 nr, offset;
7145 s64 old, val;
7146
7147 hwc->last_period = hwc->sample_period;
7148
7149again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02007150 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007151 if (val < 0)
7152 return 0;
7153
7154 nr = div64_u64(period + val, period);
7155 offset = nr * period;
7156 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02007157 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007158 goto again;
7159
7160 return nr;
7161}
7162
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007163static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007164 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007165 struct pt_regs *regs)
7166{
7167 struct hw_perf_event *hwc = &event->hw;
7168 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007169
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007170 if (!overflow)
7171 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007172
7173 if (hwc->interrupts == MAX_INTERRUPTS)
7174 return;
7175
7176 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007177 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007178 data, regs)) {
7179 /*
7180 * We inhibit the overflow from happening when
7181 * hwc->interrupts == MAX_INTERRUPTS.
7182 */
7183 break;
7184 }
7185 throttle = 1;
7186 }
7187}
7188
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007189static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007190 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007191 struct pt_regs *regs)
7192{
7193 struct hw_perf_event *hwc = &event->hw;
7194
Peter Zijlstrae7850592010-05-21 14:43:08 +02007195 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007196
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007197 if (!regs)
7198 return;
7199
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007200 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007201 return;
7202
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03007203 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
7204 data->period = nr;
7205 return perf_swevent_overflow(event, 1, data, regs);
7206 } else
7207 data->period = event->hw.last_period;
7208
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007209 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007210 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007211
Peter Zijlstrae7850592010-05-21 14:43:08 +02007212 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007213 return;
7214
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007215 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007216}
7217
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007218static int perf_exclude_event(struct perf_event *event,
7219 struct pt_regs *regs)
7220{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007221 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01007222 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007223
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007224 if (regs) {
7225 if (event->attr.exclude_user && user_mode(regs))
7226 return 1;
7227
7228 if (event->attr.exclude_kernel && !user_mode(regs))
7229 return 1;
7230 }
7231
7232 return 0;
7233}
7234
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007235static int perf_swevent_match(struct perf_event *event,
7236 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007237 u32 event_id,
7238 struct perf_sample_data *data,
7239 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007240{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007241 if (event->attr.type != type)
7242 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007244 if (event->attr.config != event_id)
7245 return 0;
7246
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007247 if (perf_exclude_event(event, regs))
7248 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007249
7250 return 1;
7251}
7252
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007253static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007254{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007255 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007256
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007257 return hash_64(val, SWEVENT_HLIST_BITS);
7258}
7259
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007260static inline struct hlist_head *
7261__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007262{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007263 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007264
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007265 return &hlist->heads[hash];
7266}
7267
7268/* For the read side: events when they trigger */
7269static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007270find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007271{
7272 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007273
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007274 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007275 if (!hlist)
7276 return NULL;
7277
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007278 return __find_swevent_head(hlist, type, event_id);
7279}
7280
7281/* For the event head insertion and removal in the hlist */
7282static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007283find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007284{
7285 struct swevent_hlist *hlist;
7286 u32 event_id = event->attr.config;
7287 u64 type = event->attr.type;
7288
7289 /*
7290 * Event scheduling is always serialized against hlist allocation
7291 * and release. Which makes the protected version suitable here.
7292 * The context lock guarantees that.
7293 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007294 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007295 lockdep_is_held(&event->ctx->lock));
7296 if (!hlist)
7297 return NULL;
7298
7299 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007300}
7301
7302static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007303 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007304 struct perf_sample_data *data,
7305 struct pt_regs *regs)
7306{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007307 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007308 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007309 struct hlist_head *head;
7310
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007311 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007312 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007313 if (!head)
7314 goto end;
7315
Sasha Levinb67bfe02013-02-27 17:06:00 -08007316 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007317 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007318 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007319 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007320end:
7321 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007322}
7323
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007324DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7325
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007326int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007327{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007328 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007329
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007330 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007331}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007332EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007333
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007334void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007335{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007336 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007337
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007338 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007339}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007340
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007341void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007342{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007343 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007344
7345 if (WARN_ON_ONCE(!regs))
7346 return;
7347
7348 perf_sample_data_init(&data, addr, 0);
7349 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7350}
7351
7352void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7353{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007354 int rctx;
7355
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007356 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007357 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007358 if (unlikely(rctx < 0))
7359 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007360
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007361 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007362
7363 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007364fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007365 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007366}
7367
7368static void perf_swevent_read(struct perf_event *event)
7369{
7370}
7371
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007372static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007373{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007374 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007375 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007376 struct hlist_head *head;
7377
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007378 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007379 hwc->last_period = hwc->sample_period;
7380 perf_swevent_set_period(event);
7381 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007382
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007383 hwc->state = !(flags & PERF_EF_START);
7384
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007385 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007386 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007387 return -EINVAL;
7388
7389 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007390 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007391
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007392 return 0;
7393}
7394
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007395static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007396{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007397 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007398}
7399
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007400static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007401{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007402 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007403}
7404
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007405static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007406{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007407 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007408}
7409
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007410/* Deref the hlist from the update side */
7411static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007412swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007413{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007414 return rcu_dereference_protected(swhash->swevent_hlist,
7415 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007416}
7417
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007418static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007419{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007420 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007421
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007422 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007423 return;
7424
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007425 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007426 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007427}
7428
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007429static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007430{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007431 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007432
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007433 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007434
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007435 if (!--swhash->hlist_refcount)
7436 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007437
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007438 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007439}
7440
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007441static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007442{
7443 int cpu;
7444
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007445 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007446 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007447}
7448
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007449static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007450{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007451 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007452 int err = 0;
7453
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007454 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007455 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007456 struct swevent_hlist *hlist;
7457
7458 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7459 if (!hlist) {
7460 err = -ENOMEM;
7461 goto exit;
7462 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007463 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007464 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007465 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007466exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007467 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007468
7469 return err;
7470}
7471
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007472static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007473{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007474 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007475
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007476 get_online_cpus();
7477 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007478 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007479 if (err) {
7480 failed_cpu = cpu;
7481 goto fail;
7482 }
7483 }
7484 put_online_cpus();
7485
7486 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007487fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007488 for_each_possible_cpu(cpu) {
7489 if (cpu == failed_cpu)
7490 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007491 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007492 }
7493
7494 put_online_cpus();
7495 return err;
7496}
7497
Ingo Molnarc5905af2012-02-24 08:31:31 +01007498struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007499
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007500static void sw_perf_event_destroy(struct perf_event *event)
7501{
7502 u64 event_id = event->attr.config;
7503
7504 WARN_ON(event->parent);
7505
Ingo Molnarc5905af2012-02-24 08:31:31 +01007506 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007507 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007508}
7509
7510static int perf_swevent_init(struct perf_event *event)
7511{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007512 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007513
7514 if (event->attr.type != PERF_TYPE_SOFTWARE)
7515 return -ENOENT;
7516
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007517 /*
7518 * no branch sampling for software events
7519 */
7520 if (has_branch_stack(event))
7521 return -EOPNOTSUPP;
7522
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007523 switch (event_id) {
7524 case PERF_COUNT_SW_CPU_CLOCK:
7525 case PERF_COUNT_SW_TASK_CLOCK:
7526 return -ENOENT;
7527
7528 default:
7529 break;
7530 }
7531
Dan Carpenterce677832010-10-24 21:50:42 +02007532 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007533 return -ENOENT;
7534
7535 if (!event->parent) {
7536 int err;
7537
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007538 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007539 if (err)
7540 return err;
7541
Ingo Molnarc5905af2012-02-24 08:31:31 +01007542 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007543 event->destroy = sw_perf_event_destroy;
7544 }
7545
7546 return 0;
7547}
7548
7549static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007550 .task_ctx_nr = perf_sw_context,
7551
Peter Zijlstra34f43922015-02-20 14:05:38 +01007552 .capabilities = PERF_PMU_CAP_NO_NMI,
7553
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007554 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007555 .add = perf_swevent_add,
7556 .del = perf_swevent_del,
7557 .start = perf_swevent_start,
7558 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007559 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007560};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007561
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007562#ifdef CONFIG_EVENT_TRACING
7563
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007564static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007565 struct perf_sample_data *data)
7566{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007567 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007568
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007569 /* only top level events have filters set */
7570 if (event->parent)
7571 event = event->parent;
7572
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007573 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7574 return 1;
7575 return 0;
7576}
7577
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007578static int perf_tp_event_match(struct perf_event *event,
7579 struct perf_sample_data *data,
7580 struct pt_regs *regs)
7581{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007582 if (event->hw.state & PERF_HES_STOPPED)
7583 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007584 /*
7585 * All tracepoints are from kernel-space.
7586 */
7587 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007588 return 0;
7589
7590 if (!perf_tp_filter_match(event, data))
7591 return 0;
7592
7593 return 1;
7594}
7595
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07007596void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7597 struct trace_event_call *call, u64 count,
7598 struct pt_regs *regs, struct hlist_head *head,
7599 struct task_struct *task)
7600{
7601 struct bpf_prog *prog = call->prog;
7602
7603 if (prog) {
7604 *(struct pt_regs **)raw_data = regs;
7605 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7606 perf_swevent_put_recursion_context(rctx);
7607 return;
7608 }
7609 }
7610 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7611 rctx, task);
7612}
7613EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7614
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007615void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007616 struct pt_regs *regs, struct hlist_head *head, int rctx,
7617 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007618{
7619 struct perf_sample_data data;
7620 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007621
7622 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007623 .frag = {
7624 .size = entry_size,
7625 .data = record,
7626 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007627 };
7628
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007629 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007630 data.raw = &raw;
7631
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007632 perf_trace_buf_update(record, event_type);
7633
Sasha Levinb67bfe02013-02-27 17:06:00 -08007634 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007635 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007636 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007637 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007638
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007639 /*
7640 * If we got specified a target task, also iterate its context and
7641 * deliver this event there too.
7642 */
7643 if (task && task != current) {
7644 struct perf_event_context *ctx;
7645 struct trace_entry *entry = record;
7646
7647 rcu_read_lock();
7648 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7649 if (!ctx)
7650 goto unlock;
7651
7652 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7653 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7654 continue;
7655 if (event->attr.config != entry->type)
7656 continue;
7657 if (perf_tp_event_match(event, &data, regs))
7658 perf_swevent_event(event, count, &data, regs);
7659 }
7660unlock:
7661 rcu_read_unlock();
7662 }
7663
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007664 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007665}
7666EXPORT_SYMBOL_GPL(perf_tp_event);
7667
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007668static void tp_perf_event_destroy(struct perf_event *event)
7669{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007670 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007671}
7672
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007673static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007674{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007675 int err;
7676
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007677 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7678 return -ENOENT;
7679
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007680 /*
7681 * no branch sampling for tracepoint events
7682 */
7683 if (has_branch_stack(event))
7684 return -EOPNOTSUPP;
7685
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007686 err = perf_trace_init(event);
7687 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007688 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007689
7690 event->destroy = tp_perf_event_destroy;
7691
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007692 return 0;
7693}
7694
7695static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007696 .task_ctx_nr = perf_sw_context,
7697
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007698 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007699 .add = perf_trace_add,
7700 .del = perf_trace_del,
7701 .start = perf_swevent_start,
7702 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007703 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007704};
7705
7706static inline void perf_tp_register(void)
7707{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007708 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007709}
Li Zefan6fb29152009-10-15 11:21:42 +08007710
Li Zefan6fb29152009-10-15 11:21:42 +08007711static void perf_event_free_filter(struct perf_event *event)
7712{
7713 ftrace_profile_free_filter(event);
7714}
7715
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007716#ifdef CONFIG_BPF_SYSCALL
7717static void bpf_overflow_handler(struct perf_event *event,
7718 struct perf_sample_data *data,
7719 struct pt_regs *regs)
7720{
7721 struct bpf_perf_event_data_kern ctx = {
7722 .data = data,
7723 .regs = regs,
7724 };
7725 int ret = 0;
7726
7727 preempt_disable();
7728 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
7729 goto out;
7730 rcu_read_lock();
7731 ret = BPF_PROG_RUN(event->prog, (void *)&ctx);
7732 rcu_read_unlock();
7733out:
7734 __this_cpu_dec(bpf_prog_active);
7735 preempt_enable();
7736 if (!ret)
7737 return;
7738
7739 event->orig_overflow_handler(event, data, regs);
7740}
7741
7742static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7743{
7744 struct bpf_prog *prog;
7745
7746 if (event->overflow_handler_context)
7747 /* hw breakpoint or kernel counter */
7748 return -EINVAL;
7749
7750 if (event->prog)
7751 return -EEXIST;
7752
7753 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
7754 if (IS_ERR(prog))
7755 return PTR_ERR(prog);
7756
7757 event->prog = prog;
7758 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
7759 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
7760 return 0;
7761}
7762
7763static void perf_event_free_bpf_handler(struct perf_event *event)
7764{
7765 struct bpf_prog *prog = event->prog;
7766
7767 if (!prog)
7768 return;
7769
7770 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
7771 event->prog = NULL;
7772 bpf_prog_put(prog);
7773}
7774#else
7775static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7776{
7777 return -EOPNOTSUPP;
7778}
7779static void perf_event_free_bpf_handler(struct perf_event *event)
7780{
7781}
7782#endif
7783
Alexei Starovoitov25415172015-03-25 12:49:20 -07007784static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7785{
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007786 bool is_kprobe, is_tracepoint;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007787 struct bpf_prog *prog;
7788
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007789 if (event->attr.type == PERF_TYPE_HARDWARE ||
7790 event->attr.type == PERF_TYPE_SOFTWARE)
7791 return perf_event_set_bpf_handler(event, prog_fd);
7792
Alexei Starovoitov25415172015-03-25 12:49:20 -07007793 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7794 return -EINVAL;
7795
7796 if (event->tp_event->prog)
7797 return -EEXIST;
7798
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007799 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7800 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7801 if (!is_kprobe && !is_tracepoint)
7802 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007803 return -EINVAL;
7804
7805 prog = bpf_prog_get(prog_fd);
7806 if (IS_ERR(prog))
7807 return PTR_ERR(prog);
7808
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007809 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7810 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007811 /* valid fd, but invalid bpf program type */
7812 bpf_prog_put(prog);
7813 return -EINVAL;
7814 }
7815
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07007816 if (is_tracepoint) {
7817 int off = trace_event_get_offsets(event->tp_event);
7818
7819 if (prog->aux->max_ctx_offset > off) {
7820 bpf_prog_put(prog);
7821 return -EACCES;
7822 }
7823 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07007824 event->tp_event->prog = prog;
7825
7826 return 0;
7827}
7828
7829static void perf_event_free_bpf_prog(struct perf_event *event)
7830{
7831 struct bpf_prog *prog;
7832
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007833 perf_event_free_bpf_handler(event);
7834
Alexei Starovoitov25415172015-03-25 12:49:20 -07007835 if (!event->tp_event)
7836 return;
7837
7838 prog = event->tp_event->prog;
7839 if (prog) {
7840 event->tp_event->prog = NULL;
Daniel Borkmann1aacde32016-06-30 17:24:43 +02007841 bpf_prog_put(prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07007842 }
7843}
7844
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007845#else
Li Zefan6fb29152009-10-15 11:21:42 +08007846
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007847static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007848{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007849}
Li Zefan6fb29152009-10-15 11:21:42 +08007850
Li Zefan6fb29152009-10-15 11:21:42 +08007851static void perf_event_free_filter(struct perf_event *event)
7852{
7853}
7854
Alexei Starovoitov25415172015-03-25 12:49:20 -07007855static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7856{
7857 return -ENOENT;
7858}
7859
7860static void perf_event_free_bpf_prog(struct perf_event *event)
7861{
7862}
Li Zefan07b139c2009-12-21 14:27:35 +08007863#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007864
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007865#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007866void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007867{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007868 struct perf_sample_data sample;
7869 struct pt_regs *regs = data;
7870
Robert Richterfd0d0002012-04-02 20:19:08 +02007871 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007872
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007873 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007874 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007875}
7876#endif
7877
Alexander Shishkin375637b2016-04-27 18:44:46 +03007878/*
7879 * Allocate a new address filter
7880 */
7881static struct perf_addr_filter *
7882perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7883{
7884 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7885 struct perf_addr_filter *filter;
7886
7887 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7888 if (!filter)
7889 return NULL;
7890
7891 INIT_LIST_HEAD(&filter->entry);
7892 list_add_tail(&filter->entry, filters);
7893
7894 return filter;
7895}
7896
7897static void free_filters_list(struct list_head *filters)
7898{
7899 struct perf_addr_filter *filter, *iter;
7900
7901 list_for_each_entry_safe(filter, iter, filters, entry) {
7902 if (filter->inode)
7903 iput(filter->inode);
7904 list_del(&filter->entry);
7905 kfree(filter);
7906 }
7907}
7908
7909/*
7910 * Free existing address filters and optionally install new ones
7911 */
7912static void perf_addr_filters_splice(struct perf_event *event,
7913 struct list_head *head)
7914{
7915 unsigned long flags;
7916 LIST_HEAD(list);
7917
7918 if (!has_addr_filter(event))
7919 return;
7920
7921 /* don't bother with children, they don't have their own filters */
7922 if (event->parent)
7923 return;
7924
7925 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
7926
7927 list_splice_init(&event->addr_filters.list, &list);
7928 if (head)
7929 list_splice(head, &event->addr_filters.list);
7930
7931 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
7932
7933 free_filters_list(&list);
7934}
7935
7936/*
7937 * Scan through mm's vmas and see if one of them matches the
7938 * @filter; if so, adjust filter's address range.
7939 * Called with mm::mmap_sem down for reading.
7940 */
7941static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
7942 struct mm_struct *mm)
7943{
7944 struct vm_area_struct *vma;
7945
7946 for (vma = mm->mmap; vma; vma = vma->vm_next) {
7947 struct file *file = vma->vm_file;
7948 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
7949 unsigned long vma_size = vma->vm_end - vma->vm_start;
7950
7951 if (!file)
7952 continue;
7953
7954 if (!perf_addr_filter_match(filter, file, off, vma_size))
7955 continue;
7956
7957 return vma->vm_start;
7958 }
7959
7960 return 0;
7961}
7962
7963/*
7964 * Update event's address range filters based on the
7965 * task's existing mappings, if any.
7966 */
7967static void perf_event_addr_filters_apply(struct perf_event *event)
7968{
7969 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7970 struct task_struct *task = READ_ONCE(event->ctx->task);
7971 struct perf_addr_filter *filter;
7972 struct mm_struct *mm = NULL;
7973 unsigned int count = 0;
7974 unsigned long flags;
7975
7976 /*
7977 * We may observe TASK_TOMBSTONE, which means that the event tear-down
7978 * will stop on the parent's child_mutex that our caller is also holding
7979 */
7980 if (task == TASK_TOMBSTONE)
7981 return;
7982
7983 mm = get_task_mm(event->ctx->task);
7984 if (!mm)
7985 goto restart;
7986
7987 down_read(&mm->mmap_sem);
7988
7989 raw_spin_lock_irqsave(&ifh->lock, flags);
7990 list_for_each_entry(filter, &ifh->list, entry) {
7991 event->addr_filters_offs[count] = 0;
7992
Mathieu Poirier99f5bc92016-07-18 10:43:07 -06007993 /*
7994 * Adjust base offset if the filter is associated to a binary
7995 * that needs to be mapped:
7996 */
7997 if (filter->inode)
Alexander Shishkin375637b2016-04-27 18:44:46 +03007998 event->addr_filters_offs[count] =
7999 perf_addr_filter_apply(filter, mm);
8000
8001 count++;
8002 }
8003
8004 event->addr_filters_gen++;
8005 raw_spin_unlock_irqrestore(&ifh->lock, flags);
8006
8007 up_read(&mm->mmap_sem);
8008
8009 mmput(mm);
8010
8011restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03008012 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008013}
8014
8015/*
8016 * Address range filtering: limiting the data to certain
8017 * instruction address ranges. Filters are ioctl()ed to us from
8018 * userspace as ascii strings.
8019 *
8020 * Filter string format:
8021 *
8022 * ACTION RANGE_SPEC
8023 * where ACTION is one of the
8024 * * "filter": limit the trace to this region
8025 * * "start": start tracing from this address
8026 * * "stop": stop tracing at this address/region;
8027 * RANGE_SPEC is
8028 * * for kernel addresses: <start address>[/<size>]
8029 * * for object files: <start address>[/<size>]@</path/to/object/file>
8030 *
8031 * if <size> is not specified, the range is treated as a single address.
8032 */
8033enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02008034 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03008035 IF_ACT_FILTER,
8036 IF_ACT_START,
8037 IF_ACT_STOP,
8038 IF_SRC_FILE,
8039 IF_SRC_KERNEL,
8040 IF_SRC_FILEADDR,
8041 IF_SRC_KERNELADDR,
8042};
8043
8044enum {
8045 IF_STATE_ACTION = 0,
8046 IF_STATE_SOURCE,
8047 IF_STATE_END,
8048};
8049
8050static const match_table_t if_tokens = {
8051 { IF_ACT_FILTER, "filter" },
8052 { IF_ACT_START, "start" },
8053 { IF_ACT_STOP, "stop" },
8054 { IF_SRC_FILE, "%u/%u@%s" },
8055 { IF_SRC_KERNEL, "%u/%u" },
8056 { IF_SRC_FILEADDR, "%u@%s" },
8057 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +02008058 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +03008059};
8060
8061/*
8062 * Address filter string parser
8063 */
8064static int
8065perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
8066 struct list_head *filters)
8067{
8068 struct perf_addr_filter *filter = NULL;
8069 char *start, *orig, *filename = NULL;
8070 struct path path;
8071 substring_t args[MAX_OPT_ARGS];
8072 int state = IF_STATE_ACTION, token;
8073 unsigned int kernel = 0;
8074 int ret = -EINVAL;
8075
8076 orig = fstr = kstrdup(fstr, GFP_KERNEL);
8077 if (!fstr)
8078 return -ENOMEM;
8079
8080 while ((start = strsep(&fstr, " ,\n")) != NULL) {
8081 ret = -EINVAL;
8082
8083 if (!*start)
8084 continue;
8085
8086 /* filter definition begins */
8087 if (state == IF_STATE_ACTION) {
8088 filter = perf_addr_filter_new(event, filters);
8089 if (!filter)
8090 goto fail;
8091 }
8092
8093 token = match_token(start, if_tokens, args);
8094 switch (token) {
8095 case IF_ACT_FILTER:
8096 case IF_ACT_START:
8097 filter->filter = 1;
8098
8099 case IF_ACT_STOP:
8100 if (state != IF_STATE_ACTION)
8101 goto fail;
8102
8103 state = IF_STATE_SOURCE;
8104 break;
8105
8106 case IF_SRC_KERNELADDR:
8107 case IF_SRC_KERNEL:
8108 kernel = 1;
8109
8110 case IF_SRC_FILEADDR:
8111 case IF_SRC_FILE:
8112 if (state != IF_STATE_SOURCE)
8113 goto fail;
8114
8115 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
8116 filter->range = 1;
8117
8118 *args[0].to = 0;
8119 ret = kstrtoul(args[0].from, 0, &filter->offset);
8120 if (ret)
8121 goto fail;
8122
8123 if (filter->range) {
8124 *args[1].to = 0;
8125 ret = kstrtoul(args[1].from, 0, &filter->size);
8126 if (ret)
8127 goto fail;
8128 }
8129
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06008130 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
8131 int fpos = filter->range ? 2 : 1;
8132
8133 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008134 if (!filename) {
8135 ret = -ENOMEM;
8136 goto fail;
8137 }
8138 }
8139
8140 state = IF_STATE_END;
8141 break;
8142
8143 default:
8144 goto fail;
8145 }
8146
8147 /*
8148 * Filter definition is fully parsed, validate and install it.
8149 * Make sure that it doesn't contradict itself or the event's
8150 * attribute.
8151 */
8152 if (state == IF_STATE_END) {
8153 if (kernel && event->attr.exclude_kernel)
8154 goto fail;
8155
8156 if (!kernel) {
8157 if (!filename)
8158 goto fail;
8159
8160 /* look up the path and grab its inode */
8161 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
8162 if (ret)
8163 goto fail_free_name;
8164
8165 filter->inode = igrab(d_inode(path.dentry));
8166 path_put(&path);
8167 kfree(filename);
8168 filename = NULL;
8169
8170 ret = -EINVAL;
8171 if (!filter->inode ||
8172 !S_ISREG(filter->inode->i_mode))
8173 /* free_filters_list() will iput() */
8174 goto fail;
8175 }
8176
8177 /* ready to consume more filters */
8178 state = IF_STATE_ACTION;
8179 filter = NULL;
8180 }
8181 }
8182
8183 if (state != IF_STATE_ACTION)
8184 goto fail;
8185
8186 kfree(orig);
8187
8188 return 0;
8189
8190fail_free_name:
8191 kfree(filename);
8192fail:
8193 free_filters_list(filters);
8194 kfree(orig);
8195
8196 return ret;
8197}
8198
8199static int
8200perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
8201{
8202 LIST_HEAD(filters);
8203 int ret;
8204
8205 /*
8206 * Since this is called in perf_ioctl() path, we're already holding
8207 * ctx::mutex.
8208 */
8209 lockdep_assert_held(&event->ctx->mutex);
8210
8211 if (WARN_ON_ONCE(event->parent))
8212 return -EINVAL;
8213
8214 /*
8215 * For now, we only support filtering in per-task events; doing so
8216 * for CPU-wide events requires additional context switching trickery,
8217 * since same object code will be mapped at different virtual
8218 * addresses in different processes.
8219 */
8220 if (!event->ctx->task)
8221 return -EOPNOTSUPP;
8222
8223 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
8224 if (ret)
8225 return ret;
8226
8227 ret = event->pmu->addr_filters_validate(&filters);
8228 if (ret) {
8229 free_filters_list(&filters);
8230 return ret;
8231 }
8232
8233 /* remove existing filters, if any */
8234 perf_addr_filters_splice(event, &filters);
8235
8236 /* install new filters */
8237 perf_event_for_each_child(event, perf_event_addr_filters_apply);
8238
8239 return ret;
8240}
8241
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008242static int perf_event_set_filter(struct perf_event *event, void __user *arg)
8243{
8244 char *filter_str;
8245 int ret = -EINVAL;
8246
Alexander Shishkin375637b2016-04-27 18:44:46 +03008247 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
8248 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
8249 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008250 return -EINVAL;
8251
8252 filter_str = strndup_user(arg, PAGE_SIZE);
8253 if (IS_ERR(filter_str))
8254 return PTR_ERR(filter_str);
8255
8256 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
8257 event->attr.type == PERF_TYPE_TRACEPOINT)
8258 ret = ftrace_profile_set_filter(event, event->attr.config,
8259 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008260 else if (has_addr_filter(event))
8261 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008262
8263 kfree(filter_str);
8264 return ret;
8265}
8266
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008267/*
8268 * hrtimer based swevent callback
8269 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008270
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008271static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008272{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008273 enum hrtimer_restart ret = HRTIMER_RESTART;
8274 struct perf_sample_data data;
8275 struct pt_regs *regs;
8276 struct perf_event *event;
8277 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008278
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008279 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008280
8281 if (event->state != PERF_EVENT_STATE_ACTIVE)
8282 return HRTIMER_NORESTART;
8283
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008284 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008285
Robert Richterfd0d0002012-04-02 20:19:08 +02008286 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008287 regs = get_irq_regs();
8288
8289 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08008290 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02008291 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008292 ret = HRTIMER_NORESTART;
8293 }
8294
8295 period = max_t(u64, 10000, event->hw.sample_period);
8296 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
8297
8298 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008299}
8300
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008301static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008302{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008303 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008304 s64 period;
8305
8306 if (!is_sampling_event(event))
8307 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008308
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008309 period = local64_read(&hwc->period_left);
8310 if (period) {
8311 if (period < 0)
8312 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008313
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008314 local64_set(&hwc->period_left, 0);
8315 } else {
8316 period = max_t(u64, 10000, hwc->sample_period);
8317 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00008318 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8319 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008320}
8321
8322static void perf_swevent_cancel_hrtimer(struct perf_event *event)
8323{
8324 struct hw_perf_event *hwc = &event->hw;
8325
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008326 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008327 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008328 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008329
8330 hrtimer_cancel(&hwc->hrtimer);
8331 }
8332}
8333
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008334static void perf_swevent_init_hrtimer(struct perf_event *event)
8335{
8336 struct hw_perf_event *hwc = &event->hw;
8337
8338 if (!is_sampling_event(event))
8339 return;
8340
8341 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8342 hwc->hrtimer.function = perf_swevent_hrtimer;
8343
8344 /*
8345 * Since hrtimers have a fixed rate, we can do a static freq->period
8346 * mapping and avoid the whole period adjust feedback stuff.
8347 */
8348 if (event->attr.freq) {
8349 long freq = event->attr.sample_freq;
8350
8351 event->attr.sample_period = NSEC_PER_SEC / freq;
8352 hwc->sample_period = event->attr.sample_period;
8353 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008354 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008355 event->attr.freq = 0;
8356 }
8357}
8358
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008359/*
8360 * Software event: cpu wall time clock
8361 */
8362
8363static void cpu_clock_event_update(struct perf_event *event)
8364{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008365 s64 prev;
8366 u64 now;
8367
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008368 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008369 prev = local64_xchg(&event->hw.prev_count, now);
8370 local64_add(now - prev, &event->count);
8371}
8372
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008373static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008374{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008375 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008376 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008377}
8378
8379static void cpu_clock_event_stop(struct perf_event *event, int flags)
8380{
8381 perf_swevent_cancel_hrtimer(event);
8382 cpu_clock_event_update(event);
8383}
8384
8385static int cpu_clock_event_add(struct perf_event *event, int flags)
8386{
8387 if (flags & PERF_EF_START)
8388 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008389 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008390
8391 return 0;
8392}
8393
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008394static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008395{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008396 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008397}
8398
8399static void cpu_clock_event_read(struct perf_event *event)
8400{
8401 cpu_clock_event_update(event);
8402}
8403
8404static int cpu_clock_event_init(struct perf_event *event)
8405{
8406 if (event->attr.type != PERF_TYPE_SOFTWARE)
8407 return -ENOENT;
8408
8409 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8410 return -ENOENT;
8411
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008412 /*
8413 * no branch sampling for software events
8414 */
8415 if (has_branch_stack(event))
8416 return -EOPNOTSUPP;
8417
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008418 perf_swevent_init_hrtimer(event);
8419
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008420 return 0;
8421}
8422
8423static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008424 .task_ctx_nr = perf_sw_context,
8425
Peter Zijlstra34f43922015-02-20 14:05:38 +01008426 .capabilities = PERF_PMU_CAP_NO_NMI,
8427
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008428 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008429 .add = cpu_clock_event_add,
8430 .del = cpu_clock_event_del,
8431 .start = cpu_clock_event_start,
8432 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008433 .read = cpu_clock_event_read,
8434};
8435
8436/*
8437 * Software event: task time clock
8438 */
8439
8440static void task_clock_event_update(struct perf_event *event, u64 now)
8441{
8442 u64 prev;
8443 s64 delta;
8444
8445 prev = local64_xchg(&event->hw.prev_count, now);
8446 delta = now - prev;
8447 local64_add(delta, &event->count);
8448}
8449
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008450static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008451{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008452 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008453 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008454}
8455
8456static void task_clock_event_stop(struct perf_event *event, int flags)
8457{
8458 perf_swevent_cancel_hrtimer(event);
8459 task_clock_event_update(event, event->ctx->time);
8460}
8461
8462static int task_clock_event_add(struct perf_event *event, int flags)
8463{
8464 if (flags & PERF_EF_START)
8465 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008466 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008467
8468 return 0;
8469}
8470
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008471static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008472{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008473 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008474}
8475
8476static void task_clock_event_read(struct perf_event *event)
8477{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008478 u64 now = perf_clock();
8479 u64 delta = now - event->ctx->timestamp;
8480 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008481
8482 task_clock_event_update(event, time);
8483}
8484
8485static int task_clock_event_init(struct perf_event *event)
8486{
8487 if (event->attr.type != PERF_TYPE_SOFTWARE)
8488 return -ENOENT;
8489
8490 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8491 return -ENOENT;
8492
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008493 /*
8494 * no branch sampling for software events
8495 */
8496 if (has_branch_stack(event))
8497 return -EOPNOTSUPP;
8498
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008499 perf_swevent_init_hrtimer(event);
8500
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008501 return 0;
8502}
8503
8504static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008505 .task_ctx_nr = perf_sw_context,
8506
Peter Zijlstra34f43922015-02-20 14:05:38 +01008507 .capabilities = PERF_PMU_CAP_NO_NMI,
8508
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008509 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008510 .add = task_clock_event_add,
8511 .del = task_clock_event_del,
8512 .start = task_clock_event_start,
8513 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008514 .read = task_clock_event_read,
8515};
8516
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008517static void perf_pmu_nop_void(struct pmu *pmu)
8518{
8519}
8520
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008521static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8522{
8523}
8524
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008525static int perf_pmu_nop_int(struct pmu *pmu)
8526{
8527 return 0;
8528}
8529
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008530static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008531
8532static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008533{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008534 __this_cpu_write(nop_txn_flags, flags);
8535
8536 if (flags & ~PERF_PMU_TXN_ADD)
8537 return;
8538
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008539 perf_pmu_disable(pmu);
8540}
8541
8542static int perf_pmu_commit_txn(struct pmu *pmu)
8543{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008544 unsigned int flags = __this_cpu_read(nop_txn_flags);
8545
8546 __this_cpu_write(nop_txn_flags, 0);
8547
8548 if (flags & ~PERF_PMU_TXN_ADD)
8549 return 0;
8550
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008551 perf_pmu_enable(pmu);
8552 return 0;
8553}
8554
8555static void perf_pmu_cancel_txn(struct pmu *pmu)
8556{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008557 unsigned int flags = __this_cpu_read(nop_txn_flags);
8558
8559 __this_cpu_write(nop_txn_flags, 0);
8560
8561 if (flags & ~PERF_PMU_TXN_ADD)
8562 return;
8563
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008564 perf_pmu_enable(pmu);
8565}
8566
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008567static int perf_event_idx_default(struct perf_event *event)
8568{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008569 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008570}
8571
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008572/*
8573 * Ensures all contexts with the same task_ctx_nr have the same
8574 * pmu_cpu_context too.
8575 */
Mark Rutland9e317042014-02-10 17:44:18 +00008576static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008577{
8578 struct pmu *pmu;
8579
8580 if (ctxn < 0)
8581 return NULL;
8582
8583 list_for_each_entry(pmu, &pmus, entry) {
8584 if (pmu->task_ctx_nr == ctxn)
8585 return pmu->pmu_cpu_context;
8586 }
8587
8588 return NULL;
8589}
8590
Peter Zijlstra51676952010-12-07 14:18:20 +01008591static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008592{
Peter Zijlstra51676952010-12-07 14:18:20 +01008593 int cpu;
8594
8595 for_each_possible_cpu(cpu) {
8596 struct perf_cpu_context *cpuctx;
8597
8598 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8599
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008600 if (cpuctx->unique_pmu == old_pmu)
8601 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008602 }
8603}
8604
8605static void free_pmu_context(struct pmu *pmu)
8606{
8607 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008608
8609 mutex_lock(&pmus_lock);
8610 /*
8611 * Like a real lame refcount.
8612 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008613 list_for_each_entry(i, &pmus, entry) {
8614 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8615 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008616 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008617 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008618 }
8619
Peter Zijlstra51676952010-12-07 14:18:20 +01008620 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008621out:
8622 mutex_unlock(&pmus_lock);
8623}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008624
8625/*
8626 * Let userspace know that this PMU supports address range filtering:
8627 */
8628static ssize_t nr_addr_filters_show(struct device *dev,
8629 struct device_attribute *attr,
8630 char *page)
8631{
8632 struct pmu *pmu = dev_get_drvdata(dev);
8633
8634 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8635}
8636DEVICE_ATTR_RO(nr_addr_filters);
8637
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008638static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008639
Peter Zijlstraabe43402010-11-17 23:17:37 +01008640static ssize_t
8641type_show(struct device *dev, struct device_attribute *attr, char *page)
8642{
8643 struct pmu *pmu = dev_get_drvdata(dev);
8644
8645 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8646}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008647static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008648
Stephane Eranian62b85632013-04-03 14:21:34 +02008649static ssize_t
8650perf_event_mux_interval_ms_show(struct device *dev,
8651 struct device_attribute *attr,
8652 char *page)
8653{
8654 struct pmu *pmu = dev_get_drvdata(dev);
8655
8656 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8657}
8658
Peter Zijlstra272325c2015-04-15 11:41:58 +02008659static DEFINE_MUTEX(mux_interval_mutex);
8660
Stephane Eranian62b85632013-04-03 14:21:34 +02008661static ssize_t
8662perf_event_mux_interval_ms_store(struct device *dev,
8663 struct device_attribute *attr,
8664 const char *buf, size_t count)
8665{
8666 struct pmu *pmu = dev_get_drvdata(dev);
8667 int timer, cpu, ret;
8668
8669 ret = kstrtoint(buf, 0, &timer);
8670 if (ret)
8671 return ret;
8672
8673 if (timer < 1)
8674 return -EINVAL;
8675
8676 /* same value, noting to do */
8677 if (timer == pmu->hrtimer_interval_ms)
8678 return count;
8679
Peter Zijlstra272325c2015-04-15 11:41:58 +02008680 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008681 pmu->hrtimer_interval_ms = timer;
8682
8683 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008684 get_online_cpus();
8685 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008686 struct perf_cpu_context *cpuctx;
8687 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8688 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8689
Peter Zijlstra272325c2015-04-15 11:41:58 +02008690 cpu_function_call(cpu,
8691 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008692 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008693 put_online_cpus();
8694 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008695
8696 return count;
8697}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008698static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008699
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008700static struct attribute *pmu_dev_attrs[] = {
8701 &dev_attr_type.attr,
8702 &dev_attr_perf_event_mux_interval_ms.attr,
8703 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008704};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008705ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008706
8707static int pmu_bus_running;
8708static struct bus_type pmu_bus = {
8709 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008710 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008711};
8712
8713static void pmu_dev_release(struct device *dev)
8714{
8715 kfree(dev);
8716}
8717
8718static int pmu_dev_alloc(struct pmu *pmu)
8719{
8720 int ret = -ENOMEM;
8721
8722 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8723 if (!pmu->dev)
8724 goto out;
8725
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008726 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008727 device_initialize(pmu->dev);
8728 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8729 if (ret)
8730 goto free_dev;
8731
8732 dev_set_drvdata(pmu->dev, pmu);
8733 pmu->dev->bus = &pmu_bus;
8734 pmu->dev->release = pmu_dev_release;
8735 ret = device_add(pmu->dev);
8736 if (ret)
8737 goto free_dev;
8738
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008739 /* For PMUs with address filters, throw in an extra attribute: */
8740 if (pmu->nr_addr_filters)
8741 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8742
8743 if (ret)
8744 goto del_dev;
8745
Peter Zijlstraabe43402010-11-17 23:17:37 +01008746out:
8747 return ret;
8748
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008749del_dev:
8750 device_del(pmu->dev);
8751
Peter Zijlstraabe43402010-11-17 23:17:37 +01008752free_dev:
8753 put_device(pmu->dev);
8754 goto out;
8755}
8756
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008757static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008758static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008759
Mischa Jonker03d8e802013-06-04 11:45:48 +02008760int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008761{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008762 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008763
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008764 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008765 ret = -ENOMEM;
8766 pmu->pmu_disable_count = alloc_percpu(int);
8767 if (!pmu->pmu_disable_count)
8768 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008769
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008770 pmu->type = -1;
8771 if (!name)
8772 goto skip_type;
8773 pmu->name = name;
8774
8775 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008776 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8777 if (type < 0) {
8778 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008779 goto free_pdc;
8780 }
8781 }
8782 pmu->type = type;
8783
Peter Zijlstraabe43402010-11-17 23:17:37 +01008784 if (pmu_bus_running) {
8785 ret = pmu_dev_alloc(pmu);
8786 if (ret)
8787 goto free_idr;
8788 }
8789
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008790skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008791 if (pmu->task_ctx_nr == perf_hw_context) {
8792 static int hw_context_taken = 0;
8793
Mark Rutland5101ef22016-04-26 11:33:46 +01008794 /*
8795 * Other than systems with heterogeneous CPUs, it never makes
8796 * sense for two PMUs to share perf_hw_context. PMUs which are
8797 * uncore must use perf_invalid_context.
8798 */
8799 if (WARN_ON_ONCE(hw_context_taken &&
8800 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008801 pmu->task_ctx_nr = perf_invalid_context;
8802
8803 hw_context_taken = 1;
8804 }
8805
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008806 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8807 if (pmu->pmu_cpu_context)
8808 goto got_cpu_context;
8809
Wei Yongjunc4814202013-04-12 11:05:54 +08008810 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008811 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8812 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008813 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008814
8815 for_each_possible_cpu(cpu) {
8816 struct perf_cpu_context *cpuctx;
8817
8818 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008819 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008820 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008821 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008822 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008823
Peter Zijlstra272325c2015-04-15 11:41:58 +02008824 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008825
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008826 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008827 }
8828
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008829got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008830 if (!pmu->start_txn) {
8831 if (pmu->pmu_enable) {
8832 /*
8833 * If we have pmu_enable/pmu_disable calls, install
8834 * transaction stubs that use that to try and batch
8835 * hardware accesses.
8836 */
8837 pmu->start_txn = perf_pmu_start_txn;
8838 pmu->commit_txn = perf_pmu_commit_txn;
8839 pmu->cancel_txn = perf_pmu_cancel_txn;
8840 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008841 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008842 pmu->commit_txn = perf_pmu_nop_int;
8843 pmu->cancel_txn = perf_pmu_nop_void;
8844 }
8845 }
8846
8847 if (!pmu->pmu_enable) {
8848 pmu->pmu_enable = perf_pmu_nop_void;
8849 pmu->pmu_disable = perf_pmu_nop_void;
8850 }
8851
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008852 if (!pmu->event_idx)
8853 pmu->event_idx = perf_event_idx_default;
8854
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008855 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008856 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008857 ret = 0;
8858unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008859 mutex_unlock(&pmus_lock);
8860
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008861 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008862
Peter Zijlstraabe43402010-11-17 23:17:37 +01008863free_dev:
8864 device_del(pmu->dev);
8865 put_device(pmu->dev);
8866
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008867free_idr:
8868 if (pmu->type >= PERF_TYPE_MAX)
8869 idr_remove(&pmu_idr, pmu->type);
8870
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008871free_pdc:
8872 free_percpu(pmu->pmu_disable_count);
8873 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008874}
Yan, Zhengc464c762014-03-18 16:56:41 +08008875EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008876
8877void perf_pmu_unregister(struct pmu *pmu)
8878{
Jiri Olsa09338402016-10-20 13:10:11 +02008879 int remove_device;
8880
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008881 mutex_lock(&pmus_lock);
Jiri Olsa09338402016-10-20 13:10:11 +02008882 remove_device = pmu_bus_running;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008883 list_del_rcu(&pmu->entry);
8884 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008885
8886 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02008887 * We dereference the pmu list under both SRCU and regular RCU, so
8888 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008889 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008890 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02008891 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008892
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008893 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008894 if (pmu->type >= PERF_TYPE_MAX)
8895 idr_remove(&pmu_idr, pmu->type);
Jiri Olsa09338402016-10-20 13:10:11 +02008896 if (remove_device) {
8897 if (pmu->nr_addr_filters)
8898 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
8899 device_del(pmu->dev);
8900 put_device(pmu->dev);
8901 }
Peter Zijlstra51676952010-12-07 14:18:20 +01008902 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008903}
Yan, Zhengc464c762014-03-18 16:56:41 +08008904EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008905
Mark Rutlandcc34b982015-01-07 14:56:51 +00008906static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
8907{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008908 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00008909 int ret;
8910
8911 if (!try_module_get(pmu->module))
8912 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008913
8914 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02008915 /*
8916 * This ctx->mutex can nest when we're called through
8917 * inheritance. See the perf_event_ctx_lock_nested() comment.
8918 */
8919 ctx = perf_event_ctx_lock_nested(event->group_leader,
8920 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008921 BUG_ON(!ctx);
8922 }
8923
Mark Rutlandcc34b982015-01-07 14:56:51 +00008924 event->pmu = pmu;
8925 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008926
8927 if (ctx)
8928 perf_event_ctx_unlock(event->group_leader, ctx);
8929
Mark Rutlandcc34b982015-01-07 14:56:51 +00008930 if (ret)
8931 module_put(pmu->module);
8932
8933 return ret;
8934}
8935
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008936static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008937{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008938 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008939 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08008940 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008941
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008942 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008943
8944 rcu_read_lock();
8945 pmu = idr_find(&pmu_idr, event->attr.type);
8946 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08008947 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008948 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08008949 if (ret)
8950 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008951 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08008952 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008953
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008954 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008955 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008956 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008957 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008958
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008959 if (ret != -ENOENT) {
8960 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008961 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008962 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008963 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008964 pmu = ERR_PTR(-ENOENT);
8965unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008966 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008967
8968 return pmu;
8969}
8970
Kan Liangf2fb6be2016-03-23 11:24:37 -07008971static void attach_sb_event(struct perf_event *event)
8972{
8973 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
8974
8975 raw_spin_lock(&pel->lock);
8976 list_add_rcu(&event->sb_list, &pel->list);
8977 raw_spin_unlock(&pel->lock);
8978}
8979
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008980/*
8981 * We keep a list of all !task (and therefore per-cpu) events
8982 * that need to receive side-band records.
8983 *
8984 * This avoids having to scan all the various PMU per-cpu contexts
8985 * looking for them.
8986 */
Kan Liangf2fb6be2016-03-23 11:24:37 -07008987static void account_pmu_sb_event(struct perf_event *event)
8988{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07008989 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -07008990 attach_sb_event(event);
8991}
8992
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008993static void account_event_cpu(struct perf_event *event, int cpu)
8994{
8995 if (event->parent)
8996 return;
8997
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008998 if (is_cgroup_event(event))
8999 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
9000}
9001
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009002/* Freq events need the tick to stay alive (see perf_event_task_tick). */
9003static void account_freq_event_nohz(void)
9004{
9005#ifdef CONFIG_NO_HZ_FULL
9006 /* Lock so we don't race with concurrent unaccount */
9007 spin_lock(&nr_freq_lock);
9008 if (atomic_inc_return(&nr_freq_events) == 1)
9009 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
9010 spin_unlock(&nr_freq_lock);
9011#endif
9012}
9013
9014static void account_freq_event(void)
9015{
9016 if (tick_nohz_full_enabled())
9017 account_freq_event_nohz();
9018 else
9019 atomic_inc(&nr_freq_events);
9020}
9021
9022
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009023static void account_event(struct perf_event *event)
9024{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009025 bool inc = false;
9026
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009027 if (event->parent)
9028 return;
9029
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009030 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009031 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009032 if (event->attr.mmap || event->attr.mmap_data)
9033 atomic_inc(&nr_mmap_events);
9034 if (event->attr.comm)
9035 atomic_inc(&nr_comm_events);
9036 if (event->attr.task)
9037 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009038 if (event->attr.freq)
9039 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03009040 if (event->attr.context_switch) {
9041 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009042 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03009043 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009044 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009045 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009046 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009047 inc = true;
9048
Peter Zijlstra9107c892016-02-24 18:45:45 +01009049 if (inc) {
9050 if (atomic_inc_not_zero(&perf_sched_count))
9051 goto enabled;
9052
9053 mutex_lock(&perf_sched_mutex);
9054 if (!atomic_read(&perf_sched_count)) {
9055 static_branch_enable(&perf_sched_events);
9056 /*
9057 * Guarantee that all CPUs observe they key change and
9058 * call the perf scheduling hooks before proceeding to
9059 * install events that need them.
9060 */
9061 synchronize_sched();
9062 }
9063 /*
9064 * Now that we have waited for the sync_sched(), allow further
9065 * increments to by-pass the mutex.
9066 */
9067 atomic_inc(&perf_sched_count);
9068 mutex_unlock(&perf_sched_mutex);
9069 }
9070enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009071
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009072 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07009073
9074 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009075}
9076
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009077/*
9078 * Allocate and initialize a event structure
9079 */
9080static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009081perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009082 struct task_struct *task,
9083 struct perf_event *group_leader,
9084 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009085 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00009086 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009087{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009088 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009089 struct perf_event *event;
9090 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009091 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009092
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009093 if ((unsigned)cpu >= nr_cpu_ids) {
9094 if (!task || cpu != -1)
9095 return ERR_PTR(-EINVAL);
9096 }
9097
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009098 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009099 if (!event)
9100 return ERR_PTR(-ENOMEM);
9101
9102 /*
9103 * Single events are their own group leaders, with an
9104 * empty sibling list:
9105 */
9106 if (!group_leader)
9107 group_leader = event;
9108
9109 mutex_init(&event->child_mutex);
9110 INIT_LIST_HEAD(&event->child_list);
9111
9112 INIT_LIST_HEAD(&event->group_entry);
9113 INIT_LIST_HEAD(&event->event_entry);
9114 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009115 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01009116 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009117 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01009118 INIT_HLIST_NODE(&event->hlist_entry);
9119
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009120
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009121 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08009122 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009123
9124 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009125 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009126
Al Viroa6fa9412012-08-20 14:59:25 +01009127 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009128 event->cpu = cpu;
9129 event->attr = *attr;
9130 event->group_leader = group_leader;
9131 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009132 event->oncpu = -1;
9133
9134 event->parent = parent_event;
9135
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08009136 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009137 event->id = atomic64_inc_return(&perf_event_id);
9138
9139 event->state = PERF_EVENT_STATE_INACTIVE;
9140
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009141 if (task) {
9142 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009143 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009144 * XXX pmu::event_init needs to know what task to account to
9145 * and we cannot use the ctx information because we need the
9146 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009147 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009148 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009149 }
9150
Peter Zijlstra34f43922015-02-20 14:05:38 +01009151 event->clock = &local_clock;
9152 if (parent_event)
9153 event->clock = parent_event->clock;
9154
Avi Kivity4dc0da82011-06-29 18:42:35 +03009155 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01009156 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009157 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +02009158#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009159 if (overflow_handler == bpf_overflow_handler) {
9160 struct bpf_prog *prog = bpf_prog_inc(parent_event->prog);
9161
9162 if (IS_ERR(prog)) {
9163 err = PTR_ERR(prog);
9164 goto err_ns;
9165 }
9166 event->prog = prog;
9167 event->orig_overflow_handler =
9168 parent_event->orig_overflow_handler;
9169 }
9170#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +03009171 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009172
Wang Nan18794452016-03-28 06:41:30 +00009173 if (overflow_handler) {
9174 event->overflow_handler = overflow_handler;
9175 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00009176 } else if (is_write_backward(event)){
9177 event->overflow_handler = perf_event_output_backward;
9178 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00009179 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00009180 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00009181 event->overflow_handler_context = NULL;
9182 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02009183
Jiri Olsa0231bb52013-02-01 11:23:45 +01009184 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009185
9186 pmu = NULL;
9187
9188 hwc = &event->hw;
9189 hwc->sample_period = attr->sample_period;
9190 if (attr->freq && attr->sample_freq)
9191 hwc->sample_period = 1;
9192 hwc->last_period = hwc->sample_period;
9193
Peter Zijlstrae7850592010-05-21 14:43:08 +02009194 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009195
9196 /*
9197 * we currently do not support PERF_FORMAT_GROUP on inherited events
9198 */
9199 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009200 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009201
Yan, Zhenga46a2302014-11-04 21:56:06 -05009202 if (!has_branch_stack(event))
9203 event->attr.branch_sample_type = 0;
9204
Matt Fleming79dff512015-01-23 18:45:42 +00009205 if (cgroup_fd != -1) {
9206 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
9207 if (err)
9208 goto err_ns;
9209 }
9210
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009211 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009212 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009213 goto err_ns;
9214 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009215 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009216 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009217 }
9218
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009219 err = exclusive_event_init(event);
9220 if (err)
9221 goto err_pmu;
9222
Alexander Shishkin375637b2016-04-27 18:44:46 +03009223 if (has_addr_filter(event)) {
9224 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
9225 sizeof(unsigned long),
9226 GFP_KERNEL);
9227 if (!event->addr_filters_offs)
9228 goto err_per_task;
9229
9230 /* force hw sync on the address filters */
9231 event->addr_filters_gen = 1;
9232 }
9233
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009234 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009235 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009236 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009237 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009238 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01009239 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009240 }
9241
Alexander Shishkin927a5572016-03-02 13:24:14 +02009242 /* symmetric to unaccount_event() in _free_event() */
9243 account_event(event);
9244
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009245 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009246
Alexander Shishkin375637b2016-04-27 18:44:46 +03009247err_addr_filters:
9248 kfree(event->addr_filters_offs);
9249
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009250err_per_task:
9251 exclusive_event_destroy(event);
9252
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009253err_pmu:
9254 if (event->destroy)
9255 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08009256 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009257err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00009258 if (is_cgroup_event(event))
9259 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009260 if (event->ns)
9261 put_pid_ns(event->ns);
9262 kfree(event);
9263
9264 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009265}
9266
9267static int perf_copy_attr(struct perf_event_attr __user *uattr,
9268 struct perf_event_attr *attr)
9269{
9270 u32 size;
9271 int ret;
9272
9273 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
9274 return -EFAULT;
9275
9276 /*
9277 * zero the full structure, so that a short copy will be nice.
9278 */
9279 memset(attr, 0, sizeof(*attr));
9280
9281 ret = get_user(size, &uattr->size);
9282 if (ret)
9283 return ret;
9284
9285 if (size > PAGE_SIZE) /* silly large */
9286 goto err_size;
9287
9288 if (!size) /* abi compat */
9289 size = PERF_ATTR_SIZE_VER0;
9290
9291 if (size < PERF_ATTR_SIZE_VER0)
9292 goto err_size;
9293
9294 /*
9295 * If we're handed a bigger struct than we know of,
9296 * ensure all the unknown bits are 0 - i.e. new
9297 * user-space does not rely on any kernel feature
9298 * extensions we dont know about yet.
9299 */
9300 if (size > sizeof(*attr)) {
9301 unsigned char __user *addr;
9302 unsigned char __user *end;
9303 unsigned char val;
9304
9305 addr = (void __user *)uattr + sizeof(*attr);
9306 end = (void __user *)uattr + size;
9307
9308 for (; addr < end; addr++) {
9309 ret = get_user(val, addr);
9310 if (ret)
9311 return ret;
9312 if (val)
9313 goto err_size;
9314 }
9315 size = sizeof(*attr);
9316 }
9317
9318 ret = copy_from_user(attr, uattr, size);
9319 if (ret)
9320 return -EFAULT;
9321
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05309322 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009323 return -EINVAL;
9324
9325 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
9326 return -EINVAL;
9327
9328 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
9329 return -EINVAL;
9330
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009331 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
9332 u64 mask = attr->branch_sample_type;
9333
9334 /* only using defined bits */
9335 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
9336 return -EINVAL;
9337
9338 /* at least one branch bit must be set */
9339 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9340 return -EINVAL;
9341
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009342 /* propagate priv level, when not set for branch */
9343 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9344
9345 /* exclude_kernel checked on syscall entry */
9346 if (!attr->exclude_kernel)
9347 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9348
9349 if (!attr->exclude_user)
9350 mask |= PERF_SAMPLE_BRANCH_USER;
9351
9352 if (!attr->exclude_hv)
9353 mask |= PERF_SAMPLE_BRANCH_HV;
9354 /*
9355 * adjust user setting (for HW filter setup)
9356 */
9357 attr->branch_sample_type = mask;
9358 }
Stephane Eraniane7122092013-06-06 11:02:04 +02009359 /* privileged levels capture (kernel, hv): check permissions */
9360 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02009361 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9362 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009363 }
Jiri Olsa40189942012-08-07 15:20:37 +02009364
Jiri Olsac5ebced2012-08-07 15:20:40 +02009365 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02009366 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02009367 if (ret)
9368 return ret;
9369 }
9370
9371 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9372 if (!arch_perf_have_user_stack_dump())
9373 return -ENOSYS;
9374
9375 /*
9376 * We have __u32 type for the size, but so far
9377 * we can only use __u16 as maximum due to the
9378 * __u16 sample size limit.
9379 */
9380 if (attr->sample_stack_user >= USHRT_MAX)
9381 ret = -EINVAL;
9382 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
9383 ret = -EINVAL;
9384 }
Jiri Olsa40189942012-08-07 15:20:37 +02009385
Stephane Eranian60e23642014-09-24 13:48:37 +02009386 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9387 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009388out:
9389 return ret;
9390
9391err_size:
9392 put_user(sizeof(*attr), &uattr->size);
9393 ret = -E2BIG;
9394 goto out;
9395}
9396
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009397static int
9398perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009399{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009400 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009401 int ret = -EINVAL;
9402
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009403 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009404 goto set;
9405
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009406 /* don't allow circular references */
9407 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009408 goto out;
9409
Peter Zijlstra0f139302010-05-20 14:35:15 +02009410 /*
9411 * Don't allow cross-cpu buffers
9412 */
9413 if (output_event->cpu != event->cpu)
9414 goto out;
9415
9416 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009417 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009418 */
9419 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9420 goto out;
9421
Peter Zijlstra34f43922015-02-20 14:05:38 +01009422 /*
9423 * Mixing clocks in the same buffer is trouble you don't need.
9424 */
9425 if (output_event->clock != event->clock)
9426 goto out;
9427
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009428 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009429 * Either writing ring buffer from beginning or from end.
9430 * Mixing is not allowed.
9431 */
9432 if (is_write_backward(output_event) != is_write_backward(event))
9433 goto out;
9434
9435 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009436 * If both events generate aux data, they must be on the same PMU
9437 */
9438 if (has_aux(event) && has_aux(output_event) &&
9439 event->pmu != output_event->pmu)
9440 goto out;
9441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009442set:
9443 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009444 /* Can't redirect output if we've got an active mmap() */
9445 if (atomic_read(&event->mmap_count))
9446 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009447
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009448 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009449 /* get the rb we want to redirect to */
9450 rb = ring_buffer_get(output_event);
9451 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009452 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009453 }
9454
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009455 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009456
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009457 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009458unlock:
9459 mutex_unlock(&event->mmap_mutex);
9460
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009461out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009462 return ret;
9463}
9464
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009465static void mutex_lock_double(struct mutex *a, struct mutex *b)
9466{
9467 if (b < a)
9468 swap(a, b);
9469
9470 mutex_lock(a);
9471 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9472}
9473
Peter Zijlstra34f43922015-02-20 14:05:38 +01009474static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9475{
9476 bool nmi_safe = false;
9477
9478 switch (clk_id) {
9479 case CLOCK_MONOTONIC:
9480 event->clock = &ktime_get_mono_fast_ns;
9481 nmi_safe = true;
9482 break;
9483
9484 case CLOCK_MONOTONIC_RAW:
9485 event->clock = &ktime_get_raw_fast_ns;
9486 nmi_safe = true;
9487 break;
9488
9489 case CLOCK_REALTIME:
9490 event->clock = &ktime_get_real_ns;
9491 break;
9492
9493 case CLOCK_BOOTTIME:
9494 event->clock = &ktime_get_boot_ns;
9495 break;
9496
9497 case CLOCK_TAI:
9498 event->clock = &ktime_get_tai_ns;
9499 break;
9500
9501 default:
9502 return -EINVAL;
9503 }
9504
9505 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9506 return -EINVAL;
9507
9508 return 0;
9509}
9510
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009511/**
9512 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9513 *
9514 * @attr_uptr: event_id type attributes for monitoring/sampling
9515 * @pid: target pid
9516 * @cpu: target cpu
9517 * @group_fd: group leader event fd
9518 */
9519SYSCALL_DEFINE5(perf_event_open,
9520 struct perf_event_attr __user *, attr_uptr,
9521 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9522{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009523 struct perf_event *group_leader = NULL, *output_event = NULL;
9524 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009525 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009526 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009527 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009528 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009529 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009530 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009531 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009532 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009533 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009534 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009535 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009536
9537 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009538 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009539 return -EINVAL;
9540
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -07009541 if (perf_paranoid_any() && !capable(CAP_SYS_ADMIN))
9542 return -EACCES;
9543
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009544 err = perf_copy_attr(attr_uptr, &attr);
9545 if (err)
9546 return err;
9547
9548 if (!attr.exclude_kernel) {
9549 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9550 return -EACCES;
9551 }
9552
9553 if (attr.freq) {
9554 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9555 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009556 } else {
9557 if (attr.sample_period & (1ULL << 63))
9558 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009559 }
9560
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009561 if (!attr.sample_max_stack)
9562 attr.sample_max_stack = sysctl_perf_event_max_stack;
9563
Stephane Eraniane5d13672011-02-14 11:20:01 +02009564 /*
9565 * In cgroup mode, the pid argument is used to pass the fd
9566 * opened to the cgroup directory in cgroupfs. The cpu argument
9567 * designates the cpu on which to monitor threads from that
9568 * cgroup.
9569 */
9570 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9571 return -EINVAL;
9572
Yann Droneauda21b0b32014-01-05 21:36:33 +01009573 if (flags & PERF_FLAG_FD_CLOEXEC)
9574 f_flags |= O_CLOEXEC;
9575
9576 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009577 if (event_fd < 0)
9578 return event_fd;
9579
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009580 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009581 err = perf_fget_light(group_fd, &group);
9582 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009583 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009584 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009585 if (flags & PERF_FLAG_FD_OUTPUT)
9586 output_event = group_leader;
9587 if (flags & PERF_FLAG_FD_NO_GROUP)
9588 group_leader = NULL;
9589 }
9590
Stephane Eraniane5d13672011-02-14 11:20:01 +02009591 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009592 task = find_lively_task_by_vpid(pid);
9593 if (IS_ERR(task)) {
9594 err = PTR_ERR(task);
9595 goto err_group_fd;
9596 }
9597 }
9598
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009599 if (task && group_leader &&
9600 group_leader->attr.inherit != attr.inherit) {
9601 err = -EINVAL;
9602 goto err_task;
9603 }
9604
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009605 get_online_cpus();
9606
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009607 if (task) {
9608 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9609 if (err)
9610 goto err_cpus;
9611
9612 /*
9613 * Reuse ptrace permission checks for now.
9614 *
9615 * We must hold cred_guard_mutex across this and any potential
9616 * perf_install_in_context() call for this new event to
9617 * serialize against exec() altering our credentials (and the
9618 * perf_event_exit_task() that could imply).
9619 */
9620 err = -EACCES;
9621 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9622 goto err_cred;
9623 }
9624
Matt Fleming79dff512015-01-23 18:45:42 +00009625 if (flags & PERF_FLAG_PID_CGROUP)
9626 cgroup_fd = pid;
9627
Avi Kivity4dc0da82011-06-29 18:42:35 +03009628 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009629 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009630 if (IS_ERR(event)) {
9631 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009632 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009633 }
9634
Vince Weaver53b25332014-05-16 17:12:12 -04009635 if (is_sampling_event(event)) {
9636 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +05309637 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -04009638 goto err_alloc;
9639 }
9640 }
9641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009642 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009643 * Special case software events and allow them to be part of
9644 * any hardware group.
9645 */
9646 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009647
Peter Zijlstra34f43922015-02-20 14:05:38 +01009648 if (attr.use_clockid) {
9649 err = perf_event_set_clock(event, attr.clockid);
9650 if (err)
9651 goto err_alloc;
9652 }
9653
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009654 if (pmu->task_ctx_nr == perf_sw_context)
9655 event->event_caps |= PERF_EV_CAP_SOFTWARE;
9656
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009657 if (group_leader &&
9658 (is_software_event(event) != is_software_event(group_leader))) {
9659 if (is_software_event(event)) {
9660 /*
9661 * If event and group_leader are not both a software
9662 * event, and event is, then group leader is not.
9663 *
9664 * Allow the addition of software events to !software
9665 * groups, this is safe because software events never
9666 * fail to schedule.
9667 */
9668 pmu = group_leader->pmu;
9669 } else if (is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009670 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009671 /*
9672 * In case the group is a pure software group, and we
9673 * try to add a hardware event, move the whole group to
9674 * the hardware context.
9675 */
9676 move_group = 1;
9677 }
9678 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009679
9680 /*
9681 * Get the target context (task or percpu):
9682 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009683 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009684 if (IS_ERR(ctx)) {
9685 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009686 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009687 }
9688
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009689 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9690 err = -EBUSY;
9691 goto err_context;
9692 }
9693
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009694 /*
9695 * Look up the group leader (we will attach this event to it):
9696 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009697 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009698 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009699
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009700 /*
9701 * Do not allow a recursive hierarchy (this new sibling
9702 * becoming part of another group-sibling):
9703 */
9704 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009705 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009706
9707 /* All events in a group should have the same clock */
9708 if (group_leader->clock != event->clock)
9709 goto err_context;
9710
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009711 /*
9712 * Do not allow to attach to a group in a different
9713 * task or CPU context:
9714 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009715 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009716 /*
9717 * Make sure we're both on the same task, or both
9718 * per-cpu events.
9719 */
9720 if (group_leader->ctx->task != ctx->task)
9721 goto err_context;
9722
9723 /*
9724 * Make sure we're both events for the same CPU;
9725 * grouping events for different CPUs is broken; since
9726 * you can never concurrently schedule them anyhow.
9727 */
9728 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009729 goto err_context;
9730 } else {
9731 if (group_leader->ctx != ctx)
9732 goto err_context;
9733 }
9734
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009735 /*
9736 * Only a group leader can be exclusive or pinned
9737 */
9738 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009739 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009740 }
9741
9742 if (output_event) {
9743 err = perf_event_set_output(event, output_event);
9744 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009745 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009746 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009747
Yann Droneauda21b0b32014-01-05 21:36:33 +01009748 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9749 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009750 if (IS_ERR(event_file)) {
9751 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009752 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009753 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009754 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009755
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009756 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009757 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009758 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009759 if (gctx->task == TASK_TOMBSTONE) {
9760 err = -ESRCH;
9761 goto err_locked;
9762 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009763 } else {
9764 mutex_lock(&ctx->mutex);
9765 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009766
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009767 if (ctx->task == TASK_TOMBSTONE) {
9768 err = -ESRCH;
9769 goto err_locked;
9770 }
9771
Peter Zijlstraa7239682015-09-09 19:06:33 +02009772 if (!perf_event_validate_size(event)) {
9773 err = -E2BIG;
9774 goto err_locked;
9775 }
9776
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009777 /*
9778 * Must be under the same ctx::mutex as perf_install_in_context(),
9779 * because we need to serialize with concurrent event creation.
9780 */
9781 if (!exclusive_event_installable(event, ctx)) {
9782 /* exclusive and group stuff are assumed mutually exclusive */
9783 WARN_ON_ONCE(move_group);
9784
9785 err = -EBUSY;
9786 goto err_locked;
9787 }
9788
9789 WARN_ON_ONCE(ctx->parent_ctx);
9790
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009791 /*
9792 * This is the point on no return; we cannot fail hereafter. This is
9793 * where we start modifying current state.
9794 */
9795
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009796 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009797 /*
9798 * See perf_event_ctx_lock() for comments on the details
9799 * of swizzling perf_event::ctx.
9800 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009801 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009802
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009803 list_for_each_entry(sibling, &group_leader->sibling_list,
9804 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009805 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009806 put_ctx(gctx);
9807 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009808
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009809 /*
9810 * Wait for everybody to stop referencing the events through
9811 * the old lists, before installing it on new lists.
9812 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009813 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009814
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009815 /*
9816 * Install the group siblings before the group leader.
9817 *
9818 * Because a group leader will try and install the entire group
9819 * (through the sibling list, which is still in-tact), we can
9820 * end up with siblings installed in the wrong context.
9821 *
9822 * By installing siblings first we NO-OP because they're not
9823 * reachable through the group lists.
9824 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009825 list_for_each_entry(sibling, &group_leader->sibling_list,
9826 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009827 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01009828 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009829 get_ctx(ctx);
9830 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009831
9832 /*
9833 * Removing from the context ends up with disabled
9834 * event. What we want here is event in the initial
9835 * startup state, ready to be add into new context.
9836 */
9837 perf_event__state_init(group_leader);
9838 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9839 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009840
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009841 /*
9842 * Now that all events are installed in @ctx, nothing
9843 * references @gctx anymore, so drop the last reference we have
9844 * on it.
9845 */
9846 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009847 }
9848
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02009849 /*
9850 * Precalculate sample_data sizes; do while holding ctx::mutex such
9851 * that we're serialized against further additions and before
9852 * perf_install_in_context() which is the point the event is active and
9853 * can use these values.
9854 */
9855 perf_event__header_size(event);
9856 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009857
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01009858 event->owner = current;
9859
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08009860 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009861 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009862
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009863 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009864 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009865 mutex_unlock(&ctx->mutex);
9866
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009867 if (task) {
9868 mutex_unlock(&task->signal->cred_guard_mutex);
9869 put_task_struct(task);
9870 }
9871
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009872 put_online_cpus();
9873
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009874 mutex_lock(&current->perf_event_mutex);
9875 list_add_tail(&event->owner_entry, &current->perf_event_list);
9876 mutex_unlock(&current->perf_event_mutex);
9877
Peter Zijlstra8a495422010-05-27 15:47:49 +02009878 /*
9879 * Drop the reference on the group_event after placing the
9880 * new event on the sibling_list. This ensures destruction
9881 * of the group leader will find the pointer to itself in
9882 * perf_group_detach().
9883 */
Al Viro2903ff02012-08-28 12:52:22 -04009884 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009885 fd_install(event_fd, event_file);
9886 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009887
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009888err_locked:
9889 if (move_group)
9890 mutex_unlock(&gctx->mutex);
9891 mutex_unlock(&ctx->mutex);
9892/* err_file: */
9893 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009894err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009895 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04009896 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009897err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01009898 /*
9899 * If event_file is set, the fput() above will have called ->release()
9900 * and that will take care of freeing the event.
9901 */
9902 if (!event_file)
9903 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009904err_cred:
9905 if (task)
9906 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009907err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009908 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009909err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02009910 if (task)
9911 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009912err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04009913 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009914err_fd:
9915 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009916 return err;
9917}
9918
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009919/**
9920 * perf_event_create_kernel_counter
9921 *
9922 * @attr: attributes of the counter to create
9923 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07009924 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009925 */
9926struct perf_event *
9927perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07009928 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009929 perf_overflow_handler_t overflow_handler,
9930 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009931{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009932 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009933 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009934 int err;
9935
9936 /*
9937 * Get the target context (task or percpu):
9938 */
9939
Avi Kivity4dc0da82011-06-29 18:42:35 +03009940 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009941 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009942 if (IS_ERR(event)) {
9943 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009944 goto err;
9945 }
9946
Jiri Olsaf8697762014-08-01 14:33:01 +02009947 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009948 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02009949
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009950 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009951 if (IS_ERR(ctx)) {
9952 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009953 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009954 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009955
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009956 WARN_ON_ONCE(ctx->parent_ctx);
9957 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009958 if (ctx->task == TASK_TOMBSTONE) {
9959 err = -ESRCH;
9960 goto err_unlock;
9961 }
9962
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009963 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009964 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009965 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009966 }
9967
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009968 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009969 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009970 mutex_unlock(&ctx->mutex);
9971
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009972 return event;
9973
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009974err_unlock:
9975 mutex_unlock(&ctx->mutex);
9976 perf_unpin_context(ctx);
9977 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009978err_free:
9979 free_event(event);
9980err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009981 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009982}
9983EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
9984
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009985void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
9986{
9987 struct perf_event_context *src_ctx;
9988 struct perf_event_context *dst_ctx;
9989 struct perf_event *event, *tmp;
9990 LIST_HEAD(events);
9991
9992 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
9993 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
9994
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009995 /*
9996 * See perf_event_ctx_lock() for comments on the details
9997 * of swizzling perf_event::ctx.
9998 */
9999 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010000 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
10001 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010002 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010003 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010004 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020010005 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010006 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010007
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010008 /*
10009 * Wait for the events to quiesce before re-instating them.
10010 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010011 synchronize_rcu();
10012
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010013 /*
10014 * Re-instate events in 2 passes.
10015 *
10016 * Skip over group leaders and only install siblings on this first
10017 * pass, siblings will not get enabled without a leader, however a
10018 * leader will enable its siblings, even if those are still on the old
10019 * context.
10020 */
10021 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10022 if (event->group_leader == event)
10023 continue;
10024
10025 list_del(&event->migrate_entry);
10026 if (event->state >= PERF_EVENT_STATE_OFF)
10027 event->state = PERF_EVENT_STATE_INACTIVE;
10028 account_event_cpu(event, dst_cpu);
10029 perf_install_in_context(dst_ctx, event, dst_cpu);
10030 get_ctx(dst_ctx);
10031 }
10032
10033 /*
10034 * Once all the siblings are setup properly, install the group leaders
10035 * to make it go.
10036 */
Peter Zijlstra98861672013-10-03 16:02:23 +020010037 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10038 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010039 if (event->state >= PERF_EVENT_STATE_OFF)
10040 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010041 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010042 perf_install_in_context(dst_ctx, event, dst_cpu);
10043 get_ctx(dst_ctx);
10044 }
10045 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010046 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010047}
10048EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
10049
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010050static void sync_child_event(struct perf_event *child_event,
10051 struct task_struct *child)
10052{
10053 struct perf_event *parent_event = child_event->parent;
10054 u64 child_val;
10055
10056 if (child_event->attr.inherit_stat)
10057 perf_event_read_event(child_event, child);
10058
Peter Zijlstrab5e58792010-05-21 14:43:12 +020010059 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010060
10061 /*
10062 * Add back the child's count to the parent's count:
10063 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020010064 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010065 atomic64_add(child_event->total_time_enabled,
10066 &parent_event->child_total_time_enabled);
10067 atomic64_add(child_event->total_time_running,
10068 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010069}
10070
10071static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010072perf_event_exit_event(struct perf_event *child_event,
10073 struct perf_event_context *child_ctx,
10074 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010075{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010076 struct perf_event *parent_event = child_event->parent;
10077
Peter Zijlstra1903d502014-07-15 17:27:27 +020010078 /*
10079 * Do not destroy the 'original' grouping; because of the context
10080 * switch optimization the original events could've ended up in a
10081 * random child task.
10082 *
10083 * If we were to destroy the original group, all group related
10084 * operations would cease to function properly after this random
10085 * child dies.
10086 *
10087 * Do destroy all inherited groups, we don't care about those
10088 * and being thorough is better.
10089 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010090 raw_spin_lock_irq(&child_ctx->lock);
10091 WARN_ON_ONCE(child_ctx->is_active);
10092
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010093 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010010094 perf_group_detach(child_event);
10095 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +010010096 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010097 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010098
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010099 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010100 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010101 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010102 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040010103 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010104 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010105 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010106 /*
10107 * Child events can be cleaned up.
10108 */
10109
10110 sync_child_event(child_event, child);
10111
10112 /*
10113 * Remove this event from the parent's list
10114 */
10115 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
10116 mutex_lock(&parent_event->child_mutex);
10117 list_del_init(&child_event->child_list);
10118 mutex_unlock(&parent_event->child_mutex);
10119
10120 /*
10121 * Kick perf_poll() for is_event_hup().
10122 */
10123 perf_event_wakeup(parent_event);
10124 free_event(child_event);
10125 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010126}
10127
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010128static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010129{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010130 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010131 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010132
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010133 WARN_ON_ONCE(child != current);
10134
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010135 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010136 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010137 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010138
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010139 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010140 * In order to reduce the amount of tricky in ctx tear-down, we hold
10141 * ctx::mutex over the entire thing. This serializes against almost
10142 * everything that wants to access the ctx.
10143 *
10144 * The exception is sys_perf_event_open() /
10145 * perf_event_create_kernel_count() which does find_get_context()
10146 * without ctx::mutex (it cannot because of the move_group double mutex
10147 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010148 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010149 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010150
10151 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010152 * In a single ctx::lock section, de-schedule the events and detach the
10153 * context from the task such that we cannot ever get it scheduled back
10154 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010155 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010156 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010157 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010158
10159 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010160 * Now that the context is inactive, destroy the task <-> ctx relation
10161 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010162 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010163 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
10164 put_ctx(child_ctx); /* cannot be last */
10165 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
10166 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010167
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010168 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010169 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010170
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010171 if (clone_ctx)
10172 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010173
10174 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010175 * Report the task dead after unscheduling the events so that we
10176 * won't get any samples after PERF_RECORD_EXIT. We can however still
10177 * get a few PERF_RECORD_READ events.
10178 */
10179 perf_event_task(child, child_ctx, 0);
10180
Peter Zijlstraebf905f2014-05-29 19:00:24 +020010181 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010182 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010183
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010184 mutex_unlock(&child_ctx->mutex);
10185
10186 put_ctx(child_ctx);
10187}
10188
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010189/*
10190 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010191 *
10192 * Can be called with cred_guard_mutex held when called from
10193 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010194 */
10195void perf_event_exit_task(struct task_struct *child)
10196{
Peter Zijlstra88821352010-11-09 19:01:43 +010010197 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010198 int ctxn;
10199
Peter Zijlstra88821352010-11-09 19:01:43 +010010200 mutex_lock(&child->perf_event_mutex);
10201 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
10202 owner_entry) {
10203 list_del_init(&event->owner_entry);
10204
10205 /*
10206 * Ensure the list deletion is visible before we clear
10207 * the owner, closes a race against perf_release() where
10208 * we need to serialize on the owner->perf_event_mutex.
10209 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010010210 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010010211 }
10212 mutex_unlock(&child->perf_event_mutex);
10213
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010214 for_each_task_context_nr(ctxn)
10215 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010010216
10217 /*
10218 * The perf_event_exit_task_context calls perf_event_task
10219 * with child's task_ctx, which generates EXIT events for
10220 * child contexts and sets child->perf_event_ctxp[] to NULL.
10221 * At this point we need to send EXIT events to cpu contexts.
10222 */
10223 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010224}
10225
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010226static void perf_free_event(struct perf_event *event,
10227 struct perf_event_context *ctx)
10228{
10229 struct perf_event *parent = event->parent;
10230
10231 if (WARN_ON_ONCE(!parent))
10232 return;
10233
10234 mutex_lock(&parent->child_mutex);
10235 list_del_init(&event->child_list);
10236 mutex_unlock(&parent->child_mutex);
10237
Al Viroa6fa9412012-08-20 14:59:25 +010010238 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010239
Peter Zijlstra652884f2015-01-23 11:20:10 +010010240 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020010241 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010242 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010010243 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010244 free_event(event);
10245}
10246
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010247/*
Peter Zijlstra652884f2015-01-23 11:20:10 +010010248 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010249 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010010250 *
10251 * Not all locks are strictly required, but take them anyway to be nice and
10252 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010253 */
10254void perf_event_free_task(struct task_struct *task)
10255{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010256 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010257 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010258 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010259
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010260 for_each_task_context_nr(ctxn) {
10261 ctx = task->perf_event_ctxp[ctxn];
10262 if (!ctx)
10263 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010264
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010265 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010266again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010267 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
10268 group_entry)
10269 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010270
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010271 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
10272 group_entry)
10273 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010274
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010275 if (!list_empty(&ctx->pinned_groups) ||
10276 !list_empty(&ctx->flexible_groups))
10277 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010278
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010279 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010280
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010281 put_ctx(ctx);
10282 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010283}
10284
Peter Zijlstra4e231c72010-09-09 21:01:59 +020010285void perf_event_delayed_put(struct task_struct *task)
10286{
10287 int ctxn;
10288
10289 for_each_task_context_nr(ctxn)
10290 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
10291}
10292
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010293struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000010294{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010295 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010296
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010297 file = fget_raw(fd);
10298 if (!file)
10299 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000010300
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010301 if (file->f_op != &perf_fops) {
10302 fput(file);
10303 return ERR_PTR(-EBADF);
10304 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000010305
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010306 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010307}
10308
10309const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
10310{
10311 if (!event)
10312 return ERR_PTR(-EINVAL);
10313
10314 return &event->attr;
10315}
10316
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010317/*
10318 * inherit a event from parent task to child task:
10319 */
10320static struct perf_event *
10321inherit_event(struct perf_event *parent_event,
10322 struct task_struct *parent,
10323 struct perf_event_context *parent_ctx,
10324 struct task_struct *child,
10325 struct perf_event *group_leader,
10326 struct perf_event_context *child_ctx)
10327{
Jiri Olsa1929def2014-09-12 13:18:27 +020010328 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010329 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020010330 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010331
10332 /*
10333 * Instead of creating recursive hierarchies of events,
10334 * we link inherited events back to the original parent,
10335 * which has a filp for sure, which we use as the reference
10336 * count:
10337 */
10338 if (parent_event->parent)
10339 parent_event = parent_event->parent;
10340
10341 child_event = perf_event_alloc(&parent_event->attr,
10342 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010343 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010344 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000010345 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010346 if (IS_ERR(child_event))
10347 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010010348
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010349 /*
10350 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10351 * must be under the same lock in order to serialize against
10352 * perf_event_release_kernel(), such that either we must observe
10353 * is_orphaned_event() or they will observe us on the child_list.
10354 */
10355 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020010356 if (is_orphaned_event(parent_event) ||
10357 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010358 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +010010359 free_event(child_event);
10360 return NULL;
10361 }
10362
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010363 get_ctx(child_ctx);
10364
10365 /*
10366 * Make the child state follow the state of the parent event,
10367 * not its attr.disabled bit. We hold the parent's mutex,
10368 * so we won't race with perf_event_{en, dis}able_family.
10369 */
Jiri Olsa1929def2014-09-12 13:18:27 +020010370 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010371 child_event->state = PERF_EVENT_STATE_INACTIVE;
10372 else
10373 child_event->state = PERF_EVENT_STATE_OFF;
10374
10375 if (parent_event->attr.freq) {
10376 u64 sample_period = parent_event->hw.sample_period;
10377 struct hw_perf_event *hwc = &child_event->hw;
10378
10379 hwc->sample_period = sample_period;
10380 hwc->last_period = sample_period;
10381
10382 local64_set(&hwc->period_left, sample_period);
10383 }
10384
10385 child_event->ctx = child_ctx;
10386 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010387 child_event->overflow_handler_context
10388 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010389
10390 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020010391 * Precalculate sample_data sizes
10392 */
10393 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020010394 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020010395
10396 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010397 * Link it up in the child's context:
10398 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020010399 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010400 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010401 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010402
10403 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010404 * Link this into the parent event's child list
10405 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010406 list_add_tail(&child_event->child_list, &parent_event->child_list);
10407 mutex_unlock(&parent_event->child_mutex);
10408
10409 return child_event;
10410}
10411
10412static int inherit_group(struct perf_event *parent_event,
10413 struct task_struct *parent,
10414 struct perf_event_context *parent_ctx,
10415 struct task_struct *child,
10416 struct perf_event_context *child_ctx)
10417{
10418 struct perf_event *leader;
10419 struct perf_event *sub;
10420 struct perf_event *child_ctr;
10421
10422 leader = inherit_event(parent_event, parent, parent_ctx,
10423 child, NULL, child_ctx);
10424 if (IS_ERR(leader))
10425 return PTR_ERR(leader);
10426 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10427 child_ctr = inherit_event(sub, parent, parent_ctx,
10428 child, leader, child_ctx);
10429 if (IS_ERR(child_ctr))
10430 return PTR_ERR(child_ctr);
10431 }
10432 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010433}
10434
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010435static int
10436inherit_task_group(struct perf_event *event, struct task_struct *parent,
10437 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010438 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010439 int *inherited_all)
10440{
10441 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010442 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010443
10444 if (!event->attr.inherit) {
10445 *inherited_all = 0;
10446 return 0;
10447 }
10448
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010449 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010450 if (!child_ctx) {
10451 /*
10452 * This is executed from the parent task context, so
10453 * inherit events that have been marked for cloning.
10454 * First allocate and initialize a context for the
10455 * child.
10456 */
10457
Jiri Olsa734df5a2013-07-09 17:44:10 +020010458 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010459 if (!child_ctx)
10460 return -ENOMEM;
10461
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010462 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010463 }
10464
10465 ret = inherit_group(event, parent, parent_ctx,
10466 child, child_ctx);
10467
10468 if (ret)
10469 *inherited_all = 0;
10470
10471 return ret;
10472}
10473
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010474/*
10475 * Initialize the perf_event context in task_struct
10476 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010477static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010478{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010479 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010480 struct perf_event_context *cloned_ctx;
10481 struct perf_event *event;
10482 struct task_struct *parent = current;
10483 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010484 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010485 int ret = 0;
10486
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010487 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010488 return 0;
10489
10490 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010491 * If the parent's context is a clone, pin it so it won't get
10492 * swapped under us.
10493 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010494 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010495 if (!parent_ctx)
10496 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010497
10498 /*
10499 * No need to check if parent_ctx != NULL here; since we saw
10500 * it non-NULL earlier, the only reason for it to become NULL
10501 * is if we exit, and since we're currently in the middle of
10502 * a fork we can't be exiting at the same time.
10503 */
10504
10505 /*
10506 * Lock the parent list. No need to lock the child - not PID
10507 * hashed yet and not running, so nobody can access it.
10508 */
10509 mutex_lock(&parent_ctx->mutex);
10510
10511 /*
10512 * We dont have to disable NMIs - we are only looking at
10513 * the list, not manipulating it:
10514 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010515 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010516 ret = inherit_task_group(event, parent, parent_ctx,
10517 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010518 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010519 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010520 }
10521
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010522 /*
10523 * We can't hold ctx->lock when iterating the ->flexible_group list due
10524 * to allocations, but we need to prevent rotation because
10525 * rotate_ctx() will change the list from interrupt context.
10526 */
10527 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10528 parent_ctx->rotate_disable = 1;
10529 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10530
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010531 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010532 ret = inherit_task_group(event, parent, parent_ctx,
10533 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010534 if (ret)
10535 break;
10536 }
10537
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010538 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10539 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010540
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010541 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010542
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010543 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010544 /*
10545 * Mark the child context as a clone of the parent
10546 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010547 *
10548 * Note that if the parent is a clone, the holding of
10549 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010550 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010551 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010552 if (cloned_ctx) {
10553 child_ctx->parent_ctx = cloned_ctx;
10554 child_ctx->parent_gen = parent_ctx->parent_gen;
10555 } else {
10556 child_ctx->parent_ctx = parent_ctx;
10557 child_ctx->parent_gen = parent_ctx->generation;
10558 }
10559 get_ctx(child_ctx->parent_ctx);
10560 }
10561
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010562 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010563 mutex_unlock(&parent_ctx->mutex);
10564
10565 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010566 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010567
10568 return ret;
10569}
10570
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010571/*
10572 * Initialize the perf_event context in task_struct
10573 */
10574int perf_event_init_task(struct task_struct *child)
10575{
10576 int ctxn, ret;
10577
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010578 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10579 mutex_init(&child->perf_event_mutex);
10580 INIT_LIST_HEAD(&child->perf_event_list);
10581
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010582 for_each_task_context_nr(ctxn) {
10583 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010584 if (ret) {
10585 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010586 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010587 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010588 }
10589
10590 return 0;
10591}
10592
Paul Mackerras220b1402010-03-10 20:45:52 +110010593static void __init perf_event_init_all_cpus(void)
10594{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010595 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010596 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010597
10598 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010599 swhash = &per_cpu(swevent_htable, cpu);
10600 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010601 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070010602
10603 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
10604 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020010605
10606 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010607 }
10608}
10609
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010610int perf_event_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010611{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010612 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010613
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010614 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010615 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010616 struct swevent_hlist *hlist;
10617
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010618 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10619 WARN_ON(!hlist);
10620 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010621 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010622 mutex_unlock(&swhash->hlist_mutex);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010623 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010624}
10625
Dave Young2965faa2015-09-09 15:38:55 -070010626#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010627static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010628{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010629 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010630 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10631 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010632
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010633 raw_spin_lock(&ctx->lock);
10634 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010635 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010636 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010637}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010638
10639static void perf_event_exit_cpu_context(int cpu)
10640{
10641 struct perf_event_context *ctx;
10642 struct pmu *pmu;
10643 int idx;
10644
10645 idx = srcu_read_lock(&pmus_srcu);
10646 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010647 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010648
10649 mutex_lock(&ctx->mutex);
10650 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10651 mutex_unlock(&ctx->mutex);
10652 }
10653 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010654}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010655#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010656
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010657static void perf_event_exit_cpu_context(int cpu) { }
10658
10659#endif
10660
10661int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010662{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010663 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010664 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010665}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010666
Peter Zijlstrac2774432010-12-08 15:29:02 +010010667static int
10668perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10669{
10670 int cpu;
10671
10672 for_each_online_cpu(cpu)
10673 perf_event_exit_cpu(cpu);
10674
10675 return NOTIFY_OK;
10676}
10677
10678/*
10679 * Run the perf reboot notifier at the very last possible moment so that
10680 * the generic watchdog code runs as long as possible.
10681 */
10682static struct notifier_block perf_reboot_notifier = {
10683 .notifier_call = perf_reboot,
10684 .priority = INT_MIN,
10685};
10686
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010687void __init perf_event_init(void)
10688{
Jason Wessel3c502e72010-11-04 17:33:01 -050010689 int ret;
10690
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010691 idr_init(&pmu_idr);
10692
Paul Mackerras220b1402010-03-10 20:45:52 +110010693 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010694 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010695 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10696 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10697 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010698 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010699 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010010700 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010701
10702 ret = init_hw_breakpoint();
10703 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010704
Jiri Olsab01c3a02012-03-23 15:41:20 +010010705 /*
10706 * Build time assertion that we keep the data_head at the intended
10707 * location. IOW, validation we got the __reserved[] size right.
10708 */
10709 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10710 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010711}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010712
Cody P Schaferfd979c02015-01-30 13:45:57 -080010713ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10714 char *page)
10715{
10716 struct perf_pmu_events_attr *pmu_attr =
10717 container_of(attr, struct perf_pmu_events_attr, attr);
10718
10719 if (pmu_attr->event_str)
10720 return sprintf(page, "%s\n", pmu_attr->event_str);
10721
10722 return 0;
10723}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010724EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010725
Peter Zijlstraabe43402010-11-17 23:17:37 +010010726static int __init perf_event_sysfs_init(void)
10727{
10728 struct pmu *pmu;
10729 int ret;
10730
10731 mutex_lock(&pmus_lock);
10732
10733 ret = bus_register(&pmu_bus);
10734 if (ret)
10735 goto unlock;
10736
10737 list_for_each_entry(pmu, &pmus, entry) {
10738 if (!pmu->name || pmu->type < 0)
10739 continue;
10740
10741 ret = pmu_dev_alloc(pmu);
10742 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10743 }
10744 pmu_bus_running = 1;
10745 ret = 0;
10746
10747unlock:
10748 mutex_unlock(&pmus_lock);
10749
10750 return ret;
10751}
10752device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010753
10754#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010755static struct cgroup_subsys_state *
10756perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010757{
10758 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010759
Li Zefan1b15d052011-03-03 14:26:06 +080010760 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010761 if (!jc)
10762 return ERR_PTR(-ENOMEM);
10763
Stephane Eraniane5d13672011-02-14 11:20:01 +020010764 jc->info = alloc_percpu(struct perf_cgroup_info);
10765 if (!jc->info) {
10766 kfree(jc);
10767 return ERR_PTR(-ENOMEM);
10768 }
10769
Stephane Eraniane5d13672011-02-14 11:20:01 +020010770 return &jc->css;
10771}
10772
Tejun Heoeb954192013-08-08 20:11:23 -040010773static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010774{
Tejun Heoeb954192013-08-08 20:11:23 -040010775 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10776
Stephane Eraniane5d13672011-02-14 11:20:01 +020010777 free_percpu(jc->info);
10778 kfree(jc);
10779}
10780
10781static int __perf_cgroup_move(void *info)
10782{
10783 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010784 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010785 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010786 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010787 return 0;
10788}
10789
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010790static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010791{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010792 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010793 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010794
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010795 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010796 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010797}
10798
Tejun Heo073219e2014-02-08 10:36:58 -050010799struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010800 .css_alloc = perf_cgroup_css_alloc,
10801 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010802 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010803};
10804#endif /* CONFIG_CGROUP_PERF */