blob: da172d3627f7ac7455f18078a495591b05c4d016 [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{
Tan Xiaojun0f8a75e2017-02-23 14:04:39 +0800461 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Dave Hansen14c63f12013-06-21 08:51:36 -0700462
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{
Song Liud4271d82018-03-12 09:59:43 -0700642 struct perf_cgroup *cgrp = cpuctx->cgrp;
643 struct cgroup_subsys_state *css;
644
645 if (cgrp) {
646 for (css = &cgrp->css; css; css = css->parent) {
647 cgrp = container_of(css, struct perf_cgroup, css);
648 __update_cgrp_time(cgrp);
649 }
650 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200651}
652
653static inline void update_cgrp_time_from_event(struct perf_event *event)
654{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200655 struct perf_cgroup *cgrp;
656
Stephane Eraniane5d13672011-02-14 11:20:01 +0200657 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200658 * ensure we access cgroup data only when needed and
659 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200660 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200661 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200662 return;
663
Stephane Eranian614e4c42015-11-12 11:00:04 +0100664 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200665 /*
666 * Do not update time when cgroup is not active
667 */
668 if (cgrp == event->cgrp)
669 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200670}
671
672static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200673perf_cgroup_set_timestamp(struct task_struct *task,
674 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200675{
676 struct perf_cgroup *cgrp;
677 struct perf_cgroup_info *info;
Song Liud4271d82018-03-12 09:59:43 -0700678 struct cgroup_subsys_state *css;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200679
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200680 /*
681 * ctx->lock held by caller
682 * ensure we do not access cgroup data
683 * unless we have the cgroup pinned (css_get)
684 */
685 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200686 return;
687
Stephane Eranian614e4c42015-11-12 11:00:04 +0100688 cgrp = perf_cgroup_from_task(task, ctx);
Song Liud4271d82018-03-12 09:59:43 -0700689
690 for (css = &cgrp->css; css; css = css->parent) {
691 cgrp = container_of(css, struct perf_cgroup, css);
692 info = this_cpu_ptr(cgrp->info);
693 info->timestamp = ctx->timestamp;
694 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200695}
696
697#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
698#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
699
700/*
701 * reschedule events based on the cgroup constraint of task.
702 *
703 * mode SWOUT : schedule out everything
704 * mode SWIN : schedule in based on cgroup for next
705 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800706static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200707{
708 struct perf_cpu_context *cpuctx;
709 struct pmu *pmu;
710 unsigned long flags;
711
712 /*
713 * disable interrupts to avoid geting nr_cgroup
714 * changes via __perf_event_disable(). Also
715 * avoids preemption.
716 */
717 local_irq_save(flags);
718
719 /*
720 * we reschedule only in the presence of cgroup
721 * constrained events.
722 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200723
724 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200725 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200726 if (cpuctx->unique_pmu != pmu)
727 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200728
Stephane Eraniane5d13672011-02-14 11:20:01 +0200729 /*
730 * perf_cgroup_events says at least one
731 * context on this CPU has cgroup events.
732 *
733 * ctx->nr_cgroups reports the number of cgroup
734 * events for a context.
735 */
736 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200737 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
738 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200739
740 if (mode & PERF_CGROUP_SWOUT) {
741 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
742 /*
743 * must not be done before ctxswout due
744 * to event_filter_match() in event_sched_out()
745 */
746 cpuctx->cgrp = NULL;
747 }
748
749 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200750 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200751 /*
752 * set cgrp before ctxsw in to allow
753 * event_filter_match() to not have to pass
754 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100755 * we pass the cpuctx->ctx to perf_cgroup_from_task()
756 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200757 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100758 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200759 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
760 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200761 perf_pmu_enable(cpuctx->ctx.pmu);
762 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200763 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200764 }
765
Stephane Eraniane5d13672011-02-14 11:20:01 +0200766 local_irq_restore(flags);
767}
768
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200769static inline void perf_cgroup_sched_out(struct task_struct *task,
770 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200771{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200772 struct perf_cgroup *cgrp1;
773 struct perf_cgroup *cgrp2 = NULL;
774
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100775 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200776 /*
777 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100778 * we do not need to pass the ctx here because we know
779 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200780 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100781 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100782 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200783
784 /*
785 * only schedule out current cgroup events if we know
786 * that we are switching to a different cgroup. Otherwise,
787 * do no touch the cgroup events.
788 */
789 if (cgrp1 != cgrp2)
790 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100791
792 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200793}
794
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200795static inline void perf_cgroup_sched_in(struct task_struct *prev,
796 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200797{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200798 struct perf_cgroup *cgrp1;
799 struct perf_cgroup *cgrp2 = NULL;
800
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100801 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200802 /*
803 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100804 * we do not need to pass the ctx here because we know
805 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200806 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100807 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100808 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200809
810 /*
811 * only need to schedule in cgroup events if we are changing
812 * cgroup during ctxsw. Cgroup events were not scheduled
813 * out of ctxsw out if that was not the case.
814 */
815 if (cgrp1 != cgrp2)
816 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100817
818 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200819}
820
821static inline int perf_cgroup_connect(int fd, struct perf_event *event,
822 struct perf_event_attr *attr,
823 struct perf_event *group_leader)
824{
825 struct perf_cgroup *cgrp;
826 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400827 struct fd f = fdget(fd);
828 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200829
Al Viro2903ff02012-08-28 12:52:22 -0400830 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200831 return -EBADF;
832
Al Virob5830432014-10-31 01:22:04 -0400833 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400834 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800835 if (IS_ERR(css)) {
836 ret = PTR_ERR(css);
837 goto out;
838 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200839
840 cgrp = container_of(css, struct perf_cgroup, css);
841 event->cgrp = cgrp;
842
843 /*
844 * all events in a group must monitor
845 * the same cgroup because a task belongs
846 * to only one perf cgroup at a time
847 */
848 if (group_leader && group_leader->cgrp != cgrp) {
849 perf_detach_cgroup(event);
850 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200851 }
Li Zefan3db272c2011-03-03 14:25:37 +0800852out:
Al Viro2903ff02012-08-28 12:52:22 -0400853 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200854 return ret;
855}
856
857static inline void
858perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
859{
860 struct perf_cgroup_info *t;
861 t = per_cpu_ptr(event->cgrp->info, event->cpu);
862 event->shadow_ctx_time = now - t->timestamp;
863}
864
865static inline void
866perf_cgroup_defer_enabled(struct perf_event *event)
867{
868 /*
869 * when the current task's perf cgroup does not match
870 * the event's, we need to remember to call the
871 * perf_mark_enable() function the first time a task with
872 * a matching perf cgroup is scheduled in.
873 */
874 if (is_cgroup_event(event) && !perf_cgroup_match(event))
875 event->cgrp_defer_enabled = 1;
876}
877
878static inline void
879perf_cgroup_mark_enabled(struct perf_event *event,
880 struct perf_event_context *ctx)
881{
882 struct perf_event *sub;
883 u64 tstamp = perf_event_time(event);
884
885 if (!event->cgrp_defer_enabled)
886 return;
887
888 event->cgrp_defer_enabled = 0;
889
890 event->tstamp_enabled = tstamp - event->total_time_enabled;
891 list_for_each_entry(sub, &event->sibling_list, group_entry) {
892 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
893 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
894 sub->cgrp_defer_enabled = 0;
895 }
896 }
897}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700898
899/*
900 * Update cpuctx->cgrp so that it is set when first cgroup event is added and
901 * cleared when last cgroup event is removed.
902 */
903static inline void
904list_update_cgroup_event(struct perf_event *event,
905 struct perf_event_context *ctx, bool add)
906{
907 struct perf_cpu_context *cpuctx;
908
909 if (!is_cgroup_event(event))
910 return;
911
912 if (add && ctx->nr_cgroups++)
913 return;
914 else if (!add && --ctx->nr_cgroups)
915 return;
916 /*
917 * Because cgroup events are always per-cpu events,
918 * this will always be called from the right CPU.
919 */
920 cpuctx = __get_cpu_context(ctx);
David Carrillo-Cisneros864c2352016-11-01 11:52:58 -0700921
David Carrillo-Cisneros8fc31ce2016-12-04 00:46:17 -0800922 /*
923 * cpuctx->cgrp is NULL until a cgroup event is sched in or
924 * ctx->nr_cgroup == 0 .
925 */
926 if (add && perf_cgroup_from_task(current, ctx) == event->cgrp)
927 cpuctx->cgrp = event->cgrp;
928 else if (!add)
929 cpuctx->cgrp = NULL;
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700930}
931
Stephane Eraniane5d13672011-02-14 11:20:01 +0200932#else /* !CONFIG_CGROUP_PERF */
933
934static inline bool
935perf_cgroup_match(struct perf_event *event)
936{
937 return true;
938}
939
940static inline void perf_detach_cgroup(struct perf_event *event)
941{}
942
943static inline int is_cgroup_event(struct perf_event *event)
944{
945 return 0;
946}
947
948static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
949{
950 return 0;
951}
952
953static inline void update_cgrp_time_from_event(struct perf_event *event)
954{
955}
956
957static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
958{
959}
960
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200961static inline void perf_cgroup_sched_out(struct task_struct *task,
962 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200963{
964}
965
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200966static inline void perf_cgroup_sched_in(struct task_struct *prev,
967 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200968{
969}
970
971static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
972 struct perf_event_attr *attr,
973 struct perf_event *group_leader)
974{
975 return -EINVAL;
976}
977
978static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200979perf_cgroup_set_timestamp(struct task_struct *task,
980 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200981{
982}
983
984void
985perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
986{
987}
988
989static inline void
990perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
991{
992}
993
994static inline u64 perf_cgroup_event_time(struct perf_event *event)
995{
996 return 0;
997}
998
999static inline void
1000perf_cgroup_defer_enabled(struct perf_event *event)
1001{
1002}
1003
1004static inline void
1005perf_cgroup_mark_enabled(struct perf_event *event,
1006 struct perf_event_context *ctx)
1007{
1008}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001009
1010static inline void
1011list_update_cgroup_event(struct perf_event *event,
1012 struct perf_event_context *ctx, bool add)
1013{
1014}
1015
Stephane Eraniane5d13672011-02-14 11:20:01 +02001016#endif
1017
Stephane Eranian9e630202013-04-03 14:21:33 +02001018/*
1019 * set default to be dependent on timer tick just
1020 * like original code
1021 */
1022#define PERF_CPU_HRTIMER (1000 / HZ)
1023/*
1024 * function must be called with interrupts disbled
1025 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001026static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +02001027{
1028 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +02001029 int rotations = 0;
1030
1031 WARN_ON(!irqs_disabled());
1032
1033 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +02001034 rotations = perf_rotate_context(cpuctx);
1035
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001036 raw_spin_lock(&cpuctx->hrtimer_lock);
1037 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +02001038 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001039 else
1040 cpuctx->hrtimer_active = 0;
1041 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +02001042
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001043 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +02001044}
1045
Peter Zijlstra272325c2015-04-15 11:41:58 +02001046static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +02001047{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001048 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001049 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +02001050 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +02001051
1052 /* no multiplexing needed for SW PMU */
1053 if (pmu->task_ctx_nr == perf_sw_context)
1054 return;
1055
Stephane Eranian62b85632013-04-03 14:21:34 +02001056 /*
1057 * check default is sane, if not set then force to
1058 * default interval (1/tick)
1059 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001060 interval = pmu->hrtimer_interval_ms;
1061 if (interval < 1)
1062 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001063
Peter Zijlstra272325c2015-04-15 11:41:58 +02001064 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001065
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001066 raw_spin_lock_init(&cpuctx->hrtimer_lock);
1067 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001068 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001069}
1070
Peter Zijlstra272325c2015-04-15 11:41:58 +02001071static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001072{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001073 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001074 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001075 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001076
1077 /* not for SW PMU */
1078 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001079 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001080
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001081 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1082 if (!cpuctx->hrtimer_active) {
1083 cpuctx->hrtimer_active = 1;
1084 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
1085 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
1086 }
1087 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001088
Peter Zijlstra272325c2015-04-15 11:41:58 +02001089 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001090}
1091
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001092void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001093{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001094 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1095 if (!(*count)++)
1096 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001097}
1098
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001099void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001100{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001101 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1102 if (!--(*count))
1103 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001104}
1105
Mark Rutland2fde4f92015-01-07 15:01:54 +00001106static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001107
1108/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001109 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1110 * perf_event_task_tick() are fully serialized because they're strictly cpu
1111 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1112 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001113 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001114static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001115{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001116 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001117
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001118 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001119
Mark Rutland2fde4f92015-01-07 15:01:54 +00001120 WARN_ON(!list_empty(&ctx->active_ctx_list));
1121
1122 list_add(&ctx->active_ctx_list, head);
1123}
1124
1125static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1126{
1127 WARN_ON(!irqs_disabled());
1128
1129 WARN_ON(list_empty(&ctx->active_ctx_list));
1130
1131 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132}
1133
1134static void get_ctx(struct perf_event_context *ctx)
1135{
1136 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1137}
1138
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001139static void free_ctx(struct rcu_head *head)
1140{
1141 struct perf_event_context *ctx;
1142
1143 ctx = container_of(head, struct perf_event_context, rcu_head);
1144 kfree(ctx->task_ctx_data);
1145 kfree(ctx);
1146}
1147
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001148static void put_ctx(struct perf_event_context *ctx)
1149{
1150 if (atomic_dec_and_test(&ctx->refcount)) {
1151 if (ctx->parent_ctx)
1152 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001153 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001154 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001155 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001156 }
1157}
1158
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001159/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001160 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1161 * perf_pmu_migrate_context() we need some magic.
1162 *
1163 * Those places that change perf_event::ctx will hold both
1164 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1165 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001166 * Lock ordering is by mutex address. There are two other sites where
1167 * perf_event_context::mutex nests and those are:
1168 *
1169 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001170 * perf_event_exit_event()
1171 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001172 *
1173 * - perf_event_init_context() [ parent, 0 ]
1174 * inherit_task_group()
1175 * inherit_group()
1176 * inherit_event()
1177 * perf_event_alloc()
1178 * perf_init_event()
1179 * perf_try_init_event() [ child , 1 ]
1180 *
1181 * While it appears there is an obvious deadlock here -- the parent and child
1182 * nesting levels are inverted between the two. This is in fact safe because
1183 * life-time rules separate them. That is an exiting task cannot fork, and a
1184 * spawning task cannot (yet) exit.
1185 *
1186 * But remember that that these are parent<->child context relations, and
1187 * migration does not affect children, therefore these two orderings should not
1188 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001189 *
1190 * The change in perf_event::ctx does not affect children (as claimed above)
1191 * because the sys_perf_event_open() case will install a new event and break
1192 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1193 * concerned with cpuctx and that doesn't have children.
1194 *
1195 * The places that change perf_event::ctx will issue:
1196 *
1197 * perf_remove_from_context();
1198 * synchronize_rcu();
1199 * perf_install_in_context();
1200 *
1201 * to affect the change. The remove_from_context() + synchronize_rcu() should
1202 * quiesce the event, after which we can install it in the new location. This
1203 * means that only external vectors (perf_fops, prctl) can perturb the event
1204 * while in transit. Therefore all such accessors should also acquire
1205 * perf_event_context::mutex to serialize against this.
1206 *
1207 * However; because event->ctx can change while we're waiting to acquire
1208 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1209 * function.
1210 *
1211 * Lock order:
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02001212 * cred_guard_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001213 * task_struct::perf_event_mutex
1214 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001215 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001216 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001217 * perf_event::mmap_mutex
1218 * mmap_sem
1219 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001220static struct perf_event_context *
1221perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001222{
1223 struct perf_event_context *ctx;
1224
1225again:
1226 rcu_read_lock();
1227 ctx = ACCESS_ONCE(event->ctx);
1228 if (!atomic_inc_not_zero(&ctx->refcount)) {
1229 rcu_read_unlock();
1230 goto again;
1231 }
1232 rcu_read_unlock();
1233
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001234 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001235 if (event->ctx != ctx) {
1236 mutex_unlock(&ctx->mutex);
1237 put_ctx(ctx);
1238 goto again;
1239 }
1240
1241 return ctx;
1242}
1243
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001244static inline struct perf_event_context *
1245perf_event_ctx_lock(struct perf_event *event)
1246{
1247 return perf_event_ctx_lock_nested(event, 0);
1248}
1249
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001250static void perf_event_ctx_unlock(struct perf_event *event,
1251 struct perf_event_context *ctx)
1252{
1253 mutex_unlock(&ctx->mutex);
1254 put_ctx(ctx);
1255}
1256
1257/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001258 * This must be done under the ctx->lock, such as to serialize against
1259 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1260 * calling scheduler related locks and ctx->lock nests inside those.
1261 */
1262static __must_check struct perf_event_context *
1263unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001265 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1266
1267 lockdep_assert_held(&ctx->lock);
1268
1269 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001271 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001272
1273 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001274}
1275
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001276static u32 perf_event_pid(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_tgid_nr_ns(p, event->ns);
1285}
1286
1287static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1288{
1289 /*
1290 * only top level events have the pid namespace they were created in
1291 */
1292 if (event->parent)
1293 event = event->parent;
1294
1295 return task_pid_nr_ns(p, event->ns);
1296}
1297
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001298/*
1299 * If we inherit events we want to return the parent event id
1300 * to userspace.
1301 */
1302static u64 primary_event_id(struct perf_event *event)
1303{
1304 u64 id = event->id;
1305
1306 if (event->parent)
1307 id = event->parent->id;
1308
1309 return id;
1310}
1311
1312/*
1313 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001314 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001315 * This has to cope with with the fact that until it is locked,
1316 * the context could get moved to another task.
1317 */
1318static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001319perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001320{
1321 struct perf_event_context *ctx;
1322
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001323retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001324 /*
1325 * One of the few rules of preemptible RCU is that one cannot do
1326 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001327 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001328 * rcu_read_unlock_special().
1329 *
1330 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001331 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001332 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001333 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001334 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001335 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001336 if (ctx) {
1337 /*
1338 * If this context is a clone of another, it might
1339 * get swapped for another underneath us by
1340 * perf_event_task_sched_out, though the
1341 * rcu_read_lock() protects us from any context
1342 * getting freed. Lock the context and check if it
1343 * got swapped before we could get the lock, and retry
1344 * if so. If we locked the right context, then it
1345 * can't get swapped on us any more.
1346 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001347 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001348 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001349 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001350 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001351 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001352 goto retry;
1353 }
1354
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001355 if (ctx->task == TASK_TOMBSTONE ||
1356 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001357 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001358 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001359 } else {
1360 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361 }
1362 }
1363 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001364 if (!ctx)
1365 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001366 return ctx;
1367}
1368
1369/*
1370 * Get the context for a task and increment its pin_count so it
1371 * can't get swapped to another task. This also increments its
1372 * reference count so that the context can't get freed.
1373 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001374static struct perf_event_context *
1375perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376{
1377 struct perf_event_context *ctx;
1378 unsigned long flags;
1379
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001380 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001381 if (ctx) {
1382 ++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 return ctx;
1386}
1387
1388static void perf_unpin_context(struct perf_event_context *ctx)
1389{
1390 unsigned long flags;
1391
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001392 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001393 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001394 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001395}
1396
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001397/*
1398 * Update the record of the current time in a context.
1399 */
1400static void update_context_time(struct perf_event_context *ctx)
1401{
1402 u64 now = perf_clock();
1403
1404 ctx->time += now - ctx->timestamp;
1405 ctx->timestamp = now;
1406}
1407
Stephane Eranian41587552011-01-03 18:20:01 +02001408static u64 perf_event_time(struct perf_event *event)
1409{
1410 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001411
1412 if (is_cgroup_event(event))
1413 return perf_cgroup_event_time(event);
1414
Stephane Eranian41587552011-01-03 18:20:01 +02001415 return ctx ? ctx->time : 0;
1416}
1417
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001418/*
1419 * Update the total_time_enabled and total_time_running fields for a event.
1420 */
1421static void update_event_times(struct perf_event *event)
1422{
1423 struct perf_event_context *ctx = event->ctx;
1424 u64 run_end;
1425
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001426 lockdep_assert_held(&ctx->lock);
1427
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001428 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1429 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1430 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001431
Stephane Eraniane5d13672011-02-14 11:20:01 +02001432 /*
1433 * in cgroup mode, time_enabled represents
1434 * the time the event was enabled AND active
1435 * tasks were in the monitored cgroup. This is
1436 * independent of the activity of the context as
1437 * there may be a mix of cgroup and non-cgroup events.
1438 *
1439 * That is why we treat cgroup events differently
1440 * here.
1441 */
1442 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001443 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001444 else if (ctx->is_active)
1445 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001446 else
1447 run_end = event->tstamp_stopped;
1448
1449 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001450
1451 if (event->state == PERF_EVENT_STATE_INACTIVE)
1452 run_end = event->tstamp_stopped;
1453 else
Stephane Eranian41587552011-01-03 18:20:01 +02001454 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001455
1456 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001457
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001458}
1459
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001460/*
1461 * Update total_time_enabled and total_time_running for all events in a group.
1462 */
1463static void update_group_times(struct perf_event *leader)
1464{
1465 struct perf_event *event;
1466
1467 update_event_times(leader);
1468 list_for_each_entry(event, &leader->sibling_list, group_entry)
1469 update_event_times(event);
1470}
1471
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001472static struct list_head *
1473ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1474{
1475 if (event->attr.pinned)
1476 return &ctx->pinned_groups;
1477 else
1478 return &ctx->flexible_groups;
1479}
1480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001481/*
1482 * Add a event from the lists for its context.
1483 * Must be called with ctx->mutex and ctx->lock held.
1484 */
1485static void
1486list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1487{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001488 lockdep_assert_held(&ctx->lock);
1489
Peter Zijlstra8a495422010-05-27 15:47:49 +02001490 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1491 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492
1493 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001494 * If we're a stand alone event or group leader, we go to the context
1495 * list, group events are kept attached to the group so that
1496 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001497 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001498 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001499 struct list_head *list;
1500
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001501 event->group_caps = event->event_caps;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001502
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001503 list = ctx_group_list(event, ctx);
1504 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001505 }
1506
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001507 list_update_cgroup_event(event, ctx, true);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001508
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001509 list_add_rcu(&event->event_entry, &ctx->event_list);
1510 ctx->nr_events++;
1511 if (event->attr.inherit_stat)
1512 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001513
1514 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001515}
1516
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001517/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001518 * Initialize event state based on the perf_event_attr::disabled.
1519 */
1520static inline void perf_event__state_init(struct perf_event *event)
1521{
1522 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1523 PERF_EVENT_STATE_INACTIVE;
1524}
1525
Peter Zijlstraa7239682015-09-09 19:06:33 +02001526static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001527{
1528 int entry = sizeof(u64); /* value */
1529 int size = 0;
1530 int nr = 1;
1531
1532 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1533 size += sizeof(u64);
1534
1535 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1536 size += sizeof(u64);
1537
1538 if (event->attr.read_format & PERF_FORMAT_ID)
1539 entry += sizeof(u64);
1540
1541 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001542 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001543 size += sizeof(u64);
1544 }
1545
1546 size += entry * nr;
1547 event->read_size = size;
1548}
1549
Peter Zijlstraa7239682015-09-09 19:06:33 +02001550static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001551{
1552 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001553 u16 size = 0;
1554
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001555 if (sample_type & PERF_SAMPLE_IP)
1556 size += sizeof(data->ip);
1557
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001558 if (sample_type & PERF_SAMPLE_ADDR)
1559 size += sizeof(data->addr);
1560
1561 if (sample_type & PERF_SAMPLE_PERIOD)
1562 size += sizeof(data->period);
1563
Andi Kleenc3feedf2013-01-24 16:10:28 +01001564 if (sample_type & PERF_SAMPLE_WEIGHT)
1565 size += sizeof(data->weight);
1566
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001567 if (sample_type & PERF_SAMPLE_READ)
1568 size += event->read_size;
1569
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001570 if (sample_type & PERF_SAMPLE_DATA_SRC)
1571 size += sizeof(data->data_src.val);
1572
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001573 if (sample_type & PERF_SAMPLE_TRANSACTION)
1574 size += sizeof(data->txn);
1575
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001576 event->header_size = size;
1577}
1578
Peter Zijlstraa7239682015-09-09 19:06:33 +02001579/*
1580 * Called at perf_event creation and when events are attached/detached from a
1581 * group.
1582 */
1583static void perf_event__header_size(struct perf_event *event)
1584{
1585 __perf_event_read_size(event,
1586 event->group_leader->nr_siblings);
1587 __perf_event_header_size(event, event->attr.sample_type);
1588}
1589
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001590static void perf_event__id_header_size(struct perf_event *event)
1591{
1592 struct perf_sample_data *data;
1593 u64 sample_type = event->attr.sample_type;
1594 u16 size = 0;
1595
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001596 if (sample_type & PERF_SAMPLE_TID)
1597 size += sizeof(data->tid_entry);
1598
1599 if (sample_type & PERF_SAMPLE_TIME)
1600 size += sizeof(data->time);
1601
Adrian Hunterff3d5272013-08-27 11:23:07 +03001602 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1603 size += sizeof(data->id);
1604
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001605 if (sample_type & PERF_SAMPLE_ID)
1606 size += sizeof(data->id);
1607
1608 if (sample_type & PERF_SAMPLE_STREAM_ID)
1609 size += sizeof(data->stream_id);
1610
1611 if (sample_type & PERF_SAMPLE_CPU)
1612 size += sizeof(data->cpu_entry);
1613
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001614 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001615}
1616
Peter Zijlstraa7239682015-09-09 19:06:33 +02001617static bool perf_event_validate_size(struct perf_event *event)
1618{
1619 /*
1620 * The values computed here will be over-written when we actually
1621 * attach the event.
1622 */
1623 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1624 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1625 perf_event__id_header_size(event);
1626
1627 /*
1628 * Sum the lot; should not exceed the 64k limit we have on records.
1629 * Conservative limit to allow for callchains and other variable fields.
1630 */
1631 if (event->read_size + event->header_size +
1632 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1633 return false;
1634
1635 return true;
1636}
1637
Peter Zijlstra8a495422010-05-27 15:47:49 +02001638static void perf_group_attach(struct perf_event *event)
1639{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001640 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001641
Peter Zijlstra3996a912017-01-26 16:39:55 +01001642 lockdep_assert_held(&event->ctx->lock);
1643
Peter Zijlstra74c33372010-10-15 11:40:29 +02001644 /*
1645 * We can have double attach due to group movement in perf_event_open.
1646 */
1647 if (event->attach_state & PERF_ATTACH_GROUP)
1648 return;
1649
Peter Zijlstra8a495422010-05-27 15:47:49 +02001650 event->attach_state |= PERF_ATTACH_GROUP;
1651
1652 if (group_leader == event)
1653 return;
1654
Peter Zijlstra652884f2015-01-23 11:20:10 +01001655 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1656
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001657 group_leader->group_caps &= event->event_caps;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001658
1659 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1660 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001661
1662 perf_event__header_size(group_leader);
1663
1664 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1665 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001666}
1667
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001668/*
1669 * Remove a event from the lists for its context.
1670 * Must be called with ctx->mutex and ctx->lock held.
1671 */
1672static void
1673list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1674{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001675 WARN_ON_ONCE(event->ctx != ctx);
1676 lockdep_assert_held(&ctx->lock);
1677
Peter Zijlstra8a495422010-05-27 15:47:49 +02001678 /*
1679 * We can have double detach due to exit/hot-unplug + close.
1680 */
1681 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001682 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001683
1684 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1685
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001686 list_update_cgroup_event(event, ctx, false);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001687
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688 ctx->nr_events--;
1689 if (event->attr.inherit_stat)
1690 ctx->nr_stat--;
1691
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692 list_del_rcu(&event->event_entry);
1693
Peter Zijlstra8a495422010-05-27 15:47:49 +02001694 if (event->group_leader == event)
1695 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001696
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001697 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001698
1699 /*
1700 * If event was in error state, then keep it
1701 * that way, otherwise bogus counts will be
1702 * returned on read(). The only way to get out
1703 * of error state is by explicit re-enabling
1704 * of the event
1705 */
1706 if (event->state > PERF_EVENT_STATE_OFF)
1707 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001708
1709 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001710}
1711
Peter Zijlstra8a495422010-05-27 15:47:49 +02001712static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001713{
1714 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001715 struct list_head *list = NULL;
1716
Peter Zijlstra3996a912017-01-26 16:39:55 +01001717 lockdep_assert_held(&event->ctx->lock);
1718
Peter Zijlstra8a495422010-05-27 15:47:49 +02001719 /*
1720 * We can have double detach due to exit/hot-unplug + close.
1721 */
1722 if (!(event->attach_state & PERF_ATTACH_GROUP))
1723 return;
1724
1725 event->attach_state &= ~PERF_ATTACH_GROUP;
1726
1727 /*
1728 * If this is a sibling, remove it from its group.
1729 */
1730 if (event->group_leader != event) {
1731 list_del_init(&event->group_entry);
1732 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001733 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001734 }
1735
1736 if (!list_empty(&event->group_entry))
1737 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739 /*
1740 * If this was a group event with sibling events then
1741 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001742 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001743 */
1744 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001745 if (list)
1746 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001748
1749 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001750 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001751
1752 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001754
1755out:
1756 perf_event__header_size(event->group_leader);
1757
1758 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1759 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760}
1761
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001762static bool is_orphaned_event(struct perf_event *event)
1763{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001764 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001765}
1766
Mark Rutland2c81a642016-06-14 16:10:41 +01001767static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01001768{
1769 struct pmu *pmu = event->pmu;
1770 return pmu->filter_match ? pmu->filter_match(event) : 1;
1771}
1772
Mark Rutland2c81a642016-06-14 16:10:41 +01001773/*
1774 * Check whether we should attempt to schedule an event group based on
1775 * PMU-specific filtering. An event group can consist of HW and SW events,
1776 * potentially with a SW leader, so we must check all the filters, to
1777 * determine whether a group is schedulable:
1778 */
1779static inline int pmu_filter_match(struct perf_event *event)
1780{
1781 struct perf_event *child;
1782
1783 if (!__pmu_filter_match(event))
1784 return 0;
1785
1786 list_for_each_entry(child, &event->sibling_list, group_entry) {
1787 if (!__pmu_filter_match(child))
1788 return 0;
1789 }
1790
1791 return 1;
1792}
1793
Stephane Eranianfa66f072010-08-26 16:40:01 +02001794static inline int
1795event_filter_match(struct perf_event *event)
1796{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001797 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
1798 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001799}
1800
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001801static void
1802event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001803 struct perf_cpu_context *cpuctx,
1804 struct perf_event_context *ctx)
1805{
Stephane Eranian41587552011-01-03 18:20:01 +02001806 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001807 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001808
1809 WARN_ON_ONCE(event->ctx != ctx);
1810 lockdep_assert_held(&ctx->lock);
1811
Stephane Eranianfa66f072010-08-26 16:40:01 +02001812 /*
1813 * An event which could not be activated because of
1814 * filter mismatch still needs to have its timings
1815 * maintained, otherwise bogus information is return
1816 * via read() for time_enabled, time_running:
1817 */
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001818 if (event->state == PERF_EVENT_STATE_INACTIVE &&
1819 !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001820 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001821 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001822 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001823 }
1824
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001825 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001826 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001827
Alexander Shishkin44377272013-12-16 14:17:36 +02001828 perf_pmu_disable(event->pmu);
1829
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001830 event->tstamp_stopped = tstamp;
1831 event->pmu->del(event, 0);
1832 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001833 event->state = PERF_EVENT_STATE_INACTIVE;
1834 if (event->pending_disable) {
1835 event->pending_disable = 0;
1836 event->state = PERF_EVENT_STATE_OFF;
1837 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838
1839 if (!is_software_event(event))
1840 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001841 if (!--ctx->nr_active)
1842 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001843 if (event->attr.freq && event->attr.sample_freq)
1844 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001845 if (event->attr.exclusive || !cpuctx->active_oncpu)
1846 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001847
1848 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001849}
1850
1851static void
1852group_sched_out(struct perf_event *group_event,
1853 struct perf_cpu_context *cpuctx,
1854 struct perf_event_context *ctx)
1855{
1856 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001857 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858
Mark Rutland3f005e72016-07-26 18:12:21 +01001859 perf_pmu_disable(ctx->pmu);
1860
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001861 event_sched_out(group_event, cpuctx, ctx);
1862
1863 /*
1864 * Schedule out siblings (if any):
1865 */
1866 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1867 event_sched_out(event, cpuctx, ctx);
1868
Mark Rutland3f005e72016-07-26 18:12:21 +01001869 perf_pmu_enable(ctx->pmu);
1870
Stephane Eranianfa66f072010-08-26 16:40:01 +02001871 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001872 cpuctx->exclusive = 0;
1873}
1874
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001875#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001877/*
1878 * Cross CPU call to remove a performance event
1879 *
1880 * We disable the event on the hardware level first. After that we
1881 * remove it from the context list.
1882 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001883static void
1884__perf_remove_from_context(struct perf_event *event,
1885 struct perf_cpu_context *cpuctx,
1886 struct perf_event_context *ctx,
1887 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001888{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001889 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001890
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001891 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001892 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001893 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001895
Peter Zijlstra39a43642016-01-11 12:46:35 +01001896 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001897 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001898 if (ctx->task) {
1899 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1900 cpuctx->task_ctx = NULL;
1901 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001903}
1904
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001905/*
1906 * Remove the event from a task's (or a CPU's) list of events.
1907 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001908 * If event->ctx is a cloned context, callers must make sure that
1909 * every task struct that event->ctx->task could possibly point to
1910 * remains valid. This is OK when called from perf_release since
1911 * that only calls us on the top-level context, which can't be a clone.
1912 * When called from perf_event_exit_task, it's OK because the
1913 * context has been detached from its task.
1914 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001915static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916{
Peter Zijlstra3996a912017-01-26 16:39:55 +01001917 struct perf_event_context *ctx = event->ctx;
1918
1919 lockdep_assert_held(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001920
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001921 event_function_call(event, __perf_remove_from_context, (void *)flags);
Peter Zijlstra3996a912017-01-26 16:39:55 +01001922
1923 /*
1924 * The above event_function_call() can NO-OP when it hits
1925 * TASK_TOMBSTONE. In that case we must already have been detached
1926 * from the context (by perf_event_exit_event()) but the grouping
1927 * might still be in-tact.
1928 */
1929 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1930 if ((flags & DETACH_GROUP) &&
1931 (event->attach_state & PERF_ATTACH_GROUP)) {
1932 /*
1933 * Since in that case we cannot possibly be scheduled, simply
1934 * detach now.
1935 */
1936 raw_spin_lock_irq(&ctx->lock);
1937 perf_group_detach(event);
1938 raw_spin_unlock_irq(&ctx->lock);
1939 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001940}
1941
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001942/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001943 * Cross CPU call to disable a performance event
1944 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001945static void __perf_event_disable(struct perf_event *event,
1946 struct perf_cpu_context *cpuctx,
1947 struct perf_event_context *ctx,
1948 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001949{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001950 if (event->state < PERF_EVENT_STATE_INACTIVE)
1951 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001952
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001953 update_context_time(ctx);
1954 update_cgrp_time_from_event(event);
1955 update_group_times(event);
1956 if (event == event->group_leader)
1957 group_sched_out(event, cpuctx, ctx);
1958 else
1959 event_sched_out(event, cpuctx, ctx);
1960 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001961}
1962
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963/*
1964 * Disable a event.
1965 *
1966 * If event->ctx is a cloned context, callers must make sure that
1967 * every task struct that event->ctx->task could possibly point to
1968 * remains valid. This condition is satisifed when called through
1969 * perf_event_for_each_child or perf_event_for_each because they
1970 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001971 * goes to exit will block in perf_event_exit_event().
1972 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001973 * When called from perf_pending_event it's OK because event->ctx
1974 * is the current context on this CPU and preemption is disabled,
1975 * hence we can't get into perf_event_task_sched_out for this context.
1976 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001977static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001978{
1979 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001981 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001982 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001983 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001984 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001985 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001986 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001987
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001988 event_function_call(event, __perf_event_disable, NULL);
1989}
1990
1991void perf_event_disable_local(struct perf_event *event)
1992{
1993 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001994}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001995
1996/*
1997 * Strictly speaking kernel users cannot create groups and therefore this
1998 * interface does not need the perf_event_ctx_lock() magic.
1999 */
2000void perf_event_disable(struct perf_event *event)
2001{
2002 struct perf_event_context *ctx;
2003
2004 ctx = perf_event_ctx_lock(event);
2005 _perf_event_disable(event);
2006 perf_event_ctx_unlock(event, ctx);
2007}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002008EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002009
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002010void perf_event_disable_inatomic(struct perf_event *event)
2011{
2012 event->pending_disable = 1;
2013 irq_work_queue(&event->pending);
2014}
2015
Stephane Eraniane5d13672011-02-14 11:20:01 +02002016static void perf_set_shadow_time(struct perf_event *event,
2017 struct perf_event_context *ctx,
2018 u64 tstamp)
2019{
2020 /*
2021 * use the correct time source for the time snapshot
2022 *
2023 * We could get by without this by leveraging the
2024 * fact that to get to this function, the caller
2025 * has most likely already called update_context_time()
2026 * and update_cgrp_time_xx() and thus both timestamp
2027 * are identical (or very close). Given that tstamp is,
2028 * already adjusted for cgroup, we could say that:
2029 * tstamp - ctx->timestamp
2030 * is equivalent to
2031 * tstamp - cgrp->timestamp.
2032 *
2033 * Then, in perf_output_read(), the calculation would
2034 * work with no changes because:
2035 * - event is guaranteed scheduled in
2036 * - no scheduled out in between
2037 * - thus the timestamp would be the same
2038 *
2039 * But this is a bit hairy.
2040 *
2041 * So instead, we have an explicit cgroup call to remain
2042 * within the time time source all along. We believe it
2043 * is cleaner and simpler to understand.
2044 */
2045 if (is_cgroup_event(event))
2046 perf_cgroup_set_shadow_time(event, tstamp);
2047 else
2048 event->shadow_ctx_time = tstamp - ctx->timestamp;
2049}
2050
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002051#define MAX_INTERRUPTS (~0ULL)
2052
2053static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002054static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002055
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002056static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002057event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002058 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002059 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060{
Stephane Eranian41587552011-01-03 18:20:01 +02002061 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02002062 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002063
Peter Zijlstra63342412014-05-05 11:49:16 +02002064 lockdep_assert_held(&ctx->lock);
2065
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002066 if (event->state <= PERF_EVENT_STATE_OFF)
2067 return 0;
2068
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002069 WRITE_ONCE(event->oncpu, smp_processor_id());
2070 /*
2071 * Order event::oncpu write to happen before the ACTIVE state
2072 * is visible.
2073 */
2074 smp_wmb();
2075 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002076
2077 /*
2078 * Unthrottle events, since we scheduled we might have missed several
2079 * ticks already, also for a heavily scheduling task there is little
2080 * guarantee it'll get a tick in a timely manner.
2081 */
2082 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2083 perf_log_throttle(event, 1);
2084 event->hw.interrupts = 0;
2085 }
2086
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002087 /*
2088 * The new state must be visible before we turn it on in the hardware:
2089 */
2090 smp_wmb();
2091
Alexander Shishkin44377272013-12-16 14:17:36 +02002092 perf_pmu_disable(event->pmu);
2093
Shaohua Li72f669c2015-02-05 15:55:31 -08002094 perf_set_shadow_time(event, ctx, tstamp);
2095
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002096 perf_log_itrace_start(event);
2097
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002098 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002099 event->state = PERF_EVENT_STATE_INACTIVE;
2100 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002101 ret = -EAGAIN;
2102 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002103 }
2104
Peter Zijlstra00a29162015-07-27 10:35:07 +02002105 event->tstamp_running += tstamp - event->tstamp_stopped;
2106
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002107 if (!is_software_event(event))
2108 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002109 if (!ctx->nr_active++)
2110 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002111 if (event->attr.freq && event->attr.sample_freq)
2112 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002113
2114 if (event->attr.exclusive)
2115 cpuctx->exclusive = 1;
2116
Alexander Shishkin44377272013-12-16 14:17:36 +02002117out:
2118 perf_pmu_enable(event->pmu);
2119
2120 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002121}
2122
2123static int
2124group_sched_in(struct perf_event *group_event,
2125 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002126 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127{
Lin Ming6bde9b62010-04-23 13:56:00 +08002128 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002129 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002130 u64 now = ctx->time;
2131 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002132
2133 if (group_event->state == PERF_EVENT_STATE_OFF)
2134 return 0;
2135
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002136 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002137
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002138 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002139 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002140 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002141 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002142 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143
2144 /*
2145 * Schedule in siblings as one group (if any):
2146 */
2147 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002148 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002149 partial_group = event;
2150 goto group_error;
2151 }
2152 }
2153
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002154 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002155 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002156
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002157group_error:
2158 /*
2159 * Groups can be scheduled in as one unit only, so undo any
2160 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002161 * The events up to the failed event are scheduled out normally,
2162 * tstamp_stopped will be updated.
2163 *
2164 * The failed events and the remaining siblings need to have
2165 * their timings updated as if they had gone thru event_sched_in()
2166 * and event_sched_out(). This is required to get consistent timings
2167 * across the group. This also takes care of the case where the group
2168 * could never be scheduled by ensuring tstamp_stopped is set to mark
2169 * the time the event was actually stopped, such that time delta
2170 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002171 */
2172 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2173 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002174 simulate = true;
2175
2176 if (simulate) {
2177 event->tstamp_running += now - event->tstamp_stopped;
2178 event->tstamp_stopped = now;
2179 } else {
2180 event_sched_out(event, cpuctx, ctx);
2181 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002182 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002183 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002184
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002185 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002186
Peter Zijlstra272325c2015-04-15 11:41:58 +02002187 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002189 return -EAGAIN;
2190}
2191
2192/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002193 * Work out whether we can put this event group on the CPU now.
2194 */
2195static int group_can_go_on(struct perf_event *event,
2196 struct perf_cpu_context *cpuctx,
2197 int can_add_hw)
2198{
2199 /*
2200 * Groups consisting entirely of software events can always go on.
2201 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002202 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002203 return 1;
2204 /*
2205 * If an exclusive group is already on, no other hardware
2206 * events can go on.
2207 */
2208 if (cpuctx->exclusive)
2209 return 0;
2210 /*
2211 * If this group is exclusive and there are already
2212 * events on the CPU, it can't go on.
2213 */
2214 if (event->attr.exclusive && cpuctx->active_oncpu)
2215 return 0;
2216 /*
2217 * Otherwise, try to add it if all previous groups were able
2218 * to go on.
2219 */
2220 return can_add_hw;
2221}
2222
2223static void add_event_to_ctx(struct perf_event *event,
2224 struct perf_event_context *ctx)
2225{
Stephane Eranian41587552011-01-03 18:20:01 +02002226 u64 tstamp = perf_event_time(event);
2227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002229 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002230 event->tstamp_enabled = tstamp;
2231 event->tstamp_running = tstamp;
2232 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002233}
2234
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002235static void ctx_sched_out(struct perf_event_context *ctx,
2236 struct perf_cpu_context *cpuctx,
2237 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002238static void
2239ctx_sched_in(struct perf_event_context *ctx,
2240 struct perf_cpu_context *cpuctx,
2241 enum event_type_t event_type,
2242 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002243
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002244static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2245 struct perf_event_context *ctx)
2246{
2247 if (!cpuctx->task_ctx)
2248 return;
2249
2250 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2251 return;
2252
2253 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2254}
2255
Peter Zijlstradce58552011-04-09 21:17:46 +02002256static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2257 struct perf_event_context *ctx,
2258 struct task_struct *task)
2259{
2260 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2261 if (ctx)
2262 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2263 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2264 if (ctx)
2265 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2266}
2267
Peter Zijlstra3e349502016-01-08 10:01:18 +01002268static void ctx_resched(struct perf_cpu_context *cpuctx,
2269 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002270{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002271 perf_pmu_disable(cpuctx->ctx.pmu);
2272 if (task_ctx)
2273 task_ctx_sched_out(cpuctx, task_ctx);
2274 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2275 perf_event_sched_in(cpuctx, task_ctx, current);
2276 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002277}
2278
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279/*
2280 * Cross CPU call to install and enable a performance event
2281 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002282 * Very similar to remote_function() + event_function() but cannot assume that
2283 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002285static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002286{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002287 struct perf_event *event = info;
2288 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002289 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002290 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra1c686332016-12-09 14:59:00 +01002291 bool reprogram = true;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002292 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002293
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002294 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002295 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002296 raw_spin_lock(&ctx->lock);
2297 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002298
Peter Zijlstra1c686332016-12-09 14:59:00 +01002299 reprogram = (ctx->task == current);
2300
2301 /*
2302 * If the task is running, it must be running on this CPU,
2303 * otherwise we cannot reprogram things.
2304 *
2305 * If its not running, we don't care, ctx->lock will
2306 * serialize against it becoming runnable.
2307 */
2308 if (task_curr(ctx->task) && !reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002309 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002310 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002311 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002312
Peter Zijlstra1c686332016-12-09 14:59:00 +01002313 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002314 } else if (task_ctx) {
2315 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002316 }
2317
Peter Zijlstra1c686332016-12-09 14:59:00 +01002318 if (reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002319 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2320 add_event_to_ctx(event, ctx);
2321 ctx_resched(cpuctx, task_ctx);
2322 } else {
2323 add_event_to_ctx(event, ctx);
2324 }
2325
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002326unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002327 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002328
Peter Zijlstraa0963092016-02-24 18:45:50 +01002329 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002330}
2331
2332/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002333 * Attach a performance event to a context.
2334 *
2335 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002336 */
2337static void
2338perf_install_in_context(struct perf_event_context *ctx,
2339 struct perf_event *event,
2340 int cpu)
2341{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002342 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002343
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002344 lockdep_assert_held(&ctx->mutex);
2345
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002346 if (event->cpu != -1)
2347 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002348
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002349 /*
2350 * Ensures that if we can observe event->ctx, both the event and ctx
2351 * will be 'complete'. See perf_iterate_sb_cpu().
2352 */
2353 smp_store_release(&event->ctx, ctx);
2354
Peter Zijlstraa0963092016-02-24 18:45:50 +01002355 if (!task) {
2356 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002357 return;
2358 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002359
Peter Zijlstraa0963092016-02-24 18:45:50 +01002360 /*
2361 * Should not happen, we validate the ctx is still alive before calling.
2362 */
2363 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2364 return;
2365
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002366 /*
2367 * Installing events is tricky because we cannot rely on ctx->is_active
2368 * to be set in case this is the nr_events 0 -> 1 transition.
Peter Zijlstra1c686332016-12-09 14:59:00 +01002369 *
2370 * Instead we use task_curr(), which tells us if the task is running.
2371 * However, since we use task_curr() outside of rq::lock, we can race
2372 * against the actual state. This means the result can be wrong.
2373 *
2374 * If we get a false positive, we retry, this is harmless.
2375 *
2376 * If we get a false negative, things are complicated. If we are after
2377 * perf_event_context_sched_in() ctx::lock will serialize us, and the
2378 * value must be correct. If we're before, it doesn't matter since
2379 * perf_event_context_sched_in() will program the counter.
2380 *
2381 * However, this hinges on the remote context switch having observed
2382 * our task->perf_event_ctxp[] store, such that it will in fact take
2383 * ctx::lock in perf_event_context_sched_in().
2384 *
2385 * We do this by task_function_call(), if the IPI fails to hit the task
2386 * we know any future context switch of task must see the
2387 * perf_event_ctpx[] store.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002388 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002389
Peter Zijlstraa0963092016-02-24 18:45:50 +01002390 /*
Peter Zijlstra1c686332016-12-09 14:59:00 +01002391 * This smp_mb() orders the task->perf_event_ctxp[] store with the
2392 * task_cpu() load, such that if the IPI then does not find the task
2393 * running, a future context switch of that task must observe the
2394 * store.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002395 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002396 smp_mb();
2397again:
2398 if (!task_function_call(task, __perf_install_in_context, event))
Peter Zijlstraa0963092016-02-24 18:45:50 +01002399 return;
2400
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401 raw_spin_lock_irq(&ctx->lock);
2402 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002403 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2404 /*
2405 * Cannot happen because we already checked above (which also
2406 * cannot happen), and we hold ctx->mutex, which serializes us
2407 * against perf_event_exit_task_context().
2408 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002409 raw_spin_unlock_irq(&ctx->lock);
2410 return;
2411 }
Peter Zijlstraa0963092016-02-24 18:45:50 +01002412 /*
Peter Zijlstra1c686332016-12-09 14:59:00 +01002413 * If the task is not running, ctx->lock will avoid it becoming so,
2414 * thus we can safely install the event.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002415 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002416 if (task_curr(task)) {
2417 raw_spin_unlock_irq(&ctx->lock);
2418 goto again;
2419 }
2420 add_event_to_ctx(event, ctx);
2421 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002422}
2423
2424/*
2425 * Put a event into inactive state and update time fields.
2426 * Enabling the leader of a group effectively enables all
2427 * the group members that aren't explicitly disabled, so we
2428 * have to update their ->tstamp_enabled also.
2429 * Note: this works for group members as well as group leaders
2430 * since the non-leader members' sibling_lists will be empty.
2431 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002432static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002433{
2434 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002435 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002436
2437 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002438 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002439 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002440 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2441 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002442 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002443}
2444
2445/*
2446 * Cross CPU call to enable a performance event
2447 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002448static void __perf_event_enable(struct perf_event *event,
2449 struct perf_cpu_context *cpuctx,
2450 struct perf_event_context *ctx,
2451 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002452{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002453 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002454 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002455
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002456 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2457 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002458 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002460 if (ctx->is_active)
2461 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2462
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002463 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002464
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002465 if (!ctx->is_active)
2466 return;
2467
Stephane Eraniane5d13672011-02-14 11:20:01 +02002468 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002469 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002470 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002471 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002472 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002473 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002474
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002475 /*
2476 * If the event is in a group and isn't the group leader,
2477 * then don't put it on unless the group is on.
2478 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002479 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2480 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002481 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002482 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002483
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002484 task_ctx = cpuctx->task_ctx;
2485 if (ctx->task)
2486 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002487
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002488 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002489}
2490
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002491/*
2492 * Enable a event.
2493 *
2494 * If event->ctx is a cloned context, callers must make sure that
2495 * every task struct that event->ctx->task could possibly point to
2496 * remains valid. This condition is satisfied when called through
2497 * perf_event_for_each_child or perf_event_for_each as described
2498 * for perf_event_disable.
2499 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002500static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002501{
2502 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002503
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002504 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002505 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2506 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002507 raw_spin_unlock_irq(&ctx->lock);
2508 return;
2509 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002510
2511 /*
2512 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002513 *
2514 * That way, if we see the event in error state below, we know that it
2515 * has gone back into error state, as distinct from the task having
2516 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002517 */
2518 if (event->state == PERF_EVENT_STATE_ERROR)
2519 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002520 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002521
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002522 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002523}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002524
2525/*
2526 * See perf_event_disable();
2527 */
2528void perf_event_enable(struct perf_event *event)
2529{
2530 struct perf_event_context *ctx;
2531
2532 ctx = perf_event_ctx_lock(event);
2533 _perf_event_enable(event);
2534 perf_event_ctx_unlock(event, ctx);
2535}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002536EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002537
Alexander Shishkin375637b2016-04-27 18:44:46 +03002538struct stop_event_data {
2539 struct perf_event *event;
2540 unsigned int restart;
2541};
2542
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002543static int __perf_event_stop(void *info)
2544{
Alexander Shishkin375637b2016-04-27 18:44:46 +03002545 struct stop_event_data *sd = info;
2546 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002547
Alexander Shishkin375637b2016-04-27 18:44:46 +03002548 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002549 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2550 return 0;
2551
2552 /* matches smp_wmb() in event_sched_in() */
2553 smp_rmb();
2554
2555 /*
2556 * There is a window with interrupts enabled before we get here,
2557 * so we need to check again lest we try to stop another CPU's event.
2558 */
2559 if (READ_ONCE(event->oncpu) != smp_processor_id())
2560 return -EAGAIN;
2561
2562 event->pmu->stop(event, PERF_EF_UPDATE);
2563
Alexander Shishkin375637b2016-04-27 18:44:46 +03002564 /*
2565 * May race with the actual stop (through perf_pmu_output_stop()),
2566 * but it is only used for events with AUX ring buffer, and such
2567 * events will refuse to restart because of rb::aux_mmap_count==0,
2568 * see comments in perf_aux_output_begin().
2569 *
2570 * Since this is happening on a event-local CPU, no trace is lost
2571 * while restarting.
2572 */
2573 if (sd->restart)
Will Deaconc9bbdd42016-08-15 11:42:45 +01002574 event->pmu->start(event, 0);
Alexander Shishkin375637b2016-04-27 18:44:46 +03002575
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002576 return 0;
2577}
2578
Alexander Shishkin767ae082016-09-06 16:23:49 +03002579static int perf_event_stop(struct perf_event *event, int restart)
Alexander Shishkin375637b2016-04-27 18:44:46 +03002580{
2581 struct stop_event_data sd = {
2582 .event = event,
Alexander Shishkin767ae082016-09-06 16:23:49 +03002583 .restart = restart,
Alexander Shishkin375637b2016-04-27 18:44:46 +03002584 };
2585 int ret = 0;
2586
2587 do {
2588 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2589 return 0;
2590
2591 /* matches smp_wmb() in event_sched_in() */
2592 smp_rmb();
2593
2594 /*
2595 * We only want to restart ACTIVE events, so if the event goes
2596 * inactive here (event->oncpu==-1), there's nothing more to do;
2597 * fall through with ret==-ENXIO.
2598 */
2599 ret = cpu_function_call(READ_ONCE(event->oncpu),
2600 __perf_event_stop, &sd);
2601 } while (ret == -EAGAIN);
2602
2603 return ret;
2604}
2605
2606/*
2607 * In order to contain the amount of racy and tricky in the address filter
2608 * configuration management, it is a two part process:
2609 *
2610 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2611 * we update the addresses of corresponding vmas in
2612 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2613 * (p2) when an event is scheduled in (pmu::add), it calls
2614 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2615 * if the generation has changed since the previous call.
2616 *
2617 * If (p1) happens while the event is active, we restart it to force (p2).
2618 *
2619 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2620 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2621 * ioctl;
2622 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2623 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2624 * for reading;
2625 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2626 * of exec.
2627 */
2628void perf_event_addr_filters_sync(struct perf_event *event)
2629{
2630 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2631
2632 if (!has_addr_filter(event))
2633 return;
2634
2635 raw_spin_lock(&ifh->lock);
2636 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2637 event->pmu->addr_filters_sync(event);
2638 event->hw.addr_filters_gen = event->addr_filters_gen;
2639 }
2640 raw_spin_unlock(&ifh->lock);
2641}
2642EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2643
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002644static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002645{
2646 /*
2647 * not supported on inherited events
2648 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002649 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002650 return -EINVAL;
2651
2652 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002653 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002654
2655 return 0;
2656}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002657
2658/*
2659 * See perf_event_disable()
2660 */
2661int perf_event_refresh(struct perf_event *event, int refresh)
2662{
2663 struct perf_event_context *ctx;
2664 int ret;
2665
2666 ctx = perf_event_ctx_lock(event);
2667 ret = _perf_event_refresh(event, refresh);
2668 perf_event_ctx_unlock(event, ctx);
2669
2670 return ret;
2671}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002672EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002673
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002674static void ctx_sched_out(struct perf_event_context *ctx,
2675 struct perf_cpu_context *cpuctx,
2676 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002677{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002678 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002679 struct perf_event *event;
2680
2681 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002682
Peter Zijlstra39a43642016-01-11 12:46:35 +01002683 if (likely(!ctx->nr_events)) {
2684 /*
2685 * See __perf_remove_from_context().
2686 */
2687 WARN_ON_ONCE(ctx->is_active);
2688 if (ctx->task)
2689 WARN_ON_ONCE(cpuctx->task_ctx);
2690 return;
2691 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002692
Peter Zijlstradb24d332011-04-09 21:17:45 +02002693 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002694 if (!(ctx->is_active & EVENT_ALL))
2695 ctx->is_active = 0;
2696
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002697 if (ctx->task) {
2698 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2699 if (!ctx->is_active)
2700 cpuctx->task_ctx = NULL;
2701 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002702
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002703 /*
2704 * Always update time if it was set; not only when it changes.
2705 * Otherwise we can 'forget' to update time for any but the last
2706 * context we sched out. For example:
2707 *
2708 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2709 * ctx_sched_out(.event_type = EVENT_PINNED)
2710 *
2711 * would only update time for the pinned events.
2712 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002713 if (is_active & EVENT_TIME) {
2714 /* update (and stop) ctx time */
2715 update_context_time(ctx);
2716 update_cgrp_time_from_cpuctx(cpuctx);
2717 }
2718
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002719 is_active ^= ctx->is_active; /* changed bits */
2720
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002721 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002722 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002723
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002724 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002725 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002726 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2727 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002728 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002729
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002730 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002731 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002732 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002733 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002734 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735}
2736
2737/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002738 * Test whether two contexts are equivalent, i.e. whether they have both been
2739 * cloned from the same version of the same context.
2740 *
2741 * Equivalence is measured using a generation number in the context that is
2742 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2743 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002744 */
2745static int context_equiv(struct perf_event_context *ctx1,
2746 struct perf_event_context *ctx2)
2747{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002748 lockdep_assert_held(&ctx1->lock);
2749 lockdep_assert_held(&ctx2->lock);
2750
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002751 /* Pinning disables the swap optimization */
2752 if (ctx1->pin_count || ctx2->pin_count)
2753 return 0;
2754
2755 /* If ctx1 is the parent of ctx2 */
2756 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2757 return 1;
2758
2759 /* If ctx2 is the parent of ctx1 */
2760 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2761 return 1;
2762
2763 /*
2764 * If ctx1 and ctx2 have the same parent; we flatten the parent
2765 * hierarchy, see perf_event_init_context().
2766 */
2767 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2768 ctx1->parent_gen == ctx2->parent_gen)
2769 return 1;
2770
2771 /* Unmatched */
2772 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773}
2774
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002775static void __perf_event_sync_stat(struct perf_event *event,
2776 struct perf_event *next_event)
2777{
2778 u64 value;
2779
2780 if (!event->attr.inherit_stat)
2781 return;
2782
2783 /*
2784 * Update the event value, we cannot use perf_event_read()
2785 * because we're in the middle of a context switch and have IRQs
2786 * disabled, which upsets smp_call_function_single(), however
2787 * we know the event must be on the current CPU, therefore we
2788 * don't need to use it.
2789 */
2790 switch (event->state) {
2791 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002792 event->pmu->read(event);
2793 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002794
2795 case PERF_EVENT_STATE_INACTIVE:
2796 update_event_times(event);
2797 break;
2798
2799 default:
2800 break;
2801 }
2802
2803 /*
2804 * In order to keep per-task stats reliable we need to flip the event
2805 * values when we flip the contexts.
2806 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002807 value = local64_read(&next_event->count);
2808 value = local64_xchg(&event->count, value);
2809 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002810
2811 swap(event->total_time_enabled, next_event->total_time_enabled);
2812 swap(event->total_time_running, next_event->total_time_running);
2813
2814 /*
2815 * Since we swizzled the values, update the user visible data too.
2816 */
2817 perf_event_update_userpage(event);
2818 perf_event_update_userpage(next_event);
2819}
2820
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002821static void perf_event_sync_stat(struct perf_event_context *ctx,
2822 struct perf_event_context *next_ctx)
2823{
2824 struct perf_event *event, *next_event;
2825
2826 if (!ctx->nr_stat)
2827 return;
2828
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002829 update_context_time(ctx);
2830
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002831 event = list_first_entry(&ctx->event_list,
2832 struct perf_event, event_entry);
2833
2834 next_event = list_first_entry(&next_ctx->event_list,
2835 struct perf_event, event_entry);
2836
2837 while (&event->event_entry != &ctx->event_list &&
2838 &next_event->event_entry != &next_ctx->event_list) {
2839
2840 __perf_event_sync_stat(event, next_event);
2841
2842 event = list_next_entry(event, event_entry);
2843 next_event = list_next_entry(next_event, event_entry);
2844 }
2845}
2846
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002847static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2848 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002850 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002851 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002852 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002853 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002854 int do_switch = 1;
2855
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002856 if (likely(!ctx))
2857 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002858
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002859 cpuctx = __get_cpu_context(ctx);
2860 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002861 return;
2862
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002863 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002864 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002865 if (!next_ctx)
2866 goto unlock;
2867
2868 parent = rcu_dereference(ctx->parent_ctx);
2869 next_parent = rcu_dereference(next_ctx->parent_ctx);
2870
2871 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002872 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002873 goto unlock;
2874
2875 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002876 /*
2877 * Looks like the two contexts are clones, so we might be
2878 * able to optimize the context switch. We lock both
2879 * contexts and check that they are clones under the
2880 * lock (including re-checking that neither has been
2881 * uncloned in the meantime). It doesn't matter which
2882 * order we take the locks because no other cpu could
2883 * be trying to lock both of these tasks.
2884 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002885 raw_spin_lock(&ctx->lock);
2886 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002887 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002888 WRITE_ONCE(ctx->task, next);
2889 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002890
2891 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2892
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002893 /*
2894 * RCU_INIT_POINTER here is safe because we've not
2895 * modified the ctx and the above modification of
2896 * ctx->task and ctx->task_ctx_data are immaterial
2897 * since those values are always verified under
2898 * ctx->lock which we're now holding.
2899 */
2900 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2901 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2902
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002903 do_switch = 0;
2904
2905 perf_event_sync_stat(ctx, next_ctx);
2906 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002907 raw_spin_unlock(&next_ctx->lock);
2908 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002910unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002911 rcu_read_unlock();
2912
2913 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002914 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002915 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002916 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002917 }
2918}
2919
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002920static DEFINE_PER_CPU(struct list_head, sched_cb_list);
2921
Yan, Zhengba532502014-11-04 21:55:58 -05002922void perf_sched_cb_dec(struct pmu *pmu)
2923{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002924 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2925
Yan, Zhengba532502014-11-04 21:55:58 -05002926 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002927
2928 if (!--cpuctx->sched_cb_usage)
2929 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05002930}
2931
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002932
Yan, Zhengba532502014-11-04 21:55:58 -05002933void perf_sched_cb_inc(struct pmu *pmu)
2934{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002935 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2936
2937 if (!cpuctx->sched_cb_usage++)
2938 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
2939
Yan, Zhengba532502014-11-04 21:55:58 -05002940 this_cpu_inc(perf_sched_cb_usages);
2941}
2942
2943/*
2944 * This function provides the context switch callback to the lower code
2945 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02002946 *
2947 * This callback is relevant even to per-cpu events; for example multi event
2948 * PEBS requires this to provide PID/TID information. This requires we flush
2949 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05002950 */
2951static void perf_pmu_sched_task(struct task_struct *prev,
2952 struct task_struct *next,
2953 bool sched_in)
2954{
2955 struct perf_cpu_context *cpuctx;
2956 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05002957
2958 if (prev == next)
2959 return;
2960
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002961 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
2962 pmu = cpuctx->unique_pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05002963
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002964 if (WARN_ON_ONCE(!pmu->sched_task))
2965 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05002966
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002967 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2968 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05002969
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002970 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05002971
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002972 perf_pmu_enable(pmu);
2973 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05002974 }
Yan, Zhengba532502014-11-04 21:55:58 -05002975}
2976
Adrian Hunter45ac1402015-07-21 12:44:02 +03002977static void perf_event_switch(struct task_struct *task,
2978 struct task_struct *next_prev, bool sched_in);
2979
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002980#define for_each_task_context_nr(ctxn) \
2981 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2982
2983/*
2984 * Called from scheduler to remove the events of the current task,
2985 * with interrupts disabled.
2986 *
2987 * We stop each event and update the event value in event->count.
2988 *
2989 * This does not protect us against NMI, but disable()
2990 * sets the disabled bit in the control field of event _before_
2991 * accessing the event control register. If a NMI hits, then it will
2992 * not restart the event.
2993 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002994void __perf_event_task_sched_out(struct task_struct *task,
2995 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002996{
2997 int ctxn;
2998
Yan, Zhengba532502014-11-04 21:55:58 -05002999 if (__this_cpu_read(perf_sched_cb_usages))
3000 perf_pmu_sched_task(task, next, false);
3001
Adrian Hunter45ac1402015-07-21 12:44:02 +03003002 if (atomic_read(&nr_switch_events))
3003 perf_event_switch(task, next, false);
3004
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003005 for_each_task_context_nr(ctxn)
3006 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003007
3008 /*
3009 * if cgroup events exist on this CPU, then we need
3010 * to check if we have to switch out PMU state.
3011 * cgroup event are system-wide mode only
3012 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05003013 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003014 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003015}
3016
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003017/*
3018 * Called with IRQs disabled
3019 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003020static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
3021 enum event_type_t event_type)
3022{
3023 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024}
3025
3026static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003027ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003028 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029{
3030 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003031
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003032 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
3033 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003034 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02003035 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003036 continue;
3037
Stephane Eraniane5d13672011-02-14 11:20:01 +02003038 /* may need to reset tstamp_enabled */
3039 if (is_cgroup_event(event))
3040 perf_cgroup_mark_enabled(event, ctx);
3041
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08003042 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01003043 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044
3045 /*
3046 * If this pinned group hasn't been scheduled,
3047 * put it in error state.
3048 */
3049 if (event->state == PERF_EVENT_STATE_INACTIVE) {
3050 update_group_times(event);
3051 event->state = PERF_EVENT_STATE_ERROR;
3052 }
3053 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003054}
3055
3056static void
3057ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003058 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003059{
3060 struct perf_event *event;
3061 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003062
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003063 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
3064 /* Ignore events in OFF or ERROR state */
3065 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003066 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003067 /*
3068 * Listen to the 'cpu' scheduling filter constraint
3069 * of events:
3070 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02003071 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003072 continue;
3073
Stephane Eraniane5d13672011-02-14 11:20:01 +02003074 /* may need to reset tstamp_enabled */
3075 if (is_cgroup_event(event))
3076 perf_cgroup_mark_enabled(event, ctx);
3077
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003078 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01003079 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003080 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003081 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003082 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003083}
3084
3085static void
3086ctx_sched_in(struct perf_event_context *ctx,
3087 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003088 enum event_type_t event_type,
3089 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003090{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003091 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003092 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003093
Peter Zijlstrac994d612016-01-08 09:20:23 +01003094 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003095
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003096 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003097 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003098
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003099 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003100 if (ctx->task) {
3101 if (!is_active)
3102 cpuctx->task_ctx = ctx;
3103 else
3104 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3105 }
3106
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003107 is_active ^= ctx->is_active; /* changed bits */
3108
3109 if (is_active & EVENT_TIME) {
3110 /* start ctx time */
3111 now = perf_clock();
3112 ctx->timestamp = now;
3113 perf_cgroup_set_timestamp(task, ctx);
3114 }
3115
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003116 /*
3117 * First go through the list and put on any pinned groups
3118 * in order to give them the best chance of going on.
3119 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003120 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003121 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003122
3123 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003124 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003125 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003126}
3127
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003128static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003129 enum event_type_t event_type,
3130 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003131{
3132 struct perf_event_context *ctx = &cpuctx->ctx;
3133
Stephane Eraniane5d13672011-02-14 11:20:01 +02003134 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003135}
3136
Stephane Eraniane5d13672011-02-14 11:20:01 +02003137static void perf_event_context_sched_in(struct perf_event_context *ctx,
3138 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003139{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003140 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003141
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003142 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003143 if (cpuctx->task_ctx == ctx)
3144 return;
3145
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003146 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003147 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003148 /*
3149 * We want to keep the following priority order:
3150 * cpu pinned (that don't need to move), task pinned,
3151 * cpu flexible, task flexible.
3152 */
3153 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003154 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003155 perf_pmu_enable(ctx->pmu);
3156 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157}
3158
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003159/*
3160 * Called from scheduler to add the events of the current task
3161 * with interrupts disabled.
3162 *
3163 * We restore the event value and then enable it.
3164 *
3165 * This does not protect us against NMI, but enable()
3166 * sets the enabled bit in the control field of event _before_
3167 * accessing the event control register. If a NMI hits, then it will
3168 * keep the event running.
3169 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003170void __perf_event_task_sched_in(struct task_struct *prev,
3171 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003172{
3173 struct perf_event_context *ctx;
3174 int ctxn;
3175
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003176 /*
3177 * If cgroup events exist on this CPU, then we need to check if we have
3178 * to switch in PMU state; cgroup event are system-wide mode only.
3179 *
3180 * Since cgroup events are CPU events, we must schedule these in before
3181 * we schedule in the task events.
3182 */
3183 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3184 perf_cgroup_sched_in(prev, task);
3185
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003186 for_each_task_context_nr(ctxn) {
3187 ctx = task->perf_event_ctxp[ctxn];
3188 if (likely(!ctx))
3189 continue;
3190
Stephane Eraniane5d13672011-02-14 11:20:01 +02003191 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003192 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003193
Adrian Hunter45ac1402015-07-21 12:44:02 +03003194 if (atomic_read(&nr_switch_events))
3195 perf_event_switch(task, prev, true);
3196
Yan, Zhengba532502014-11-04 21:55:58 -05003197 if (__this_cpu_read(perf_sched_cb_usages))
3198 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199}
3200
Peter Zijlstraabd50712010-01-26 18:50:16 +01003201static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3202{
3203 u64 frequency = event->attr.sample_freq;
3204 u64 sec = NSEC_PER_SEC;
3205 u64 divisor, dividend;
3206
3207 int count_fls, nsec_fls, frequency_fls, sec_fls;
3208
3209 count_fls = fls64(count);
3210 nsec_fls = fls64(nsec);
3211 frequency_fls = fls64(frequency);
3212 sec_fls = 30;
3213
3214 /*
3215 * We got @count in @nsec, with a target of sample_freq HZ
3216 * the target period becomes:
3217 *
3218 * @count * 10^9
3219 * period = -------------------
3220 * @nsec * sample_freq
3221 *
3222 */
3223
3224 /*
3225 * Reduce accuracy by one bit such that @a and @b converge
3226 * to a similar magnitude.
3227 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003228#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003229do { \
3230 if (a##_fls > b##_fls) { \
3231 a >>= 1; \
3232 a##_fls--; \
3233 } else { \
3234 b >>= 1; \
3235 b##_fls--; \
3236 } \
3237} while (0)
3238
3239 /*
3240 * Reduce accuracy until either term fits in a u64, then proceed with
3241 * the other, so that finally we can do a u64/u64 division.
3242 */
3243 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3244 REDUCE_FLS(nsec, frequency);
3245 REDUCE_FLS(sec, count);
3246 }
3247
3248 if (count_fls + sec_fls > 64) {
3249 divisor = nsec * frequency;
3250
3251 while (count_fls + sec_fls > 64) {
3252 REDUCE_FLS(count, sec);
3253 divisor >>= 1;
3254 }
3255
3256 dividend = count * sec;
3257 } else {
3258 dividend = count * sec;
3259
3260 while (nsec_fls + frequency_fls > 64) {
3261 REDUCE_FLS(nsec, frequency);
3262 dividend >>= 1;
3263 }
3264
3265 divisor = nsec * frequency;
3266 }
3267
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003268 if (!divisor)
3269 return dividend;
3270
Peter Zijlstraabd50712010-01-26 18:50:16 +01003271 return div64_u64(dividend, divisor);
3272}
3273
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003274static DEFINE_PER_CPU(int, perf_throttled_count);
3275static DEFINE_PER_CPU(u64, perf_throttled_seq);
3276
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003277static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003278{
3279 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003280 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003281 s64 delta;
3282
Peter Zijlstraabd50712010-01-26 18:50:16 +01003283 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003284
3285 delta = (s64)(period - hwc->sample_period);
3286 delta = (delta + 7) / 8; /* low pass filter */
3287
3288 sample_period = hwc->sample_period + delta;
3289
3290 if (!sample_period)
3291 sample_period = 1;
3292
3293 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003294
Peter Zijlstrae7850592010-05-21 14:43:08 +02003295 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003296 if (disable)
3297 event->pmu->stop(event, PERF_EF_UPDATE);
3298
Peter Zijlstrae7850592010-05-21 14:43:08 +02003299 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003300
3301 if (disable)
3302 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003303 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003304}
3305
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003306/*
3307 * combine freq adjustment with unthrottling to avoid two passes over the
3308 * events. At the same time, make sure, having freq events does not change
3309 * the rate of unthrottling as that would introduce bias.
3310 */
3311static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3312 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003313{
3314 struct perf_event *event;
3315 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003316 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003317 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003318
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003319 /*
3320 * only need to iterate over all events iff:
3321 * - context have events in frequency mode (needs freq adjust)
3322 * - there are events to unthrottle on this cpu
3323 */
3324 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003325 return;
3326
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003327 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003328 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003329
Paul Mackerras03541f82009-10-14 16:58:03 +11003330 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003331 if (event->state != PERF_EVENT_STATE_ACTIVE)
3332 continue;
3333
Stephane Eranian5632ab12011-01-03 18:20:01 +02003334 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003335 continue;
3336
Alexander Shishkin44377272013-12-16 14:17:36 +02003337 perf_pmu_disable(event->pmu);
3338
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003339 hwc = &event->hw;
3340
Jiri Olsaae23bff2013-08-24 16:45:54 +02003341 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003342 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003343 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003344 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003345 }
3346
3347 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003348 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003349
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003350 /*
3351 * stop the event and update event->count
3352 */
3353 event->pmu->stop(event, PERF_EF_UPDATE);
3354
Peter Zijlstrae7850592010-05-21 14:43:08 +02003355 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003356 delta = now - hwc->freq_count_stamp;
3357 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003358
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003359 /*
3360 * restart the event
3361 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003362 * we have stopped the event so tell that
3363 * to perf_adjust_period() to avoid stopping it
3364 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003365 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003366 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003367 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003368
3369 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003370 next:
3371 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003372 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003373
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003374 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003375 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003376}
3377
3378/*
3379 * Round-robin a context's events:
3380 */
3381static void rotate_ctx(struct perf_event_context *ctx)
3382{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003383 /*
3384 * Rotate the first entry last of non-pinned groups. Rotation might be
3385 * disabled by the inheritance code.
3386 */
3387 if (!ctx->rotate_disable)
3388 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389}
3390
Stephane Eranian9e630202013-04-03 14:21:33 +02003391static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003392{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003393 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003394 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003396 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003397 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3398 rotate = 1;
3399 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003400
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003401 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003402 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003403 if (ctx->nr_events != ctx->nr_active)
3404 rotate = 1;
3405 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003406
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003407 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003408 goto done;
3409
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003410 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003411 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003412
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003413 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3414 if (ctx)
3415 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003416
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003417 rotate_ctx(&cpuctx->ctx);
3418 if (ctx)
3419 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003420
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003421 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003422
3423 perf_pmu_enable(cpuctx->ctx.pmu);
3424 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003425done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003426
3427 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003428}
3429
3430void perf_event_task_tick(void)
3431{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003432 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3433 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003434 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003435
3436 WARN_ON(!irqs_disabled());
3437
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003438 __this_cpu_inc(perf_throttled_seq);
3439 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003440 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003441
Mark Rutland2fde4f92015-01-07 15:01:54 +00003442 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003443 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003444}
3445
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003446static int event_enable_on_exec(struct perf_event *event,
3447 struct perf_event_context *ctx)
3448{
3449 if (!event->attr.enable_on_exec)
3450 return 0;
3451
3452 event->attr.enable_on_exec = 0;
3453 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3454 return 0;
3455
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003456 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003457
3458 return 1;
3459}
3460
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003461/*
3462 * Enable all of a task's events that have been marked enable-on-exec.
3463 * This expects task == current.
3464 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003465static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003466{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003467 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003468 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003469 struct perf_event *event;
3470 unsigned long flags;
3471 int enabled = 0;
3472
3473 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003474 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003475 if (!ctx || !ctx->nr_events)
3476 goto out;
3477
Peter Zijlstra3e349502016-01-08 10:01:18 +01003478 cpuctx = __get_cpu_context(ctx);
3479 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003480 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003481 list_for_each_entry(event, &ctx->event_list, event_entry)
3482 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003483
3484 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003485 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003486 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003487 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003488 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003489 ctx_resched(cpuctx, ctx);
3490 }
3491 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003492
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003493out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003494 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003495
3496 if (clone_ctx)
3497 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003498}
3499
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003500struct perf_read_data {
3501 struct perf_event *event;
3502 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003503 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003504};
3505
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003506static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003507{
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003508 u16 local_pkg, event_pkg;
3509
3510 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003511 int local_cpu = smp_processor_id();
3512
3513 event_pkg = topology_physical_package_id(event_cpu);
3514 local_pkg = topology_physical_package_id(local_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003515
3516 if (event_pkg == local_pkg)
3517 return local_cpu;
3518 }
3519
3520 return event_cpu;
3521}
3522
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003523/*
3524 * Cross CPU call to read the hardware event
3525 */
3526static void __perf_event_read(void *info)
3527{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003528 struct perf_read_data *data = info;
3529 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003530 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003531 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003532 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003533
3534 /*
3535 * If this is a task context, we need to check whether it is
3536 * the current task context of this cpu. If not it has been
3537 * scheduled out before the smp call arrived. In that case
3538 * event->count would have been updated to a recent sample
3539 * when the event was scheduled out.
3540 */
3541 if (ctx->task && cpuctx->task_ctx != ctx)
3542 return;
3543
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003544 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003545 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003546 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003547 update_cgrp_time_from_event(event);
3548 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003549
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003550 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003551 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003552 goto unlock;
3553
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003554 if (!data->group) {
3555 pmu->read(event);
3556 data->ret = 0;
3557 goto unlock;
3558 }
3559
3560 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3561
3562 pmu->read(event);
3563
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003564 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3565 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003566 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3567 /*
3568 * Use sibling's PMU rather than @event's since
3569 * sibling could be on different (eg: software) PMU.
3570 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003571 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003572 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003573 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003574
3575 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003576
3577unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003578 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003579}
3580
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003581static inline u64 perf_event_count(struct perf_event *event)
3582{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003583 if (event->pmu->count)
3584 return event->pmu->count(event);
3585
3586 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003587}
3588
Kaixu Xiaffe86902015-08-06 07:02:32 +00003589/*
3590 * NMI-safe method to read a local event, that is an event that
3591 * is:
3592 * - either for the current task, or for this CPU
3593 * - does not have inherit set, for inherited task events
3594 * will not be local and we cannot read them atomically
3595 * - must not have a pmu::count method
3596 */
3597u64 perf_event_read_local(struct perf_event *event)
3598{
3599 unsigned long flags;
3600 u64 val;
3601
3602 /*
3603 * Disabling interrupts avoids all counter scheduling (context
3604 * switches, timer based rotation and IPIs).
3605 */
3606 local_irq_save(flags);
3607
3608 /* If this is a per-task event, it must be for current */
3609 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3610 event->hw.target != current);
3611
3612 /* If this is a per-CPU event, it must be for this CPU */
3613 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3614 event->cpu != smp_processor_id());
3615
3616 /*
3617 * It must not be an event with inherit set, we cannot read
3618 * all child counters from atomic context.
3619 */
3620 WARN_ON_ONCE(event->attr.inherit);
3621
3622 /*
3623 * It must not have a pmu::count method, those are not
3624 * NMI safe.
3625 */
3626 WARN_ON_ONCE(event->pmu->count);
3627
3628 /*
3629 * If the event is currently on this CPU, its either a per-task event,
3630 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3631 * oncpu == -1).
3632 */
3633 if (event->oncpu == smp_processor_id())
3634 event->pmu->read(event);
3635
3636 val = local64_read(&event->count);
3637 local_irq_restore(flags);
3638
3639 return val;
3640}
3641
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003642static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003643{
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003644 int event_cpu, ret = 0;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003645
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003646 /*
3647 * If event is enabled and currently active on a CPU, update the
3648 * value in the event structure:
3649 */
3650 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003651 struct perf_read_data data = {
3652 .event = event,
3653 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003654 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003655 };
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003656
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003657 event_cpu = READ_ONCE(event->oncpu);
3658 if ((unsigned)event_cpu >= nr_cpu_ids)
3659 return 0;
3660
3661 preempt_disable();
3662 event_cpu = __perf_event_read_cpu(event, event_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003663
Peter Zijlstra58763142016-08-30 10:15:03 +02003664 /*
3665 * Purposely ignore the smp_call_function_single() return
3666 * value.
3667 *
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003668 * If event_cpu isn't a valid CPU it means the event got
Peter Zijlstra58763142016-08-30 10:15:03 +02003669 * scheduled out and that will have updated the event count.
3670 *
3671 * Therefore, either way, we'll have an up-to-date event count
3672 * after this.
3673 */
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003674 (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1);
3675 preempt_enable();
Peter Zijlstra58763142016-08-30 10:15:03 +02003676 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003677 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003678 struct perf_event_context *ctx = event->ctx;
3679 unsigned long flags;
3680
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003681 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003682 /*
3683 * may read while context is not active
3684 * (e.g., thread is blocked), in that case
3685 * we cannot update context time
3686 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003687 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003688 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003689 update_cgrp_time_from_event(event);
3690 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003691 if (group)
3692 update_group_times(event);
3693 else
3694 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003695 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003696 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003697
3698 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003699}
3700
3701/*
3702 * Initialize the perf_event context in a task_struct:
3703 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003704static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003705{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003706 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003707 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003708 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003709 INIT_LIST_HEAD(&ctx->pinned_groups);
3710 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003711 INIT_LIST_HEAD(&ctx->event_list);
3712 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003713}
3714
Peter Zijlstraeb184472010-09-07 15:55:13 +02003715static struct perf_event_context *
3716alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717{
3718 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003719
3720 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3721 if (!ctx)
3722 return NULL;
3723
3724 __perf_event_init_context(ctx);
3725 if (task) {
3726 ctx->task = task;
3727 get_task_struct(task);
3728 }
3729 ctx->pmu = pmu;
3730
3731 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003732}
3733
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003734static struct task_struct *
3735find_lively_task_by_vpid(pid_t vpid)
3736{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003737 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003739 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003740 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003741 task = current;
3742 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003743 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003744 if (task)
3745 get_task_struct(task);
3746 rcu_read_unlock();
3747
3748 if (!task)
3749 return ERR_PTR(-ESRCH);
3750
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003751 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003752}
3753
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003754/*
3755 * Returns a matching context with refcount and pincount.
3756 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003757static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003758find_get_context(struct pmu *pmu, struct task_struct *task,
3759 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003760{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003761 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003762 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003763 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003764 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003765 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003766 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003767
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003768 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003769 /* Must be root to operate on a CPU event: */
3770 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3771 return ERR_PTR(-EACCES);
3772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003773 /*
3774 * We could be clever and allow to attach a event to an
3775 * offline CPU and activate it when the CPU comes up, but
3776 * that's for later.
3777 */
3778 if (!cpu_online(cpu))
3779 return ERR_PTR(-ENODEV);
3780
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003781 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003782 ctx = &cpuctx->ctx;
3783 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003784 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003785
3786 return ctx;
3787 }
3788
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003789 err = -EINVAL;
3790 ctxn = pmu->task_ctx_nr;
3791 if (ctxn < 0)
3792 goto errout;
3793
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003794 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3795 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3796 if (!task_ctx_data) {
3797 err = -ENOMEM;
3798 goto errout;
3799 }
3800 }
3801
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003802retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003803 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003804 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003805 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003806 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003807
3808 if (task_ctx_data && !ctx->task_ctx_data) {
3809 ctx->task_ctx_data = task_ctx_data;
3810 task_ctx_data = NULL;
3811 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003812 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003813
3814 if (clone_ctx)
3815 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003816 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003817 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003818 err = -ENOMEM;
3819 if (!ctx)
3820 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003821
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003822 if (task_ctx_data) {
3823 ctx->task_ctx_data = task_ctx_data;
3824 task_ctx_data = NULL;
3825 }
3826
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003827 err = 0;
3828 mutex_lock(&task->perf_event_mutex);
3829 /*
3830 * If it has already passed perf_event_exit_task().
3831 * we must see PF_EXITING, it takes this mutex too.
3832 */
3833 if (task->flags & PF_EXITING)
3834 err = -ESRCH;
3835 else if (task->perf_event_ctxp[ctxn])
3836 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003837 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003838 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003839 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003840 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003841 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003842 mutex_unlock(&task->perf_event_mutex);
3843
3844 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003845 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003846
3847 if (err == -EAGAIN)
3848 goto retry;
3849 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003850 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003851 }
3852
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003853 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003854 return ctx;
3855
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003856errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003857 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003858 return ERR_PTR(err);
3859}
3860
Li Zefan6fb29152009-10-15 11:21:42 +08003861static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003862static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003863
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003864static void free_event_rcu(struct rcu_head *head)
3865{
3866 struct perf_event *event;
3867
3868 event = container_of(head, struct perf_event, rcu_head);
3869 if (event->ns)
3870 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003871 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003872 kfree(event);
3873}
3874
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003875static void ring_buffer_attach(struct perf_event *event,
3876 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003877
Kan Liangf2fb6be2016-03-23 11:24:37 -07003878static void detach_sb_event(struct perf_event *event)
3879{
3880 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
3881
3882 raw_spin_lock(&pel->lock);
3883 list_del_rcu(&event->sb_list);
3884 raw_spin_unlock(&pel->lock);
3885}
3886
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003887static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07003888{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003889 struct perf_event_attr *attr = &event->attr;
3890
Kan Liangf2fb6be2016-03-23 11:24:37 -07003891 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003892 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003893
3894 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003895 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003896
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003897 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
3898 attr->comm || attr->comm_exec ||
3899 attr->task ||
3900 attr->context_switch)
3901 return true;
3902 return false;
3903}
3904
3905static void unaccount_pmu_sb_event(struct perf_event *event)
3906{
3907 if (is_sb_event(event))
3908 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003909}
3910
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003911static void unaccount_event_cpu(struct perf_event *event, int cpu)
3912{
3913 if (event->parent)
3914 return;
3915
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003916 if (is_cgroup_event(event))
3917 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3918}
3919
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003920#ifdef CONFIG_NO_HZ_FULL
3921static DEFINE_SPINLOCK(nr_freq_lock);
3922#endif
3923
3924static void unaccount_freq_event_nohz(void)
3925{
3926#ifdef CONFIG_NO_HZ_FULL
3927 spin_lock(&nr_freq_lock);
3928 if (atomic_dec_and_test(&nr_freq_events))
3929 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3930 spin_unlock(&nr_freq_lock);
3931#endif
3932}
3933
3934static void unaccount_freq_event(void)
3935{
3936 if (tick_nohz_full_enabled())
3937 unaccount_freq_event_nohz();
3938 else
3939 atomic_dec(&nr_freq_events);
3940}
3941
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003942static void unaccount_event(struct perf_event *event)
3943{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003944 bool dec = false;
3945
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003946 if (event->parent)
3947 return;
3948
3949 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003950 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003951 if (event->attr.mmap || event->attr.mmap_data)
3952 atomic_dec(&nr_mmap_events);
3953 if (event->attr.comm)
3954 atomic_dec(&nr_comm_events);
3955 if (event->attr.task)
3956 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003957 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003958 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003959 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003960 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003961 atomic_dec(&nr_switch_events);
3962 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003963 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003964 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003965 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003966 dec = true;
3967
Peter Zijlstra9107c892016-02-24 18:45:45 +01003968 if (dec) {
3969 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3970 schedule_delayed_work(&perf_sched_work, HZ);
3971 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003972
3973 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003974
3975 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003976}
3977
Peter Zijlstra9107c892016-02-24 18:45:45 +01003978static void perf_sched_delayed(struct work_struct *work)
3979{
3980 mutex_lock(&perf_sched_mutex);
3981 if (atomic_dec_and_test(&perf_sched_count))
3982 static_branch_disable(&perf_sched_events);
3983 mutex_unlock(&perf_sched_mutex);
3984}
3985
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003986/*
3987 * The following implement mutual exclusion of events on "exclusive" pmus
3988 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3989 * at a time, so we disallow creating events that might conflict, namely:
3990 *
3991 * 1) cpu-wide events in the presence of per-task events,
3992 * 2) per-task events in the presence of cpu-wide events,
3993 * 3) two matching events on the same context.
3994 *
3995 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003996 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003997 */
3998static int exclusive_event_init(struct perf_event *event)
3999{
4000 struct pmu *pmu = event->pmu;
4001
4002 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4003 return 0;
4004
4005 /*
4006 * Prevent co-existence of per-task and cpu-wide events on the
4007 * same exclusive pmu.
4008 *
4009 * Negative pmu::exclusive_cnt means there are cpu-wide
4010 * events on this "exclusive" pmu, positive means there are
4011 * per-task events.
4012 *
4013 * Since this is called in perf_event_alloc() path, event::ctx
4014 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
4015 * to mean "per-task event", because unlike other attach states it
4016 * never gets cleared.
4017 */
4018 if (event->attach_state & PERF_ATTACH_TASK) {
4019 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
4020 return -EBUSY;
4021 } else {
4022 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
4023 return -EBUSY;
4024 }
4025
4026 return 0;
4027}
4028
4029static void exclusive_event_destroy(struct perf_event *event)
4030{
4031 struct pmu *pmu = event->pmu;
4032
4033 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4034 return;
4035
4036 /* see comment in exclusive_event_init() */
4037 if (event->attach_state & PERF_ATTACH_TASK)
4038 atomic_dec(&pmu->exclusive_cnt);
4039 else
4040 atomic_inc(&pmu->exclusive_cnt);
4041}
4042
4043static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
4044{
Alexander Shishkin3bf62152016-09-20 18:48:11 +03004045 if ((e1->pmu == e2->pmu) &&
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004046 (e1->cpu == e2->cpu ||
4047 e1->cpu == -1 ||
4048 e2->cpu == -1))
4049 return true;
4050 return false;
4051}
4052
4053/* Called under the same ctx::mutex as perf_install_in_context() */
4054static bool exclusive_event_installable(struct perf_event *event,
4055 struct perf_event_context *ctx)
4056{
4057 struct perf_event *iter_event;
4058 struct pmu *pmu = event->pmu;
4059
4060 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4061 return true;
4062
4063 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
4064 if (exclusive_event_match(iter_event, event))
4065 return false;
4066 }
4067
4068 return true;
4069}
4070
Alexander Shishkin375637b2016-04-27 18:44:46 +03004071static void perf_addr_filters_splice(struct perf_event *event,
4072 struct list_head *head);
4073
Peter Zijlstra683ede42014-05-05 12:11:24 +02004074static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004075{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004076 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004077
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004078 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079
Frederic Weisbecker76369132011-05-19 19:55:04 +02004080 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004081 /*
4082 * Can happen when we close an event with re-directed output.
4083 *
4084 * Since we have a 0 refcount, perf_mmap_close() will skip
4085 * over us; possibly making our ring_buffer_put() the last.
4086 */
4087 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004088 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004089 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004090 }
4091
Stephane Eraniane5d13672011-02-14 11:20:01 +02004092 if (is_cgroup_event(event))
4093 perf_detach_cgroup(event);
4094
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004095 if (!event->parent) {
4096 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
4097 put_callchain_buffers();
4098 }
4099
4100 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03004101 perf_addr_filters_splice(event, NULL);
4102 kfree(event->addr_filters_offs);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004103
4104 if (event->destroy)
4105 event->destroy(event);
4106
4107 if (event->ctx)
4108 put_ctx(event->ctx);
4109
Prashant Bholeb951ffb2018-04-09 19:03:46 +09004110 if (event->hw.target)
4111 put_task_struct(event->hw.target);
4112
Alexander Shishkin62a92c82016-06-07 15:44:15 +03004113 exclusive_event_destroy(event);
4114 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004115
4116 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117}
4118
Peter Zijlstra683ede42014-05-05 12:11:24 +02004119/*
4120 * Used to free events which have a known refcount of 1, such as in error paths
4121 * where the event isn't exposed yet and inherited events.
4122 */
4123static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004124{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004125 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4126 "unexpected event refcount: %ld; ptr=%p\n",
4127 atomic_long_read(&event->refcount), event)) {
4128 /* leak to avoid use-after-free */
4129 return;
4130 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004131
Peter Zijlstra683ede42014-05-05 12:11:24 +02004132 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004133}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004134
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004135/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004136 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004137 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004138static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004139{
Peter Zijlstra88821352010-11-09 19:01:43 +01004140 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004141
Peter Zijlstra88821352010-11-09 19:01:43 +01004142 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004143 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004144 * Matches the smp_store_release() in perf_event_exit_task(). If we
4145 * observe !owner it means the list deletion is complete and we can
4146 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004147 * owner->perf_event_mutex.
4148 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004149 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004150 if (owner) {
4151 /*
4152 * Since delayed_put_task_struct() also drops the last
4153 * task reference we can safely take a new reference
4154 * while holding the rcu_read_lock().
4155 */
4156 get_task_struct(owner);
4157 }
4158 rcu_read_unlock();
4159
4160 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004161 /*
4162 * If we're here through perf_event_exit_task() we're already
4163 * holding ctx->mutex which would be an inversion wrt. the
4164 * normal lock order.
4165 *
4166 * However we can safely take this lock because its the child
4167 * ctx->mutex.
4168 */
4169 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4170
Peter Zijlstra88821352010-11-09 19:01:43 +01004171 /*
4172 * We have to re-check the event->owner field, if it is cleared
4173 * we raced with perf_event_exit_task(), acquiring the mutex
4174 * ensured they're done, and we can proceed with freeing the
4175 * event.
4176 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004177 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004178 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004179 smp_store_release(&event->owner, NULL);
4180 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004181 mutex_unlock(&owner->perf_event_mutex);
4182 put_task_struct(owner);
4183 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004184}
4185
Jiri Olsaf8697762014-08-01 14:33:01 +02004186static void put_event(struct perf_event *event)
4187{
Jiri Olsaf8697762014-08-01 14:33:01 +02004188 if (!atomic_long_dec_and_test(&event->refcount))
4189 return;
4190
Peter Zijlstra683ede42014-05-05 12:11:24 +02004191 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004192}
4193
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004194/*
4195 * Kill an event dead; while event:refcount will preserve the event
4196 * object, it will not preserve its functionality. Once the last 'user'
4197 * gives up the object, we'll destroy the thing.
4198 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004199int perf_event_release_kernel(struct perf_event *event)
4200{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004201 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004202 struct perf_event *child, *tmp;
4203
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004204 /*
4205 * If we got here through err_file: fput(event_file); we will not have
4206 * attached to a context yet.
4207 */
4208 if (!ctx) {
4209 WARN_ON_ONCE(event->attach_state &
4210 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4211 goto no_ctx;
4212 }
4213
Peter Zijlstra88821352010-11-09 19:01:43 +01004214 if (!is_kernel_event(event))
4215 perf_remove_from_owner(event);
4216
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004217 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004218 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004219 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004220
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004221 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004222 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004223 * Mark this even as STATE_DEAD, there is no external reference to it
4224 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004225 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004226 * Anybody acquiring event->child_mutex after the below loop _must_
4227 * also see this, most importantly inherit_event() which will avoid
4228 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004229 *
4230 * Thus this guarantees that we will in fact observe and kill _ALL_
4231 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004232 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004233 event->state = PERF_EVENT_STATE_DEAD;
4234 raw_spin_unlock_irq(&ctx->lock);
4235
4236 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004237
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004238again:
4239 mutex_lock(&event->child_mutex);
4240 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004241
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004242 /*
4243 * Cannot change, child events are not migrated, see the
4244 * comment with perf_event_ctx_lock_nested().
4245 */
4246 ctx = lockless_dereference(child->ctx);
4247 /*
4248 * Since child_mutex nests inside ctx::mutex, we must jump
4249 * through hoops. We start by grabbing a reference on the ctx.
4250 *
4251 * Since the event cannot get freed while we hold the
4252 * child_mutex, the context must also exist and have a !0
4253 * reference count.
4254 */
4255 get_ctx(ctx);
4256
4257 /*
4258 * Now that we have a ctx ref, we can drop child_mutex, and
4259 * acquire ctx::mutex without fear of it going away. Then we
4260 * can re-acquire child_mutex.
4261 */
4262 mutex_unlock(&event->child_mutex);
4263 mutex_lock(&ctx->mutex);
4264 mutex_lock(&event->child_mutex);
4265
4266 /*
4267 * Now that we hold ctx::mutex and child_mutex, revalidate our
4268 * state, if child is still the first entry, it didn't get freed
4269 * and we can continue doing so.
4270 */
4271 tmp = list_first_entry_or_null(&event->child_list,
4272 struct perf_event, child_list);
4273 if (tmp == child) {
4274 perf_remove_from_context(child, DETACH_GROUP);
4275 list_del(&child->child_list);
4276 free_event(child);
4277 /*
4278 * This matches the refcount bump in inherit_event();
4279 * this can't be the last reference.
4280 */
4281 put_event(event);
4282 }
4283
4284 mutex_unlock(&event->child_mutex);
4285 mutex_unlock(&ctx->mutex);
4286 put_ctx(ctx);
4287 goto again;
4288 }
4289 mutex_unlock(&event->child_mutex);
4290
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004291no_ctx:
4292 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004293 return 0;
4294}
4295EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4296
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004297/*
4298 * Called when the last reference to the file is gone.
4299 */
Al Viroa6fa9412012-08-20 14:59:25 +01004300static int perf_release(struct inode *inode, struct file *file)
4301{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004302 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004303 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004304}
4305
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004306u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004307{
4308 struct perf_event *child;
4309 u64 total = 0;
4310
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004311 *enabled = 0;
4312 *running = 0;
4313
Peter Zijlstra6f105812009-11-20 22:19:56 +01004314 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004315
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004316 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004317 total += perf_event_count(event);
4318
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004319 *enabled += event->total_time_enabled +
4320 atomic64_read(&event->child_total_time_enabled);
4321 *running += event->total_time_running +
4322 atomic64_read(&event->child_total_time_running);
4323
4324 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004325 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004326 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004327 *enabled += child->total_time_enabled;
4328 *running += child->total_time_running;
4329 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004330 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004331
4332 return total;
4333}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004334EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004335
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004336static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004337 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004338{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004339 struct perf_event *sub;
4340 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004341 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004342
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004343 ret = perf_event_read(leader, true);
4344 if (ret)
4345 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004346
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004347 /*
4348 * Since we co-schedule groups, {enabled,running} times of siblings
4349 * will be identical to those of the leader, so we only publish one
4350 * set.
4351 */
4352 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4353 values[n++] += leader->total_time_enabled +
4354 atomic64_read(&leader->child_total_time_enabled);
4355 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004356
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004357 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4358 values[n++] += leader->total_time_running +
4359 atomic64_read(&leader->child_total_time_running);
4360 }
4361
4362 /*
4363 * Write {count,id} tuples for every sibling.
4364 */
4365 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004366 if (read_format & PERF_FORMAT_ID)
4367 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004369 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004370 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004371 if (read_format & PERF_FORMAT_ID)
4372 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004373 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004374
4375 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004376}
4377
4378static int perf_read_group(struct perf_event *event,
4379 u64 read_format, char __user *buf)
4380{
4381 struct perf_event *leader = event->group_leader, *child;
4382 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004383 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004384 u64 *values;
4385
4386 lockdep_assert_held(&ctx->mutex);
4387
4388 values = kzalloc(event->read_size, GFP_KERNEL);
4389 if (!values)
4390 return -ENOMEM;
4391
4392 values[0] = 1 + leader->nr_siblings;
4393
4394 /*
4395 * By locking the child_mutex of the leader we effectively
4396 * lock the child list of all siblings.. XXX explain how.
4397 */
4398 mutex_lock(&leader->child_mutex);
4399
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004400 ret = __perf_read_group_add(leader, read_format, values);
4401 if (ret)
4402 goto unlock;
4403
4404 list_for_each_entry(child, &leader->child_list, child_list) {
4405 ret = __perf_read_group_add(child, read_format, values);
4406 if (ret)
4407 goto unlock;
4408 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004409
4410 mutex_unlock(&leader->child_mutex);
4411
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004412 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004413 if (copy_to_user(buf, values, event->read_size))
4414 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004415 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004416
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004417unlock:
4418 mutex_unlock(&leader->child_mutex);
4419out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004420 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004421 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004422}
4423
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004424static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004425 u64 read_format, char __user *buf)
4426{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004427 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004428 u64 values[4];
4429 int n = 0;
4430
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004431 values[n++] = perf_event_read_value(event, &enabled, &running);
4432 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4433 values[n++] = enabled;
4434 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4435 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004436 if (read_format & PERF_FORMAT_ID)
4437 values[n++] = primary_event_id(event);
4438
4439 if (copy_to_user(buf, values, n * sizeof(u64)))
4440 return -EFAULT;
4441
4442 return n * sizeof(u64);
4443}
4444
Jiri Olsadc633982014-09-12 13:18:26 +02004445static bool is_event_hup(struct perf_event *event)
4446{
4447 bool no_children;
4448
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004449 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004450 return false;
4451
4452 mutex_lock(&event->child_mutex);
4453 no_children = list_empty(&event->child_list);
4454 mutex_unlock(&event->child_mutex);
4455 return no_children;
4456}
4457
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004458/*
4459 * Read the performance event - simple non blocking version for now
4460 */
4461static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004462__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463{
4464 u64 read_format = event->attr.read_format;
4465 int ret;
4466
4467 /*
4468 * Return end-of-file for a read on a event that is in
4469 * error state (i.e. because it was pinned but it couldn't be
4470 * scheduled on to the CPU at some point).
4471 */
4472 if (event->state == PERF_EVENT_STATE_ERROR)
4473 return 0;
4474
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004475 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004476 return -ENOSPC;
4477
4478 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004479 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004480 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004481 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004482 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004483
4484 return ret;
4485}
4486
4487static ssize_t
4488perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4489{
4490 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004491 struct perf_event_context *ctx;
4492 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004493
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004494 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004495 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004496 perf_event_ctx_unlock(event, ctx);
4497
4498 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004499}
4500
4501static unsigned int perf_poll(struct file *file, poll_table *wait)
4502{
4503 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004504 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004505 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004507 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004508
Jiri Olsadc633982014-09-12 13:18:26 +02004509 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004510 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004512 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004513 * Pin the event->rb by taking event->mmap_mutex; otherwise
4514 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004515 */
4516 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004517 rb = event->rb;
4518 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004519 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004520 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004521 return events;
4522}
4523
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004524static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004525{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004526 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004527 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004528 perf_event_update_userpage(event);
4529}
4530
4531/*
4532 * Holding the top-level event's child_mutex means that any
4533 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004534 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004535 * task existence requirements of perf_event_enable/disable.
4536 */
4537static void perf_event_for_each_child(struct perf_event *event,
4538 void (*func)(struct perf_event *))
4539{
4540 struct perf_event *child;
4541
4542 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004543
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004544 mutex_lock(&event->child_mutex);
4545 func(event);
4546 list_for_each_entry(child, &event->child_list, child_list)
4547 func(child);
4548 mutex_unlock(&event->child_mutex);
4549}
4550
4551static void perf_event_for_each(struct perf_event *event,
4552 void (*func)(struct perf_event *))
4553{
4554 struct perf_event_context *ctx = event->ctx;
4555 struct perf_event *sibling;
4556
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004557 lockdep_assert_held(&ctx->mutex);
4558
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004559 event = event->group_leader;
4560
4561 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004562 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004563 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004564}
4565
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004566static void __perf_event_period(struct perf_event *event,
4567 struct perf_cpu_context *cpuctx,
4568 struct perf_event_context *ctx,
4569 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004570{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004571 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004572 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004573
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004574 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004575 event->attr.sample_freq = value;
4576 } else {
4577 event->attr.sample_period = value;
4578 event->hw.sample_period = value;
4579 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004580
4581 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4582 if (active) {
4583 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004584 /*
4585 * We could be throttled; unthrottle now to avoid the tick
4586 * trying to unthrottle while we already re-started the event.
4587 */
4588 if (event->hw.interrupts == MAX_INTERRUPTS) {
4589 event->hw.interrupts = 0;
4590 perf_log_throttle(event, 1);
4591 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004592 event->pmu->stop(event, PERF_EF_UPDATE);
4593 }
4594
4595 local64_set(&event->hw.period_left, 0);
4596
4597 if (active) {
4598 event->pmu->start(event, PERF_EF_RELOAD);
4599 perf_pmu_enable(ctx->pmu);
4600 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004601}
4602
4603static int perf_event_period(struct perf_event *event, u64 __user *arg)
4604{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004605 u64 value;
4606
4607 if (!is_sampling_event(event))
4608 return -EINVAL;
4609
4610 if (copy_from_user(&value, arg, sizeof(value)))
4611 return -EFAULT;
4612
4613 if (!value)
4614 return -EINVAL;
4615
4616 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4617 return -EINVAL;
4618
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004619 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004620
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004621 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004622}
4623
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004624static const struct file_operations perf_fops;
4625
Al Viro2903ff02012-08-28 12:52:22 -04004626static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004627{
Al Viro2903ff02012-08-28 12:52:22 -04004628 struct fd f = fdget(fd);
4629 if (!f.file)
4630 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004631
Al Viro2903ff02012-08-28 12:52:22 -04004632 if (f.file->f_op != &perf_fops) {
4633 fdput(f);
4634 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004635 }
Al Viro2903ff02012-08-28 12:52:22 -04004636 *p = f;
4637 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004638}
4639
4640static int perf_event_set_output(struct perf_event *event,
4641 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004642static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004643static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004644
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004645static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004646{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004647 void (*func)(struct perf_event *);
4648 u32 flags = arg;
4649
4650 switch (cmd) {
4651 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004652 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004653 break;
4654 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004655 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004656 break;
4657 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004658 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004659 break;
4660
4661 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004662 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004663
4664 case PERF_EVENT_IOC_PERIOD:
4665 return perf_event_period(event, (u64 __user *)arg);
4666
Jiri Olsacf4957f2012-10-24 13:37:58 +02004667 case PERF_EVENT_IOC_ID:
4668 {
4669 u64 id = primary_event_id(event);
4670
4671 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4672 return -EFAULT;
4673 return 0;
4674 }
4675
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004676 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004677 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004678 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004679 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004680 struct perf_event *output_event;
4681 struct fd output;
4682 ret = perf_fget_light(arg, &output);
4683 if (ret)
4684 return ret;
4685 output_event = output.file->private_data;
4686 ret = perf_event_set_output(event, output_event);
4687 fdput(output);
4688 } else {
4689 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004690 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004691 return ret;
4692 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004693
Li Zefan6fb29152009-10-15 11:21:42 +08004694 case PERF_EVENT_IOC_SET_FILTER:
4695 return perf_event_set_filter(event, (void __user *)arg);
4696
Alexei Starovoitov25415172015-03-25 12:49:20 -07004697 case PERF_EVENT_IOC_SET_BPF:
4698 return perf_event_set_bpf_prog(event, arg);
4699
Wang Nan86e79722016-03-28 06:41:29 +00004700 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4701 struct ring_buffer *rb;
4702
4703 rcu_read_lock();
4704 rb = rcu_dereference(event->rb);
4705 if (!rb || !rb->nr_pages) {
4706 rcu_read_unlock();
4707 return -EINVAL;
4708 }
4709 rb_toggle_paused(rb, !!arg);
4710 rcu_read_unlock();
4711 return 0;
4712 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004713 default:
4714 return -ENOTTY;
4715 }
4716
4717 if (flags & PERF_IOC_FLAG_GROUP)
4718 perf_event_for_each(event, func);
4719 else
4720 perf_event_for_each_child(event, func);
4721
4722 return 0;
4723}
4724
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004725static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4726{
4727 struct perf_event *event = file->private_data;
4728 struct perf_event_context *ctx;
4729 long ret;
4730
4731 ctx = perf_event_ctx_lock(event);
4732 ret = _perf_ioctl(event, cmd, arg);
4733 perf_event_ctx_unlock(event, ctx);
4734
4735 return ret;
4736}
4737
Pawel Mollb3f20782014-06-13 16:03:32 +01004738#ifdef CONFIG_COMPAT
4739static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4740 unsigned long arg)
4741{
4742 switch (_IOC_NR(cmd)) {
4743 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4744 case _IOC_NR(PERF_EVENT_IOC_ID):
4745 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4746 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4747 cmd &= ~IOCSIZE_MASK;
4748 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4749 }
4750 break;
4751 }
4752 return perf_ioctl(file, cmd, arg);
4753}
4754#else
4755# define perf_compat_ioctl NULL
4756#endif
4757
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004758int perf_event_task_enable(void)
4759{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004760 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004761 struct perf_event *event;
4762
4763 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004764 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4765 ctx = perf_event_ctx_lock(event);
4766 perf_event_for_each_child(event, _perf_event_enable);
4767 perf_event_ctx_unlock(event, ctx);
4768 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004769 mutex_unlock(&current->perf_event_mutex);
4770
4771 return 0;
4772}
4773
4774int perf_event_task_disable(void)
4775{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004776 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004777 struct perf_event *event;
4778
4779 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004780 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4781 ctx = perf_event_ctx_lock(event);
4782 perf_event_for_each_child(event, _perf_event_disable);
4783 perf_event_ctx_unlock(event, ctx);
4784 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004785 mutex_unlock(&current->perf_event_mutex);
4786
4787 return 0;
4788}
4789
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004790static int perf_event_index(struct perf_event *event)
4791{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004792 if (event->hw.state & PERF_HES_STOPPED)
4793 return 0;
4794
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004795 if (event->state != PERF_EVENT_STATE_ACTIVE)
4796 return 0;
4797
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004798 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004799}
4800
Eric B Munsonc4794292011-06-23 16:34:38 -04004801static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004802 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004803 u64 *enabled,
4804 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004805{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004806 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004807
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004808 *now = perf_clock();
4809 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004810 *enabled = ctx_time - event->tstamp_enabled;
4811 *running = ctx_time - event->tstamp_running;
4812}
4813
Peter Zijlstrafa731582013-09-19 10:16:42 +02004814static void perf_event_init_userpage(struct perf_event *event)
4815{
4816 struct perf_event_mmap_page *userpg;
4817 struct ring_buffer *rb;
4818
4819 rcu_read_lock();
4820 rb = rcu_dereference(event->rb);
4821 if (!rb)
4822 goto unlock;
4823
4824 userpg = rb->user_page;
4825
4826 /* Allow new userspace to detect that bit 0 is deprecated */
4827 userpg->cap_bit0_is_deprecated = 1;
4828 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004829 userpg->data_offset = PAGE_SIZE;
4830 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004831
4832unlock:
4833 rcu_read_unlock();
4834}
4835
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004836void __weak arch_perf_update_userpage(
4837 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004838{
4839}
4840
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004841/*
4842 * Callers need to ensure there can be no nesting of this function, otherwise
4843 * the seqlock logic goes bad. We can not serialize this because the arch
4844 * code calls this from NMI context.
4845 */
4846void perf_event_update_userpage(struct perf_event *event)
4847{
4848 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004849 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004850 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004851
4852 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004853 rb = rcu_dereference(event->rb);
4854 if (!rb)
4855 goto unlock;
4856
Eric B Munson0d641202011-06-24 12:26:26 -04004857 /*
4858 * compute total_time_enabled, total_time_running
4859 * based on snapshot values taken when the event
4860 * was last scheduled in.
4861 *
4862 * we cannot simply called update_context_time()
4863 * because of locking issue as we can be called in
4864 * NMI context
4865 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004866 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004867
Frederic Weisbecker76369132011-05-19 19:55:04 +02004868 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004869 /*
4870 * Disable preemption so as to not let the corresponding user-space
4871 * spin too long if we get preempted.
4872 */
4873 preempt_disable();
4874 ++userpg->lock;
4875 barrier();
4876 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004877 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004878 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004879 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004880
Eric B Munson0d641202011-06-24 12:26:26 -04004881 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004882 atomic64_read(&event->child_total_time_enabled);
4883
Eric B Munson0d641202011-06-24 12:26:26 -04004884 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004885 atomic64_read(&event->child_total_time_running);
4886
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004887 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004888
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889 barrier();
4890 ++userpg->lock;
4891 preempt_enable();
4892unlock:
4893 rcu_read_unlock();
4894}
4895
Peter Zijlstra906010b2009-09-21 16:08:49 +02004896static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4897{
4898 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004899 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004900 int ret = VM_FAULT_SIGBUS;
4901
4902 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4903 if (vmf->pgoff == 0)
4904 ret = 0;
4905 return ret;
4906 }
4907
4908 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004909 rb = rcu_dereference(event->rb);
4910 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004911 goto unlock;
4912
4913 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4914 goto unlock;
4915
Frederic Weisbecker76369132011-05-19 19:55:04 +02004916 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004917 if (!vmf->page)
4918 goto unlock;
4919
4920 get_page(vmf->page);
4921 vmf->page->mapping = vma->vm_file->f_mapping;
4922 vmf->page->index = vmf->pgoff;
4923
4924 ret = 0;
4925unlock:
4926 rcu_read_unlock();
4927
4928 return ret;
4929}
4930
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004931static void ring_buffer_attach(struct perf_event *event,
4932 struct ring_buffer *rb)
4933{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004934 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004935 unsigned long flags;
4936
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004937 if (event->rb) {
4938 /*
4939 * Should be impossible, we set this when removing
4940 * event->rb_entry and wait/clear when adding event->rb_entry.
4941 */
4942 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004943
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004944 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004945 spin_lock_irqsave(&old_rb->event_lock, flags);
4946 list_del_rcu(&event->rb_entry);
4947 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004948
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004949 event->rcu_batches = get_state_synchronize_rcu();
4950 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004951 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004952
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004953 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004954 if (event->rcu_pending) {
4955 cond_synchronize_rcu(event->rcu_batches);
4956 event->rcu_pending = 0;
4957 }
4958
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004959 spin_lock_irqsave(&rb->event_lock, flags);
4960 list_add_rcu(&event->rb_entry, &rb->event_list);
4961 spin_unlock_irqrestore(&rb->event_lock, flags);
4962 }
4963
Alexander Shishkin767ae082016-09-06 16:23:49 +03004964 /*
4965 * Avoid racing with perf_mmap_close(AUX): stop the event
4966 * before swizzling the event::rb pointer; if it's getting
4967 * unmapped, its aux_mmap_count will be 0 and it won't
4968 * restart. See the comment in __perf_pmu_output_stop().
4969 *
4970 * Data will inevitably be lost when set_output is done in
4971 * mid-air, but then again, whoever does it like this is
4972 * not in for the data anyway.
4973 */
4974 if (has_aux(event))
4975 perf_event_stop(event, 0);
4976
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004977 rcu_assign_pointer(event->rb, rb);
4978
4979 if (old_rb) {
4980 ring_buffer_put(old_rb);
4981 /*
4982 * Since we detached before setting the new rb, so that we
4983 * could attach the new rb, we could have missed a wakeup.
4984 * Provide it now.
4985 */
4986 wake_up_all(&event->waitq);
4987 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004988}
4989
4990static void ring_buffer_wakeup(struct perf_event *event)
4991{
4992 struct ring_buffer *rb;
4993
4994 rcu_read_lock();
4995 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004996 if (rb) {
4997 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4998 wake_up_all(&event->waitq);
4999 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005000 rcu_read_unlock();
5001}
5002
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005003struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005004{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005005 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005006
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005007 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005008 rb = rcu_dereference(event->rb);
5009 if (rb) {
5010 if (!atomic_inc_not_zero(&rb->refcount))
5011 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005012 }
5013 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005014
Frederic Weisbecker76369132011-05-19 19:55:04 +02005015 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005016}
5017
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005018void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005019{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005020 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005021 return;
5022
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005023 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005024
Frederic Weisbecker76369132011-05-19 19:55:04 +02005025 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005026}
5027
5028static void perf_mmap_open(struct vm_area_struct *vma)
5029{
5030 struct perf_event *event = vma->vm_file->private_data;
5031
5032 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005033 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005034
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005035 if (vma->vm_pgoff)
5036 atomic_inc(&event->rb->aux_mmap_count);
5037
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005038 if (event->pmu->event_mapped)
5039 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005040}
5041
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005042static void perf_pmu_output_stop(struct perf_event *event);
5043
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005044/*
5045 * A buffer can be mmap()ed multiple times; either directly through the same
5046 * event, or through other events by use of perf_event_set_output().
5047 *
5048 * In order to undo the VM accounting done by perf_mmap() we need to destroy
5049 * the buffer here, where we still have a VM context. This means we need
5050 * to detach all events redirecting to us.
5051 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005052static void perf_mmap_close(struct vm_area_struct *vma)
5053{
5054 struct perf_event *event = vma->vm_file->private_data;
5055
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005056 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005057 struct user_struct *mmap_user = rb->mmap_user;
5058 int mmap_locked = rb->mmap_locked;
5059 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005060
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005061 if (event->pmu->event_unmapped)
5062 event->pmu->event_unmapped(event);
5063
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005064 /*
5065 * rb->aux_mmap_count will always drop before rb->mmap_count and
5066 * event->mmap_count, so it is ok to use event->mmap_mutex to
5067 * serialize with perf_mmap here.
5068 */
5069 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5070 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005071 /*
5072 * Stop all AUX events that are writing to this buffer,
5073 * so that we can free its AUX pages and corresponding PMU
5074 * data. Note that after rb::aux_mmap_count dropped to zero,
5075 * they won't start any more (see perf_aux_output_begin()).
5076 */
5077 perf_pmu_output_stop(event);
5078
5079 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005080 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
5081 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
5082
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005083 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005084 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005085 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
5086
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005087 mutex_unlock(&event->mmap_mutex);
5088 }
5089
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005090 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005091
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005092 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005093 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005094
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005095 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005096 mutex_unlock(&event->mmap_mutex);
5097
5098 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005099 if (atomic_read(&rb->mmap_count))
5100 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005101
5102 /*
5103 * No other mmap()s, detach from all other events that might redirect
5104 * into the now unreachable buffer. Somewhat complicated by the
5105 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5106 */
5107again:
5108 rcu_read_lock();
5109 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5110 if (!atomic_long_inc_not_zero(&event->refcount)) {
5111 /*
5112 * This event is en-route to free_event() which will
5113 * detach it and remove it from the list.
5114 */
5115 continue;
5116 }
5117 rcu_read_unlock();
5118
5119 mutex_lock(&event->mmap_mutex);
5120 /*
5121 * Check we didn't race with perf_event_set_output() which can
5122 * swizzle the rb from under us while we were waiting to
5123 * acquire mmap_mutex.
5124 *
5125 * If we find a different rb; ignore this event, a next
5126 * iteration will no longer find it on the list. We have to
5127 * still restart the iteration to make sure we're not now
5128 * iterating the wrong list.
5129 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005130 if (event->rb == rb)
5131 ring_buffer_attach(event, NULL);
5132
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005133 mutex_unlock(&event->mmap_mutex);
5134 put_event(event);
5135
5136 /*
5137 * Restart the iteration; either we're on the wrong list or
5138 * destroyed its integrity by doing a deletion.
5139 */
5140 goto again;
5141 }
5142 rcu_read_unlock();
5143
5144 /*
5145 * It could be there's still a few 0-ref events on the list; they'll
5146 * get cleaned up by free_event() -- they'll also still have their
5147 * ref on the rb and will free it whenever they are done with it.
5148 *
5149 * Aside from that, this buffer is 'fully' detached and unmapped,
5150 * undo the VM accounting.
5151 */
5152
5153 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
5154 vma->vm_mm->pinned_vm -= mmap_locked;
5155 free_uid(mmap_user);
5156
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005157out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005158 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005159}
5160
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04005161static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005162 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005163 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005164 .fault = perf_mmap_fault,
5165 .page_mkwrite = perf_mmap_fault,
5166};
5167
5168static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5169{
5170 struct perf_event *event = file->private_data;
5171 unsigned long user_locked, user_lock_limit;
5172 struct user_struct *user = current_user();
5173 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005174 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005175 unsigned long vma_size;
5176 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005177 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005178 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005179
Peter Zijlstrac7920612010-05-18 10:33:24 +02005180 /*
5181 * Don't allow mmap() of inherited per-task counters. This would
5182 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005183 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005184 */
5185 if (event->cpu == -1 && event->attr.inherit)
5186 return -EINVAL;
5187
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005188 if (!(vma->vm_flags & VM_SHARED))
5189 return -EINVAL;
5190
5191 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005192
5193 if (vma->vm_pgoff == 0) {
5194 nr_pages = (vma_size / PAGE_SIZE) - 1;
5195 } else {
5196 /*
5197 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5198 * mapped, all subsequent mappings should have the same size
5199 * and offset. Must be above the normal perf buffer.
5200 */
5201 u64 aux_offset, aux_size;
5202
5203 if (!event->rb)
5204 return -EINVAL;
5205
5206 nr_pages = vma_size / PAGE_SIZE;
5207
5208 mutex_lock(&event->mmap_mutex);
5209 ret = -EINVAL;
5210
5211 rb = event->rb;
5212 if (!rb)
5213 goto aux_unlock;
5214
5215 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
5216 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
5217
5218 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5219 goto aux_unlock;
5220
5221 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5222 goto aux_unlock;
5223
5224 /* already mapped with a different offset */
5225 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5226 goto aux_unlock;
5227
5228 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5229 goto aux_unlock;
5230
5231 /* already mapped with a different size */
5232 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5233 goto aux_unlock;
5234
5235 if (!is_power_of_2(nr_pages))
5236 goto aux_unlock;
5237
5238 if (!atomic_inc_not_zero(&rb->mmap_count))
5239 goto aux_unlock;
5240
5241 if (rb_has_aux(rb)) {
5242 atomic_inc(&rb->aux_mmap_count);
5243 ret = 0;
5244 goto unlock;
5245 }
5246
5247 atomic_set(&rb->aux_mmap_count, 1);
5248 user_extra = nr_pages;
5249
5250 goto accounting;
5251 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005252
5253 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005254 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005255 * can do bitmasks instead of modulo.
5256 */
Kan Liang2ed11312015-03-02 02:14:26 -05005257 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005258 return -EINVAL;
5259
5260 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5261 return -EINVAL;
5262
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005263 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005264again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005266 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005267 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005268 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005269 goto unlock;
5270 }
5271
5272 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5273 /*
5274 * Raced against perf_mmap_close() through
5275 * perf_event_set_output(). Try again, hope for better
5276 * luck.
5277 */
5278 mutex_unlock(&event->mmap_mutex);
5279 goto again;
5280 }
5281
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005282 goto unlock;
5283 }
5284
5285 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005286
5287accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005288 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5289
5290 /*
5291 * Increase the limit linearly with more CPUs:
5292 */
5293 user_lock_limit *= num_online_cpus();
5294
5295 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5296
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005297 if (user_locked > user_lock_limit)
5298 extra = user_locked - user_lock_limit;
5299
Jiri Slaby78d7d402010-03-05 13:42:54 -08005300 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005301 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005302 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005303
5304 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5305 !capable(CAP_IPC_LOCK)) {
5306 ret = -EPERM;
5307 goto unlock;
5308 }
5309
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005310 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005311
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005312 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005313 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005314
Frederic Weisbecker76369132011-05-19 19:55:04 +02005315 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005316 rb = rb_alloc(nr_pages,
5317 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5318 event->cpu, flags);
5319
5320 if (!rb) {
5321 ret = -ENOMEM;
5322 goto unlock;
5323 }
5324
5325 atomic_set(&rb->mmap_count, 1);
5326 rb->mmap_user = get_current_user();
5327 rb->mmap_locked = extra;
5328
5329 ring_buffer_attach(event, rb);
5330
5331 perf_event_init_userpage(event);
5332 perf_event_update_userpage(event);
5333 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005334 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5335 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005336 if (!ret)
5337 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005338 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005339
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005340unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005341 if (!ret) {
5342 atomic_long_add(user_extra, &user->locked_vm);
5343 vma->vm_mm->pinned_vm += extra;
5344
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005345 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005346 } else if (rb) {
5347 atomic_dec(&rb->mmap_count);
5348 }
5349aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005350 mutex_unlock(&event->mmap_mutex);
5351
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005352 /*
5353 * Since pinned accounting is per vm we cannot allow fork() to copy our
5354 * vma.
5355 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005356 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005357 vma->vm_ops = &perf_mmap_vmops;
5358
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005359 if (event->pmu->event_mapped)
5360 event->pmu->event_mapped(event);
5361
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005362 return ret;
5363}
5364
5365static int perf_fasync(int fd, struct file *filp, int on)
5366{
Al Viro496ad9a2013-01-23 17:07:38 -05005367 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005368 struct perf_event *event = filp->private_data;
5369 int retval;
5370
Al Viro59551022016-01-22 15:40:57 -05005371 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005372 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005373 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005374
5375 if (retval < 0)
5376 return retval;
5377
5378 return 0;
5379}
5380
5381static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005382 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005383 .release = perf_release,
5384 .read = perf_read,
5385 .poll = perf_poll,
5386 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005387 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005388 .mmap = perf_mmap,
5389 .fasync = perf_fasync,
5390};
5391
5392/*
5393 * Perf event wakeup
5394 *
5395 * If there's data, ensure we set the poll() state and publish everything
5396 * to user-space before waking everybody up.
5397 */
5398
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005399static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5400{
5401 /* only the parent has fasync state */
5402 if (event->parent)
5403 event = event->parent;
5404 return &event->fasync;
5405}
5406
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005407void perf_event_wakeup(struct perf_event *event)
5408{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005409 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005410
5411 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005412 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005413 event->pending_kill = 0;
5414 }
5415}
5416
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005417static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005418{
5419 struct perf_event *event = container_of(entry,
5420 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005421 int rctx;
5422
5423 rctx = perf_swevent_get_recursion_context();
5424 /*
5425 * If we 'fail' here, that's OK, it means recursion is already disabled
5426 * and we won't recurse 'further'.
5427 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005428
5429 if (event->pending_disable) {
5430 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005431 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005432 }
5433
5434 if (event->pending_wakeup) {
5435 event->pending_wakeup = 0;
5436 perf_event_wakeup(event);
5437 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005438
5439 if (rctx >= 0)
5440 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005441}
5442
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005443/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005444 * We assume there is only KVM supporting the callbacks.
5445 * Later on, we might change it to a list if there is
5446 * another virtualization implementation supporting the callbacks.
5447 */
5448struct perf_guest_info_callbacks *perf_guest_cbs;
5449
5450int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5451{
5452 perf_guest_cbs = cbs;
5453 return 0;
5454}
5455EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5456
5457int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5458{
5459 perf_guest_cbs = NULL;
5460 return 0;
5461}
5462EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5463
Jiri Olsa40189942012-08-07 15:20:37 +02005464static void
5465perf_output_sample_regs(struct perf_output_handle *handle,
5466 struct pt_regs *regs, u64 mask)
5467{
5468 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305469 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02005470
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305471 bitmap_from_u64(_mask, mask);
5472 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02005473 u64 val;
5474
5475 val = perf_reg_value(regs, bit);
5476 perf_output_put(handle, val);
5477 }
5478}
5479
Stephane Eranian60e23642014-09-24 13:48:37 +02005480static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005481 struct pt_regs *regs,
5482 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005483{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005484 if (user_mode(regs)) {
5485 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005486 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005487 } else if (current->mm) {
5488 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005489 } else {
5490 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5491 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005492 }
5493}
5494
Stephane Eranian60e23642014-09-24 13:48:37 +02005495static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5496 struct pt_regs *regs)
5497{
5498 regs_intr->regs = regs;
5499 regs_intr->abi = perf_reg_abi(current);
5500}
5501
5502
Jiri Olsac5ebced2012-08-07 15:20:40 +02005503/*
5504 * Get remaining task size from user stack pointer.
5505 *
5506 * It'd be better to take stack vma map and limit this more
5507 * precisly, but there's no way to get it safely under interrupt,
5508 * so using TASK_SIZE as limit.
5509 */
5510static u64 perf_ustack_task_size(struct pt_regs *regs)
5511{
5512 unsigned long addr = perf_user_stack_pointer(regs);
5513
5514 if (!addr || addr >= TASK_SIZE)
5515 return 0;
5516
5517 return TASK_SIZE - addr;
5518}
5519
5520static u16
5521perf_sample_ustack_size(u16 stack_size, u16 header_size,
5522 struct pt_regs *regs)
5523{
5524 u64 task_size;
5525
5526 /* No regs, no stack pointer, no dump. */
5527 if (!regs)
5528 return 0;
5529
5530 /*
5531 * Check if we fit in with the requested stack size into the:
5532 * - TASK_SIZE
5533 * If we don't, we limit the size to the TASK_SIZE.
5534 *
5535 * - remaining sample size
5536 * If we don't, we customize the stack size to
5537 * fit in to the remaining sample size.
5538 */
5539
5540 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5541 stack_size = min(stack_size, (u16) task_size);
5542
5543 /* Current header size plus static size and dynamic size. */
5544 header_size += 2 * sizeof(u64);
5545
5546 /* Do we fit in with the current stack dump size? */
5547 if ((u16) (header_size + stack_size) < header_size) {
5548 /*
5549 * If we overflow the maximum size for the sample,
5550 * we customize the stack dump size to fit in.
5551 */
5552 stack_size = USHRT_MAX - header_size - sizeof(u64);
5553 stack_size = round_up(stack_size, sizeof(u64));
5554 }
5555
5556 return stack_size;
5557}
5558
5559static void
5560perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5561 struct pt_regs *regs)
5562{
5563 /* Case of a kernel thread, nothing to dump */
5564 if (!regs) {
5565 u64 size = 0;
5566 perf_output_put(handle, size);
5567 } else {
5568 unsigned long sp;
5569 unsigned int rem;
5570 u64 dyn_size;
5571
5572 /*
5573 * We dump:
5574 * static size
5575 * - the size requested by user or the best one we can fit
5576 * in to the sample max size
5577 * data
5578 * - user stack dump data
5579 * dynamic size
5580 * - the actual dumped size
5581 */
5582
5583 /* Static size. */
5584 perf_output_put(handle, dump_size);
5585
5586 /* Data. */
5587 sp = perf_user_stack_pointer(regs);
5588 rem = __output_copy_user(handle, (void *) sp, dump_size);
5589 dyn_size = dump_size - rem;
5590
5591 perf_output_skip(handle, rem);
5592
5593 /* Dynamic size. */
5594 perf_output_put(handle, dyn_size);
5595 }
5596}
5597
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005598static void __perf_event_header__init_id(struct perf_event_header *header,
5599 struct perf_sample_data *data,
5600 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005601{
5602 u64 sample_type = event->attr.sample_type;
5603
5604 data->type = sample_type;
5605 header->size += event->id_header_size;
5606
5607 if (sample_type & PERF_SAMPLE_TID) {
5608 /* namespace issues */
5609 data->tid_entry.pid = perf_event_pid(event, current);
5610 data->tid_entry.tid = perf_event_tid(event, current);
5611 }
5612
5613 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005614 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005615
Adrian Hunterff3d5272013-08-27 11:23:07 +03005616 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005617 data->id = primary_event_id(event);
5618
5619 if (sample_type & PERF_SAMPLE_STREAM_ID)
5620 data->stream_id = event->id;
5621
5622 if (sample_type & PERF_SAMPLE_CPU) {
5623 data->cpu_entry.cpu = raw_smp_processor_id();
5624 data->cpu_entry.reserved = 0;
5625 }
5626}
5627
Frederic Weisbecker76369132011-05-19 19:55:04 +02005628void perf_event_header__init_id(struct perf_event_header *header,
5629 struct perf_sample_data *data,
5630 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005631{
5632 if (event->attr.sample_id_all)
5633 __perf_event_header__init_id(header, data, event);
5634}
5635
5636static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5637 struct perf_sample_data *data)
5638{
5639 u64 sample_type = data->type;
5640
5641 if (sample_type & PERF_SAMPLE_TID)
5642 perf_output_put(handle, data->tid_entry);
5643
5644 if (sample_type & PERF_SAMPLE_TIME)
5645 perf_output_put(handle, data->time);
5646
5647 if (sample_type & PERF_SAMPLE_ID)
5648 perf_output_put(handle, data->id);
5649
5650 if (sample_type & PERF_SAMPLE_STREAM_ID)
5651 perf_output_put(handle, data->stream_id);
5652
5653 if (sample_type & PERF_SAMPLE_CPU)
5654 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005655
5656 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5657 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005658}
5659
Frederic Weisbecker76369132011-05-19 19:55:04 +02005660void perf_event__output_id_sample(struct perf_event *event,
5661 struct perf_output_handle *handle,
5662 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005663{
5664 if (event->attr.sample_id_all)
5665 __perf_event__output_id_sample(handle, sample);
5666}
5667
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005668static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005669 struct perf_event *event,
5670 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005671{
5672 u64 read_format = event->attr.read_format;
5673 u64 values[4];
5674 int n = 0;
5675
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005676 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005677 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005678 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005679 atomic64_read(&event->child_total_time_enabled);
5680 }
5681 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005682 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005683 atomic64_read(&event->child_total_time_running);
5684 }
5685 if (read_format & PERF_FORMAT_ID)
5686 values[n++] = primary_event_id(event);
5687
Frederic Weisbecker76369132011-05-19 19:55:04 +02005688 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005689}
5690
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005691static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005692 struct perf_event *event,
5693 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005694{
5695 struct perf_event *leader = event->group_leader, *sub;
5696 u64 read_format = event->attr.read_format;
5697 u64 values[5];
5698 int n = 0;
5699
5700 values[n++] = 1 + leader->nr_siblings;
5701
5702 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005703 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005704
5705 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005706 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005707
Peter Zijlstrabc09bf82018-03-09 12:52:04 +01005708 if ((leader != event) &&
5709 (leader->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005710 leader->pmu->read(leader);
5711
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005712 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005713 if (read_format & PERF_FORMAT_ID)
5714 values[n++] = primary_event_id(leader);
5715
Frederic Weisbecker76369132011-05-19 19:55:04 +02005716 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005717
5718 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5719 n = 0;
5720
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005721 if ((sub != event) &&
5722 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005723 sub->pmu->read(sub);
5724
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005725 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005726 if (read_format & PERF_FORMAT_ID)
5727 values[n++] = primary_event_id(sub);
5728
Frederic Weisbecker76369132011-05-19 19:55:04 +02005729 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005730 }
5731}
5732
Stephane Eranianeed01522010-10-26 16:08:01 +02005733#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5734 PERF_FORMAT_TOTAL_TIME_RUNNING)
5735
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02005736/*
5737 * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
5738 *
5739 * The problem is that its both hard and excessively expensive to iterate the
5740 * child list, not to mention that its impossible to IPI the children running
5741 * on another CPU, from interrupt/NMI context.
5742 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005743static void perf_output_read(struct perf_output_handle *handle,
5744 struct perf_event *event)
5745{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005746 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005747 u64 read_format = event->attr.read_format;
5748
5749 /*
5750 * compute total_time_enabled, total_time_running
5751 * based on snapshot values taken when the event
5752 * was last scheduled in.
5753 *
5754 * we cannot simply called update_context_time()
5755 * because of locking issue as we are called in
5756 * NMI context
5757 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005758 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005759 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005760
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005761 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005762 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005763 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005764 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005765}
5766
5767void perf_output_sample(struct perf_output_handle *handle,
5768 struct perf_event_header *header,
5769 struct perf_sample_data *data,
5770 struct perf_event *event)
5771{
5772 u64 sample_type = data->type;
5773
5774 perf_output_put(handle, *header);
5775
Adrian Hunterff3d5272013-08-27 11:23:07 +03005776 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5777 perf_output_put(handle, data->id);
5778
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005779 if (sample_type & PERF_SAMPLE_IP)
5780 perf_output_put(handle, data->ip);
5781
5782 if (sample_type & PERF_SAMPLE_TID)
5783 perf_output_put(handle, data->tid_entry);
5784
5785 if (sample_type & PERF_SAMPLE_TIME)
5786 perf_output_put(handle, data->time);
5787
5788 if (sample_type & PERF_SAMPLE_ADDR)
5789 perf_output_put(handle, data->addr);
5790
5791 if (sample_type & PERF_SAMPLE_ID)
5792 perf_output_put(handle, data->id);
5793
5794 if (sample_type & PERF_SAMPLE_STREAM_ID)
5795 perf_output_put(handle, data->stream_id);
5796
5797 if (sample_type & PERF_SAMPLE_CPU)
5798 perf_output_put(handle, data->cpu_entry);
5799
5800 if (sample_type & PERF_SAMPLE_PERIOD)
5801 perf_output_put(handle, data->period);
5802
5803 if (sample_type & PERF_SAMPLE_READ)
5804 perf_output_read(handle, event);
5805
5806 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5807 if (data->callchain) {
5808 int size = 1;
5809
5810 if (data->callchain)
5811 size += data->callchain->nr;
5812
5813 size *= sizeof(u64);
5814
Frederic Weisbecker76369132011-05-19 19:55:04 +02005815 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005816 } else {
5817 u64 nr = 0;
5818 perf_output_put(handle, nr);
5819 }
5820 }
5821
5822 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005823 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005824
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005825 if (raw) {
5826 struct perf_raw_frag *frag = &raw->frag;
5827
5828 perf_output_put(handle, raw->size);
5829 do {
5830 if (frag->copy) {
5831 __output_custom(handle, frag->copy,
5832 frag->data, frag->size);
5833 } else {
5834 __output_copy(handle, frag->data,
5835 frag->size);
5836 }
5837 if (perf_raw_frag_last(frag))
5838 break;
5839 frag = frag->next;
5840 } while (1);
5841 if (frag->pad)
5842 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005843 } else {
5844 struct {
5845 u32 size;
5846 u32 data;
5847 } raw = {
5848 .size = sizeof(u32),
5849 .data = 0,
5850 };
5851 perf_output_put(handle, raw);
5852 }
5853 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005854
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005855 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5856 if (data->br_stack) {
5857 size_t size;
5858
5859 size = data->br_stack->nr
5860 * sizeof(struct perf_branch_entry);
5861
5862 perf_output_put(handle, data->br_stack->nr);
5863 perf_output_copy(handle, data->br_stack->entries, size);
5864 } else {
5865 /*
5866 * we always store at least the value of nr
5867 */
5868 u64 nr = 0;
5869 perf_output_put(handle, nr);
5870 }
5871 }
Jiri Olsa40189942012-08-07 15:20:37 +02005872
5873 if (sample_type & PERF_SAMPLE_REGS_USER) {
5874 u64 abi = data->regs_user.abi;
5875
5876 /*
5877 * If there are no regs to dump, notice it through
5878 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5879 */
5880 perf_output_put(handle, abi);
5881
5882 if (abi) {
5883 u64 mask = event->attr.sample_regs_user;
5884 perf_output_sample_regs(handle,
5885 data->regs_user.regs,
5886 mask);
5887 }
5888 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005889
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005890 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005891 perf_output_sample_ustack(handle,
5892 data->stack_user_size,
5893 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005894 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005895
5896 if (sample_type & PERF_SAMPLE_WEIGHT)
5897 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005898
5899 if (sample_type & PERF_SAMPLE_DATA_SRC)
5900 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005901
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005902 if (sample_type & PERF_SAMPLE_TRANSACTION)
5903 perf_output_put(handle, data->txn);
5904
Stephane Eranian60e23642014-09-24 13:48:37 +02005905 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5906 u64 abi = data->regs_intr.abi;
5907 /*
5908 * If there are no regs to dump, notice it through
5909 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5910 */
5911 perf_output_put(handle, abi);
5912
5913 if (abi) {
5914 u64 mask = event->attr.sample_regs_intr;
5915
5916 perf_output_sample_regs(handle,
5917 data->regs_intr.regs,
5918 mask);
5919 }
5920 }
5921
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005922 if (!event->attr.watermark) {
5923 int wakeup_events = event->attr.wakeup_events;
5924
5925 if (wakeup_events) {
5926 struct ring_buffer *rb = handle->rb;
5927 int events = local_inc_return(&rb->events);
5928
5929 if (events >= wakeup_events) {
5930 local_sub(wakeup_events, &rb->events);
5931 local_inc(&rb->wakeup);
5932 }
5933 }
5934 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005935}
5936
5937void perf_prepare_sample(struct perf_event_header *header,
5938 struct perf_sample_data *data,
5939 struct perf_event *event,
5940 struct pt_regs *regs)
5941{
5942 u64 sample_type = event->attr.sample_type;
5943
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005944 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005945 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005946
5947 header->misc = 0;
5948 header->misc |= perf_misc_flags(regs);
5949
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005950 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005951
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005952 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005953 data->ip = perf_instruction_pointer(regs);
5954
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005955 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5956 int size = 1;
5957
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005958 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005959
5960 if (data->callchain)
5961 size += data->callchain->nr;
5962
5963 header->size += size * sizeof(u64);
5964 }
5965
5966 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005967 struct perf_raw_record *raw = data->raw;
5968 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005969
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005970 if (raw) {
5971 struct perf_raw_frag *frag = &raw->frag;
5972 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005973
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005974 do {
5975 sum += frag->size;
5976 if (perf_raw_frag_last(frag))
5977 break;
5978 frag = frag->next;
5979 } while (1);
5980
5981 size = round_up(sum + sizeof(u32), sizeof(u64));
5982 raw->size = size - sizeof(u32);
5983 frag->pad = raw->size - sum;
5984 } else {
5985 size = sizeof(u64);
5986 }
5987
5988 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005989 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005990
5991 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5992 int size = sizeof(u64); /* nr */
5993 if (data->br_stack) {
5994 size += data->br_stack->nr
5995 * sizeof(struct perf_branch_entry);
5996 }
5997 header->size += size;
5998 }
Jiri Olsa40189942012-08-07 15:20:37 +02005999
Peter Zijlstra25657112014-09-24 13:48:42 +02006000 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006001 perf_sample_regs_user(&data->regs_user, regs,
6002 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02006003
Jiri Olsa40189942012-08-07 15:20:37 +02006004 if (sample_type & PERF_SAMPLE_REGS_USER) {
6005 /* regs dump ABI info */
6006 int size = sizeof(u64);
6007
Jiri Olsa40189942012-08-07 15:20:37 +02006008 if (data->regs_user.regs) {
6009 u64 mask = event->attr.sample_regs_user;
6010 size += hweight64(mask) * sizeof(u64);
6011 }
6012
6013 header->size += size;
6014 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006015
6016 if (sample_type & PERF_SAMPLE_STACK_USER) {
6017 /*
6018 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
6019 * processed as the last one or have additional check added
6020 * in case new sample type is added, because we could eat
6021 * up the rest of the sample size.
6022 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02006023 u16 stack_size = event->attr.sample_stack_user;
6024 u16 size = sizeof(u64);
6025
Jiri Olsac5ebced2012-08-07 15:20:40 +02006026 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02006027 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006028
6029 /*
6030 * If there is something to dump, add space for the dump
6031 * itself and for the field that tells the dynamic size,
6032 * which is how many have been actually dumped.
6033 */
6034 if (stack_size)
6035 size += sizeof(u64) + stack_size;
6036
6037 data->stack_user_size = stack_size;
6038 header->size += size;
6039 }
Stephane Eranian60e23642014-09-24 13:48:37 +02006040
6041 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6042 /* regs dump ABI info */
6043 int size = sizeof(u64);
6044
6045 perf_sample_regs_intr(&data->regs_intr, regs);
6046
6047 if (data->regs_intr.regs) {
6048 u64 mask = event->attr.sample_regs_intr;
6049
6050 size += hweight64(mask) * sizeof(u64);
6051 }
6052
6053 header->size += size;
6054 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006055}
6056
Wang Nan9ecda412016-04-05 14:11:18 +00006057static void __always_inline
6058__perf_event_output(struct perf_event *event,
6059 struct perf_sample_data *data,
6060 struct pt_regs *regs,
6061 int (*output_begin)(struct perf_output_handle *,
6062 struct perf_event *,
6063 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006064{
6065 struct perf_output_handle handle;
6066 struct perf_event_header header;
6067
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006068 /* protect the callchain buffers */
6069 rcu_read_lock();
6070
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006071 perf_prepare_sample(&header, data, event, regs);
6072
Wang Nan9ecda412016-04-05 14:11:18 +00006073 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006074 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006075
6076 perf_output_sample(&handle, &header, data, event);
6077
6078 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006079
6080exit:
6081 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006082}
6083
Wang Nan9ecda412016-04-05 14:11:18 +00006084void
6085perf_event_output_forward(struct perf_event *event,
6086 struct perf_sample_data *data,
6087 struct pt_regs *regs)
6088{
6089 __perf_event_output(event, data, regs, perf_output_begin_forward);
6090}
6091
6092void
6093perf_event_output_backward(struct perf_event *event,
6094 struct perf_sample_data *data,
6095 struct pt_regs *regs)
6096{
6097 __perf_event_output(event, data, regs, perf_output_begin_backward);
6098}
6099
6100void
6101perf_event_output(struct perf_event *event,
6102 struct perf_sample_data *data,
6103 struct pt_regs *regs)
6104{
6105 __perf_event_output(event, data, regs, perf_output_begin);
6106}
6107
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006108/*
6109 * read event_id
6110 */
6111
6112struct perf_read_event {
6113 struct perf_event_header header;
6114
6115 u32 pid;
6116 u32 tid;
6117};
6118
6119static void
6120perf_event_read_event(struct perf_event *event,
6121 struct task_struct *task)
6122{
6123 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006124 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006125 struct perf_read_event read_event = {
6126 .header = {
6127 .type = PERF_RECORD_READ,
6128 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006129 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006130 },
6131 .pid = perf_event_pid(event, task),
6132 .tid = perf_event_tid(event, task),
6133 };
6134 int ret;
6135
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006136 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006137 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006138 if (ret)
6139 return;
6140
6141 perf_output_put(&handle, read_event);
6142 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006143 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006144
6145 perf_output_end(&handle);
6146}
6147
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006148typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006149
6150static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006151perf_iterate_ctx(struct perf_event_context *ctx,
6152 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006153 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02006154{
6155 struct perf_event *event;
6156
6157 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006158 if (!all) {
6159 if (event->state < PERF_EVENT_STATE_INACTIVE)
6160 continue;
6161 if (!event_filter_match(event))
6162 continue;
6163 }
6164
Jiri Olsa67516842013-07-09 18:56:31 +02006165 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006166 }
6167}
6168
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006169static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07006170{
6171 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
6172 struct perf_event *event;
6173
6174 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02006175 /*
6176 * Skip events that are not fully formed yet; ensure that
6177 * if we observe event->ctx, both event and ctx will be
6178 * complete enough. See perf_install_in_context().
6179 */
6180 if (!smp_load_acquire(&event->ctx))
6181 continue;
6182
Kan Liangf2fb6be2016-03-23 11:24:37 -07006183 if (event->state < PERF_EVENT_STATE_INACTIVE)
6184 continue;
6185 if (!event_filter_match(event))
6186 continue;
6187 output(event, data);
6188 }
6189}
6190
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006191/*
6192 * Iterate all events that need to receive side-band events.
6193 *
6194 * For new callers; ensure that account_pmu_sb_event() includes
6195 * your event, otherwise it might not get delivered.
6196 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006197static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006198perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006199 struct perf_event_context *task_ctx)
6200{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006201 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02006202 int ctxn;
6203
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006204 rcu_read_lock();
6205 preempt_disable();
6206
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006207 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006208 * If we have task_ctx != NULL we only notify the task context itself.
6209 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006210 * context.
6211 */
6212 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006213 perf_iterate_ctx(task_ctx, output, data, false);
6214 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006215 }
6216
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006217 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07006218
6219 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02006220 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6221 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006222 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006223 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006224done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07006225 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02006226 rcu_read_unlock();
6227}
6228
Alexander Shishkin375637b2016-04-27 18:44:46 +03006229/*
6230 * Clear all file-based filters at exec, they'll have to be
6231 * re-instated when/if these objects are mmapped again.
6232 */
6233static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
6234{
6235 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6236 struct perf_addr_filter *filter;
6237 unsigned int restart = 0, count = 0;
6238 unsigned long flags;
6239
6240 if (!has_addr_filter(event))
6241 return;
6242
6243 raw_spin_lock_irqsave(&ifh->lock, flags);
6244 list_for_each_entry(filter, &ifh->list, entry) {
6245 if (filter->inode) {
6246 event->addr_filters_offs[count] = 0;
6247 restart++;
6248 }
6249
6250 count++;
6251 }
6252
6253 if (restart)
6254 event->addr_filters_gen++;
6255 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6256
6257 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006258 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006259}
6260
6261void perf_event_exec(void)
6262{
6263 struct perf_event_context *ctx;
6264 int ctxn;
6265
6266 rcu_read_lock();
6267 for_each_task_context_nr(ctxn) {
6268 ctx = current->perf_event_ctxp[ctxn];
6269 if (!ctx)
6270 continue;
6271
6272 perf_event_enable_on_exec(ctxn);
6273
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006274 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03006275 true);
6276 }
6277 rcu_read_unlock();
6278}
6279
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006280struct remote_output {
6281 struct ring_buffer *rb;
6282 int err;
6283};
6284
6285static void __perf_event_output_stop(struct perf_event *event, void *data)
6286{
6287 struct perf_event *parent = event->parent;
6288 struct remote_output *ro = data;
6289 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03006290 struct stop_event_data sd = {
6291 .event = event,
6292 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006293
6294 if (!has_aux(event))
6295 return;
6296
6297 if (!parent)
6298 parent = event;
6299
6300 /*
6301 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03006302 * ring-buffer, but it will be the child that's actually using it.
6303 *
6304 * We are using event::rb to determine if the event should be stopped,
6305 * however this may race with ring_buffer_attach() (through set_output),
6306 * which will make us skip the event that actually needs to be stopped.
6307 * So ring_buffer_attach() has to stop an aux event before re-assigning
6308 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006309 */
6310 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006311 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006312}
6313
6314static int __perf_pmu_output_stop(void *info)
6315{
6316 struct perf_event *event = info;
6317 struct pmu *pmu = event->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01006318 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006319 struct remote_output ro = {
6320 .rb = event->rb,
6321 };
6322
6323 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006324 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006325 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006326 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006327 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006328 rcu_read_unlock();
6329
6330 return ro.err;
6331}
6332
6333static void perf_pmu_output_stop(struct perf_event *event)
6334{
6335 struct perf_event *iter;
6336 int err, cpu;
6337
6338restart:
6339 rcu_read_lock();
6340 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6341 /*
6342 * For per-CPU events, we need to make sure that neither they
6343 * nor their children are running; for cpu==-1 events it's
6344 * sufficient to stop the event itself if it's active, since
6345 * it can't have children.
6346 */
6347 cpu = iter->cpu;
6348 if (cpu == -1)
6349 cpu = READ_ONCE(iter->oncpu);
6350
6351 if (cpu == -1)
6352 continue;
6353
6354 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6355 if (err == -EAGAIN) {
6356 rcu_read_unlock();
6357 goto restart;
6358 }
6359 }
6360 rcu_read_unlock();
6361}
6362
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006363/*
6364 * task tracking -- fork/exit
6365 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006366 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006367 */
6368
6369struct perf_task_event {
6370 struct task_struct *task;
6371 struct perf_event_context *task_ctx;
6372
6373 struct {
6374 struct perf_event_header header;
6375
6376 u32 pid;
6377 u32 ppid;
6378 u32 tid;
6379 u32 ptid;
6380 u64 time;
6381 } event_id;
6382};
6383
Jiri Olsa67516842013-07-09 18:56:31 +02006384static int perf_event_task_match(struct perf_event *event)
6385{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006386 return event->attr.comm || event->attr.mmap ||
6387 event->attr.mmap2 || event->attr.mmap_data ||
6388 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006389}
6390
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006391static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006392 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006393{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006394 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006395 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006396 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006397 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006398 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006399
Jiri Olsa67516842013-07-09 18:56:31 +02006400 if (!perf_event_task_match(event))
6401 return;
6402
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006403 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006404
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006405 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006406 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006407 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006408 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006409
6410 task_event->event_id.pid = perf_event_pid(event, task);
6411 task_event->event_id.ppid = perf_event_pid(event, current);
6412
6413 task_event->event_id.tid = perf_event_tid(event, task);
6414 task_event->event_id.ptid = perf_event_tid(event, current);
6415
Peter Zijlstra34f43922015-02-20 14:05:38 +01006416 task_event->event_id.time = perf_event_clock(event);
6417
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006418 perf_output_put(&handle, task_event->event_id);
6419
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006420 perf_event__output_id_sample(event, &handle, &sample);
6421
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006422 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006423out:
6424 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006425}
6426
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006427static void perf_event_task(struct task_struct *task,
6428 struct perf_event_context *task_ctx,
6429 int new)
6430{
6431 struct perf_task_event task_event;
6432
6433 if (!atomic_read(&nr_comm_events) &&
6434 !atomic_read(&nr_mmap_events) &&
6435 !atomic_read(&nr_task_events))
6436 return;
6437
6438 task_event = (struct perf_task_event){
6439 .task = task,
6440 .task_ctx = task_ctx,
6441 .event_id = {
6442 .header = {
6443 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6444 .misc = 0,
6445 .size = sizeof(task_event.event_id),
6446 },
6447 /* .pid */
6448 /* .ppid */
6449 /* .tid */
6450 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006451 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006452 },
6453 };
6454
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006455 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006456 &task_event,
6457 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006458}
6459
6460void perf_event_fork(struct task_struct *task)
6461{
6462 perf_event_task(task, NULL, 1);
6463}
6464
6465/*
6466 * comm tracking
6467 */
6468
6469struct perf_comm_event {
6470 struct task_struct *task;
6471 char *comm;
6472 int comm_size;
6473
6474 struct {
6475 struct perf_event_header header;
6476
6477 u32 pid;
6478 u32 tid;
6479 } event_id;
6480};
6481
Jiri Olsa67516842013-07-09 18:56:31 +02006482static int perf_event_comm_match(struct perf_event *event)
6483{
6484 return event->attr.comm;
6485}
6486
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006487static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006488 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006489{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006490 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006491 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006492 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006493 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006494 int ret;
6495
Jiri Olsa67516842013-07-09 18:56:31 +02006496 if (!perf_event_comm_match(event))
6497 return;
6498
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006499 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6500 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006501 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006502
6503 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006504 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006505
6506 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6507 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6508
6509 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006510 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006511 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006512
6513 perf_event__output_id_sample(event, &handle, &sample);
6514
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006515 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006516out:
6517 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006518}
6519
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006520static void perf_event_comm_event(struct perf_comm_event *comm_event)
6521{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006522 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006523 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006524
6525 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006526 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006527 size = ALIGN(strlen(comm)+1, sizeof(u64));
6528
6529 comm_event->comm = comm;
6530 comm_event->comm_size = size;
6531
6532 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006533
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006534 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006535 comm_event,
6536 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537}
6538
Adrian Hunter82b89772014-05-28 11:45:04 +03006539void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006540{
6541 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006542
6543 if (!atomic_read(&nr_comm_events))
6544 return;
6545
6546 comm_event = (struct perf_comm_event){
6547 .task = task,
6548 /* .comm */
6549 /* .comm_size */
6550 .event_id = {
6551 .header = {
6552 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006553 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006554 /* .size */
6555 },
6556 /* .pid */
6557 /* .tid */
6558 },
6559 };
6560
6561 perf_event_comm_event(&comm_event);
6562}
6563
6564/*
6565 * mmap tracking
6566 */
6567
6568struct perf_mmap_event {
6569 struct vm_area_struct *vma;
6570
6571 const char *file_name;
6572 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006573 int maj, min;
6574 u64 ino;
6575 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006576 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006577
6578 struct {
6579 struct perf_event_header header;
6580
6581 u32 pid;
6582 u32 tid;
6583 u64 start;
6584 u64 len;
6585 u64 pgoff;
6586 } event_id;
6587};
6588
Jiri Olsa67516842013-07-09 18:56:31 +02006589static int perf_event_mmap_match(struct perf_event *event,
6590 void *data)
6591{
6592 struct perf_mmap_event *mmap_event = data;
6593 struct vm_area_struct *vma = mmap_event->vma;
6594 int executable = vma->vm_flags & VM_EXEC;
6595
6596 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006597 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006598}
6599
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006600static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006601 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006602{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006603 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006604 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006605 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006606 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006607 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006608
Jiri Olsa67516842013-07-09 18:56:31 +02006609 if (!perf_event_mmap_match(event, data))
6610 return;
6611
Stephane Eranian13d7a242013-08-21 12:10:24 +02006612 if (event->attr.mmap2) {
6613 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6614 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6615 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6616 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006617 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006618 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6619 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006620 }
6621
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006622 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6623 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006624 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006625 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006626 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006627
6628 mmap_event->event_id.pid = perf_event_pid(event, current);
6629 mmap_event->event_id.tid = perf_event_tid(event, current);
6630
6631 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006632
6633 if (event->attr.mmap2) {
6634 perf_output_put(&handle, mmap_event->maj);
6635 perf_output_put(&handle, mmap_event->min);
6636 perf_output_put(&handle, mmap_event->ino);
6637 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006638 perf_output_put(&handle, mmap_event->prot);
6639 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006640 }
6641
Frederic Weisbecker76369132011-05-19 19:55:04 +02006642 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006643 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006644
6645 perf_event__output_id_sample(event, &handle, &sample);
6646
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006647 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006648out:
6649 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006650}
6651
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006652static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6653{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006654 struct vm_area_struct *vma = mmap_event->vma;
6655 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006656 int maj = 0, min = 0;
6657 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006658 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006659 unsigned int size;
6660 char tmp[16];
6661 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006662 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006663
Peter Zijlstrab41615a2017-01-26 23:15:08 +01006664 if (vma->vm_flags & VM_READ)
6665 prot |= PROT_READ;
6666 if (vma->vm_flags & VM_WRITE)
6667 prot |= PROT_WRITE;
6668 if (vma->vm_flags & VM_EXEC)
6669 prot |= PROT_EXEC;
6670
6671 if (vma->vm_flags & VM_MAYSHARE)
6672 flags = MAP_SHARED;
6673 else
6674 flags = MAP_PRIVATE;
6675
6676 if (vma->vm_flags & VM_DENYWRITE)
6677 flags |= MAP_DENYWRITE;
6678 if (vma->vm_flags & VM_MAYEXEC)
6679 flags |= MAP_EXECUTABLE;
6680 if (vma->vm_flags & VM_LOCKED)
6681 flags |= MAP_LOCKED;
6682 if (vma->vm_flags & VM_HUGETLB)
6683 flags |= MAP_HUGETLB;
6684
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006685 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006686 struct inode *inode;
6687 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006688
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006689 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006690 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006691 name = "//enomem";
6692 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006693 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006694 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006695 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006696 * need to add enough zero bytes after the string to handle
6697 * the 64bit alignment we do later.
6698 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006699 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006700 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006701 name = "//toolong";
6702 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006703 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006704 inode = file_inode(vma->vm_file);
6705 dev = inode->i_sb->s_dev;
6706 ino = inode->i_ino;
6707 gen = inode->i_generation;
6708 maj = MAJOR(dev);
6709 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006710
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006711 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006712 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006713 if (vma->vm_ops && vma->vm_ops->name) {
6714 name = (char *) vma->vm_ops->name(vma);
6715 if (name)
6716 goto cpy_name;
6717 }
6718
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006719 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006720 if (name)
6721 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006722
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006723 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006724 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006725 name = "[heap]";
6726 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006727 }
6728 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006729 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006730 name = "[stack]";
6731 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006732 }
6733
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006734 name = "//anon";
6735 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006736 }
6737
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006738cpy_name:
6739 strlcpy(tmp, name, sizeof(tmp));
6740 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006741got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006742 /*
6743 * Since our buffer works in 8 byte units we need to align our string
6744 * size to a multiple of 8. However, we must guarantee the tail end is
6745 * zero'd out to avoid leaking random bits to userspace.
6746 */
6747 size = strlen(name)+1;
6748 while (!IS_ALIGNED(size, sizeof(u64)))
6749 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006750
6751 mmap_event->file_name = name;
6752 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006753 mmap_event->maj = maj;
6754 mmap_event->min = min;
6755 mmap_event->ino = ino;
6756 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006757 mmap_event->prot = prot;
6758 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006759
Stephane Eranian2fe85422013-01-24 16:10:39 +01006760 if (!(vma->vm_flags & VM_EXEC))
6761 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6762
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006763 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6764
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006765 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006766 mmap_event,
6767 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006768
6769 kfree(buf);
6770}
6771
Alexander Shishkin375637b2016-04-27 18:44:46 +03006772/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03006773 * Check whether inode and address range match filter criteria.
6774 */
6775static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6776 struct file *file, unsigned long offset,
6777 unsigned long size)
6778{
6779 if (filter->inode != file->f_inode)
6780 return false;
6781
6782 if (filter->offset > offset + size)
6783 return false;
6784
6785 if (filter->offset + filter->size < offset)
6786 return false;
6787
6788 return true;
6789}
6790
6791static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6792{
6793 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6794 struct vm_area_struct *vma = data;
6795 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6796 struct file *file = vma->vm_file;
6797 struct perf_addr_filter *filter;
6798 unsigned int restart = 0, count = 0;
6799
6800 if (!has_addr_filter(event))
6801 return;
6802
6803 if (!file)
6804 return;
6805
6806 raw_spin_lock_irqsave(&ifh->lock, flags);
6807 list_for_each_entry(filter, &ifh->list, entry) {
6808 if (perf_addr_filter_match(filter, file, off,
6809 vma->vm_end - vma->vm_start)) {
6810 event->addr_filters_offs[count] = vma->vm_start;
6811 restart++;
6812 }
6813
6814 count++;
6815 }
6816
6817 if (restart)
6818 event->addr_filters_gen++;
6819 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6820
6821 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006822 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006823}
6824
6825/*
6826 * Adjust all task's events' filters to the new vma
6827 */
6828static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6829{
6830 struct perf_event_context *ctx;
6831 int ctxn;
6832
Mathieu Poirier12b40a22016-07-18 10:43:06 -06006833 /*
6834 * Data tracing isn't supported yet and as such there is no need
6835 * to keep track of anything that isn't related to executable code:
6836 */
6837 if (!(vma->vm_flags & VM_EXEC))
6838 return;
6839
Alexander Shishkin375637b2016-04-27 18:44:46 +03006840 rcu_read_lock();
6841 for_each_task_context_nr(ctxn) {
6842 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6843 if (!ctx)
6844 continue;
6845
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006846 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006847 }
6848 rcu_read_unlock();
6849}
6850
Eric B Munson3af9e852010-05-18 15:30:49 +01006851void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006852{
6853 struct perf_mmap_event mmap_event;
6854
6855 if (!atomic_read(&nr_mmap_events))
6856 return;
6857
6858 mmap_event = (struct perf_mmap_event){
6859 .vma = vma,
6860 /* .file_name */
6861 /* .file_size */
6862 .event_id = {
6863 .header = {
6864 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006865 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006866 /* .size */
6867 },
6868 /* .pid */
6869 /* .tid */
6870 .start = vma->vm_start,
6871 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006872 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006873 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006874 /* .maj (attr_mmap2 only) */
6875 /* .min (attr_mmap2 only) */
6876 /* .ino (attr_mmap2 only) */
6877 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006878 /* .prot (attr_mmap2 only) */
6879 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006880 };
6881
Alexander Shishkin375637b2016-04-27 18:44:46 +03006882 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006883 perf_event_mmap_event(&mmap_event);
6884}
6885
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006886void perf_event_aux_event(struct perf_event *event, unsigned long head,
6887 unsigned long size, u64 flags)
6888{
6889 struct perf_output_handle handle;
6890 struct perf_sample_data sample;
6891 struct perf_aux_event {
6892 struct perf_event_header header;
6893 u64 offset;
6894 u64 size;
6895 u64 flags;
6896 } rec = {
6897 .header = {
6898 .type = PERF_RECORD_AUX,
6899 .misc = 0,
6900 .size = sizeof(rec),
6901 },
6902 .offset = head,
6903 .size = size,
6904 .flags = flags,
6905 };
6906 int ret;
6907
6908 perf_event_header__init_id(&rec.header, &sample, event);
6909 ret = perf_output_begin(&handle, event, rec.header.size);
6910
6911 if (ret)
6912 return;
6913
6914 perf_output_put(&handle, rec);
6915 perf_event__output_id_sample(event, &handle, &sample);
6916
6917 perf_output_end(&handle);
6918}
6919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006920/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006921 * Lost/dropped samples logging
6922 */
6923void perf_log_lost_samples(struct perf_event *event, u64 lost)
6924{
6925 struct perf_output_handle handle;
6926 struct perf_sample_data sample;
6927 int ret;
6928
6929 struct {
6930 struct perf_event_header header;
6931 u64 lost;
6932 } lost_samples_event = {
6933 .header = {
6934 .type = PERF_RECORD_LOST_SAMPLES,
6935 .misc = 0,
6936 .size = sizeof(lost_samples_event),
6937 },
6938 .lost = lost,
6939 };
6940
6941 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6942
6943 ret = perf_output_begin(&handle, event,
6944 lost_samples_event.header.size);
6945 if (ret)
6946 return;
6947
6948 perf_output_put(&handle, lost_samples_event);
6949 perf_event__output_id_sample(event, &handle, &sample);
6950 perf_output_end(&handle);
6951}
6952
6953/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006954 * context_switch tracking
6955 */
6956
6957struct perf_switch_event {
6958 struct task_struct *task;
6959 struct task_struct *next_prev;
6960
6961 struct {
6962 struct perf_event_header header;
6963 u32 next_prev_pid;
6964 u32 next_prev_tid;
6965 } event_id;
6966};
6967
6968static int perf_event_switch_match(struct perf_event *event)
6969{
6970 return event->attr.context_switch;
6971}
6972
6973static void perf_event_switch_output(struct perf_event *event, void *data)
6974{
6975 struct perf_switch_event *se = data;
6976 struct perf_output_handle handle;
6977 struct perf_sample_data sample;
6978 int ret;
6979
6980 if (!perf_event_switch_match(event))
6981 return;
6982
6983 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6984 if (event->ctx->task) {
6985 se->event_id.header.type = PERF_RECORD_SWITCH;
6986 se->event_id.header.size = sizeof(se->event_id.header);
6987 } else {
6988 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6989 se->event_id.header.size = sizeof(se->event_id);
6990 se->event_id.next_prev_pid =
6991 perf_event_pid(event, se->next_prev);
6992 se->event_id.next_prev_tid =
6993 perf_event_tid(event, se->next_prev);
6994 }
6995
6996 perf_event_header__init_id(&se->event_id.header, &sample, event);
6997
6998 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6999 if (ret)
7000 return;
7001
7002 if (event->ctx->task)
7003 perf_output_put(&handle, se->event_id.header);
7004 else
7005 perf_output_put(&handle, se->event_id);
7006
7007 perf_event__output_id_sample(event, &handle, &sample);
7008
7009 perf_output_end(&handle);
7010}
7011
7012static void perf_event_switch(struct task_struct *task,
7013 struct task_struct *next_prev, bool sched_in)
7014{
7015 struct perf_switch_event switch_event;
7016
7017 /* N.B. caller checks nr_switch_events != 0 */
7018
7019 switch_event = (struct perf_switch_event){
7020 .task = task,
7021 .next_prev = next_prev,
7022 .event_id = {
7023 .header = {
7024 /* .type */
7025 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
7026 /* .size */
7027 },
7028 /* .next_prev_pid */
7029 /* .next_prev_tid */
7030 },
7031 };
7032
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007033 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03007034 &switch_event,
7035 NULL);
7036}
7037
7038/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007039 * IRQ throttle logging
7040 */
7041
7042static void perf_log_throttle(struct perf_event *event, int enable)
7043{
7044 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007045 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007046 int ret;
7047
7048 struct {
7049 struct perf_event_header header;
7050 u64 time;
7051 u64 id;
7052 u64 stream_id;
7053 } throttle_event = {
7054 .header = {
7055 .type = PERF_RECORD_THROTTLE,
7056 .misc = 0,
7057 .size = sizeof(throttle_event),
7058 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01007059 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007060 .id = primary_event_id(event),
7061 .stream_id = event->id,
7062 };
7063
7064 if (enable)
7065 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
7066
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007067 perf_event_header__init_id(&throttle_event.header, &sample, event);
7068
7069 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007070 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007071 if (ret)
7072 return;
7073
7074 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007075 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007076 perf_output_end(&handle);
7077}
7078
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007079static void perf_log_itrace_start(struct perf_event *event)
7080{
7081 struct perf_output_handle handle;
7082 struct perf_sample_data sample;
7083 struct perf_aux_event {
7084 struct perf_event_header header;
7085 u32 pid;
7086 u32 tid;
7087 } rec;
7088 int ret;
7089
7090 if (event->parent)
7091 event = event->parent;
7092
7093 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
7094 event->hw.itrace_started)
7095 return;
7096
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007097 rec.header.type = PERF_RECORD_ITRACE_START;
7098 rec.header.misc = 0;
7099 rec.header.size = sizeof(rec);
7100 rec.pid = perf_event_pid(event, current);
7101 rec.tid = perf_event_tid(event, current);
7102
7103 perf_event_header__init_id(&rec.header, &sample, event);
7104 ret = perf_output_begin(&handle, event, rec.header.size);
7105
7106 if (ret)
7107 return;
7108
7109 perf_output_put(&handle, rec);
7110 perf_event__output_id_sample(event, &handle, &sample);
7111
7112 perf_output_end(&handle);
7113}
7114
Jiri Olsaa88ff232016-12-28 14:31:03 +01007115static int
7116__perf_event_account_interrupt(struct perf_event *event, int throttle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007117{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007118 struct hw_perf_event *hwc = &event->hw;
7119 int ret = 0;
Jiri Olsaa88ff232016-12-28 14:31:03 +01007120 u64 seq;
Peter Zijlstra96398822010-11-24 18:55:29 +01007121
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007122 seq = __this_cpu_read(perf_throttled_seq);
7123 if (seq != hwc->interrupts_seq) {
7124 hwc->interrupts_seq = seq;
7125 hwc->interrupts = 1;
7126 } else {
7127 hwc->interrupts++;
7128 if (unlikely(throttle
7129 && hwc->interrupts >= max_samples_per_tick)) {
7130 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007131 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01007132 hwc->interrupts = MAX_INTERRUPTS;
7133 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007134 ret = 1;
7135 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007136 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007137
7138 if (event->attr.freq) {
7139 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01007140 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007141
Peter Zijlstraabd50712010-01-26 18:50:16 +01007142 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007143
Peter Zijlstraabd50712010-01-26 18:50:16 +01007144 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01007145 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007146 }
7147
Jiri Olsaa88ff232016-12-28 14:31:03 +01007148 return ret;
7149}
7150
7151int perf_event_account_interrupt(struct perf_event *event)
7152{
7153 return __perf_event_account_interrupt(event, 1);
7154}
7155
7156/*
7157 * Generic event overflow handling, sampling.
7158 */
7159
7160static int __perf_event_overflow(struct perf_event *event,
7161 int throttle, struct perf_sample_data *data,
7162 struct pt_regs *regs)
7163{
7164 int events = atomic_read(&event->event_limit);
7165 int ret = 0;
7166
7167 /*
7168 * Non-sampling counters might still use the PMI to fold short
7169 * hardware counters, ignore those.
7170 */
7171 if (unlikely(!is_sampling_event(event)))
7172 return 0;
7173
7174 ret = __perf_event_account_interrupt(event, throttle);
7175
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007176 /*
7177 * XXX event_limit might not quite work as expected on inherited
7178 * events
7179 */
7180
7181 event->pending_kill = POLL_IN;
7182 if (events && atomic_dec_and_test(&event->event_limit)) {
7183 ret = 1;
7184 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02007185
7186 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007187 }
7188
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007189 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01007190
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02007191 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007192 event->pending_wakeup = 1;
7193 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02007194 }
7195
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007196 return ret;
7197}
7198
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007199int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007200 struct perf_sample_data *data,
7201 struct pt_regs *regs)
7202{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007203 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007204}
7205
7206/*
7207 * Generic software event infrastructure
7208 */
7209
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007210struct swevent_htable {
7211 struct swevent_hlist *swevent_hlist;
7212 struct mutex hlist_mutex;
7213 int hlist_refcount;
7214
7215 /* Recursion avoidance in each contexts */
7216 int recursion[PERF_NR_CONTEXTS];
7217};
7218
7219static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7220
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007221/*
7222 * We directly increment event->count and keep a second value in
7223 * event->hw.period_left to count intervals. This period event
7224 * is kept in the range [-sample_period, 0] so that we can use the
7225 * sign as trigger.
7226 */
7227
Jiri Olsaab573842013-05-01 17:25:44 +02007228u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007229{
7230 struct hw_perf_event *hwc = &event->hw;
7231 u64 period = hwc->last_period;
7232 u64 nr, offset;
7233 s64 old, val;
7234
7235 hwc->last_period = hwc->sample_period;
7236
7237again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02007238 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007239 if (val < 0)
7240 return 0;
7241
7242 nr = div64_u64(period + val, period);
7243 offset = nr * period;
7244 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02007245 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007246 goto again;
7247
7248 return nr;
7249}
7250
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007251static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007252 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007253 struct pt_regs *regs)
7254{
7255 struct hw_perf_event *hwc = &event->hw;
7256 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007257
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007258 if (!overflow)
7259 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007260
7261 if (hwc->interrupts == MAX_INTERRUPTS)
7262 return;
7263
7264 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007265 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007266 data, regs)) {
7267 /*
7268 * We inhibit the overflow from happening when
7269 * hwc->interrupts == MAX_INTERRUPTS.
7270 */
7271 break;
7272 }
7273 throttle = 1;
7274 }
7275}
7276
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007277static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007278 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007279 struct pt_regs *regs)
7280{
7281 struct hw_perf_event *hwc = &event->hw;
7282
Peter Zijlstrae7850592010-05-21 14:43:08 +02007283 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007284
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007285 if (!regs)
7286 return;
7287
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007288 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007289 return;
7290
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03007291 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
7292 data->period = nr;
7293 return perf_swevent_overflow(event, 1, data, regs);
7294 } else
7295 data->period = event->hw.last_period;
7296
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007297 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007298 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007299
Peter Zijlstrae7850592010-05-21 14:43:08 +02007300 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007301 return;
7302
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007303 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007304}
7305
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007306static int perf_exclude_event(struct perf_event *event,
7307 struct pt_regs *regs)
7308{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007309 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01007310 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007311
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007312 if (regs) {
7313 if (event->attr.exclude_user && user_mode(regs))
7314 return 1;
7315
7316 if (event->attr.exclude_kernel && !user_mode(regs))
7317 return 1;
7318 }
7319
7320 return 0;
7321}
7322
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007323static int perf_swevent_match(struct perf_event *event,
7324 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007325 u32 event_id,
7326 struct perf_sample_data *data,
7327 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007328{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007329 if (event->attr.type != type)
7330 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007331
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007332 if (event->attr.config != event_id)
7333 return 0;
7334
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007335 if (perf_exclude_event(event, regs))
7336 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007337
7338 return 1;
7339}
7340
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007341static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007342{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007343 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007344
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007345 return hash_64(val, SWEVENT_HLIST_BITS);
7346}
7347
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007348static inline struct hlist_head *
7349__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007350{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007351 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007352
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007353 return &hlist->heads[hash];
7354}
7355
7356/* For the read side: events when they trigger */
7357static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007358find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007359{
7360 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007361
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007362 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007363 if (!hlist)
7364 return NULL;
7365
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007366 return __find_swevent_head(hlist, type, event_id);
7367}
7368
7369/* For the event head insertion and removal in the hlist */
7370static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007371find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007372{
7373 struct swevent_hlist *hlist;
7374 u32 event_id = event->attr.config;
7375 u64 type = event->attr.type;
7376
7377 /*
7378 * Event scheduling is always serialized against hlist allocation
7379 * and release. Which makes the protected version suitable here.
7380 * The context lock guarantees that.
7381 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007382 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007383 lockdep_is_held(&event->ctx->lock));
7384 if (!hlist)
7385 return NULL;
7386
7387 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007388}
7389
7390static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007391 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007392 struct perf_sample_data *data,
7393 struct pt_regs *regs)
7394{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007395 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007396 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007397 struct hlist_head *head;
7398
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007399 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007400 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007401 if (!head)
7402 goto end;
7403
Sasha Levinb67bfe02013-02-27 17:06:00 -08007404 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007405 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007406 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007407 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007408end:
7409 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007410}
7411
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007412DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7413
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007414int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007415{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007416 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007417
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007418 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007419}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007420EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007421
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007422void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007423{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007424 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007425
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007426 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007427}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007428
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007429void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007430{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007431 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007432
7433 if (WARN_ON_ONCE(!regs))
7434 return;
7435
7436 perf_sample_data_init(&data, addr, 0);
7437 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7438}
7439
7440void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7441{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007442 int rctx;
7443
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007444 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007445 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007446 if (unlikely(rctx < 0))
7447 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007448
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007449 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007450
7451 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007452fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007453 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007454}
7455
7456static void perf_swevent_read(struct perf_event *event)
7457{
7458}
7459
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007460static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007461{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007462 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007463 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007464 struct hlist_head *head;
7465
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007466 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007467 hwc->last_period = hwc->sample_period;
7468 perf_swevent_set_period(event);
7469 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007470
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007471 hwc->state = !(flags & PERF_EF_START);
7472
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007473 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007474 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007475 return -EINVAL;
7476
7477 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007478 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007479
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007480 return 0;
7481}
7482
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007483static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007484{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007485 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007486}
7487
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007488static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007489{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007490 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007491}
7492
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007493static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007494{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007495 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007496}
7497
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007498/* Deref the hlist from the update side */
7499static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007500swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007501{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007502 return rcu_dereference_protected(swhash->swevent_hlist,
7503 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007504}
7505
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007506static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007507{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007508 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007509
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007510 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007511 return;
7512
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007513 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007514 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007515}
7516
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007517static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007518{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007519 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007520
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007521 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007522
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007523 if (!--swhash->hlist_refcount)
7524 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007525
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007526 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007527}
7528
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007529static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007530{
7531 int cpu;
7532
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007533 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007534 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007535}
7536
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007537static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007538{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007539 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007540 int err = 0;
7541
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007542 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007543 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007544 struct swevent_hlist *hlist;
7545
7546 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7547 if (!hlist) {
7548 err = -ENOMEM;
7549 goto exit;
7550 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007551 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007552 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007553 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007554exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007555 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007556
7557 return err;
7558}
7559
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007560static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007561{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007562 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007563
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007564 get_online_cpus();
7565 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007566 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007567 if (err) {
7568 failed_cpu = cpu;
7569 goto fail;
7570 }
7571 }
7572 put_online_cpus();
7573
7574 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007575fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007576 for_each_possible_cpu(cpu) {
7577 if (cpu == failed_cpu)
7578 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007579 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007580 }
7581
7582 put_online_cpus();
7583 return err;
7584}
7585
Ingo Molnarc5905af2012-02-24 08:31:31 +01007586struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007587
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007588static void sw_perf_event_destroy(struct perf_event *event)
7589{
7590 u64 event_id = event->attr.config;
7591
7592 WARN_ON(event->parent);
7593
Ingo Molnarc5905af2012-02-24 08:31:31 +01007594 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007595 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007596}
7597
7598static int perf_swevent_init(struct perf_event *event)
7599{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007600 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007601
7602 if (event->attr.type != PERF_TYPE_SOFTWARE)
7603 return -ENOENT;
7604
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007605 /*
7606 * no branch sampling for software events
7607 */
7608 if (has_branch_stack(event))
7609 return -EOPNOTSUPP;
7610
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007611 switch (event_id) {
7612 case PERF_COUNT_SW_CPU_CLOCK:
7613 case PERF_COUNT_SW_TASK_CLOCK:
7614 return -ENOENT;
7615
7616 default:
7617 break;
7618 }
7619
Dan Carpenterce677832010-10-24 21:50:42 +02007620 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007621 return -ENOENT;
7622
7623 if (!event->parent) {
7624 int err;
7625
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007626 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007627 if (err)
7628 return err;
7629
Ingo Molnarc5905af2012-02-24 08:31:31 +01007630 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007631 event->destroy = sw_perf_event_destroy;
7632 }
7633
7634 return 0;
7635}
7636
7637static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007638 .task_ctx_nr = perf_sw_context,
7639
Peter Zijlstra34f43922015-02-20 14:05:38 +01007640 .capabilities = PERF_PMU_CAP_NO_NMI,
7641
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007642 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007643 .add = perf_swevent_add,
7644 .del = perf_swevent_del,
7645 .start = perf_swevent_start,
7646 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007647 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007648};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007649
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007650#ifdef CONFIG_EVENT_TRACING
7651
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007652static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007653 struct perf_sample_data *data)
7654{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007655 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007656
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007657 /* only top level events have filters set */
7658 if (event->parent)
7659 event = event->parent;
7660
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007661 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7662 return 1;
7663 return 0;
7664}
7665
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007666static int perf_tp_event_match(struct perf_event *event,
7667 struct perf_sample_data *data,
7668 struct pt_regs *regs)
7669{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007670 if (event->hw.state & PERF_HES_STOPPED)
7671 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007672 /*
7673 * All tracepoints are from kernel-space.
7674 */
7675 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007676 return 0;
7677
7678 if (!perf_tp_filter_match(event, data))
7679 return 0;
7680
7681 return 1;
7682}
7683
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07007684void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7685 struct trace_event_call *call, u64 count,
7686 struct pt_regs *regs, struct hlist_head *head,
7687 struct task_struct *task)
7688{
7689 struct bpf_prog *prog = call->prog;
7690
7691 if (prog) {
7692 *(struct pt_regs **)raw_data = regs;
7693 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7694 perf_swevent_put_recursion_context(rctx);
7695 return;
7696 }
7697 }
7698 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7699 rctx, task);
7700}
7701EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7702
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007703void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007704 struct pt_regs *regs, struct hlist_head *head, int rctx,
7705 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007706{
7707 struct perf_sample_data data;
7708 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007709
7710 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007711 .frag = {
7712 .size = entry_size,
7713 .data = record,
7714 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007715 };
7716
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007717 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007718 data.raw = &raw;
7719
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007720 perf_trace_buf_update(record, event_type);
7721
Sasha Levinb67bfe02013-02-27 17:06:00 -08007722 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007723 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007724 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007725 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007726
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007727 /*
7728 * If we got specified a target task, also iterate its context and
7729 * deliver this event there too.
7730 */
7731 if (task && task != current) {
7732 struct perf_event_context *ctx;
7733 struct trace_entry *entry = record;
7734
7735 rcu_read_lock();
7736 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7737 if (!ctx)
7738 goto unlock;
7739
7740 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7741 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7742 continue;
7743 if (event->attr.config != entry->type)
7744 continue;
7745 if (perf_tp_event_match(event, &data, regs))
7746 perf_swevent_event(event, count, &data, regs);
7747 }
7748unlock:
7749 rcu_read_unlock();
7750 }
7751
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007752 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007753}
7754EXPORT_SYMBOL_GPL(perf_tp_event);
7755
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007756static void tp_perf_event_destroy(struct perf_event *event)
7757{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007758 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007759}
7760
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007761static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007762{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007763 int err;
7764
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007765 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7766 return -ENOENT;
7767
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007768 /*
7769 * no branch sampling for tracepoint events
7770 */
7771 if (has_branch_stack(event))
7772 return -EOPNOTSUPP;
7773
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007774 err = perf_trace_init(event);
7775 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007776 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007777
7778 event->destroy = tp_perf_event_destroy;
7779
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007780 return 0;
7781}
7782
7783static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007784 .task_ctx_nr = perf_sw_context,
7785
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007786 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007787 .add = perf_trace_add,
7788 .del = perf_trace_del,
7789 .start = perf_swevent_start,
7790 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007791 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007792};
7793
7794static inline void perf_tp_register(void)
7795{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007796 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007797}
Li Zefan6fb29152009-10-15 11:21:42 +08007798
Li Zefan6fb29152009-10-15 11:21:42 +08007799static void perf_event_free_filter(struct perf_event *event)
7800{
7801 ftrace_profile_free_filter(event);
7802}
7803
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007804#ifdef CONFIG_BPF_SYSCALL
7805static void bpf_overflow_handler(struct perf_event *event,
7806 struct perf_sample_data *data,
7807 struct pt_regs *regs)
7808{
7809 struct bpf_perf_event_data_kern ctx = {
7810 .data = data,
7811 .regs = regs,
7812 };
7813 int ret = 0;
7814
7815 preempt_disable();
7816 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
7817 goto out;
7818 rcu_read_lock();
7819 ret = BPF_PROG_RUN(event->prog, (void *)&ctx);
7820 rcu_read_unlock();
7821out:
7822 __this_cpu_dec(bpf_prog_active);
7823 preempt_enable();
7824 if (!ret)
7825 return;
7826
7827 event->orig_overflow_handler(event, data, regs);
7828}
7829
7830static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7831{
7832 struct bpf_prog *prog;
7833
7834 if (event->overflow_handler_context)
7835 /* hw breakpoint or kernel counter */
7836 return -EINVAL;
7837
7838 if (event->prog)
7839 return -EEXIST;
7840
7841 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
7842 if (IS_ERR(prog))
7843 return PTR_ERR(prog);
7844
7845 event->prog = prog;
7846 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
7847 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
7848 return 0;
7849}
7850
7851static void perf_event_free_bpf_handler(struct perf_event *event)
7852{
7853 struct bpf_prog *prog = event->prog;
7854
7855 if (!prog)
7856 return;
7857
7858 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
7859 event->prog = NULL;
7860 bpf_prog_put(prog);
7861}
7862#else
7863static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7864{
7865 return -EOPNOTSUPP;
7866}
7867static void perf_event_free_bpf_handler(struct perf_event *event)
7868{
7869}
7870#endif
7871
Alexei Starovoitov25415172015-03-25 12:49:20 -07007872static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7873{
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007874 bool is_kprobe, is_tracepoint;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007875 struct bpf_prog *prog;
7876
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007877 if (event->attr.type == PERF_TYPE_HARDWARE ||
7878 event->attr.type == PERF_TYPE_SOFTWARE)
7879 return perf_event_set_bpf_handler(event, prog_fd);
7880
Alexei Starovoitov25415172015-03-25 12:49:20 -07007881 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7882 return -EINVAL;
7883
7884 if (event->tp_event->prog)
7885 return -EEXIST;
7886
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007887 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7888 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7889 if (!is_kprobe && !is_tracepoint)
7890 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007891 return -EINVAL;
7892
7893 prog = bpf_prog_get(prog_fd);
7894 if (IS_ERR(prog))
7895 return PTR_ERR(prog);
7896
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007897 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7898 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007899 /* valid fd, but invalid bpf program type */
7900 bpf_prog_put(prog);
7901 return -EINVAL;
7902 }
7903
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07007904 if (is_tracepoint) {
7905 int off = trace_event_get_offsets(event->tp_event);
7906
7907 if (prog->aux->max_ctx_offset > off) {
7908 bpf_prog_put(prog);
7909 return -EACCES;
7910 }
7911 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07007912 event->tp_event->prog = prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007913 event->tp_event->bpf_prog_owner = event;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007914
7915 return 0;
7916}
7917
7918static void perf_event_free_bpf_prog(struct perf_event *event)
7919{
7920 struct bpf_prog *prog;
7921
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007922 perf_event_free_bpf_handler(event);
7923
Alexei Starovoitov25415172015-03-25 12:49:20 -07007924 if (!event->tp_event)
7925 return;
7926
7927 prog = event->tp_event->prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007928 if (prog && event->tp_event->bpf_prog_owner == event) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007929 event->tp_event->prog = NULL;
Daniel Borkmann1aacde32016-06-30 17:24:43 +02007930 bpf_prog_put(prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07007931 }
7932}
7933
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007934#else
Li Zefan6fb29152009-10-15 11:21:42 +08007935
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007936static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007937{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007938}
Li Zefan6fb29152009-10-15 11:21:42 +08007939
Li Zefan6fb29152009-10-15 11:21:42 +08007940static void perf_event_free_filter(struct perf_event *event)
7941{
7942}
7943
Alexei Starovoitov25415172015-03-25 12:49:20 -07007944static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7945{
7946 return -ENOENT;
7947}
7948
7949static void perf_event_free_bpf_prog(struct perf_event *event)
7950{
7951}
Li Zefan07b139c2009-12-21 14:27:35 +08007952#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007953
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007954#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007955void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007956{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007957 struct perf_sample_data sample;
7958 struct pt_regs *regs = data;
7959
Robert Richterfd0d0002012-04-02 20:19:08 +02007960 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007961
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007962 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007963 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007964}
7965#endif
7966
Alexander Shishkin375637b2016-04-27 18:44:46 +03007967/*
7968 * Allocate a new address filter
7969 */
7970static struct perf_addr_filter *
7971perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7972{
7973 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7974 struct perf_addr_filter *filter;
7975
7976 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7977 if (!filter)
7978 return NULL;
7979
7980 INIT_LIST_HEAD(&filter->entry);
7981 list_add_tail(&filter->entry, filters);
7982
7983 return filter;
7984}
7985
7986static void free_filters_list(struct list_head *filters)
7987{
7988 struct perf_addr_filter *filter, *iter;
7989
7990 list_for_each_entry_safe(filter, iter, filters, entry) {
7991 if (filter->inode)
7992 iput(filter->inode);
7993 list_del(&filter->entry);
7994 kfree(filter);
7995 }
7996}
7997
7998/*
7999 * Free existing address filters and optionally install new ones
8000 */
8001static void perf_addr_filters_splice(struct perf_event *event,
8002 struct list_head *head)
8003{
8004 unsigned long flags;
8005 LIST_HEAD(list);
8006
8007 if (!has_addr_filter(event))
8008 return;
8009
8010 /* don't bother with children, they don't have their own filters */
8011 if (event->parent)
8012 return;
8013
8014 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
8015
8016 list_splice_init(&event->addr_filters.list, &list);
8017 if (head)
8018 list_splice(head, &event->addr_filters.list);
8019
8020 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
8021
8022 free_filters_list(&list);
8023}
8024
8025/*
8026 * Scan through mm's vmas and see if one of them matches the
8027 * @filter; if so, adjust filter's address range.
8028 * Called with mm::mmap_sem down for reading.
8029 */
8030static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
8031 struct mm_struct *mm)
8032{
8033 struct vm_area_struct *vma;
8034
8035 for (vma = mm->mmap; vma; vma = vma->vm_next) {
8036 struct file *file = vma->vm_file;
8037 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
8038 unsigned long vma_size = vma->vm_end - vma->vm_start;
8039
8040 if (!file)
8041 continue;
8042
8043 if (!perf_addr_filter_match(filter, file, off, vma_size))
8044 continue;
8045
8046 return vma->vm_start;
8047 }
8048
8049 return 0;
8050}
8051
8052/*
8053 * Update event's address range filters based on the
8054 * task's existing mappings, if any.
8055 */
8056static void perf_event_addr_filters_apply(struct perf_event *event)
8057{
8058 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
8059 struct task_struct *task = READ_ONCE(event->ctx->task);
8060 struct perf_addr_filter *filter;
8061 struct mm_struct *mm = NULL;
8062 unsigned int count = 0;
8063 unsigned long flags;
8064
8065 /*
8066 * We may observe TASK_TOMBSTONE, which means that the event tear-down
8067 * will stop on the parent's child_mutex that our caller is also holding
8068 */
8069 if (task == TASK_TOMBSTONE)
8070 return;
8071
8072 mm = get_task_mm(event->ctx->task);
8073 if (!mm)
8074 goto restart;
8075
8076 down_read(&mm->mmap_sem);
8077
8078 raw_spin_lock_irqsave(&ifh->lock, flags);
8079 list_for_each_entry(filter, &ifh->list, entry) {
8080 event->addr_filters_offs[count] = 0;
8081
Mathieu Poirier99f5bc92016-07-18 10:43:07 -06008082 /*
8083 * Adjust base offset if the filter is associated to a binary
8084 * that needs to be mapped:
8085 */
8086 if (filter->inode)
Alexander Shishkin375637b2016-04-27 18:44:46 +03008087 event->addr_filters_offs[count] =
8088 perf_addr_filter_apply(filter, mm);
8089
8090 count++;
8091 }
8092
8093 event->addr_filters_gen++;
8094 raw_spin_unlock_irqrestore(&ifh->lock, flags);
8095
8096 up_read(&mm->mmap_sem);
8097
8098 mmput(mm);
8099
8100restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03008101 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008102}
8103
8104/*
8105 * Address range filtering: limiting the data to certain
8106 * instruction address ranges. Filters are ioctl()ed to us from
8107 * userspace as ascii strings.
8108 *
8109 * Filter string format:
8110 *
8111 * ACTION RANGE_SPEC
8112 * where ACTION is one of the
8113 * * "filter": limit the trace to this region
8114 * * "start": start tracing from this address
8115 * * "stop": stop tracing at this address/region;
8116 * RANGE_SPEC is
8117 * * for kernel addresses: <start address>[/<size>]
8118 * * for object files: <start address>[/<size>]@</path/to/object/file>
8119 *
8120 * if <size> is not specified, the range is treated as a single address.
8121 */
8122enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02008123 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03008124 IF_ACT_FILTER,
8125 IF_ACT_START,
8126 IF_ACT_STOP,
8127 IF_SRC_FILE,
8128 IF_SRC_KERNEL,
8129 IF_SRC_FILEADDR,
8130 IF_SRC_KERNELADDR,
8131};
8132
8133enum {
8134 IF_STATE_ACTION = 0,
8135 IF_STATE_SOURCE,
8136 IF_STATE_END,
8137};
8138
8139static const match_table_t if_tokens = {
8140 { IF_ACT_FILTER, "filter" },
8141 { IF_ACT_START, "start" },
8142 { IF_ACT_STOP, "stop" },
8143 { IF_SRC_FILE, "%u/%u@%s" },
8144 { IF_SRC_KERNEL, "%u/%u" },
8145 { IF_SRC_FILEADDR, "%u@%s" },
8146 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +02008147 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +03008148};
8149
8150/*
8151 * Address filter string parser
8152 */
8153static int
8154perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
8155 struct list_head *filters)
8156{
8157 struct perf_addr_filter *filter = NULL;
8158 char *start, *orig, *filename = NULL;
8159 struct path path;
8160 substring_t args[MAX_OPT_ARGS];
8161 int state = IF_STATE_ACTION, token;
8162 unsigned int kernel = 0;
8163 int ret = -EINVAL;
8164
8165 orig = fstr = kstrdup(fstr, GFP_KERNEL);
8166 if (!fstr)
8167 return -ENOMEM;
8168
8169 while ((start = strsep(&fstr, " ,\n")) != NULL) {
8170 ret = -EINVAL;
8171
8172 if (!*start)
8173 continue;
8174
8175 /* filter definition begins */
8176 if (state == IF_STATE_ACTION) {
8177 filter = perf_addr_filter_new(event, filters);
8178 if (!filter)
8179 goto fail;
8180 }
8181
8182 token = match_token(start, if_tokens, args);
8183 switch (token) {
8184 case IF_ACT_FILTER:
8185 case IF_ACT_START:
8186 filter->filter = 1;
8187
8188 case IF_ACT_STOP:
8189 if (state != IF_STATE_ACTION)
8190 goto fail;
8191
8192 state = IF_STATE_SOURCE;
8193 break;
8194
8195 case IF_SRC_KERNELADDR:
8196 case IF_SRC_KERNEL:
8197 kernel = 1;
8198
8199 case IF_SRC_FILEADDR:
8200 case IF_SRC_FILE:
8201 if (state != IF_STATE_SOURCE)
8202 goto fail;
8203
8204 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
8205 filter->range = 1;
8206
8207 *args[0].to = 0;
8208 ret = kstrtoul(args[0].from, 0, &filter->offset);
8209 if (ret)
8210 goto fail;
8211
8212 if (filter->range) {
8213 *args[1].to = 0;
8214 ret = kstrtoul(args[1].from, 0, &filter->size);
8215 if (ret)
8216 goto fail;
8217 }
8218
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06008219 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
8220 int fpos = filter->range ? 2 : 1;
8221
8222 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008223 if (!filename) {
8224 ret = -ENOMEM;
8225 goto fail;
8226 }
8227 }
8228
8229 state = IF_STATE_END;
8230 break;
8231
8232 default:
8233 goto fail;
8234 }
8235
8236 /*
8237 * Filter definition is fully parsed, validate and install it.
8238 * Make sure that it doesn't contradict itself or the event's
8239 * attribute.
8240 */
8241 if (state == IF_STATE_END) {
8242 if (kernel && event->attr.exclude_kernel)
8243 goto fail;
8244
8245 if (!kernel) {
8246 if (!filename)
8247 goto fail;
8248
8249 /* look up the path and grab its inode */
8250 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
8251 if (ret)
8252 goto fail_free_name;
8253
8254 filter->inode = igrab(d_inode(path.dentry));
8255 path_put(&path);
8256 kfree(filename);
8257 filename = NULL;
8258
8259 ret = -EINVAL;
8260 if (!filter->inode ||
8261 !S_ISREG(filter->inode->i_mode))
8262 /* free_filters_list() will iput() */
8263 goto fail;
8264 }
8265
8266 /* ready to consume more filters */
8267 state = IF_STATE_ACTION;
8268 filter = NULL;
8269 }
8270 }
8271
8272 if (state != IF_STATE_ACTION)
8273 goto fail;
8274
8275 kfree(orig);
8276
8277 return 0;
8278
8279fail_free_name:
8280 kfree(filename);
8281fail:
8282 free_filters_list(filters);
8283 kfree(orig);
8284
8285 return ret;
8286}
8287
8288static int
8289perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
8290{
8291 LIST_HEAD(filters);
8292 int ret;
8293
8294 /*
8295 * Since this is called in perf_ioctl() path, we're already holding
8296 * ctx::mutex.
8297 */
8298 lockdep_assert_held(&event->ctx->mutex);
8299
8300 if (WARN_ON_ONCE(event->parent))
8301 return -EINVAL;
8302
8303 /*
8304 * For now, we only support filtering in per-task events; doing so
8305 * for CPU-wide events requires additional context switching trickery,
8306 * since same object code will be mapped at different virtual
8307 * addresses in different processes.
8308 */
8309 if (!event->ctx->task)
8310 return -EOPNOTSUPP;
8311
8312 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
8313 if (ret)
8314 return ret;
8315
8316 ret = event->pmu->addr_filters_validate(&filters);
8317 if (ret) {
8318 free_filters_list(&filters);
8319 return ret;
8320 }
8321
8322 /* remove existing filters, if any */
8323 perf_addr_filters_splice(event, &filters);
8324
8325 /* install new filters */
8326 perf_event_for_each_child(event, perf_event_addr_filters_apply);
8327
8328 return ret;
8329}
8330
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008331static int perf_event_set_filter(struct perf_event *event, void __user *arg)
8332{
8333 char *filter_str;
8334 int ret = -EINVAL;
8335
Alexander Shishkin375637b2016-04-27 18:44:46 +03008336 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
8337 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
8338 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008339 return -EINVAL;
8340
8341 filter_str = strndup_user(arg, PAGE_SIZE);
8342 if (IS_ERR(filter_str))
8343 return PTR_ERR(filter_str);
8344
8345 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
8346 event->attr.type == PERF_TYPE_TRACEPOINT)
8347 ret = ftrace_profile_set_filter(event, event->attr.config,
8348 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008349 else if (has_addr_filter(event))
8350 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008351
8352 kfree(filter_str);
8353 return ret;
8354}
8355
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008356/*
8357 * hrtimer based swevent callback
8358 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008359
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008360static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008361{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008362 enum hrtimer_restart ret = HRTIMER_RESTART;
8363 struct perf_sample_data data;
8364 struct pt_regs *regs;
8365 struct perf_event *event;
8366 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008367
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008368 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008369
8370 if (event->state != PERF_EVENT_STATE_ACTIVE)
8371 return HRTIMER_NORESTART;
8372
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008373 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008374
Robert Richterfd0d0002012-04-02 20:19:08 +02008375 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008376 regs = get_irq_regs();
8377
8378 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08008379 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02008380 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008381 ret = HRTIMER_NORESTART;
8382 }
8383
8384 period = max_t(u64, 10000, event->hw.sample_period);
8385 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
8386
8387 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008388}
8389
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008390static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008391{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008392 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008393 s64 period;
8394
8395 if (!is_sampling_event(event))
8396 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008397
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008398 period = local64_read(&hwc->period_left);
8399 if (period) {
8400 if (period < 0)
8401 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008402
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008403 local64_set(&hwc->period_left, 0);
8404 } else {
8405 period = max_t(u64, 10000, hwc->sample_period);
8406 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00008407 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8408 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008409}
8410
8411static void perf_swevent_cancel_hrtimer(struct perf_event *event)
8412{
8413 struct hw_perf_event *hwc = &event->hw;
8414
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008415 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008416 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008417 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008418
8419 hrtimer_cancel(&hwc->hrtimer);
8420 }
8421}
8422
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008423static void perf_swevent_init_hrtimer(struct perf_event *event)
8424{
8425 struct hw_perf_event *hwc = &event->hw;
8426
8427 if (!is_sampling_event(event))
8428 return;
8429
8430 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8431 hwc->hrtimer.function = perf_swevent_hrtimer;
8432
8433 /*
8434 * Since hrtimers have a fixed rate, we can do a static freq->period
8435 * mapping and avoid the whole period adjust feedback stuff.
8436 */
8437 if (event->attr.freq) {
8438 long freq = event->attr.sample_freq;
8439
8440 event->attr.sample_period = NSEC_PER_SEC / freq;
8441 hwc->sample_period = event->attr.sample_period;
8442 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008443 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008444 event->attr.freq = 0;
8445 }
8446}
8447
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008448/*
8449 * Software event: cpu wall time clock
8450 */
8451
8452static void cpu_clock_event_update(struct perf_event *event)
8453{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008454 s64 prev;
8455 u64 now;
8456
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008457 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008458 prev = local64_xchg(&event->hw.prev_count, now);
8459 local64_add(now - prev, &event->count);
8460}
8461
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008462static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008463{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008464 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008465 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008466}
8467
8468static void cpu_clock_event_stop(struct perf_event *event, int flags)
8469{
8470 perf_swevent_cancel_hrtimer(event);
8471 cpu_clock_event_update(event);
8472}
8473
8474static int cpu_clock_event_add(struct perf_event *event, int flags)
8475{
8476 if (flags & PERF_EF_START)
8477 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008478 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008479
8480 return 0;
8481}
8482
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008483static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008484{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008485 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008486}
8487
8488static void cpu_clock_event_read(struct perf_event *event)
8489{
8490 cpu_clock_event_update(event);
8491}
8492
8493static int cpu_clock_event_init(struct perf_event *event)
8494{
8495 if (event->attr.type != PERF_TYPE_SOFTWARE)
8496 return -ENOENT;
8497
8498 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8499 return -ENOENT;
8500
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008501 /*
8502 * no branch sampling for software events
8503 */
8504 if (has_branch_stack(event))
8505 return -EOPNOTSUPP;
8506
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008507 perf_swevent_init_hrtimer(event);
8508
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008509 return 0;
8510}
8511
8512static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008513 .task_ctx_nr = perf_sw_context,
8514
Peter Zijlstra34f43922015-02-20 14:05:38 +01008515 .capabilities = PERF_PMU_CAP_NO_NMI,
8516
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008517 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008518 .add = cpu_clock_event_add,
8519 .del = cpu_clock_event_del,
8520 .start = cpu_clock_event_start,
8521 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008522 .read = cpu_clock_event_read,
8523};
8524
8525/*
8526 * Software event: task time clock
8527 */
8528
8529static void task_clock_event_update(struct perf_event *event, u64 now)
8530{
8531 u64 prev;
8532 s64 delta;
8533
8534 prev = local64_xchg(&event->hw.prev_count, now);
8535 delta = now - prev;
8536 local64_add(delta, &event->count);
8537}
8538
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008539static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008540{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008541 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008542 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008543}
8544
8545static void task_clock_event_stop(struct perf_event *event, int flags)
8546{
8547 perf_swevent_cancel_hrtimer(event);
8548 task_clock_event_update(event, event->ctx->time);
8549}
8550
8551static int task_clock_event_add(struct perf_event *event, int flags)
8552{
8553 if (flags & PERF_EF_START)
8554 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008555 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008556
8557 return 0;
8558}
8559
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008560static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008561{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008562 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008563}
8564
8565static void task_clock_event_read(struct perf_event *event)
8566{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008567 u64 now = perf_clock();
8568 u64 delta = now - event->ctx->timestamp;
8569 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008570
8571 task_clock_event_update(event, time);
8572}
8573
8574static int task_clock_event_init(struct perf_event *event)
8575{
8576 if (event->attr.type != PERF_TYPE_SOFTWARE)
8577 return -ENOENT;
8578
8579 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8580 return -ENOENT;
8581
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008582 /*
8583 * no branch sampling for software events
8584 */
8585 if (has_branch_stack(event))
8586 return -EOPNOTSUPP;
8587
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008588 perf_swevent_init_hrtimer(event);
8589
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008590 return 0;
8591}
8592
8593static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008594 .task_ctx_nr = perf_sw_context,
8595
Peter Zijlstra34f43922015-02-20 14:05:38 +01008596 .capabilities = PERF_PMU_CAP_NO_NMI,
8597
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008598 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008599 .add = task_clock_event_add,
8600 .del = task_clock_event_del,
8601 .start = task_clock_event_start,
8602 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008603 .read = task_clock_event_read,
8604};
8605
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008606static void perf_pmu_nop_void(struct pmu *pmu)
8607{
8608}
8609
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008610static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8611{
8612}
8613
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008614static int perf_pmu_nop_int(struct pmu *pmu)
8615{
8616 return 0;
8617}
8618
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008619static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008620
8621static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008622{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008623 __this_cpu_write(nop_txn_flags, flags);
8624
8625 if (flags & ~PERF_PMU_TXN_ADD)
8626 return;
8627
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008628 perf_pmu_disable(pmu);
8629}
8630
8631static int perf_pmu_commit_txn(struct pmu *pmu)
8632{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008633 unsigned int flags = __this_cpu_read(nop_txn_flags);
8634
8635 __this_cpu_write(nop_txn_flags, 0);
8636
8637 if (flags & ~PERF_PMU_TXN_ADD)
8638 return 0;
8639
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008640 perf_pmu_enable(pmu);
8641 return 0;
8642}
8643
8644static void perf_pmu_cancel_txn(struct pmu *pmu)
8645{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008646 unsigned int flags = __this_cpu_read(nop_txn_flags);
8647
8648 __this_cpu_write(nop_txn_flags, 0);
8649
8650 if (flags & ~PERF_PMU_TXN_ADD)
8651 return;
8652
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008653 perf_pmu_enable(pmu);
8654}
8655
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008656static int perf_event_idx_default(struct perf_event *event)
8657{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008658 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008659}
8660
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008661/*
8662 * Ensures all contexts with the same task_ctx_nr have the same
8663 * pmu_cpu_context too.
8664 */
Mark Rutland9e317042014-02-10 17:44:18 +00008665static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008666{
8667 struct pmu *pmu;
8668
8669 if (ctxn < 0)
8670 return NULL;
8671
8672 list_for_each_entry(pmu, &pmus, entry) {
8673 if (pmu->task_ctx_nr == ctxn)
8674 return pmu->pmu_cpu_context;
8675 }
8676
8677 return NULL;
8678}
8679
Peter Zijlstra51676952010-12-07 14:18:20 +01008680static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008681{
Peter Zijlstra51676952010-12-07 14:18:20 +01008682 int cpu;
8683
8684 for_each_possible_cpu(cpu) {
8685 struct perf_cpu_context *cpuctx;
8686
8687 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8688
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008689 if (cpuctx->unique_pmu == old_pmu)
8690 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008691 }
8692}
8693
8694static void free_pmu_context(struct pmu *pmu)
8695{
8696 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008697
8698 mutex_lock(&pmus_lock);
8699 /*
8700 * Like a real lame refcount.
8701 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008702 list_for_each_entry(i, &pmus, entry) {
8703 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8704 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008705 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008706 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008707 }
8708
Peter Zijlstra51676952010-12-07 14:18:20 +01008709 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008710out:
8711 mutex_unlock(&pmus_lock);
8712}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008713
8714/*
8715 * Let userspace know that this PMU supports address range filtering:
8716 */
8717static ssize_t nr_addr_filters_show(struct device *dev,
8718 struct device_attribute *attr,
8719 char *page)
8720{
8721 struct pmu *pmu = dev_get_drvdata(dev);
8722
8723 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8724}
8725DEVICE_ATTR_RO(nr_addr_filters);
8726
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008727static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008728
Peter Zijlstraabe43402010-11-17 23:17:37 +01008729static ssize_t
8730type_show(struct device *dev, struct device_attribute *attr, char *page)
8731{
8732 struct pmu *pmu = dev_get_drvdata(dev);
8733
8734 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8735}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008736static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008737
Stephane Eranian62b85632013-04-03 14:21:34 +02008738static ssize_t
8739perf_event_mux_interval_ms_show(struct device *dev,
8740 struct device_attribute *attr,
8741 char *page)
8742{
8743 struct pmu *pmu = dev_get_drvdata(dev);
8744
8745 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8746}
8747
Peter Zijlstra272325c2015-04-15 11:41:58 +02008748static DEFINE_MUTEX(mux_interval_mutex);
8749
Stephane Eranian62b85632013-04-03 14:21:34 +02008750static ssize_t
8751perf_event_mux_interval_ms_store(struct device *dev,
8752 struct device_attribute *attr,
8753 const char *buf, size_t count)
8754{
8755 struct pmu *pmu = dev_get_drvdata(dev);
8756 int timer, cpu, ret;
8757
8758 ret = kstrtoint(buf, 0, &timer);
8759 if (ret)
8760 return ret;
8761
8762 if (timer < 1)
8763 return -EINVAL;
8764
8765 /* same value, noting to do */
8766 if (timer == pmu->hrtimer_interval_ms)
8767 return count;
8768
Peter Zijlstra272325c2015-04-15 11:41:58 +02008769 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008770 pmu->hrtimer_interval_ms = timer;
8771
8772 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008773 get_online_cpus();
8774 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008775 struct perf_cpu_context *cpuctx;
8776 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8777 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8778
Peter Zijlstra272325c2015-04-15 11:41:58 +02008779 cpu_function_call(cpu,
8780 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008781 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008782 put_online_cpus();
8783 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008784
8785 return count;
8786}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008787static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008788
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008789static struct attribute *pmu_dev_attrs[] = {
8790 &dev_attr_type.attr,
8791 &dev_attr_perf_event_mux_interval_ms.attr,
8792 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008793};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008794ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008795
8796static int pmu_bus_running;
8797static struct bus_type pmu_bus = {
8798 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008799 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008800};
8801
8802static void pmu_dev_release(struct device *dev)
8803{
8804 kfree(dev);
8805}
8806
8807static int pmu_dev_alloc(struct pmu *pmu)
8808{
8809 int ret = -ENOMEM;
8810
8811 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8812 if (!pmu->dev)
8813 goto out;
8814
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008815 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008816 device_initialize(pmu->dev);
8817 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8818 if (ret)
8819 goto free_dev;
8820
8821 dev_set_drvdata(pmu->dev, pmu);
8822 pmu->dev->bus = &pmu_bus;
8823 pmu->dev->release = pmu_dev_release;
8824 ret = device_add(pmu->dev);
8825 if (ret)
8826 goto free_dev;
8827
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008828 /* For PMUs with address filters, throw in an extra attribute: */
8829 if (pmu->nr_addr_filters)
8830 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8831
8832 if (ret)
8833 goto del_dev;
8834
Peter Zijlstraabe43402010-11-17 23:17:37 +01008835out:
8836 return ret;
8837
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008838del_dev:
8839 device_del(pmu->dev);
8840
Peter Zijlstraabe43402010-11-17 23:17:37 +01008841free_dev:
8842 put_device(pmu->dev);
8843 goto out;
8844}
8845
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008846static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008847static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008848
Mischa Jonker03d8e802013-06-04 11:45:48 +02008849int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008850{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008851 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008852
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008853 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008854 ret = -ENOMEM;
8855 pmu->pmu_disable_count = alloc_percpu(int);
8856 if (!pmu->pmu_disable_count)
8857 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008858
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008859 pmu->type = -1;
8860 if (!name)
8861 goto skip_type;
8862 pmu->name = name;
8863
8864 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008865 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8866 if (type < 0) {
8867 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008868 goto free_pdc;
8869 }
8870 }
8871 pmu->type = type;
8872
Peter Zijlstraabe43402010-11-17 23:17:37 +01008873 if (pmu_bus_running) {
8874 ret = pmu_dev_alloc(pmu);
8875 if (ret)
8876 goto free_idr;
8877 }
8878
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008879skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008880 if (pmu->task_ctx_nr == perf_hw_context) {
8881 static int hw_context_taken = 0;
8882
Mark Rutland5101ef22016-04-26 11:33:46 +01008883 /*
8884 * Other than systems with heterogeneous CPUs, it never makes
8885 * sense for two PMUs to share perf_hw_context. PMUs which are
8886 * uncore must use perf_invalid_context.
8887 */
8888 if (WARN_ON_ONCE(hw_context_taken &&
8889 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008890 pmu->task_ctx_nr = perf_invalid_context;
8891
8892 hw_context_taken = 1;
8893 }
8894
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008895 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8896 if (pmu->pmu_cpu_context)
8897 goto got_cpu_context;
8898
Wei Yongjunc4814202013-04-12 11:05:54 +08008899 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008900 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8901 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008902 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008903
8904 for_each_possible_cpu(cpu) {
8905 struct perf_cpu_context *cpuctx;
8906
8907 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008908 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008909 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008910 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008911 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008912
Peter Zijlstra272325c2015-04-15 11:41:58 +02008913 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008914
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008915 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008916 }
8917
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008918got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008919 if (!pmu->start_txn) {
8920 if (pmu->pmu_enable) {
8921 /*
8922 * If we have pmu_enable/pmu_disable calls, install
8923 * transaction stubs that use that to try and batch
8924 * hardware accesses.
8925 */
8926 pmu->start_txn = perf_pmu_start_txn;
8927 pmu->commit_txn = perf_pmu_commit_txn;
8928 pmu->cancel_txn = perf_pmu_cancel_txn;
8929 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008930 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008931 pmu->commit_txn = perf_pmu_nop_int;
8932 pmu->cancel_txn = perf_pmu_nop_void;
8933 }
8934 }
8935
8936 if (!pmu->pmu_enable) {
8937 pmu->pmu_enable = perf_pmu_nop_void;
8938 pmu->pmu_disable = perf_pmu_nop_void;
8939 }
8940
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008941 if (!pmu->event_idx)
8942 pmu->event_idx = perf_event_idx_default;
8943
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008944 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008945 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008946 ret = 0;
8947unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008948 mutex_unlock(&pmus_lock);
8949
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008950 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008951
Peter Zijlstraabe43402010-11-17 23:17:37 +01008952free_dev:
8953 device_del(pmu->dev);
8954 put_device(pmu->dev);
8955
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008956free_idr:
8957 if (pmu->type >= PERF_TYPE_MAX)
8958 idr_remove(&pmu_idr, pmu->type);
8959
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008960free_pdc:
8961 free_percpu(pmu->pmu_disable_count);
8962 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008963}
Yan, Zhengc464c762014-03-18 16:56:41 +08008964EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008965
8966void perf_pmu_unregister(struct pmu *pmu)
8967{
Jiri Olsa09338402016-10-20 13:10:11 +02008968 int remove_device;
8969
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008970 mutex_lock(&pmus_lock);
Jiri Olsa09338402016-10-20 13:10:11 +02008971 remove_device = pmu_bus_running;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008972 list_del_rcu(&pmu->entry);
8973 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008974
8975 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02008976 * We dereference the pmu list under both SRCU and regular RCU, so
8977 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008978 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008979 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02008980 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008981
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008982 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008983 if (pmu->type >= PERF_TYPE_MAX)
8984 idr_remove(&pmu_idr, pmu->type);
Jiri Olsa09338402016-10-20 13:10:11 +02008985 if (remove_device) {
8986 if (pmu->nr_addr_filters)
8987 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
8988 device_del(pmu->dev);
8989 put_device(pmu->dev);
8990 }
Peter Zijlstra51676952010-12-07 14:18:20 +01008991 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008992}
Yan, Zhengc464c762014-03-18 16:56:41 +08008993EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008994
Mark Rutlandcc34b982015-01-07 14:56:51 +00008995static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
8996{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008997 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00008998 int ret;
8999
9000 if (!try_module_get(pmu->module))
9001 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009002
9003 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02009004 /*
9005 * This ctx->mutex can nest when we're called through
9006 * inheritance. See the perf_event_ctx_lock_nested() comment.
9007 */
9008 ctx = perf_event_ctx_lock_nested(event->group_leader,
9009 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009010 BUG_ON(!ctx);
9011 }
9012
Mark Rutlandcc34b982015-01-07 14:56:51 +00009013 event->pmu = pmu;
9014 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009015
9016 if (ctx)
9017 perf_event_ctx_unlock(event->group_leader, ctx);
9018
Mark Rutlandcc34b982015-01-07 14:56:51 +00009019 if (ret)
9020 module_put(pmu->module);
9021
9022 return ret;
9023}
9024
Geliang Tang18ab2cd2015-09-27 23:25:50 +08009025static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009026{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009027 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009028 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08009029 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009030
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009031 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009032
9033 rcu_read_lock();
9034 pmu = idr_find(&pmu_idr, event->attr.type);
9035 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08009036 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009037 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08009038 if (ret)
9039 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009040 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08009041 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009042
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009043 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009044 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009045 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009046 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009047
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009048 if (ret != -ENOENT) {
9049 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009050 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009051 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009052 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009053 pmu = ERR_PTR(-ENOENT);
9054unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009055 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009056
9057 return pmu;
9058}
9059
Kan Liangf2fb6be2016-03-23 11:24:37 -07009060static void attach_sb_event(struct perf_event *event)
9061{
9062 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
9063
9064 raw_spin_lock(&pel->lock);
9065 list_add_rcu(&event->sb_list, &pel->list);
9066 raw_spin_unlock(&pel->lock);
9067}
9068
Peter Zijlstraaab5b712016-05-12 17:26:46 +02009069/*
9070 * We keep a list of all !task (and therefore per-cpu) events
9071 * that need to receive side-band records.
9072 *
9073 * This avoids having to scan all the various PMU per-cpu contexts
9074 * looking for them.
9075 */
Kan Liangf2fb6be2016-03-23 11:24:37 -07009076static void account_pmu_sb_event(struct perf_event *event)
9077{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07009078 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -07009079 attach_sb_event(event);
9080}
9081
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009082static void account_event_cpu(struct perf_event *event, int cpu)
9083{
9084 if (event->parent)
9085 return;
9086
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009087 if (is_cgroup_event(event))
9088 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
9089}
9090
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009091/* Freq events need the tick to stay alive (see perf_event_task_tick). */
9092static void account_freq_event_nohz(void)
9093{
9094#ifdef CONFIG_NO_HZ_FULL
9095 /* Lock so we don't race with concurrent unaccount */
9096 spin_lock(&nr_freq_lock);
9097 if (atomic_inc_return(&nr_freq_events) == 1)
9098 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
9099 spin_unlock(&nr_freq_lock);
9100#endif
9101}
9102
9103static void account_freq_event(void)
9104{
9105 if (tick_nohz_full_enabled())
9106 account_freq_event_nohz();
9107 else
9108 atomic_inc(&nr_freq_events);
9109}
9110
9111
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009112static void account_event(struct perf_event *event)
9113{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009114 bool inc = false;
9115
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009116 if (event->parent)
9117 return;
9118
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009119 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009120 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009121 if (event->attr.mmap || event->attr.mmap_data)
9122 atomic_inc(&nr_mmap_events);
9123 if (event->attr.comm)
9124 atomic_inc(&nr_comm_events);
9125 if (event->attr.task)
9126 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009127 if (event->attr.freq)
9128 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03009129 if (event->attr.context_switch) {
9130 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009131 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03009132 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009133 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009134 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009135 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009136 inc = true;
9137
Peter Zijlstra9107c892016-02-24 18:45:45 +01009138 if (inc) {
9139 if (atomic_inc_not_zero(&perf_sched_count))
9140 goto enabled;
9141
9142 mutex_lock(&perf_sched_mutex);
9143 if (!atomic_read(&perf_sched_count)) {
9144 static_branch_enable(&perf_sched_events);
9145 /*
9146 * Guarantee that all CPUs observe they key change and
9147 * call the perf scheduling hooks before proceeding to
9148 * install events that need them.
9149 */
9150 synchronize_sched();
9151 }
9152 /*
9153 * Now that we have waited for the sync_sched(), allow further
9154 * increments to by-pass the mutex.
9155 */
9156 atomic_inc(&perf_sched_count);
9157 mutex_unlock(&perf_sched_mutex);
9158 }
9159enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009160
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009161 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07009162
9163 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009164}
9165
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009166/*
9167 * Allocate and initialize a event structure
9168 */
9169static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009170perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009171 struct task_struct *task,
9172 struct perf_event *group_leader,
9173 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009174 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00009175 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009176{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009177 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009178 struct perf_event *event;
9179 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009180 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009181
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009182 if ((unsigned)cpu >= nr_cpu_ids) {
9183 if (!task || cpu != -1)
9184 return ERR_PTR(-EINVAL);
9185 }
9186
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009187 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009188 if (!event)
9189 return ERR_PTR(-ENOMEM);
9190
9191 /*
9192 * Single events are their own group leaders, with an
9193 * empty sibling list:
9194 */
9195 if (!group_leader)
9196 group_leader = event;
9197
9198 mutex_init(&event->child_mutex);
9199 INIT_LIST_HEAD(&event->child_list);
9200
9201 INIT_LIST_HEAD(&event->group_entry);
9202 INIT_LIST_HEAD(&event->event_entry);
9203 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009204 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01009205 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009206 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01009207 INIT_HLIST_NODE(&event->hlist_entry);
9208
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009209
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009210 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08009211 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009212
9213 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009214 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009215
Al Viroa6fa9412012-08-20 14:59:25 +01009216 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009217 event->cpu = cpu;
9218 event->attr = *attr;
9219 event->group_leader = group_leader;
9220 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009221 event->oncpu = -1;
9222
9223 event->parent = parent_event;
9224
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08009225 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009226 event->id = atomic64_inc_return(&perf_event_id);
9227
9228 event->state = PERF_EVENT_STATE_INACTIVE;
9229
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009230 if (task) {
9231 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009232 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009233 * XXX pmu::event_init needs to know what task to account to
9234 * and we cannot use the ctx information because we need the
9235 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009236 */
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009237 get_task_struct(task);
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009238 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009239 }
9240
Peter Zijlstra34f43922015-02-20 14:05:38 +01009241 event->clock = &local_clock;
9242 if (parent_event)
9243 event->clock = parent_event->clock;
9244
Avi Kivity4dc0da82011-06-29 18:42:35 +03009245 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01009246 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009247 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +02009248#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009249 if (overflow_handler == bpf_overflow_handler) {
9250 struct bpf_prog *prog = bpf_prog_inc(parent_event->prog);
9251
9252 if (IS_ERR(prog)) {
9253 err = PTR_ERR(prog);
9254 goto err_ns;
9255 }
9256 event->prog = prog;
9257 event->orig_overflow_handler =
9258 parent_event->orig_overflow_handler;
9259 }
9260#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +03009261 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009262
Wang Nan18794452016-03-28 06:41:30 +00009263 if (overflow_handler) {
9264 event->overflow_handler = overflow_handler;
9265 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00009266 } else if (is_write_backward(event)){
9267 event->overflow_handler = perf_event_output_backward;
9268 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00009269 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00009270 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00009271 event->overflow_handler_context = NULL;
9272 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02009273
Jiri Olsa0231bb52013-02-01 11:23:45 +01009274 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009275
9276 pmu = NULL;
9277
9278 hwc = &event->hw;
9279 hwc->sample_period = attr->sample_period;
9280 if (attr->freq && attr->sample_freq)
9281 hwc->sample_period = 1;
9282 hwc->last_period = hwc->sample_period;
9283
Peter Zijlstrae7850592010-05-21 14:43:08 +02009284 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009285
9286 /*
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009287 * We currently do not support PERF_SAMPLE_READ on inherited events.
9288 * See perf_output_read().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009289 */
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009290 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009291 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009292
Yan, Zhenga46a2302014-11-04 21:56:06 -05009293 if (!has_branch_stack(event))
9294 event->attr.branch_sample_type = 0;
9295
Matt Fleming79dff512015-01-23 18:45:42 +00009296 if (cgroup_fd != -1) {
9297 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
9298 if (err)
9299 goto err_ns;
9300 }
9301
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009302 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009303 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009304 goto err_ns;
9305 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009306 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009307 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009308 }
9309
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009310 err = exclusive_event_init(event);
9311 if (err)
9312 goto err_pmu;
9313
Alexander Shishkin375637b2016-04-27 18:44:46 +03009314 if (has_addr_filter(event)) {
9315 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
9316 sizeof(unsigned long),
9317 GFP_KERNEL);
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009318 if (!event->addr_filters_offs) {
9319 err = -ENOMEM;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009320 goto err_per_task;
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009321 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009322
9323 /* force hw sync on the address filters */
9324 event->addr_filters_gen = 1;
9325 }
9326
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009327 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009328 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009329 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009330 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009331 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01009332 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009333 }
9334
Alexander Shishkin927a5572016-03-02 13:24:14 +02009335 /* symmetric to unaccount_event() in _free_event() */
9336 account_event(event);
9337
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009338 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009339
Alexander Shishkin375637b2016-04-27 18:44:46 +03009340err_addr_filters:
9341 kfree(event->addr_filters_offs);
9342
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009343err_per_task:
9344 exclusive_event_destroy(event);
9345
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009346err_pmu:
9347 if (event->destroy)
9348 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08009349 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009350err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00009351 if (is_cgroup_event(event))
9352 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009353 if (event->ns)
9354 put_pid_ns(event->ns);
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009355 if (event->hw.target)
9356 put_task_struct(event->hw.target);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009357 kfree(event);
9358
9359 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009360}
9361
9362static int perf_copy_attr(struct perf_event_attr __user *uattr,
9363 struct perf_event_attr *attr)
9364{
9365 u32 size;
9366 int ret;
9367
9368 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
9369 return -EFAULT;
9370
9371 /*
9372 * zero the full structure, so that a short copy will be nice.
9373 */
9374 memset(attr, 0, sizeof(*attr));
9375
9376 ret = get_user(size, &uattr->size);
9377 if (ret)
9378 return ret;
9379
9380 if (size > PAGE_SIZE) /* silly large */
9381 goto err_size;
9382
9383 if (!size) /* abi compat */
9384 size = PERF_ATTR_SIZE_VER0;
9385
9386 if (size < PERF_ATTR_SIZE_VER0)
9387 goto err_size;
9388
9389 /*
9390 * If we're handed a bigger struct than we know of,
9391 * ensure all the unknown bits are 0 - i.e. new
9392 * user-space does not rely on any kernel feature
9393 * extensions we dont know about yet.
9394 */
9395 if (size > sizeof(*attr)) {
9396 unsigned char __user *addr;
9397 unsigned char __user *end;
9398 unsigned char val;
9399
9400 addr = (void __user *)uattr + sizeof(*attr);
9401 end = (void __user *)uattr + size;
9402
9403 for (; addr < end; addr++) {
9404 ret = get_user(val, addr);
9405 if (ret)
9406 return ret;
9407 if (val)
9408 goto err_size;
9409 }
9410 size = sizeof(*attr);
9411 }
9412
9413 ret = copy_from_user(attr, uattr, size);
9414 if (ret)
9415 return -EFAULT;
9416
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05309417 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009418 return -EINVAL;
9419
9420 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
9421 return -EINVAL;
9422
9423 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
9424 return -EINVAL;
9425
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009426 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
9427 u64 mask = attr->branch_sample_type;
9428
9429 /* only using defined bits */
9430 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
9431 return -EINVAL;
9432
9433 /* at least one branch bit must be set */
9434 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9435 return -EINVAL;
9436
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009437 /* propagate priv level, when not set for branch */
9438 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9439
9440 /* exclude_kernel checked on syscall entry */
9441 if (!attr->exclude_kernel)
9442 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9443
9444 if (!attr->exclude_user)
9445 mask |= PERF_SAMPLE_BRANCH_USER;
9446
9447 if (!attr->exclude_hv)
9448 mask |= PERF_SAMPLE_BRANCH_HV;
9449 /*
9450 * adjust user setting (for HW filter setup)
9451 */
9452 attr->branch_sample_type = mask;
9453 }
Stephane Eraniane7122092013-06-06 11:02:04 +02009454 /* privileged levels capture (kernel, hv): check permissions */
9455 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02009456 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9457 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009458 }
Jiri Olsa40189942012-08-07 15:20:37 +02009459
Jiri Olsac5ebced2012-08-07 15:20:40 +02009460 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02009461 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02009462 if (ret)
9463 return ret;
9464 }
9465
9466 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9467 if (!arch_perf_have_user_stack_dump())
9468 return -ENOSYS;
9469
9470 /*
9471 * We have __u32 type for the size, but so far
9472 * we can only use __u16 as maximum due to the
9473 * __u16 sample size limit.
9474 */
9475 if (attr->sample_stack_user >= USHRT_MAX)
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009476 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009477 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009478 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009479 }
Jiri Olsa40189942012-08-07 15:20:37 +02009480
Stephane Eranian60e23642014-09-24 13:48:37 +02009481 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9482 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009483out:
9484 return ret;
9485
9486err_size:
9487 put_user(sizeof(*attr), &uattr->size);
9488 ret = -E2BIG;
9489 goto out;
9490}
9491
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009492static int
9493perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009494{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009495 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009496 int ret = -EINVAL;
9497
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009498 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009499 goto set;
9500
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009501 /* don't allow circular references */
9502 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009503 goto out;
9504
Peter Zijlstra0f139302010-05-20 14:35:15 +02009505 /*
9506 * Don't allow cross-cpu buffers
9507 */
9508 if (output_event->cpu != event->cpu)
9509 goto out;
9510
9511 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009512 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009513 */
9514 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9515 goto out;
9516
Peter Zijlstra34f43922015-02-20 14:05:38 +01009517 /*
9518 * Mixing clocks in the same buffer is trouble you don't need.
9519 */
9520 if (output_event->clock != event->clock)
9521 goto out;
9522
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009523 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009524 * Either writing ring buffer from beginning or from end.
9525 * Mixing is not allowed.
9526 */
9527 if (is_write_backward(output_event) != is_write_backward(event))
9528 goto out;
9529
9530 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009531 * If both events generate aux data, they must be on the same PMU
9532 */
9533 if (has_aux(event) && has_aux(output_event) &&
9534 event->pmu != output_event->pmu)
9535 goto out;
9536
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009537set:
9538 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009539 /* Can't redirect output if we've got an active mmap() */
9540 if (atomic_read(&event->mmap_count))
9541 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009542
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009543 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009544 /* get the rb we want to redirect to */
9545 rb = ring_buffer_get(output_event);
9546 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009547 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009548 }
9549
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009550 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009551
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009552 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009553unlock:
9554 mutex_unlock(&event->mmap_mutex);
9555
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009556out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009557 return ret;
9558}
9559
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009560static void mutex_lock_double(struct mutex *a, struct mutex *b)
9561{
9562 if (b < a)
9563 swap(a, b);
9564
9565 mutex_lock(a);
9566 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9567}
9568
Peter Zijlstra34f43922015-02-20 14:05:38 +01009569static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9570{
9571 bool nmi_safe = false;
9572
9573 switch (clk_id) {
9574 case CLOCK_MONOTONIC:
9575 event->clock = &ktime_get_mono_fast_ns;
9576 nmi_safe = true;
9577 break;
9578
9579 case CLOCK_MONOTONIC_RAW:
9580 event->clock = &ktime_get_raw_fast_ns;
9581 nmi_safe = true;
9582 break;
9583
9584 case CLOCK_REALTIME:
9585 event->clock = &ktime_get_real_ns;
9586 break;
9587
9588 case CLOCK_BOOTTIME:
9589 event->clock = &ktime_get_boot_ns;
9590 break;
9591
9592 case CLOCK_TAI:
9593 event->clock = &ktime_get_tai_ns;
9594 break;
9595
9596 default:
9597 return -EINVAL;
9598 }
9599
9600 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9601 return -EINVAL;
9602
9603 return 0;
9604}
9605
Peter Zijlstra922813f2017-01-11 21:09:50 +01009606/*
9607 * Variation on perf_event_ctx_lock_nested(), except we take two context
9608 * mutexes.
9609 */
9610static struct perf_event_context *
9611__perf_event_ctx_lock_double(struct perf_event *group_leader,
9612 struct perf_event_context *ctx)
9613{
9614 struct perf_event_context *gctx;
9615
9616again:
9617 rcu_read_lock();
9618 gctx = READ_ONCE(group_leader->ctx);
9619 if (!atomic_inc_not_zero(&gctx->refcount)) {
9620 rcu_read_unlock();
9621 goto again;
9622 }
9623 rcu_read_unlock();
9624
9625 mutex_lock_double(&gctx->mutex, &ctx->mutex);
9626
9627 if (group_leader->ctx != gctx) {
9628 mutex_unlock(&ctx->mutex);
9629 mutex_unlock(&gctx->mutex);
9630 put_ctx(gctx);
9631 goto again;
9632 }
9633
9634 return gctx;
9635}
9636
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009637/**
9638 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9639 *
9640 * @attr_uptr: event_id type attributes for monitoring/sampling
9641 * @pid: target pid
9642 * @cpu: target cpu
9643 * @group_fd: group leader event fd
9644 */
9645SYSCALL_DEFINE5(perf_event_open,
9646 struct perf_event_attr __user *, attr_uptr,
9647 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9648{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009649 struct perf_event *group_leader = NULL, *output_event = NULL;
9650 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009651 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009652 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009653 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009654 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009655 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009656 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009657 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009658 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009659 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009660 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009661 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009662
9663 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009664 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009665 return -EINVAL;
9666
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -07009667 if (perf_paranoid_any() && !capable(CAP_SYS_ADMIN))
9668 return -EACCES;
9669
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009670 err = perf_copy_attr(attr_uptr, &attr);
9671 if (err)
9672 return err;
9673
9674 if (!attr.exclude_kernel) {
9675 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9676 return -EACCES;
9677 }
9678
9679 if (attr.freq) {
9680 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9681 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009682 } else {
9683 if (attr.sample_period & (1ULL << 63))
9684 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009685 }
9686
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009687 if (!attr.sample_max_stack)
9688 attr.sample_max_stack = sysctl_perf_event_max_stack;
9689
Stephane Eraniane5d13672011-02-14 11:20:01 +02009690 /*
9691 * In cgroup mode, the pid argument is used to pass the fd
9692 * opened to the cgroup directory in cgroupfs. The cpu argument
9693 * designates the cpu on which to monitor threads from that
9694 * cgroup.
9695 */
9696 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9697 return -EINVAL;
9698
Yann Droneauda21b0b32014-01-05 21:36:33 +01009699 if (flags & PERF_FLAG_FD_CLOEXEC)
9700 f_flags |= O_CLOEXEC;
9701
9702 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009703 if (event_fd < 0)
9704 return event_fd;
9705
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009706 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009707 err = perf_fget_light(group_fd, &group);
9708 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009709 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009710 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009711 if (flags & PERF_FLAG_FD_OUTPUT)
9712 output_event = group_leader;
9713 if (flags & PERF_FLAG_FD_NO_GROUP)
9714 group_leader = NULL;
9715 }
9716
Stephane Eraniane5d13672011-02-14 11:20:01 +02009717 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009718 task = find_lively_task_by_vpid(pid);
9719 if (IS_ERR(task)) {
9720 err = PTR_ERR(task);
9721 goto err_group_fd;
9722 }
9723 }
9724
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009725 if (task && group_leader &&
9726 group_leader->attr.inherit != attr.inherit) {
9727 err = -EINVAL;
9728 goto err_task;
9729 }
9730
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009731 get_online_cpus();
9732
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009733 if (task) {
9734 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9735 if (err)
9736 goto err_cpus;
9737
9738 /*
9739 * Reuse ptrace permission checks for now.
9740 *
9741 * We must hold cred_guard_mutex across this and any potential
9742 * perf_install_in_context() call for this new event to
9743 * serialize against exec() altering our credentials (and the
9744 * perf_event_exit_task() that could imply).
9745 */
9746 err = -EACCES;
9747 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9748 goto err_cred;
9749 }
9750
Matt Fleming79dff512015-01-23 18:45:42 +00009751 if (flags & PERF_FLAG_PID_CGROUP)
9752 cgroup_fd = pid;
9753
Avi Kivity4dc0da82011-06-29 18:42:35 +03009754 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009755 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009756 if (IS_ERR(event)) {
9757 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009758 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009759 }
9760
Vince Weaver53b25332014-05-16 17:12:12 -04009761 if (is_sampling_event(event)) {
9762 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +05309763 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -04009764 goto err_alloc;
9765 }
9766 }
9767
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009768 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009769 * Special case software events and allow them to be part of
9770 * any hardware group.
9771 */
9772 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009773
Peter Zijlstra34f43922015-02-20 14:05:38 +01009774 if (attr.use_clockid) {
9775 err = perf_event_set_clock(event, attr.clockid);
9776 if (err)
9777 goto err_alloc;
9778 }
9779
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009780 if (pmu->task_ctx_nr == perf_sw_context)
9781 event->event_caps |= PERF_EV_CAP_SOFTWARE;
9782
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009783 if (group_leader &&
9784 (is_software_event(event) != is_software_event(group_leader))) {
9785 if (is_software_event(event)) {
9786 /*
9787 * If event and group_leader are not both a software
9788 * event, and event is, then group leader is not.
9789 *
9790 * Allow the addition of software events to !software
9791 * groups, this is safe because software events never
9792 * fail to schedule.
9793 */
9794 pmu = group_leader->pmu;
9795 } else if (is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009796 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009797 /*
9798 * In case the group is a pure software group, and we
9799 * try to add a hardware event, move the whole group to
9800 * the hardware context.
9801 */
9802 move_group = 1;
9803 }
9804 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009805
9806 /*
9807 * Get the target context (task or percpu):
9808 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009809 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009810 if (IS_ERR(ctx)) {
9811 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009812 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009813 }
9814
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009815 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9816 err = -EBUSY;
9817 goto err_context;
9818 }
9819
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009820 /*
9821 * Look up the group leader (we will attach this event to it):
9822 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009823 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009824 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009825
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009826 /*
9827 * Do not allow a recursive hierarchy (this new sibling
9828 * becoming part of another group-sibling):
9829 */
9830 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009831 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009832
9833 /* All events in a group should have the same clock */
9834 if (group_leader->clock != event->clock)
9835 goto err_context;
9836
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009837 /*
Mark Rutlandbde66082017-06-22 15:41:38 +01009838 * Make sure we're both events for the same CPU;
9839 * grouping events for different CPUs is broken; since
9840 * you can never concurrently schedule them anyhow.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009841 */
Mark Rutlandbde66082017-06-22 15:41:38 +01009842 if (group_leader->cpu != event->cpu)
9843 goto err_context;
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009844
Mark Rutlandbde66082017-06-22 15:41:38 +01009845 /*
9846 * Make sure we're both on the same task, or both
9847 * per-CPU events.
9848 */
9849 if (group_leader->ctx->task != ctx->task)
9850 goto err_context;
9851
9852 /*
9853 * Do not allow to attach to a group in a different task
9854 * or CPU context. If we're moving SW events, we'll fix
9855 * this up later, so allow that.
9856 */
9857 if (!move_group && group_leader->ctx != ctx)
9858 goto err_context;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009859
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009860 /*
9861 * Only a group leader can be exclusive or pinned
9862 */
9863 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009864 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009865 }
9866
9867 if (output_event) {
9868 err = perf_event_set_output(event, output_event);
9869 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009870 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009871 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009872
Yann Droneauda21b0b32014-01-05 21:36:33 +01009873 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9874 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009875 if (IS_ERR(event_file)) {
9876 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009877 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009878 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009879 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009880
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009881 if (move_group) {
Peter Zijlstra922813f2017-01-11 21:09:50 +01009882 gctx = __perf_event_ctx_lock_double(group_leader, ctx);
9883
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009884 if (gctx->task == TASK_TOMBSTONE) {
9885 err = -ESRCH;
9886 goto err_locked;
9887 }
Peter Zijlstra922813f2017-01-11 21:09:50 +01009888
9889 /*
9890 * Check if we raced against another sys_perf_event_open() call
9891 * moving the software group underneath us.
9892 */
9893 if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
9894 /*
9895 * If someone moved the group out from under us, check
9896 * if this new event wound up on the same ctx, if so
9897 * its the regular !move_group case, otherwise fail.
9898 */
9899 if (gctx != ctx) {
9900 err = -EINVAL;
9901 goto err_locked;
9902 } else {
9903 perf_event_ctx_unlock(group_leader, gctx);
9904 move_group = 0;
9905 }
9906 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009907 } else {
9908 mutex_lock(&ctx->mutex);
9909 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009910
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009911 if (ctx->task == TASK_TOMBSTONE) {
9912 err = -ESRCH;
9913 goto err_locked;
9914 }
9915
Peter Zijlstraa7239682015-09-09 19:06:33 +02009916 if (!perf_event_validate_size(event)) {
9917 err = -E2BIG;
9918 goto err_locked;
9919 }
9920
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009921 /*
9922 * Must be under the same ctx::mutex as perf_install_in_context(),
9923 * because we need to serialize with concurrent event creation.
9924 */
9925 if (!exclusive_event_installable(event, ctx)) {
9926 /* exclusive and group stuff are assumed mutually exclusive */
9927 WARN_ON_ONCE(move_group);
9928
9929 err = -EBUSY;
9930 goto err_locked;
9931 }
9932
9933 WARN_ON_ONCE(ctx->parent_ctx);
9934
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009935 /*
9936 * This is the point on no return; we cannot fail hereafter. This is
9937 * where we start modifying current state.
9938 */
9939
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009940 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009941 /*
9942 * See perf_event_ctx_lock() for comments on the details
9943 * of swizzling perf_event::ctx.
9944 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009945 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009946
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009947 list_for_each_entry(sibling, &group_leader->sibling_list,
9948 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009949 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009950 put_ctx(gctx);
9951 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009952
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009953 /*
9954 * Wait for everybody to stop referencing the events through
9955 * the old lists, before installing it on new lists.
9956 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009957 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009958
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009959 /*
9960 * Install the group siblings before the group leader.
9961 *
9962 * Because a group leader will try and install the entire group
9963 * (through the sibling list, which is still in-tact), we can
9964 * end up with siblings installed in the wrong context.
9965 *
9966 * By installing siblings first we NO-OP because they're not
9967 * reachable through the group lists.
9968 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009969 list_for_each_entry(sibling, &group_leader->sibling_list,
9970 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009971 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01009972 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009973 get_ctx(ctx);
9974 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009975
9976 /*
9977 * Removing from the context ends up with disabled
9978 * event. What we want here is event in the initial
9979 * startup state, ready to be add into new context.
9980 */
9981 perf_event__state_init(group_leader);
9982 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9983 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009984
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009985 /*
9986 * Now that all events are installed in @ctx, nothing
9987 * references @gctx anymore, so drop the last reference we have
9988 * on it.
9989 */
9990 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009991 }
9992
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02009993 /*
9994 * Precalculate sample_data sizes; do while holding ctx::mutex such
9995 * that we're serialized against further additions and before
9996 * perf_install_in_context() which is the point the event is active and
9997 * can use these values.
9998 */
9999 perf_event__header_size(event);
10000 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010001
Peter Zijlstra78cd2c72016-01-25 14:08:45 +010010002 event->owner = current;
10003
Yan, Zhenge2d37cd2012-06-15 14:31:32 +080010004 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010005 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010006
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010007 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010008 perf_event_ctx_unlock(group_leader, gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010009 mutex_unlock(&ctx->mutex);
10010
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010011 if (task) {
10012 mutex_unlock(&task->signal->cred_guard_mutex);
10013 put_task_struct(task);
10014 }
10015
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010016 put_online_cpus();
10017
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010018 mutex_lock(&current->perf_event_mutex);
10019 list_add_tail(&event->owner_entry, &current->perf_event_list);
10020 mutex_unlock(&current->perf_event_mutex);
10021
Peter Zijlstra8a495422010-05-27 15:47:49 +020010022 /*
10023 * Drop the reference on the group_event after placing the
10024 * new event on the sibling_list. This ensures destruction
10025 * of the group leader will find the pointer to itself in
10026 * perf_group_detach().
10027 */
Al Viro2903ff02012-08-28 12:52:22 -040010028 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010029 fd_install(event_fd, event_file);
10030 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010031
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010032err_locked:
10033 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010034 perf_event_ctx_unlock(group_leader, gctx);
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010035 mutex_unlock(&ctx->mutex);
10036/* err_file: */
10037 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010038err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010039 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -040010040 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020010041err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +010010042 /*
10043 * If event_file is set, the fput() above will have called ->release()
10044 * and that will take care of freeing the event.
10045 */
10046 if (!event_file)
10047 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010048err_cred:
10049 if (task)
10050 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010051err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010052 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010053err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +020010054 if (task)
10055 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010056err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -040010057 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010058err_fd:
10059 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010060 return err;
10061}
10062
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010063/**
10064 * perf_event_create_kernel_counter
10065 *
10066 * @attr: attributes of the counter to create
10067 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -070010068 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010069 */
10070struct perf_event *
10071perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -070010072 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +030010073 perf_overflow_handler_t overflow_handler,
10074 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010075{
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010076 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010077 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010078 int err;
10079
10080 /*
10081 * Get the target context (task or percpu):
10082 */
10083
Avi Kivity4dc0da82011-06-29 18:42:35 +030010084 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000010085 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010086 if (IS_ERR(event)) {
10087 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010088 goto err;
10089 }
10090
Jiri Olsaf8697762014-08-01 14:33:01 +020010091 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010092 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +020010093
Yan, Zheng4af57ef282014-11-04 21:56:01 -050010094 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010095 if (IS_ERR(ctx)) {
10096 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010097 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010098 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010099
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010100 WARN_ON_ONCE(ctx->parent_ctx);
10101 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010102 if (ctx->task == TASK_TOMBSTONE) {
10103 err = -ESRCH;
10104 goto err_unlock;
10105 }
10106
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010107 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010108 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010109 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010110 }
10111
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010112 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010113 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010114 mutex_unlock(&ctx->mutex);
10115
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010116 return event;
10117
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010118err_unlock:
10119 mutex_unlock(&ctx->mutex);
10120 perf_unpin_context(ctx);
10121 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010122err_free:
10123 free_event(event);
10124err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010125 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010126}
10127EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
10128
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010129void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
10130{
10131 struct perf_event_context *src_ctx;
10132 struct perf_event_context *dst_ctx;
10133 struct perf_event *event, *tmp;
10134 LIST_HEAD(events);
10135
10136 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
10137 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
10138
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010139 /*
10140 * See perf_event_ctx_lock() for comments on the details
10141 * of swizzling perf_event::ctx.
10142 */
10143 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010144 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
10145 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010146 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010147 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010148 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020010149 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010150 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010151
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010152 /*
10153 * Wait for the events to quiesce before re-instating them.
10154 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010155 synchronize_rcu();
10156
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010157 /*
10158 * Re-instate events in 2 passes.
10159 *
10160 * Skip over group leaders and only install siblings on this first
10161 * pass, siblings will not get enabled without a leader, however a
10162 * leader will enable its siblings, even if those are still on the old
10163 * context.
10164 */
10165 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10166 if (event->group_leader == event)
10167 continue;
10168
10169 list_del(&event->migrate_entry);
10170 if (event->state >= PERF_EVENT_STATE_OFF)
10171 event->state = PERF_EVENT_STATE_INACTIVE;
10172 account_event_cpu(event, dst_cpu);
10173 perf_install_in_context(dst_ctx, event, dst_cpu);
10174 get_ctx(dst_ctx);
10175 }
10176
10177 /*
10178 * Once all the siblings are setup properly, install the group leaders
10179 * to make it go.
10180 */
Peter Zijlstra98861672013-10-03 16:02:23 +020010181 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10182 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010183 if (event->state >= PERF_EVENT_STATE_OFF)
10184 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010185 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010186 perf_install_in_context(dst_ctx, event, dst_cpu);
10187 get_ctx(dst_ctx);
10188 }
10189 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010190 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010191}
10192EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
10193
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010194static void sync_child_event(struct perf_event *child_event,
10195 struct task_struct *child)
10196{
10197 struct perf_event *parent_event = child_event->parent;
10198 u64 child_val;
10199
10200 if (child_event->attr.inherit_stat)
10201 perf_event_read_event(child_event, child);
10202
Peter Zijlstrab5e58792010-05-21 14:43:12 +020010203 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010204
10205 /*
10206 * Add back the child's count to the parent's count:
10207 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020010208 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010209 atomic64_add(child_event->total_time_enabled,
10210 &parent_event->child_total_time_enabled);
10211 atomic64_add(child_event->total_time_running,
10212 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010213}
10214
10215static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010216perf_event_exit_event(struct perf_event *child_event,
10217 struct perf_event_context *child_ctx,
10218 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010219{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010220 struct perf_event *parent_event = child_event->parent;
10221
Peter Zijlstra1903d502014-07-15 17:27:27 +020010222 /*
10223 * Do not destroy the 'original' grouping; because of the context
10224 * switch optimization the original events could've ended up in a
10225 * random child task.
10226 *
10227 * If we were to destroy the original group, all group related
10228 * operations would cease to function properly after this random
10229 * child dies.
10230 *
10231 * Do destroy all inherited groups, we don't care about those
10232 * and being thorough is better.
10233 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010234 raw_spin_lock_irq(&child_ctx->lock);
10235 WARN_ON_ONCE(child_ctx->is_active);
10236
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010237 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010010238 perf_group_detach(child_event);
10239 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +010010240 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010241 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010242
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010243 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010244 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010245 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010246 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040010247 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010248 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010249 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010250 /*
10251 * Child events can be cleaned up.
10252 */
10253
10254 sync_child_event(child_event, child);
10255
10256 /*
10257 * Remove this event from the parent's list
10258 */
10259 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
10260 mutex_lock(&parent_event->child_mutex);
10261 list_del_init(&child_event->child_list);
10262 mutex_unlock(&parent_event->child_mutex);
10263
10264 /*
10265 * Kick perf_poll() for is_event_hup().
10266 */
10267 perf_event_wakeup(parent_event);
10268 free_event(child_event);
10269 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010270}
10271
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010272static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010273{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010274 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010275 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010276
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010277 WARN_ON_ONCE(child != current);
10278
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010279 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010280 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010281 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010282
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010283 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010284 * In order to reduce the amount of tricky in ctx tear-down, we hold
10285 * ctx::mutex over the entire thing. This serializes against almost
10286 * everything that wants to access the ctx.
10287 *
10288 * The exception is sys_perf_event_open() /
10289 * perf_event_create_kernel_count() which does find_get_context()
10290 * without ctx::mutex (it cannot because of the move_group double mutex
10291 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010292 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010293 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010294
10295 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010296 * In a single ctx::lock section, de-schedule the events and detach the
10297 * context from the task such that we cannot ever get it scheduled back
10298 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010299 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010300 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010301 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010302
10303 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010304 * Now that the context is inactive, destroy the task <-> ctx relation
10305 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010306 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010307 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
10308 put_ctx(child_ctx); /* cannot be last */
10309 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
10310 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010311
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010312 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010313 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010314
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010315 if (clone_ctx)
10316 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010317
10318 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010319 * Report the task dead after unscheduling the events so that we
10320 * won't get any samples after PERF_RECORD_EXIT. We can however still
10321 * get a few PERF_RECORD_READ events.
10322 */
10323 perf_event_task(child, child_ctx, 0);
10324
Peter Zijlstraebf905f2014-05-29 19:00:24 +020010325 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010326 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010327
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010328 mutex_unlock(&child_ctx->mutex);
10329
10330 put_ctx(child_ctx);
10331}
10332
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010333/*
10334 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010335 *
10336 * Can be called with cred_guard_mutex held when called from
10337 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010338 */
10339void perf_event_exit_task(struct task_struct *child)
10340{
Peter Zijlstra88821352010-11-09 19:01:43 +010010341 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010342 int ctxn;
10343
Peter Zijlstra88821352010-11-09 19:01:43 +010010344 mutex_lock(&child->perf_event_mutex);
10345 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
10346 owner_entry) {
10347 list_del_init(&event->owner_entry);
10348
10349 /*
10350 * Ensure the list deletion is visible before we clear
10351 * the owner, closes a race against perf_release() where
10352 * we need to serialize on the owner->perf_event_mutex.
10353 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010010354 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010010355 }
10356 mutex_unlock(&child->perf_event_mutex);
10357
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010358 for_each_task_context_nr(ctxn)
10359 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010010360
10361 /*
10362 * The perf_event_exit_task_context calls perf_event_task
10363 * with child's task_ctx, which generates EXIT events for
10364 * child contexts and sets child->perf_event_ctxp[] to NULL.
10365 * At this point we need to send EXIT events to cpu contexts.
10366 */
10367 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010368}
10369
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010370static void perf_free_event(struct perf_event *event,
10371 struct perf_event_context *ctx)
10372{
10373 struct perf_event *parent = event->parent;
10374
10375 if (WARN_ON_ONCE(!parent))
10376 return;
10377
10378 mutex_lock(&parent->child_mutex);
10379 list_del_init(&event->child_list);
10380 mutex_unlock(&parent->child_mutex);
10381
Al Viroa6fa9412012-08-20 14:59:25 +010010382 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010383
Peter Zijlstra652884f2015-01-23 11:20:10 +010010384 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020010385 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010386 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010010387 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010388 free_event(event);
10389}
10390
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010391/*
Peter Zijlstra652884f2015-01-23 11:20:10 +010010392 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010393 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010010394 *
10395 * Not all locks are strictly required, but take them anyway to be nice and
10396 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010397 */
10398void perf_event_free_task(struct task_struct *task)
10399{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010400 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010401 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010402 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010403
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010404 for_each_task_context_nr(ctxn) {
10405 ctx = task->perf_event_ctxp[ctxn];
10406 if (!ctx)
10407 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010408
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010409 mutex_lock(&ctx->mutex);
Peter Zijlstrac04a9382017-03-16 13:47:48 +010010410 raw_spin_lock_irq(&ctx->lock);
10411 /*
10412 * Destroy the task <-> ctx relation and mark the context dead.
10413 *
10414 * This is important because even though the task hasn't been
10415 * exposed yet the context has been (through child_list).
10416 */
10417 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
10418 WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
10419 put_task_struct(task); /* cannot be last */
10420 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010421again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010422 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
10423 group_entry)
10424 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010425
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010426 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
10427 group_entry)
10428 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010429
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010430 if (!list_empty(&ctx->pinned_groups) ||
10431 !list_empty(&ctx->flexible_groups))
10432 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010433
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010434 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010435
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010436 put_ctx(ctx);
10437 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010438}
10439
Peter Zijlstra4e231c72010-09-09 21:01:59 +020010440void perf_event_delayed_put(struct task_struct *task)
10441{
10442 int ctxn;
10443
10444 for_each_task_context_nr(ctxn)
10445 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
10446}
10447
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010448struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000010449{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010450 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010451
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010452 file = fget_raw(fd);
10453 if (!file)
10454 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000010455
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010456 if (file->f_op != &perf_fops) {
10457 fput(file);
10458 return ERR_PTR(-EBADF);
10459 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000010460
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010461 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010462}
10463
10464const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
10465{
10466 if (!event)
10467 return ERR_PTR(-EINVAL);
10468
10469 return &event->attr;
10470}
10471
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010472/*
10473 * inherit a event from parent task to child task:
10474 */
10475static struct perf_event *
10476inherit_event(struct perf_event *parent_event,
10477 struct task_struct *parent,
10478 struct perf_event_context *parent_ctx,
10479 struct task_struct *child,
10480 struct perf_event *group_leader,
10481 struct perf_event_context *child_ctx)
10482{
Jiri Olsa1929def2014-09-12 13:18:27 +020010483 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010484 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020010485 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010486
10487 /*
10488 * Instead of creating recursive hierarchies of events,
10489 * we link inherited events back to the original parent,
10490 * which has a filp for sure, which we use as the reference
10491 * count:
10492 */
10493 if (parent_event->parent)
10494 parent_event = parent_event->parent;
10495
10496 child_event = perf_event_alloc(&parent_event->attr,
10497 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010498 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010499 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000010500 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010501 if (IS_ERR(child_event))
10502 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010010503
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010504 /*
10505 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10506 * must be under the same lock in order to serialize against
10507 * perf_event_release_kernel(), such that either we must observe
10508 * is_orphaned_event() or they will observe us on the child_list.
10509 */
10510 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020010511 if (is_orphaned_event(parent_event) ||
10512 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010513 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +010010514 free_event(child_event);
10515 return NULL;
10516 }
10517
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010518 get_ctx(child_ctx);
10519
10520 /*
10521 * Make the child state follow the state of the parent event,
10522 * not its attr.disabled bit. We hold the parent's mutex,
10523 * so we won't race with perf_event_{en, dis}able_family.
10524 */
Jiri Olsa1929def2014-09-12 13:18:27 +020010525 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010526 child_event->state = PERF_EVENT_STATE_INACTIVE;
10527 else
10528 child_event->state = PERF_EVENT_STATE_OFF;
10529
10530 if (parent_event->attr.freq) {
10531 u64 sample_period = parent_event->hw.sample_period;
10532 struct hw_perf_event *hwc = &child_event->hw;
10533
10534 hwc->sample_period = sample_period;
10535 hwc->last_period = sample_period;
10536
10537 local64_set(&hwc->period_left, sample_period);
10538 }
10539
10540 child_event->ctx = child_ctx;
10541 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010542 child_event->overflow_handler_context
10543 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010544
10545 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020010546 * Precalculate sample_data sizes
10547 */
10548 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020010549 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020010550
10551 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010552 * Link it up in the child's context:
10553 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020010554 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010555 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010556 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010557
10558 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010559 * Link this into the parent event's child list
10560 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010561 list_add_tail(&child_event->child_list, &parent_event->child_list);
10562 mutex_unlock(&parent_event->child_mutex);
10563
10564 return child_event;
10565}
10566
10567static int inherit_group(struct perf_event *parent_event,
10568 struct task_struct *parent,
10569 struct perf_event_context *parent_ctx,
10570 struct task_struct *child,
10571 struct perf_event_context *child_ctx)
10572{
10573 struct perf_event *leader;
10574 struct perf_event *sub;
10575 struct perf_event *child_ctr;
10576
10577 leader = inherit_event(parent_event, parent, parent_ctx,
10578 child, NULL, child_ctx);
10579 if (IS_ERR(leader))
10580 return PTR_ERR(leader);
10581 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10582 child_ctr = inherit_event(sub, parent, parent_ctx,
10583 child, leader, child_ctx);
10584 if (IS_ERR(child_ctr))
10585 return PTR_ERR(child_ctr);
10586 }
10587 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010588}
10589
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010590static int
10591inherit_task_group(struct perf_event *event, struct task_struct *parent,
10592 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010593 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010594 int *inherited_all)
10595{
10596 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010597 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010598
10599 if (!event->attr.inherit) {
10600 *inherited_all = 0;
10601 return 0;
10602 }
10603
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010604 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010605 if (!child_ctx) {
10606 /*
10607 * This is executed from the parent task context, so
10608 * inherit events that have been marked for cloning.
10609 * First allocate and initialize a context for the
10610 * child.
10611 */
10612
Jiri Olsa734df5a2013-07-09 17:44:10 +020010613 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010614 if (!child_ctx)
10615 return -ENOMEM;
10616
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010617 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010618 }
10619
10620 ret = inherit_group(event, parent, parent_ctx,
10621 child, child_ctx);
10622
10623 if (ret)
10624 *inherited_all = 0;
10625
10626 return ret;
10627}
10628
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010629/*
10630 * Initialize the perf_event context in task_struct
10631 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010632static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010633{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010634 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010635 struct perf_event_context *cloned_ctx;
10636 struct perf_event *event;
10637 struct task_struct *parent = current;
10638 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010639 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010640 int ret = 0;
10641
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010642 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010643 return 0;
10644
10645 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010646 * If the parent's context is a clone, pin it so it won't get
10647 * swapped under us.
10648 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010649 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010650 if (!parent_ctx)
10651 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010652
10653 /*
10654 * No need to check if parent_ctx != NULL here; since we saw
10655 * it non-NULL earlier, the only reason for it to become NULL
10656 * is if we exit, and since we're currently in the middle of
10657 * a fork we can't be exiting at the same time.
10658 */
10659
10660 /*
10661 * Lock the parent list. No need to lock the child - not PID
10662 * hashed yet and not running, so nobody can access it.
10663 */
10664 mutex_lock(&parent_ctx->mutex);
10665
10666 /*
10667 * We dont have to disable NMIs - we are only looking at
10668 * the list, not manipulating it:
10669 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010670 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010671 ret = inherit_task_group(event, parent, parent_ctx,
10672 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010673 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010674 goto out_unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010675 }
10676
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010677 /*
10678 * We can't hold ctx->lock when iterating the ->flexible_group list due
10679 * to allocations, but we need to prevent rotation because
10680 * rotate_ctx() will change the list from interrupt context.
10681 */
10682 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10683 parent_ctx->rotate_disable = 1;
10684 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10685
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010686 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010687 ret = inherit_task_group(event, parent, parent_ctx,
10688 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010689 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010690 goto out_unlock;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010691 }
10692
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010693 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10694 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010695
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010696 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010697
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010698 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010699 /*
10700 * Mark the child context as a clone of the parent
10701 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010702 *
10703 * Note that if the parent is a clone, the holding of
10704 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010705 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010706 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010707 if (cloned_ctx) {
10708 child_ctx->parent_ctx = cloned_ctx;
10709 child_ctx->parent_gen = parent_ctx->parent_gen;
10710 } else {
10711 child_ctx->parent_ctx = parent_ctx;
10712 child_ctx->parent_gen = parent_ctx->generation;
10713 }
10714 get_ctx(child_ctx->parent_ctx);
10715 }
10716
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010717 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010718out_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010719 mutex_unlock(&parent_ctx->mutex);
10720
10721 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010722 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010723
10724 return ret;
10725}
10726
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010727/*
10728 * Initialize the perf_event context in task_struct
10729 */
10730int perf_event_init_task(struct task_struct *child)
10731{
10732 int ctxn, ret;
10733
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010734 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10735 mutex_init(&child->perf_event_mutex);
10736 INIT_LIST_HEAD(&child->perf_event_list);
10737
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010738 for_each_task_context_nr(ctxn) {
10739 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010740 if (ret) {
10741 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010742 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010743 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010744 }
10745
10746 return 0;
10747}
10748
Paul Mackerras220b1402010-03-10 20:45:52 +110010749static void __init perf_event_init_all_cpus(void)
10750{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010751 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010752 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010753
10754 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010755 swhash = &per_cpu(swevent_htable, cpu);
10756 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010757 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070010758
10759 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
10760 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020010761
10762 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010763 }
10764}
10765
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010766int perf_event_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010767{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010768 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010769
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010770 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010771 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010772 struct swevent_hlist *hlist;
10773
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010774 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10775 WARN_ON(!hlist);
10776 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010777 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010778 mutex_unlock(&swhash->hlist_mutex);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010779 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010780}
10781
Dave Young2965faa2015-09-09 15:38:55 -070010782#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010783static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010784{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010785 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010786 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10787 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010788
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010789 raw_spin_lock(&ctx->lock);
10790 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010791 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010792 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010793}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010794
10795static void perf_event_exit_cpu_context(int cpu)
10796{
10797 struct perf_event_context *ctx;
10798 struct pmu *pmu;
10799 int idx;
10800
10801 idx = srcu_read_lock(&pmus_srcu);
10802 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010803 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010804
10805 mutex_lock(&ctx->mutex);
10806 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10807 mutex_unlock(&ctx->mutex);
10808 }
10809 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010810}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010811#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010812
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010813static void perf_event_exit_cpu_context(int cpu) { }
10814
10815#endif
10816
10817int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010818{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010819 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010820 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010821}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010822
Peter Zijlstrac2774432010-12-08 15:29:02 +010010823static int
10824perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10825{
10826 int cpu;
10827
10828 for_each_online_cpu(cpu)
10829 perf_event_exit_cpu(cpu);
10830
10831 return NOTIFY_OK;
10832}
10833
10834/*
10835 * Run the perf reboot notifier at the very last possible moment so that
10836 * the generic watchdog code runs as long as possible.
10837 */
10838static struct notifier_block perf_reboot_notifier = {
10839 .notifier_call = perf_reboot,
10840 .priority = INT_MIN,
10841};
10842
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010843void __init perf_event_init(void)
10844{
Jason Wessel3c502e72010-11-04 17:33:01 -050010845 int ret;
10846
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010847 idr_init(&pmu_idr);
10848
Paul Mackerras220b1402010-03-10 20:45:52 +110010849 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010850 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010851 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10852 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10853 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010854 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010855 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010010856 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010857
10858 ret = init_hw_breakpoint();
10859 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010860
Jiri Olsab01c3a02012-03-23 15:41:20 +010010861 /*
10862 * Build time assertion that we keep the data_head at the intended
10863 * location. IOW, validation we got the __reserved[] size right.
10864 */
10865 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10866 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010867}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010868
Cody P Schaferfd979c02015-01-30 13:45:57 -080010869ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10870 char *page)
10871{
10872 struct perf_pmu_events_attr *pmu_attr =
10873 container_of(attr, struct perf_pmu_events_attr, attr);
10874
10875 if (pmu_attr->event_str)
10876 return sprintf(page, "%s\n", pmu_attr->event_str);
10877
10878 return 0;
10879}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010880EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010881
Peter Zijlstraabe43402010-11-17 23:17:37 +010010882static int __init perf_event_sysfs_init(void)
10883{
10884 struct pmu *pmu;
10885 int ret;
10886
10887 mutex_lock(&pmus_lock);
10888
10889 ret = bus_register(&pmu_bus);
10890 if (ret)
10891 goto unlock;
10892
10893 list_for_each_entry(pmu, &pmus, entry) {
10894 if (!pmu->name || pmu->type < 0)
10895 continue;
10896
10897 ret = pmu_dev_alloc(pmu);
10898 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10899 }
10900 pmu_bus_running = 1;
10901 ret = 0;
10902
10903unlock:
10904 mutex_unlock(&pmus_lock);
10905
10906 return ret;
10907}
10908device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010909
10910#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010911static struct cgroup_subsys_state *
10912perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010913{
10914 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010915
Li Zefan1b15d052011-03-03 14:26:06 +080010916 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010917 if (!jc)
10918 return ERR_PTR(-ENOMEM);
10919
Stephane Eraniane5d13672011-02-14 11:20:01 +020010920 jc->info = alloc_percpu(struct perf_cgroup_info);
10921 if (!jc->info) {
10922 kfree(jc);
10923 return ERR_PTR(-ENOMEM);
10924 }
10925
Stephane Eraniane5d13672011-02-14 11:20:01 +020010926 return &jc->css;
10927}
10928
Tejun Heoeb954192013-08-08 20:11:23 -040010929static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010930{
Tejun Heoeb954192013-08-08 20:11:23 -040010931 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10932
Stephane Eraniane5d13672011-02-14 11:20:01 +020010933 free_percpu(jc->info);
10934 kfree(jc);
10935}
10936
10937static int __perf_cgroup_move(void *info)
10938{
10939 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010940 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010941 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010942 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010943 return 0;
10944}
10945
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010946static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010947{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010948 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010949 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010950
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010951 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010952 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010953}
10954
Tejun Heo073219e2014-02-08 10:36:58 -050010955struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010956 .css_alloc = perf_cgroup_css_alloc,
10957 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010958 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010959};
10960#endif /* CONFIG_CGROUP_PERF */