blob: be42bfeb87ae9d10813eee2268b676826451dac2 [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{
Jiri Olsa6279fc52017-07-20 16:14:55 +02004339 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004340 struct perf_event *sub;
Jiri Olsa6279fc52017-07-20 16:14:55 +02004341 unsigned long flags;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004342 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004343 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004344
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004345 ret = perf_event_read(leader, true);
4346 if (ret)
4347 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004348
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004349 /*
4350 * Since we co-schedule groups, {enabled,running} times of siblings
4351 * will be identical to those of the leader, so we only publish one
4352 * set.
4353 */
4354 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4355 values[n++] += leader->total_time_enabled +
4356 atomic64_read(&leader->child_total_time_enabled);
4357 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004358
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004359 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4360 values[n++] += leader->total_time_running +
4361 atomic64_read(&leader->child_total_time_running);
4362 }
4363
4364 /*
4365 * Write {count,id} tuples for every sibling.
4366 */
4367 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004368 if (read_format & PERF_FORMAT_ID)
4369 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004370
Jiri Olsa6279fc52017-07-20 16:14:55 +02004371 raw_spin_lock_irqsave(&ctx->lock, flags);
4372
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004373 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004374 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004375 if (read_format & PERF_FORMAT_ID)
4376 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004377 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004378
Jiri Olsa6279fc52017-07-20 16:14:55 +02004379 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004380 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004381}
4382
4383static int perf_read_group(struct perf_event *event,
4384 u64 read_format, char __user *buf)
4385{
4386 struct perf_event *leader = event->group_leader, *child;
4387 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004388 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004389 u64 *values;
4390
4391 lockdep_assert_held(&ctx->mutex);
4392
4393 values = kzalloc(event->read_size, GFP_KERNEL);
4394 if (!values)
4395 return -ENOMEM;
4396
4397 values[0] = 1 + leader->nr_siblings;
4398
4399 /*
4400 * By locking the child_mutex of the leader we effectively
4401 * lock the child list of all siblings.. XXX explain how.
4402 */
4403 mutex_lock(&leader->child_mutex);
4404
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004405 ret = __perf_read_group_add(leader, read_format, values);
4406 if (ret)
4407 goto unlock;
4408
4409 list_for_each_entry(child, &leader->child_list, child_list) {
4410 ret = __perf_read_group_add(child, read_format, values);
4411 if (ret)
4412 goto unlock;
4413 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004414
4415 mutex_unlock(&leader->child_mutex);
4416
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004417 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004418 if (copy_to_user(buf, values, event->read_size))
4419 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004420 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004421
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004422unlock:
4423 mutex_unlock(&leader->child_mutex);
4424out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004425 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004426 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004427}
4428
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004429static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004430 u64 read_format, char __user *buf)
4431{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004432 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004433 u64 values[4];
4434 int n = 0;
4435
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004436 values[n++] = perf_event_read_value(event, &enabled, &running);
4437 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4438 values[n++] = enabled;
4439 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4440 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004441 if (read_format & PERF_FORMAT_ID)
4442 values[n++] = primary_event_id(event);
4443
4444 if (copy_to_user(buf, values, n * sizeof(u64)))
4445 return -EFAULT;
4446
4447 return n * sizeof(u64);
4448}
4449
Jiri Olsadc633982014-09-12 13:18:26 +02004450static bool is_event_hup(struct perf_event *event)
4451{
4452 bool no_children;
4453
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004454 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004455 return false;
4456
4457 mutex_lock(&event->child_mutex);
4458 no_children = list_empty(&event->child_list);
4459 mutex_unlock(&event->child_mutex);
4460 return no_children;
4461}
4462
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463/*
4464 * Read the performance event - simple non blocking version for now
4465 */
4466static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004467__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004468{
4469 u64 read_format = event->attr.read_format;
4470 int ret;
4471
4472 /*
4473 * Return end-of-file for a read on a event that is in
4474 * error state (i.e. because it was pinned but it couldn't be
4475 * scheduled on to the CPU at some point).
4476 */
4477 if (event->state == PERF_EVENT_STATE_ERROR)
4478 return 0;
4479
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004480 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004481 return -ENOSPC;
4482
4483 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004484 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004485 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004486 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004487 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004488
4489 return ret;
4490}
4491
4492static ssize_t
4493perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4494{
4495 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004496 struct perf_event_context *ctx;
4497 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004498
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004499 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004500 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004501 perf_event_ctx_unlock(event, ctx);
4502
4503 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004504}
4505
4506static unsigned int perf_poll(struct file *file, poll_table *wait)
4507{
4508 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004509 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004510 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004512 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004513
Jiri Olsadc633982014-09-12 13:18:26 +02004514 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004515 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004516
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004517 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004518 * Pin the event->rb by taking event->mmap_mutex; otherwise
4519 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004520 */
4521 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004522 rb = event->rb;
4523 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004524 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004525 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526 return events;
4527}
4528
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004529static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004531 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004532 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004533 perf_event_update_userpage(event);
4534}
4535
4536/*
4537 * Holding the top-level event's child_mutex means that any
4538 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004539 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004540 * task existence requirements of perf_event_enable/disable.
4541 */
4542static void perf_event_for_each_child(struct perf_event *event,
4543 void (*func)(struct perf_event *))
4544{
4545 struct perf_event *child;
4546
4547 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004548
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004549 mutex_lock(&event->child_mutex);
4550 func(event);
4551 list_for_each_entry(child, &event->child_list, child_list)
4552 func(child);
4553 mutex_unlock(&event->child_mutex);
4554}
4555
4556static void perf_event_for_each(struct perf_event *event,
4557 void (*func)(struct perf_event *))
4558{
4559 struct perf_event_context *ctx = event->ctx;
4560 struct perf_event *sibling;
4561
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004562 lockdep_assert_held(&ctx->mutex);
4563
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004564 event = event->group_leader;
4565
4566 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004567 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004568 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004569}
4570
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004571static void __perf_event_period(struct perf_event *event,
4572 struct perf_cpu_context *cpuctx,
4573 struct perf_event_context *ctx,
4574 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004575{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004576 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004577 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004578
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004579 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004580 event->attr.sample_freq = value;
4581 } else {
4582 event->attr.sample_period = value;
4583 event->hw.sample_period = value;
4584 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004585
4586 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4587 if (active) {
4588 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004589 /*
4590 * We could be throttled; unthrottle now to avoid the tick
4591 * trying to unthrottle while we already re-started the event.
4592 */
4593 if (event->hw.interrupts == MAX_INTERRUPTS) {
4594 event->hw.interrupts = 0;
4595 perf_log_throttle(event, 1);
4596 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004597 event->pmu->stop(event, PERF_EF_UPDATE);
4598 }
4599
4600 local64_set(&event->hw.period_left, 0);
4601
4602 if (active) {
4603 event->pmu->start(event, PERF_EF_RELOAD);
4604 perf_pmu_enable(ctx->pmu);
4605 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004606}
4607
4608static int perf_event_period(struct perf_event *event, u64 __user *arg)
4609{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004610 u64 value;
4611
4612 if (!is_sampling_event(event))
4613 return -EINVAL;
4614
4615 if (copy_from_user(&value, arg, sizeof(value)))
4616 return -EFAULT;
4617
4618 if (!value)
4619 return -EINVAL;
4620
4621 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4622 return -EINVAL;
4623
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004624 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004625
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004626 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004627}
4628
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004629static const struct file_operations perf_fops;
4630
Al Viro2903ff02012-08-28 12:52:22 -04004631static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004632{
Al Viro2903ff02012-08-28 12:52:22 -04004633 struct fd f = fdget(fd);
4634 if (!f.file)
4635 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004636
Al Viro2903ff02012-08-28 12:52:22 -04004637 if (f.file->f_op != &perf_fops) {
4638 fdput(f);
4639 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004640 }
Al Viro2903ff02012-08-28 12:52:22 -04004641 *p = f;
4642 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004643}
4644
4645static int perf_event_set_output(struct perf_event *event,
4646 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004647static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004648static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004649
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004650static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004651{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004652 void (*func)(struct perf_event *);
4653 u32 flags = arg;
4654
4655 switch (cmd) {
4656 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004657 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004658 break;
4659 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004660 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004661 break;
4662 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004663 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004664 break;
4665
4666 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004667 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004668
4669 case PERF_EVENT_IOC_PERIOD:
4670 return perf_event_period(event, (u64 __user *)arg);
4671
Jiri Olsacf4957f2012-10-24 13:37:58 +02004672 case PERF_EVENT_IOC_ID:
4673 {
4674 u64 id = primary_event_id(event);
4675
4676 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4677 return -EFAULT;
4678 return 0;
4679 }
4680
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004681 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004682 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004683 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004684 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004685 struct perf_event *output_event;
4686 struct fd output;
4687 ret = perf_fget_light(arg, &output);
4688 if (ret)
4689 return ret;
4690 output_event = output.file->private_data;
4691 ret = perf_event_set_output(event, output_event);
4692 fdput(output);
4693 } else {
4694 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004695 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004696 return ret;
4697 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004698
Li Zefan6fb29152009-10-15 11:21:42 +08004699 case PERF_EVENT_IOC_SET_FILTER:
4700 return perf_event_set_filter(event, (void __user *)arg);
4701
Alexei Starovoitov25415172015-03-25 12:49:20 -07004702 case PERF_EVENT_IOC_SET_BPF:
4703 return perf_event_set_bpf_prog(event, arg);
4704
Wang Nan86e79722016-03-28 06:41:29 +00004705 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4706 struct ring_buffer *rb;
4707
4708 rcu_read_lock();
4709 rb = rcu_dereference(event->rb);
4710 if (!rb || !rb->nr_pages) {
4711 rcu_read_unlock();
4712 return -EINVAL;
4713 }
4714 rb_toggle_paused(rb, !!arg);
4715 rcu_read_unlock();
4716 return 0;
4717 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004718 default:
4719 return -ENOTTY;
4720 }
4721
4722 if (flags & PERF_IOC_FLAG_GROUP)
4723 perf_event_for_each(event, func);
4724 else
4725 perf_event_for_each_child(event, func);
4726
4727 return 0;
4728}
4729
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004730static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4731{
4732 struct perf_event *event = file->private_data;
4733 struct perf_event_context *ctx;
4734 long ret;
4735
4736 ctx = perf_event_ctx_lock(event);
4737 ret = _perf_ioctl(event, cmd, arg);
4738 perf_event_ctx_unlock(event, ctx);
4739
4740 return ret;
4741}
4742
Pawel Mollb3f20782014-06-13 16:03:32 +01004743#ifdef CONFIG_COMPAT
4744static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4745 unsigned long arg)
4746{
4747 switch (_IOC_NR(cmd)) {
4748 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4749 case _IOC_NR(PERF_EVENT_IOC_ID):
4750 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4751 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4752 cmd &= ~IOCSIZE_MASK;
4753 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4754 }
4755 break;
4756 }
4757 return perf_ioctl(file, cmd, arg);
4758}
4759#else
4760# define perf_compat_ioctl NULL
4761#endif
4762
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004763int perf_event_task_enable(void)
4764{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004765 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004766 struct perf_event *event;
4767
4768 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004769 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4770 ctx = perf_event_ctx_lock(event);
4771 perf_event_for_each_child(event, _perf_event_enable);
4772 perf_event_ctx_unlock(event, ctx);
4773 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004774 mutex_unlock(&current->perf_event_mutex);
4775
4776 return 0;
4777}
4778
4779int perf_event_task_disable(void)
4780{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004781 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004782 struct perf_event *event;
4783
4784 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004785 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4786 ctx = perf_event_ctx_lock(event);
4787 perf_event_for_each_child(event, _perf_event_disable);
4788 perf_event_ctx_unlock(event, ctx);
4789 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004790 mutex_unlock(&current->perf_event_mutex);
4791
4792 return 0;
4793}
4794
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004795static int perf_event_index(struct perf_event *event)
4796{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004797 if (event->hw.state & PERF_HES_STOPPED)
4798 return 0;
4799
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004800 if (event->state != PERF_EVENT_STATE_ACTIVE)
4801 return 0;
4802
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004803 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004804}
4805
Eric B Munsonc4794292011-06-23 16:34:38 -04004806static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004807 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004808 u64 *enabled,
4809 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004810{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004811 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004812
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004813 *now = perf_clock();
4814 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004815 *enabled = ctx_time - event->tstamp_enabled;
4816 *running = ctx_time - event->tstamp_running;
4817}
4818
Peter Zijlstrafa731582013-09-19 10:16:42 +02004819static void perf_event_init_userpage(struct perf_event *event)
4820{
4821 struct perf_event_mmap_page *userpg;
4822 struct ring_buffer *rb;
4823
4824 rcu_read_lock();
4825 rb = rcu_dereference(event->rb);
4826 if (!rb)
4827 goto unlock;
4828
4829 userpg = rb->user_page;
4830
4831 /* Allow new userspace to detect that bit 0 is deprecated */
4832 userpg->cap_bit0_is_deprecated = 1;
4833 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004834 userpg->data_offset = PAGE_SIZE;
4835 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004836
4837unlock:
4838 rcu_read_unlock();
4839}
4840
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004841void __weak arch_perf_update_userpage(
4842 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004843{
4844}
4845
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004846/*
4847 * Callers need to ensure there can be no nesting of this function, otherwise
4848 * the seqlock logic goes bad. We can not serialize this because the arch
4849 * code calls this from NMI context.
4850 */
4851void perf_event_update_userpage(struct perf_event *event)
4852{
4853 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004854 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004855 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004856
4857 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004858 rb = rcu_dereference(event->rb);
4859 if (!rb)
4860 goto unlock;
4861
Eric B Munson0d641202011-06-24 12:26:26 -04004862 /*
4863 * compute total_time_enabled, total_time_running
4864 * based on snapshot values taken when the event
4865 * was last scheduled in.
4866 *
4867 * we cannot simply called update_context_time()
4868 * because of locking issue as we can be called in
4869 * NMI context
4870 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004871 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872
Frederic Weisbecker76369132011-05-19 19:55:04 +02004873 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004874 /*
4875 * Disable preemption so as to not let the corresponding user-space
4876 * spin too long if we get preempted.
4877 */
4878 preempt_disable();
4879 ++userpg->lock;
4880 barrier();
4881 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004882 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004883 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004884 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004885
Eric B Munson0d641202011-06-24 12:26:26 -04004886 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004887 atomic64_read(&event->child_total_time_enabled);
4888
Eric B Munson0d641202011-06-24 12:26:26 -04004889 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004890 atomic64_read(&event->child_total_time_running);
4891
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004892 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004893
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004894 barrier();
4895 ++userpg->lock;
4896 preempt_enable();
4897unlock:
4898 rcu_read_unlock();
4899}
4900
Peter Zijlstra906010b2009-09-21 16:08:49 +02004901static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4902{
4903 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004904 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004905 int ret = VM_FAULT_SIGBUS;
4906
4907 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4908 if (vmf->pgoff == 0)
4909 ret = 0;
4910 return ret;
4911 }
4912
4913 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004914 rb = rcu_dereference(event->rb);
4915 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004916 goto unlock;
4917
4918 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4919 goto unlock;
4920
Frederic Weisbecker76369132011-05-19 19:55:04 +02004921 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004922 if (!vmf->page)
4923 goto unlock;
4924
4925 get_page(vmf->page);
4926 vmf->page->mapping = vma->vm_file->f_mapping;
4927 vmf->page->index = vmf->pgoff;
4928
4929 ret = 0;
4930unlock:
4931 rcu_read_unlock();
4932
4933 return ret;
4934}
4935
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004936static void ring_buffer_attach(struct perf_event *event,
4937 struct ring_buffer *rb)
4938{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004939 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004940 unsigned long flags;
4941
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004942 if (event->rb) {
4943 /*
4944 * Should be impossible, we set this when removing
4945 * event->rb_entry and wait/clear when adding event->rb_entry.
4946 */
4947 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004948
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004949 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004950 spin_lock_irqsave(&old_rb->event_lock, flags);
4951 list_del_rcu(&event->rb_entry);
4952 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004953
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004954 event->rcu_batches = get_state_synchronize_rcu();
4955 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004956 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004957
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004958 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004959 if (event->rcu_pending) {
4960 cond_synchronize_rcu(event->rcu_batches);
4961 event->rcu_pending = 0;
4962 }
4963
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004964 spin_lock_irqsave(&rb->event_lock, flags);
4965 list_add_rcu(&event->rb_entry, &rb->event_list);
4966 spin_unlock_irqrestore(&rb->event_lock, flags);
4967 }
4968
Alexander Shishkin767ae082016-09-06 16:23:49 +03004969 /*
4970 * Avoid racing with perf_mmap_close(AUX): stop the event
4971 * before swizzling the event::rb pointer; if it's getting
4972 * unmapped, its aux_mmap_count will be 0 and it won't
4973 * restart. See the comment in __perf_pmu_output_stop().
4974 *
4975 * Data will inevitably be lost when set_output is done in
4976 * mid-air, but then again, whoever does it like this is
4977 * not in for the data anyway.
4978 */
4979 if (has_aux(event))
4980 perf_event_stop(event, 0);
4981
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004982 rcu_assign_pointer(event->rb, rb);
4983
4984 if (old_rb) {
4985 ring_buffer_put(old_rb);
4986 /*
4987 * Since we detached before setting the new rb, so that we
4988 * could attach the new rb, we could have missed a wakeup.
4989 * Provide it now.
4990 */
4991 wake_up_all(&event->waitq);
4992 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004993}
4994
4995static void ring_buffer_wakeup(struct perf_event *event)
4996{
4997 struct ring_buffer *rb;
4998
4999 rcu_read_lock();
5000 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005001 if (rb) {
5002 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
5003 wake_up_all(&event->waitq);
5004 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005005 rcu_read_unlock();
5006}
5007
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005008struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005009{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005010 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005011
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005012 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005013 rb = rcu_dereference(event->rb);
5014 if (rb) {
5015 if (!atomic_inc_not_zero(&rb->refcount))
5016 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005017 }
5018 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005019
Frederic Weisbecker76369132011-05-19 19:55:04 +02005020 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005021}
5022
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005023void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005024{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005025 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005026 return;
5027
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005028 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005029
Frederic Weisbecker76369132011-05-19 19:55:04 +02005030 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005031}
5032
5033static void perf_mmap_open(struct vm_area_struct *vma)
5034{
5035 struct perf_event *event = vma->vm_file->private_data;
5036
5037 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005038 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005039
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005040 if (vma->vm_pgoff)
5041 atomic_inc(&event->rb->aux_mmap_count);
5042
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005043 if (event->pmu->event_mapped)
5044 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005045}
5046
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005047static void perf_pmu_output_stop(struct perf_event *event);
5048
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005049/*
5050 * A buffer can be mmap()ed multiple times; either directly through the same
5051 * event, or through other events by use of perf_event_set_output().
5052 *
5053 * In order to undo the VM accounting done by perf_mmap() we need to destroy
5054 * the buffer here, where we still have a VM context. This means we need
5055 * to detach all events redirecting to us.
5056 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005057static void perf_mmap_close(struct vm_area_struct *vma)
5058{
5059 struct perf_event *event = vma->vm_file->private_data;
5060
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005061 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005062 struct user_struct *mmap_user = rb->mmap_user;
5063 int mmap_locked = rb->mmap_locked;
5064 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005065
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005066 if (event->pmu->event_unmapped)
5067 event->pmu->event_unmapped(event);
5068
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005069 /*
5070 * rb->aux_mmap_count will always drop before rb->mmap_count and
5071 * event->mmap_count, so it is ok to use event->mmap_mutex to
5072 * serialize with perf_mmap here.
5073 */
5074 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5075 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005076 /*
5077 * Stop all AUX events that are writing to this buffer,
5078 * so that we can free its AUX pages and corresponding PMU
5079 * data. Note that after rb::aux_mmap_count dropped to zero,
5080 * they won't start any more (see perf_aux_output_begin()).
5081 */
5082 perf_pmu_output_stop(event);
5083
5084 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005085 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
5086 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
5087
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005088 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005089 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005090 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
5091
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005092 mutex_unlock(&event->mmap_mutex);
5093 }
5094
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005095 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005096
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005097 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005098 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005099
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005100 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005101 mutex_unlock(&event->mmap_mutex);
5102
5103 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005104 if (atomic_read(&rb->mmap_count))
5105 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005106
5107 /*
5108 * No other mmap()s, detach from all other events that might redirect
5109 * into the now unreachable buffer. Somewhat complicated by the
5110 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5111 */
5112again:
5113 rcu_read_lock();
5114 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5115 if (!atomic_long_inc_not_zero(&event->refcount)) {
5116 /*
5117 * This event is en-route to free_event() which will
5118 * detach it and remove it from the list.
5119 */
5120 continue;
5121 }
5122 rcu_read_unlock();
5123
5124 mutex_lock(&event->mmap_mutex);
5125 /*
5126 * Check we didn't race with perf_event_set_output() which can
5127 * swizzle the rb from under us while we were waiting to
5128 * acquire mmap_mutex.
5129 *
5130 * If we find a different rb; ignore this event, a next
5131 * iteration will no longer find it on the list. We have to
5132 * still restart the iteration to make sure we're not now
5133 * iterating the wrong list.
5134 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005135 if (event->rb == rb)
5136 ring_buffer_attach(event, NULL);
5137
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005138 mutex_unlock(&event->mmap_mutex);
5139 put_event(event);
5140
5141 /*
5142 * Restart the iteration; either we're on the wrong list or
5143 * destroyed its integrity by doing a deletion.
5144 */
5145 goto again;
5146 }
5147 rcu_read_unlock();
5148
5149 /*
5150 * It could be there's still a few 0-ref events on the list; they'll
5151 * get cleaned up by free_event() -- they'll also still have their
5152 * ref on the rb and will free it whenever they are done with it.
5153 *
5154 * Aside from that, this buffer is 'fully' detached and unmapped,
5155 * undo the VM accounting.
5156 */
5157
5158 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
5159 vma->vm_mm->pinned_vm -= mmap_locked;
5160 free_uid(mmap_user);
5161
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005162out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005163 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005164}
5165
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04005166static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005167 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005168 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005169 .fault = perf_mmap_fault,
5170 .page_mkwrite = perf_mmap_fault,
5171};
5172
5173static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5174{
5175 struct perf_event *event = file->private_data;
5176 unsigned long user_locked, user_lock_limit;
5177 struct user_struct *user = current_user();
5178 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005179 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005180 unsigned long vma_size;
5181 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005182 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005183 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184
Peter Zijlstrac7920612010-05-18 10:33:24 +02005185 /*
5186 * Don't allow mmap() of inherited per-task counters. This would
5187 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005188 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005189 */
5190 if (event->cpu == -1 && event->attr.inherit)
5191 return -EINVAL;
5192
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005193 if (!(vma->vm_flags & VM_SHARED))
5194 return -EINVAL;
5195
5196 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005197
5198 if (vma->vm_pgoff == 0) {
5199 nr_pages = (vma_size / PAGE_SIZE) - 1;
5200 } else {
5201 /*
5202 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5203 * mapped, all subsequent mappings should have the same size
5204 * and offset. Must be above the normal perf buffer.
5205 */
5206 u64 aux_offset, aux_size;
5207
5208 if (!event->rb)
5209 return -EINVAL;
5210
5211 nr_pages = vma_size / PAGE_SIZE;
5212
5213 mutex_lock(&event->mmap_mutex);
5214 ret = -EINVAL;
5215
5216 rb = event->rb;
5217 if (!rb)
5218 goto aux_unlock;
5219
5220 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
5221 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
5222
5223 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5224 goto aux_unlock;
5225
5226 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5227 goto aux_unlock;
5228
5229 /* already mapped with a different offset */
5230 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5231 goto aux_unlock;
5232
5233 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5234 goto aux_unlock;
5235
5236 /* already mapped with a different size */
5237 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5238 goto aux_unlock;
5239
5240 if (!is_power_of_2(nr_pages))
5241 goto aux_unlock;
5242
5243 if (!atomic_inc_not_zero(&rb->mmap_count))
5244 goto aux_unlock;
5245
5246 if (rb_has_aux(rb)) {
5247 atomic_inc(&rb->aux_mmap_count);
5248 ret = 0;
5249 goto unlock;
5250 }
5251
5252 atomic_set(&rb->aux_mmap_count, 1);
5253 user_extra = nr_pages;
5254
5255 goto accounting;
5256 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005257
5258 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005259 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260 * can do bitmasks instead of modulo.
5261 */
Kan Liang2ed11312015-03-02 02:14:26 -05005262 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005263 return -EINVAL;
5264
5265 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5266 return -EINVAL;
5267
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005268 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005269again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005270 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005271 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005272 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005273 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005274 goto unlock;
5275 }
5276
5277 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5278 /*
5279 * Raced against perf_mmap_close() through
5280 * perf_event_set_output(). Try again, hope for better
5281 * luck.
5282 */
5283 mutex_unlock(&event->mmap_mutex);
5284 goto again;
5285 }
5286
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005287 goto unlock;
5288 }
5289
5290 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005291
5292accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005293 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5294
5295 /*
5296 * Increase the limit linearly with more CPUs:
5297 */
5298 user_lock_limit *= num_online_cpus();
5299
5300 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5301
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005302 if (user_locked > user_lock_limit)
5303 extra = user_locked - user_lock_limit;
5304
Jiri Slaby78d7d402010-03-05 13:42:54 -08005305 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005306 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005307 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005308
5309 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5310 !capable(CAP_IPC_LOCK)) {
5311 ret = -EPERM;
5312 goto unlock;
5313 }
5314
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005315 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005316
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005317 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005318 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005319
Frederic Weisbecker76369132011-05-19 19:55:04 +02005320 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005321 rb = rb_alloc(nr_pages,
5322 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5323 event->cpu, flags);
5324
5325 if (!rb) {
5326 ret = -ENOMEM;
5327 goto unlock;
5328 }
5329
5330 atomic_set(&rb->mmap_count, 1);
5331 rb->mmap_user = get_current_user();
5332 rb->mmap_locked = extra;
5333
5334 ring_buffer_attach(event, rb);
5335
5336 perf_event_init_userpage(event);
5337 perf_event_update_userpage(event);
5338 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005339 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5340 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005341 if (!ret)
5342 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005343 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005346 if (!ret) {
5347 atomic_long_add(user_extra, &user->locked_vm);
5348 vma->vm_mm->pinned_vm += extra;
5349
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005350 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005351 } else if (rb) {
5352 atomic_dec(&rb->mmap_count);
5353 }
5354aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005355 mutex_unlock(&event->mmap_mutex);
5356
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005357 /*
5358 * Since pinned accounting is per vm we cannot allow fork() to copy our
5359 * vma.
5360 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005361 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005362 vma->vm_ops = &perf_mmap_vmops;
5363
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005364 if (event->pmu->event_mapped)
5365 event->pmu->event_mapped(event);
5366
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005367 return ret;
5368}
5369
5370static int perf_fasync(int fd, struct file *filp, int on)
5371{
Al Viro496ad9a2013-01-23 17:07:38 -05005372 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005373 struct perf_event *event = filp->private_data;
5374 int retval;
5375
Al Viro59551022016-01-22 15:40:57 -05005376 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005377 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005378 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005379
5380 if (retval < 0)
5381 return retval;
5382
5383 return 0;
5384}
5385
5386static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005387 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005388 .release = perf_release,
5389 .read = perf_read,
5390 .poll = perf_poll,
5391 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005392 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005393 .mmap = perf_mmap,
5394 .fasync = perf_fasync,
5395};
5396
5397/*
5398 * Perf event wakeup
5399 *
5400 * If there's data, ensure we set the poll() state and publish everything
5401 * to user-space before waking everybody up.
5402 */
5403
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005404static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5405{
5406 /* only the parent has fasync state */
5407 if (event->parent)
5408 event = event->parent;
5409 return &event->fasync;
5410}
5411
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005412void perf_event_wakeup(struct perf_event *event)
5413{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005414 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005415
5416 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005417 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005418 event->pending_kill = 0;
5419 }
5420}
5421
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005422static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423{
5424 struct perf_event *event = container_of(entry,
5425 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005426 int rctx;
5427
5428 rctx = perf_swevent_get_recursion_context();
5429 /*
5430 * If we 'fail' here, that's OK, it means recursion is already disabled
5431 * and we won't recurse 'further'.
5432 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005433
5434 if (event->pending_disable) {
5435 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005436 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005437 }
5438
5439 if (event->pending_wakeup) {
5440 event->pending_wakeup = 0;
5441 perf_event_wakeup(event);
5442 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005443
5444 if (rctx >= 0)
5445 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005446}
5447
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005448/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005449 * We assume there is only KVM supporting the callbacks.
5450 * Later on, we might change it to a list if there is
5451 * another virtualization implementation supporting the callbacks.
5452 */
5453struct perf_guest_info_callbacks *perf_guest_cbs;
5454
5455int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5456{
5457 perf_guest_cbs = cbs;
5458 return 0;
5459}
5460EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5461
5462int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5463{
5464 perf_guest_cbs = NULL;
5465 return 0;
5466}
5467EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5468
Jiri Olsa40189942012-08-07 15:20:37 +02005469static void
5470perf_output_sample_regs(struct perf_output_handle *handle,
5471 struct pt_regs *regs, u64 mask)
5472{
5473 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305474 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02005475
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305476 bitmap_from_u64(_mask, mask);
5477 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02005478 u64 val;
5479
5480 val = perf_reg_value(regs, bit);
5481 perf_output_put(handle, val);
5482 }
5483}
5484
Stephane Eranian60e23642014-09-24 13:48:37 +02005485static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005486 struct pt_regs *regs,
5487 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005488{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005489 if (user_mode(regs)) {
5490 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005491 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005492 } else if (current->mm) {
5493 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005494 } else {
5495 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5496 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005497 }
5498}
5499
Stephane Eranian60e23642014-09-24 13:48:37 +02005500static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5501 struct pt_regs *regs)
5502{
5503 regs_intr->regs = regs;
5504 regs_intr->abi = perf_reg_abi(current);
5505}
5506
5507
Jiri Olsac5ebced2012-08-07 15:20:40 +02005508/*
5509 * Get remaining task size from user stack pointer.
5510 *
5511 * It'd be better to take stack vma map and limit this more
5512 * precisly, but there's no way to get it safely under interrupt,
5513 * so using TASK_SIZE as limit.
5514 */
5515static u64 perf_ustack_task_size(struct pt_regs *regs)
5516{
5517 unsigned long addr = perf_user_stack_pointer(regs);
5518
5519 if (!addr || addr >= TASK_SIZE)
5520 return 0;
5521
5522 return TASK_SIZE - addr;
5523}
5524
5525static u16
5526perf_sample_ustack_size(u16 stack_size, u16 header_size,
5527 struct pt_regs *regs)
5528{
5529 u64 task_size;
5530
5531 /* No regs, no stack pointer, no dump. */
5532 if (!regs)
5533 return 0;
5534
5535 /*
5536 * Check if we fit in with the requested stack size into the:
5537 * - TASK_SIZE
5538 * If we don't, we limit the size to the TASK_SIZE.
5539 *
5540 * - remaining sample size
5541 * If we don't, we customize the stack size to
5542 * fit in to the remaining sample size.
5543 */
5544
5545 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5546 stack_size = min(stack_size, (u16) task_size);
5547
5548 /* Current header size plus static size and dynamic size. */
5549 header_size += 2 * sizeof(u64);
5550
5551 /* Do we fit in with the current stack dump size? */
5552 if ((u16) (header_size + stack_size) < header_size) {
5553 /*
5554 * If we overflow the maximum size for the sample,
5555 * we customize the stack dump size to fit in.
5556 */
5557 stack_size = USHRT_MAX - header_size - sizeof(u64);
5558 stack_size = round_up(stack_size, sizeof(u64));
5559 }
5560
5561 return stack_size;
5562}
5563
5564static void
5565perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5566 struct pt_regs *regs)
5567{
5568 /* Case of a kernel thread, nothing to dump */
5569 if (!regs) {
5570 u64 size = 0;
5571 perf_output_put(handle, size);
5572 } else {
5573 unsigned long sp;
5574 unsigned int rem;
5575 u64 dyn_size;
Yabin Cuia64fbec2018-08-23 15:59:35 -07005576 mm_segment_t fs;
Jiri Olsac5ebced2012-08-07 15:20:40 +02005577
5578 /*
5579 * We dump:
5580 * static size
5581 * - the size requested by user or the best one we can fit
5582 * in to the sample max size
5583 * data
5584 * - user stack dump data
5585 * dynamic size
5586 * - the actual dumped size
5587 */
5588
5589 /* Static size. */
5590 perf_output_put(handle, dump_size);
5591
5592 /* Data. */
5593 sp = perf_user_stack_pointer(regs);
Yabin Cuia64fbec2018-08-23 15:59:35 -07005594 fs = get_fs();
5595 set_fs(USER_DS);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005596 rem = __output_copy_user(handle, (void *) sp, dump_size);
Yabin Cuia64fbec2018-08-23 15:59:35 -07005597 set_fs(fs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005598 dyn_size = dump_size - rem;
5599
5600 perf_output_skip(handle, rem);
5601
5602 /* Dynamic size. */
5603 perf_output_put(handle, dyn_size);
5604 }
5605}
5606
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005607static void __perf_event_header__init_id(struct perf_event_header *header,
5608 struct perf_sample_data *data,
5609 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005610{
5611 u64 sample_type = event->attr.sample_type;
5612
5613 data->type = sample_type;
5614 header->size += event->id_header_size;
5615
5616 if (sample_type & PERF_SAMPLE_TID) {
5617 /* namespace issues */
5618 data->tid_entry.pid = perf_event_pid(event, current);
5619 data->tid_entry.tid = perf_event_tid(event, current);
5620 }
5621
5622 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005623 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005624
Adrian Hunterff3d5272013-08-27 11:23:07 +03005625 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005626 data->id = primary_event_id(event);
5627
5628 if (sample_type & PERF_SAMPLE_STREAM_ID)
5629 data->stream_id = event->id;
5630
5631 if (sample_type & PERF_SAMPLE_CPU) {
5632 data->cpu_entry.cpu = raw_smp_processor_id();
5633 data->cpu_entry.reserved = 0;
5634 }
5635}
5636
Frederic Weisbecker76369132011-05-19 19:55:04 +02005637void perf_event_header__init_id(struct perf_event_header *header,
5638 struct perf_sample_data *data,
5639 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005640{
5641 if (event->attr.sample_id_all)
5642 __perf_event_header__init_id(header, data, event);
5643}
5644
5645static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5646 struct perf_sample_data *data)
5647{
5648 u64 sample_type = data->type;
5649
5650 if (sample_type & PERF_SAMPLE_TID)
5651 perf_output_put(handle, data->tid_entry);
5652
5653 if (sample_type & PERF_SAMPLE_TIME)
5654 perf_output_put(handle, data->time);
5655
5656 if (sample_type & PERF_SAMPLE_ID)
5657 perf_output_put(handle, data->id);
5658
5659 if (sample_type & PERF_SAMPLE_STREAM_ID)
5660 perf_output_put(handle, data->stream_id);
5661
5662 if (sample_type & PERF_SAMPLE_CPU)
5663 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005664
5665 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5666 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005667}
5668
Frederic Weisbecker76369132011-05-19 19:55:04 +02005669void perf_event__output_id_sample(struct perf_event *event,
5670 struct perf_output_handle *handle,
5671 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005672{
5673 if (event->attr.sample_id_all)
5674 __perf_event__output_id_sample(handle, sample);
5675}
5676
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005677static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005678 struct perf_event *event,
5679 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005680{
5681 u64 read_format = event->attr.read_format;
5682 u64 values[4];
5683 int n = 0;
5684
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005685 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005686 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005687 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005688 atomic64_read(&event->child_total_time_enabled);
5689 }
5690 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005691 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005692 atomic64_read(&event->child_total_time_running);
5693 }
5694 if (read_format & PERF_FORMAT_ID)
5695 values[n++] = primary_event_id(event);
5696
Frederic Weisbecker76369132011-05-19 19:55:04 +02005697 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005698}
5699
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005700static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005701 struct perf_event *event,
5702 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005703{
5704 struct perf_event *leader = event->group_leader, *sub;
5705 u64 read_format = event->attr.read_format;
5706 u64 values[5];
5707 int n = 0;
5708
5709 values[n++] = 1 + leader->nr_siblings;
5710
5711 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005712 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005713
5714 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005715 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005716
Peter Zijlstrabc09bf82018-03-09 12:52:04 +01005717 if ((leader != event) &&
5718 (leader->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005719 leader->pmu->read(leader);
5720
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005721 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005722 if (read_format & PERF_FORMAT_ID)
5723 values[n++] = primary_event_id(leader);
5724
Frederic Weisbecker76369132011-05-19 19:55:04 +02005725 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005726
5727 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5728 n = 0;
5729
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005730 if ((sub != event) &&
5731 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005732 sub->pmu->read(sub);
5733
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005734 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005735 if (read_format & PERF_FORMAT_ID)
5736 values[n++] = primary_event_id(sub);
5737
Frederic Weisbecker76369132011-05-19 19:55:04 +02005738 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005739 }
5740}
5741
Stephane Eranianeed01522010-10-26 16:08:01 +02005742#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5743 PERF_FORMAT_TOTAL_TIME_RUNNING)
5744
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02005745/*
5746 * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
5747 *
5748 * The problem is that its both hard and excessively expensive to iterate the
5749 * child list, not to mention that its impossible to IPI the children running
5750 * on another CPU, from interrupt/NMI context.
5751 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005752static void perf_output_read(struct perf_output_handle *handle,
5753 struct perf_event *event)
5754{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005755 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005756 u64 read_format = event->attr.read_format;
5757
5758 /*
5759 * compute total_time_enabled, total_time_running
5760 * based on snapshot values taken when the event
5761 * was last scheduled in.
5762 *
5763 * we cannot simply called update_context_time()
5764 * because of locking issue as we are called in
5765 * NMI context
5766 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005767 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005768 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005769
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005770 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005771 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005772 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005773 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005774}
5775
5776void perf_output_sample(struct perf_output_handle *handle,
5777 struct perf_event_header *header,
5778 struct perf_sample_data *data,
5779 struct perf_event *event)
5780{
5781 u64 sample_type = data->type;
5782
5783 perf_output_put(handle, *header);
5784
Adrian Hunterff3d5272013-08-27 11:23:07 +03005785 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5786 perf_output_put(handle, data->id);
5787
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005788 if (sample_type & PERF_SAMPLE_IP)
5789 perf_output_put(handle, data->ip);
5790
5791 if (sample_type & PERF_SAMPLE_TID)
5792 perf_output_put(handle, data->tid_entry);
5793
5794 if (sample_type & PERF_SAMPLE_TIME)
5795 perf_output_put(handle, data->time);
5796
5797 if (sample_type & PERF_SAMPLE_ADDR)
5798 perf_output_put(handle, data->addr);
5799
5800 if (sample_type & PERF_SAMPLE_ID)
5801 perf_output_put(handle, data->id);
5802
5803 if (sample_type & PERF_SAMPLE_STREAM_ID)
5804 perf_output_put(handle, data->stream_id);
5805
5806 if (sample_type & PERF_SAMPLE_CPU)
5807 perf_output_put(handle, data->cpu_entry);
5808
5809 if (sample_type & PERF_SAMPLE_PERIOD)
5810 perf_output_put(handle, data->period);
5811
5812 if (sample_type & PERF_SAMPLE_READ)
5813 perf_output_read(handle, event);
5814
5815 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5816 if (data->callchain) {
5817 int size = 1;
5818
5819 if (data->callchain)
5820 size += data->callchain->nr;
5821
5822 size *= sizeof(u64);
5823
Frederic Weisbecker76369132011-05-19 19:55:04 +02005824 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005825 } else {
5826 u64 nr = 0;
5827 perf_output_put(handle, nr);
5828 }
5829 }
5830
5831 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005832 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005833
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005834 if (raw) {
5835 struct perf_raw_frag *frag = &raw->frag;
5836
5837 perf_output_put(handle, raw->size);
5838 do {
5839 if (frag->copy) {
5840 __output_custom(handle, frag->copy,
5841 frag->data, frag->size);
5842 } else {
5843 __output_copy(handle, frag->data,
5844 frag->size);
5845 }
5846 if (perf_raw_frag_last(frag))
5847 break;
5848 frag = frag->next;
5849 } while (1);
5850 if (frag->pad)
5851 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005852 } else {
5853 struct {
5854 u32 size;
5855 u32 data;
5856 } raw = {
5857 .size = sizeof(u32),
5858 .data = 0,
5859 };
5860 perf_output_put(handle, raw);
5861 }
5862 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005863
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005864 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5865 if (data->br_stack) {
5866 size_t size;
5867
5868 size = data->br_stack->nr
5869 * sizeof(struct perf_branch_entry);
5870
5871 perf_output_put(handle, data->br_stack->nr);
5872 perf_output_copy(handle, data->br_stack->entries, size);
5873 } else {
5874 /*
5875 * we always store at least the value of nr
5876 */
5877 u64 nr = 0;
5878 perf_output_put(handle, nr);
5879 }
5880 }
Jiri Olsa40189942012-08-07 15:20:37 +02005881
5882 if (sample_type & PERF_SAMPLE_REGS_USER) {
5883 u64 abi = data->regs_user.abi;
5884
5885 /*
5886 * If there are no regs to dump, notice it through
5887 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5888 */
5889 perf_output_put(handle, abi);
5890
5891 if (abi) {
5892 u64 mask = event->attr.sample_regs_user;
5893 perf_output_sample_regs(handle,
5894 data->regs_user.regs,
5895 mask);
5896 }
5897 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005898
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005899 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005900 perf_output_sample_ustack(handle,
5901 data->stack_user_size,
5902 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005903 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005904
5905 if (sample_type & PERF_SAMPLE_WEIGHT)
5906 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005907
5908 if (sample_type & PERF_SAMPLE_DATA_SRC)
5909 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005910
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005911 if (sample_type & PERF_SAMPLE_TRANSACTION)
5912 perf_output_put(handle, data->txn);
5913
Stephane Eranian60e23642014-09-24 13:48:37 +02005914 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5915 u64 abi = data->regs_intr.abi;
5916 /*
5917 * If there are no regs to dump, notice it through
5918 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5919 */
5920 perf_output_put(handle, abi);
5921
5922 if (abi) {
5923 u64 mask = event->attr.sample_regs_intr;
5924
5925 perf_output_sample_regs(handle,
5926 data->regs_intr.regs,
5927 mask);
5928 }
5929 }
5930
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005931 if (!event->attr.watermark) {
5932 int wakeup_events = event->attr.wakeup_events;
5933
5934 if (wakeup_events) {
5935 struct ring_buffer *rb = handle->rb;
5936 int events = local_inc_return(&rb->events);
5937
5938 if (events >= wakeup_events) {
5939 local_sub(wakeup_events, &rb->events);
5940 local_inc(&rb->wakeup);
5941 }
5942 }
5943 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005944}
5945
5946void perf_prepare_sample(struct perf_event_header *header,
5947 struct perf_sample_data *data,
5948 struct perf_event *event,
5949 struct pt_regs *regs)
5950{
5951 u64 sample_type = event->attr.sample_type;
5952
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005953 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005954 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005955
5956 header->misc = 0;
5957 header->misc |= perf_misc_flags(regs);
5958
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005959 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005960
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005961 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005962 data->ip = perf_instruction_pointer(regs);
5963
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005964 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5965 int size = 1;
5966
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005967 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005968
5969 if (data->callchain)
5970 size += data->callchain->nr;
5971
5972 header->size += size * sizeof(u64);
5973 }
5974
5975 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005976 struct perf_raw_record *raw = data->raw;
5977 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005978
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005979 if (raw) {
5980 struct perf_raw_frag *frag = &raw->frag;
5981 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005982
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005983 do {
5984 sum += frag->size;
5985 if (perf_raw_frag_last(frag))
5986 break;
5987 frag = frag->next;
5988 } while (1);
5989
5990 size = round_up(sum + sizeof(u32), sizeof(u64));
5991 raw->size = size - sizeof(u32);
5992 frag->pad = raw->size - sum;
5993 } else {
5994 size = sizeof(u64);
5995 }
5996
5997 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005998 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005999
6000 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
6001 int size = sizeof(u64); /* nr */
6002 if (data->br_stack) {
6003 size += data->br_stack->nr
6004 * sizeof(struct perf_branch_entry);
6005 }
6006 header->size += size;
6007 }
Jiri Olsa40189942012-08-07 15:20:37 +02006008
Peter Zijlstra25657112014-09-24 13:48:42 +02006009 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006010 perf_sample_regs_user(&data->regs_user, regs,
6011 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02006012
Jiri Olsa40189942012-08-07 15:20:37 +02006013 if (sample_type & PERF_SAMPLE_REGS_USER) {
6014 /* regs dump ABI info */
6015 int size = sizeof(u64);
6016
Jiri Olsa40189942012-08-07 15:20:37 +02006017 if (data->regs_user.regs) {
6018 u64 mask = event->attr.sample_regs_user;
6019 size += hweight64(mask) * sizeof(u64);
6020 }
6021
6022 header->size += size;
6023 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006024
6025 if (sample_type & PERF_SAMPLE_STACK_USER) {
6026 /*
6027 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
6028 * processed as the last one or have additional check added
6029 * in case new sample type is added, because we could eat
6030 * up the rest of the sample size.
6031 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02006032 u16 stack_size = event->attr.sample_stack_user;
6033 u16 size = sizeof(u64);
6034
Jiri Olsac5ebced2012-08-07 15:20:40 +02006035 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02006036 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006037
6038 /*
6039 * If there is something to dump, add space for the dump
6040 * itself and for the field that tells the dynamic size,
6041 * which is how many have been actually dumped.
6042 */
6043 if (stack_size)
6044 size += sizeof(u64) + stack_size;
6045
6046 data->stack_user_size = stack_size;
6047 header->size += size;
6048 }
Stephane Eranian60e23642014-09-24 13:48:37 +02006049
6050 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6051 /* regs dump ABI info */
6052 int size = sizeof(u64);
6053
6054 perf_sample_regs_intr(&data->regs_intr, regs);
6055
6056 if (data->regs_intr.regs) {
6057 u64 mask = event->attr.sample_regs_intr;
6058
6059 size += hweight64(mask) * sizeof(u64);
6060 }
6061
6062 header->size += size;
6063 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006064}
6065
Wang Nan9ecda412016-04-05 14:11:18 +00006066static void __always_inline
6067__perf_event_output(struct perf_event *event,
6068 struct perf_sample_data *data,
6069 struct pt_regs *regs,
6070 int (*output_begin)(struct perf_output_handle *,
6071 struct perf_event *,
6072 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006073{
6074 struct perf_output_handle handle;
6075 struct perf_event_header header;
6076
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006077 /* protect the callchain buffers */
6078 rcu_read_lock();
6079
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006080 perf_prepare_sample(&header, data, event, regs);
6081
Wang Nan9ecda412016-04-05 14:11:18 +00006082 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006083 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006084
6085 perf_output_sample(&handle, &header, data, event);
6086
6087 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006088
6089exit:
6090 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006091}
6092
Wang Nan9ecda412016-04-05 14:11:18 +00006093void
6094perf_event_output_forward(struct perf_event *event,
6095 struct perf_sample_data *data,
6096 struct pt_regs *regs)
6097{
6098 __perf_event_output(event, data, regs, perf_output_begin_forward);
6099}
6100
6101void
6102perf_event_output_backward(struct perf_event *event,
6103 struct perf_sample_data *data,
6104 struct pt_regs *regs)
6105{
6106 __perf_event_output(event, data, regs, perf_output_begin_backward);
6107}
6108
6109void
6110perf_event_output(struct perf_event *event,
6111 struct perf_sample_data *data,
6112 struct pt_regs *regs)
6113{
6114 __perf_event_output(event, data, regs, perf_output_begin);
6115}
6116
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006117/*
6118 * read event_id
6119 */
6120
6121struct perf_read_event {
6122 struct perf_event_header header;
6123
6124 u32 pid;
6125 u32 tid;
6126};
6127
6128static void
6129perf_event_read_event(struct perf_event *event,
6130 struct task_struct *task)
6131{
6132 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006133 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006134 struct perf_read_event read_event = {
6135 .header = {
6136 .type = PERF_RECORD_READ,
6137 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006138 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006139 },
6140 .pid = perf_event_pid(event, task),
6141 .tid = perf_event_tid(event, task),
6142 };
6143 int ret;
6144
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006145 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006146 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006147 if (ret)
6148 return;
6149
6150 perf_output_put(&handle, read_event);
6151 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006152 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006153
6154 perf_output_end(&handle);
6155}
6156
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006157typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006158
6159static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006160perf_iterate_ctx(struct perf_event_context *ctx,
6161 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006162 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02006163{
6164 struct perf_event *event;
6165
6166 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006167 if (!all) {
6168 if (event->state < PERF_EVENT_STATE_INACTIVE)
6169 continue;
6170 if (!event_filter_match(event))
6171 continue;
6172 }
6173
Jiri Olsa67516842013-07-09 18:56:31 +02006174 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006175 }
6176}
6177
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006178static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07006179{
6180 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
6181 struct perf_event *event;
6182
6183 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02006184 /*
6185 * Skip events that are not fully formed yet; ensure that
6186 * if we observe event->ctx, both event and ctx will be
6187 * complete enough. See perf_install_in_context().
6188 */
6189 if (!smp_load_acquire(&event->ctx))
6190 continue;
6191
Kan Liangf2fb6be2016-03-23 11:24:37 -07006192 if (event->state < PERF_EVENT_STATE_INACTIVE)
6193 continue;
6194 if (!event_filter_match(event))
6195 continue;
6196 output(event, data);
6197 }
6198}
6199
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006200/*
6201 * Iterate all events that need to receive side-band events.
6202 *
6203 * For new callers; ensure that account_pmu_sb_event() includes
6204 * your event, otherwise it might not get delivered.
6205 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006206static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006207perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006208 struct perf_event_context *task_ctx)
6209{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006210 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02006211 int ctxn;
6212
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006213 rcu_read_lock();
6214 preempt_disable();
6215
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006216 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006217 * If we have task_ctx != NULL we only notify the task context itself.
6218 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006219 * context.
6220 */
6221 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006222 perf_iterate_ctx(task_ctx, output, data, false);
6223 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006224 }
6225
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006226 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07006227
6228 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02006229 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6230 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006231 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006232 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006233done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07006234 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02006235 rcu_read_unlock();
6236}
6237
Alexander Shishkin375637b2016-04-27 18:44:46 +03006238/*
6239 * Clear all file-based filters at exec, they'll have to be
6240 * re-instated when/if these objects are mmapped again.
6241 */
6242static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
6243{
6244 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6245 struct perf_addr_filter *filter;
6246 unsigned int restart = 0, count = 0;
6247 unsigned long flags;
6248
6249 if (!has_addr_filter(event))
6250 return;
6251
6252 raw_spin_lock_irqsave(&ifh->lock, flags);
6253 list_for_each_entry(filter, &ifh->list, entry) {
6254 if (filter->inode) {
6255 event->addr_filters_offs[count] = 0;
6256 restart++;
6257 }
6258
6259 count++;
6260 }
6261
6262 if (restart)
6263 event->addr_filters_gen++;
6264 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6265
6266 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006267 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006268}
6269
6270void perf_event_exec(void)
6271{
6272 struct perf_event_context *ctx;
6273 int ctxn;
6274
6275 rcu_read_lock();
6276 for_each_task_context_nr(ctxn) {
6277 ctx = current->perf_event_ctxp[ctxn];
6278 if (!ctx)
6279 continue;
6280
6281 perf_event_enable_on_exec(ctxn);
6282
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006283 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03006284 true);
6285 }
6286 rcu_read_unlock();
6287}
6288
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006289struct remote_output {
6290 struct ring_buffer *rb;
6291 int err;
6292};
6293
6294static void __perf_event_output_stop(struct perf_event *event, void *data)
6295{
6296 struct perf_event *parent = event->parent;
6297 struct remote_output *ro = data;
6298 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03006299 struct stop_event_data sd = {
6300 .event = event,
6301 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006302
6303 if (!has_aux(event))
6304 return;
6305
6306 if (!parent)
6307 parent = event;
6308
6309 /*
6310 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03006311 * ring-buffer, but it will be the child that's actually using it.
6312 *
6313 * We are using event::rb to determine if the event should be stopped,
6314 * however this may race with ring_buffer_attach() (through set_output),
6315 * which will make us skip the event that actually needs to be stopped.
6316 * So ring_buffer_attach() has to stop an aux event before re-assigning
6317 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006318 */
6319 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006320 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006321}
6322
6323static int __perf_pmu_output_stop(void *info)
6324{
6325 struct perf_event *event = info;
6326 struct pmu *pmu = event->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01006327 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006328 struct remote_output ro = {
6329 .rb = event->rb,
6330 };
6331
6332 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006333 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006334 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006335 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006336 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006337 rcu_read_unlock();
6338
6339 return ro.err;
6340}
6341
6342static void perf_pmu_output_stop(struct perf_event *event)
6343{
6344 struct perf_event *iter;
6345 int err, cpu;
6346
6347restart:
6348 rcu_read_lock();
6349 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6350 /*
6351 * For per-CPU events, we need to make sure that neither they
6352 * nor their children are running; for cpu==-1 events it's
6353 * sufficient to stop the event itself if it's active, since
6354 * it can't have children.
6355 */
6356 cpu = iter->cpu;
6357 if (cpu == -1)
6358 cpu = READ_ONCE(iter->oncpu);
6359
6360 if (cpu == -1)
6361 continue;
6362
6363 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6364 if (err == -EAGAIN) {
6365 rcu_read_unlock();
6366 goto restart;
6367 }
6368 }
6369 rcu_read_unlock();
6370}
6371
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006372/*
6373 * task tracking -- fork/exit
6374 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006375 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006376 */
6377
6378struct perf_task_event {
6379 struct task_struct *task;
6380 struct perf_event_context *task_ctx;
6381
6382 struct {
6383 struct perf_event_header header;
6384
6385 u32 pid;
6386 u32 ppid;
6387 u32 tid;
6388 u32 ptid;
6389 u64 time;
6390 } event_id;
6391};
6392
Jiri Olsa67516842013-07-09 18:56:31 +02006393static int perf_event_task_match(struct perf_event *event)
6394{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006395 return event->attr.comm || event->attr.mmap ||
6396 event->attr.mmap2 || event->attr.mmap_data ||
6397 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006398}
6399
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006400static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006401 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006402{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006403 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006404 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006405 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006406 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006407 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006408
Jiri Olsa67516842013-07-09 18:56:31 +02006409 if (!perf_event_task_match(event))
6410 return;
6411
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006412 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006413
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006414 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006415 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006416 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006417 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006418
6419 task_event->event_id.pid = perf_event_pid(event, task);
6420 task_event->event_id.ppid = perf_event_pid(event, current);
6421
6422 task_event->event_id.tid = perf_event_tid(event, task);
6423 task_event->event_id.ptid = perf_event_tid(event, current);
6424
Peter Zijlstra34f43922015-02-20 14:05:38 +01006425 task_event->event_id.time = perf_event_clock(event);
6426
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006427 perf_output_put(&handle, task_event->event_id);
6428
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006429 perf_event__output_id_sample(event, &handle, &sample);
6430
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006431 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006432out:
6433 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006434}
6435
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006436static void perf_event_task(struct task_struct *task,
6437 struct perf_event_context *task_ctx,
6438 int new)
6439{
6440 struct perf_task_event task_event;
6441
6442 if (!atomic_read(&nr_comm_events) &&
6443 !atomic_read(&nr_mmap_events) &&
6444 !atomic_read(&nr_task_events))
6445 return;
6446
6447 task_event = (struct perf_task_event){
6448 .task = task,
6449 .task_ctx = task_ctx,
6450 .event_id = {
6451 .header = {
6452 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6453 .misc = 0,
6454 .size = sizeof(task_event.event_id),
6455 },
6456 /* .pid */
6457 /* .ppid */
6458 /* .tid */
6459 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006460 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006461 },
6462 };
6463
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006464 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006465 &task_event,
6466 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006467}
6468
6469void perf_event_fork(struct task_struct *task)
6470{
6471 perf_event_task(task, NULL, 1);
6472}
6473
6474/*
6475 * comm tracking
6476 */
6477
6478struct perf_comm_event {
6479 struct task_struct *task;
6480 char *comm;
6481 int comm_size;
6482
6483 struct {
6484 struct perf_event_header header;
6485
6486 u32 pid;
6487 u32 tid;
6488 } event_id;
6489};
6490
Jiri Olsa67516842013-07-09 18:56:31 +02006491static int perf_event_comm_match(struct perf_event *event)
6492{
6493 return event->attr.comm;
6494}
6495
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006496static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006497 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006498{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006499 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006500 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006501 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006502 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006503 int ret;
6504
Jiri Olsa67516842013-07-09 18:56:31 +02006505 if (!perf_event_comm_match(event))
6506 return;
6507
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006508 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6509 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006510 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006511
6512 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006513 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006514
6515 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6516 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6517
6518 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006519 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006520 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006521
6522 perf_event__output_id_sample(event, &handle, &sample);
6523
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006524 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006525out:
6526 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006527}
6528
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006529static void perf_event_comm_event(struct perf_comm_event *comm_event)
6530{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006531 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006532 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533
6534 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006535 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006536 size = ALIGN(strlen(comm)+1, sizeof(u64));
6537
6538 comm_event->comm = comm;
6539 comm_event->comm_size = size;
6540
6541 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006542
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006543 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006544 comm_event,
6545 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006546}
6547
Adrian Hunter82b89772014-05-28 11:45:04 +03006548void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006549{
6550 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006551
6552 if (!atomic_read(&nr_comm_events))
6553 return;
6554
6555 comm_event = (struct perf_comm_event){
6556 .task = task,
6557 /* .comm */
6558 /* .comm_size */
6559 .event_id = {
6560 .header = {
6561 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006562 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006563 /* .size */
6564 },
6565 /* .pid */
6566 /* .tid */
6567 },
6568 };
6569
6570 perf_event_comm_event(&comm_event);
6571}
6572
6573/*
6574 * mmap tracking
6575 */
6576
6577struct perf_mmap_event {
6578 struct vm_area_struct *vma;
6579
6580 const char *file_name;
6581 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006582 int maj, min;
6583 u64 ino;
6584 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006585 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006586
6587 struct {
6588 struct perf_event_header header;
6589
6590 u32 pid;
6591 u32 tid;
6592 u64 start;
6593 u64 len;
6594 u64 pgoff;
6595 } event_id;
6596};
6597
Jiri Olsa67516842013-07-09 18:56:31 +02006598static int perf_event_mmap_match(struct perf_event *event,
6599 void *data)
6600{
6601 struct perf_mmap_event *mmap_event = data;
6602 struct vm_area_struct *vma = mmap_event->vma;
6603 int executable = vma->vm_flags & VM_EXEC;
6604
6605 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006606 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006607}
6608
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006609static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006610 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006611{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006612 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006613 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006614 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006615 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006616 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006617
Jiri Olsa67516842013-07-09 18:56:31 +02006618 if (!perf_event_mmap_match(event, data))
6619 return;
6620
Stephane Eranian13d7a242013-08-21 12:10:24 +02006621 if (event->attr.mmap2) {
6622 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6623 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6624 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6625 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006626 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006627 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6628 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006629 }
6630
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006631 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6632 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006633 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006634 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006635 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006636
6637 mmap_event->event_id.pid = perf_event_pid(event, current);
6638 mmap_event->event_id.tid = perf_event_tid(event, current);
6639
6640 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006641
6642 if (event->attr.mmap2) {
6643 perf_output_put(&handle, mmap_event->maj);
6644 perf_output_put(&handle, mmap_event->min);
6645 perf_output_put(&handle, mmap_event->ino);
6646 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006647 perf_output_put(&handle, mmap_event->prot);
6648 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006649 }
6650
Frederic Weisbecker76369132011-05-19 19:55:04 +02006651 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006652 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006653
6654 perf_event__output_id_sample(event, &handle, &sample);
6655
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006656 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006657out:
6658 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006659}
6660
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006661static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6662{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006663 struct vm_area_struct *vma = mmap_event->vma;
6664 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006665 int maj = 0, min = 0;
6666 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006667 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006668 unsigned int size;
6669 char tmp[16];
6670 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006671 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006672
Peter Zijlstrab41615a2017-01-26 23:15:08 +01006673 if (vma->vm_flags & VM_READ)
6674 prot |= PROT_READ;
6675 if (vma->vm_flags & VM_WRITE)
6676 prot |= PROT_WRITE;
6677 if (vma->vm_flags & VM_EXEC)
6678 prot |= PROT_EXEC;
6679
6680 if (vma->vm_flags & VM_MAYSHARE)
6681 flags = MAP_SHARED;
6682 else
6683 flags = MAP_PRIVATE;
6684
6685 if (vma->vm_flags & VM_DENYWRITE)
6686 flags |= MAP_DENYWRITE;
6687 if (vma->vm_flags & VM_MAYEXEC)
6688 flags |= MAP_EXECUTABLE;
6689 if (vma->vm_flags & VM_LOCKED)
6690 flags |= MAP_LOCKED;
6691 if (vma->vm_flags & VM_HUGETLB)
6692 flags |= MAP_HUGETLB;
6693
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006694 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006695 struct inode *inode;
6696 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006697
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006698 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006699 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006700 name = "//enomem";
6701 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006702 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006703 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006704 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006705 * need to add enough zero bytes after the string to handle
6706 * the 64bit alignment we do later.
6707 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006708 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006709 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006710 name = "//toolong";
6711 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006712 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006713 inode = file_inode(vma->vm_file);
6714 dev = inode->i_sb->s_dev;
6715 ino = inode->i_ino;
6716 gen = inode->i_generation;
6717 maj = MAJOR(dev);
6718 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006719
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006720 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006721 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006722 if (vma->vm_ops && vma->vm_ops->name) {
6723 name = (char *) vma->vm_ops->name(vma);
6724 if (name)
6725 goto cpy_name;
6726 }
6727
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006728 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006729 if (name)
6730 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006731
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006732 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006733 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006734 name = "[heap]";
6735 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006736 }
6737 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006738 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006739 name = "[stack]";
6740 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006741 }
6742
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006743 name = "//anon";
6744 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006745 }
6746
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006747cpy_name:
6748 strlcpy(tmp, name, sizeof(tmp));
6749 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006750got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006751 /*
6752 * Since our buffer works in 8 byte units we need to align our string
6753 * size to a multiple of 8. However, we must guarantee the tail end is
6754 * zero'd out to avoid leaking random bits to userspace.
6755 */
6756 size = strlen(name)+1;
6757 while (!IS_ALIGNED(size, sizeof(u64)))
6758 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006759
6760 mmap_event->file_name = name;
6761 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006762 mmap_event->maj = maj;
6763 mmap_event->min = min;
6764 mmap_event->ino = ino;
6765 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006766 mmap_event->prot = prot;
6767 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006768
Stephane Eranian2fe85422013-01-24 16:10:39 +01006769 if (!(vma->vm_flags & VM_EXEC))
6770 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6771
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006772 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6773
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006774 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006775 mmap_event,
6776 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006777
6778 kfree(buf);
6779}
6780
Alexander Shishkin375637b2016-04-27 18:44:46 +03006781/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03006782 * Check whether inode and address range match filter criteria.
6783 */
6784static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6785 struct file *file, unsigned long offset,
6786 unsigned long size)
6787{
6788 if (filter->inode != file->f_inode)
6789 return false;
6790
6791 if (filter->offset > offset + size)
6792 return false;
6793
6794 if (filter->offset + filter->size < offset)
6795 return false;
6796
6797 return true;
6798}
6799
6800static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6801{
6802 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6803 struct vm_area_struct *vma = data;
6804 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6805 struct file *file = vma->vm_file;
6806 struct perf_addr_filter *filter;
6807 unsigned int restart = 0, count = 0;
6808
6809 if (!has_addr_filter(event))
6810 return;
6811
6812 if (!file)
6813 return;
6814
6815 raw_spin_lock_irqsave(&ifh->lock, flags);
6816 list_for_each_entry(filter, &ifh->list, entry) {
6817 if (perf_addr_filter_match(filter, file, off,
6818 vma->vm_end - vma->vm_start)) {
6819 event->addr_filters_offs[count] = vma->vm_start;
6820 restart++;
6821 }
6822
6823 count++;
6824 }
6825
6826 if (restart)
6827 event->addr_filters_gen++;
6828 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6829
6830 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006831 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006832}
6833
6834/*
6835 * Adjust all task's events' filters to the new vma
6836 */
6837static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6838{
6839 struct perf_event_context *ctx;
6840 int ctxn;
6841
Mathieu Poirier12b40a22016-07-18 10:43:06 -06006842 /*
6843 * Data tracing isn't supported yet and as such there is no need
6844 * to keep track of anything that isn't related to executable code:
6845 */
6846 if (!(vma->vm_flags & VM_EXEC))
6847 return;
6848
Alexander Shishkin375637b2016-04-27 18:44:46 +03006849 rcu_read_lock();
6850 for_each_task_context_nr(ctxn) {
6851 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6852 if (!ctx)
6853 continue;
6854
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006855 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006856 }
6857 rcu_read_unlock();
6858}
6859
Eric B Munson3af9e852010-05-18 15:30:49 +01006860void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006861{
6862 struct perf_mmap_event mmap_event;
6863
6864 if (!atomic_read(&nr_mmap_events))
6865 return;
6866
6867 mmap_event = (struct perf_mmap_event){
6868 .vma = vma,
6869 /* .file_name */
6870 /* .file_size */
6871 .event_id = {
6872 .header = {
6873 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006874 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006875 /* .size */
6876 },
6877 /* .pid */
6878 /* .tid */
6879 .start = vma->vm_start,
6880 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006881 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006882 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006883 /* .maj (attr_mmap2 only) */
6884 /* .min (attr_mmap2 only) */
6885 /* .ino (attr_mmap2 only) */
6886 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006887 /* .prot (attr_mmap2 only) */
6888 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006889 };
6890
Alexander Shishkin375637b2016-04-27 18:44:46 +03006891 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006892 perf_event_mmap_event(&mmap_event);
6893}
6894
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006895void perf_event_aux_event(struct perf_event *event, unsigned long head,
6896 unsigned long size, u64 flags)
6897{
6898 struct perf_output_handle handle;
6899 struct perf_sample_data sample;
6900 struct perf_aux_event {
6901 struct perf_event_header header;
6902 u64 offset;
6903 u64 size;
6904 u64 flags;
6905 } rec = {
6906 .header = {
6907 .type = PERF_RECORD_AUX,
6908 .misc = 0,
6909 .size = sizeof(rec),
6910 },
6911 .offset = head,
6912 .size = size,
6913 .flags = flags,
6914 };
6915 int ret;
6916
6917 perf_event_header__init_id(&rec.header, &sample, event);
6918 ret = perf_output_begin(&handle, event, rec.header.size);
6919
6920 if (ret)
6921 return;
6922
6923 perf_output_put(&handle, rec);
6924 perf_event__output_id_sample(event, &handle, &sample);
6925
6926 perf_output_end(&handle);
6927}
6928
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006929/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006930 * Lost/dropped samples logging
6931 */
6932void perf_log_lost_samples(struct perf_event *event, u64 lost)
6933{
6934 struct perf_output_handle handle;
6935 struct perf_sample_data sample;
6936 int ret;
6937
6938 struct {
6939 struct perf_event_header header;
6940 u64 lost;
6941 } lost_samples_event = {
6942 .header = {
6943 .type = PERF_RECORD_LOST_SAMPLES,
6944 .misc = 0,
6945 .size = sizeof(lost_samples_event),
6946 },
6947 .lost = lost,
6948 };
6949
6950 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6951
6952 ret = perf_output_begin(&handle, event,
6953 lost_samples_event.header.size);
6954 if (ret)
6955 return;
6956
6957 perf_output_put(&handle, lost_samples_event);
6958 perf_event__output_id_sample(event, &handle, &sample);
6959 perf_output_end(&handle);
6960}
6961
6962/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006963 * context_switch tracking
6964 */
6965
6966struct perf_switch_event {
6967 struct task_struct *task;
6968 struct task_struct *next_prev;
6969
6970 struct {
6971 struct perf_event_header header;
6972 u32 next_prev_pid;
6973 u32 next_prev_tid;
6974 } event_id;
6975};
6976
6977static int perf_event_switch_match(struct perf_event *event)
6978{
6979 return event->attr.context_switch;
6980}
6981
6982static void perf_event_switch_output(struct perf_event *event, void *data)
6983{
6984 struct perf_switch_event *se = data;
6985 struct perf_output_handle handle;
6986 struct perf_sample_data sample;
6987 int ret;
6988
6989 if (!perf_event_switch_match(event))
6990 return;
6991
6992 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6993 if (event->ctx->task) {
6994 se->event_id.header.type = PERF_RECORD_SWITCH;
6995 se->event_id.header.size = sizeof(se->event_id.header);
6996 } else {
6997 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6998 se->event_id.header.size = sizeof(se->event_id);
6999 se->event_id.next_prev_pid =
7000 perf_event_pid(event, se->next_prev);
7001 se->event_id.next_prev_tid =
7002 perf_event_tid(event, se->next_prev);
7003 }
7004
7005 perf_event_header__init_id(&se->event_id.header, &sample, event);
7006
7007 ret = perf_output_begin(&handle, event, se->event_id.header.size);
7008 if (ret)
7009 return;
7010
7011 if (event->ctx->task)
7012 perf_output_put(&handle, se->event_id.header);
7013 else
7014 perf_output_put(&handle, se->event_id);
7015
7016 perf_event__output_id_sample(event, &handle, &sample);
7017
7018 perf_output_end(&handle);
7019}
7020
7021static void perf_event_switch(struct task_struct *task,
7022 struct task_struct *next_prev, bool sched_in)
7023{
7024 struct perf_switch_event switch_event;
7025
7026 /* N.B. caller checks nr_switch_events != 0 */
7027
7028 switch_event = (struct perf_switch_event){
7029 .task = task,
7030 .next_prev = next_prev,
7031 .event_id = {
7032 .header = {
7033 /* .type */
7034 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
7035 /* .size */
7036 },
7037 /* .next_prev_pid */
7038 /* .next_prev_tid */
7039 },
7040 };
7041
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007042 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03007043 &switch_event,
7044 NULL);
7045}
7046
7047/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007048 * IRQ throttle logging
7049 */
7050
7051static void perf_log_throttle(struct perf_event *event, int enable)
7052{
7053 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007054 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007055 int ret;
7056
7057 struct {
7058 struct perf_event_header header;
7059 u64 time;
7060 u64 id;
7061 u64 stream_id;
7062 } throttle_event = {
7063 .header = {
7064 .type = PERF_RECORD_THROTTLE,
7065 .misc = 0,
7066 .size = sizeof(throttle_event),
7067 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01007068 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007069 .id = primary_event_id(event),
7070 .stream_id = event->id,
7071 };
7072
7073 if (enable)
7074 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
7075
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007076 perf_event_header__init_id(&throttle_event.header, &sample, event);
7077
7078 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007079 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007080 if (ret)
7081 return;
7082
7083 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007084 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007085 perf_output_end(&handle);
7086}
7087
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007088static void perf_log_itrace_start(struct perf_event *event)
7089{
7090 struct perf_output_handle handle;
7091 struct perf_sample_data sample;
7092 struct perf_aux_event {
7093 struct perf_event_header header;
7094 u32 pid;
7095 u32 tid;
7096 } rec;
7097 int ret;
7098
7099 if (event->parent)
7100 event = event->parent;
7101
7102 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
7103 event->hw.itrace_started)
7104 return;
7105
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007106 rec.header.type = PERF_RECORD_ITRACE_START;
7107 rec.header.misc = 0;
7108 rec.header.size = sizeof(rec);
7109 rec.pid = perf_event_pid(event, current);
7110 rec.tid = perf_event_tid(event, current);
7111
7112 perf_event_header__init_id(&rec.header, &sample, event);
7113 ret = perf_output_begin(&handle, event, rec.header.size);
7114
7115 if (ret)
7116 return;
7117
7118 perf_output_put(&handle, rec);
7119 perf_event__output_id_sample(event, &handle, &sample);
7120
7121 perf_output_end(&handle);
7122}
7123
Jiri Olsaa88ff232016-12-28 14:31:03 +01007124static int
7125__perf_event_account_interrupt(struct perf_event *event, int throttle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007126{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007127 struct hw_perf_event *hwc = &event->hw;
7128 int ret = 0;
Jiri Olsaa88ff232016-12-28 14:31:03 +01007129 u64 seq;
Peter Zijlstra96398822010-11-24 18:55:29 +01007130
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007131 seq = __this_cpu_read(perf_throttled_seq);
7132 if (seq != hwc->interrupts_seq) {
7133 hwc->interrupts_seq = seq;
7134 hwc->interrupts = 1;
7135 } else {
7136 hwc->interrupts++;
7137 if (unlikely(throttle
7138 && hwc->interrupts >= max_samples_per_tick)) {
7139 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007140 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01007141 hwc->interrupts = MAX_INTERRUPTS;
7142 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007143 ret = 1;
7144 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007145 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007146
7147 if (event->attr.freq) {
7148 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01007149 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007150
Peter Zijlstraabd50712010-01-26 18:50:16 +01007151 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007152
Peter Zijlstraabd50712010-01-26 18:50:16 +01007153 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01007154 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007155 }
7156
Jiri Olsaa88ff232016-12-28 14:31:03 +01007157 return ret;
7158}
7159
7160int perf_event_account_interrupt(struct perf_event *event)
7161{
7162 return __perf_event_account_interrupt(event, 1);
7163}
7164
7165/*
7166 * Generic event overflow handling, sampling.
7167 */
7168
7169static int __perf_event_overflow(struct perf_event *event,
7170 int throttle, struct perf_sample_data *data,
7171 struct pt_regs *regs)
7172{
7173 int events = atomic_read(&event->event_limit);
7174 int ret = 0;
7175
7176 /*
7177 * Non-sampling counters might still use the PMI to fold short
7178 * hardware counters, ignore those.
7179 */
7180 if (unlikely(!is_sampling_event(event)))
7181 return 0;
7182
7183 ret = __perf_event_account_interrupt(event, throttle);
7184
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007185 /*
7186 * XXX event_limit might not quite work as expected on inherited
7187 * events
7188 */
7189
7190 event->pending_kill = POLL_IN;
7191 if (events && atomic_dec_and_test(&event->event_limit)) {
7192 ret = 1;
7193 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02007194
7195 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007196 }
7197
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007198 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01007199
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02007200 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007201 event->pending_wakeup = 1;
7202 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02007203 }
7204
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007205 return ret;
7206}
7207
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007208int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007209 struct perf_sample_data *data,
7210 struct pt_regs *regs)
7211{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007212 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007213}
7214
7215/*
7216 * Generic software event infrastructure
7217 */
7218
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007219struct swevent_htable {
7220 struct swevent_hlist *swevent_hlist;
7221 struct mutex hlist_mutex;
7222 int hlist_refcount;
7223
7224 /* Recursion avoidance in each contexts */
7225 int recursion[PERF_NR_CONTEXTS];
7226};
7227
7228static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7229
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007230/*
7231 * We directly increment event->count and keep a second value in
7232 * event->hw.period_left to count intervals. This period event
7233 * is kept in the range [-sample_period, 0] so that we can use the
7234 * sign as trigger.
7235 */
7236
Jiri Olsaab573842013-05-01 17:25:44 +02007237u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007238{
7239 struct hw_perf_event *hwc = &event->hw;
7240 u64 period = hwc->last_period;
7241 u64 nr, offset;
7242 s64 old, val;
7243
7244 hwc->last_period = hwc->sample_period;
7245
7246again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02007247 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007248 if (val < 0)
7249 return 0;
7250
7251 nr = div64_u64(period + val, period);
7252 offset = nr * period;
7253 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02007254 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007255 goto again;
7256
7257 return nr;
7258}
7259
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007260static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007261 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007262 struct pt_regs *regs)
7263{
7264 struct hw_perf_event *hwc = &event->hw;
7265 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007266
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007267 if (!overflow)
7268 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007269
7270 if (hwc->interrupts == MAX_INTERRUPTS)
7271 return;
7272
7273 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007274 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007275 data, regs)) {
7276 /*
7277 * We inhibit the overflow from happening when
7278 * hwc->interrupts == MAX_INTERRUPTS.
7279 */
7280 break;
7281 }
7282 throttle = 1;
7283 }
7284}
7285
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007286static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007287 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007288 struct pt_regs *regs)
7289{
7290 struct hw_perf_event *hwc = &event->hw;
7291
Peter Zijlstrae7850592010-05-21 14:43:08 +02007292 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007293
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007294 if (!regs)
7295 return;
7296
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007297 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007298 return;
7299
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03007300 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
7301 data->period = nr;
7302 return perf_swevent_overflow(event, 1, data, regs);
7303 } else
7304 data->period = event->hw.last_period;
7305
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007306 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007307 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007308
Peter Zijlstrae7850592010-05-21 14:43:08 +02007309 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007310 return;
7311
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007312 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007313}
7314
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007315static int perf_exclude_event(struct perf_event *event,
7316 struct pt_regs *regs)
7317{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007318 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01007319 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007320
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007321 if (regs) {
7322 if (event->attr.exclude_user && user_mode(regs))
7323 return 1;
7324
7325 if (event->attr.exclude_kernel && !user_mode(regs))
7326 return 1;
7327 }
7328
7329 return 0;
7330}
7331
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007332static int perf_swevent_match(struct perf_event *event,
7333 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007334 u32 event_id,
7335 struct perf_sample_data *data,
7336 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007337{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007338 if (event->attr.type != type)
7339 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007340
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007341 if (event->attr.config != event_id)
7342 return 0;
7343
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007344 if (perf_exclude_event(event, regs))
7345 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007346
7347 return 1;
7348}
7349
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007350static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007351{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007352 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007353
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007354 return hash_64(val, SWEVENT_HLIST_BITS);
7355}
7356
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007357static inline struct hlist_head *
7358__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007359{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007360 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007361
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007362 return &hlist->heads[hash];
7363}
7364
7365/* For the read side: events when they trigger */
7366static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007367find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007368{
7369 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007370
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007371 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007372 if (!hlist)
7373 return NULL;
7374
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007375 return __find_swevent_head(hlist, type, event_id);
7376}
7377
7378/* For the event head insertion and removal in the hlist */
7379static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007380find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007381{
7382 struct swevent_hlist *hlist;
7383 u32 event_id = event->attr.config;
7384 u64 type = event->attr.type;
7385
7386 /*
7387 * Event scheduling is always serialized against hlist allocation
7388 * and release. Which makes the protected version suitable here.
7389 * The context lock guarantees that.
7390 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007391 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007392 lockdep_is_held(&event->ctx->lock));
7393 if (!hlist)
7394 return NULL;
7395
7396 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007397}
7398
7399static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007400 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007401 struct perf_sample_data *data,
7402 struct pt_regs *regs)
7403{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007404 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007405 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007406 struct hlist_head *head;
7407
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007408 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007409 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007410 if (!head)
7411 goto end;
7412
Sasha Levinb67bfe02013-02-27 17:06:00 -08007413 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007414 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007415 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007416 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007417end:
7418 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007419}
7420
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007421DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7422
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007423int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007424{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007425 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007426
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007427 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007428}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007429EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007430
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007431void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007432{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007433 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007434
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007435 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007436}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007437
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007438void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007439{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007440 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007441
7442 if (WARN_ON_ONCE(!regs))
7443 return;
7444
7445 perf_sample_data_init(&data, addr, 0);
7446 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7447}
7448
7449void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7450{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007451 int rctx;
7452
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007453 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007454 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007455 if (unlikely(rctx < 0))
7456 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007457
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007458 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007459
7460 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007461fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007462 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007463}
7464
7465static void perf_swevent_read(struct perf_event *event)
7466{
7467}
7468
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007469static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007470{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007471 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007472 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007473 struct hlist_head *head;
7474
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007475 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007476 hwc->last_period = hwc->sample_period;
7477 perf_swevent_set_period(event);
7478 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007479
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007480 hwc->state = !(flags & PERF_EF_START);
7481
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007482 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007483 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007484 return -EINVAL;
7485
7486 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007487 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007488
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007489 return 0;
7490}
7491
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007492static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007493{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007494 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007495}
7496
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007497static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007498{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007499 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007500}
7501
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007502static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007503{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007504 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007505}
7506
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007507/* Deref the hlist from the update side */
7508static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007509swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007510{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007511 return rcu_dereference_protected(swhash->swevent_hlist,
7512 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007513}
7514
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007515static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007516{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007517 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007518
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007519 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007520 return;
7521
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007522 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007523 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007524}
7525
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007526static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007527{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007528 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007529
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007530 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007531
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007532 if (!--swhash->hlist_refcount)
7533 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007534
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007535 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007536}
7537
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007538static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007539{
7540 int cpu;
7541
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007542 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007543 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007544}
7545
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007546static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007547{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007548 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007549 int err = 0;
7550
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007551 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007552 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007553 struct swevent_hlist *hlist;
7554
7555 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7556 if (!hlist) {
7557 err = -ENOMEM;
7558 goto exit;
7559 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007560 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007561 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007562 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007563exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007564 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007565
7566 return err;
7567}
7568
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007569static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007570{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007571 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007572
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007573 get_online_cpus();
7574 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007575 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007576 if (err) {
7577 failed_cpu = cpu;
7578 goto fail;
7579 }
7580 }
7581 put_online_cpus();
7582
7583 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007584fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007585 for_each_possible_cpu(cpu) {
7586 if (cpu == failed_cpu)
7587 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007588 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007589 }
7590
7591 put_online_cpus();
7592 return err;
7593}
7594
Ingo Molnarc5905af2012-02-24 08:31:31 +01007595struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007596
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007597static void sw_perf_event_destroy(struct perf_event *event)
7598{
7599 u64 event_id = event->attr.config;
7600
7601 WARN_ON(event->parent);
7602
Ingo Molnarc5905af2012-02-24 08:31:31 +01007603 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007604 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007605}
7606
7607static int perf_swevent_init(struct perf_event *event)
7608{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007609 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007610
7611 if (event->attr.type != PERF_TYPE_SOFTWARE)
7612 return -ENOENT;
7613
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007614 /*
7615 * no branch sampling for software events
7616 */
7617 if (has_branch_stack(event))
7618 return -EOPNOTSUPP;
7619
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007620 switch (event_id) {
7621 case PERF_COUNT_SW_CPU_CLOCK:
7622 case PERF_COUNT_SW_TASK_CLOCK:
7623 return -ENOENT;
7624
7625 default:
7626 break;
7627 }
7628
Dan Carpenterce677832010-10-24 21:50:42 +02007629 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007630 return -ENOENT;
7631
7632 if (!event->parent) {
7633 int err;
7634
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007635 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007636 if (err)
7637 return err;
7638
Ingo Molnarc5905af2012-02-24 08:31:31 +01007639 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007640 event->destroy = sw_perf_event_destroy;
7641 }
7642
7643 return 0;
7644}
7645
7646static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007647 .task_ctx_nr = perf_sw_context,
7648
Peter Zijlstra34f43922015-02-20 14:05:38 +01007649 .capabilities = PERF_PMU_CAP_NO_NMI,
7650
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007651 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007652 .add = perf_swevent_add,
7653 .del = perf_swevent_del,
7654 .start = perf_swevent_start,
7655 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007656 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007657};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007658
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007659#ifdef CONFIG_EVENT_TRACING
7660
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007661static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007662 struct perf_sample_data *data)
7663{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007664 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007665
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007666 /* only top level events have filters set */
7667 if (event->parent)
7668 event = event->parent;
7669
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007670 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7671 return 1;
7672 return 0;
7673}
7674
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007675static int perf_tp_event_match(struct perf_event *event,
7676 struct perf_sample_data *data,
7677 struct pt_regs *regs)
7678{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007679 if (event->hw.state & PERF_HES_STOPPED)
7680 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007681 /*
7682 * All tracepoints are from kernel-space.
7683 */
7684 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007685 return 0;
7686
7687 if (!perf_tp_filter_match(event, data))
7688 return 0;
7689
7690 return 1;
7691}
7692
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07007693void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7694 struct trace_event_call *call, u64 count,
7695 struct pt_regs *regs, struct hlist_head *head,
7696 struct task_struct *task)
7697{
7698 struct bpf_prog *prog = call->prog;
7699
7700 if (prog) {
7701 *(struct pt_regs **)raw_data = regs;
7702 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7703 perf_swevent_put_recursion_context(rctx);
7704 return;
7705 }
7706 }
7707 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7708 rctx, task);
7709}
7710EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7711
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007712void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007713 struct pt_regs *regs, struct hlist_head *head, int rctx,
7714 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007715{
7716 struct perf_sample_data data;
7717 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007718
7719 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007720 .frag = {
7721 .size = entry_size,
7722 .data = record,
7723 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007724 };
7725
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007726 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007727 data.raw = &raw;
7728
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007729 perf_trace_buf_update(record, event_type);
7730
Sasha Levinb67bfe02013-02-27 17:06:00 -08007731 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007732 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007733 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007734 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007735
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007736 /*
7737 * If we got specified a target task, also iterate its context and
7738 * deliver this event there too.
7739 */
7740 if (task && task != current) {
7741 struct perf_event_context *ctx;
7742 struct trace_entry *entry = record;
7743
7744 rcu_read_lock();
7745 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7746 if (!ctx)
7747 goto unlock;
7748
7749 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Jiri Olsa4d083662018-09-23 18:13:43 +02007750 if (event->cpu != smp_processor_id())
7751 continue;
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007752 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7753 continue;
7754 if (event->attr.config != entry->type)
7755 continue;
7756 if (perf_tp_event_match(event, &data, regs))
7757 perf_swevent_event(event, count, &data, regs);
7758 }
7759unlock:
7760 rcu_read_unlock();
7761 }
7762
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007763 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007764}
7765EXPORT_SYMBOL_GPL(perf_tp_event);
7766
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007767static void tp_perf_event_destroy(struct perf_event *event)
7768{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007769 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007770}
7771
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007772static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007773{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007774 int err;
7775
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007776 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7777 return -ENOENT;
7778
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007779 /*
7780 * no branch sampling for tracepoint events
7781 */
7782 if (has_branch_stack(event))
7783 return -EOPNOTSUPP;
7784
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007785 err = perf_trace_init(event);
7786 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007787 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007788
7789 event->destroy = tp_perf_event_destroy;
7790
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007791 return 0;
7792}
7793
7794static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007795 .task_ctx_nr = perf_sw_context,
7796
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007797 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007798 .add = perf_trace_add,
7799 .del = perf_trace_del,
7800 .start = perf_swevent_start,
7801 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007802 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007803};
7804
7805static inline void perf_tp_register(void)
7806{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007807 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007808}
Li Zefan6fb29152009-10-15 11:21:42 +08007809
Li Zefan6fb29152009-10-15 11:21:42 +08007810static void perf_event_free_filter(struct perf_event *event)
7811{
7812 ftrace_profile_free_filter(event);
7813}
7814
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007815#ifdef CONFIG_BPF_SYSCALL
7816static void bpf_overflow_handler(struct perf_event *event,
7817 struct perf_sample_data *data,
7818 struct pt_regs *regs)
7819{
7820 struct bpf_perf_event_data_kern ctx = {
7821 .data = data,
7822 .regs = regs,
7823 };
7824 int ret = 0;
7825
7826 preempt_disable();
7827 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
7828 goto out;
7829 rcu_read_lock();
7830 ret = BPF_PROG_RUN(event->prog, (void *)&ctx);
7831 rcu_read_unlock();
7832out:
7833 __this_cpu_dec(bpf_prog_active);
7834 preempt_enable();
7835 if (!ret)
7836 return;
7837
7838 event->orig_overflow_handler(event, data, regs);
7839}
7840
7841static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7842{
7843 struct bpf_prog *prog;
7844
7845 if (event->overflow_handler_context)
7846 /* hw breakpoint or kernel counter */
7847 return -EINVAL;
7848
7849 if (event->prog)
7850 return -EEXIST;
7851
7852 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
7853 if (IS_ERR(prog))
7854 return PTR_ERR(prog);
7855
7856 event->prog = prog;
7857 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
7858 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
7859 return 0;
7860}
7861
7862static void perf_event_free_bpf_handler(struct perf_event *event)
7863{
7864 struct bpf_prog *prog = event->prog;
7865
7866 if (!prog)
7867 return;
7868
7869 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
7870 event->prog = NULL;
7871 bpf_prog_put(prog);
7872}
7873#else
7874static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7875{
7876 return -EOPNOTSUPP;
7877}
7878static void perf_event_free_bpf_handler(struct perf_event *event)
7879{
7880}
7881#endif
7882
Alexei Starovoitov25415172015-03-25 12:49:20 -07007883static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7884{
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007885 bool is_kprobe, is_tracepoint;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007886 struct bpf_prog *prog;
7887
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007888 if (event->attr.type == PERF_TYPE_HARDWARE ||
7889 event->attr.type == PERF_TYPE_SOFTWARE)
7890 return perf_event_set_bpf_handler(event, prog_fd);
7891
Alexei Starovoitov25415172015-03-25 12:49:20 -07007892 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7893 return -EINVAL;
7894
7895 if (event->tp_event->prog)
7896 return -EEXIST;
7897
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007898 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7899 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7900 if (!is_kprobe && !is_tracepoint)
7901 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007902 return -EINVAL;
7903
7904 prog = bpf_prog_get(prog_fd);
7905 if (IS_ERR(prog))
7906 return PTR_ERR(prog);
7907
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007908 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7909 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007910 /* valid fd, but invalid bpf program type */
7911 bpf_prog_put(prog);
7912 return -EINVAL;
7913 }
7914
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07007915 if (is_tracepoint) {
7916 int off = trace_event_get_offsets(event->tp_event);
7917
7918 if (prog->aux->max_ctx_offset > off) {
7919 bpf_prog_put(prog);
7920 return -EACCES;
7921 }
7922 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07007923 event->tp_event->prog = prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007924 event->tp_event->bpf_prog_owner = event;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007925
7926 return 0;
7927}
7928
7929static void perf_event_free_bpf_prog(struct perf_event *event)
7930{
7931 struct bpf_prog *prog;
7932
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007933 perf_event_free_bpf_handler(event);
7934
Alexei Starovoitov25415172015-03-25 12:49:20 -07007935 if (!event->tp_event)
7936 return;
7937
7938 prog = event->tp_event->prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007939 if (prog && event->tp_event->bpf_prog_owner == event) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007940 event->tp_event->prog = NULL;
Daniel Borkmann1aacde32016-06-30 17:24:43 +02007941 bpf_prog_put(prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07007942 }
7943}
7944
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007945#else
Li Zefan6fb29152009-10-15 11:21:42 +08007946
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007947static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007948{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007949}
Li Zefan6fb29152009-10-15 11:21:42 +08007950
Li Zefan6fb29152009-10-15 11:21:42 +08007951static void perf_event_free_filter(struct perf_event *event)
7952{
7953}
7954
Alexei Starovoitov25415172015-03-25 12:49:20 -07007955static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7956{
7957 return -ENOENT;
7958}
7959
7960static void perf_event_free_bpf_prog(struct perf_event *event)
7961{
7962}
Li Zefan07b139c2009-12-21 14:27:35 +08007963#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007964
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007965#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007966void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007967{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007968 struct perf_sample_data sample;
7969 struct pt_regs *regs = data;
7970
Robert Richterfd0d0002012-04-02 20:19:08 +02007971 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007972
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007973 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007974 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007975}
7976#endif
7977
Alexander Shishkin375637b2016-04-27 18:44:46 +03007978/*
7979 * Allocate a new address filter
7980 */
7981static struct perf_addr_filter *
7982perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7983{
7984 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7985 struct perf_addr_filter *filter;
7986
7987 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7988 if (!filter)
7989 return NULL;
7990
7991 INIT_LIST_HEAD(&filter->entry);
7992 list_add_tail(&filter->entry, filters);
7993
7994 return filter;
7995}
7996
7997static void free_filters_list(struct list_head *filters)
7998{
7999 struct perf_addr_filter *filter, *iter;
8000
8001 list_for_each_entry_safe(filter, iter, filters, entry) {
8002 if (filter->inode)
8003 iput(filter->inode);
8004 list_del(&filter->entry);
8005 kfree(filter);
8006 }
8007}
8008
8009/*
8010 * Free existing address filters and optionally install new ones
8011 */
8012static void perf_addr_filters_splice(struct perf_event *event,
8013 struct list_head *head)
8014{
8015 unsigned long flags;
8016 LIST_HEAD(list);
8017
8018 if (!has_addr_filter(event))
8019 return;
8020
8021 /* don't bother with children, they don't have their own filters */
8022 if (event->parent)
8023 return;
8024
8025 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
8026
8027 list_splice_init(&event->addr_filters.list, &list);
8028 if (head)
8029 list_splice(head, &event->addr_filters.list);
8030
8031 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
8032
8033 free_filters_list(&list);
8034}
8035
8036/*
8037 * Scan through mm's vmas and see if one of them matches the
8038 * @filter; if so, adjust filter's address range.
8039 * Called with mm::mmap_sem down for reading.
8040 */
8041static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
8042 struct mm_struct *mm)
8043{
8044 struct vm_area_struct *vma;
8045
8046 for (vma = mm->mmap; vma; vma = vma->vm_next) {
8047 struct file *file = vma->vm_file;
8048 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
8049 unsigned long vma_size = vma->vm_end - vma->vm_start;
8050
8051 if (!file)
8052 continue;
8053
8054 if (!perf_addr_filter_match(filter, file, off, vma_size))
8055 continue;
8056
8057 return vma->vm_start;
8058 }
8059
8060 return 0;
8061}
8062
8063/*
8064 * Update event's address range filters based on the
8065 * task's existing mappings, if any.
8066 */
8067static void perf_event_addr_filters_apply(struct perf_event *event)
8068{
8069 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
8070 struct task_struct *task = READ_ONCE(event->ctx->task);
8071 struct perf_addr_filter *filter;
8072 struct mm_struct *mm = NULL;
8073 unsigned int count = 0;
8074 unsigned long flags;
8075
8076 /*
8077 * We may observe TASK_TOMBSTONE, which means that the event tear-down
8078 * will stop on the parent's child_mutex that our caller is also holding
8079 */
8080 if (task == TASK_TOMBSTONE)
8081 return;
8082
8083 mm = get_task_mm(event->ctx->task);
8084 if (!mm)
8085 goto restart;
8086
8087 down_read(&mm->mmap_sem);
8088
8089 raw_spin_lock_irqsave(&ifh->lock, flags);
8090 list_for_each_entry(filter, &ifh->list, entry) {
8091 event->addr_filters_offs[count] = 0;
8092
Mathieu Poirier99f5bc92016-07-18 10:43:07 -06008093 /*
8094 * Adjust base offset if the filter is associated to a binary
8095 * that needs to be mapped:
8096 */
8097 if (filter->inode)
Alexander Shishkin375637b2016-04-27 18:44:46 +03008098 event->addr_filters_offs[count] =
8099 perf_addr_filter_apply(filter, mm);
8100
8101 count++;
8102 }
8103
8104 event->addr_filters_gen++;
8105 raw_spin_unlock_irqrestore(&ifh->lock, flags);
8106
8107 up_read(&mm->mmap_sem);
8108
8109 mmput(mm);
8110
8111restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03008112 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008113}
8114
8115/*
8116 * Address range filtering: limiting the data to certain
8117 * instruction address ranges. Filters are ioctl()ed to us from
8118 * userspace as ascii strings.
8119 *
8120 * Filter string format:
8121 *
8122 * ACTION RANGE_SPEC
8123 * where ACTION is one of the
8124 * * "filter": limit the trace to this region
8125 * * "start": start tracing from this address
8126 * * "stop": stop tracing at this address/region;
8127 * RANGE_SPEC is
8128 * * for kernel addresses: <start address>[/<size>]
8129 * * for object files: <start address>[/<size>]@</path/to/object/file>
8130 *
8131 * if <size> is not specified, the range is treated as a single address.
8132 */
8133enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02008134 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03008135 IF_ACT_FILTER,
8136 IF_ACT_START,
8137 IF_ACT_STOP,
8138 IF_SRC_FILE,
8139 IF_SRC_KERNEL,
8140 IF_SRC_FILEADDR,
8141 IF_SRC_KERNELADDR,
8142};
8143
8144enum {
8145 IF_STATE_ACTION = 0,
8146 IF_STATE_SOURCE,
8147 IF_STATE_END,
8148};
8149
8150static const match_table_t if_tokens = {
8151 { IF_ACT_FILTER, "filter" },
8152 { IF_ACT_START, "start" },
8153 { IF_ACT_STOP, "stop" },
8154 { IF_SRC_FILE, "%u/%u@%s" },
8155 { IF_SRC_KERNEL, "%u/%u" },
8156 { IF_SRC_FILEADDR, "%u@%s" },
8157 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +02008158 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +03008159};
8160
8161/*
8162 * Address filter string parser
8163 */
8164static int
8165perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
8166 struct list_head *filters)
8167{
8168 struct perf_addr_filter *filter = NULL;
8169 char *start, *orig, *filename = NULL;
8170 struct path path;
8171 substring_t args[MAX_OPT_ARGS];
8172 int state = IF_STATE_ACTION, token;
8173 unsigned int kernel = 0;
8174 int ret = -EINVAL;
8175
8176 orig = fstr = kstrdup(fstr, GFP_KERNEL);
8177 if (!fstr)
8178 return -ENOMEM;
8179
8180 while ((start = strsep(&fstr, " ,\n")) != NULL) {
8181 ret = -EINVAL;
8182
8183 if (!*start)
8184 continue;
8185
8186 /* filter definition begins */
8187 if (state == IF_STATE_ACTION) {
8188 filter = perf_addr_filter_new(event, filters);
8189 if (!filter)
8190 goto fail;
8191 }
8192
8193 token = match_token(start, if_tokens, args);
8194 switch (token) {
8195 case IF_ACT_FILTER:
8196 case IF_ACT_START:
8197 filter->filter = 1;
8198
8199 case IF_ACT_STOP:
8200 if (state != IF_STATE_ACTION)
8201 goto fail;
8202
8203 state = IF_STATE_SOURCE;
8204 break;
8205
8206 case IF_SRC_KERNELADDR:
8207 case IF_SRC_KERNEL:
8208 kernel = 1;
8209
8210 case IF_SRC_FILEADDR:
8211 case IF_SRC_FILE:
8212 if (state != IF_STATE_SOURCE)
8213 goto fail;
8214
8215 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
8216 filter->range = 1;
8217
8218 *args[0].to = 0;
8219 ret = kstrtoul(args[0].from, 0, &filter->offset);
8220 if (ret)
8221 goto fail;
8222
8223 if (filter->range) {
8224 *args[1].to = 0;
8225 ret = kstrtoul(args[1].from, 0, &filter->size);
8226 if (ret)
8227 goto fail;
8228 }
8229
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06008230 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
8231 int fpos = filter->range ? 2 : 1;
8232
8233 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008234 if (!filename) {
8235 ret = -ENOMEM;
8236 goto fail;
8237 }
8238 }
8239
8240 state = IF_STATE_END;
8241 break;
8242
8243 default:
8244 goto fail;
8245 }
8246
8247 /*
8248 * Filter definition is fully parsed, validate and install it.
8249 * Make sure that it doesn't contradict itself or the event's
8250 * attribute.
8251 */
8252 if (state == IF_STATE_END) {
8253 if (kernel && event->attr.exclude_kernel)
8254 goto fail;
8255
8256 if (!kernel) {
8257 if (!filename)
8258 goto fail;
8259
8260 /* look up the path and grab its inode */
8261 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
8262 if (ret)
8263 goto fail_free_name;
8264
8265 filter->inode = igrab(d_inode(path.dentry));
8266 path_put(&path);
8267 kfree(filename);
8268 filename = NULL;
8269
8270 ret = -EINVAL;
8271 if (!filter->inode ||
8272 !S_ISREG(filter->inode->i_mode))
8273 /* free_filters_list() will iput() */
8274 goto fail;
8275 }
8276
8277 /* ready to consume more filters */
8278 state = IF_STATE_ACTION;
8279 filter = NULL;
8280 }
8281 }
8282
8283 if (state != IF_STATE_ACTION)
8284 goto fail;
8285
8286 kfree(orig);
8287
8288 return 0;
8289
8290fail_free_name:
8291 kfree(filename);
8292fail:
8293 free_filters_list(filters);
8294 kfree(orig);
8295
8296 return ret;
8297}
8298
8299static int
8300perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
8301{
8302 LIST_HEAD(filters);
8303 int ret;
8304
8305 /*
8306 * Since this is called in perf_ioctl() path, we're already holding
8307 * ctx::mutex.
8308 */
8309 lockdep_assert_held(&event->ctx->mutex);
8310
8311 if (WARN_ON_ONCE(event->parent))
8312 return -EINVAL;
8313
8314 /*
8315 * For now, we only support filtering in per-task events; doing so
8316 * for CPU-wide events requires additional context switching trickery,
8317 * since same object code will be mapped at different virtual
8318 * addresses in different processes.
8319 */
8320 if (!event->ctx->task)
8321 return -EOPNOTSUPP;
8322
8323 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
8324 if (ret)
8325 return ret;
8326
8327 ret = event->pmu->addr_filters_validate(&filters);
8328 if (ret) {
8329 free_filters_list(&filters);
8330 return ret;
8331 }
8332
8333 /* remove existing filters, if any */
8334 perf_addr_filters_splice(event, &filters);
8335
8336 /* install new filters */
8337 perf_event_for_each_child(event, perf_event_addr_filters_apply);
8338
8339 return ret;
8340}
8341
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008342static int perf_event_set_filter(struct perf_event *event, void __user *arg)
8343{
8344 char *filter_str;
8345 int ret = -EINVAL;
8346
Alexander Shishkin375637b2016-04-27 18:44:46 +03008347 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
8348 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
8349 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008350 return -EINVAL;
8351
8352 filter_str = strndup_user(arg, PAGE_SIZE);
8353 if (IS_ERR(filter_str))
8354 return PTR_ERR(filter_str);
8355
8356 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
8357 event->attr.type == PERF_TYPE_TRACEPOINT)
8358 ret = ftrace_profile_set_filter(event, event->attr.config,
8359 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008360 else if (has_addr_filter(event))
8361 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008362
8363 kfree(filter_str);
8364 return ret;
8365}
8366
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008367/*
8368 * hrtimer based swevent callback
8369 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008370
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008371static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008372{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008373 enum hrtimer_restart ret = HRTIMER_RESTART;
8374 struct perf_sample_data data;
8375 struct pt_regs *regs;
8376 struct perf_event *event;
8377 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008378
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008379 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008380
8381 if (event->state != PERF_EVENT_STATE_ACTIVE)
8382 return HRTIMER_NORESTART;
8383
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008384 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008385
Robert Richterfd0d0002012-04-02 20:19:08 +02008386 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008387 regs = get_irq_regs();
8388
8389 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08008390 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02008391 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008392 ret = HRTIMER_NORESTART;
8393 }
8394
8395 period = max_t(u64, 10000, event->hw.sample_period);
8396 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
8397
8398 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008399}
8400
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008401static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008402{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008403 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008404 s64 period;
8405
8406 if (!is_sampling_event(event))
8407 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008408
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008409 period = local64_read(&hwc->period_left);
8410 if (period) {
8411 if (period < 0)
8412 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008413
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008414 local64_set(&hwc->period_left, 0);
8415 } else {
8416 period = max_t(u64, 10000, hwc->sample_period);
8417 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00008418 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8419 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008420}
8421
8422static void perf_swevent_cancel_hrtimer(struct perf_event *event)
8423{
8424 struct hw_perf_event *hwc = &event->hw;
8425
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008426 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008427 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008428 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008429
8430 hrtimer_cancel(&hwc->hrtimer);
8431 }
8432}
8433
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008434static void perf_swevent_init_hrtimer(struct perf_event *event)
8435{
8436 struct hw_perf_event *hwc = &event->hw;
8437
8438 if (!is_sampling_event(event))
8439 return;
8440
8441 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8442 hwc->hrtimer.function = perf_swevent_hrtimer;
8443
8444 /*
8445 * Since hrtimers have a fixed rate, we can do a static freq->period
8446 * mapping and avoid the whole period adjust feedback stuff.
8447 */
8448 if (event->attr.freq) {
8449 long freq = event->attr.sample_freq;
8450
8451 event->attr.sample_period = NSEC_PER_SEC / freq;
8452 hwc->sample_period = event->attr.sample_period;
8453 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008454 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008455 event->attr.freq = 0;
8456 }
8457}
8458
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008459/*
8460 * Software event: cpu wall time clock
8461 */
8462
8463static void cpu_clock_event_update(struct perf_event *event)
8464{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008465 s64 prev;
8466 u64 now;
8467
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008468 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008469 prev = local64_xchg(&event->hw.prev_count, now);
8470 local64_add(now - prev, &event->count);
8471}
8472
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008473static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008474{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008475 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008476 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008477}
8478
8479static void cpu_clock_event_stop(struct perf_event *event, int flags)
8480{
8481 perf_swevent_cancel_hrtimer(event);
8482 cpu_clock_event_update(event);
8483}
8484
8485static int cpu_clock_event_add(struct perf_event *event, int flags)
8486{
8487 if (flags & PERF_EF_START)
8488 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008489 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008490
8491 return 0;
8492}
8493
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008494static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008495{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008496 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008497}
8498
8499static void cpu_clock_event_read(struct perf_event *event)
8500{
8501 cpu_clock_event_update(event);
8502}
8503
8504static int cpu_clock_event_init(struct perf_event *event)
8505{
8506 if (event->attr.type != PERF_TYPE_SOFTWARE)
8507 return -ENOENT;
8508
8509 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8510 return -ENOENT;
8511
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008512 /*
8513 * no branch sampling for software events
8514 */
8515 if (has_branch_stack(event))
8516 return -EOPNOTSUPP;
8517
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008518 perf_swevent_init_hrtimer(event);
8519
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008520 return 0;
8521}
8522
8523static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008524 .task_ctx_nr = perf_sw_context,
8525
Peter Zijlstra34f43922015-02-20 14:05:38 +01008526 .capabilities = PERF_PMU_CAP_NO_NMI,
8527
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008528 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008529 .add = cpu_clock_event_add,
8530 .del = cpu_clock_event_del,
8531 .start = cpu_clock_event_start,
8532 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008533 .read = cpu_clock_event_read,
8534};
8535
8536/*
8537 * Software event: task time clock
8538 */
8539
8540static void task_clock_event_update(struct perf_event *event, u64 now)
8541{
8542 u64 prev;
8543 s64 delta;
8544
8545 prev = local64_xchg(&event->hw.prev_count, now);
8546 delta = now - prev;
8547 local64_add(delta, &event->count);
8548}
8549
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008550static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008551{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008552 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008553 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008554}
8555
8556static void task_clock_event_stop(struct perf_event *event, int flags)
8557{
8558 perf_swevent_cancel_hrtimer(event);
8559 task_clock_event_update(event, event->ctx->time);
8560}
8561
8562static int task_clock_event_add(struct perf_event *event, int flags)
8563{
8564 if (flags & PERF_EF_START)
8565 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008566 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008567
8568 return 0;
8569}
8570
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008571static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008572{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008573 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008574}
8575
8576static void task_clock_event_read(struct perf_event *event)
8577{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008578 u64 now = perf_clock();
8579 u64 delta = now - event->ctx->timestamp;
8580 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008581
8582 task_clock_event_update(event, time);
8583}
8584
8585static int task_clock_event_init(struct perf_event *event)
8586{
8587 if (event->attr.type != PERF_TYPE_SOFTWARE)
8588 return -ENOENT;
8589
8590 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8591 return -ENOENT;
8592
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008593 /*
8594 * no branch sampling for software events
8595 */
8596 if (has_branch_stack(event))
8597 return -EOPNOTSUPP;
8598
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008599 perf_swevent_init_hrtimer(event);
8600
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008601 return 0;
8602}
8603
8604static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008605 .task_ctx_nr = perf_sw_context,
8606
Peter Zijlstra34f43922015-02-20 14:05:38 +01008607 .capabilities = PERF_PMU_CAP_NO_NMI,
8608
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008609 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008610 .add = task_clock_event_add,
8611 .del = task_clock_event_del,
8612 .start = task_clock_event_start,
8613 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008614 .read = task_clock_event_read,
8615};
8616
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008617static void perf_pmu_nop_void(struct pmu *pmu)
8618{
8619}
8620
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008621static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8622{
8623}
8624
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008625static int perf_pmu_nop_int(struct pmu *pmu)
8626{
8627 return 0;
8628}
8629
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008630static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008631
8632static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008633{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008634 __this_cpu_write(nop_txn_flags, flags);
8635
8636 if (flags & ~PERF_PMU_TXN_ADD)
8637 return;
8638
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008639 perf_pmu_disable(pmu);
8640}
8641
8642static int perf_pmu_commit_txn(struct pmu *pmu)
8643{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008644 unsigned int flags = __this_cpu_read(nop_txn_flags);
8645
8646 __this_cpu_write(nop_txn_flags, 0);
8647
8648 if (flags & ~PERF_PMU_TXN_ADD)
8649 return 0;
8650
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008651 perf_pmu_enable(pmu);
8652 return 0;
8653}
8654
8655static void perf_pmu_cancel_txn(struct pmu *pmu)
8656{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008657 unsigned int flags = __this_cpu_read(nop_txn_flags);
8658
8659 __this_cpu_write(nop_txn_flags, 0);
8660
8661 if (flags & ~PERF_PMU_TXN_ADD)
8662 return;
8663
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008664 perf_pmu_enable(pmu);
8665}
8666
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008667static int perf_event_idx_default(struct perf_event *event)
8668{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008669 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008670}
8671
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008672/*
8673 * Ensures all contexts with the same task_ctx_nr have the same
8674 * pmu_cpu_context too.
8675 */
Mark Rutland9e317042014-02-10 17:44:18 +00008676static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008677{
8678 struct pmu *pmu;
8679
8680 if (ctxn < 0)
8681 return NULL;
8682
8683 list_for_each_entry(pmu, &pmus, entry) {
8684 if (pmu->task_ctx_nr == ctxn)
8685 return pmu->pmu_cpu_context;
8686 }
8687
8688 return NULL;
8689}
8690
Peter Zijlstra51676952010-12-07 14:18:20 +01008691static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008692{
Peter Zijlstra51676952010-12-07 14:18:20 +01008693 int cpu;
8694
8695 for_each_possible_cpu(cpu) {
8696 struct perf_cpu_context *cpuctx;
8697
8698 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8699
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008700 if (cpuctx->unique_pmu == old_pmu)
8701 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008702 }
8703}
8704
8705static void free_pmu_context(struct pmu *pmu)
8706{
8707 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008708
8709 mutex_lock(&pmus_lock);
8710 /*
8711 * Like a real lame refcount.
8712 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008713 list_for_each_entry(i, &pmus, entry) {
8714 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8715 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008716 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008717 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008718 }
8719
Peter Zijlstra51676952010-12-07 14:18:20 +01008720 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008721out:
8722 mutex_unlock(&pmus_lock);
8723}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008724
8725/*
8726 * Let userspace know that this PMU supports address range filtering:
8727 */
8728static ssize_t nr_addr_filters_show(struct device *dev,
8729 struct device_attribute *attr,
8730 char *page)
8731{
8732 struct pmu *pmu = dev_get_drvdata(dev);
8733
8734 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8735}
8736DEVICE_ATTR_RO(nr_addr_filters);
8737
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008738static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008739
Peter Zijlstraabe43402010-11-17 23:17:37 +01008740static ssize_t
8741type_show(struct device *dev, struct device_attribute *attr, char *page)
8742{
8743 struct pmu *pmu = dev_get_drvdata(dev);
8744
8745 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8746}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008747static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008748
Stephane Eranian62b85632013-04-03 14:21:34 +02008749static ssize_t
8750perf_event_mux_interval_ms_show(struct device *dev,
8751 struct device_attribute *attr,
8752 char *page)
8753{
8754 struct pmu *pmu = dev_get_drvdata(dev);
8755
8756 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8757}
8758
Peter Zijlstra272325c2015-04-15 11:41:58 +02008759static DEFINE_MUTEX(mux_interval_mutex);
8760
Stephane Eranian62b85632013-04-03 14:21:34 +02008761static ssize_t
8762perf_event_mux_interval_ms_store(struct device *dev,
8763 struct device_attribute *attr,
8764 const char *buf, size_t count)
8765{
8766 struct pmu *pmu = dev_get_drvdata(dev);
8767 int timer, cpu, ret;
8768
8769 ret = kstrtoint(buf, 0, &timer);
8770 if (ret)
8771 return ret;
8772
8773 if (timer < 1)
8774 return -EINVAL;
8775
8776 /* same value, noting to do */
8777 if (timer == pmu->hrtimer_interval_ms)
8778 return count;
8779
Peter Zijlstra272325c2015-04-15 11:41:58 +02008780 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008781 pmu->hrtimer_interval_ms = timer;
8782
8783 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008784 get_online_cpus();
8785 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008786 struct perf_cpu_context *cpuctx;
8787 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8788 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8789
Peter Zijlstra272325c2015-04-15 11:41:58 +02008790 cpu_function_call(cpu,
8791 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008792 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008793 put_online_cpus();
8794 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008795
8796 return count;
8797}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008798static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008799
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008800static struct attribute *pmu_dev_attrs[] = {
8801 &dev_attr_type.attr,
8802 &dev_attr_perf_event_mux_interval_ms.attr,
8803 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008804};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008805ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008806
8807static int pmu_bus_running;
8808static struct bus_type pmu_bus = {
8809 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008810 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008811};
8812
8813static void pmu_dev_release(struct device *dev)
8814{
8815 kfree(dev);
8816}
8817
8818static int pmu_dev_alloc(struct pmu *pmu)
8819{
8820 int ret = -ENOMEM;
8821
8822 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8823 if (!pmu->dev)
8824 goto out;
8825
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008826 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008827 device_initialize(pmu->dev);
8828 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8829 if (ret)
8830 goto free_dev;
8831
8832 dev_set_drvdata(pmu->dev, pmu);
8833 pmu->dev->bus = &pmu_bus;
8834 pmu->dev->release = pmu_dev_release;
8835 ret = device_add(pmu->dev);
8836 if (ret)
8837 goto free_dev;
8838
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008839 /* For PMUs with address filters, throw in an extra attribute: */
8840 if (pmu->nr_addr_filters)
8841 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8842
8843 if (ret)
8844 goto del_dev;
8845
Peter Zijlstraabe43402010-11-17 23:17:37 +01008846out:
8847 return ret;
8848
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008849del_dev:
8850 device_del(pmu->dev);
8851
Peter Zijlstraabe43402010-11-17 23:17:37 +01008852free_dev:
8853 put_device(pmu->dev);
8854 goto out;
8855}
8856
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008857static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008858static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008859
Mischa Jonker03d8e802013-06-04 11:45:48 +02008860int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008861{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008862 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008863
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008864 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008865 ret = -ENOMEM;
8866 pmu->pmu_disable_count = alloc_percpu(int);
8867 if (!pmu->pmu_disable_count)
8868 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008869
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008870 pmu->type = -1;
8871 if (!name)
8872 goto skip_type;
8873 pmu->name = name;
8874
8875 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008876 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8877 if (type < 0) {
8878 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008879 goto free_pdc;
8880 }
8881 }
8882 pmu->type = type;
8883
Peter Zijlstraabe43402010-11-17 23:17:37 +01008884 if (pmu_bus_running) {
8885 ret = pmu_dev_alloc(pmu);
8886 if (ret)
8887 goto free_idr;
8888 }
8889
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008890skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008891 if (pmu->task_ctx_nr == perf_hw_context) {
8892 static int hw_context_taken = 0;
8893
Mark Rutland5101ef22016-04-26 11:33:46 +01008894 /*
8895 * Other than systems with heterogeneous CPUs, it never makes
8896 * sense for two PMUs to share perf_hw_context. PMUs which are
8897 * uncore must use perf_invalid_context.
8898 */
8899 if (WARN_ON_ONCE(hw_context_taken &&
8900 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008901 pmu->task_ctx_nr = perf_invalid_context;
8902
8903 hw_context_taken = 1;
8904 }
8905
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008906 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8907 if (pmu->pmu_cpu_context)
8908 goto got_cpu_context;
8909
Wei Yongjunc4814202013-04-12 11:05:54 +08008910 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008911 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8912 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008913 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008914
8915 for_each_possible_cpu(cpu) {
8916 struct perf_cpu_context *cpuctx;
8917
8918 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008919 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008920 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008921 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008922 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008923
Peter Zijlstra272325c2015-04-15 11:41:58 +02008924 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008925
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008926 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008927 }
8928
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008929got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008930 if (!pmu->start_txn) {
8931 if (pmu->pmu_enable) {
8932 /*
8933 * If we have pmu_enable/pmu_disable calls, install
8934 * transaction stubs that use that to try and batch
8935 * hardware accesses.
8936 */
8937 pmu->start_txn = perf_pmu_start_txn;
8938 pmu->commit_txn = perf_pmu_commit_txn;
8939 pmu->cancel_txn = perf_pmu_cancel_txn;
8940 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008941 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008942 pmu->commit_txn = perf_pmu_nop_int;
8943 pmu->cancel_txn = perf_pmu_nop_void;
8944 }
8945 }
8946
8947 if (!pmu->pmu_enable) {
8948 pmu->pmu_enable = perf_pmu_nop_void;
8949 pmu->pmu_disable = perf_pmu_nop_void;
8950 }
8951
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008952 if (!pmu->event_idx)
8953 pmu->event_idx = perf_event_idx_default;
8954
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008955 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008956 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008957 ret = 0;
8958unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008959 mutex_unlock(&pmus_lock);
8960
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008961 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008962
Peter Zijlstraabe43402010-11-17 23:17:37 +01008963free_dev:
8964 device_del(pmu->dev);
8965 put_device(pmu->dev);
8966
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008967free_idr:
8968 if (pmu->type >= PERF_TYPE_MAX)
8969 idr_remove(&pmu_idr, pmu->type);
8970
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008971free_pdc:
8972 free_percpu(pmu->pmu_disable_count);
8973 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008974}
Yan, Zhengc464c762014-03-18 16:56:41 +08008975EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008976
8977void perf_pmu_unregister(struct pmu *pmu)
8978{
Jiri Olsa09338402016-10-20 13:10:11 +02008979 int remove_device;
8980
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008981 mutex_lock(&pmus_lock);
Jiri Olsa09338402016-10-20 13:10:11 +02008982 remove_device = pmu_bus_running;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008983 list_del_rcu(&pmu->entry);
8984 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008985
8986 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02008987 * We dereference the pmu list under both SRCU and regular RCU, so
8988 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008989 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008990 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02008991 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008992
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008993 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008994 if (pmu->type >= PERF_TYPE_MAX)
8995 idr_remove(&pmu_idr, pmu->type);
Jiri Olsa09338402016-10-20 13:10:11 +02008996 if (remove_device) {
8997 if (pmu->nr_addr_filters)
8998 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
8999 device_del(pmu->dev);
9000 put_device(pmu->dev);
9001 }
Peter Zijlstra51676952010-12-07 14:18:20 +01009002 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009003}
Yan, Zhengc464c762014-03-18 16:56:41 +08009004EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009005
Mark Rutlandcc34b982015-01-07 14:56:51 +00009006static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
9007{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009008 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00009009 int ret;
9010
9011 if (!try_module_get(pmu->module))
9012 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009013
9014 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02009015 /*
9016 * This ctx->mutex can nest when we're called through
9017 * inheritance. See the perf_event_ctx_lock_nested() comment.
9018 */
9019 ctx = perf_event_ctx_lock_nested(event->group_leader,
9020 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009021 BUG_ON(!ctx);
9022 }
9023
Mark Rutlandcc34b982015-01-07 14:56:51 +00009024 event->pmu = pmu;
9025 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009026
9027 if (ctx)
9028 perf_event_ctx_unlock(event->group_leader, ctx);
9029
Mark Rutlandcc34b982015-01-07 14:56:51 +00009030 if (ret)
9031 module_put(pmu->module);
9032
9033 return ret;
9034}
9035
Geliang Tang18ab2cd2015-09-27 23:25:50 +08009036static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009037{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009038 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009039 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08009040 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009041
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009042 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009043
9044 rcu_read_lock();
9045 pmu = idr_find(&pmu_idr, event->attr.type);
9046 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08009047 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009048 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08009049 if (ret)
9050 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009051 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08009052 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009053
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009054 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009055 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009056 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009057 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009058
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009059 if (ret != -ENOENT) {
9060 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009061 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009062 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009063 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009064 pmu = ERR_PTR(-ENOENT);
9065unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009066 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009067
9068 return pmu;
9069}
9070
Kan Liangf2fb6be2016-03-23 11:24:37 -07009071static void attach_sb_event(struct perf_event *event)
9072{
9073 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
9074
9075 raw_spin_lock(&pel->lock);
9076 list_add_rcu(&event->sb_list, &pel->list);
9077 raw_spin_unlock(&pel->lock);
9078}
9079
Peter Zijlstraaab5b712016-05-12 17:26:46 +02009080/*
9081 * We keep a list of all !task (and therefore per-cpu) events
9082 * that need to receive side-band records.
9083 *
9084 * This avoids having to scan all the various PMU per-cpu contexts
9085 * looking for them.
9086 */
Kan Liangf2fb6be2016-03-23 11:24:37 -07009087static void account_pmu_sb_event(struct perf_event *event)
9088{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07009089 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -07009090 attach_sb_event(event);
9091}
9092
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009093static void account_event_cpu(struct perf_event *event, int cpu)
9094{
9095 if (event->parent)
9096 return;
9097
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009098 if (is_cgroup_event(event))
9099 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
9100}
9101
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009102/* Freq events need the tick to stay alive (see perf_event_task_tick). */
9103static void account_freq_event_nohz(void)
9104{
9105#ifdef CONFIG_NO_HZ_FULL
9106 /* Lock so we don't race with concurrent unaccount */
9107 spin_lock(&nr_freq_lock);
9108 if (atomic_inc_return(&nr_freq_events) == 1)
9109 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
9110 spin_unlock(&nr_freq_lock);
9111#endif
9112}
9113
9114static void account_freq_event(void)
9115{
9116 if (tick_nohz_full_enabled())
9117 account_freq_event_nohz();
9118 else
9119 atomic_inc(&nr_freq_events);
9120}
9121
9122
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009123static void account_event(struct perf_event *event)
9124{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009125 bool inc = false;
9126
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009127 if (event->parent)
9128 return;
9129
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009130 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009131 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009132 if (event->attr.mmap || event->attr.mmap_data)
9133 atomic_inc(&nr_mmap_events);
9134 if (event->attr.comm)
9135 atomic_inc(&nr_comm_events);
9136 if (event->attr.task)
9137 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009138 if (event->attr.freq)
9139 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03009140 if (event->attr.context_switch) {
9141 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009142 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03009143 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009144 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009145 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009146 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009147 inc = true;
9148
Peter Zijlstra9107c892016-02-24 18:45:45 +01009149 if (inc) {
9150 if (atomic_inc_not_zero(&perf_sched_count))
9151 goto enabled;
9152
9153 mutex_lock(&perf_sched_mutex);
9154 if (!atomic_read(&perf_sched_count)) {
9155 static_branch_enable(&perf_sched_events);
9156 /*
9157 * Guarantee that all CPUs observe they key change and
9158 * call the perf scheduling hooks before proceeding to
9159 * install events that need them.
9160 */
9161 synchronize_sched();
9162 }
9163 /*
9164 * Now that we have waited for the sync_sched(), allow further
9165 * increments to by-pass the mutex.
9166 */
9167 atomic_inc(&perf_sched_count);
9168 mutex_unlock(&perf_sched_mutex);
9169 }
9170enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009171
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009172 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07009173
9174 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009175}
9176
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009177/*
9178 * Allocate and initialize a event structure
9179 */
9180static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009181perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009182 struct task_struct *task,
9183 struct perf_event *group_leader,
9184 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009185 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00009186 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009187{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009188 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009189 struct perf_event *event;
9190 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009191 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009192
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009193 if ((unsigned)cpu >= nr_cpu_ids) {
9194 if (!task || cpu != -1)
9195 return ERR_PTR(-EINVAL);
9196 }
9197
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009198 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009199 if (!event)
9200 return ERR_PTR(-ENOMEM);
9201
9202 /*
9203 * Single events are their own group leaders, with an
9204 * empty sibling list:
9205 */
9206 if (!group_leader)
9207 group_leader = event;
9208
9209 mutex_init(&event->child_mutex);
9210 INIT_LIST_HEAD(&event->child_list);
9211
9212 INIT_LIST_HEAD(&event->group_entry);
9213 INIT_LIST_HEAD(&event->event_entry);
9214 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009215 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01009216 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009217 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01009218 INIT_HLIST_NODE(&event->hlist_entry);
9219
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009220
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009221 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08009222 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009223
9224 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009225 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009226
Al Viroa6fa9412012-08-20 14:59:25 +01009227 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009228 event->cpu = cpu;
9229 event->attr = *attr;
9230 event->group_leader = group_leader;
9231 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009232 event->oncpu = -1;
9233
9234 event->parent = parent_event;
9235
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08009236 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009237 event->id = atomic64_inc_return(&perf_event_id);
9238
9239 event->state = PERF_EVENT_STATE_INACTIVE;
9240
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009241 if (task) {
9242 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009243 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009244 * XXX pmu::event_init needs to know what task to account to
9245 * and we cannot use the ctx information because we need the
9246 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009247 */
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009248 get_task_struct(task);
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009249 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009250 }
9251
Peter Zijlstra34f43922015-02-20 14:05:38 +01009252 event->clock = &local_clock;
9253 if (parent_event)
9254 event->clock = parent_event->clock;
9255
Avi Kivity4dc0da82011-06-29 18:42:35 +03009256 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01009257 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009258 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +02009259#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009260 if (overflow_handler == bpf_overflow_handler) {
9261 struct bpf_prog *prog = bpf_prog_inc(parent_event->prog);
9262
9263 if (IS_ERR(prog)) {
9264 err = PTR_ERR(prog);
9265 goto err_ns;
9266 }
9267 event->prog = prog;
9268 event->orig_overflow_handler =
9269 parent_event->orig_overflow_handler;
9270 }
9271#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +03009272 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009273
Wang Nan18794452016-03-28 06:41:30 +00009274 if (overflow_handler) {
9275 event->overflow_handler = overflow_handler;
9276 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00009277 } else if (is_write_backward(event)){
9278 event->overflow_handler = perf_event_output_backward;
9279 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00009280 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00009281 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00009282 event->overflow_handler_context = NULL;
9283 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02009284
Jiri Olsa0231bb52013-02-01 11:23:45 +01009285 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009286
9287 pmu = NULL;
9288
9289 hwc = &event->hw;
9290 hwc->sample_period = attr->sample_period;
9291 if (attr->freq && attr->sample_freq)
9292 hwc->sample_period = 1;
9293 hwc->last_period = hwc->sample_period;
9294
Peter Zijlstrae7850592010-05-21 14:43:08 +02009295 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009296
9297 /*
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009298 * We currently do not support PERF_SAMPLE_READ on inherited events.
9299 * See perf_output_read().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009300 */
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009301 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009302 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009303
Yan, Zhenga46a2302014-11-04 21:56:06 -05009304 if (!has_branch_stack(event))
9305 event->attr.branch_sample_type = 0;
9306
Matt Fleming79dff512015-01-23 18:45:42 +00009307 if (cgroup_fd != -1) {
9308 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
9309 if (err)
9310 goto err_ns;
9311 }
9312
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009313 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009314 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009315 goto err_ns;
9316 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009317 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009318 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009319 }
9320
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009321 err = exclusive_event_init(event);
9322 if (err)
9323 goto err_pmu;
9324
Alexander Shishkin375637b2016-04-27 18:44:46 +03009325 if (has_addr_filter(event)) {
9326 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
9327 sizeof(unsigned long),
9328 GFP_KERNEL);
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009329 if (!event->addr_filters_offs) {
9330 err = -ENOMEM;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009331 goto err_per_task;
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009332 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009333
9334 /* force hw sync on the address filters */
9335 event->addr_filters_gen = 1;
9336 }
9337
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009338 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009339 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009340 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009341 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009342 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01009343 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009344 }
9345
Alexander Shishkin927a5572016-03-02 13:24:14 +02009346 /* symmetric to unaccount_event() in _free_event() */
9347 account_event(event);
9348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009349 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009350
Alexander Shishkin375637b2016-04-27 18:44:46 +03009351err_addr_filters:
9352 kfree(event->addr_filters_offs);
9353
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009354err_per_task:
9355 exclusive_event_destroy(event);
9356
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009357err_pmu:
9358 if (event->destroy)
9359 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08009360 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009361err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00009362 if (is_cgroup_event(event))
9363 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009364 if (event->ns)
9365 put_pid_ns(event->ns);
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009366 if (event->hw.target)
9367 put_task_struct(event->hw.target);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009368 kfree(event);
9369
9370 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009371}
9372
9373static int perf_copy_attr(struct perf_event_attr __user *uattr,
9374 struct perf_event_attr *attr)
9375{
9376 u32 size;
9377 int ret;
9378
9379 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
9380 return -EFAULT;
9381
9382 /*
9383 * zero the full structure, so that a short copy will be nice.
9384 */
9385 memset(attr, 0, sizeof(*attr));
9386
9387 ret = get_user(size, &uattr->size);
9388 if (ret)
9389 return ret;
9390
9391 if (size > PAGE_SIZE) /* silly large */
9392 goto err_size;
9393
9394 if (!size) /* abi compat */
9395 size = PERF_ATTR_SIZE_VER0;
9396
9397 if (size < PERF_ATTR_SIZE_VER0)
9398 goto err_size;
9399
9400 /*
9401 * If we're handed a bigger struct than we know of,
9402 * ensure all the unknown bits are 0 - i.e. new
9403 * user-space does not rely on any kernel feature
9404 * extensions we dont know about yet.
9405 */
9406 if (size > sizeof(*attr)) {
9407 unsigned char __user *addr;
9408 unsigned char __user *end;
9409 unsigned char val;
9410
9411 addr = (void __user *)uattr + sizeof(*attr);
9412 end = (void __user *)uattr + size;
9413
9414 for (; addr < end; addr++) {
9415 ret = get_user(val, addr);
9416 if (ret)
9417 return ret;
9418 if (val)
9419 goto err_size;
9420 }
9421 size = sizeof(*attr);
9422 }
9423
9424 ret = copy_from_user(attr, uattr, size);
9425 if (ret)
9426 return -EFAULT;
9427
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05309428 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009429 return -EINVAL;
9430
9431 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
9432 return -EINVAL;
9433
9434 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
9435 return -EINVAL;
9436
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009437 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
9438 u64 mask = attr->branch_sample_type;
9439
9440 /* only using defined bits */
9441 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
9442 return -EINVAL;
9443
9444 /* at least one branch bit must be set */
9445 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9446 return -EINVAL;
9447
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009448 /* propagate priv level, when not set for branch */
9449 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9450
9451 /* exclude_kernel checked on syscall entry */
9452 if (!attr->exclude_kernel)
9453 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9454
9455 if (!attr->exclude_user)
9456 mask |= PERF_SAMPLE_BRANCH_USER;
9457
9458 if (!attr->exclude_hv)
9459 mask |= PERF_SAMPLE_BRANCH_HV;
9460 /*
9461 * adjust user setting (for HW filter setup)
9462 */
9463 attr->branch_sample_type = mask;
9464 }
Stephane Eraniane7122092013-06-06 11:02:04 +02009465 /* privileged levels capture (kernel, hv): check permissions */
9466 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02009467 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9468 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009469 }
Jiri Olsa40189942012-08-07 15:20:37 +02009470
Jiri Olsac5ebced2012-08-07 15:20:40 +02009471 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02009472 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02009473 if (ret)
9474 return ret;
9475 }
9476
9477 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9478 if (!arch_perf_have_user_stack_dump())
9479 return -ENOSYS;
9480
9481 /*
9482 * We have __u32 type for the size, but so far
9483 * we can only use __u16 as maximum due to the
9484 * __u16 sample size limit.
9485 */
9486 if (attr->sample_stack_user >= USHRT_MAX)
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009487 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009488 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009489 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009490 }
Jiri Olsa40189942012-08-07 15:20:37 +02009491
Stephane Eranian60e23642014-09-24 13:48:37 +02009492 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9493 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009494out:
9495 return ret;
9496
9497err_size:
9498 put_user(sizeof(*attr), &uattr->size);
9499 ret = -E2BIG;
9500 goto out;
9501}
9502
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009503static int
9504perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009505{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009506 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009507 int ret = -EINVAL;
9508
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009509 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009510 goto set;
9511
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009512 /* don't allow circular references */
9513 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009514 goto out;
9515
Peter Zijlstra0f139302010-05-20 14:35:15 +02009516 /*
9517 * Don't allow cross-cpu buffers
9518 */
9519 if (output_event->cpu != event->cpu)
9520 goto out;
9521
9522 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009523 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009524 */
9525 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9526 goto out;
9527
Peter Zijlstra34f43922015-02-20 14:05:38 +01009528 /*
9529 * Mixing clocks in the same buffer is trouble you don't need.
9530 */
9531 if (output_event->clock != event->clock)
9532 goto out;
9533
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009534 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009535 * Either writing ring buffer from beginning or from end.
9536 * Mixing is not allowed.
9537 */
9538 if (is_write_backward(output_event) != is_write_backward(event))
9539 goto out;
9540
9541 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009542 * If both events generate aux data, they must be on the same PMU
9543 */
9544 if (has_aux(event) && has_aux(output_event) &&
9545 event->pmu != output_event->pmu)
9546 goto out;
9547
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009548set:
9549 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009550 /* Can't redirect output if we've got an active mmap() */
9551 if (atomic_read(&event->mmap_count))
9552 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009553
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009554 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009555 /* get the rb we want to redirect to */
9556 rb = ring_buffer_get(output_event);
9557 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009558 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009559 }
9560
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009561 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009562
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009563 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009564unlock:
9565 mutex_unlock(&event->mmap_mutex);
9566
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009567out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009568 return ret;
9569}
9570
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009571static void mutex_lock_double(struct mutex *a, struct mutex *b)
9572{
9573 if (b < a)
9574 swap(a, b);
9575
9576 mutex_lock(a);
9577 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9578}
9579
Peter Zijlstra34f43922015-02-20 14:05:38 +01009580static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9581{
9582 bool nmi_safe = false;
9583
9584 switch (clk_id) {
9585 case CLOCK_MONOTONIC:
9586 event->clock = &ktime_get_mono_fast_ns;
9587 nmi_safe = true;
9588 break;
9589
9590 case CLOCK_MONOTONIC_RAW:
9591 event->clock = &ktime_get_raw_fast_ns;
9592 nmi_safe = true;
9593 break;
9594
9595 case CLOCK_REALTIME:
9596 event->clock = &ktime_get_real_ns;
9597 break;
9598
9599 case CLOCK_BOOTTIME:
9600 event->clock = &ktime_get_boot_ns;
9601 break;
9602
9603 case CLOCK_TAI:
9604 event->clock = &ktime_get_tai_ns;
9605 break;
9606
9607 default:
9608 return -EINVAL;
9609 }
9610
9611 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9612 return -EINVAL;
9613
9614 return 0;
9615}
9616
Peter Zijlstra922813f2017-01-11 21:09:50 +01009617/*
9618 * Variation on perf_event_ctx_lock_nested(), except we take two context
9619 * mutexes.
9620 */
9621static struct perf_event_context *
9622__perf_event_ctx_lock_double(struct perf_event *group_leader,
9623 struct perf_event_context *ctx)
9624{
9625 struct perf_event_context *gctx;
9626
9627again:
9628 rcu_read_lock();
9629 gctx = READ_ONCE(group_leader->ctx);
9630 if (!atomic_inc_not_zero(&gctx->refcount)) {
9631 rcu_read_unlock();
9632 goto again;
9633 }
9634 rcu_read_unlock();
9635
9636 mutex_lock_double(&gctx->mutex, &ctx->mutex);
9637
9638 if (group_leader->ctx != gctx) {
9639 mutex_unlock(&ctx->mutex);
9640 mutex_unlock(&gctx->mutex);
9641 put_ctx(gctx);
9642 goto again;
9643 }
9644
9645 return gctx;
9646}
9647
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009648/**
9649 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9650 *
9651 * @attr_uptr: event_id type attributes for monitoring/sampling
9652 * @pid: target pid
9653 * @cpu: target cpu
9654 * @group_fd: group leader event fd
9655 */
9656SYSCALL_DEFINE5(perf_event_open,
9657 struct perf_event_attr __user *, attr_uptr,
9658 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9659{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009660 struct perf_event *group_leader = NULL, *output_event = NULL;
9661 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009662 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009663 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009664 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009665 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009666 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009667 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009668 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009669 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009670 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009671 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009672 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009673
9674 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009675 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009676 return -EINVAL;
9677
Jeff Vander Stoepd28f8562016-05-29 14:22:32 -07009678 if (perf_paranoid_any() && !capable(CAP_SYS_ADMIN))
9679 return -EACCES;
9680
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009681 err = perf_copy_attr(attr_uptr, &attr);
9682 if (err)
9683 return err;
9684
9685 if (!attr.exclude_kernel) {
9686 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9687 return -EACCES;
9688 }
9689
9690 if (attr.freq) {
9691 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9692 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009693 } else {
9694 if (attr.sample_period & (1ULL << 63))
9695 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009696 }
9697
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009698 if (!attr.sample_max_stack)
9699 attr.sample_max_stack = sysctl_perf_event_max_stack;
9700
Stephane Eraniane5d13672011-02-14 11:20:01 +02009701 /*
9702 * In cgroup mode, the pid argument is used to pass the fd
9703 * opened to the cgroup directory in cgroupfs. The cpu argument
9704 * designates the cpu on which to monitor threads from that
9705 * cgroup.
9706 */
9707 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9708 return -EINVAL;
9709
Yann Droneauda21b0b32014-01-05 21:36:33 +01009710 if (flags & PERF_FLAG_FD_CLOEXEC)
9711 f_flags |= O_CLOEXEC;
9712
9713 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009714 if (event_fd < 0)
9715 return event_fd;
9716
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009717 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009718 err = perf_fget_light(group_fd, &group);
9719 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009720 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009721 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009722 if (flags & PERF_FLAG_FD_OUTPUT)
9723 output_event = group_leader;
9724 if (flags & PERF_FLAG_FD_NO_GROUP)
9725 group_leader = NULL;
9726 }
9727
Stephane Eraniane5d13672011-02-14 11:20:01 +02009728 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009729 task = find_lively_task_by_vpid(pid);
9730 if (IS_ERR(task)) {
9731 err = PTR_ERR(task);
9732 goto err_group_fd;
9733 }
9734 }
9735
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009736 if (task && group_leader &&
9737 group_leader->attr.inherit != attr.inherit) {
9738 err = -EINVAL;
9739 goto err_task;
9740 }
9741
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009742 get_online_cpus();
9743
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009744 if (task) {
9745 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9746 if (err)
9747 goto err_cpus;
9748
9749 /*
9750 * Reuse ptrace permission checks for now.
9751 *
9752 * We must hold cred_guard_mutex across this and any potential
9753 * perf_install_in_context() call for this new event to
9754 * serialize against exec() altering our credentials (and the
9755 * perf_event_exit_task() that could imply).
9756 */
9757 err = -EACCES;
9758 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9759 goto err_cred;
9760 }
9761
Matt Fleming79dff512015-01-23 18:45:42 +00009762 if (flags & PERF_FLAG_PID_CGROUP)
9763 cgroup_fd = pid;
9764
Avi Kivity4dc0da82011-06-29 18:42:35 +03009765 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009766 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009767 if (IS_ERR(event)) {
9768 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009769 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009770 }
9771
Vince Weaver53b25332014-05-16 17:12:12 -04009772 if (is_sampling_event(event)) {
9773 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +05309774 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -04009775 goto err_alloc;
9776 }
9777 }
9778
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009779 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009780 * Special case software events and allow them to be part of
9781 * any hardware group.
9782 */
9783 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009784
Peter Zijlstra34f43922015-02-20 14:05:38 +01009785 if (attr.use_clockid) {
9786 err = perf_event_set_clock(event, attr.clockid);
9787 if (err)
9788 goto err_alloc;
9789 }
9790
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009791 if (pmu->task_ctx_nr == perf_sw_context)
9792 event->event_caps |= PERF_EV_CAP_SOFTWARE;
9793
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009794 if (group_leader &&
9795 (is_software_event(event) != is_software_event(group_leader))) {
9796 if (is_software_event(event)) {
9797 /*
9798 * If event and group_leader are not both a software
9799 * event, and event is, then group leader is not.
9800 *
9801 * Allow the addition of software events to !software
9802 * groups, this is safe because software events never
9803 * fail to schedule.
9804 */
9805 pmu = group_leader->pmu;
9806 } else if (is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009807 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009808 /*
9809 * In case the group is a pure software group, and we
9810 * try to add a hardware event, move the whole group to
9811 * the hardware context.
9812 */
9813 move_group = 1;
9814 }
9815 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009816
9817 /*
9818 * Get the target context (task or percpu):
9819 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009820 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009821 if (IS_ERR(ctx)) {
9822 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009823 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009824 }
9825
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009826 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9827 err = -EBUSY;
9828 goto err_context;
9829 }
9830
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009831 /*
9832 * Look up the group leader (we will attach this event to it):
9833 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009834 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009835 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009836
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009837 /*
9838 * Do not allow a recursive hierarchy (this new sibling
9839 * becoming part of another group-sibling):
9840 */
9841 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009842 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009843
9844 /* All events in a group should have the same clock */
9845 if (group_leader->clock != event->clock)
9846 goto err_context;
9847
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009848 /*
Mark Rutlandbde66082017-06-22 15:41:38 +01009849 * Make sure we're both events for the same CPU;
9850 * grouping events for different CPUs is broken; since
9851 * you can never concurrently schedule them anyhow.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009852 */
Mark Rutlandbde66082017-06-22 15:41:38 +01009853 if (group_leader->cpu != event->cpu)
9854 goto err_context;
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009855
Mark Rutlandbde66082017-06-22 15:41:38 +01009856 /*
9857 * Make sure we're both on the same task, or both
9858 * per-CPU events.
9859 */
9860 if (group_leader->ctx->task != ctx->task)
9861 goto err_context;
9862
9863 /*
9864 * Do not allow to attach to a group in a different task
9865 * or CPU context. If we're moving SW events, we'll fix
9866 * this up later, so allow that.
9867 */
9868 if (!move_group && group_leader->ctx != ctx)
9869 goto err_context;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009871 /*
9872 * Only a group leader can be exclusive or pinned
9873 */
9874 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009875 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009876 }
9877
9878 if (output_event) {
9879 err = perf_event_set_output(event, output_event);
9880 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009881 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009882 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009883
Yann Droneauda21b0b32014-01-05 21:36:33 +01009884 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9885 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009886 if (IS_ERR(event_file)) {
9887 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009888 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009889 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009890 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009891
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009892 if (move_group) {
Peter Zijlstra922813f2017-01-11 21:09:50 +01009893 gctx = __perf_event_ctx_lock_double(group_leader, ctx);
9894
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009895 if (gctx->task == TASK_TOMBSTONE) {
9896 err = -ESRCH;
9897 goto err_locked;
9898 }
Peter Zijlstra922813f2017-01-11 21:09:50 +01009899
9900 /*
9901 * Check if we raced against another sys_perf_event_open() call
9902 * moving the software group underneath us.
9903 */
9904 if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
9905 /*
9906 * If someone moved the group out from under us, check
9907 * if this new event wound up on the same ctx, if so
9908 * its the regular !move_group case, otherwise fail.
9909 */
9910 if (gctx != ctx) {
9911 err = -EINVAL;
9912 goto err_locked;
9913 } else {
9914 perf_event_ctx_unlock(group_leader, gctx);
9915 move_group = 0;
9916 }
9917 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009918 } else {
9919 mutex_lock(&ctx->mutex);
9920 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009921
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009922 if (ctx->task == TASK_TOMBSTONE) {
9923 err = -ESRCH;
9924 goto err_locked;
9925 }
9926
Peter Zijlstraa7239682015-09-09 19:06:33 +02009927 if (!perf_event_validate_size(event)) {
9928 err = -E2BIG;
9929 goto err_locked;
9930 }
9931
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009932 /*
9933 * Must be under the same ctx::mutex as perf_install_in_context(),
9934 * because we need to serialize with concurrent event creation.
9935 */
9936 if (!exclusive_event_installable(event, ctx)) {
9937 /* exclusive and group stuff are assumed mutually exclusive */
9938 WARN_ON_ONCE(move_group);
9939
9940 err = -EBUSY;
9941 goto err_locked;
9942 }
9943
9944 WARN_ON_ONCE(ctx->parent_ctx);
9945
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009946 /*
9947 * This is the point on no return; we cannot fail hereafter. This is
9948 * where we start modifying current state.
9949 */
9950
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009951 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009952 /*
9953 * See perf_event_ctx_lock() for comments on the details
9954 * of swizzling perf_event::ctx.
9955 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009956 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009957
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009958 list_for_each_entry(sibling, &group_leader->sibling_list,
9959 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009960 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009961 put_ctx(gctx);
9962 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009963
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009964 /*
9965 * Wait for everybody to stop referencing the events through
9966 * the old lists, before installing it on new lists.
9967 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009968 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009969
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009970 /*
9971 * Install the group siblings before the group leader.
9972 *
9973 * Because a group leader will try and install the entire group
9974 * (through the sibling list, which is still in-tact), we can
9975 * end up with siblings installed in the wrong context.
9976 *
9977 * By installing siblings first we NO-OP because they're not
9978 * reachable through the group lists.
9979 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009980 list_for_each_entry(sibling, &group_leader->sibling_list,
9981 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009982 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01009983 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009984 get_ctx(ctx);
9985 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009986
9987 /*
9988 * Removing from the context ends up with disabled
9989 * event. What we want here is event in the initial
9990 * startup state, ready to be add into new context.
9991 */
9992 perf_event__state_init(group_leader);
9993 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9994 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009995
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009996 /*
9997 * Now that all events are installed in @ctx, nothing
9998 * references @gctx anymore, so drop the last reference we have
9999 * on it.
10000 */
10001 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010002 }
10003
Peter Zijlstraf73e22a2015-09-09 20:48:22 +020010004 /*
10005 * Precalculate sample_data sizes; do while holding ctx::mutex such
10006 * that we're serialized against further additions and before
10007 * perf_install_in_context() which is the point the event is active and
10008 * can use these values.
10009 */
10010 perf_event__header_size(event);
10011 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010012
Peter Zijlstra78cd2c72016-01-25 14:08:45 +010010013 event->owner = current;
10014
Yan, Zhenge2d37cd2012-06-15 14:31:32 +080010015 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010016 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010017
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010018 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010019 perf_event_ctx_unlock(group_leader, gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010020 mutex_unlock(&ctx->mutex);
10021
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010022 if (task) {
10023 mutex_unlock(&task->signal->cred_guard_mutex);
10024 put_task_struct(task);
10025 }
10026
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010027 put_online_cpus();
10028
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010029 mutex_lock(&current->perf_event_mutex);
10030 list_add_tail(&event->owner_entry, &current->perf_event_list);
10031 mutex_unlock(&current->perf_event_mutex);
10032
Peter Zijlstra8a495422010-05-27 15:47:49 +020010033 /*
10034 * Drop the reference on the group_event after placing the
10035 * new event on the sibling_list. This ensures destruction
10036 * of the group leader will find the pointer to itself in
10037 * perf_group_detach().
10038 */
Al Viro2903ff02012-08-28 12:52:22 -040010039 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010040 fd_install(event_fd, event_file);
10041 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010042
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010043err_locked:
10044 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010045 perf_event_ctx_unlock(group_leader, gctx);
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010046 mutex_unlock(&ctx->mutex);
10047/* err_file: */
10048 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010049err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010050 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -040010051 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020010052err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +010010053 /*
10054 * If event_file is set, the fput() above will have called ->release()
10055 * and that will take care of freeing the event.
10056 */
10057 if (!event_file)
10058 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010059err_cred:
10060 if (task)
10061 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010062err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010063 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010064err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +020010065 if (task)
10066 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010067err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -040010068 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010069err_fd:
10070 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010071 return err;
10072}
10073
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010074/**
10075 * perf_event_create_kernel_counter
10076 *
10077 * @attr: attributes of the counter to create
10078 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -070010079 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010080 */
10081struct perf_event *
10082perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -070010083 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +030010084 perf_overflow_handler_t overflow_handler,
10085 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010086{
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010087 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010088 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010089 int err;
10090
10091 /*
10092 * Get the target context (task or percpu):
10093 */
10094
Avi Kivity4dc0da82011-06-29 18:42:35 +030010095 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000010096 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010097 if (IS_ERR(event)) {
10098 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010099 goto err;
10100 }
10101
Jiri Olsaf8697762014-08-01 14:33:01 +020010102 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010103 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +020010104
Yan, Zheng4af57ef282014-11-04 21:56:01 -050010105 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010106 if (IS_ERR(ctx)) {
10107 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010108 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010109 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010110
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010111 WARN_ON_ONCE(ctx->parent_ctx);
10112 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010113 if (ctx->task == TASK_TOMBSTONE) {
10114 err = -ESRCH;
10115 goto err_unlock;
10116 }
10117
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010118 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010119 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010120 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010121 }
10122
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010123 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010124 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010125 mutex_unlock(&ctx->mutex);
10126
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010127 return event;
10128
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010129err_unlock:
10130 mutex_unlock(&ctx->mutex);
10131 perf_unpin_context(ctx);
10132 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010133err_free:
10134 free_event(event);
10135err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010136 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010137}
10138EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
10139
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010140void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
10141{
10142 struct perf_event_context *src_ctx;
10143 struct perf_event_context *dst_ctx;
10144 struct perf_event *event, *tmp;
10145 LIST_HEAD(events);
10146
10147 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
10148 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
10149
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010150 /*
10151 * See perf_event_ctx_lock() for comments on the details
10152 * of swizzling perf_event::ctx.
10153 */
10154 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010155 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
10156 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010157 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010158 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010159 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020010160 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010161 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010162
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010163 /*
10164 * Wait for the events to quiesce before re-instating them.
10165 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010166 synchronize_rcu();
10167
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010168 /*
10169 * Re-instate events in 2 passes.
10170 *
10171 * Skip over group leaders and only install siblings on this first
10172 * pass, siblings will not get enabled without a leader, however a
10173 * leader will enable its siblings, even if those are still on the old
10174 * context.
10175 */
10176 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10177 if (event->group_leader == event)
10178 continue;
10179
10180 list_del(&event->migrate_entry);
10181 if (event->state >= PERF_EVENT_STATE_OFF)
10182 event->state = PERF_EVENT_STATE_INACTIVE;
10183 account_event_cpu(event, dst_cpu);
10184 perf_install_in_context(dst_ctx, event, dst_cpu);
10185 get_ctx(dst_ctx);
10186 }
10187
10188 /*
10189 * Once all the siblings are setup properly, install the group leaders
10190 * to make it go.
10191 */
Peter Zijlstra98861672013-10-03 16:02:23 +020010192 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10193 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010194 if (event->state >= PERF_EVENT_STATE_OFF)
10195 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010196 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010197 perf_install_in_context(dst_ctx, event, dst_cpu);
10198 get_ctx(dst_ctx);
10199 }
10200 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010201 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010202}
10203EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
10204
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010205static void sync_child_event(struct perf_event *child_event,
10206 struct task_struct *child)
10207{
10208 struct perf_event *parent_event = child_event->parent;
10209 u64 child_val;
10210
10211 if (child_event->attr.inherit_stat)
10212 perf_event_read_event(child_event, child);
10213
Peter Zijlstrab5e58792010-05-21 14:43:12 +020010214 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010215
10216 /*
10217 * Add back the child's count to the parent's count:
10218 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020010219 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010220 atomic64_add(child_event->total_time_enabled,
10221 &parent_event->child_total_time_enabled);
10222 atomic64_add(child_event->total_time_running,
10223 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010224}
10225
10226static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010227perf_event_exit_event(struct perf_event *child_event,
10228 struct perf_event_context *child_ctx,
10229 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010230{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010231 struct perf_event *parent_event = child_event->parent;
10232
Peter Zijlstra1903d502014-07-15 17:27:27 +020010233 /*
10234 * Do not destroy the 'original' grouping; because of the context
10235 * switch optimization the original events could've ended up in a
10236 * random child task.
10237 *
10238 * If we were to destroy the original group, all group related
10239 * operations would cease to function properly after this random
10240 * child dies.
10241 *
10242 * Do destroy all inherited groups, we don't care about those
10243 * and being thorough is better.
10244 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010245 raw_spin_lock_irq(&child_ctx->lock);
10246 WARN_ON_ONCE(child_ctx->is_active);
10247
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010248 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010010249 perf_group_detach(child_event);
10250 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +010010251 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010252 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010253
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010254 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010255 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010256 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010257 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040010258 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010259 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010260 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010261 /*
10262 * Child events can be cleaned up.
10263 */
10264
10265 sync_child_event(child_event, child);
10266
10267 /*
10268 * Remove this event from the parent's list
10269 */
10270 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
10271 mutex_lock(&parent_event->child_mutex);
10272 list_del_init(&child_event->child_list);
10273 mutex_unlock(&parent_event->child_mutex);
10274
10275 /*
10276 * Kick perf_poll() for is_event_hup().
10277 */
10278 perf_event_wakeup(parent_event);
10279 free_event(child_event);
10280 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010281}
10282
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010283static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010284{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010285 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010286 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010287
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010288 WARN_ON_ONCE(child != current);
10289
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010290 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010291 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010292 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010293
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010294 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010295 * In order to reduce the amount of tricky in ctx tear-down, we hold
10296 * ctx::mutex over the entire thing. This serializes against almost
10297 * everything that wants to access the ctx.
10298 *
10299 * The exception is sys_perf_event_open() /
10300 * perf_event_create_kernel_count() which does find_get_context()
10301 * without ctx::mutex (it cannot because of the move_group double mutex
10302 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010303 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010304 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010305
10306 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010307 * In a single ctx::lock section, de-schedule the events and detach the
10308 * context from the task such that we cannot ever get it scheduled back
10309 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010310 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010311 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010312 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010313
10314 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010315 * Now that the context is inactive, destroy the task <-> ctx relation
10316 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010317 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010318 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
10319 put_ctx(child_ctx); /* cannot be last */
10320 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
10321 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010322
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010323 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010324 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010325
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010326 if (clone_ctx)
10327 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010328
10329 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010330 * Report the task dead after unscheduling the events so that we
10331 * won't get any samples after PERF_RECORD_EXIT. We can however still
10332 * get a few PERF_RECORD_READ events.
10333 */
10334 perf_event_task(child, child_ctx, 0);
10335
Peter Zijlstraebf905f2014-05-29 19:00:24 +020010336 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010337 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010338
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010339 mutex_unlock(&child_ctx->mutex);
10340
10341 put_ctx(child_ctx);
10342}
10343
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010344/*
10345 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010346 *
10347 * Can be called with cred_guard_mutex held when called from
10348 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010349 */
10350void perf_event_exit_task(struct task_struct *child)
10351{
Peter Zijlstra88821352010-11-09 19:01:43 +010010352 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010353 int ctxn;
10354
Peter Zijlstra88821352010-11-09 19:01:43 +010010355 mutex_lock(&child->perf_event_mutex);
10356 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
10357 owner_entry) {
10358 list_del_init(&event->owner_entry);
10359
10360 /*
10361 * Ensure the list deletion is visible before we clear
10362 * the owner, closes a race against perf_release() where
10363 * we need to serialize on the owner->perf_event_mutex.
10364 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010010365 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010010366 }
10367 mutex_unlock(&child->perf_event_mutex);
10368
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010369 for_each_task_context_nr(ctxn)
10370 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010010371
10372 /*
10373 * The perf_event_exit_task_context calls perf_event_task
10374 * with child's task_ctx, which generates EXIT events for
10375 * child contexts and sets child->perf_event_ctxp[] to NULL.
10376 * At this point we need to send EXIT events to cpu contexts.
10377 */
10378 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010379}
10380
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010381static void perf_free_event(struct perf_event *event,
10382 struct perf_event_context *ctx)
10383{
10384 struct perf_event *parent = event->parent;
10385
10386 if (WARN_ON_ONCE(!parent))
10387 return;
10388
10389 mutex_lock(&parent->child_mutex);
10390 list_del_init(&event->child_list);
10391 mutex_unlock(&parent->child_mutex);
10392
Al Viroa6fa9412012-08-20 14:59:25 +010010393 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010394
Peter Zijlstra652884f2015-01-23 11:20:10 +010010395 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020010396 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010397 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010010398 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010399 free_event(event);
10400}
10401
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010402/*
Peter Zijlstra652884f2015-01-23 11:20:10 +010010403 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010404 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010010405 *
10406 * Not all locks are strictly required, but take them anyway to be nice and
10407 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010408 */
10409void perf_event_free_task(struct task_struct *task)
10410{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010411 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010412 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010413 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010414
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010415 for_each_task_context_nr(ctxn) {
10416 ctx = task->perf_event_ctxp[ctxn];
10417 if (!ctx)
10418 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010419
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010420 mutex_lock(&ctx->mutex);
Peter Zijlstrac04a9382017-03-16 13:47:48 +010010421 raw_spin_lock_irq(&ctx->lock);
10422 /*
10423 * Destroy the task <-> ctx relation and mark the context dead.
10424 *
10425 * This is important because even though the task hasn't been
10426 * exposed yet the context has been (through child_list).
10427 */
10428 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
10429 WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
10430 put_task_struct(task); /* cannot be last */
10431 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010432again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010433 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
10434 group_entry)
10435 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010436
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010437 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
10438 group_entry)
10439 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010440
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010441 if (!list_empty(&ctx->pinned_groups) ||
10442 !list_empty(&ctx->flexible_groups))
10443 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010444
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010445 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010446
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010447 put_ctx(ctx);
10448 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010449}
10450
Peter Zijlstra4e231c72010-09-09 21:01:59 +020010451void perf_event_delayed_put(struct task_struct *task)
10452{
10453 int ctxn;
10454
10455 for_each_task_context_nr(ctxn)
10456 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
10457}
10458
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010459struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000010460{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010461 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010462
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010463 file = fget_raw(fd);
10464 if (!file)
10465 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000010466
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010467 if (file->f_op != &perf_fops) {
10468 fput(file);
10469 return ERR_PTR(-EBADF);
10470 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000010471
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010472 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010473}
10474
10475const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
10476{
10477 if (!event)
10478 return ERR_PTR(-EINVAL);
10479
10480 return &event->attr;
10481}
10482
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010483/*
10484 * inherit a event from parent task to child task:
10485 */
10486static struct perf_event *
10487inherit_event(struct perf_event *parent_event,
10488 struct task_struct *parent,
10489 struct perf_event_context *parent_ctx,
10490 struct task_struct *child,
10491 struct perf_event *group_leader,
10492 struct perf_event_context *child_ctx)
10493{
Jiri Olsa1929def2014-09-12 13:18:27 +020010494 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010495 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020010496 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010497
10498 /*
10499 * Instead of creating recursive hierarchies of events,
10500 * we link inherited events back to the original parent,
10501 * which has a filp for sure, which we use as the reference
10502 * count:
10503 */
10504 if (parent_event->parent)
10505 parent_event = parent_event->parent;
10506
10507 child_event = perf_event_alloc(&parent_event->attr,
10508 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010509 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010510 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000010511 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010512 if (IS_ERR(child_event))
10513 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010010514
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010515 /*
10516 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10517 * must be under the same lock in order to serialize against
10518 * perf_event_release_kernel(), such that either we must observe
10519 * is_orphaned_event() or they will observe us on the child_list.
10520 */
10521 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020010522 if (is_orphaned_event(parent_event) ||
10523 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010524 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +010010525 free_event(child_event);
10526 return NULL;
10527 }
10528
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010529 get_ctx(child_ctx);
10530
10531 /*
10532 * Make the child state follow the state of the parent event,
10533 * not its attr.disabled bit. We hold the parent's mutex,
10534 * so we won't race with perf_event_{en, dis}able_family.
10535 */
Jiri Olsa1929def2014-09-12 13:18:27 +020010536 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010537 child_event->state = PERF_EVENT_STATE_INACTIVE;
10538 else
10539 child_event->state = PERF_EVENT_STATE_OFF;
10540
10541 if (parent_event->attr.freq) {
10542 u64 sample_period = parent_event->hw.sample_period;
10543 struct hw_perf_event *hwc = &child_event->hw;
10544
10545 hwc->sample_period = sample_period;
10546 hwc->last_period = sample_period;
10547
10548 local64_set(&hwc->period_left, sample_period);
10549 }
10550
10551 child_event->ctx = child_ctx;
10552 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010553 child_event->overflow_handler_context
10554 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010555
10556 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020010557 * Precalculate sample_data sizes
10558 */
10559 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020010560 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020010561
10562 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010563 * Link it up in the child's context:
10564 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020010565 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010566 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010567 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010568
10569 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010570 * Link this into the parent event's child list
10571 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010572 list_add_tail(&child_event->child_list, &parent_event->child_list);
10573 mutex_unlock(&parent_event->child_mutex);
10574
10575 return child_event;
10576}
10577
10578static int inherit_group(struct perf_event *parent_event,
10579 struct task_struct *parent,
10580 struct perf_event_context *parent_ctx,
10581 struct task_struct *child,
10582 struct perf_event_context *child_ctx)
10583{
10584 struct perf_event *leader;
10585 struct perf_event *sub;
10586 struct perf_event *child_ctr;
10587
10588 leader = inherit_event(parent_event, parent, parent_ctx,
10589 child, NULL, child_ctx);
10590 if (IS_ERR(leader))
10591 return PTR_ERR(leader);
10592 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10593 child_ctr = inherit_event(sub, parent, parent_ctx,
10594 child, leader, child_ctx);
10595 if (IS_ERR(child_ctr))
10596 return PTR_ERR(child_ctr);
10597 }
10598 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010599}
10600
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010601static int
10602inherit_task_group(struct perf_event *event, struct task_struct *parent,
10603 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010604 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010605 int *inherited_all)
10606{
10607 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010608 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010609
10610 if (!event->attr.inherit) {
10611 *inherited_all = 0;
10612 return 0;
10613 }
10614
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010615 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010616 if (!child_ctx) {
10617 /*
10618 * This is executed from the parent task context, so
10619 * inherit events that have been marked for cloning.
10620 * First allocate and initialize a context for the
10621 * child.
10622 */
10623
Jiri Olsa734df5a2013-07-09 17:44:10 +020010624 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010625 if (!child_ctx)
10626 return -ENOMEM;
10627
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010628 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010629 }
10630
10631 ret = inherit_group(event, parent, parent_ctx,
10632 child, child_ctx);
10633
10634 if (ret)
10635 *inherited_all = 0;
10636
10637 return ret;
10638}
10639
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010640/*
10641 * Initialize the perf_event context in task_struct
10642 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010643static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010644{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010645 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010646 struct perf_event_context *cloned_ctx;
10647 struct perf_event *event;
10648 struct task_struct *parent = current;
10649 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010650 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010651 int ret = 0;
10652
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010653 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010654 return 0;
10655
10656 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010657 * If the parent's context is a clone, pin it so it won't get
10658 * swapped under us.
10659 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010660 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010661 if (!parent_ctx)
10662 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010663
10664 /*
10665 * No need to check if parent_ctx != NULL here; since we saw
10666 * it non-NULL earlier, the only reason for it to become NULL
10667 * is if we exit, and since we're currently in the middle of
10668 * a fork we can't be exiting at the same time.
10669 */
10670
10671 /*
10672 * Lock the parent list. No need to lock the child - not PID
10673 * hashed yet and not running, so nobody can access it.
10674 */
10675 mutex_lock(&parent_ctx->mutex);
10676
10677 /*
10678 * We dont have to disable NMIs - we are only looking at
10679 * the list, not manipulating it:
10680 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010681 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010682 ret = inherit_task_group(event, parent, parent_ctx,
10683 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010684 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010685 goto out_unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010686 }
10687
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010688 /*
10689 * We can't hold ctx->lock when iterating the ->flexible_group list due
10690 * to allocations, but we need to prevent rotation because
10691 * rotate_ctx() will change the list from interrupt context.
10692 */
10693 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10694 parent_ctx->rotate_disable = 1;
10695 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10696
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010697 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010698 ret = inherit_task_group(event, parent, parent_ctx,
10699 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010700 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010701 goto out_unlock;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010702 }
10703
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010704 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10705 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010706
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010707 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010708
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010709 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010710 /*
10711 * Mark the child context as a clone of the parent
10712 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010713 *
10714 * Note that if the parent is a clone, the holding of
10715 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010716 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010717 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010718 if (cloned_ctx) {
10719 child_ctx->parent_ctx = cloned_ctx;
10720 child_ctx->parent_gen = parent_ctx->parent_gen;
10721 } else {
10722 child_ctx->parent_ctx = parent_ctx;
10723 child_ctx->parent_gen = parent_ctx->generation;
10724 }
10725 get_ctx(child_ctx->parent_ctx);
10726 }
10727
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010728 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010729out_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010730 mutex_unlock(&parent_ctx->mutex);
10731
10732 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010733 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010734
10735 return ret;
10736}
10737
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010738/*
10739 * Initialize the perf_event context in task_struct
10740 */
10741int perf_event_init_task(struct task_struct *child)
10742{
10743 int ctxn, ret;
10744
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010745 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10746 mutex_init(&child->perf_event_mutex);
10747 INIT_LIST_HEAD(&child->perf_event_list);
10748
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010749 for_each_task_context_nr(ctxn) {
10750 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010751 if (ret) {
10752 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010753 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010754 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010755 }
10756
10757 return 0;
10758}
10759
Paul Mackerras220b1402010-03-10 20:45:52 +110010760static void __init perf_event_init_all_cpus(void)
10761{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010762 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010763 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010764
10765 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010766 swhash = &per_cpu(swevent_htable, cpu);
10767 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010768 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070010769
10770 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
10771 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020010772
10773 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010774 }
10775}
10776
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010777int perf_event_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010778{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010779 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010780
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010781 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010782 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010783 struct swevent_hlist *hlist;
10784
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010785 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10786 WARN_ON(!hlist);
10787 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010788 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010789 mutex_unlock(&swhash->hlist_mutex);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010790 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010791}
10792
Dave Young2965faa2015-09-09 15:38:55 -070010793#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010794static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010795{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010796 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010797 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10798 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010799
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010800 raw_spin_lock(&ctx->lock);
10801 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010802 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010803 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010804}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010805
10806static void perf_event_exit_cpu_context(int cpu)
10807{
10808 struct perf_event_context *ctx;
10809 struct pmu *pmu;
10810 int idx;
10811
10812 idx = srcu_read_lock(&pmus_srcu);
10813 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010814 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010815
10816 mutex_lock(&ctx->mutex);
10817 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10818 mutex_unlock(&ctx->mutex);
10819 }
10820 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010821}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010822#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010823
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010824static void perf_event_exit_cpu_context(int cpu) { }
10825
10826#endif
10827
10828int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010829{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010830 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010831 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010832}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010833
Peter Zijlstrac2774432010-12-08 15:29:02 +010010834static int
10835perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10836{
10837 int cpu;
10838
10839 for_each_online_cpu(cpu)
10840 perf_event_exit_cpu(cpu);
10841
10842 return NOTIFY_OK;
10843}
10844
10845/*
10846 * Run the perf reboot notifier at the very last possible moment so that
10847 * the generic watchdog code runs as long as possible.
10848 */
10849static struct notifier_block perf_reboot_notifier = {
10850 .notifier_call = perf_reboot,
10851 .priority = INT_MIN,
10852};
10853
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010854void __init perf_event_init(void)
10855{
Jason Wessel3c502e72010-11-04 17:33:01 -050010856 int ret;
10857
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010858 idr_init(&pmu_idr);
10859
Paul Mackerras220b1402010-03-10 20:45:52 +110010860 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010861 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010862 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10863 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10864 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010865 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010866 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010010867 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010868
10869 ret = init_hw_breakpoint();
10870 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010871
Jiri Olsab01c3a02012-03-23 15:41:20 +010010872 /*
10873 * Build time assertion that we keep the data_head at the intended
10874 * location. IOW, validation we got the __reserved[] size right.
10875 */
10876 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10877 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010878}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010879
Cody P Schaferfd979c02015-01-30 13:45:57 -080010880ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10881 char *page)
10882{
10883 struct perf_pmu_events_attr *pmu_attr =
10884 container_of(attr, struct perf_pmu_events_attr, attr);
10885
10886 if (pmu_attr->event_str)
10887 return sprintf(page, "%s\n", pmu_attr->event_str);
10888
10889 return 0;
10890}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010891EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010892
Peter Zijlstraabe43402010-11-17 23:17:37 +010010893static int __init perf_event_sysfs_init(void)
10894{
10895 struct pmu *pmu;
10896 int ret;
10897
10898 mutex_lock(&pmus_lock);
10899
10900 ret = bus_register(&pmu_bus);
10901 if (ret)
10902 goto unlock;
10903
10904 list_for_each_entry(pmu, &pmus, entry) {
10905 if (!pmu->name || pmu->type < 0)
10906 continue;
10907
10908 ret = pmu_dev_alloc(pmu);
10909 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10910 }
10911 pmu_bus_running = 1;
10912 ret = 0;
10913
10914unlock:
10915 mutex_unlock(&pmus_lock);
10916
10917 return ret;
10918}
10919device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010920
10921#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010922static struct cgroup_subsys_state *
10923perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010924{
10925 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010926
Li Zefan1b15d052011-03-03 14:26:06 +080010927 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010928 if (!jc)
10929 return ERR_PTR(-ENOMEM);
10930
Stephane Eraniane5d13672011-02-14 11:20:01 +020010931 jc->info = alloc_percpu(struct perf_cgroup_info);
10932 if (!jc->info) {
10933 kfree(jc);
10934 return ERR_PTR(-ENOMEM);
10935 }
10936
Stephane Eraniane5d13672011-02-14 11:20:01 +020010937 return &jc->css;
10938}
10939
Tejun Heoeb954192013-08-08 20:11:23 -040010940static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010941{
Tejun Heoeb954192013-08-08 20:11:23 -040010942 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10943
Stephane Eraniane5d13672011-02-14 11:20:01 +020010944 free_percpu(jc->info);
10945 kfree(jc);
10946}
10947
10948static int __perf_cgroup_move(void *info)
10949{
10950 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010951 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010952 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010953 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010954 return 0;
10955}
10956
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010957static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010958{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010959 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010960 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010961
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010962 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010963 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010964}
10965
Tejun Heo073219e2014-02-08 10:36:58 -050010966struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010967 .css_alloc = perf_cgroup_css_alloc,
10968 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010969 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010970};
10971#endif /* CONFIG_CGROUP_PERF */