blob: 95bd00d9f2c3bec6d5a38f7a44e901e00b01f10c [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
392 */
Andy Lutomirski01610282016-05-09 15:48:51 -0700393int sysctl_perf_event_paranoid __read_mostly = 2;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200394
Frederic Weisbecker20443382011-03-31 03:33:29 +0200395/* Minimum for 512 kiB + 1 user control page */
396int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200397
398/*
399 * max perf event sample rate
400 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700401#define DEFAULT_MAX_SAMPLE_RATE 100000
402#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
403#define DEFAULT_CPU_TIME_MAX_PERCENT 25
404
405int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
406
407static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
408static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
409
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200410static int perf_sample_allowed_ns __read_mostly =
411 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700412
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800413static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700414{
415 u64 tmp = perf_sample_period_ns;
416
417 tmp *= sysctl_perf_cpu_time_max_percent;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100418 tmp = div_u64(tmp, 100);
419 if (!tmp)
420 tmp = 1;
421
422 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700423}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100424
Stephane Eranian9e630202013-04-03 14:21:33 +0200425static int perf_rotate_context(struct perf_cpu_context *cpuctx);
426
Peter Zijlstra163ec432011-02-16 11:22:34 +0100427int perf_proc_update_handler(struct ctl_table *table, int write,
428 void __user *buffer, size_t *lenp,
429 loff_t *ppos)
430{
Knut Petersen723478c2013-09-25 14:29:37 +0200431 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100432
433 if (ret || !write)
434 return ret;
435
Kan Liangab7fdef2016-05-03 00:26:06 -0700436 /*
437 * If throttling is disabled don't allow the write:
438 */
439 if (sysctl_perf_cpu_time_max_percent == 100 ||
440 sysctl_perf_cpu_time_max_percent == 0)
441 return -EINVAL;
442
Peter Zijlstra163ec432011-02-16 11:22:34 +0100443 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700444 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
445 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100446
447 return 0;
448}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200449
Dave Hansen14c63f12013-06-21 08:51:36 -0700450int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
451
452int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
453 void __user *buffer, size_t *lenp,
454 loff_t *ppos)
455{
Tan Xiaojun0f8a75e2017-02-23 14:04:39 +0800456 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Dave Hansen14c63f12013-06-21 08:51:36 -0700457
458 if (ret || !write)
459 return ret;
460
Peter Zijlstrab303e7c2016-04-04 09:57:40 +0200461 if (sysctl_perf_cpu_time_max_percent == 100 ||
462 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100463 printk(KERN_WARNING
464 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
465 WRITE_ONCE(perf_sample_allowed_ns, 0);
466 } else {
467 update_perf_cpu_limits();
468 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700469
470 return 0;
471}
472
473/*
474 * perf samples are done in some very critical code paths (NMIs).
475 * If they take too much CPU time, the system can lock up and not
476 * get any real work done. This will drop the sample rate when
477 * we detect that events are taking too long.
478 */
479#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200480static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700481
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100482static u64 __report_avg;
483static u64 __report_allowed;
484
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100485static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700486{
David Ahern0d87d7e2016-08-01 13:49:29 -0700487 printk_ratelimited(KERN_INFO
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100488 "perf: interrupt took too long (%lld > %lld), lowering "
489 "kernel.perf_event_max_sample_rate to %d\n",
490 __report_avg, __report_allowed,
491 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100492}
493
494static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
495
496void perf_sample_event_took(u64 sample_len_ns)
497{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100498 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
499 u64 running_len;
500 u64 avg_len;
501 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700502
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100503 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700504 return;
505
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100506 /* Decay the counter by 1 average sample. */
507 running_len = __this_cpu_read(running_sample_length);
508 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
509 running_len += sample_len_ns;
510 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700511
512 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100513 * Note: this will be biased artifically low until we have
514 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700515 * from having to maintain a count.
516 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100517 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
518 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700519 return;
520
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100521 __report_avg = avg_len;
522 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700523
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100524 /*
525 * Compute a throttle threshold 25% below the current duration.
526 */
527 avg_len += avg_len / 4;
528 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
529 if (avg_len < max)
530 max /= (u32)avg_len;
531 else
532 max = 1;
533
534 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
535 WRITE_ONCE(max_samples_per_tick, max);
536
537 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700538 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
539
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100540 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100541 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100542 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100543 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100544 sysctl_perf_event_sample_rate);
545 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700546}
547
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200548static atomic64_t perf_event_id;
549
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200550static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
551 enum event_type_t event_type);
552
553static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200554 enum event_type_t event_type,
555 struct task_struct *task);
556
557static void update_context_time(struct perf_event_context *ctx);
558static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200559
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200560void __weak perf_event_print_debug(void) { }
561
Matt Fleming84c79912010-10-03 21:41:13 +0100562extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200563{
Matt Fleming84c79912010-10-03 21:41:13 +0100564 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200565}
566
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200567static inline u64 perf_clock(void)
568{
569 return local_clock();
570}
571
Peter Zijlstra34f43922015-02-20 14:05:38 +0100572static inline u64 perf_event_clock(struct perf_event *event)
573{
574 return event->clock();
575}
576
Stephane Eraniane5d13672011-02-14 11:20:01 +0200577#ifdef CONFIG_CGROUP_PERF
578
Stephane Eraniane5d13672011-02-14 11:20:01 +0200579static inline bool
580perf_cgroup_match(struct perf_event *event)
581{
582 struct perf_event_context *ctx = event->ctx;
583 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
584
Tejun Heoef824fa2013-04-08 19:00:38 -0700585 /* @event doesn't care about cgroup */
586 if (!event->cgrp)
587 return true;
588
589 /* wants specific cgroup scope but @cpuctx isn't associated with any */
590 if (!cpuctx->cgrp)
591 return false;
592
593 /*
594 * Cgroup scoping is recursive. An event enabled for a cgroup is
595 * also enabled for all its descendant cgroups. If @cpuctx's
596 * cgroup is a descendant of @event's (the test covers identity
597 * case), it's a match.
598 */
599 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
600 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200601}
602
Stephane Eraniane5d13672011-02-14 11:20:01 +0200603static inline void perf_detach_cgroup(struct perf_event *event)
604{
Zefan Li4e2ba652014-09-19 16:53:14 +0800605 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200606 event->cgrp = NULL;
607}
608
609static inline int is_cgroup_event(struct perf_event *event)
610{
611 return event->cgrp != NULL;
612}
613
614static inline u64 perf_cgroup_event_time(struct perf_event *event)
615{
616 struct perf_cgroup_info *t;
617
618 t = per_cpu_ptr(event->cgrp->info, event->cpu);
619 return t->time;
620}
621
622static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
623{
624 struct perf_cgroup_info *info;
625 u64 now;
626
627 now = perf_clock();
628
629 info = this_cpu_ptr(cgrp->info);
630
631 info->time += now - info->timestamp;
632 info->timestamp = now;
633}
634
635static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
636{
Song Liud4271d82018-03-12 09:59:43 -0700637 struct perf_cgroup *cgrp = cpuctx->cgrp;
638 struct cgroup_subsys_state *css;
639
640 if (cgrp) {
641 for (css = &cgrp->css; css; css = css->parent) {
642 cgrp = container_of(css, struct perf_cgroup, css);
643 __update_cgrp_time(cgrp);
644 }
645 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200646}
647
648static inline void update_cgrp_time_from_event(struct perf_event *event)
649{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200650 struct perf_cgroup *cgrp;
651
Stephane Eraniane5d13672011-02-14 11:20:01 +0200652 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200653 * ensure we access cgroup data only when needed and
654 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200655 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200656 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200657 return;
658
Stephane Eranian614e4c42015-11-12 11:00:04 +0100659 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200660 /*
661 * Do not update time when cgroup is not active
662 */
663 if (cgrp == event->cgrp)
664 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200665}
666
667static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200668perf_cgroup_set_timestamp(struct task_struct *task,
669 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200670{
671 struct perf_cgroup *cgrp;
672 struct perf_cgroup_info *info;
Song Liud4271d82018-03-12 09:59:43 -0700673 struct cgroup_subsys_state *css;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200674
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200675 /*
676 * ctx->lock held by caller
677 * ensure we do not access cgroup data
678 * unless we have the cgroup pinned (css_get)
679 */
680 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200681 return;
682
Stephane Eranian614e4c42015-11-12 11:00:04 +0100683 cgrp = perf_cgroup_from_task(task, ctx);
Song Liud4271d82018-03-12 09:59:43 -0700684
685 for (css = &cgrp->css; css; css = css->parent) {
686 cgrp = container_of(css, struct perf_cgroup, css);
687 info = this_cpu_ptr(cgrp->info);
688 info->timestamp = ctx->timestamp;
689 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200690}
691
692#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
693#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
694
695/*
696 * reschedule events based on the cgroup constraint of task.
697 *
698 * mode SWOUT : schedule out everything
699 * mode SWIN : schedule in based on cgroup for next
700 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800701static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200702{
703 struct perf_cpu_context *cpuctx;
704 struct pmu *pmu;
705 unsigned long flags;
706
707 /*
708 * disable interrupts to avoid geting nr_cgroup
709 * changes via __perf_event_disable(). Also
710 * avoids preemption.
711 */
712 local_irq_save(flags);
713
714 /*
715 * we reschedule only in the presence of cgroup
716 * constrained events.
717 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200718
719 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200720 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200721 if (cpuctx->unique_pmu != pmu)
722 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200723
Stephane Eraniane5d13672011-02-14 11:20:01 +0200724 /*
725 * perf_cgroup_events says at least one
726 * context on this CPU has cgroup events.
727 *
728 * ctx->nr_cgroups reports the number of cgroup
729 * events for a context.
730 */
731 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200732 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
733 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200734
735 if (mode & PERF_CGROUP_SWOUT) {
736 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
737 /*
738 * must not be done before ctxswout due
739 * to event_filter_match() in event_sched_out()
740 */
741 cpuctx->cgrp = NULL;
742 }
743
744 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200745 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200746 /*
747 * set cgrp before ctxsw in to allow
748 * event_filter_match() to not have to pass
749 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100750 * we pass the cpuctx->ctx to perf_cgroup_from_task()
751 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200752 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100753 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200754 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
755 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200756 perf_pmu_enable(cpuctx->ctx.pmu);
757 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200758 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200759 }
760
Stephane Eraniane5d13672011-02-14 11:20:01 +0200761 local_irq_restore(flags);
762}
763
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200764static inline void perf_cgroup_sched_out(struct task_struct *task,
765 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200766{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200767 struct perf_cgroup *cgrp1;
768 struct perf_cgroup *cgrp2 = NULL;
769
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100770 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200771 /*
772 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100773 * we do not need to pass the ctx here because we know
774 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200775 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100776 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100777 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200778
779 /*
780 * only schedule out current cgroup events if we know
781 * that we are switching to a different cgroup. Otherwise,
782 * do no touch the cgroup events.
783 */
784 if (cgrp1 != cgrp2)
785 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100786
787 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200788}
789
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200790static inline void perf_cgroup_sched_in(struct task_struct *prev,
791 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200792{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200793 struct perf_cgroup *cgrp1;
794 struct perf_cgroup *cgrp2 = NULL;
795
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100796 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200797 /*
798 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100799 * we do not need to pass the ctx here because we know
800 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200801 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100802 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100803 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200804
805 /*
806 * only need to schedule in cgroup events if we are changing
807 * cgroup during ctxsw. Cgroup events were not scheduled
808 * out of ctxsw out if that was not the case.
809 */
810 if (cgrp1 != cgrp2)
811 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100812
813 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200814}
815
816static inline int perf_cgroup_connect(int fd, struct perf_event *event,
817 struct perf_event_attr *attr,
818 struct perf_event *group_leader)
819{
820 struct perf_cgroup *cgrp;
821 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400822 struct fd f = fdget(fd);
823 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200824
Al Viro2903ff02012-08-28 12:52:22 -0400825 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200826 return -EBADF;
827
Al Virob5830432014-10-31 01:22:04 -0400828 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400829 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800830 if (IS_ERR(css)) {
831 ret = PTR_ERR(css);
832 goto out;
833 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200834
835 cgrp = container_of(css, struct perf_cgroup, css);
836 event->cgrp = cgrp;
837
838 /*
839 * all events in a group must monitor
840 * the same cgroup because a task belongs
841 * to only one perf cgroup at a time
842 */
843 if (group_leader && group_leader->cgrp != cgrp) {
844 perf_detach_cgroup(event);
845 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200846 }
Li Zefan3db272c2011-03-03 14:25:37 +0800847out:
Al Viro2903ff02012-08-28 12:52:22 -0400848 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200849 return ret;
850}
851
852static inline void
853perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
854{
855 struct perf_cgroup_info *t;
856 t = per_cpu_ptr(event->cgrp->info, event->cpu);
857 event->shadow_ctx_time = now - t->timestamp;
858}
859
860static inline void
861perf_cgroup_defer_enabled(struct perf_event *event)
862{
863 /*
864 * when the current task's perf cgroup does not match
865 * the event's, we need to remember to call the
866 * perf_mark_enable() function the first time a task with
867 * a matching perf cgroup is scheduled in.
868 */
869 if (is_cgroup_event(event) && !perf_cgroup_match(event))
870 event->cgrp_defer_enabled = 1;
871}
872
873static inline void
874perf_cgroup_mark_enabled(struct perf_event *event,
875 struct perf_event_context *ctx)
876{
877 struct perf_event *sub;
878 u64 tstamp = perf_event_time(event);
879
880 if (!event->cgrp_defer_enabled)
881 return;
882
883 event->cgrp_defer_enabled = 0;
884
885 event->tstamp_enabled = tstamp - event->total_time_enabled;
886 list_for_each_entry(sub, &event->sibling_list, group_entry) {
887 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
888 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
889 sub->cgrp_defer_enabled = 0;
890 }
891 }
892}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700893
894/*
895 * Update cpuctx->cgrp so that it is set when first cgroup event is added and
896 * cleared when last cgroup event is removed.
897 */
898static inline void
899list_update_cgroup_event(struct perf_event *event,
900 struct perf_event_context *ctx, bool add)
901{
902 struct perf_cpu_context *cpuctx;
903
904 if (!is_cgroup_event(event))
905 return;
906
907 if (add && ctx->nr_cgroups++)
908 return;
909 else if (!add && --ctx->nr_cgroups)
910 return;
911 /*
912 * Because cgroup events are always per-cpu events,
913 * this will always be called from the right CPU.
914 */
915 cpuctx = __get_cpu_context(ctx);
David Carrillo-Cisneros864c2352016-11-01 11:52:58 -0700916
David Carrillo-Cisneros8fc31ce2016-12-04 00:46:17 -0800917 /*
918 * cpuctx->cgrp is NULL until a cgroup event is sched in or
919 * ctx->nr_cgroup == 0 .
920 */
921 if (add && perf_cgroup_from_task(current, ctx) == event->cgrp)
922 cpuctx->cgrp = event->cgrp;
923 else if (!add)
924 cpuctx->cgrp = NULL;
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700925}
926
Stephane Eraniane5d13672011-02-14 11:20:01 +0200927#else /* !CONFIG_CGROUP_PERF */
928
929static inline bool
930perf_cgroup_match(struct perf_event *event)
931{
932 return true;
933}
934
935static inline void perf_detach_cgroup(struct perf_event *event)
936{}
937
938static inline int is_cgroup_event(struct perf_event *event)
939{
940 return 0;
941}
942
943static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
944{
945 return 0;
946}
947
948static inline void update_cgrp_time_from_event(struct perf_event *event)
949{
950}
951
952static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
953{
954}
955
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200956static inline void perf_cgroup_sched_out(struct task_struct *task,
957 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200958{
959}
960
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200961static inline void perf_cgroup_sched_in(struct task_struct *prev,
962 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200963{
964}
965
966static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
967 struct perf_event_attr *attr,
968 struct perf_event *group_leader)
969{
970 return -EINVAL;
971}
972
973static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200974perf_cgroup_set_timestamp(struct task_struct *task,
975 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200976{
977}
978
979void
980perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
981{
982}
983
984static inline void
985perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
986{
987}
988
989static inline u64 perf_cgroup_event_time(struct perf_event *event)
990{
991 return 0;
992}
993
994static inline void
995perf_cgroup_defer_enabled(struct perf_event *event)
996{
997}
998
999static inline void
1000perf_cgroup_mark_enabled(struct perf_event *event,
1001 struct perf_event_context *ctx)
1002{
1003}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001004
1005static inline void
1006list_update_cgroup_event(struct perf_event *event,
1007 struct perf_event_context *ctx, bool add)
1008{
1009}
1010
Stephane Eraniane5d13672011-02-14 11:20:01 +02001011#endif
1012
Stephane Eranian9e630202013-04-03 14:21:33 +02001013/*
1014 * set default to be dependent on timer tick just
1015 * like original code
1016 */
1017#define PERF_CPU_HRTIMER (1000 / HZ)
1018/*
1019 * function must be called with interrupts disbled
1020 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001021static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +02001022{
1023 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +02001024 int rotations = 0;
1025
1026 WARN_ON(!irqs_disabled());
1027
1028 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +02001029 rotations = perf_rotate_context(cpuctx);
1030
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001031 raw_spin_lock(&cpuctx->hrtimer_lock);
1032 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +02001033 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001034 else
1035 cpuctx->hrtimer_active = 0;
1036 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +02001037
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001038 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +02001039}
1040
Peter Zijlstra272325c2015-04-15 11:41:58 +02001041static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +02001042{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001043 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001044 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +02001045 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +02001046
1047 /* no multiplexing needed for SW PMU */
1048 if (pmu->task_ctx_nr == perf_sw_context)
1049 return;
1050
Stephane Eranian62b85632013-04-03 14:21:34 +02001051 /*
1052 * check default is sane, if not set then force to
1053 * default interval (1/tick)
1054 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001055 interval = pmu->hrtimer_interval_ms;
1056 if (interval < 1)
1057 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001058
Peter Zijlstra272325c2015-04-15 11:41:58 +02001059 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001060
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001061 raw_spin_lock_init(&cpuctx->hrtimer_lock);
1062 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001063 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001064}
1065
Peter Zijlstra272325c2015-04-15 11:41:58 +02001066static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001067{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001068 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001069 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001070 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001071
1072 /* not for SW PMU */
1073 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001074 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001075
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001076 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1077 if (!cpuctx->hrtimer_active) {
1078 cpuctx->hrtimer_active = 1;
1079 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
1080 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
1081 }
1082 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001083
Peter Zijlstra272325c2015-04-15 11:41:58 +02001084 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001085}
1086
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001087void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001088{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001089 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1090 if (!(*count)++)
1091 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001092}
1093
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001094void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001095{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001096 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1097 if (!--(*count))
1098 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001099}
1100
Mark Rutland2fde4f92015-01-07 15:01:54 +00001101static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001102
1103/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001104 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1105 * perf_event_task_tick() are fully serialized because they're strictly cpu
1106 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1107 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001108 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001109static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001110{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001111 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001112
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001113 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001114
Mark Rutland2fde4f92015-01-07 15:01:54 +00001115 WARN_ON(!list_empty(&ctx->active_ctx_list));
1116
1117 list_add(&ctx->active_ctx_list, head);
1118}
1119
1120static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1121{
1122 WARN_ON(!irqs_disabled());
1123
1124 WARN_ON(list_empty(&ctx->active_ctx_list));
1125
1126 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001127}
1128
1129static void get_ctx(struct perf_event_context *ctx)
1130{
1131 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1132}
1133
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001134static void free_ctx(struct rcu_head *head)
1135{
1136 struct perf_event_context *ctx;
1137
1138 ctx = container_of(head, struct perf_event_context, rcu_head);
1139 kfree(ctx->task_ctx_data);
1140 kfree(ctx);
1141}
1142
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143static void put_ctx(struct perf_event_context *ctx)
1144{
1145 if (atomic_dec_and_test(&ctx->refcount)) {
1146 if (ctx->parent_ctx)
1147 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001148 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001150 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001151 }
1152}
1153
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001154/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001155 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1156 * perf_pmu_migrate_context() we need some magic.
1157 *
1158 * Those places that change perf_event::ctx will hold both
1159 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1160 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001161 * Lock ordering is by mutex address. There are two other sites where
1162 * perf_event_context::mutex nests and those are:
1163 *
1164 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001165 * perf_event_exit_event()
1166 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001167 *
1168 * - perf_event_init_context() [ parent, 0 ]
1169 * inherit_task_group()
1170 * inherit_group()
1171 * inherit_event()
1172 * perf_event_alloc()
1173 * perf_init_event()
1174 * perf_try_init_event() [ child , 1 ]
1175 *
1176 * While it appears there is an obvious deadlock here -- the parent and child
1177 * nesting levels are inverted between the two. This is in fact safe because
1178 * life-time rules separate them. That is an exiting task cannot fork, and a
1179 * spawning task cannot (yet) exit.
1180 *
1181 * But remember that that these are parent<->child context relations, and
1182 * migration does not affect children, therefore these two orderings should not
1183 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001184 *
1185 * The change in perf_event::ctx does not affect children (as claimed above)
1186 * because the sys_perf_event_open() case will install a new event and break
1187 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1188 * concerned with cpuctx and that doesn't have children.
1189 *
1190 * The places that change perf_event::ctx will issue:
1191 *
1192 * perf_remove_from_context();
1193 * synchronize_rcu();
1194 * perf_install_in_context();
1195 *
1196 * to affect the change. The remove_from_context() + synchronize_rcu() should
1197 * quiesce the event, after which we can install it in the new location. This
1198 * means that only external vectors (perf_fops, prctl) can perturb the event
1199 * while in transit. Therefore all such accessors should also acquire
1200 * perf_event_context::mutex to serialize against this.
1201 *
1202 * However; because event->ctx can change while we're waiting to acquire
1203 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1204 * function.
1205 *
1206 * Lock order:
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02001207 * cred_guard_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001208 * task_struct::perf_event_mutex
1209 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001210 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001211 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001212 * perf_event::mmap_mutex
1213 * mmap_sem
1214 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001215static struct perf_event_context *
1216perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001217{
1218 struct perf_event_context *ctx;
1219
1220again:
1221 rcu_read_lock();
1222 ctx = ACCESS_ONCE(event->ctx);
1223 if (!atomic_inc_not_zero(&ctx->refcount)) {
1224 rcu_read_unlock();
1225 goto again;
1226 }
1227 rcu_read_unlock();
1228
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001229 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001230 if (event->ctx != ctx) {
1231 mutex_unlock(&ctx->mutex);
1232 put_ctx(ctx);
1233 goto again;
1234 }
1235
1236 return ctx;
1237}
1238
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001239static inline struct perf_event_context *
1240perf_event_ctx_lock(struct perf_event *event)
1241{
1242 return perf_event_ctx_lock_nested(event, 0);
1243}
1244
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001245static void perf_event_ctx_unlock(struct perf_event *event,
1246 struct perf_event_context *ctx)
1247{
1248 mutex_unlock(&ctx->mutex);
1249 put_ctx(ctx);
1250}
1251
1252/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001253 * This must be done under the ctx->lock, such as to serialize against
1254 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1255 * calling scheduler related locks and ctx->lock nests inside those.
1256 */
1257static __must_check struct perf_event_context *
1258unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001259{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001260 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1261
1262 lockdep_assert_held(&ctx->lock);
1263
1264 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001265 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001266 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001267
1268 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001269}
1270
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001271static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1272{
1273 /*
1274 * only top level events have the pid namespace they were created in
1275 */
1276 if (event->parent)
1277 event = event->parent;
1278
1279 return task_tgid_nr_ns(p, event->ns);
1280}
1281
1282static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1283{
1284 /*
1285 * only top level events have the pid namespace they were created in
1286 */
1287 if (event->parent)
1288 event = event->parent;
1289
1290 return task_pid_nr_ns(p, event->ns);
1291}
1292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001293/*
1294 * If we inherit events we want to return the parent event id
1295 * to userspace.
1296 */
1297static u64 primary_event_id(struct perf_event *event)
1298{
1299 u64 id = event->id;
1300
1301 if (event->parent)
1302 id = event->parent->id;
1303
1304 return id;
1305}
1306
1307/*
1308 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001309 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001310 * This has to cope with with the fact that until it is locked,
1311 * the context could get moved to another task.
1312 */
1313static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001314perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001315{
1316 struct perf_event_context *ctx;
1317
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001318retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001319 /*
1320 * One of the few rules of preemptible RCU is that one cannot do
1321 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001322 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001323 * rcu_read_unlock_special().
1324 *
1325 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001326 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001327 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001328 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001329 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001330 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001331 if (ctx) {
1332 /*
1333 * If this context is a clone of another, it might
1334 * get swapped for another underneath us by
1335 * perf_event_task_sched_out, though the
1336 * rcu_read_lock() protects us from any context
1337 * getting freed. Lock the context and check if it
1338 * got swapped before we could get the lock, and retry
1339 * if so. If we locked the right context, then it
1340 * can't get swapped on us any more.
1341 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001342 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001343 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001344 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001345 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001346 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001347 goto retry;
1348 }
1349
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001350 if (ctx->task == TASK_TOMBSTONE ||
1351 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001352 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001353 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001354 } else {
1355 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001356 }
1357 }
1358 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001359 if (!ctx)
1360 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361 return ctx;
1362}
1363
1364/*
1365 * Get the context for a task and increment its pin_count so it
1366 * can't get swapped to another task. This also increments its
1367 * reference count so that the context can't get freed.
1368 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001369static struct perf_event_context *
1370perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371{
1372 struct perf_event_context *ctx;
1373 unsigned long flags;
1374
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001375 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376 if (ctx) {
1377 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001378 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001379 }
1380 return ctx;
1381}
1382
1383static void perf_unpin_context(struct perf_event_context *ctx)
1384{
1385 unsigned long flags;
1386
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001387 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001388 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001389 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001390}
1391
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001392/*
1393 * Update the record of the current time in a context.
1394 */
1395static void update_context_time(struct perf_event_context *ctx)
1396{
1397 u64 now = perf_clock();
1398
1399 ctx->time += now - ctx->timestamp;
1400 ctx->timestamp = now;
1401}
1402
Stephane Eranian41587552011-01-03 18:20:01 +02001403static u64 perf_event_time(struct perf_event *event)
1404{
1405 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001406
1407 if (is_cgroup_event(event))
1408 return perf_cgroup_event_time(event);
1409
Stephane Eranian41587552011-01-03 18:20:01 +02001410 return ctx ? ctx->time : 0;
1411}
1412
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001413/*
1414 * Update the total_time_enabled and total_time_running fields for a event.
1415 */
1416static void update_event_times(struct perf_event *event)
1417{
1418 struct perf_event_context *ctx = event->ctx;
1419 u64 run_end;
1420
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001421 lockdep_assert_held(&ctx->lock);
1422
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001423 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1424 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1425 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001426
Stephane Eraniane5d13672011-02-14 11:20:01 +02001427 /*
1428 * in cgroup mode, time_enabled represents
1429 * the time the event was enabled AND active
1430 * tasks were in the monitored cgroup. This is
1431 * independent of the activity of the context as
1432 * there may be a mix of cgroup and non-cgroup events.
1433 *
1434 * That is why we treat cgroup events differently
1435 * here.
1436 */
1437 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001438 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001439 else if (ctx->is_active)
1440 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001441 else
1442 run_end = event->tstamp_stopped;
1443
1444 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001445
1446 if (event->state == PERF_EVENT_STATE_INACTIVE)
1447 run_end = event->tstamp_stopped;
1448 else
Stephane Eranian41587552011-01-03 18:20:01 +02001449 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001450
1451 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001452
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001453}
1454
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001455/*
1456 * Update total_time_enabled and total_time_running for all events in a group.
1457 */
1458static void update_group_times(struct perf_event *leader)
1459{
1460 struct perf_event *event;
1461
1462 update_event_times(leader);
1463 list_for_each_entry(event, &leader->sibling_list, group_entry)
1464 update_event_times(event);
1465}
1466
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001467static struct list_head *
1468ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1469{
1470 if (event->attr.pinned)
1471 return &ctx->pinned_groups;
1472 else
1473 return &ctx->flexible_groups;
1474}
1475
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001476/*
1477 * Add a event from the lists for its context.
1478 * Must be called with ctx->mutex and ctx->lock held.
1479 */
1480static void
1481list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1482{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001483 lockdep_assert_held(&ctx->lock);
1484
Peter Zijlstra8a495422010-05-27 15:47:49 +02001485 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1486 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487
1488 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001489 * If we're a stand alone event or group leader, we go to the context
1490 * list, group events are kept attached to the group so that
1491 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001493 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001494 struct list_head *list;
1495
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001496 event->group_caps = event->event_caps;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001497
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001498 list = ctx_group_list(event, ctx);
1499 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001500 }
1501
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001502 list_update_cgroup_event(event, ctx, true);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001503
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001504 list_add_rcu(&event->event_entry, &ctx->event_list);
1505 ctx->nr_events++;
1506 if (event->attr.inherit_stat)
1507 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001508
1509 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001510}
1511
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001512/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001513 * Initialize event state based on the perf_event_attr::disabled.
1514 */
1515static inline void perf_event__state_init(struct perf_event *event)
1516{
1517 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1518 PERF_EVENT_STATE_INACTIVE;
1519}
1520
Peter Zijlstraa7239682015-09-09 19:06:33 +02001521static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001522{
1523 int entry = sizeof(u64); /* value */
1524 int size = 0;
1525 int nr = 1;
1526
1527 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1528 size += sizeof(u64);
1529
1530 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1531 size += sizeof(u64);
1532
1533 if (event->attr.read_format & PERF_FORMAT_ID)
1534 entry += sizeof(u64);
1535
1536 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001537 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001538 size += sizeof(u64);
1539 }
1540
1541 size += entry * nr;
1542 event->read_size = size;
1543}
1544
Peter Zijlstraa7239682015-09-09 19:06:33 +02001545static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001546{
1547 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001548 u16 size = 0;
1549
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001550 if (sample_type & PERF_SAMPLE_IP)
1551 size += sizeof(data->ip);
1552
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001553 if (sample_type & PERF_SAMPLE_ADDR)
1554 size += sizeof(data->addr);
1555
1556 if (sample_type & PERF_SAMPLE_PERIOD)
1557 size += sizeof(data->period);
1558
Andi Kleenc3feedf2013-01-24 16:10:28 +01001559 if (sample_type & PERF_SAMPLE_WEIGHT)
1560 size += sizeof(data->weight);
1561
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001562 if (sample_type & PERF_SAMPLE_READ)
1563 size += event->read_size;
1564
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001565 if (sample_type & PERF_SAMPLE_DATA_SRC)
1566 size += sizeof(data->data_src.val);
1567
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001568 if (sample_type & PERF_SAMPLE_TRANSACTION)
1569 size += sizeof(data->txn);
1570
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001571 event->header_size = size;
1572}
1573
Peter Zijlstraa7239682015-09-09 19:06:33 +02001574/*
1575 * Called at perf_event creation and when events are attached/detached from a
1576 * group.
1577 */
1578static void perf_event__header_size(struct perf_event *event)
1579{
1580 __perf_event_read_size(event,
1581 event->group_leader->nr_siblings);
1582 __perf_event_header_size(event, event->attr.sample_type);
1583}
1584
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001585static void perf_event__id_header_size(struct perf_event *event)
1586{
1587 struct perf_sample_data *data;
1588 u64 sample_type = event->attr.sample_type;
1589 u16 size = 0;
1590
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001591 if (sample_type & PERF_SAMPLE_TID)
1592 size += sizeof(data->tid_entry);
1593
1594 if (sample_type & PERF_SAMPLE_TIME)
1595 size += sizeof(data->time);
1596
Adrian Hunterff3d5272013-08-27 11:23:07 +03001597 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1598 size += sizeof(data->id);
1599
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001600 if (sample_type & PERF_SAMPLE_ID)
1601 size += sizeof(data->id);
1602
1603 if (sample_type & PERF_SAMPLE_STREAM_ID)
1604 size += sizeof(data->stream_id);
1605
1606 if (sample_type & PERF_SAMPLE_CPU)
1607 size += sizeof(data->cpu_entry);
1608
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001609 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001610}
1611
Peter Zijlstraa7239682015-09-09 19:06:33 +02001612static bool perf_event_validate_size(struct perf_event *event)
1613{
1614 /*
1615 * The values computed here will be over-written when we actually
1616 * attach the event.
1617 */
1618 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1619 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1620 perf_event__id_header_size(event);
1621
1622 /*
1623 * Sum the lot; should not exceed the 64k limit we have on records.
1624 * Conservative limit to allow for callchains and other variable fields.
1625 */
1626 if (event->read_size + event->header_size +
1627 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1628 return false;
1629
1630 return true;
1631}
1632
Peter Zijlstra8a495422010-05-27 15:47:49 +02001633static void perf_group_attach(struct perf_event *event)
1634{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001635 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001636
Peter Zijlstra3996a912017-01-26 16:39:55 +01001637 lockdep_assert_held(&event->ctx->lock);
1638
Peter Zijlstra74c33372010-10-15 11:40:29 +02001639 /*
1640 * We can have double attach due to group movement in perf_event_open.
1641 */
1642 if (event->attach_state & PERF_ATTACH_GROUP)
1643 return;
1644
Peter Zijlstra8a495422010-05-27 15:47:49 +02001645 event->attach_state |= PERF_ATTACH_GROUP;
1646
1647 if (group_leader == event)
1648 return;
1649
Peter Zijlstra652884f2015-01-23 11:20:10 +01001650 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1651
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001652 group_leader->group_caps &= event->event_caps;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001653
1654 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1655 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001656
1657 perf_event__header_size(group_leader);
1658
1659 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1660 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001661}
1662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663/*
1664 * Remove a event from the lists for its context.
1665 * Must be called with ctx->mutex and ctx->lock held.
1666 */
1667static void
1668list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1669{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001670 WARN_ON_ONCE(event->ctx != ctx);
1671 lockdep_assert_held(&ctx->lock);
1672
Peter Zijlstra8a495422010-05-27 15:47:49 +02001673 /*
1674 * We can have double detach due to exit/hot-unplug + close.
1675 */
1676 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001677 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001678
1679 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1680
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001681 list_update_cgroup_event(event, ctx, false);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001682
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001683 ctx->nr_events--;
1684 if (event->attr.inherit_stat)
1685 ctx->nr_stat--;
1686
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001687 list_del_rcu(&event->event_entry);
1688
Peter Zijlstra8a495422010-05-27 15:47:49 +02001689 if (event->group_leader == event)
1690 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001691
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001692 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001693
1694 /*
1695 * If event was in error state, then keep it
1696 * that way, otherwise bogus counts will be
1697 * returned on read(). The only way to get out
1698 * of error state is by explicit re-enabling
1699 * of the event
1700 */
1701 if (event->state > PERF_EVENT_STATE_OFF)
1702 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001703
1704 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001705}
1706
Peter Zijlstra8a495422010-05-27 15:47:49 +02001707static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001708{
1709 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001710 struct list_head *list = NULL;
1711
Peter Zijlstra3996a912017-01-26 16:39:55 +01001712 lockdep_assert_held(&event->ctx->lock);
1713
Peter Zijlstra8a495422010-05-27 15:47:49 +02001714 /*
1715 * We can have double detach due to exit/hot-unplug + close.
1716 */
1717 if (!(event->attach_state & PERF_ATTACH_GROUP))
1718 return;
1719
1720 event->attach_state &= ~PERF_ATTACH_GROUP;
1721
1722 /*
1723 * If this is a sibling, remove it from its group.
1724 */
1725 if (event->group_leader != event) {
1726 list_del_init(&event->group_entry);
1727 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001728 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001729 }
1730
1731 if (!list_empty(&event->group_entry))
1732 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001733
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001734 /*
1735 * If this was a group event with sibling events then
1736 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001737 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738 */
1739 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001740 if (list)
1741 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001743
1744 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001745 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001746
1747 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001748 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001749
1750out:
1751 perf_event__header_size(event->group_leader);
1752
1753 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1754 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001755}
1756
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001757static bool is_orphaned_event(struct perf_event *event)
1758{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001759 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001760}
1761
Mark Rutland2c81a642016-06-14 16:10:41 +01001762static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01001763{
1764 struct pmu *pmu = event->pmu;
1765 return pmu->filter_match ? pmu->filter_match(event) : 1;
1766}
1767
Mark Rutland2c81a642016-06-14 16:10:41 +01001768/*
1769 * Check whether we should attempt to schedule an event group based on
1770 * PMU-specific filtering. An event group can consist of HW and SW events,
1771 * potentially with a SW leader, so we must check all the filters, to
1772 * determine whether a group is schedulable:
1773 */
1774static inline int pmu_filter_match(struct perf_event *event)
1775{
1776 struct perf_event *child;
1777
1778 if (!__pmu_filter_match(event))
1779 return 0;
1780
1781 list_for_each_entry(child, &event->sibling_list, group_entry) {
1782 if (!__pmu_filter_match(child))
1783 return 0;
1784 }
1785
1786 return 1;
1787}
1788
Stephane Eranianfa66f072010-08-26 16:40:01 +02001789static inline int
1790event_filter_match(struct perf_event *event)
1791{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001792 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
1793 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001794}
1795
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001796static void
1797event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001798 struct perf_cpu_context *cpuctx,
1799 struct perf_event_context *ctx)
1800{
Stephane Eranian41587552011-01-03 18:20:01 +02001801 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001802 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001803
1804 WARN_ON_ONCE(event->ctx != ctx);
1805 lockdep_assert_held(&ctx->lock);
1806
Stephane Eranianfa66f072010-08-26 16:40:01 +02001807 /*
1808 * An event which could not be activated because of
1809 * filter mismatch still needs to have its timings
1810 * maintained, otherwise bogus information is return
1811 * via read() for time_enabled, time_running:
1812 */
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001813 if (event->state == PERF_EVENT_STATE_INACTIVE &&
1814 !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001815 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001816 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001817 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001818 }
1819
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001820 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001821 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001822
Alexander Shishkin44377272013-12-16 14:17:36 +02001823 perf_pmu_disable(event->pmu);
1824
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001825 event->tstamp_stopped = tstamp;
1826 event->pmu->del(event, 0);
1827 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001828 event->state = PERF_EVENT_STATE_INACTIVE;
1829 if (event->pending_disable) {
1830 event->pending_disable = 0;
1831 event->state = PERF_EVENT_STATE_OFF;
1832 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001833
1834 if (!is_software_event(event))
1835 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001836 if (!--ctx->nr_active)
1837 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001838 if (event->attr.freq && event->attr.sample_freq)
1839 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001840 if (event->attr.exclusive || !cpuctx->active_oncpu)
1841 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001842
1843 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001844}
1845
1846static void
1847group_sched_out(struct perf_event *group_event,
1848 struct perf_cpu_context *cpuctx,
1849 struct perf_event_context *ctx)
1850{
1851 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001852 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001853
Mark Rutland3f005e72016-07-26 18:12:21 +01001854 perf_pmu_disable(ctx->pmu);
1855
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001856 event_sched_out(group_event, cpuctx, ctx);
1857
1858 /*
1859 * Schedule out siblings (if any):
1860 */
1861 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1862 event_sched_out(event, cpuctx, ctx);
1863
Mark Rutland3f005e72016-07-26 18:12:21 +01001864 perf_pmu_enable(ctx->pmu);
1865
Stephane Eranianfa66f072010-08-26 16:40:01 +02001866 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001867 cpuctx->exclusive = 0;
1868}
1869
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001870#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001871
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001872/*
1873 * Cross CPU call to remove a performance event
1874 *
1875 * We disable the event on the hardware level first. After that we
1876 * remove it from the context list.
1877 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001878static void
1879__perf_remove_from_context(struct perf_event *event,
1880 struct perf_cpu_context *cpuctx,
1881 struct perf_event_context *ctx,
1882 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001883{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001884 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001885
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001886 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001887 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001888 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001890
Peter Zijlstra39a43642016-01-11 12:46:35 +01001891 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001892 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001893 if (ctx->task) {
1894 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1895 cpuctx->task_ctx = NULL;
1896 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001897 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001898}
1899
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900/*
1901 * Remove the event from a task's (or a CPU's) list of events.
1902 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001903 * If event->ctx is a cloned context, callers must make sure that
1904 * every task struct that event->ctx->task could possibly point to
1905 * remains valid. This is OK when called from perf_release since
1906 * that only calls us on the top-level context, which can't be a clone.
1907 * When called from perf_event_exit_task, it's OK because the
1908 * context has been detached from its task.
1909 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001910static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001911{
Peter Zijlstra3996a912017-01-26 16:39:55 +01001912 struct perf_event_context *ctx = event->ctx;
1913
1914 lockdep_assert_held(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001915
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001916 event_function_call(event, __perf_remove_from_context, (void *)flags);
Peter Zijlstra3996a912017-01-26 16:39:55 +01001917
1918 /*
1919 * The above event_function_call() can NO-OP when it hits
1920 * TASK_TOMBSTONE. In that case we must already have been detached
1921 * from the context (by perf_event_exit_event()) but the grouping
1922 * might still be in-tact.
1923 */
1924 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1925 if ((flags & DETACH_GROUP) &&
1926 (event->attach_state & PERF_ATTACH_GROUP)) {
1927 /*
1928 * Since in that case we cannot possibly be scheduled, simply
1929 * detach now.
1930 */
1931 raw_spin_lock_irq(&ctx->lock);
1932 perf_group_detach(event);
1933 raw_spin_unlock_irq(&ctx->lock);
1934 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001935}
1936
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001937/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938 * Cross CPU call to disable a performance event
1939 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001940static void __perf_event_disable(struct perf_event *event,
1941 struct perf_cpu_context *cpuctx,
1942 struct perf_event_context *ctx,
1943 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001944{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001945 if (event->state < PERF_EVENT_STATE_INACTIVE)
1946 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001947
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001948 update_context_time(ctx);
1949 update_cgrp_time_from_event(event);
1950 update_group_times(event);
1951 if (event == event->group_leader)
1952 group_sched_out(event, cpuctx, ctx);
1953 else
1954 event_sched_out(event, cpuctx, ctx);
1955 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001956}
1957
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958/*
1959 * Disable a event.
1960 *
1961 * If event->ctx is a cloned context, callers must make sure that
1962 * every task struct that event->ctx->task could possibly point to
1963 * remains valid. This condition is satisifed when called through
1964 * perf_event_for_each_child or perf_event_for_each because they
1965 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001966 * goes to exit will block in perf_event_exit_event().
1967 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968 * When called from perf_pending_event it's OK because event->ctx
1969 * is the current context on this CPU and preemption is disabled,
1970 * hence we can't get into perf_event_task_sched_out for this context.
1971 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001972static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001973{
1974 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001975
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001976 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001977 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001978 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001979 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001981 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001982
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001983 event_function_call(event, __perf_event_disable, NULL);
1984}
1985
1986void perf_event_disable_local(struct perf_event *event)
1987{
1988 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001990
1991/*
1992 * Strictly speaking kernel users cannot create groups and therefore this
1993 * interface does not need the perf_event_ctx_lock() magic.
1994 */
1995void perf_event_disable(struct perf_event *event)
1996{
1997 struct perf_event_context *ctx;
1998
1999 ctx = perf_event_ctx_lock(event);
2000 _perf_event_disable(event);
2001 perf_event_ctx_unlock(event, ctx);
2002}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002003EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002005void perf_event_disable_inatomic(struct perf_event *event)
2006{
2007 event->pending_disable = 1;
2008 irq_work_queue(&event->pending);
2009}
2010
Stephane Eraniane5d13672011-02-14 11:20:01 +02002011static void perf_set_shadow_time(struct perf_event *event,
2012 struct perf_event_context *ctx,
2013 u64 tstamp)
2014{
2015 /*
2016 * use the correct time source for the time snapshot
2017 *
2018 * We could get by without this by leveraging the
2019 * fact that to get to this function, the caller
2020 * has most likely already called update_context_time()
2021 * and update_cgrp_time_xx() and thus both timestamp
2022 * are identical (or very close). Given that tstamp is,
2023 * already adjusted for cgroup, we could say that:
2024 * tstamp - ctx->timestamp
2025 * is equivalent to
2026 * tstamp - cgrp->timestamp.
2027 *
2028 * Then, in perf_output_read(), the calculation would
2029 * work with no changes because:
2030 * - event is guaranteed scheduled in
2031 * - no scheduled out in between
2032 * - thus the timestamp would be the same
2033 *
2034 * But this is a bit hairy.
2035 *
2036 * So instead, we have an explicit cgroup call to remain
2037 * within the time time source all along. We believe it
2038 * is cleaner and simpler to understand.
2039 */
2040 if (is_cgroup_event(event))
2041 perf_cgroup_set_shadow_time(event, tstamp);
2042 else
2043 event->shadow_ctx_time = tstamp - ctx->timestamp;
2044}
2045
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002046#define MAX_INTERRUPTS (~0ULL)
2047
2048static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002049static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002050
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002051static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002052event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002053 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002054 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002055{
Stephane Eranian41587552011-01-03 18:20:01 +02002056 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02002057 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002058
Peter Zijlstra63342412014-05-05 11:49:16 +02002059 lockdep_assert_held(&ctx->lock);
2060
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002061 if (event->state <= PERF_EVENT_STATE_OFF)
2062 return 0;
2063
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002064 WRITE_ONCE(event->oncpu, smp_processor_id());
2065 /*
2066 * Order event::oncpu write to happen before the ACTIVE state
2067 * is visible.
2068 */
2069 smp_wmb();
2070 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002071
2072 /*
2073 * Unthrottle events, since we scheduled we might have missed several
2074 * ticks already, also for a heavily scheduling task there is little
2075 * guarantee it'll get a tick in a timely manner.
2076 */
2077 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2078 perf_log_throttle(event, 1);
2079 event->hw.interrupts = 0;
2080 }
2081
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002082 /*
2083 * The new state must be visible before we turn it on in the hardware:
2084 */
2085 smp_wmb();
2086
Alexander Shishkin44377272013-12-16 14:17:36 +02002087 perf_pmu_disable(event->pmu);
2088
Shaohua Li72f669c2015-02-05 15:55:31 -08002089 perf_set_shadow_time(event, ctx, tstamp);
2090
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002091 perf_log_itrace_start(event);
2092
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002093 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002094 event->state = PERF_EVENT_STATE_INACTIVE;
2095 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002096 ret = -EAGAIN;
2097 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098 }
2099
Peter Zijlstra00a29162015-07-27 10:35:07 +02002100 event->tstamp_running += tstamp - event->tstamp_stopped;
2101
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002102 if (!is_software_event(event))
2103 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002104 if (!ctx->nr_active++)
2105 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002106 if (event->attr.freq && event->attr.sample_freq)
2107 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002108
2109 if (event->attr.exclusive)
2110 cpuctx->exclusive = 1;
2111
Alexander Shishkin44377272013-12-16 14:17:36 +02002112out:
2113 perf_pmu_enable(event->pmu);
2114
2115 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002116}
2117
2118static int
2119group_sched_in(struct perf_event *group_event,
2120 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002121 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002122{
Lin Ming6bde9b62010-04-23 13:56:00 +08002123 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002124 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002125 u64 now = ctx->time;
2126 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127
2128 if (group_event->state == PERF_EVENT_STATE_OFF)
2129 return 0;
2130
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002131 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002132
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002133 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002134 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002135 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002137 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138
2139 /*
2140 * Schedule in siblings as one group (if any):
2141 */
2142 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002143 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002144 partial_group = event;
2145 goto group_error;
2146 }
2147 }
2148
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002149 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002150 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002151
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002152group_error:
2153 /*
2154 * Groups can be scheduled in as one unit only, so undo any
2155 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002156 * The events up to the failed event are scheduled out normally,
2157 * tstamp_stopped will be updated.
2158 *
2159 * The failed events and the remaining siblings need to have
2160 * their timings updated as if they had gone thru event_sched_in()
2161 * and event_sched_out(). This is required to get consistent timings
2162 * across the group. This also takes care of the case where the group
2163 * could never be scheduled by ensuring tstamp_stopped is set to mark
2164 * the time the event was actually stopped, such that time delta
2165 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002166 */
2167 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2168 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002169 simulate = true;
2170
2171 if (simulate) {
2172 event->tstamp_running += now - event->tstamp_stopped;
2173 event->tstamp_stopped = now;
2174 } else {
2175 event_sched_out(event, cpuctx, ctx);
2176 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002177 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002178 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002179
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002180 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002181
Peter Zijlstra272325c2015-04-15 11:41:58 +02002182 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002183
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002184 return -EAGAIN;
2185}
2186
2187/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002188 * Work out whether we can put this event group on the CPU now.
2189 */
2190static int group_can_go_on(struct perf_event *event,
2191 struct perf_cpu_context *cpuctx,
2192 int can_add_hw)
2193{
2194 /*
2195 * Groups consisting entirely of software events can always go on.
2196 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002197 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002198 return 1;
2199 /*
2200 * If an exclusive group is already on, no other hardware
2201 * events can go on.
2202 */
2203 if (cpuctx->exclusive)
2204 return 0;
2205 /*
2206 * If this group is exclusive and there are already
2207 * events on the CPU, it can't go on.
2208 */
2209 if (event->attr.exclusive && cpuctx->active_oncpu)
2210 return 0;
2211 /*
2212 * Otherwise, try to add it if all previous groups were able
2213 * to go on.
2214 */
2215 return can_add_hw;
2216}
2217
2218static void add_event_to_ctx(struct perf_event *event,
2219 struct perf_event_context *ctx)
2220{
Stephane Eranian41587552011-01-03 18:20:01 +02002221 u64 tstamp = perf_event_time(event);
2222
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002223 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002224 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002225 event->tstamp_enabled = tstamp;
2226 event->tstamp_running = tstamp;
2227 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228}
2229
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002230static void ctx_sched_out(struct perf_event_context *ctx,
2231 struct perf_cpu_context *cpuctx,
2232 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002233static void
2234ctx_sched_in(struct perf_event_context *ctx,
2235 struct perf_cpu_context *cpuctx,
2236 enum event_type_t event_type,
2237 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002238
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002239static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2240 struct perf_event_context *ctx)
2241{
2242 if (!cpuctx->task_ctx)
2243 return;
2244
2245 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2246 return;
2247
2248 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2249}
2250
Peter Zijlstradce58552011-04-09 21:17:46 +02002251static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2252 struct perf_event_context *ctx,
2253 struct task_struct *task)
2254{
2255 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2256 if (ctx)
2257 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2258 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2259 if (ctx)
2260 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2261}
2262
Peter Zijlstra3e349502016-01-08 10:01:18 +01002263static void ctx_resched(struct perf_cpu_context *cpuctx,
2264 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002265{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002266 perf_pmu_disable(cpuctx->ctx.pmu);
2267 if (task_ctx)
2268 task_ctx_sched_out(cpuctx, task_ctx);
2269 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2270 perf_event_sched_in(cpuctx, task_ctx, current);
2271 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002272}
2273
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002274/*
2275 * Cross CPU call to install and enable a performance event
2276 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002277 * Very similar to remote_function() + event_function() but cannot assume that
2278 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002280static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002281{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002282 struct perf_event *event = info;
2283 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002284 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002285 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra1c686332016-12-09 14:59:00 +01002286 bool reprogram = true;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002287 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002288
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002289 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002290 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002291 raw_spin_lock(&ctx->lock);
2292 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002293
Peter Zijlstra1c686332016-12-09 14:59:00 +01002294 reprogram = (ctx->task == current);
2295
2296 /*
2297 * If the task is running, it must be running on this CPU,
2298 * otherwise we cannot reprogram things.
2299 *
2300 * If its not running, we don't care, ctx->lock will
2301 * serialize against it becoming runnable.
2302 */
2303 if (task_curr(ctx->task) && !reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002304 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002305 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002306 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307
Peter Zijlstra1c686332016-12-09 14:59:00 +01002308 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002309 } else if (task_ctx) {
2310 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002311 }
2312
Peter Zijlstra1c686332016-12-09 14:59:00 +01002313 if (reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002314 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2315 add_event_to_ctx(event, ctx);
2316 ctx_resched(cpuctx, task_ctx);
2317 } else {
2318 add_event_to_ctx(event, ctx);
2319 }
2320
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002321unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002322 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002323
Peter Zijlstraa0963092016-02-24 18:45:50 +01002324 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325}
2326
2327/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002328 * Attach a performance event to a context.
2329 *
2330 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002331 */
2332static void
2333perf_install_in_context(struct perf_event_context *ctx,
2334 struct perf_event *event,
2335 int cpu)
2336{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002337 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002338
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002339 lockdep_assert_held(&ctx->mutex);
2340
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002341 if (event->cpu != -1)
2342 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002343
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002344 /*
2345 * Ensures that if we can observe event->ctx, both the event and ctx
2346 * will be 'complete'. See perf_iterate_sb_cpu().
2347 */
2348 smp_store_release(&event->ctx, ctx);
2349
Peter Zijlstraa0963092016-02-24 18:45:50 +01002350 if (!task) {
2351 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002352 return;
2353 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002354
Peter Zijlstraa0963092016-02-24 18:45:50 +01002355 /*
2356 * Should not happen, we validate the ctx is still alive before calling.
2357 */
2358 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2359 return;
2360
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002361 /*
2362 * Installing events is tricky because we cannot rely on ctx->is_active
2363 * to be set in case this is the nr_events 0 -> 1 transition.
Peter Zijlstra1c686332016-12-09 14:59:00 +01002364 *
2365 * Instead we use task_curr(), which tells us if the task is running.
2366 * However, since we use task_curr() outside of rq::lock, we can race
2367 * against the actual state. This means the result can be wrong.
2368 *
2369 * If we get a false positive, we retry, this is harmless.
2370 *
2371 * If we get a false negative, things are complicated. If we are after
2372 * perf_event_context_sched_in() ctx::lock will serialize us, and the
2373 * value must be correct. If we're before, it doesn't matter since
2374 * perf_event_context_sched_in() will program the counter.
2375 *
2376 * However, this hinges on the remote context switch having observed
2377 * our task->perf_event_ctxp[] store, such that it will in fact take
2378 * ctx::lock in perf_event_context_sched_in().
2379 *
2380 * We do this by task_function_call(), if the IPI fails to hit the task
2381 * we know any future context switch of task must see the
2382 * perf_event_ctpx[] store.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002383 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002384
Peter Zijlstraa0963092016-02-24 18:45:50 +01002385 /*
Peter Zijlstra1c686332016-12-09 14:59:00 +01002386 * This smp_mb() orders the task->perf_event_ctxp[] store with the
2387 * task_cpu() load, such that if the IPI then does not find the task
2388 * running, a future context switch of that task must observe the
2389 * store.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002390 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002391 smp_mb();
2392again:
2393 if (!task_function_call(task, __perf_install_in_context, event))
Peter Zijlstraa0963092016-02-24 18:45:50 +01002394 return;
2395
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002396 raw_spin_lock_irq(&ctx->lock);
2397 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002398 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2399 /*
2400 * Cannot happen because we already checked above (which also
2401 * cannot happen), and we hold ctx->mutex, which serializes us
2402 * against perf_event_exit_task_context().
2403 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002404 raw_spin_unlock_irq(&ctx->lock);
2405 return;
2406 }
Peter Zijlstraa0963092016-02-24 18:45:50 +01002407 /*
Peter Zijlstra1c686332016-12-09 14:59:00 +01002408 * If the task is not running, ctx->lock will avoid it becoming so,
2409 * thus we can safely install the event.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002410 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002411 if (task_curr(task)) {
2412 raw_spin_unlock_irq(&ctx->lock);
2413 goto again;
2414 }
2415 add_event_to_ctx(event, ctx);
2416 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002417}
2418
2419/*
2420 * Put a event into inactive state and update time fields.
2421 * Enabling the leader of a group effectively enables all
2422 * the group members that aren't explicitly disabled, so we
2423 * have to update their ->tstamp_enabled also.
2424 * Note: this works for group members as well as group leaders
2425 * since the non-leader members' sibling_lists will be empty.
2426 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002427static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428{
2429 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002430 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002431
2432 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002433 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002434 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002435 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2436 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002437 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002438}
2439
2440/*
2441 * Cross CPU call to enable a performance event
2442 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002443static void __perf_event_enable(struct perf_event *event,
2444 struct perf_cpu_context *cpuctx,
2445 struct perf_event_context *ctx,
2446 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002447{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002448 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002449 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002450
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002451 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2452 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002453 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002454
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002455 if (ctx->is_active)
2456 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2457
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002458 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002460 if (!ctx->is_active)
2461 return;
2462
Stephane Eraniane5d13672011-02-14 11:20:01 +02002463 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002464 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002465 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002466 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002467 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002468 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002469
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002470 /*
2471 * If the event is in a group and isn't the group leader,
2472 * then don't put it on unless the group is on.
2473 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002474 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2475 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002476 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002477 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002478
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002479 task_ctx = cpuctx->task_ctx;
2480 if (ctx->task)
2481 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002482
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002483 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002484}
2485
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002486/*
2487 * Enable a event.
2488 *
2489 * If event->ctx is a cloned context, callers must make sure that
2490 * every task struct that event->ctx->task could possibly point to
2491 * remains valid. This condition is satisfied when called through
2492 * perf_event_for_each_child or perf_event_for_each as described
2493 * for perf_event_disable.
2494 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002495static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002496{
2497 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002498
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002499 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002500 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2501 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002502 raw_spin_unlock_irq(&ctx->lock);
2503 return;
2504 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002505
2506 /*
2507 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002508 *
2509 * That way, if we see the event in error state below, we know that it
2510 * has gone back into error state, as distinct from the task having
2511 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002512 */
2513 if (event->state == PERF_EVENT_STATE_ERROR)
2514 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002515 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002516
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002517 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002518}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002519
2520/*
2521 * See perf_event_disable();
2522 */
2523void perf_event_enable(struct perf_event *event)
2524{
2525 struct perf_event_context *ctx;
2526
2527 ctx = perf_event_ctx_lock(event);
2528 _perf_event_enable(event);
2529 perf_event_ctx_unlock(event, ctx);
2530}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002531EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002532
Alexander Shishkin375637b2016-04-27 18:44:46 +03002533struct stop_event_data {
2534 struct perf_event *event;
2535 unsigned int restart;
2536};
2537
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002538static int __perf_event_stop(void *info)
2539{
Alexander Shishkin375637b2016-04-27 18:44:46 +03002540 struct stop_event_data *sd = info;
2541 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002542
Alexander Shishkin375637b2016-04-27 18:44:46 +03002543 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002544 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2545 return 0;
2546
2547 /* matches smp_wmb() in event_sched_in() */
2548 smp_rmb();
2549
2550 /*
2551 * There is a window with interrupts enabled before we get here,
2552 * so we need to check again lest we try to stop another CPU's event.
2553 */
2554 if (READ_ONCE(event->oncpu) != smp_processor_id())
2555 return -EAGAIN;
2556
2557 event->pmu->stop(event, PERF_EF_UPDATE);
2558
Alexander Shishkin375637b2016-04-27 18:44:46 +03002559 /*
2560 * May race with the actual stop (through perf_pmu_output_stop()),
2561 * but it is only used for events with AUX ring buffer, and such
2562 * events will refuse to restart because of rb::aux_mmap_count==0,
2563 * see comments in perf_aux_output_begin().
2564 *
2565 * Since this is happening on a event-local CPU, no trace is lost
2566 * while restarting.
2567 */
2568 if (sd->restart)
Will Deaconc9bbdd42016-08-15 11:42:45 +01002569 event->pmu->start(event, 0);
Alexander Shishkin375637b2016-04-27 18:44:46 +03002570
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002571 return 0;
2572}
2573
Alexander Shishkin767ae082016-09-06 16:23:49 +03002574static int perf_event_stop(struct perf_event *event, int restart)
Alexander Shishkin375637b2016-04-27 18:44:46 +03002575{
2576 struct stop_event_data sd = {
2577 .event = event,
Alexander Shishkin767ae082016-09-06 16:23:49 +03002578 .restart = restart,
Alexander Shishkin375637b2016-04-27 18:44:46 +03002579 };
2580 int ret = 0;
2581
2582 do {
2583 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2584 return 0;
2585
2586 /* matches smp_wmb() in event_sched_in() */
2587 smp_rmb();
2588
2589 /*
2590 * We only want to restart ACTIVE events, so if the event goes
2591 * inactive here (event->oncpu==-1), there's nothing more to do;
2592 * fall through with ret==-ENXIO.
2593 */
2594 ret = cpu_function_call(READ_ONCE(event->oncpu),
2595 __perf_event_stop, &sd);
2596 } while (ret == -EAGAIN);
2597
2598 return ret;
2599}
2600
2601/*
2602 * In order to contain the amount of racy and tricky in the address filter
2603 * configuration management, it is a two part process:
2604 *
2605 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2606 * we update the addresses of corresponding vmas in
2607 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2608 * (p2) when an event is scheduled in (pmu::add), it calls
2609 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2610 * if the generation has changed since the previous call.
2611 *
2612 * If (p1) happens while the event is active, we restart it to force (p2).
2613 *
2614 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2615 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2616 * ioctl;
2617 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2618 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2619 * for reading;
2620 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2621 * of exec.
2622 */
2623void perf_event_addr_filters_sync(struct perf_event *event)
2624{
2625 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2626
2627 if (!has_addr_filter(event))
2628 return;
2629
2630 raw_spin_lock(&ifh->lock);
2631 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2632 event->pmu->addr_filters_sync(event);
2633 event->hw.addr_filters_gen = event->addr_filters_gen;
2634 }
2635 raw_spin_unlock(&ifh->lock);
2636}
2637EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2638
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002639static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002640{
2641 /*
2642 * not supported on inherited events
2643 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002644 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002645 return -EINVAL;
2646
2647 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002648 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002649
2650 return 0;
2651}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002652
2653/*
2654 * See perf_event_disable()
2655 */
2656int perf_event_refresh(struct perf_event *event, int refresh)
2657{
2658 struct perf_event_context *ctx;
2659 int ret;
2660
2661 ctx = perf_event_ctx_lock(event);
2662 ret = _perf_event_refresh(event, refresh);
2663 perf_event_ctx_unlock(event, ctx);
2664
2665 return ret;
2666}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002667EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002668
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002669static void ctx_sched_out(struct perf_event_context *ctx,
2670 struct perf_cpu_context *cpuctx,
2671 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002672{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002673 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002674 struct perf_event *event;
2675
2676 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002677
Peter Zijlstra39a43642016-01-11 12:46:35 +01002678 if (likely(!ctx->nr_events)) {
2679 /*
2680 * See __perf_remove_from_context().
2681 */
2682 WARN_ON_ONCE(ctx->is_active);
2683 if (ctx->task)
2684 WARN_ON_ONCE(cpuctx->task_ctx);
2685 return;
2686 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002687
Peter Zijlstradb24d332011-04-09 21:17:45 +02002688 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002689 if (!(ctx->is_active & EVENT_ALL))
2690 ctx->is_active = 0;
2691
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002692 if (ctx->task) {
2693 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2694 if (!ctx->is_active)
2695 cpuctx->task_ctx = NULL;
2696 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002697
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002698 /*
2699 * Always update time if it was set; not only when it changes.
2700 * Otherwise we can 'forget' to update time for any but the last
2701 * context we sched out. For example:
2702 *
2703 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2704 * ctx_sched_out(.event_type = EVENT_PINNED)
2705 *
2706 * would only update time for the pinned events.
2707 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002708 if (is_active & EVENT_TIME) {
2709 /* update (and stop) ctx time */
2710 update_context_time(ctx);
2711 update_cgrp_time_from_cpuctx(cpuctx);
2712 }
2713
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002714 is_active ^= ctx->is_active; /* changed bits */
2715
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002716 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002717 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002718
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002719 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002720 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002721 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2722 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002723 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002724
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002725 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002726 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002727 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002728 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002729 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730}
2731
2732/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002733 * Test whether two contexts are equivalent, i.e. whether they have both been
2734 * cloned from the same version of the same context.
2735 *
2736 * Equivalence is measured using a generation number in the context that is
2737 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2738 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002739 */
2740static int context_equiv(struct perf_event_context *ctx1,
2741 struct perf_event_context *ctx2)
2742{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002743 lockdep_assert_held(&ctx1->lock);
2744 lockdep_assert_held(&ctx2->lock);
2745
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002746 /* Pinning disables the swap optimization */
2747 if (ctx1->pin_count || ctx2->pin_count)
2748 return 0;
2749
2750 /* If ctx1 is the parent of ctx2 */
2751 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2752 return 1;
2753
2754 /* If ctx2 is the parent of ctx1 */
2755 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2756 return 1;
2757
2758 /*
2759 * If ctx1 and ctx2 have the same parent; we flatten the parent
2760 * hierarchy, see perf_event_init_context().
2761 */
2762 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2763 ctx1->parent_gen == ctx2->parent_gen)
2764 return 1;
2765
2766 /* Unmatched */
2767 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002768}
2769
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002770static void __perf_event_sync_stat(struct perf_event *event,
2771 struct perf_event *next_event)
2772{
2773 u64 value;
2774
2775 if (!event->attr.inherit_stat)
2776 return;
2777
2778 /*
2779 * Update the event value, we cannot use perf_event_read()
2780 * because we're in the middle of a context switch and have IRQs
2781 * disabled, which upsets smp_call_function_single(), however
2782 * we know the event must be on the current CPU, therefore we
2783 * don't need to use it.
2784 */
2785 switch (event->state) {
2786 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002787 event->pmu->read(event);
2788 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002789
2790 case PERF_EVENT_STATE_INACTIVE:
2791 update_event_times(event);
2792 break;
2793
2794 default:
2795 break;
2796 }
2797
2798 /*
2799 * In order to keep per-task stats reliable we need to flip the event
2800 * values when we flip the contexts.
2801 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002802 value = local64_read(&next_event->count);
2803 value = local64_xchg(&event->count, value);
2804 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002805
2806 swap(event->total_time_enabled, next_event->total_time_enabled);
2807 swap(event->total_time_running, next_event->total_time_running);
2808
2809 /*
2810 * Since we swizzled the values, update the user visible data too.
2811 */
2812 perf_event_update_userpage(event);
2813 perf_event_update_userpage(next_event);
2814}
2815
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002816static void perf_event_sync_stat(struct perf_event_context *ctx,
2817 struct perf_event_context *next_ctx)
2818{
2819 struct perf_event *event, *next_event;
2820
2821 if (!ctx->nr_stat)
2822 return;
2823
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002824 update_context_time(ctx);
2825
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002826 event = list_first_entry(&ctx->event_list,
2827 struct perf_event, event_entry);
2828
2829 next_event = list_first_entry(&next_ctx->event_list,
2830 struct perf_event, event_entry);
2831
2832 while (&event->event_entry != &ctx->event_list &&
2833 &next_event->event_entry != &next_ctx->event_list) {
2834
2835 __perf_event_sync_stat(event, next_event);
2836
2837 event = list_next_entry(event, event_entry);
2838 next_event = list_next_entry(next_event, event_entry);
2839 }
2840}
2841
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002842static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2843 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002844{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002845 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002846 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002847 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002848 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849 int do_switch = 1;
2850
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002851 if (likely(!ctx))
2852 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002853
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002854 cpuctx = __get_cpu_context(ctx);
2855 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002856 return;
2857
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002858 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002859 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002860 if (!next_ctx)
2861 goto unlock;
2862
2863 parent = rcu_dereference(ctx->parent_ctx);
2864 next_parent = rcu_dereference(next_ctx->parent_ctx);
2865
2866 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002867 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002868 goto unlock;
2869
2870 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002871 /*
2872 * Looks like the two contexts are clones, so we might be
2873 * able to optimize the context switch. We lock both
2874 * contexts and check that they are clones under the
2875 * lock (including re-checking that neither has been
2876 * uncloned in the meantime). It doesn't matter which
2877 * order we take the locks because no other cpu could
2878 * be trying to lock both of these tasks.
2879 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002880 raw_spin_lock(&ctx->lock);
2881 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002882 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002883 WRITE_ONCE(ctx->task, next);
2884 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002885
2886 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2887
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002888 /*
2889 * RCU_INIT_POINTER here is safe because we've not
2890 * modified the ctx and the above modification of
2891 * ctx->task and ctx->task_ctx_data are immaterial
2892 * since those values are always verified under
2893 * ctx->lock which we're now holding.
2894 */
2895 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2896 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2897
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002898 do_switch = 0;
2899
2900 perf_event_sync_stat(ctx, next_ctx);
2901 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002902 raw_spin_unlock(&next_ctx->lock);
2903 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002904 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002905unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002906 rcu_read_unlock();
2907
2908 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002909 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002910 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002911 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002912 }
2913}
2914
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002915static DEFINE_PER_CPU(struct list_head, sched_cb_list);
2916
Yan, Zhengba532502014-11-04 21:55:58 -05002917void perf_sched_cb_dec(struct pmu *pmu)
2918{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002919 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2920
Yan, Zhengba532502014-11-04 21:55:58 -05002921 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002922
2923 if (!--cpuctx->sched_cb_usage)
2924 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05002925}
2926
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002927
Yan, Zhengba532502014-11-04 21:55:58 -05002928void perf_sched_cb_inc(struct pmu *pmu)
2929{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002930 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2931
2932 if (!cpuctx->sched_cb_usage++)
2933 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
2934
Yan, Zhengba532502014-11-04 21:55:58 -05002935 this_cpu_inc(perf_sched_cb_usages);
2936}
2937
2938/*
2939 * This function provides the context switch callback to the lower code
2940 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02002941 *
2942 * This callback is relevant even to per-cpu events; for example multi event
2943 * PEBS requires this to provide PID/TID information. This requires we flush
2944 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05002945 */
2946static void perf_pmu_sched_task(struct task_struct *prev,
2947 struct task_struct *next,
2948 bool sched_in)
2949{
2950 struct perf_cpu_context *cpuctx;
2951 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05002952
2953 if (prev == next)
2954 return;
2955
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002956 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
2957 pmu = cpuctx->unique_pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05002958
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002959 if (WARN_ON_ONCE(!pmu->sched_task))
2960 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05002961
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002962 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2963 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05002964
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002965 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05002966
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002967 perf_pmu_enable(pmu);
2968 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05002969 }
Yan, Zhengba532502014-11-04 21:55:58 -05002970}
2971
Adrian Hunter45ac1402015-07-21 12:44:02 +03002972static void perf_event_switch(struct task_struct *task,
2973 struct task_struct *next_prev, bool sched_in);
2974
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002975#define for_each_task_context_nr(ctxn) \
2976 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2977
2978/*
2979 * Called from scheduler to remove the events of the current task,
2980 * with interrupts disabled.
2981 *
2982 * We stop each event and update the event value in event->count.
2983 *
2984 * This does not protect us against NMI, but disable()
2985 * sets the disabled bit in the control field of event _before_
2986 * accessing the event control register. If a NMI hits, then it will
2987 * not restart the event.
2988 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002989void __perf_event_task_sched_out(struct task_struct *task,
2990 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002991{
2992 int ctxn;
2993
Yan, Zhengba532502014-11-04 21:55:58 -05002994 if (__this_cpu_read(perf_sched_cb_usages))
2995 perf_pmu_sched_task(task, next, false);
2996
Adrian Hunter45ac1402015-07-21 12:44:02 +03002997 if (atomic_read(&nr_switch_events))
2998 perf_event_switch(task, next, false);
2999
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003000 for_each_task_context_nr(ctxn)
3001 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003002
3003 /*
3004 * if cgroup events exist on this CPU, then we need
3005 * to check if we have to switch out PMU state.
3006 * cgroup event are system-wide mode only
3007 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05003008 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003009 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003010}
3011
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003012/*
3013 * Called with IRQs disabled
3014 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003015static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
3016 enum event_type_t event_type)
3017{
3018 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019}
3020
3021static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003022ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003023 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024{
3025 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003027 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
3028 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02003030 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003031 continue;
3032
Stephane Eraniane5d13672011-02-14 11:20:01 +02003033 /* may need to reset tstamp_enabled */
3034 if (is_cgroup_event(event))
3035 perf_cgroup_mark_enabled(event, ctx);
3036
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08003037 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01003038 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003039
3040 /*
3041 * If this pinned group hasn't been scheduled,
3042 * put it in error state.
3043 */
3044 if (event->state == PERF_EVENT_STATE_INACTIVE) {
3045 update_group_times(event);
3046 event->state = PERF_EVENT_STATE_ERROR;
3047 }
3048 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003049}
3050
3051static void
3052ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003053 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003054{
3055 struct perf_event *event;
3056 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003058 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
3059 /* Ignore events in OFF or ERROR state */
3060 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003061 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003062 /*
3063 * Listen to the 'cpu' scheduling filter constraint
3064 * of events:
3065 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02003066 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003067 continue;
3068
Stephane Eraniane5d13672011-02-14 11:20:01 +02003069 /* may need to reset tstamp_enabled */
3070 if (is_cgroup_event(event))
3071 perf_cgroup_mark_enabled(event, ctx);
3072
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003073 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01003074 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003075 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003076 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003078}
3079
3080static void
3081ctx_sched_in(struct perf_event_context *ctx,
3082 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003083 enum event_type_t event_type,
3084 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003085{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003086 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003087 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003088
Peter Zijlstrac994d612016-01-08 09:20:23 +01003089 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003090
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003091 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003092 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003093
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003094 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003095 if (ctx->task) {
3096 if (!is_active)
3097 cpuctx->task_ctx = ctx;
3098 else
3099 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3100 }
3101
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003102 is_active ^= ctx->is_active; /* changed bits */
3103
3104 if (is_active & EVENT_TIME) {
3105 /* start ctx time */
3106 now = perf_clock();
3107 ctx->timestamp = now;
3108 perf_cgroup_set_timestamp(task, ctx);
3109 }
3110
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003111 /*
3112 * First go through the list and put on any pinned groups
3113 * in order to give them the best chance of going on.
3114 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003115 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003116 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003117
3118 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003119 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003120 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003121}
3122
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003123static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003124 enum event_type_t event_type,
3125 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003126{
3127 struct perf_event_context *ctx = &cpuctx->ctx;
3128
Stephane Eraniane5d13672011-02-14 11:20:01 +02003129 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003130}
3131
Stephane Eraniane5d13672011-02-14 11:20:01 +02003132static void perf_event_context_sched_in(struct perf_event_context *ctx,
3133 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003134{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003135 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003136
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003137 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003138 if (cpuctx->task_ctx == ctx)
3139 return;
3140
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003141 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003142 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003143 /*
3144 * We want to keep the following priority order:
3145 * cpu pinned (that don't need to move), task pinned,
3146 * cpu flexible, task flexible.
3147 */
3148 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003149 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003150 perf_pmu_enable(ctx->pmu);
3151 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003152}
3153
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003154/*
3155 * Called from scheduler to add the events of the current task
3156 * with interrupts disabled.
3157 *
3158 * We restore the event value and then enable it.
3159 *
3160 * This does not protect us against NMI, but enable()
3161 * sets the enabled bit in the control field of event _before_
3162 * accessing the event control register. If a NMI hits, then it will
3163 * keep the event running.
3164 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003165void __perf_event_task_sched_in(struct task_struct *prev,
3166 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003167{
3168 struct perf_event_context *ctx;
3169 int ctxn;
3170
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003171 /*
3172 * If cgroup events exist on this CPU, then we need to check if we have
3173 * to switch in PMU state; cgroup event are system-wide mode only.
3174 *
3175 * Since cgroup events are CPU events, we must schedule these in before
3176 * we schedule in the task events.
3177 */
3178 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3179 perf_cgroup_sched_in(prev, task);
3180
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003181 for_each_task_context_nr(ctxn) {
3182 ctx = task->perf_event_ctxp[ctxn];
3183 if (likely(!ctx))
3184 continue;
3185
Stephane Eraniane5d13672011-02-14 11:20:01 +02003186 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003187 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003188
Adrian Hunter45ac1402015-07-21 12:44:02 +03003189 if (atomic_read(&nr_switch_events))
3190 perf_event_switch(task, prev, true);
3191
Yan, Zhengba532502014-11-04 21:55:58 -05003192 if (__this_cpu_read(perf_sched_cb_usages))
3193 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003194}
3195
Peter Zijlstraabd50712010-01-26 18:50:16 +01003196static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3197{
3198 u64 frequency = event->attr.sample_freq;
3199 u64 sec = NSEC_PER_SEC;
3200 u64 divisor, dividend;
3201
3202 int count_fls, nsec_fls, frequency_fls, sec_fls;
3203
3204 count_fls = fls64(count);
3205 nsec_fls = fls64(nsec);
3206 frequency_fls = fls64(frequency);
3207 sec_fls = 30;
3208
3209 /*
3210 * We got @count in @nsec, with a target of sample_freq HZ
3211 * the target period becomes:
3212 *
3213 * @count * 10^9
3214 * period = -------------------
3215 * @nsec * sample_freq
3216 *
3217 */
3218
3219 /*
3220 * Reduce accuracy by one bit such that @a and @b converge
3221 * to a similar magnitude.
3222 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003223#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003224do { \
3225 if (a##_fls > b##_fls) { \
3226 a >>= 1; \
3227 a##_fls--; \
3228 } else { \
3229 b >>= 1; \
3230 b##_fls--; \
3231 } \
3232} while (0)
3233
3234 /*
3235 * Reduce accuracy until either term fits in a u64, then proceed with
3236 * the other, so that finally we can do a u64/u64 division.
3237 */
3238 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3239 REDUCE_FLS(nsec, frequency);
3240 REDUCE_FLS(sec, count);
3241 }
3242
3243 if (count_fls + sec_fls > 64) {
3244 divisor = nsec * frequency;
3245
3246 while (count_fls + sec_fls > 64) {
3247 REDUCE_FLS(count, sec);
3248 divisor >>= 1;
3249 }
3250
3251 dividend = count * sec;
3252 } else {
3253 dividend = count * sec;
3254
3255 while (nsec_fls + frequency_fls > 64) {
3256 REDUCE_FLS(nsec, frequency);
3257 dividend >>= 1;
3258 }
3259
3260 divisor = nsec * frequency;
3261 }
3262
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003263 if (!divisor)
3264 return dividend;
3265
Peter Zijlstraabd50712010-01-26 18:50:16 +01003266 return div64_u64(dividend, divisor);
3267}
3268
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003269static DEFINE_PER_CPU(int, perf_throttled_count);
3270static DEFINE_PER_CPU(u64, perf_throttled_seq);
3271
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003272static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003273{
3274 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003275 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003276 s64 delta;
3277
Peter Zijlstraabd50712010-01-26 18:50:16 +01003278 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003279
3280 delta = (s64)(period - hwc->sample_period);
3281 delta = (delta + 7) / 8; /* low pass filter */
3282
3283 sample_period = hwc->sample_period + delta;
3284
3285 if (!sample_period)
3286 sample_period = 1;
3287
3288 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003289
Peter Zijlstrae7850592010-05-21 14:43:08 +02003290 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003291 if (disable)
3292 event->pmu->stop(event, PERF_EF_UPDATE);
3293
Peter Zijlstrae7850592010-05-21 14:43:08 +02003294 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003295
3296 if (disable)
3297 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003298 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299}
3300
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003301/*
3302 * combine freq adjustment with unthrottling to avoid two passes over the
3303 * events. At the same time, make sure, having freq events does not change
3304 * the rate of unthrottling as that would introduce bias.
3305 */
3306static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3307 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308{
3309 struct perf_event *event;
3310 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003311 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003312 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003313
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003314 /*
3315 * only need to iterate over all events iff:
3316 * - context have events in frequency mode (needs freq adjust)
3317 * - there are events to unthrottle on this cpu
3318 */
3319 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003320 return;
3321
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003322 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003323 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003324
Paul Mackerras03541f82009-10-14 16:58:03 +11003325 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003326 if (event->state != PERF_EVENT_STATE_ACTIVE)
3327 continue;
3328
Stephane Eranian5632ab12011-01-03 18:20:01 +02003329 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003330 continue;
3331
Alexander Shishkin44377272013-12-16 14:17:36 +02003332 perf_pmu_disable(event->pmu);
3333
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003334 hwc = &event->hw;
3335
Jiri Olsaae23bff2013-08-24 16:45:54 +02003336 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003337 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003339 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340 }
3341
3342 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003343 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003344
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003345 /*
3346 * stop the event and update event->count
3347 */
3348 event->pmu->stop(event, PERF_EF_UPDATE);
3349
Peter Zijlstrae7850592010-05-21 14:43:08 +02003350 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003351 delta = now - hwc->freq_count_stamp;
3352 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003353
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003354 /*
3355 * restart the event
3356 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003357 * we have stopped the event so tell that
3358 * to perf_adjust_period() to avoid stopping it
3359 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003360 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003361 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003362 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003363
3364 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003365 next:
3366 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003368
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003369 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003370 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371}
3372
3373/*
3374 * Round-robin a context's events:
3375 */
3376static void rotate_ctx(struct perf_event_context *ctx)
3377{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003378 /*
3379 * Rotate the first entry last of non-pinned groups. Rotation might be
3380 * disabled by the inheritance code.
3381 */
3382 if (!ctx->rotate_disable)
3383 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384}
3385
Stephane Eranian9e630202013-04-03 14:21:33 +02003386static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003388 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003389 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003391 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003392 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3393 rotate = 1;
3394 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003396 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003397 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003398 if (ctx->nr_events != ctx->nr_active)
3399 rotate = 1;
3400 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003401
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003402 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003403 goto done;
3404
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003405 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003406 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003408 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3409 if (ctx)
3410 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003411
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003412 rotate_ctx(&cpuctx->ctx);
3413 if (ctx)
3414 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003415
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003416 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003417
3418 perf_pmu_enable(cpuctx->ctx.pmu);
3419 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003420done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003421
3422 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003423}
3424
3425void perf_event_task_tick(void)
3426{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003427 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3428 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003429 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003430
3431 WARN_ON(!irqs_disabled());
3432
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003433 __this_cpu_inc(perf_throttled_seq);
3434 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003435 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003436
Mark Rutland2fde4f92015-01-07 15:01:54 +00003437 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003438 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003439}
3440
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003441static int event_enable_on_exec(struct perf_event *event,
3442 struct perf_event_context *ctx)
3443{
3444 if (!event->attr.enable_on_exec)
3445 return 0;
3446
3447 event->attr.enable_on_exec = 0;
3448 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3449 return 0;
3450
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003451 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003452
3453 return 1;
3454}
3455
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003456/*
3457 * Enable all of a task's events that have been marked enable-on-exec.
3458 * This expects task == current.
3459 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003460static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003461{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003462 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003463 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003464 struct perf_event *event;
3465 unsigned long flags;
3466 int enabled = 0;
3467
3468 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003469 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003470 if (!ctx || !ctx->nr_events)
3471 goto out;
3472
Peter Zijlstra3e349502016-01-08 10:01:18 +01003473 cpuctx = __get_cpu_context(ctx);
3474 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003475 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003476 list_for_each_entry(event, &ctx->event_list, event_entry)
3477 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003478
3479 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003480 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003481 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003482 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003483 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003484 ctx_resched(cpuctx, ctx);
3485 }
3486 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003487
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003488out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003489 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003490
3491 if (clone_ctx)
3492 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003493}
3494
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003495struct perf_read_data {
3496 struct perf_event *event;
3497 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003498 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003499};
3500
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003501static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003502{
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003503 u16 local_pkg, event_pkg;
3504
3505 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003506 int local_cpu = smp_processor_id();
3507
3508 event_pkg = topology_physical_package_id(event_cpu);
3509 local_pkg = topology_physical_package_id(local_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003510
3511 if (event_pkg == local_pkg)
3512 return local_cpu;
3513 }
3514
3515 return event_cpu;
3516}
3517
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003518/*
3519 * Cross CPU call to read the hardware event
3520 */
3521static void __perf_event_read(void *info)
3522{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003523 struct perf_read_data *data = info;
3524 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003525 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003526 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003527 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003528
3529 /*
3530 * If this is a task context, we need to check whether it is
3531 * the current task context of this cpu. If not it has been
3532 * scheduled out before the smp call arrived. In that case
3533 * event->count would have been updated to a recent sample
3534 * when the event was scheduled out.
3535 */
3536 if (ctx->task && cpuctx->task_ctx != ctx)
3537 return;
3538
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003539 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003540 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003541 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003542 update_cgrp_time_from_event(event);
3543 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003544
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003545 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003546 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003547 goto unlock;
3548
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003549 if (!data->group) {
3550 pmu->read(event);
3551 data->ret = 0;
3552 goto unlock;
3553 }
3554
3555 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3556
3557 pmu->read(event);
3558
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003559 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3560 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003561 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3562 /*
3563 * Use sibling's PMU rather than @event's since
3564 * sibling could be on different (eg: software) PMU.
3565 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003566 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003567 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003568 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003569
3570 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003571
3572unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003573 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003574}
3575
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003576static inline u64 perf_event_count(struct perf_event *event)
3577{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003578 if (event->pmu->count)
3579 return event->pmu->count(event);
3580
3581 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003582}
3583
Kaixu Xiaffe86902015-08-06 07:02:32 +00003584/*
3585 * NMI-safe method to read a local event, that is an event that
3586 * is:
3587 * - either for the current task, or for this CPU
3588 * - does not have inherit set, for inherited task events
3589 * will not be local and we cannot read them atomically
3590 * - must not have a pmu::count method
3591 */
3592u64 perf_event_read_local(struct perf_event *event)
3593{
3594 unsigned long flags;
3595 u64 val;
3596
3597 /*
3598 * Disabling interrupts avoids all counter scheduling (context
3599 * switches, timer based rotation and IPIs).
3600 */
3601 local_irq_save(flags);
3602
3603 /* If this is a per-task event, it must be for current */
3604 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3605 event->hw.target != current);
3606
3607 /* If this is a per-CPU event, it must be for this CPU */
3608 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3609 event->cpu != smp_processor_id());
3610
3611 /*
3612 * It must not be an event with inherit set, we cannot read
3613 * all child counters from atomic context.
3614 */
3615 WARN_ON_ONCE(event->attr.inherit);
3616
3617 /*
3618 * It must not have a pmu::count method, those are not
3619 * NMI safe.
3620 */
3621 WARN_ON_ONCE(event->pmu->count);
3622
3623 /*
3624 * If the event is currently on this CPU, its either a per-task event,
3625 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3626 * oncpu == -1).
3627 */
3628 if (event->oncpu == smp_processor_id())
3629 event->pmu->read(event);
3630
3631 val = local64_read(&event->count);
3632 local_irq_restore(flags);
3633
3634 return val;
3635}
3636
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003637static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003638{
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003639 int event_cpu, ret = 0;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003640
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003641 /*
3642 * If event is enabled and currently active on a CPU, update the
3643 * value in the event structure:
3644 */
3645 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003646 struct perf_read_data data = {
3647 .event = event,
3648 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003649 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003650 };
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003651
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003652 event_cpu = READ_ONCE(event->oncpu);
3653 if ((unsigned)event_cpu >= nr_cpu_ids)
3654 return 0;
3655
3656 preempt_disable();
3657 event_cpu = __perf_event_read_cpu(event, event_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003658
Peter Zijlstra58763142016-08-30 10:15:03 +02003659 /*
3660 * Purposely ignore the smp_call_function_single() return
3661 * value.
3662 *
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003663 * If event_cpu isn't a valid CPU it means the event got
Peter Zijlstra58763142016-08-30 10:15:03 +02003664 * scheduled out and that will have updated the event count.
3665 *
3666 * Therefore, either way, we'll have an up-to-date event count
3667 * after this.
3668 */
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003669 (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1);
3670 preempt_enable();
Peter Zijlstra58763142016-08-30 10:15:03 +02003671 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003672 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003673 struct perf_event_context *ctx = event->ctx;
3674 unsigned long flags;
3675
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003676 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003677 /*
3678 * may read while context is not active
3679 * (e.g., thread is blocked), in that case
3680 * we cannot update context time
3681 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003682 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003683 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003684 update_cgrp_time_from_event(event);
3685 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003686 if (group)
3687 update_group_times(event);
3688 else
3689 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003690 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003691 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003692
3693 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003694}
3695
3696/*
3697 * Initialize the perf_event context in a task_struct:
3698 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003699static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003700{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003701 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003702 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003703 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003704 INIT_LIST_HEAD(&ctx->pinned_groups);
3705 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003706 INIT_LIST_HEAD(&ctx->event_list);
3707 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003708}
3709
Peter Zijlstraeb184472010-09-07 15:55:13 +02003710static struct perf_event_context *
3711alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003712{
3713 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003714
3715 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3716 if (!ctx)
3717 return NULL;
3718
3719 __perf_event_init_context(ctx);
3720 if (task) {
3721 ctx->task = task;
3722 get_task_struct(task);
3723 }
3724 ctx->pmu = pmu;
3725
3726 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003727}
3728
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003729static struct task_struct *
3730find_lively_task_by_vpid(pid_t vpid)
3731{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003732 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003733
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003734 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003735 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003736 task = current;
3737 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003738 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003739 if (task)
3740 get_task_struct(task);
3741 rcu_read_unlock();
3742
3743 if (!task)
3744 return ERR_PTR(-ESRCH);
3745
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003746 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003747}
3748
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003749/*
3750 * Returns a matching context with refcount and pincount.
3751 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003752static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003753find_get_context(struct pmu *pmu, struct task_struct *task,
3754 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003755{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003756 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003757 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003758 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003760 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003761 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003762
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003763 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003764 /* Must be root to operate on a CPU event: */
3765 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3766 return ERR_PTR(-EACCES);
3767
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003768 /*
3769 * We could be clever and allow to attach a event to an
3770 * offline CPU and activate it when the CPU comes up, but
3771 * that's for later.
3772 */
3773 if (!cpu_online(cpu))
3774 return ERR_PTR(-ENODEV);
3775
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003776 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003777 ctx = &cpuctx->ctx;
3778 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003779 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003780
3781 return ctx;
3782 }
3783
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003784 err = -EINVAL;
3785 ctxn = pmu->task_ctx_nr;
3786 if (ctxn < 0)
3787 goto errout;
3788
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003789 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3790 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3791 if (!task_ctx_data) {
3792 err = -ENOMEM;
3793 goto errout;
3794 }
3795 }
3796
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003797retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003798 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003799 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003800 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003801 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003802
3803 if (task_ctx_data && !ctx->task_ctx_data) {
3804 ctx->task_ctx_data = task_ctx_data;
3805 task_ctx_data = NULL;
3806 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003807 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003808
3809 if (clone_ctx)
3810 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003811 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003812 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003813 err = -ENOMEM;
3814 if (!ctx)
3815 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003816
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003817 if (task_ctx_data) {
3818 ctx->task_ctx_data = task_ctx_data;
3819 task_ctx_data = NULL;
3820 }
3821
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003822 err = 0;
3823 mutex_lock(&task->perf_event_mutex);
3824 /*
3825 * If it has already passed perf_event_exit_task().
3826 * we must see PF_EXITING, it takes this mutex too.
3827 */
3828 if (task->flags & PF_EXITING)
3829 err = -ESRCH;
3830 else if (task->perf_event_ctxp[ctxn])
3831 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003832 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003833 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003834 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003835 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003836 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003837 mutex_unlock(&task->perf_event_mutex);
3838
3839 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003840 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003841
3842 if (err == -EAGAIN)
3843 goto retry;
3844 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003845 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003846 }
3847
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003848 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003849 return ctx;
3850
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003851errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003852 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003853 return ERR_PTR(err);
3854}
3855
Li Zefan6fb29152009-10-15 11:21:42 +08003856static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003857static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003858
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003859static void free_event_rcu(struct rcu_head *head)
3860{
3861 struct perf_event *event;
3862
3863 event = container_of(head, struct perf_event, rcu_head);
3864 if (event->ns)
3865 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003866 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003867 kfree(event);
3868}
3869
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003870static void ring_buffer_attach(struct perf_event *event,
3871 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003872
Kan Liangf2fb6be2016-03-23 11:24:37 -07003873static void detach_sb_event(struct perf_event *event)
3874{
3875 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
3876
3877 raw_spin_lock(&pel->lock);
3878 list_del_rcu(&event->sb_list);
3879 raw_spin_unlock(&pel->lock);
3880}
3881
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003882static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07003883{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003884 struct perf_event_attr *attr = &event->attr;
3885
Kan Liangf2fb6be2016-03-23 11:24:37 -07003886 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003887 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003888
3889 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003890 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003891
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003892 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
3893 attr->comm || attr->comm_exec ||
3894 attr->task ||
3895 attr->context_switch)
3896 return true;
3897 return false;
3898}
3899
3900static void unaccount_pmu_sb_event(struct perf_event *event)
3901{
3902 if (is_sb_event(event))
3903 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003904}
3905
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003906static void unaccount_event_cpu(struct perf_event *event, int cpu)
3907{
3908 if (event->parent)
3909 return;
3910
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003911 if (is_cgroup_event(event))
3912 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3913}
3914
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003915#ifdef CONFIG_NO_HZ_FULL
3916static DEFINE_SPINLOCK(nr_freq_lock);
3917#endif
3918
3919static void unaccount_freq_event_nohz(void)
3920{
3921#ifdef CONFIG_NO_HZ_FULL
3922 spin_lock(&nr_freq_lock);
3923 if (atomic_dec_and_test(&nr_freq_events))
3924 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3925 spin_unlock(&nr_freq_lock);
3926#endif
3927}
3928
3929static void unaccount_freq_event(void)
3930{
3931 if (tick_nohz_full_enabled())
3932 unaccount_freq_event_nohz();
3933 else
3934 atomic_dec(&nr_freq_events);
3935}
3936
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003937static void unaccount_event(struct perf_event *event)
3938{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003939 bool dec = false;
3940
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003941 if (event->parent)
3942 return;
3943
3944 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003945 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003946 if (event->attr.mmap || event->attr.mmap_data)
3947 atomic_dec(&nr_mmap_events);
3948 if (event->attr.comm)
3949 atomic_dec(&nr_comm_events);
3950 if (event->attr.task)
3951 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003952 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003953 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003954 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003955 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003956 atomic_dec(&nr_switch_events);
3957 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003958 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003959 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003960 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003961 dec = true;
3962
Peter Zijlstra9107c892016-02-24 18:45:45 +01003963 if (dec) {
3964 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3965 schedule_delayed_work(&perf_sched_work, HZ);
3966 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003967
3968 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003969
3970 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003971}
3972
Peter Zijlstra9107c892016-02-24 18:45:45 +01003973static void perf_sched_delayed(struct work_struct *work)
3974{
3975 mutex_lock(&perf_sched_mutex);
3976 if (atomic_dec_and_test(&perf_sched_count))
3977 static_branch_disable(&perf_sched_events);
3978 mutex_unlock(&perf_sched_mutex);
3979}
3980
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003981/*
3982 * The following implement mutual exclusion of events on "exclusive" pmus
3983 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3984 * at a time, so we disallow creating events that might conflict, namely:
3985 *
3986 * 1) cpu-wide events in the presence of per-task events,
3987 * 2) per-task events in the presence of cpu-wide events,
3988 * 3) two matching events on the same context.
3989 *
3990 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003991 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003992 */
3993static int exclusive_event_init(struct perf_event *event)
3994{
3995 struct pmu *pmu = event->pmu;
3996
3997 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3998 return 0;
3999
4000 /*
4001 * Prevent co-existence of per-task and cpu-wide events on the
4002 * same exclusive pmu.
4003 *
4004 * Negative pmu::exclusive_cnt means there are cpu-wide
4005 * events on this "exclusive" pmu, positive means there are
4006 * per-task events.
4007 *
4008 * Since this is called in perf_event_alloc() path, event::ctx
4009 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
4010 * to mean "per-task event", because unlike other attach states it
4011 * never gets cleared.
4012 */
4013 if (event->attach_state & PERF_ATTACH_TASK) {
4014 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
4015 return -EBUSY;
4016 } else {
4017 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
4018 return -EBUSY;
4019 }
4020
4021 return 0;
4022}
4023
4024static void exclusive_event_destroy(struct perf_event *event)
4025{
4026 struct pmu *pmu = event->pmu;
4027
4028 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4029 return;
4030
4031 /* see comment in exclusive_event_init() */
4032 if (event->attach_state & PERF_ATTACH_TASK)
4033 atomic_dec(&pmu->exclusive_cnt);
4034 else
4035 atomic_inc(&pmu->exclusive_cnt);
4036}
4037
4038static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
4039{
Alexander Shishkin3bf62152016-09-20 18:48:11 +03004040 if ((e1->pmu == e2->pmu) &&
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004041 (e1->cpu == e2->cpu ||
4042 e1->cpu == -1 ||
4043 e2->cpu == -1))
4044 return true;
4045 return false;
4046}
4047
4048/* Called under the same ctx::mutex as perf_install_in_context() */
4049static bool exclusive_event_installable(struct perf_event *event,
4050 struct perf_event_context *ctx)
4051{
4052 struct perf_event *iter_event;
4053 struct pmu *pmu = event->pmu;
4054
4055 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4056 return true;
4057
4058 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
4059 if (exclusive_event_match(iter_event, event))
4060 return false;
4061 }
4062
4063 return true;
4064}
4065
Alexander Shishkin375637b2016-04-27 18:44:46 +03004066static void perf_addr_filters_splice(struct perf_event *event,
4067 struct list_head *head);
4068
Peter Zijlstra683ede42014-05-05 12:11:24 +02004069static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004070{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004071 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004072
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004073 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004074
Frederic Weisbecker76369132011-05-19 19:55:04 +02004075 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004076 /*
4077 * Can happen when we close an event with re-directed output.
4078 *
4079 * Since we have a 0 refcount, perf_mmap_close() will skip
4080 * over us; possibly making our ring_buffer_put() the last.
4081 */
4082 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004083 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004084 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004085 }
4086
Stephane Eraniane5d13672011-02-14 11:20:01 +02004087 if (is_cgroup_event(event))
4088 perf_detach_cgroup(event);
4089
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004090 if (!event->parent) {
4091 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
4092 put_callchain_buffers();
4093 }
4094
4095 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03004096 perf_addr_filters_splice(event, NULL);
4097 kfree(event->addr_filters_offs);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004098
4099 if (event->destroy)
4100 event->destroy(event);
4101
4102 if (event->ctx)
4103 put_ctx(event->ctx);
4104
Prashant Bholeb951ffb2018-04-09 19:03:46 +09004105 if (event->hw.target)
4106 put_task_struct(event->hw.target);
4107
Alexander Shishkin62a92c82016-06-07 15:44:15 +03004108 exclusive_event_destroy(event);
4109 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004110
4111 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112}
4113
Peter Zijlstra683ede42014-05-05 12:11:24 +02004114/*
4115 * Used to free events which have a known refcount of 1, such as in error paths
4116 * where the event isn't exposed yet and inherited events.
4117 */
4118static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004119{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004120 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4121 "unexpected event refcount: %ld; ptr=%p\n",
4122 atomic_long_read(&event->refcount), event)) {
4123 /* leak to avoid use-after-free */
4124 return;
4125 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004126
Peter Zijlstra683ede42014-05-05 12:11:24 +02004127 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004128}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004129
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004130/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004131 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004132 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004133static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004134{
Peter Zijlstra88821352010-11-09 19:01:43 +01004135 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004136
Peter Zijlstra88821352010-11-09 19:01:43 +01004137 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004138 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004139 * Matches the smp_store_release() in perf_event_exit_task(). If we
4140 * observe !owner it means the list deletion is complete and we can
4141 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004142 * owner->perf_event_mutex.
4143 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004144 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004145 if (owner) {
4146 /*
4147 * Since delayed_put_task_struct() also drops the last
4148 * task reference we can safely take a new reference
4149 * while holding the rcu_read_lock().
4150 */
4151 get_task_struct(owner);
4152 }
4153 rcu_read_unlock();
4154
4155 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004156 /*
4157 * If we're here through perf_event_exit_task() we're already
4158 * holding ctx->mutex which would be an inversion wrt. the
4159 * normal lock order.
4160 *
4161 * However we can safely take this lock because its the child
4162 * ctx->mutex.
4163 */
4164 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4165
Peter Zijlstra88821352010-11-09 19:01:43 +01004166 /*
4167 * We have to re-check the event->owner field, if it is cleared
4168 * we raced with perf_event_exit_task(), acquiring the mutex
4169 * ensured they're done, and we can proceed with freeing the
4170 * event.
4171 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004172 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004173 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004174 smp_store_release(&event->owner, NULL);
4175 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004176 mutex_unlock(&owner->perf_event_mutex);
4177 put_task_struct(owner);
4178 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004179}
4180
Jiri Olsaf8697762014-08-01 14:33:01 +02004181static void put_event(struct perf_event *event)
4182{
Jiri Olsaf8697762014-08-01 14:33:01 +02004183 if (!atomic_long_dec_and_test(&event->refcount))
4184 return;
4185
Peter Zijlstra683ede42014-05-05 12:11:24 +02004186 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004187}
4188
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004189/*
4190 * Kill an event dead; while event:refcount will preserve the event
4191 * object, it will not preserve its functionality. Once the last 'user'
4192 * gives up the object, we'll destroy the thing.
4193 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004194int perf_event_release_kernel(struct perf_event *event)
4195{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004196 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004197 struct perf_event *child, *tmp;
4198
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004199 /*
4200 * If we got here through err_file: fput(event_file); we will not have
4201 * attached to a context yet.
4202 */
4203 if (!ctx) {
4204 WARN_ON_ONCE(event->attach_state &
4205 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4206 goto no_ctx;
4207 }
4208
Peter Zijlstra88821352010-11-09 19:01:43 +01004209 if (!is_kernel_event(event))
4210 perf_remove_from_owner(event);
4211
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004212 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004213 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004214 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004215
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004216 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004217 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004218 * Mark this even as STATE_DEAD, there is no external reference to it
4219 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004220 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004221 * Anybody acquiring event->child_mutex after the below loop _must_
4222 * also see this, most importantly inherit_event() which will avoid
4223 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004224 *
4225 * Thus this guarantees that we will in fact observe and kill _ALL_
4226 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004227 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004228 event->state = PERF_EVENT_STATE_DEAD;
4229 raw_spin_unlock_irq(&ctx->lock);
4230
4231 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004232
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004233again:
4234 mutex_lock(&event->child_mutex);
4235 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004236
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004237 /*
4238 * Cannot change, child events are not migrated, see the
4239 * comment with perf_event_ctx_lock_nested().
4240 */
4241 ctx = lockless_dereference(child->ctx);
4242 /*
4243 * Since child_mutex nests inside ctx::mutex, we must jump
4244 * through hoops. We start by grabbing a reference on the ctx.
4245 *
4246 * Since the event cannot get freed while we hold the
4247 * child_mutex, the context must also exist and have a !0
4248 * reference count.
4249 */
4250 get_ctx(ctx);
4251
4252 /*
4253 * Now that we have a ctx ref, we can drop child_mutex, and
4254 * acquire ctx::mutex without fear of it going away. Then we
4255 * can re-acquire child_mutex.
4256 */
4257 mutex_unlock(&event->child_mutex);
4258 mutex_lock(&ctx->mutex);
4259 mutex_lock(&event->child_mutex);
4260
4261 /*
4262 * Now that we hold ctx::mutex and child_mutex, revalidate our
4263 * state, if child is still the first entry, it didn't get freed
4264 * and we can continue doing so.
4265 */
4266 tmp = list_first_entry_or_null(&event->child_list,
4267 struct perf_event, child_list);
4268 if (tmp == child) {
4269 perf_remove_from_context(child, DETACH_GROUP);
4270 list_del(&child->child_list);
4271 free_event(child);
4272 /*
4273 * This matches the refcount bump in inherit_event();
4274 * this can't be the last reference.
4275 */
4276 put_event(event);
4277 }
4278
4279 mutex_unlock(&event->child_mutex);
4280 mutex_unlock(&ctx->mutex);
4281 put_ctx(ctx);
4282 goto again;
4283 }
4284 mutex_unlock(&event->child_mutex);
4285
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004286no_ctx:
4287 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004288 return 0;
4289}
4290EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4291
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004292/*
4293 * Called when the last reference to the file is gone.
4294 */
Al Viroa6fa9412012-08-20 14:59:25 +01004295static int perf_release(struct inode *inode, struct file *file)
4296{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004297 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004298 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004299}
4300
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004301u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302{
4303 struct perf_event *child;
4304 u64 total = 0;
4305
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004306 *enabled = 0;
4307 *running = 0;
4308
Peter Zijlstra6f105812009-11-20 22:19:56 +01004309 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004310
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004311 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004312 total += perf_event_count(event);
4313
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004314 *enabled += event->total_time_enabled +
4315 atomic64_read(&event->child_total_time_enabled);
4316 *running += event->total_time_running +
4317 atomic64_read(&event->child_total_time_running);
4318
4319 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004320 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004321 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004322 *enabled += child->total_time_enabled;
4323 *running += child->total_time_running;
4324 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004325 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004326
4327 return total;
4328}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004329EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004330
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004331static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004332 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004333{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004334 struct perf_event *sub;
4335 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004336 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004337
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004338 ret = perf_event_read(leader, true);
4339 if (ret)
4340 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004341
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004342 /*
4343 * Since we co-schedule groups, {enabled,running} times of siblings
4344 * will be identical to those of the leader, so we only publish one
4345 * set.
4346 */
4347 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4348 values[n++] += leader->total_time_enabled +
4349 atomic64_read(&leader->child_total_time_enabled);
4350 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004351
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004352 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4353 values[n++] += leader->total_time_running +
4354 atomic64_read(&leader->child_total_time_running);
4355 }
4356
4357 /*
4358 * Write {count,id} tuples for every sibling.
4359 */
4360 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004361 if (read_format & PERF_FORMAT_ID)
4362 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004364 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004365 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004366 if (read_format & PERF_FORMAT_ID)
4367 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004369
4370 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004371}
4372
4373static int perf_read_group(struct perf_event *event,
4374 u64 read_format, char __user *buf)
4375{
4376 struct perf_event *leader = event->group_leader, *child;
4377 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004378 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004379 u64 *values;
4380
4381 lockdep_assert_held(&ctx->mutex);
4382
4383 values = kzalloc(event->read_size, GFP_KERNEL);
4384 if (!values)
4385 return -ENOMEM;
4386
4387 values[0] = 1 + leader->nr_siblings;
4388
4389 /*
4390 * By locking the child_mutex of the leader we effectively
4391 * lock the child list of all siblings.. XXX explain how.
4392 */
4393 mutex_lock(&leader->child_mutex);
4394
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004395 ret = __perf_read_group_add(leader, read_format, values);
4396 if (ret)
4397 goto unlock;
4398
4399 list_for_each_entry(child, &leader->child_list, child_list) {
4400 ret = __perf_read_group_add(child, read_format, values);
4401 if (ret)
4402 goto unlock;
4403 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004404
4405 mutex_unlock(&leader->child_mutex);
4406
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004407 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004408 if (copy_to_user(buf, values, event->read_size))
4409 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004410 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004411
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004412unlock:
4413 mutex_unlock(&leader->child_mutex);
4414out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004415 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004416 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004417}
4418
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004419static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004420 u64 read_format, char __user *buf)
4421{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004422 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004423 u64 values[4];
4424 int n = 0;
4425
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004426 values[n++] = perf_event_read_value(event, &enabled, &running);
4427 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4428 values[n++] = enabled;
4429 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4430 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431 if (read_format & PERF_FORMAT_ID)
4432 values[n++] = primary_event_id(event);
4433
4434 if (copy_to_user(buf, values, n * sizeof(u64)))
4435 return -EFAULT;
4436
4437 return n * sizeof(u64);
4438}
4439
Jiri Olsadc633982014-09-12 13:18:26 +02004440static bool is_event_hup(struct perf_event *event)
4441{
4442 bool no_children;
4443
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004444 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004445 return false;
4446
4447 mutex_lock(&event->child_mutex);
4448 no_children = list_empty(&event->child_list);
4449 mutex_unlock(&event->child_mutex);
4450 return no_children;
4451}
4452
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004453/*
4454 * Read the performance event - simple non blocking version for now
4455 */
4456static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004457__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004458{
4459 u64 read_format = event->attr.read_format;
4460 int ret;
4461
4462 /*
4463 * Return end-of-file for a read on a event that is in
4464 * error state (i.e. because it was pinned but it couldn't be
4465 * scheduled on to the CPU at some point).
4466 */
4467 if (event->state == PERF_EVENT_STATE_ERROR)
4468 return 0;
4469
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004470 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004471 return -ENOSPC;
4472
4473 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004474 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004475 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004476 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004477 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004478
4479 return ret;
4480}
4481
4482static ssize_t
4483perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4484{
4485 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004486 struct perf_event_context *ctx;
4487 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004488
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004489 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004490 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004491 perf_event_ctx_unlock(event, ctx);
4492
4493 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494}
4495
4496static unsigned int perf_poll(struct file *file, poll_table *wait)
4497{
4498 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004499 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004500 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004501
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004502 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004503
Jiri Olsadc633982014-09-12 13:18:26 +02004504 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004505 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004507 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004508 * Pin the event->rb by taking event->mmap_mutex; otherwise
4509 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004510 */
4511 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004512 rb = event->rb;
4513 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004514 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004515 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004516 return events;
4517}
4518
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004519static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004520{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004521 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004522 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004523 perf_event_update_userpage(event);
4524}
4525
4526/*
4527 * Holding the top-level event's child_mutex means that any
4528 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004529 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530 * task existence requirements of perf_event_enable/disable.
4531 */
4532static void perf_event_for_each_child(struct perf_event *event,
4533 void (*func)(struct perf_event *))
4534{
4535 struct perf_event *child;
4536
4537 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004538
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004539 mutex_lock(&event->child_mutex);
4540 func(event);
4541 list_for_each_entry(child, &event->child_list, child_list)
4542 func(child);
4543 mutex_unlock(&event->child_mutex);
4544}
4545
4546static void perf_event_for_each(struct perf_event *event,
4547 void (*func)(struct perf_event *))
4548{
4549 struct perf_event_context *ctx = event->ctx;
4550 struct perf_event *sibling;
4551
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004552 lockdep_assert_held(&ctx->mutex);
4553
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004554 event = event->group_leader;
4555
4556 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004557 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004558 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004559}
4560
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004561static void __perf_event_period(struct perf_event *event,
4562 struct perf_cpu_context *cpuctx,
4563 struct perf_event_context *ctx,
4564 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004565{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004566 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004567 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004568
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004569 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004570 event->attr.sample_freq = value;
4571 } else {
4572 event->attr.sample_period = value;
4573 event->hw.sample_period = value;
4574 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004575
4576 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4577 if (active) {
4578 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004579 /*
4580 * We could be throttled; unthrottle now to avoid the tick
4581 * trying to unthrottle while we already re-started the event.
4582 */
4583 if (event->hw.interrupts == MAX_INTERRUPTS) {
4584 event->hw.interrupts = 0;
4585 perf_log_throttle(event, 1);
4586 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004587 event->pmu->stop(event, PERF_EF_UPDATE);
4588 }
4589
4590 local64_set(&event->hw.period_left, 0);
4591
4592 if (active) {
4593 event->pmu->start(event, PERF_EF_RELOAD);
4594 perf_pmu_enable(ctx->pmu);
4595 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004596}
4597
4598static int perf_event_period(struct perf_event *event, u64 __user *arg)
4599{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004600 u64 value;
4601
4602 if (!is_sampling_event(event))
4603 return -EINVAL;
4604
4605 if (copy_from_user(&value, arg, sizeof(value)))
4606 return -EFAULT;
4607
4608 if (!value)
4609 return -EINVAL;
4610
4611 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4612 return -EINVAL;
4613
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004614 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004615
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004616 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004617}
4618
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004619static const struct file_operations perf_fops;
4620
Al Viro2903ff02012-08-28 12:52:22 -04004621static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004622{
Al Viro2903ff02012-08-28 12:52:22 -04004623 struct fd f = fdget(fd);
4624 if (!f.file)
4625 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004626
Al Viro2903ff02012-08-28 12:52:22 -04004627 if (f.file->f_op != &perf_fops) {
4628 fdput(f);
4629 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004630 }
Al Viro2903ff02012-08-28 12:52:22 -04004631 *p = f;
4632 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004633}
4634
4635static int perf_event_set_output(struct perf_event *event,
4636 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004637static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004638static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004639
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004640static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004641{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642 void (*func)(struct perf_event *);
4643 u32 flags = arg;
4644
4645 switch (cmd) {
4646 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004647 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004648 break;
4649 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004650 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004651 break;
4652 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004653 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004654 break;
4655
4656 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004657 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004658
4659 case PERF_EVENT_IOC_PERIOD:
4660 return perf_event_period(event, (u64 __user *)arg);
4661
Jiri Olsacf4957f2012-10-24 13:37:58 +02004662 case PERF_EVENT_IOC_ID:
4663 {
4664 u64 id = primary_event_id(event);
4665
4666 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4667 return -EFAULT;
4668 return 0;
4669 }
4670
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004671 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004672 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004673 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004674 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004675 struct perf_event *output_event;
4676 struct fd output;
4677 ret = perf_fget_light(arg, &output);
4678 if (ret)
4679 return ret;
4680 output_event = output.file->private_data;
4681 ret = perf_event_set_output(event, output_event);
4682 fdput(output);
4683 } else {
4684 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004685 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004686 return ret;
4687 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004688
Li Zefan6fb29152009-10-15 11:21:42 +08004689 case PERF_EVENT_IOC_SET_FILTER:
4690 return perf_event_set_filter(event, (void __user *)arg);
4691
Alexei Starovoitov25415172015-03-25 12:49:20 -07004692 case PERF_EVENT_IOC_SET_BPF:
4693 return perf_event_set_bpf_prog(event, arg);
4694
Wang Nan86e79722016-03-28 06:41:29 +00004695 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4696 struct ring_buffer *rb;
4697
4698 rcu_read_lock();
4699 rb = rcu_dereference(event->rb);
4700 if (!rb || !rb->nr_pages) {
4701 rcu_read_unlock();
4702 return -EINVAL;
4703 }
4704 rb_toggle_paused(rb, !!arg);
4705 rcu_read_unlock();
4706 return 0;
4707 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004708 default:
4709 return -ENOTTY;
4710 }
4711
4712 if (flags & PERF_IOC_FLAG_GROUP)
4713 perf_event_for_each(event, func);
4714 else
4715 perf_event_for_each_child(event, func);
4716
4717 return 0;
4718}
4719
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004720static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4721{
4722 struct perf_event *event = file->private_data;
4723 struct perf_event_context *ctx;
4724 long ret;
4725
4726 ctx = perf_event_ctx_lock(event);
4727 ret = _perf_ioctl(event, cmd, arg);
4728 perf_event_ctx_unlock(event, ctx);
4729
4730 return ret;
4731}
4732
Pawel Mollb3f20782014-06-13 16:03:32 +01004733#ifdef CONFIG_COMPAT
4734static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4735 unsigned long arg)
4736{
4737 switch (_IOC_NR(cmd)) {
4738 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4739 case _IOC_NR(PERF_EVENT_IOC_ID):
4740 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4741 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4742 cmd &= ~IOCSIZE_MASK;
4743 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4744 }
4745 break;
4746 }
4747 return perf_ioctl(file, cmd, arg);
4748}
4749#else
4750# define perf_compat_ioctl NULL
4751#endif
4752
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004753int perf_event_task_enable(void)
4754{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004755 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004756 struct perf_event *event;
4757
4758 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004759 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4760 ctx = perf_event_ctx_lock(event);
4761 perf_event_for_each_child(event, _perf_event_enable);
4762 perf_event_ctx_unlock(event, ctx);
4763 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764 mutex_unlock(&current->perf_event_mutex);
4765
4766 return 0;
4767}
4768
4769int perf_event_task_disable(void)
4770{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004771 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004772 struct perf_event *event;
4773
4774 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004775 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4776 ctx = perf_event_ctx_lock(event);
4777 perf_event_for_each_child(event, _perf_event_disable);
4778 perf_event_ctx_unlock(event, ctx);
4779 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004780 mutex_unlock(&current->perf_event_mutex);
4781
4782 return 0;
4783}
4784
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004785static int perf_event_index(struct perf_event *event)
4786{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004787 if (event->hw.state & PERF_HES_STOPPED)
4788 return 0;
4789
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004790 if (event->state != PERF_EVENT_STATE_ACTIVE)
4791 return 0;
4792
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004793 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004794}
4795
Eric B Munsonc4794292011-06-23 16:34:38 -04004796static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004797 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004798 u64 *enabled,
4799 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004800{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004801 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004802
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004803 *now = perf_clock();
4804 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004805 *enabled = ctx_time - event->tstamp_enabled;
4806 *running = ctx_time - event->tstamp_running;
4807}
4808
Peter Zijlstrafa731582013-09-19 10:16:42 +02004809static void perf_event_init_userpage(struct perf_event *event)
4810{
4811 struct perf_event_mmap_page *userpg;
4812 struct ring_buffer *rb;
4813
4814 rcu_read_lock();
4815 rb = rcu_dereference(event->rb);
4816 if (!rb)
4817 goto unlock;
4818
4819 userpg = rb->user_page;
4820
4821 /* Allow new userspace to detect that bit 0 is deprecated */
4822 userpg->cap_bit0_is_deprecated = 1;
4823 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004824 userpg->data_offset = PAGE_SIZE;
4825 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004826
4827unlock:
4828 rcu_read_unlock();
4829}
4830
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004831void __weak arch_perf_update_userpage(
4832 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004833{
4834}
4835
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004836/*
4837 * Callers need to ensure there can be no nesting of this function, otherwise
4838 * the seqlock logic goes bad. We can not serialize this because the arch
4839 * code calls this from NMI context.
4840 */
4841void perf_event_update_userpage(struct perf_event *event)
4842{
4843 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004844 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004845 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004846
4847 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004848 rb = rcu_dereference(event->rb);
4849 if (!rb)
4850 goto unlock;
4851
Eric B Munson0d641202011-06-24 12:26:26 -04004852 /*
4853 * compute total_time_enabled, total_time_running
4854 * based on snapshot values taken when the event
4855 * was last scheduled in.
4856 *
4857 * we cannot simply called update_context_time()
4858 * because of locking issue as we can be called in
4859 * NMI context
4860 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004861 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004862
Frederic Weisbecker76369132011-05-19 19:55:04 +02004863 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004864 /*
4865 * Disable preemption so as to not let the corresponding user-space
4866 * spin too long if we get preempted.
4867 */
4868 preempt_disable();
4869 ++userpg->lock;
4870 barrier();
4871 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004872 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004873 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004874 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004875
Eric B Munson0d641202011-06-24 12:26:26 -04004876 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004877 atomic64_read(&event->child_total_time_enabled);
4878
Eric B Munson0d641202011-06-24 12:26:26 -04004879 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004880 atomic64_read(&event->child_total_time_running);
4881
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004882 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004883
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004884 barrier();
4885 ++userpg->lock;
4886 preempt_enable();
4887unlock:
4888 rcu_read_unlock();
4889}
4890
Peter Zijlstra906010b2009-09-21 16:08:49 +02004891static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4892{
4893 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004894 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004895 int ret = VM_FAULT_SIGBUS;
4896
4897 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4898 if (vmf->pgoff == 0)
4899 ret = 0;
4900 return ret;
4901 }
4902
4903 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004904 rb = rcu_dereference(event->rb);
4905 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004906 goto unlock;
4907
4908 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4909 goto unlock;
4910
Frederic Weisbecker76369132011-05-19 19:55:04 +02004911 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004912 if (!vmf->page)
4913 goto unlock;
4914
4915 get_page(vmf->page);
4916 vmf->page->mapping = vma->vm_file->f_mapping;
4917 vmf->page->index = vmf->pgoff;
4918
4919 ret = 0;
4920unlock:
4921 rcu_read_unlock();
4922
4923 return ret;
4924}
4925
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004926static void ring_buffer_attach(struct perf_event *event,
4927 struct ring_buffer *rb)
4928{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004929 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004930 unsigned long flags;
4931
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004932 if (event->rb) {
4933 /*
4934 * Should be impossible, we set this when removing
4935 * event->rb_entry and wait/clear when adding event->rb_entry.
4936 */
4937 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004938
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004939 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004940 spin_lock_irqsave(&old_rb->event_lock, flags);
4941 list_del_rcu(&event->rb_entry);
4942 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004943
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004944 event->rcu_batches = get_state_synchronize_rcu();
4945 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004946 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004947
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004948 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004949 if (event->rcu_pending) {
4950 cond_synchronize_rcu(event->rcu_batches);
4951 event->rcu_pending = 0;
4952 }
4953
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004954 spin_lock_irqsave(&rb->event_lock, flags);
4955 list_add_rcu(&event->rb_entry, &rb->event_list);
4956 spin_unlock_irqrestore(&rb->event_lock, flags);
4957 }
4958
Alexander Shishkin767ae082016-09-06 16:23:49 +03004959 /*
4960 * Avoid racing with perf_mmap_close(AUX): stop the event
4961 * before swizzling the event::rb pointer; if it's getting
4962 * unmapped, its aux_mmap_count will be 0 and it won't
4963 * restart. See the comment in __perf_pmu_output_stop().
4964 *
4965 * Data will inevitably be lost when set_output is done in
4966 * mid-air, but then again, whoever does it like this is
4967 * not in for the data anyway.
4968 */
4969 if (has_aux(event))
4970 perf_event_stop(event, 0);
4971
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004972 rcu_assign_pointer(event->rb, rb);
4973
4974 if (old_rb) {
4975 ring_buffer_put(old_rb);
4976 /*
4977 * Since we detached before setting the new rb, so that we
4978 * could attach the new rb, we could have missed a wakeup.
4979 * Provide it now.
4980 */
4981 wake_up_all(&event->waitq);
4982 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004983}
4984
4985static void ring_buffer_wakeup(struct perf_event *event)
4986{
4987 struct ring_buffer *rb;
4988
4989 rcu_read_lock();
4990 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004991 if (rb) {
4992 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4993 wake_up_all(&event->waitq);
4994 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004995 rcu_read_unlock();
4996}
4997
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004998struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004999{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005000 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005001
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005002 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005003 rb = rcu_dereference(event->rb);
5004 if (rb) {
5005 if (!atomic_inc_not_zero(&rb->refcount))
5006 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005007 }
5008 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005009
Frederic Weisbecker76369132011-05-19 19:55:04 +02005010 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005011}
5012
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005013void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005014{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005015 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005016 return;
5017
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005018 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005019
Frederic Weisbecker76369132011-05-19 19:55:04 +02005020 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005021}
5022
5023static void perf_mmap_open(struct vm_area_struct *vma)
5024{
5025 struct perf_event *event = vma->vm_file->private_data;
5026
5027 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005028 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005029
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005030 if (vma->vm_pgoff)
5031 atomic_inc(&event->rb->aux_mmap_count);
5032
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005033 if (event->pmu->event_mapped)
5034 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005035}
5036
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005037static void perf_pmu_output_stop(struct perf_event *event);
5038
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005039/*
5040 * A buffer can be mmap()ed multiple times; either directly through the same
5041 * event, or through other events by use of perf_event_set_output().
5042 *
5043 * In order to undo the VM accounting done by perf_mmap() we need to destroy
5044 * the buffer here, where we still have a VM context. This means we need
5045 * to detach all events redirecting to us.
5046 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005047static void perf_mmap_close(struct vm_area_struct *vma)
5048{
5049 struct perf_event *event = vma->vm_file->private_data;
5050
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005051 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005052 struct user_struct *mmap_user = rb->mmap_user;
5053 int mmap_locked = rb->mmap_locked;
5054 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005055
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005056 if (event->pmu->event_unmapped)
5057 event->pmu->event_unmapped(event);
5058
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005059 /*
5060 * rb->aux_mmap_count will always drop before rb->mmap_count and
5061 * event->mmap_count, so it is ok to use event->mmap_mutex to
5062 * serialize with perf_mmap here.
5063 */
5064 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5065 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005066 /*
5067 * Stop all AUX events that are writing to this buffer,
5068 * so that we can free its AUX pages and corresponding PMU
5069 * data. Note that after rb::aux_mmap_count dropped to zero,
5070 * they won't start any more (see perf_aux_output_begin()).
5071 */
5072 perf_pmu_output_stop(event);
5073
5074 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005075 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
5076 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
5077
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005078 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005079 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005080 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
5081
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005082 mutex_unlock(&event->mmap_mutex);
5083 }
5084
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005085 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005086
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005087 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005088 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005089
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005090 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005091 mutex_unlock(&event->mmap_mutex);
5092
5093 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005094 if (atomic_read(&rb->mmap_count))
5095 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005096
5097 /*
5098 * No other mmap()s, detach from all other events that might redirect
5099 * into the now unreachable buffer. Somewhat complicated by the
5100 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5101 */
5102again:
5103 rcu_read_lock();
5104 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5105 if (!atomic_long_inc_not_zero(&event->refcount)) {
5106 /*
5107 * This event is en-route to free_event() which will
5108 * detach it and remove it from the list.
5109 */
5110 continue;
5111 }
5112 rcu_read_unlock();
5113
5114 mutex_lock(&event->mmap_mutex);
5115 /*
5116 * Check we didn't race with perf_event_set_output() which can
5117 * swizzle the rb from under us while we were waiting to
5118 * acquire mmap_mutex.
5119 *
5120 * If we find a different rb; ignore this event, a next
5121 * iteration will no longer find it on the list. We have to
5122 * still restart the iteration to make sure we're not now
5123 * iterating the wrong list.
5124 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005125 if (event->rb == rb)
5126 ring_buffer_attach(event, NULL);
5127
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005128 mutex_unlock(&event->mmap_mutex);
5129 put_event(event);
5130
5131 /*
5132 * Restart the iteration; either we're on the wrong list or
5133 * destroyed its integrity by doing a deletion.
5134 */
5135 goto again;
5136 }
5137 rcu_read_unlock();
5138
5139 /*
5140 * It could be there's still a few 0-ref events on the list; they'll
5141 * get cleaned up by free_event() -- they'll also still have their
5142 * ref on the rb and will free it whenever they are done with it.
5143 *
5144 * Aside from that, this buffer is 'fully' detached and unmapped,
5145 * undo the VM accounting.
5146 */
5147
5148 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
5149 vma->vm_mm->pinned_vm -= mmap_locked;
5150 free_uid(mmap_user);
5151
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005152out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005153 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005154}
5155
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04005156static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005157 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005158 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005159 .fault = perf_mmap_fault,
5160 .page_mkwrite = perf_mmap_fault,
5161};
5162
5163static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5164{
5165 struct perf_event *event = file->private_data;
5166 unsigned long user_locked, user_lock_limit;
5167 struct user_struct *user = current_user();
5168 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005169 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005170 unsigned long vma_size;
5171 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005172 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005173 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005174
Peter Zijlstrac7920612010-05-18 10:33:24 +02005175 /*
5176 * Don't allow mmap() of inherited per-task counters. This would
5177 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005178 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005179 */
5180 if (event->cpu == -1 && event->attr.inherit)
5181 return -EINVAL;
5182
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005183 if (!(vma->vm_flags & VM_SHARED))
5184 return -EINVAL;
5185
5186 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005187
5188 if (vma->vm_pgoff == 0) {
5189 nr_pages = (vma_size / PAGE_SIZE) - 1;
5190 } else {
5191 /*
5192 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5193 * mapped, all subsequent mappings should have the same size
5194 * and offset. Must be above the normal perf buffer.
5195 */
5196 u64 aux_offset, aux_size;
5197
5198 if (!event->rb)
5199 return -EINVAL;
5200
5201 nr_pages = vma_size / PAGE_SIZE;
5202
5203 mutex_lock(&event->mmap_mutex);
5204 ret = -EINVAL;
5205
5206 rb = event->rb;
5207 if (!rb)
5208 goto aux_unlock;
5209
5210 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
5211 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
5212
5213 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5214 goto aux_unlock;
5215
5216 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5217 goto aux_unlock;
5218
5219 /* already mapped with a different offset */
5220 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5221 goto aux_unlock;
5222
5223 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5224 goto aux_unlock;
5225
5226 /* already mapped with a different size */
5227 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5228 goto aux_unlock;
5229
5230 if (!is_power_of_2(nr_pages))
5231 goto aux_unlock;
5232
5233 if (!atomic_inc_not_zero(&rb->mmap_count))
5234 goto aux_unlock;
5235
5236 if (rb_has_aux(rb)) {
5237 atomic_inc(&rb->aux_mmap_count);
5238 ret = 0;
5239 goto unlock;
5240 }
5241
5242 atomic_set(&rb->aux_mmap_count, 1);
5243 user_extra = nr_pages;
5244
5245 goto accounting;
5246 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247
5248 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005249 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005250 * can do bitmasks instead of modulo.
5251 */
Kan Liang2ed11312015-03-02 02:14:26 -05005252 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005253 return -EINVAL;
5254
5255 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5256 return -EINVAL;
5257
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005258 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005259again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005261 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005262 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005263 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005264 goto unlock;
5265 }
5266
5267 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5268 /*
5269 * Raced against perf_mmap_close() through
5270 * perf_event_set_output(). Try again, hope for better
5271 * luck.
5272 */
5273 mutex_unlock(&event->mmap_mutex);
5274 goto again;
5275 }
5276
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005277 goto unlock;
5278 }
5279
5280 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005281
5282accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005283 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5284
5285 /*
5286 * Increase the limit linearly with more CPUs:
5287 */
5288 user_lock_limit *= num_online_cpus();
5289
5290 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5291
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005292 if (user_locked > user_lock_limit)
5293 extra = user_locked - user_lock_limit;
5294
Jiri Slaby78d7d402010-03-05 13:42:54 -08005295 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005296 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005297 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005298
5299 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5300 !capable(CAP_IPC_LOCK)) {
5301 ret = -EPERM;
5302 goto unlock;
5303 }
5304
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005305 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005306
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005307 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005308 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005309
Frederic Weisbecker76369132011-05-19 19:55:04 +02005310 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005311 rb = rb_alloc(nr_pages,
5312 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5313 event->cpu, flags);
5314
5315 if (!rb) {
5316 ret = -ENOMEM;
5317 goto unlock;
5318 }
5319
5320 atomic_set(&rb->mmap_count, 1);
5321 rb->mmap_user = get_current_user();
5322 rb->mmap_locked = extra;
5323
5324 ring_buffer_attach(event, rb);
5325
5326 perf_event_init_userpage(event);
5327 perf_event_update_userpage(event);
5328 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005329 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5330 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005331 if (!ret)
5332 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005333 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005334
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005335unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005336 if (!ret) {
5337 atomic_long_add(user_extra, &user->locked_vm);
5338 vma->vm_mm->pinned_vm += extra;
5339
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005340 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005341 } else if (rb) {
5342 atomic_dec(&rb->mmap_count);
5343 }
5344aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345 mutex_unlock(&event->mmap_mutex);
5346
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005347 /*
5348 * Since pinned accounting is per vm we cannot allow fork() to copy our
5349 * vma.
5350 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005351 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005352 vma->vm_ops = &perf_mmap_vmops;
5353
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005354 if (event->pmu->event_mapped)
5355 event->pmu->event_mapped(event);
5356
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005357 return ret;
5358}
5359
5360static int perf_fasync(int fd, struct file *filp, int on)
5361{
Al Viro496ad9a2013-01-23 17:07:38 -05005362 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005363 struct perf_event *event = filp->private_data;
5364 int retval;
5365
Al Viro59551022016-01-22 15:40:57 -05005366 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005367 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005368 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005369
5370 if (retval < 0)
5371 return retval;
5372
5373 return 0;
5374}
5375
5376static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005377 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005378 .release = perf_release,
5379 .read = perf_read,
5380 .poll = perf_poll,
5381 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005382 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005383 .mmap = perf_mmap,
5384 .fasync = perf_fasync,
5385};
5386
5387/*
5388 * Perf event wakeup
5389 *
5390 * If there's data, ensure we set the poll() state and publish everything
5391 * to user-space before waking everybody up.
5392 */
5393
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005394static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5395{
5396 /* only the parent has fasync state */
5397 if (event->parent)
5398 event = event->parent;
5399 return &event->fasync;
5400}
5401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005402void perf_event_wakeup(struct perf_event *event)
5403{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005404 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005405
5406 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005407 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005408 event->pending_kill = 0;
5409 }
5410}
5411
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005412static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005413{
5414 struct perf_event *event = container_of(entry,
5415 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005416 int rctx;
5417
5418 rctx = perf_swevent_get_recursion_context();
5419 /*
5420 * If we 'fail' here, that's OK, it means recursion is already disabled
5421 * and we won't recurse 'further'.
5422 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423
5424 if (event->pending_disable) {
5425 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005426 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005427 }
5428
5429 if (event->pending_wakeup) {
5430 event->pending_wakeup = 0;
5431 perf_event_wakeup(event);
5432 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005433
5434 if (rctx >= 0)
5435 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005436}
5437
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005438/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005439 * We assume there is only KVM supporting the callbacks.
5440 * Later on, we might change it to a list if there is
5441 * another virtualization implementation supporting the callbacks.
5442 */
5443struct perf_guest_info_callbacks *perf_guest_cbs;
5444
5445int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5446{
5447 perf_guest_cbs = cbs;
5448 return 0;
5449}
5450EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5451
5452int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5453{
5454 perf_guest_cbs = NULL;
5455 return 0;
5456}
5457EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5458
Jiri Olsa40189942012-08-07 15:20:37 +02005459static void
5460perf_output_sample_regs(struct perf_output_handle *handle,
5461 struct pt_regs *regs, u64 mask)
5462{
5463 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305464 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02005465
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305466 bitmap_from_u64(_mask, mask);
5467 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02005468 u64 val;
5469
5470 val = perf_reg_value(regs, bit);
5471 perf_output_put(handle, val);
5472 }
5473}
5474
Stephane Eranian60e23642014-09-24 13:48:37 +02005475static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005476 struct pt_regs *regs,
5477 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005478{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005479 if (user_mode(regs)) {
5480 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005481 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005482 } else if (current->mm) {
5483 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005484 } else {
5485 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5486 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005487 }
5488}
5489
Stephane Eranian60e23642014-09-24 13:48:37 +02005490static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5491 struct pt_regs *regs)
5492{
5493 regs_intr->regs = regs;
5494 regs_intr->abi = perf_reg_abi(current);
5495}
5496
5497
Jiri Olsac5ebced2012-08-07 15:20:40 +02005498/*
5499 * Get remaining task size from user stack pointer.
5500 *
5501 * It'd be better to take stack vma map and limit this more
5502 * precisly, but there's no way to get it safely under interrupt,
5503 * so using TASK_SIZE as limit.
5504 */
5505static u64 perf_ustack_task_size(struct pt_regs *regs)
5506{
5507 unsigned long addr = perf_user_stack_pointer(regs);
5508
5509 if (!addr || addr >= TASK_SIZE)
5510 return 0;
5511
5512 return TASK_SIZE - addr;
5513}
5514
5515static u16
5516perf_sample_ustack_size(u16 stack_size, u16 header_size,
5517 struct pt_regs *regs)
5518{
5519 u64 task_size;
5520
5521 /* No regs, no stack pointer, no dump. */
5522 if (!regs)
5523 return 0;
5524
5525 /*
5526 * Check if we fit in with the requested stack size into the:
5527 * - TASK_SIZE
5528 * If we don't, we limit the size to the TASK_SIZE.
5529 *
5530 * - remaining sample size
5531 * If we don't, we customize the stack size to
5532 * fit in to the remaining sample size.
5533 */
5534
5535 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5536 stack_size = min(stack_size, (u16) task_size);
5537
5538 /* Current header size plus static size and dynamic size. */
5539 header_size += 2 * sizeof(u64);
5540
5541 /* Do we fit in with the current stack dump size? */
5542 if ((u16) (header_size + stack_size) < header_size) {
5543 /*
5544 * If we overflow the maximum size for the sample,
5545 * we customize the stack dump size to fit in.
5546 */
5547 stack_size = USHRT_MAX - header_size - sizeof(u64);
5548 stack_size = round_up(stack_size, sizeof(u64));
5549 }
5550
5551 return stack_size;
5552}
5553
5554static void
5555perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5556 struct pt_regs *regs)
5557{
5558 /* Case of a kernel thread, nothing to dump */
5559 if (!regs) {
5560 u64 size = 0;
5561 perf_output_put(handle, size);
5562 } else {
5563 unsigned long sp;
5564 unsigned int rem;
5565 u64 dyn_size;
Yabin Cuia64fbec2018-08-23 15:59:35 -07005566 mm_segment_t fs;
Jiri Olsac5ebced2012-08-07 15:20:40 +02005567
5568 /*
5569 * We dump:
5570 * static size
5571 * - the size requested by user or the best one we can fit
5572 * in to the sample max size
5573 * data
5574 * - user stack dump data
5575 * dynamic size
5576 * - the actual dumped size
5577 */
5578
5579 /* Static size. */
5580 perf_output_put(handle, dump_size);
5581
5582 /* Data. */
5583 sp = perf_user_stack_pointer(regs);
Yabin Cuia64fbec2018-08-23 15:59:35 -07005584 fs = get_fs();
5585 set_fs(USER_DS);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005586 rem = __output_copy_user(handle, (void *) sp, dump_size);
Yabin Cuia64fbec2018-08-23 15:59:35 -07005587 set_fs(fs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005588 dyn_size = dump_size - rem;
5589
5590 perf_output_skip(handle, rem);
5591
5592 /* Dynamic size. */
5593 perf_output_put(handle, dyn_size);
5594 }
5595}
5596
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005597static void __perf_event_header__init_id(struct perf_event_header *header,
5598 struct perf_sample_data *data,
5599 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005600{
5601 u64 sample_type = event->attr.sample_type;
5602
5603 data->type = sample_type;
5604 header->size += event->id_header_size;
5605
5606 if (sample_type & PERF_SAMPLE_TID) {
5607 /* namespace issues */
5608 data->tid_entry.pid = perf_event_pid(event, current);
5609 data->tid_entry.tid = perf_event_tid(event, current);
5610 }
5611
5612 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005613 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005614
Adrian Hunterff3d5272013-08-27 11:23:07 +03005615 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005616 data->id = primary_event_id(event);
5617
5618 if (sample_type & PERF_SAMPLE_STREAM_ID)
5619 data->stream_id = event->id;
5620
5621 if (sample_type & PERF_SAMPLE_CPU) {
5622 data->cpu_entry.cpu = raw_smp_processor_id();
5623 data->cpu_entry.reserved = 0;
5624 }
5625}
5626
Frederic Weisbecker76369132011-05-19 19:55:04 +02005627void perf_event_header__init_id(struct perf_event_header *header,
5628 struct perf_sample_data *data,
5629 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005630{
5631 if (event->attr.sample_id_all)
5632 __perf_event_header__init_id(header, data, event);
5633}
5634
5635static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5636 struct perf_sample_data *data)
5637{
5638 u64 sample_type = data->type;
5639
5640 if (sample_type & PERF_SAMPLE_TID)
5641 perf_output_put(handle, data->tid_entry);
5642
5643 if (sample_type & PERF_SAMPLE_TIME)
5644 perf_output_put(handle, data->time);
5645
5646 if (sample_type & PERF_SAMPLE_ID)
5647 perf_output_put(handle, data->id);
5648
5649 if (sample_type & PERF_SAMPLE_STREAM_ID)
5650 perf_output_put(handle, data->stream_id);
5651
5652 if (sample_type & PERF_SAMPLE_CPU)
5653 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005654
5655 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5656 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005657}
5658
Frederic Weisbecker76369132011-05-19 19:55:04 +02005659void perf_event__output_id_sample(struct perf_event *event,
5660 struct perf_output_handle *handle,
5661 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005662{
5663 if (event->attr.sample_id_all)
5664 __perf_event__output_id_sample(handle, sample);
5665}
5666
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005667static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005668 struct perf_event *event,
5669 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005670{
5671 u64 read_format = event->attr.read_format;
5672 u64 values[4];
5673 int n = 0;
5674
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005675 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005676 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005677 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005678 atomic64_read(&event->child_total_time_enabled);
5679 }
5680 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005681 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682 atomic64_read(&event->child_total_time_running);
5683 }
5684 if (read_format & PERF_FORMAT_ID)
5685 values[n++] = primary_event_id(event);
5686
Frederic Weisbecker76369132011-05-19 19:55:04 +02005687 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005688}
5689
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005690static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005691 struct perf_event *event,
5692 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005693{
5694 struct perf_event *leader = event->group_leader, *sub;
5695 u64 read_format = event->attr.read_format;
5696 u64 values[5];
5697 int n = 0;
5698
5699 values[n++] = 1 + leader->nr_siblings;
5700
5701 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005702 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005703
5704 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005705 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005706
Peter Zijlstrabc09bf82018-03-09 12:52:04 +01005707 if ((leader != event) &&
5708 (leader->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005709 leader->pmu->read(leader);
5710
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005711 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005712 if (read_format & PERF_FORMAT_ID)
5713 values[n++] = primary_event_id(leader);
5714
Frederic Weisbecker76369132011-05-19 19:55:04 +02005715 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005716
5717 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5718 n = 0;
5719
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005720 if ((sub != event) &&
5721 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005722 sub->pmu->read(sub);
5723
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005724 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005725 if (read_format & PERF_FORMAT_ID)
5726 values[n++] = primary_event_id(sub);
5727
Frederic Weisbecker76369132011-05-19 19:55:04 +02005728 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005729 }
5730}
5731
Stephane Eranianeed01522010-10-26 16:08:01 +02005732#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5733 PERF_FORMAT_TOTAL_TIME_RUNNING)
5734
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02005735/*
5736 * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
5737 *
5738 * The problem is that its both hard and excessively expensive to iterate the
5739 * child list, not to mention that its impossible to IPI the children running
5740 * on another CPU, from interrupt/NMI context.
5741 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005742static void perf_output_read(struct perf_output_handle *handle,
5743 struct perf_event *event)
5744{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005745 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005746 u64 read_format = event->attr.read_format;
5747
5748 /*
5749 * compute total_time_enabled, total_time_running
5750 * based on snapshot values taken when the event
5751 * was last scheduled in.
5752 *
5753 * we cannot simply called update_context_time()
5754 * because of locking issue as we are called in
5755 * NMI context
5756 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005757 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005758 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005759
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005761 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005762 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005763 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005764}
5765
5766void perf_output_sample(struct perf_output_handle *handle,
5767 struct perf_event_header *header,
5768 struct perf_sample_data *data,
5769 struct perf_event *event)
5770{
5771 u64 sample_type = data->type;
5772
5773 perf_output_put(handle, *header);
5774
Adrian Hunterff3d5272013-08-27 11:23:07 +03005775 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5776 perf_output_put(handle, data->id);
5777
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005778 if (sample_type & PERF_SAMPLE_IP)
5779 perf_output_put(handle, data->ip);
5780
5781 if (sample_type & PERF_SAMPLE_TID)
5782 perf_output_put(handle, data->tid_entry);
5783
5784 if (sample_type & PERF_SAMPLE_TIME)
5785 perf_output_put(handle, data->time);
5786
5787 if (sample_type & PERF_SAMPLE_ADDR)
5788 perf_output_put(handle, data->addr);
5789
5790 if (sample_type & PERF_SAMPLE_ID)
5791 perf_output_put(handle, data->id);
5792
5793 if (sample_type & PERF_SAMPLE_STREAM_ID)
5794 perf_output_put(handle, data->stream_id);
5795
5796 if (sample_type & PERF_SAMPLE_CPU)
5797 perf_output_put(handle, data->cpu_entry);
5798
5799 if (sample_type & PERF_SAMPLE_PERIOD)
5800 perf_output_put(handle, data->period);
5801
5802 if (sample_type & PERF_SAMPLE_READ)
5803 perf_output_read(handle, event);
5804
5805 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5806 if (data->callchain) {
5807 int size = 1;
5808
5809 if (data->callchain)
5810 size += data->callchain->nr;
5811
5812 size *= sizeof(u64);
5813
Frederic Weisbecker76369132011-05-19 19:55:04 +02005814 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005815 } else {
5816 u64 nr = 0;
5817 perf_output_put(handle, nr);
5818 }
5819 }
5820
5821 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005822 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005823
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005824 if (raw) {
5825 struct perf_raw_frag *frag = &raw->frag;
5826
5827 perf_output_put(handle, raw->size);
5828 do {
5829 if (frag->copy) {
5830 __output_custom(handle, frag->copy,
5831 frag->data, frag->size);
5832 } else {
5833 __output_copy(handle, frag->data,
5834 frag->size);
5835 }
5836 if (perf_raw_frag_last(frag))
5837 break;
5838 frag = frag->next;
5839 } while (1);
5840 if (frag->pad)
5841 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005842 } else {
5843 struct {
5844 u32 size;
5845 u32 data;
5846 } raw = {
5847 .size = sizeof(u32),
5848 .data = 0,
5849 };
5850 perf_output_put(handle, raw);
5851 }
5852 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005853
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005854 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5855 if (data->br_stack) {
5856 size_t size;
5857
5858 size = data->br_stack->nr
5859 * sizeof(struct perf_branch_entry);
5860
5861 perf_output_put(handle, data->br_stack->nr);
5862 perf_output_copy(handle, data->br_stack->entries, size);
5863 } else {
5864 /*
5865 * we always store at least the value of nr
5866 */
5867 u64 nr = 0;
5868 perf_output_put(handle, nr);
5869 }
5870 }
Jiri Olsa40189942012-08-07 15:20:37 +02005871
5872 if (sample_type & PERF_SAMPLE_REGS_USER) {
5873 u64 abi = data->regs_user.abi;
5874
5875 /*
5876 * If there are no regs to dump, notice it through
5877 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5878 */
5879 perf_output_put(handle, abi);
5880
5881 if (abi) {
5882 u64 mask = event->attr.sample_regs_user;
5883 perf_output_sample_regs(handle,
5884 data->regs_user.regs,
5885 mask);
5886 }
5887 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005888
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005889 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005890 perf_output_sample_ustack(handle,
5891 data->stack_user_size,
5892 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005893 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005894
5895 if (sample_type & PERF_SAMPLE_WEIGHT)
5896 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005897
5898 if (sample_type & PERF_SAMPLE_DATA_SRC)
5899 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005900
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005901 if (sample_type & PERF_SAMPLE_TRANSACTION)
5902 perf_output_put(handle, data->txn);
5903
Stephane Eranian60e23642014-09-24 13:48:37 +02005904 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5905 u64 abi = data->regs_intr.abi;
5906 /*
5907 * If there are no regs to dump, notice it through
5908 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5909 */
5910 perf_output_put(handle, abi);
5911
5912 if (abi) {
5913 u64 mask = event->attr.sample_regs_intr;
5914
5915 perf_output_sample_regs(handle,
5916 data->regs_intr.regs,
5917 mask);
5918 }
5919 }
5920
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005921 if (!event->attr.watermark) {
5922 int wakeup_events = event->attr.wakeup_events;
5923
5924 if (wakeup_events) {
5925 struct ring_buffer *rb = handle->rb;
5926 int events = local_inc_return(&rb->events);
5927
5928 if (events >= wakeup_events) {
5929 local_sub(wakeup_events, &rb->events);
5930 local_inc(&rb->wakeup);
5931 }
5932 }
5933 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005934}
5935
5936void perf_prepare_sample(struct perf_event_header *header,
5937 struct perf_sample_data *data,
5938 struct perf_event *event,
5939 struct pt_regs *regs)
5940{
5941 u64 sample_type = event->attr.sample_type;
5942
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005943 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005944 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005945
5946 header->misc = 0;
5947 header->misc |= perf_misc_flags(regs);
5948
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005949 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005950
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005951 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005952 data->ip = perf_instruction_pointer(regs);
5953
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005954 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5955 int size = 1;
5956
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005957 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005958
5959 if (data->callchain)
5960 size += data->callchain->nr;
5961
5962 header->size += size * sizeof(u64);
5963 }
5964
5965 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005966 struct perf_raw_record *raw = data->raw;
5967 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005968
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005969 if (raw) {
5970 struct perf_raw_frag *frag = &raw->frag;
5971 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005972
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005973 do {
5974 sum += frag->size;
5975 if (perf_raw_frag_last(frag))
5976 break;
5977 frag = frag->next;
5978 } while (1);
5979
5980 size = round_up(sum + sizeof(u32), sizeof(u64));
5981 raw->size = size - sizeof(u32);
5982 frag->pad = raw->size - sum;
5983 } else {
5984 size = sizeof(u64);
5985 }
5986
5987 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005988 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005989
5990 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5991 int size = sizeof(u64); /* nr */
5992 if (data->br_stack) {
5993 size += data->br_stack->nr
5994 * sizeof(struct perf_branch_entry);
5995 }
5996 header->size += size;
5997 }
Jiri Olsa40189942012-08-07 15:20:37 +02005998
Peter Zijlstra25657112014-09-24 13:48:42 +02005999 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006000 perf_sample_regs_user(&data->regs_user, regs,
6001 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02006002
Jiri Olsa40189942012-08-07 15:20:37 +02006003 if (sample_type & PERF_SAMPLE_REGS_USER) {
6004 /* regs dump ABI info */
6005 int size = sizeof(u64);
6006
Jiri Olsa40189942012-08-07 15:20:37 +02006007 if (data->regs_user.regs) {
6008 u64 mask = event->attr.sample_regs_user;
6009 size += hweight64(mask) * sizeof(u64);
6010 }
6011
6012 header->size += size;
6013 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006014
6015 if (sample_type & PERF_SAMPLE_STACK_USER) {
6016 /*
6017 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
6018 * processed as the last one or have additional check added
6019 * in case new sample type is added, because we could eat
6020 * up the rest of the sample size.
6021 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02006022 u16 stack_size = event->attr.sample_stack_user;
6023 u16 size = sizeof(u64);
6024
Jiri Olsac5ebced2012-08-07 15:20:40 +02006025 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02006026 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006027
6028 /*
6029 * If there is something to dump, add space for the dump
6030 * itself and for the field that tells the dynamic size,
6031 * which is how many have been actually dumped.
6032 */
6033 if (stack_size)
6034 size += sizeof(u64) + stack_size;
6035
6036 data->stack_user_size = stack_size;
6037 header->size += size;
6038 }
Stephane Eranian60e23642014-09-24 13:48:37 +02006039
6040 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6041 /* regs dump ABI info */
6042 int size = sizeof(u64);
6043
6044 perf_sample_regs_intr(&data->regs_intr, regs);
6045
6046 if (data->regs_intr.regs) {
6047 u64 mask = event->attr.sample_regs_intr;
6048
6049 size += hweight64(mask) * sizeof(u64);
6050 }
6051
6052 header->size += size;
6053 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006054}
6055
Wang Nan9ecda412016-04-05 14:11:18 +00006056static void __always_inline
6057__perf_event_output(struct perf_event *event,
6058 struct perf_sample_data *data,
6059 struct pt_regs *regs,
6060 int (*output_begin)(struct perf_output_handle *,
6061 struct perf_event *,
6062 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006063{
6064 struct perf_output_handle handle;
6065 struct perf_event_header header;
6066
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006067 /* protect the callchain buffers */
6068 rcu_read_lock();
6069
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006070 perf_prepare_sample(&header, data, event, regs);
6071
Wang Nan9ecda412016-04-05 14:11:18 +00006072 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006073 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006074
6075 perf_output_sample(&handle, &header, data, event);
6076
6077 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006078
6079exit:
6080 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006081}
6082
Wang Nan9ecda412016-04-05 14:11:18 +00006083void
6084perf_event_output_forward(struct perf_event *event,
6085 struct perf_sample_data *data,
6086 struct pt_regs *regs)
6087{
6088 __perf_event_output(event, data, regs, perf_output_begin_forward);
6089}
6090
6091void
6092perf_event_output_backward(struct perf_event *event,
6093 struct perf_sample_data *data,
6094 struct pt_regs *regs)
6095{
6096 __perf_event_output(event, data, regs, perf_output_begin_backward);
6097}
6098
6099void
6100perf_event_output(struct perf_event *event,
6101 struct perf_sample_data *data,
6102 struct pt_regs *regs)
6103{
6104 __perf_event_output(event, data, regs, perf_output_begin);
6105}
6106
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006107/*
6108 * read event_id
6109 */
6110
6111struct perf_read_event {
6112 struct perf_event_header header;
6113
6114 u32 pid;
6115 u32 tid;
6116};
6117
6118static void
6119perf_event_read_event(struct perf_event *event,
6120 struct task_struct *task)
6121{
6122 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006123 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006124 struct perf_read_event read_event = {
6125 .header = {
6126 .type = PERF_RECORD_READ,
6127 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006128 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006129 },
6130 .pid = perf_event_pid(event, task),
6131 .tid = perf_event_tid(event, task),
6132 };
6133 int ret;
6134
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006135 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006136 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006137 if (ret)
6138 return;
6139
6140 perf_output_put(&handle, read_event);
6141 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006142 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006143
6144 perf_output_end(&handle);
6145}
6146
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006147typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006148
6149static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006150perf_iterate_ctx(struct perf_event_context *ctx,
6151 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006152 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02006153{
6154 struct perf_event *event;
6155
6156 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006157 if (!all) {
6158 if (event->state < PERF_EVENT_STATE_INACTIVE)
6159 continue;
6160 if (!event_filter_match(event))
6161 continue;
6162 }
6163
Jiri Olsa67516842013-07-09 18:56:31 +02006164 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006165 }
6166}
6167
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006168static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07006169{
6170 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
6171 struct perf_event *event;
6172
6173 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02006174 /*
6175 * Skip events that are not fully formed yet; ensure that
6176 * if we observe event->ctx, both event and ctx will be
6177 * complete enough. See perf_install_in_context().
6178 */
6179 if (!smp_load_acquire(&event->ctx))
6180 continue;
6181
Kan Liangf2fb6be2016-03-23 11:24:37 -07006182 if (event->state < PERF_EVENT_STATE_INACTIVE)
6183 continue;
6184 if (!event_filter_match(event))
6185 continue;
6186 output(event, data);
6187 }
6188}
6189
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006190/*
6191 * Iterate all events that need to receive side-band events.
6192 *
6193 * For new callers; ensure that account_pmu_sb_event() includes
6194 * your event, otherwise it might not get delivered.
6195 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006196static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006197perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006198 struct perf_event_context *task_ctx)
6199{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006200 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02006201 int ctxn;
6202
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006203 rcu_read_lock();
6204 preempt_disable();
6205
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006206 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006207 * If we have task_ctx != NULL we only notify the task context itself.
6208 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006209 * context.
6210 */
6211 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006212 perf_iterate_ctx(task_ctx, output, data, false);
6213 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006214 }
6215
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006216 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07006217
6218 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02006219 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6220 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006221 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006222 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006223done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07006224 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02006225 rcu_read_unlock();
6226}
6227
Alexander Shishkin375637b2016-04-27 18:44:46 +03006228/*
6229 * Clear all file-based filters at exec, they'll have to be
6230 * re-instated when/if these objects are mmapped again.
6231 */
6232static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
6233{
6234 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6235 struct perf_addr_filter *filter;
6236 unsigned int restart = 0, count = 0;
6237 unsigned long flags;
6238
6239 if (!has_addr_filter(event))
6240 return;
6241
6242 raw_spin_lock_irqsave(&ifh->lock, flags);
6243 list_for_each_entry(filter, &ifh->list, entry) {
6244 if (filter->inode) {
6245 event->addr_filters_offs[count] = 0;
6246 restart++;
6247 }
6248
6249 count++;
6250 }
6251
6252 if (restart)
6253 event->addr_filters_gen++;
6254 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6255
6256 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006257 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006258}
6259
6260void perf_event_exec(void)
6261{
6262 struct perf_event_context *ctx;
6263 int ctxn;
6264
6265 rcu_read_lock();
6266 for_each_task_context_nr(ctxn) {
6267 ctx = current->perf_event_ctxp[ctxn];
6268 if (!ctx)
6269 continue;
6270
6271 perf_event_enable_on_exec(ctxn);
6272
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006273 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03006274 true);
6275 }
6276 rcu_read_unlock();
6277}
6278
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006279struct remote_output {
6280 struct ring_buffer *rb;
6281 int err;
6282};
6283
6284static void __perf_event_output_stop(struct perf_event *event, void *data)
6285{
6286 struct perf_event *parent = event->parent;
6287 struct remote_output *ro = data;
6288 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03006289 struct stop_event_data sd = {
6290 .event = event,
6291 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006292
6293 if (!has_aux(event))
6294 return;
6295
6296 if (!parent)
6297 parent = event;
6298
6299 /*
6300 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03006301 * ring-buffer, but it will be the child that's actually using it.
6302 *
6303 * We are using event::rb to determine if the event should be stopped,
6304 * however this may race with ring_buffer_attach() (through set_output),
6305 * which will make us skip the event that actually needs to be stopped.
6306 * So ring_buffer_attach() has to stop an aux event before re-assigning
6307 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006308 */
6309 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006310 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006311}
6312
6313static int __perf_pmu_output_stop(void *info)
6314{
6315 struct perf_event *event = info;
6316 struct pmu *pmu = event->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01006317 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006318 struct remote_output ro = {
6319 .rb = event->rb,
6320 };
6321
6322 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006323 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006324 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006325 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006326 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006327 rcu_read_unlock();
6328
6329 return ro.err;
6330}
6331
6332static void perf_pmu_output_stop(struct perf_event *event)
6333{
6334 struct perf_event *iter;
6335 int err, cpu;
6336
6337restart:
6338 rcu_read_lock();
6339 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6340 /*
6341 * For per-CPU events, we need to make sure that neither they
6342 * nor their children are running; for cpu==-1 events it's
6343 * sufficient to stop the event itself if it's active, since
6344 * it can't have children.
6345 */
6346 cpu = iter->cpu;
6347 if (cpu == -1)
6348 cpu = READ_ONCE(iter->oncpu);
6349
6350 if (cpu == -1)
6351 continue;
6352
6353 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6354 if (err == -EAGAIN) {
6355 rcu_read_unlock();
6356 goto restart;
6357 }
6358 }
6359 rcu_read_unlock();
6360}
6361
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006362/*
6363 * task tracking -- fork/exit
6364 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006365 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006366 */
6367
6368struct perf_task_event {
6369 struct task_struct *task;
6370 struct perf_event_context *task_ctx;
6371
6372 struct {
6373 struct perf_event_header header;
6374
6375 u32 pid;
6376 u32 ppid;
6377 u32 tid;
6378 u32 ptid;
6379 u64 time;
6380 } event_id;
6381};
6382
Jiri Olsa67516842013-07-09 18:56:31 +02006383static int perf_event_task_match(struct perf_event *event)
6384{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006385 return event->attr.comm || event->attr.mmap ||
6386 event->attr.mmap2 || event->attr.mmap_data ||
6387 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006388}
6389
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006390static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006391 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006392{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006393 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006394 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006395 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006396 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006397 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006398
Jiri Olsa67516842013-07-09 18:56:31 +02006399 if (!perf_event_task_match(event))
6400 return;
6401
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006402 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006403
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006404 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006405 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006406 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006407 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006408
6409 task_event->event_id.pid = perf_event_pid(event, task);
6410 task_event->event_id.ppid = perf_event_pid(event, current);
6411
6412 task_event->event_id.tid = perf_event_tid(event, task);
6413 task_event->event_id.ptid = perf_event_tid(event, current);
6414
Peter Zijlstra34f43922015-02-20 14:05:38 +01006415 task_event->event_id.time = perf_event_clock(event);
6416
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006417 perf_output_put(&handle, task_event->event_id);
6418
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006419 perf_event__output_id_sample(event, &handle, &sample);
6420
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006421 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006422out:
6423 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006424}
6425
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006426static void perf_event_task(struct task_struct *task,
6427 struct perf_event_context *task_ctx,
6428 int new)
6429{
6430 struct perf_task_event task_event;
6431
6432 if (!atomic_read(&nr_comm_events) &&
6433 !atomic_read(&nr_mmap_events) &&
6434 !atomic_read(&nr_task_events))
6435 return;
6436
6437 task_event = (struct perf_task_event){
6438 .task = task,
6439 .task_ctx = task_ctx,
6440 .event_id = {
6441 .header = {
6442 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6443 .misc = 0,
6444 .size = sizeof(task_event.event_id),
6445 },
6446 /* .pid */
6447 /* .ppid */
6448 /* .tid */
6449 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006450 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006451 },
6452 };
6453
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006454 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006455 &task_event,
6456 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006457}
6458
6459void perf_event_fork(struct task_struct *task)
6460{
6461 perf_event_task(task, NULL, 1);
6462}
6463
6464/*
6465 * comm tracking
6466 */
6467
6468struct perf_comm_event {
6469 struct task_struct *task;
6470 char *comm;
6471 int comm_size;
6472
6473 struct {
6474 struct perf_event_header header;
6475
6476 u32 pid;
6477 u32 tid;
6478 } event_id;
6479};
6480
Jiri Olsa67516842013-07-09 18:56:31 +02006481static int perf_event_comm_match(struct perf_event *event)
6482{
6483 return event->attr.comm;
6484}
6485
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006486static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006487 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006488{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006489 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006490 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006491 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006492 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006493 int ret;
6494
Jiri Olsa67516842013-07-09 18:56:31 +02006495 if (!perf_event_comm_match(event))
6496 return;
6497
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006498 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6499 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006500 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006501
6502 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006503 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006504
6505 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6506 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6507
6508 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006509 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006510 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006511
6512 perf_event__output_id_sample(event, &handle, &sample);
6513
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006514 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006515out:
6516 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006517}
6518
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006519static void perf_event_comm_event(struct perf_comm_event *comm_event)
6520{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006521 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006522 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006523
6524 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006525 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006526 size = ALIGN(strlen(comm)+1, sizeof(u64));
6527
6528 comm_event->comm = comm;
6529 comm_event->comm_size = size;
6530
6531 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006532
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006533 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006534 comm_event,
6535 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006536}
6537
Adrian Hunter82b89772014-05-28 11:45:04 +03006538void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006539{
6540 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006541
6542 if (!atomic_read(&nr_comm_events))
6543 return;
6544
6545 comm_event = (struct perf_comm_event){
6546 .task = task,
6547 /* .comm */
6548 /* .comm_size */
6549 .event_id = {
6550 .header = {
6551 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006552 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006553 /* .size */
6554 },
6555 /* .pid */
6556 /* .tid */
6557 },
6558 };
6559
6560 perf_event_comm_event(&comm_event);
6561}
6562
6563/*
6564 * mmap tracking
6565 */
6566
6567struct perf_mmap_event {
6568 struct vm_area_struct *vma;
6569
6570 const char *file_name;
6571 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006572 int maj, min;
6573 u64 ino;
6574 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006575 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006576
6577 struct {
6578 struct perf_event_header header;
6579
6580 u32 pid;
6581 u32 tid;
6582 u64 start;
6583 u64 len;
6584 u64 pgoff;
6585 } event_id;
6586};
6587
Jiri Olsa67516842013-07-09 18:56:31 +02006588static int perf_event_mmap_match(struct perf_event *event,
6589 void *data)
6590{
6591 struct perf_mmap_event *mmap_event = data;
6592 struct vm_area_struct *vma = mmap_event->vma;
6593 int executable = vma->vm_flags & VM_EXEC;
6594
6595 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006596 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006597}
6598
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006599static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006600 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006601{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006602 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006603 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006604 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006605 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006606 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006607
Jiri Olsa67516842013-07-09 18:56:31 +02006608 if (!perf_event_mmap_match(event, data))
6609 return;
6610
Stephane Eranian13d7a242013-08-21 12:10:24 +02006611 if (event->attr.mmap2) {
6612 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6613 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6614 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6615 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006616 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006617 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6618 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006619 }
6620
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006621 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6622 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006623 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006624 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006625 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006626
6627 mmap_event->event_id.pid = perf_event_pid(event, current);
6628 mmap_event->event_id.tid = perf_event_tid(event, current);
6629
6630 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006631
6632 if (event->attr.mmap2) {
6633 perf_output_put(&handle, mmap_event->maj);
6634 perf_output_put(&handle, mmap_event->min);
6635 perf_output_put(&handle, mmap_event->ino);
6636 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006637 perf_output_put(&handle, mmap_event->prot);
6638 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006639 }
6640
Frederic Weisbecker76369132011-05-19 19:55:04 +02006641 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006642 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006643
6644 perf_event__output_id_sample(event, &handle, &sample);
6645
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006646 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006647out:
6648 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649}
6650
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006651static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6652{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006653 struct vm_area_struct *vma = mmap_event->vma;
6654 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006655 int maj = 0, min = 0;
6656 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006657 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006658 unsigned int size;
6659 char tmp[16];
6660 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006661 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006662
Peter Zijlstrab41615a2017-01-26 23:15:08 +01006663 if (vma->vm_flags & VM_READ)
6664 prot |= PROT_READ;
6665 if (vma->vm_flags & VM_WRITE)
6666 prot |= PROT_WRITE;
6667 if (vma->vm_flags & VM_EXEC)
6668 prot |= PROT_EXEC;
6669
6670 if (vma->vm_flags & VM_MAYSHARE)
6671 flags = MAP_SHARED;
6672 else
6673 flags = MAP_PRIVATE;
6674
6675 if (vma->vm_flags & VM_DENYWRITE)
6676 flags |= MAP_DENYWRITE;
6677 if (vma->vm_flags & VM_MAYEXEC)
6678 flags |= MAP_EXECUTABLE;
6679 if (vma->vm_flags & VM_LOCKED)
6680 flags |= MAP_LOCKED;
6681 if (vma->vm_flags & VM_HUGETLB)
6682 flags |= MAP_HUGETLB;
6683
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006684 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006685 struct inode *inode;
6686 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006687
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006688 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006689 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006690 name = "//enomem";
6691 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006692 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006693 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006694 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006695 * need to add enough zero bytes after the string to handle
6696 * the 64bit alignment we do later.
6697 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006698 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006699 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006700 name = "//toolong";
6701 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006702 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006703 inode = file_inode(vma->vm_file);
6704 dev = inode->i_sb->s_dev;
6705 ino = inode->i_ino;
6706 gen = inode->i_generation;
6707 maj = MAJOR(dev);
6708 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006709
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006710 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006711 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006712 if (vma->vm_ops && vma->vm_ops->name) {
6713 name = (char *) vma->vm_ops->name(vma);
6714 if (name)
6715 goto cpy_name;
6716 }
6717
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006718 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006719 if (name)
6720 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006721
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006722 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006723 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006724 name = "[heap]";
6725 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006726 }
6727 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006728 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006729 name = "[stack]";
6730 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006731 }
6732
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006733 name = "//anon";
6734 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006735 }
6736
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006737cpy_name:
6738 strlcpy(tmp, name, sizeof(tmp));
6739 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006740got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006741 /*
6742 * Since our buffer works in 8 byte units we need to align our string
6743 * size to a multiple of 8. However, we must guarantee the tail end is
6744 * zero'd out to avoid leaking random bits to userspace.
6745 */
6746 size = strlen(name)+1;
6747 while (!IS_ALIGNED(size, sizeof(u64)))
6748 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006749
6750 mmap_event->file_name = name;
6751 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006752 mmap_event->maj = maj;
6753 mmap_event->min = min;
6754 mmap_event->ino = ino;
6755 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006756 mmap_event->prot = prot;
6757 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006758
Stephane Eranian2fe85422013-01-24 16:10:39 +01006759 if (!(vma->vm_flags & VM_EXEC))
6760 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6761
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006762 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6763
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006764 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006765 mmap_event,
6766 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006767
6768 kfree(buf);
6769}
6770
Alexander Shishkin375637b2016-04-27 18:44:46 +03006771/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03006772 * Check whether inode and address range match filter criteria.
6773 */
6774static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6775 struct file *file, unsigned long offset,
6776 unsigned long size)
6777{
6778 if (filter->inode != file->f_inode)
6779 return false;
6780
6781 if (filter->offset > offset + size)
6782 return false;
6783
6784 if (filter->offset + filter->size < offset)
6785 return false;
6786
6787 return true;
6788}
6789
6790static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6791{
6792 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6793 struct vm_area_struct *vma = data;
6794 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6795 struct file *file = vma->vm_file;
6796 struct perf_addr_filter *filter;
6797 unsigned int restart = 0, count = 0;
6798
6799 if (!has_addr_filter(event))
6800 return;
6801
6802 if (!file)
6803 return;
6804
6805 raw_spin_lock_irqsave(&ifh->lock, flags);
6806 list_for_each_entry(filter, &ifh->list, entry) {
6807 if (perf_addr_filter_match(filter, file, off,
6808 vma->vm_end - vma->vm_start)) {
6809 event->addr_filters_offs[count] = vma->vm_start;
6810 restart++;
6811 }
6812
6813 count++;
6814 }
6815
6816 if (restart)
6817 event->addr_filters_gen++;
6818 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6819
6820 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006821 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006822}
6823
6824/*
6825 * Adjust all task's events' filters to the new vma
6826 */
6827static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6828{
6829 struct perf_event_context *ctx;
6830 int ctxn;
6831
Mathieu Poirier12b40a22016-07-18 10:43:06 -06006832 /*
6833 * Data tracing isn't supported yet and as such there is no need
6834 * to keep track of anything that isn't related to executable code:
6835 */
6836 if (!(vma->vm_flags & VM_EXEC))
6837 return;
6838
Alexander Shishkin375637b2016-04-27 18:44:46 +03006839 rcu_read_lock();
6840 for_each_task_context_nr(ctxn) {
6841 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6842 if (!ctx)
6843 continue;
6844
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006845 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006846 }
6847 rcu_read_unlock();
6848}
6849
Eric B Munson3af9e852010-05-18 15:30:49 +01006850void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006851{
6852 struct perf_mmap_event mmap_event;
6853
6854 if (!atomic_read(&nr_mmap_events))
6855 return;
6856
6857 mmap_event = (struct perf_mmap_event){
6858 .vma = vma,
6859 /* .file_name */
6860 /* .file_size */
6861 .event_id = {
6862 .header = {
6863 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006864 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006865 /* .size */
6866 },
6867 /* .pid */
6868 /* .tid */
6869 .start = vma->vm_start,
6870 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006871 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006872 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006873 /* .maj (attr_mmap2 only) */
6874 /* .min (attr_mmap2 only) */
6875 /* .ino (attr_mmap2 only) */
6876 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006877 /* .prot (attr_mmap2 only) */
6878 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006879 };
6880
Alexander Shishkin375637b2016-04-27 18:44:46 +03006881 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006882 perf_event_mmap_event(&mmap_event);
6883}
6884
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006885void perf_event_aux_event(struct perf_event *event, unsigned long head,
6886 unsigned long size, u64 flags)
6887{
6888 struct perf_output_handle handle;
6889 struct perf_sample_data sample;
6890 struct perf_aux_event {
6891 struct perf_event_header header;
6892 u64 offset;
6893 u64 size;
6894 u64 flags;
6895 } rec = {
6896 .header = {
6897 .type = PERF_RECORD_AUX,
6898 .misc = 0,
6899 .size = sizeof(rec),
6900 },
6901 .offset = head,
6902 .size = size,
6903 .flags = flags,
6904 };
6905 int ret;
6906
6907 perf_event_header__init_id(&rec.header, &sample, event);
6908 ret = perf_output_begin(&handle, event, rec.header.size);
6909
6910 if (ret)
6911 return;
6912
6913 perf_output_put(&handle, rec);
6914 perf_event__output_id_sample(event, &handle, &sample);
6915
6916 perf_output_end(&handle);
6917}
6918
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006919/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006920 * Lost/dropped samples logging
6921 */
6922void perf_log_lost_samples(struct perf_event *event, u64 lost)
6923{
6924 struct perf_output_handle handle;
6925 struct perf_sample_data sample;
6926 int ret;
6927
6928 struct {
6929 struct perf_event_header header;
6930 u64 lost;
6931 } lost_samples_event = {
6932 .header = {
6933 .type = PERF_RECORD_LOST_SAMPLES,
6934 .misc = 0,
6935 .size = sizeof(lost_samples_event),
6936 },
6937 .lost = lost,
6938 };
6939
6940 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6941
6942 ret = perf_output_begin(&handle, event,
6943 lost_samples_event.header.size);
6944 if (ret)
6945 return;
6946
6947 perf_output_put(&handle, lost_samples_event);
6948 perf_event__output_id_sample(event, &handle, &sample);
6949 perf_output_end(&handle);
6950}
6951
6952/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006953 * context_switch tracking
6954 */
6955
6956struct perf_switch_event {
6957 struct task_struct *task;
6958 struct task_struct *next_prev;
6959
6960 struct {
6961 struct perf_event_header header;
6962 u32 next_prev_pid;
6963 u32 next_prev_tid;
6964 } event_id;
6965};
6966
6967static int perf_event_switch_match(struct perf_event *event)
6968{
6969 return event->attr.context_switch;
6970}
6971
6972static void perf_event_switch_output(struct perf_event *event, void *data)
6973{
6974 struct perf_switch_event *se = data;
6975 struct perf_output_handle handle;
6976 struct perf_sample_data sample;
6977 int ret;
6978
6979 if (!perf_event_switch_match(event))
6980 return;
6981
6982 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6983 if (event->ctx->task) {
6984 se->event_id.header.type = PERF_RECORD_SWITCH;
6985 se->event_id.header.size = sizeof(se->event_id.header);
6986 } else {
6987 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6988 se->event_id.header.size = sizeof(se->event_id);
6989 se->event_id.next_prev_pid =
6990 perf_event_pid(event, se->next_prev);
6991 se->event_id.next_prev_tid =
6992 perf_event_tid(event, se->next_prev);
6993 }
6994
6995 perf_event_header__init_id(&se->event_id.header, &sample, event);
6996
6997 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6998 if (ret)
6999 return;
7000
7001 if (event->ctx->task)
7002 perf_output_put(&handle, se->event_id.header);
7003 else
7004 perf_output_put(&handle, se->event_id);
7005
7006 perf_event__output_id_sample(event, &handle, &sample);
7007
7008 perf_output_end(&handle);
7009}
7010
7011static void perf_event_switch(struct task_struct *task,
7012 struct task_struct *next_prev, bool sched_in)
7013{
7014 struct perf_switch_event switch_event;
7015
7016 /* N.B. caller checks nr_switch_events != 0 */
7017
7018 switch_event = (struct perf_switch_event){
7019 .task = task,
7020 .next_prev = next_prev,
7021 .event_id = {
7022 .header = {
7023 /* .type */
7024 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
7025 /* .size */
7026 },
7027 /* .next_prev_pid */
7028 /* .next_prev_tid */
7029 },
7030 };
7031
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007032 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03007033 &switch_event,
7034 NULL);
7035}
7036
7037/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007038 * IRQ throttle logging
7039 */
7040
7041static void perf_log_throttle(struct perf_event *event, int enable)
7042{
7043 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007044 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007045 int ret;
7046
7047 struct {
7048 struct perf_event_header header;
7049 u64 time;
7050 u64 id;
7051 u64 stream_id;
7052 } throttle_event = {
7053 .header = {
7054 .type = PERF_RECORD_THROTTLE,
7055 .misc = 0,
7056 .size = sizeof(throttle_event),
7057 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01007058 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007059 .id = primary_event_id(event),
7060 .stream_id = event->id,
7061 };
7062
7063 if (enable)
7064 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
7065
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007066 perf_event_header__init_id(&throttle_event.header, &sample, event);
7067
7068 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007069 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007070 if (ret)
7071 return;
7072
7073 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007074 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007075 perf_output_end(&handle);
7076}
7077
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007078static void perf_log_itrace_start(struct perf_event *event)
7079{
7080 struct perf_output_handle handle;
7081 struct perf_sample_data sample;
7082 struct perf_aux_event {
7083 struct perf_event_header header;
7084 u32 pid;
7085 u32 tid;
7086 } rec;
7087 int ret;
7088
7089 if (event->parent)
7090 event = event->parent;
7091
7092 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
7093 event->hw.itrace_started)
7094 return;
7095
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007096 rec.header.type = PERF_RECORD_ITRACE_START;
7097 rec.header.misc = 0;
7098 rec.header.size = sizeof(rec);
7099 rec.pid = perf_event_pid(event, current);
7100 rec.tid = perf_event_tid(event, current);
7101
7102 perf_event_header__init_id(&rec.header, &sample, event);
7103 ret = perf_output_begin(&handle, event, rec.header.size);
7104
7105 if (ret)
7106 return;
7107
7108 perf_output_put(&handle, rec);
7109 perf_event__output_id_sample(event, &handle, &sample);
7110
7111 perf_output_end(&handle);
7112}
7113
Jiri Olsaa88ff232016-12-28 14:31:03 +01007114static int
7115__perf_event_account_interrupt(struct perf_event *event, int throttle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007116{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007117 struct hw_perf_event *hwc = &event->hw;
7118 int ret = 0;
Jiri Olsaa88ff232016-12-28 14:31:03 +01007119 u64 seq;
Peter Zijlstra96398822010-11-24 18:55:29 +01007120
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007121 seq = __this_cpu_read(perf_throttled_seq);
7122 if (seq != hwc->interrupts_seq) {
7123 hwc->interrupts_seq = seq;
7124 hwc->interrupts = 1;
7125 } else {
7126 hwc->interrupts++;
7127 if (unlikely(throttle
7128 && hwc->interrupts >= max_samples_per_tick)) {
7129 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007130 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01007131 hwc->interrupts = MAX_INTERRUPTS;
7132 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007133 ret = 1;
7134 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007135 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007136
7137 if (event->attr.freq) {
7138 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01007139 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007140
Peter Zijlstraabd50712010-01-26 18:50:16 +01007141 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007142
Peter Zijlstraabd50712010-01-26 18:50:16 +01007143 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01007144 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007145 }
7146
Jiri Olsaa88ff232016-12-28 14:31:03 +01007147 return ret;
7148}
7149
7150int perf_event_account_interrupt(struct perf_event *event)
7151{
7152 return __perf_event_account_interrupt(event, 1);
7153}
7154
7155/*
7156 * Generic event overflow handling, sampling.
7157 */
7158
7159static int __perf_event_overflow(struct perf_event *event,
7160 int throttle, struct perf_sample_data *data,
7161 struct pt_regs *regs)
7162{
7163 int events = atomic_read(&event->event_limit);
7164 int ret = 0;
7165
7166 /*
7167 * Non-sampling counters might still use the PMI to fold short
7168 * hardware counters, ignore those.
7169 */
7170 if (unlikely(!is_sampling_event(event)))
7171 return 0;
7172
7173 ret = __perf_event_account_interrupt(event, throttle);
7174
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007175 /*
7176 * XXX event_limit might not quite work as expected on inherited
7177 * events
7178 */
7179
7180 event->pending_kill = POLL_IN;
7181 if (events && atomic_dec_and_test(&event->event_limit)) {
7182 ret = 1;
7183 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02007184
7185 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007186 }
7187
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007188 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01007189
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02007190 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007191 event->pending_wakeup = 1;
7192 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02007193 }
7194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007195 return ret;
7196}
7197
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007198int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007199 struct perf_sample_data *data,
7200 struct pt_regs *regs)
7201{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007202 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007203}
7204
7205/*
7206 * Generic software event infrastructure
7207 */
7208
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007209struct swevent_htable {
7210 struct swevent_hlist *swevent_hlist;
7211 struct mutex hlist_mutex;
7212 int hlist_refcount;
7213
7214 /* Recursion avoidance in each contexts */
7215 int recursion[PERF_NR_CONTEXTS];
7216};
7217
7218static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7219
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007220/*
7221 * We directly increment event->count and keep a second value in
7222 * event->hw.period_left to count intervals. This period event
7223 * is kept in the range [-sample_period, 0] so that we can use the
7224 * sign as trigger.
7225 */
7226
Jiri Olsaab573842013-05-01 17:25:44 +02007227u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007228{
7229 struct hw_perf_event *hwc = &event->hw;
7230 u64 period = hwc->last_period;
7231 u64 nr, offset;
7232 s64 old, val;
7233
7234 hwc->last_period = hwc->sample_period;
7235
7236again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02007237 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007238 if (val < 0)
7239 return 0;
7240
7241 nr = div64_u64(period + val, period);
7242 offset = nr * period;
7243 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02007244 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007245 goto again;
7246
7247 return nr;
7248}
7249
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007250static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007251 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007252 struct pt_regs *regs)
7253{
7254 struct hw_perf_event *hwc = &event->hw;
7255 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007256
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007257 if (!overflow)
7258 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007259
7260 if (hwc->interrupts == MAX_INTERRUPTS)
7261 return;
7262
7263 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007264 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007265 data, regs)) {
7266 /*
7267 * We inhibit the overflow from happening when
7268 * hwc->interrupts == MAX_INTERRUPTS.
7269 */
7270 break;
7271 }
7272 throttle = 1;
7273 }
7274}
7275
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007276static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007277 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007278 struct pt_regs *regs)
7279{
7280 struct hw_perf_event *hwc = &event->hw;
7281
Peter Zijlstrae7850592010-05-21 14:43:08 +02007282 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007283
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007284 if (!regs)
7285 return;
7286
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007287 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007288 return;
7289
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03007290 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
7291 data->period = nr;
7292 return perf_swevent_overflow(event, 1, data, regs);
7293 } else
7294 data->period = event->hw.last_period;
7295
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007296 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007297 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007298
Peter Zijlstrae7850592010-05-21 14:43:08 +02007299 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007300 return;
7301
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007302 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007303}
7304
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007305static int perf_exclude_event(struct perf_event *event,
7306 struct pt_regs *regs)
7307{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007308 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01007309 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007310
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007311 if (regs) {
7312 if (event->attr.exclude_user && user_mode(regs))
7313 return 1;
7314
7315 if (event->attr.exclude_kernel && !user_mode(regs))
7316 return 1;
7317 }
7318
7319 return 0;
7320}
7321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007322static int perf_swevent_match(struct perf_event *event,
7323 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007324 u32 event_id,
7325 struct perf_sample_data *data,
7326 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007327{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007328 if (event->attr.type != type)
7329 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007330
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007331 if (event->attr.config != event_id)
7332 return 0;
7333
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007334 if (perf_exclude_event(event, regs))
7335 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007336
7337 return 1;
7338}
7339
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007340static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007341{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007342 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007343
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007344 return hash_64(val, SWEVENT_HLIST_BITS);
7345}
7346
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007347static inline struct hlist_head *
7348__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007349{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007350 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007351
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007352 return &hlist->heads[hash];
7353}
7354
7355/* For the read side: events when they trigger */
7356static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007357find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007358{
7359 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007360
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007361 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007362 if (!hlist)
7363 return NULL;
7364
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007365 return __find_swevent_head(hlist, type, event_id);
7366}
7367
7368/* For the event head insertion and removal in the hlist */
7369static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007370find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007371{
7372 struct swevent_hlist *hlist;
7373 u32 event_id = event->attr.config;
7374 u64 type = event->attr.type;
7375
7376 /*
7377 * Event scheduling is always serialized against hlist allocation
7378 * and release. Which makes the protected version suitable here.
7379 * The context lock guarantees that.
7380 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007381 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007382 lockdep_is_held(&event->ctx->lock));
7383 if (!hlist)
7384 return NULL;
7385
7386 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007387}
7388
7389static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007390 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007391 struct perf_sample_data *data,
7392 struct pt_regs *regs)
7393{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007394 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007395 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007396 struct hlist_head *head;
7397
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007398 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007399 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007400 if (!head)
7401 goto end;
7402
Sasha Levinb67bfe02013-02-27 17:06:00 -08007403 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007404 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007405 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007406 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007407end:
7408 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007409}
7410
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007411DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7412
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007413int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007414{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007415 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007416
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007417 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007418}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007419EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007420
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007421void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007422{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007423 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007424
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007425 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007426}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007427
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007428void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007429{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007430 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007431
7432 if (WARN_ON_ONCE(!regs))
7433 return;
7434
7435 perf_sample_data_init(&data, addr, 0);
7436 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7437}
7438
7439void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7440{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007441 int rctx;
7442
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007443 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007444 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007445 if (unlikely(rctx < 0))
7446 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007447
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007448 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007449
7450 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007451fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007452 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007453}
7454
7455static void perf_swevent_read(struct perf_event *event)
7456{
7457}
7458
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007459static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007460{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007461 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007462 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007463 struct hlist_head *head;
7464
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007465 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007466 hwc->last_period = hwc->sample_period;
7467 perf_swevent_set_period(event);
7468 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007469
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007470 hwc->state = !(flags & PERF_EF_START);
7471
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007472 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007473 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007474 return -EINVAL;
7475
7476 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007477 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007478
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007479 return 0;
7480}
7481
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007482static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007483{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007484 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007485}
7486
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007487static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007488{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007489 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007490}
7491
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007492static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007493{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007494 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007495}
7496
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007497/* Deref the hlist from the update side */
7498static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007499swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007500{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007501 return rcu_dereference_protected(swhash->swevent_hlist,
7502 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007503}
7504
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007505static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007506{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007507 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007508
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007509 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007510 return;
7511
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007512 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007513 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007514}
7515
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007516static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007517{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007518 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007519
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007520 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007521
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007522 if (!--swhash->hlist_refcount)
7523 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007524
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007525 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007526}
7527
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007528static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007529{
7530 int cpu;
7531
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007532 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007533 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007534}
7535
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007536static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007537{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007538 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007539 int err = 0;
7540
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007541 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007542 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007543 struct swevent_hlist *hlist;
7544
7545 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7546 if (!hlist) {
7547 err = -ENOMEM;
7548 goto exit;
7549 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007550 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007551 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007552 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007553exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007554 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007555
7556 return err;
7557}
7558
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007559static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007560{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007561 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007562
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007563 get_online_cpus();
7564 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007565 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007566 if (err) {
7567 failed_cpu = cpu;
7568 goto fail;
7569 }
7570 }
7571 put_online_cpus();
7572
7573 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007574fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007575 for_each_possible_cpu(cpu) {
7576 if (cpu == failed_cpu)
7577 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007578 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007579 }
7580
7581 put_online_cpus();
7582 return err;
7583}
7584
Ingo Molnarc5905af2012-02-24 08:31:31 +01007585struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007586
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007587static void sw_perf_event_destroy(struct perf_event *event)
7588{
7589 u64 event_id = event->attr.config;
7590
7591 WARN_ON(event->parent);
7592
Ingo Molnarc5905af2012-02-24 08:31:31 +01007593 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007594 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007595}
7596
7597static int perf_swevent_init(struct perf_event *event)
7598{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007599 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007600
7601 if (event->attr.type != PERF_TYPE_SOFTWARE)
7602 return -ENOENT;
7603
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007604 /*
7605 * no branch sampling for software events
7606 */
7607 if (has_branch_stack(event))
7608 return -EOPNOTSUPP;
7609
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007610 switch (event_id) {
7611 case PERF_COUNT_SW_CPU_CLOCK:
7612 case PERF_COUNT_SW_TASK_CLOCK:
7613 return -ENOENT;
7614
7615 default:
7616 break;
7617 }
7618
Dan Carpenterce677832010-10-24 21:50:42 +02007619 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007620 return -ENOENT;
7621
7622 if (!event->parent) {
7623 int err;
7624
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007625 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007626 if (err)
7627 return err;
7628
Ingo Molnarc5905af2012-02-24 08:31:31 +01007629 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007630 event->destroy = sw_perf_event_destroy;
7631 }
7632
7633 return 0;
7634}
7635
7636static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007637 .task_ctx_nr = perf_sw_context,
7638
Peter Zijlstra34f43922015-02-20 14:05:38 +01007639 .capabilities = PERF_PMU_CAP_NO_NMI,
7640
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007641 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007642 .add = perf_swevent_add,
7643 .del = perf_swevent_del,
7644 .start = perf_swevent_start,
7645 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007646 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007647};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007648
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007649#ifdef CONFIG_EVENT_TRACING
7650
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007651static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007652 struct perf_sample_data *data)
7653{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007654 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007655
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007656 /* only top level events have filters set */
7657 if (event->parent)
7658 event = event->parent;
7659
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007660 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7661 return 1;
7662 return 0;
7663}
7664
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007665static int perf_tp_event_match(struct perf_event *event,
7666 struct perf_sample_data *data,
7667 struct pt_regs *regs)
7668{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007669 if (event->hw.state & PERF_HES_STOPPED)
7670 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007671 /*
7672 * All tracepoints are from kernel-space.
7673 */
7674 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007675 return 0;
7676
7677 if (!perf_tp_filter_match(event, data))
7678 return 0;
7679
7680 return 1;
7681}
7682
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07007683void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7684 struct trace_event_call *call, u64 count,
7685 struct pt_regs *regs, struct hlist_head *head,
7686 struct task_struct *task)
7687{
7688 struct bpf_prog *prog = call->prog;
7689
7690 if (prog) {
7691 *(struct pt_regs **)raw_data = regs;
7692 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7693 perf_swevent_put_recursion_context(rctx);
7694 return;
7695 }
7696 }
7697 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7698 rctx, task);
7699}
7700EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7701
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007702void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007703 struct pt_regs *regs, struct hlist_head *head, int rctx,
7704 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007705{
7706 struct perf_sample_data data;
7707 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007708
7709 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007710 .frag = {
7711 .size = entry_size,
7712 .data = record,
7713 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007714 };
7715
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007716 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007717 data.raw = &raw;
7718
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007719 perf_trace_buf_update(record, event_type);
7720
Sasha Levinb67bfe02013-02-27 17:06:00 -08007721 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007722 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007723 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007724 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007725
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007726 /*
7727 * If we got specified a target task, also iterate its context and
7728 * deliver this event there too.
7729 */
7730 if (task && task != current) {
7731 struct perf_event_context *ctx;
7732 struct trace_entry *entry = record;
7733
7734 rcu_read_lock();
7735 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7736 if (!ctx)
7737 goto unlock;
7738
7739 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7740 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7741 continue;
7742 if (event->attr.config != entry->type)
7743 continue;
7744 if (perf_tp_event_match(event, &data, regs))
7745 perf_swevent_event(event, count, &data, regs);
7746 }
7747unlock:
7748 rcu_read_unlock();
7749 }
7750
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007751 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007752}
7753EXPORT_SYMBOL_GPL(perf_tp_event);
7754
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007755static void tp_perf_event_destroy(struct perf_event *event)
7756{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007757 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007758}
7759
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007760static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007761{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007762 int err;
7763
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007764 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7765 return -ENOENT;
7766
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007767 /*
7768 * no branch sampling for tracepoint events
7769 */
7770 if (has_branch_stack(event))
7771 return -EOPNOTSUPP;
7772
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007773 err = perf_trace_init(event);
7774 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007775 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007776
7777 event->destroy = tp_perf_event_destroy;
7778
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007779 return 0;
7780}
7781
7782static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007783 .task_ctx_nr = perf_sw_context,
7784
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007785 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007786 .add = perf_trace_add,
7787 .del = perf_trace_del,
7788 .start = perf_swevent_start,
7789 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007790 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007791};
7792
7793static inline void perf_tp_register(void)
7794{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007795 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007796}
Li Zefan6fb29152009-10-15 11:21:42 +08007797
Li Zefan6fb29152009-10-15 11:21:42 +08007798static void perf_event_free_filter(struct perf_event *event)
7799{
7800 ftrace_profile_free_filter(event);
7801}
7802
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007803#ifdef CONFIG_BPF_SYSCALL
7804static void bpf_overflow_handler(struct perf_event *event,
7805 struct perf_sample_data *data,
7806 struct pt_regs *regs)
7807{
7808 struct bpf_perf_event_data_kern ctx = {
7809 .data = data,
7810 .regs = regs,
7811 };
7812 int ret = 0;
7813
7814 preempt_disable();
7815 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
7816 goto out;
7817 rcu_read_lock();
7818 ret = BPF_PROG_RUN(event->prog, (void *)&ctx);
7819 rcu_read_unlock();
7820out:
7821 __this_cpu_dec(bpf_prog_active);
7822 preempt_enable();
7823 if (!ret)
7824 return;
7825
7826 event->orig_overflow_handler(event, data, regs);
7827}
7828
7829static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7830{
7831 struct bpf_prog *prog;
7832
7833 if (event->overflow_handler_context)
7834 /* hw breakpoint or kernel counter */
7835 return -EINVAL;
7836
7837 if (event->prog)
7838 return -EEXIST;
7839
7840 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
7841 if (IS_ERR(prog))
7842 return PTR_ERR(prog);
7843
7844 event->prog = prog;
7845 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
7846 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
7847 return 0;
7848}
7849
7850static void perf_event_free_bpf_handler(struct perf_event *event)
7851{
7852 struct bpf_prog *prog = event->prog;
7853
7854 if (!prog)
7855 return;
7856
7857 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
7858 event->prog = NULL;
7859 bpf_prog_put(prog);
7860}
7861#else
7862static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7863{
7864 return -EOPNOTSUPP;
7865}
7866static void perf_event_free_bpf_handler(struct perf_event *event)
7867{
7868}
7869#endif
7870
Alexei Starovoitov25415172015-03-25 12:49:20 -07007871static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7872{
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007873 bool is_kprobe, is_tracepoint;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007874 struct bpf_prog *prog;
7875
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007876 if (event->attr.type == PERF_TYPE_HARDWARE ||
7877 event->attr.type == PERF_TYPE_SOFTWARE)
7878 return perf_event_set_bpf_handler(event, prog_fd);
7879
Alexei Starovoitov25415172015-03-25 12:49:20 -07007880 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7881 return -EINVAL;
7882
7883 if (event->tp_event->prog)
7884 return -EEXIST;
7885
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007886 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7887 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7888 if (!is_kprobe && !is_tracepoint)
7889 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007890 return -EINVAL;
7891
7892 prog = bpf_prog_get(prog_fd);
7893 if (IS_ERR(prog))
7894 return PTR_ERR(prog);
7895
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007896 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7897 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007898 /* valid fd, but invalid bpf program type */
7899 bpf_prog_put(prog);
7900 return -EINVAL;
7901 }
7902
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07007903 if (is_tracepoint) {
7904 int off = trace_event_get_offsets(event->tp_event);
7905
7906 if (prog->aux->max_ctx_offset > off) {
7907 bpf_prog_put(prog);
7908 return -EACCES;
7909 }
7910 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07007911 event->tp_event->prog = prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007912 event->tp_event->bpf_prog_owner = event;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007913
7914 return 0;
7915}
7916
7917static void perf_event_free_bpf_prog(struct perf_event *event)
7918{
7919 struct bpf_prog *prog;
7920
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007921 perf_event_free_bpf_handler(event);
7922
Alexei Starovoitov25415172015-03-25 12:49:20 -07007923 if (!event->tp_event)
7924 return;
7925
7926 prog = event->tp_event->prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007927 if (prog && event->tp_event->bpf_prog_owner == event) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007928 event->tp_event->prog = NULL;
Daniel Borkmann1aacde32016-06-30 17:24:43 +02007929 bpf_prog_put(prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07007930 }
7931}
7932
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007933#else
Li Zefan6fb29152009-10-15 11:21:42 +08007934
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007935static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007936{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007937}
Li Zefan6fb29152009-10-15 11:21:42 +08007938
Li Zefan6fb29152009-10-15 11:21:42 +08007939static void perf_event_free_filter(struct perf_event *event)
7940{
7941}
7942
Alexei Starovoitov25415172015-03-25 12:49:20 -07007943static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7944{
7945 return -ENOENT;
7946}
7947
7948static void perf_event_free_bpf_prog(struct perf_event *event)
7949{
7950}
Li Zefan07b139c2009-12-21 14:27:35 +08007951#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007952
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007953#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007954void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007955{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007956 struct perf_sample_data sample;
7957 struct pt_regs *regs = data;
7958
Robert Richterfd0d0002012-04-02 20:19:08 +02007959 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007960
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007961 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007962 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007963}
7964#endif
7965
Alexander Shishkin375637b2016-04-27 18:44:46 +03007966/*
7967 * Allocate a new address filter
7968 */
7969static struct perf_addr_filter *
7970perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7971{
7972 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7973 struct perf_addr_filter *filter;
7974
7975 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7976 if (!filter)
7977 return NULL;
7978
7979 INIT_LIST_HEAD(&filter->entry);
7980 list_add_tail(&filter->entry, filters);
7981
7982 return filter;
7983}
7984
7985static void free_filters_list(struct list_head *filters)
7986{
7987 struct perf_addr_filter *filter, *iter;
7988
7989 list_for_each_entry_safe(filter, iter, filters, entry) {
7990 if (filter->inode)
7991 iput(filter->inode);
7992 list_del(&filter->entry);
7993 kfree(filter);
7994 }
7995}
7996
7997/*
7998 * Free existing address filters and optionally install new ones
7999 */
8000static void perf_addr_filters_splice(struct perf_event *event,
8001 struct list_head *head)
8002{
8003 unsigned long flags;
8004 LIST_HEAD(list);
8005
8006 if (!has_addr_filter(event))
8007 return;
8008
8009 /* don't bother with children, they don't have their own filters */
8010 if (event->parent)
8011 return;
8012
8013 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
8014
8015 list_splice_init(&event->addr_filters.list, &list);
8016 if (head)
8017 list_splice(head, &event->addr_filters.list);
8018
8019 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
8020
8021 free_filters_list(&list);
8022}
8023
8024/*
8025 * Scan through mm's vmas and see if one of them matches the
8026 * @filter; if so, adjust filter's address range.
8027 * Called with mm::mmap_sem down for reading.
8028 */
8029static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
8030 struct mm_struct *mm)
8031{
8032 struct vm_area_struct *vma;
8033
8034 for (vma = mm->mmap; vma; vma = vma->vm_next) {
8035 struct file *file = vma->vm_file;
8036 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
8037 unsigned long vma_size = vma->vm_end - vma->vm_start;
8038
8039 if (!file)
8040 continue;
8041
8042 if (!perf_addr_filter_match(filter, file, off, vma_size))
8043 continue;
8044
8045 return vma->vm_start;
8046 }
8047
8048 return 0;
8049}
8050
8051/*
8052 * Update event's address range filters based on the
8053 * task's existing mappings, if any.
8054 */
8055static void perf_event_addr_filters_apply(struct perf_event *event)
8056{
8057 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
8058 struct task_struct *task = READ_ONCE(event->ctx->task);
8059 struct perf_addr_filter *filter;
8060 struct mm_struct *mm = NULL;
8061 unsigned int count = 0;
8062 unsigned long flags;
8063
8064 /*
8065 * We may observe TASK_TOMBSTONE, which means that the event tear-down
8066 * will stop on the parent's child_mutex that our caller is also holding
8067 */
8068 if (task == TASK_TOMBSTONE)
8069 return;
8070
8071 mm = get_task_mm(event->ctx->task);
8072 if (!mm)
8073 goto restart;
8074
8075 down_read(&mm->mmap_sem);
8076
8077 raw_spin_lock_irqsave(&ifh->lock, flags);
8078 list_for_each_entry(filter, &ifh->list, entry) {
8079 event->addr_filters_offs[count] = 0;
8080
Mathieu Poirier99f5bc92016-07-18 10:43:07 -06008081 /*
8082 * Adjust base offset if the filter is associated to a binary
8083 * that needs to be mapped:
8084 */
8085 if (filter->inode)
Alexander Shishkin375637b2016-04-27 18:44:46 +03008086 event->addr_filters_offs[count] =
8087 perf_addr_filter_apply(filter, mm);
8088
8089 count++;
8090 }
8091
8092 event->addr_filters_gen++;
8093 raw_spin_unlock_irqrestore(&ifh->lock, flags);
8094
8095 up_read(&mm->mmap_sem);
8096
8097 mmput(mm);
8098
8099restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03008100 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008101}
8102
8103/*
8104 * Address range filtering: limiting the data to certain
8105 * instruction address ranges. Filters are ioctl()ed to us from
8106 * userspace as ascii strings.
8107 *
8108 * Filter string format:
8109 *
8110 * ACTION RANGE_SPEC
8111 * where ACTION is one of the
8112 * * "filter": limit the trace to this region
8113 * * "start": start tracing from this address
8114 * * "stop": stop tracing at this address/region;
8115 * RANGE_SPEC is
8116 * * for kernel addresses: <start address>[/<size>]
8117 * * for object files: <start address>[/<size>]@</path/to/object/file>
8118 *
8119 * if <size> is not specified, the range is treated as a single address.
8120 */
8121enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02008122 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03008123 IF_ACT_FILTER,
8124 IF_ACT_START,
8125 IF_ACT_STOP,
8126 IF_SRC_FILE,
8127 IF_SRC_KERNEL,
8128 IF_SRC_FILEADDR,
8129 IF_SRC_KERNELADDR,
8130};
8131
8132enum {
8133 IF_STATE_ACTION = 0,
8134 IF_STATE_SOURCE,
8135 IF_STATE_END,
8136};
8137
8138static const match_table_t if_tokens = {
8139 { IF_ACT_FILTER, "filter" },
8140 { IF_ACT_START, "start" },
8141 { IF_ACT_STOP, "stop" },
8142 { IF_SRC_FILE, "%u/%u@%s" },
8143 { IF_SRC_KERNEL, "%u/%u" },
8144 { IF_SRC_FILEADDR, "%u@%s" },
8145 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +02008146 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +03008147};
8148
8149/*
8150 * Address filter string parser
8151 */
8152static int
8153perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
8154 struct list_head *filters)
8155{
8156 struct perf_addr_filter *filter = NULL;
8157 char *start, *orig, *filename = NULL;
8158 struct path path;
8159 substring_t args[MAX_OPT_ARGS];
8160 int state = IF_STATE_ACTION, token;
8161 unsigned int kernel = 0;
8162 int ret = -EINVAL;
8163
8164 orig = fstr = kstrdup(fstr, GFP_KERNEL);
8165 if (!fstr)
8166 return -ENOMEM;
8167
8168 while ((start = strsep(&fstr, " ,\n")) != NULL) {
8169 ret = -EINVAL;
8170
8171 if (!*start)
8172 continue;
8173
8174 /* filter definition begins */
8175 if (state == IF_STATE_ACTION) {
8176 filter = perf_addr_filter_new(event, filters);
8177 if (!filter)
8178 goto fail;
8179 }
8180
8181 token = match_token(start, if_tokens, args);
8182 switch (token) {
8183 case IF_ACT_FILTER:
8184 case IF_ACT_START:
8185 filter->filter = 1;
8186
8187 case IF_ACT_STOP:
8188 if (state != IF_STATE_ACTION)
8189 goto fail;
8190
8191 state = IF_STATE_SOURCE;
8192 break;
8193
8194 case IF_SRC_KERNELADDR:
8195 case IF_SRC_KERNEL:
8196 kernel = 1;
8197
8198 case IF_SRC_FILEADDR:
8199 case IF_SRC_FILE:
8200 if (state != IF_STATE_SOURCE)
8201 goto fail;
8202
8203 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
8204 filter->range = 1;
8205
8206 *args[0].to = 0;
8207 ret = kstrtoul(args[0].from, 0, &filter->offset);
8208 if (ret)
8209 goto fail;
8210
8211 if (filter->range) {
8212 *args[1].to = 0;
8213 ret = kstrtoul(args[1].from, 0, &filter->size);
8214 if (ret)
8215 goto fail;
8216 }
8217
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06008218 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
8219 int fpos = filter->range ? 2 : 1;
8220
8221 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008222 if (!filename) {
8223 ret = -ENOMEM;
8224 goto fail;
8225 }
8226 }
8227
8228 state = IF_STATE_END;
8229 break;
8230
8231 default:
8232 goto fail;
8233 }
8234
8235 /*
8236 * Filter definition is fully parsed, validate and install it.
8237 * Make sure that it doesn't contradict itself or the event's
8238 * attribute.
8239 */
8240 if (state == IF_STATE_END) {
8241 if (kernel && event->attr.exclude_kernel)
8242 goto fail;
8243
8244 if (!kernel) {
8245 if (!filename)
8246 goto fail;
8247
8248 /* look up the path and grab its inode */
8249 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
8250 if (ret)
8251 goto fail_free_name;
8252
8253 filter->inode = igrab(d_inode(path.dentry));
8254 path_put(&path);
8255 kfree(filename);
8256 filename = NULL;
8257
8258 ret = -EINVAL;
8259 if (!filter->inode ||
8260 !S_ISREG(filter->inode->i_mode))
8261 /* free_filters_list() will iput() */
8262 goto fail;
8263 }
8264
8265 /* ready to consume more filters */
8266 state = IF_STATE_ACTION;
8267 filter = NULL;
8268 }
8269 }
8270
8271 if (state != IF_STATE_ACTION)
8272 goto fail;
8273
8274 kfree(orig);
8275
8276 return 0;
8277
8278fail_free_name:
8279 kfree(filename);
8280fail:
8281 free_filters_list(filters);
8282 kfree(orig);
8283
8284 return ret;
8285}
8286
8287static int
8288perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
8289{
8290 LIST_HEAD(filters);
8291 int ret;
8292
8293 /*
8294 * Since this is called in perf_ioctl() path, we're already holding
8295 * ctx::mutex.
8296 */
8297 lockdep_assert_held(&event->ctx->mutex);
8298
8299 if (WARN_ON_ONCE(event->parent))
8300 return -EINVAL;
8301
8302 /*
8303 * For now, we only support filtering in per-task events; doing so
8304 * for CPU-wide events requires additional context switching trickery,
8305 * since same object code will be mapped at different virtual
8306 * addresses in different processes.
8307 */
8308 if (!event->ctx->task)
8309 return -EOPNOTSUPP;
8310
8311 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
8312 if (ret)
8313 return ret;
8314
8315 ret = event->pmu->addr_filters_validate(&filters);
8316 if (ret) {
8317 free_filters_list(&filters);
8318 return ret;
8319 }
8320
8321 /* remove existing filters, if any */
8322 perf_addr_filters_splice(event, &filters);
8323
8324 /* install new filters */
8325 perf_event_for_each_child(event, perf_event_addr_filters_apply);
8326
8327 return ret;
8328}
8329
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008330static int perf_event_set_filter(struct perf_event *event, void __user *arg)
8331{
8332 char *filter_str;
8333 int ret = -EINVAL;
8334
Alexander Shishkin375637b2016-04-27 18:44:46 +03008335 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
8336 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
8337 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008338 return -EINVAL;
8339
8340 filter_str = strndup_user(arg, PAGE_SIZE);
8341 if (IS_ERR(filter_str))
8342 return PTR_ERR(filter_str);
8343
8344 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
8345 event->attr.type == PERF_TYPE_TRACEPOINT)
8346 ret = ftrace_profile_set_filter(event, event->attr.config,
8347 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008348 else if (has_addr_filter(event))
8349 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008350
8351 kfree(filter_str);
8352 return ret;
8353}
8354
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008355/*
8356 * hrtimer based swevent callback
8357 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008358
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008359static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008360{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008361 enum hrtimer_restart ret = HRTIMER_RESTART;
8362 struct perf_sample_data data;
8363 struct pt_regs *regs;
8364 struct perf_event *event;
8365 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008366
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008367 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008368
8369 if (event->state != PERF_EVENT_STATE_ACTIVE)
8370 return HRTIMER_NORESTART;
8371
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008372 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008373
Robert Richterfd0d0002012-04-02 20:19:08 +02008374 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008375 regs = get_irq_regs();
8376
8377 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08008378 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02008379 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008380 ret = HRTIMER_NORESTART;
8381 }
8382
8383 period = max_t(u64, 10000, event->hw.sample_period);
8384 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
8385
8386 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008387}
8388
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008389static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008390{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008391 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008392 s64 period;
8393
8394 if (!is_sampling_event(event))
8395 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008396
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008397 period = local64_read(&hwc->period_left);
8398 if (period) {
8399 if (period < 0)
8400 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008401
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008402 local64_set(&hwc->period_left, 0);
8403 } else {
8404 period = max_t(u64, 10000, hwc->sample_period);
8405 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00008406 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8407 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008408}
8409
8410static void perf_swevent_cancel_hrtimer(struct perf_event *event)
8411{
8412 struct hw_perf_event *hwc = &event->hw;
8413
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008414 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008415 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008416 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008417
8418 hrtimer_cancel(&hwc->hrtimer);
8419 }
8420}
8421
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008422static void perf_swevent_init_hrtimer(struct perf_event *event)
8423{
8424 struct hw_perf_event *hwc = &event->hw;
8425
8426 if (!is_sampling_event(event))
8427 return;
8428
8429 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8430 hwc->hrtimer.function = perf_swevent_hrtimer;
8431
8432 /*
8433 * Since hrtimers have a fixed rate, we can do a static freq->period
8434 * mapping and avoid the whole period adjust feedback stuff.
8435 */
8436 if (event->attr.freq) {
8437 long freq = event->attr.sample_freq;
8438
8439 event->attr.sample_period = NSEC_PER_SEC / freq;
8440 hwc->sample_period = event->attr.sample_period;
8441 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008442 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008443 event->attr.freq = 0;
8444 }
8445}
8446
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008447/*
8448 * Software event: cpu wall time clock
8449 */
8450
8451static void cpu_clock_event_update(struct perf_event *event)
8452{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008453 s64 prev;
8454 u64 now;
8455
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008456 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008457 prev = local64_xchg(&event->hw.prev_count, now);
8458 local64_add(now - prev, &event->count);
8459}
8460
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008461static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008462{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008463 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008464 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008465}
8466
8467static void cpu_clock_event_stop(struct perf_event *event, int flags)
8468{
8469 perf_swevent_cancel_hrtimer(event);
8470 cpu_clock_event_update(event);
8471}
8472
8473static int cpu_clock_event_add(struct perf_event *event, int flags)
8474{
8475 if (flags & PERF_EF_START)
8476 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008477 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008478
8479 return 0;
8480}
8481
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008482static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008483{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008484 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008485}
8486
8487static void cpu_clock_event_read(struct perf_event *event)
8488{
8489 cpu_clock_event_update(event);
8490}
8491
8492static int cpu_clock_event_init(struct perf_event *event)
8493{
8494 if (event->attr.type != PERF_TYPE_SOFTWARE)
8495 return -ENOENT;
8496
8497 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8498 return -ENOENT;
8499
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008500 /*
8501 * no branch sampling for software events
8502 */
8503 if (has_branch_stack(event))
8504 return -EOPNOTSUPP;
8505
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008506 perf_swevent_init_hrtimer(event);
8507
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008508 return 0;
8509}
8510
8511static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008512 .task_ctx_nr = perf_sw_context,
8513
Peter Zijlstra34f43922015-02-20 14:05:38 +01008514 .capabilities = PERF_PMU_CAP_NO_NMI,
8515
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008516 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008517 .add = cpu_clock_event_add,
8518 .del = cpu_clock_event_del,
8519 .start = cpu_clock_event_start,
8520 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008521 .read = cpu_clock_event_read,
8522};
8523
8524/*
8525 * Software event: task time clock
8526 */
8527
8528static void task_clock_event_update(struct perf_event *event, u64 now)
8529{
8530 u64 prev;
8531 s64 delta;
8532
8533 prev = local64_xchg(&event->hw.prev_count, now);
8534 delta = now - prev;
8535 local64_add(delta, &event->count);
8536}
8537
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008538static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008539{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008540 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008541 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008542}
8543
8544static void task_clock_event_stop(struct perf_event *event, int flags)
8545{
8546 perf_swevent_cancel_hrtimer(event);
8547 task_clock_event_update(event, event->ctx->time);
8548}
8549
8550static int task_clock_event_add(struct perf_event *event, int flags)
8551{
8552 if (flags & PERF_EF_START)
8553 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008554 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008555
8556 return 0;
8557}
8558
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008559static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008560{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008561 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008562}
8563
8564static void task_clock_event_read(struct perf_event *event)
8565{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008566 u64 now = perf_clock();
8567 u64 delta = now - event->ctx->timestamp;
8568 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008569
8570 task_clock_event_update(event, time);
8571}
8572
8573static int task_clock_event_init(struct perf_event *event)
8574{
8575 if (event->attr.type != PERF_TYPE_SOFTWARE)
8576 return -ENOENT;
8577
8578 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8579 return -ENOENT;
8580
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008581 /*
8582 * no branch sampling for software events
8583 */
8584 if (has_branch_stack(event))
8585 return -EOPNOTSUPP;
8586
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008587 perf_swevent_init_hrtimer(event);
8588
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008589 return 0;
8590}
8591
8592static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008593 .task_ctx_nr = perf_sw_context,
8594
Peter Zijlstra34f43922015-02-20 14:05:38 +01008595 .capabilities = PERF_PMU_CAP_NO_NMI,
8596
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008597 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008598 .add = task_clock_event_add,
8599 .del = task_clock_event_del,
8600 .start = task_clock_event_start,
8601 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008602 .read = task_clock_event_read,
8603};
8604
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008605static void perf_pmu_nop_void(struct pmu *pmu)
8606{
8607}
8608
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008609static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8610{
8611}
8612
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008613static int perf_pmu_nop_int(struct pmu *pmu)
8614{
8615 return 0;
8616}
8617
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008618static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008619
8620static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008621{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008622 __this_cpu_write(nop_txn_flags, flags);
8623
8624 if (flags & ~PERF_PMU_TXN_ADD)
8625 return;
8626
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008627 perf_pmu_disable(pmu);
8628}
8629
8630static int perf_pmu_commit_txn(struct pmu *pmu)
8631{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008632 unsigned int flags = __this_cpu_read(nop_txn_flags);
8633
8634 __this_cpu_write(nop_txn_flags, 0);
8635
8636 if (flags & ~PERF_PMU_TXN_ADD)
8637 return 0;
8638
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008639 perf_pmu_enable(pmu);
8640 return 0;
8641}
8642
8643static void perf_pmu_cancel_txn(struct pmu *pmu)
8644{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008645 unsigned int flags = __this_cpu_read(nop_txn_flags);
8646
8647 __this_cpu_write(nop_txn_flags, 0);
8648
8649 if (flags & ~PERF_PMU_TXN_ADD)
8650 return;
8651
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008652 perf_pmu_enable(pmu);
8653}
8654
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008655static int perf_event_idx_default(struct perf_event *event)
8656{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008657 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008658}
8659
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008660/*
8661 * Ensures all contexts with the same task_ctx_nr have the same
8662 * pmu_cpu_context too.
8663 */
Mark Rutland9e317042014-02-10 17:44:18 +00008664static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008665{
8666 struct pmu *pmu;
8667
8668 if (ctxn < 0)
8669 return NULL;
8670
8671 list_for_each_entry(pmu, &pmus, entry) {
8672 if (pmu->task_ctx_nr == ctxn)
8673 return pmu->pmu_cpu_context;
8674 }
8675
8676 return NULL;
8677}
8678
Peter Zijlstra51676952010-12-07 14:18:20 +01008679static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008680{
Peter Zijlstra51676952010-12-07 14:18:20 +01008681 int cpu;
8682
8683 for_each_possible_cpu(cpu) {
8684 struct perf_cpu_context *cpuctx;
8685
8686 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8687
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008688 if (cpuctx->unique_pmu == old_pmu)
8689 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008690 }
8691}
8692
8693static void free_pmu_context(struct pmu *pmu)
8694{
8695 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008696
8697 mutex_lock(&pmus_lock);
8698 /*
8699 * Like a real lame refcount.
8700 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008701 list_for_each_entry(i, &pmus, entry) {
8702 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8703 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008704 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008705 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008706 }
8707
Peter Zijlstra51676952010-12-07 14:18:20 +01008708 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008709out:
8710 mutex_unlock(&pmus_lock);
8711}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008712
8713/*
8714 * Let userspace know that this PMU supports address range filtering:
8715 */
8716static ssize_t nr_addr_filters_show(struct device *dev,
8717 struct device_attribute *attr,
8718 char *page)
8719{
8720 struct pmu *pmu = dev_get_drvdata(dev);
8721
8722 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8723}
8724DEVICE_ATTR_RO(nr_addr_filters);
8725
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008726static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008727
Peter Zijlstraabe43402010-11-17 23:17:37 +01008728static ssize_t
8729type_show(struct device *dev, struct device_attribute *attr, char *page)
8730{
8731 struct pmu *pmu = dev_get_drvdata(dev);
8732
8733 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8734}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008735static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008736
Stephane Eranian62b85632013-04-03 14:21:34 +02008737static ssize_t
8738perf_event_mux_interval_ms_show(struct device *dev,
8739 struct device_attribute *attr,
8740 char *page)
8741{
8742 struct pmu *pmu = dev_get_drvdata(dev);
8743
8744 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8745}
8746
Peter Zijlstra272325c2015-04-15 11:41:58 +02008747static DEFINE_MUTEX(mux_interval_mutex);
8748
Stephane Eranian62b85632013-04-03 14:21:34 +02008749static ssize_t
8750perf_event_mux_interval_ms_store(struct device *dev,
8751 struct device_attribute *attr,
8752 const char *buf, size_t count)
8753{
8754 struct pmu *pmu = dev_get_drvdata(dev);
8755 int timer, cpu, ret;
8756
8757 ret = kstrtoint(buf, 0, &timer);
8758 if (ret)
8759 return ret;
8760
8761 if (timer < 1)
8762 return -EINVAL;
8763
8764 /* same value, noting to do */
8765 if (timer == pmu->hrtimer_interval_ms)
8766 return count;
8767
Peter Zijlstra272325c2015-04-15 11:41:58 +02008768 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008769 pmu->hrtimer_interval_ms = timer;
8770
8771 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008772 get_online_cpus();
8773 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008774 struct perf_cpu_context *cpuctx;
8775 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8776 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8777
Peter Zijlstra272325c2015-04-15 11:41:58 +02008778 cpu_function_call(cpu,
8779 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008780 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008781 put_online_cpus();
8782 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008783
8784 return count;
8785}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008786static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008787
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008788static struct attribute *pmu_dev_attrs[] = {
8789 &dev_attr_type.attr,
8790 &dev_attr_perf_event_mux_interval_ms.attr,
8791 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008792};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008793ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008794
8795static int pmu_bus_running;
8796static struct bus_type pmu_bus = {
8797 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008798 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008799};
8800
8801static void pmu_dev_release(struct device *dev)
8802{
8803 kfree(dev);
8804}
8805
8806static int pmu_dev_alloc(struct pmu *pmu)
8807{
8808 int ret = -ENOMEM;
8809
8810 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8811 if (!pmu->dev)
8812 goto out;
8813
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008814 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008815 device_initialize(pmu->dev);
8816 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8817 if (ret)
8818 goto free_dev;
8819
8820 dev_set_drvdata(pmu->dev, pmu);
8821 pmu->dev->bus = &pmu_bus;
8822 pmu->dev->release = pmu_dev_release;
8823 ret = device_add(pmu->dev);
8824 if (ret)
8825 goto free_dev;
8826
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008827 /* For PMUs with address filters, throw in an extra attribute: */
8828 if (pmu->nr_addr_filters)
8829 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8830
8831 if (ret)
8832 goto del_dev;
8833
Peter Zijlstraabe43402010-11-17 23:17:37 +01008834out:
8835 return ret;
8836
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008837del_dev:
8838 device_del(pmu->dev);
8839
Peter Zijlstraabe43402010-11-17 23:17:37 +01008840free_dev:
8841 put_device(pmu->dev);
8842 goto out;
8843}
8844
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008845static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008846static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008847
Mischa Jonker03d8e802013-06-04 11:45:48 +02008848int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008849{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008850 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008851
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008852 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008853 ret = -ENOMEM;
8854 pmu->pmu_disable_count = alloc_percpu(int);
8855 if (!pmu->pmu_disable_count)
8856 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008857
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008858 pmu->type = -1;
8859 if (!name)
8860 goto skip_type;
8861 pmu->name = name;
8862
8863 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008864 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8865 if (type < 0) {
8866 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008867 goto free_pdc;
8868 }
8869 }
8870 pmu->type = type;
8871
Peter Zijlstraabe43402010-11-17 23:17:37 +01008872 if (pmu_bus_running) {
8873 ret = pmu_dev_alloc(pmu);
8874 if (ret)
8875 goto free_idr;
8876 }
8877
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008878skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008879 if (pmu->task_ctx_nr == perf_hw_context) {
8880 static int hw_context_taken = 0;
8881
Mark Rutland5101ef22016-04-26 11:33:46 +01008882 /*
8883 * Other than systems with heterogeneous CPUs, it never makes
8884 * sense for two PMUs to share perf_hw_context. PMUs which are
8885 * uncore must use perf_invalid_context.
8886 */
8887 if (WARN_ON_ONCE(hw_context_taken &&
8888 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008889 pmu->task_ctx_nr = perf_invalid_context;
8890
8891 hw_context_taken = 1;
8892 }
8893
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008894 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8895 if (pmu->pmu_cpu_context)
8896 goto got_cpu_context;
8897
Wei Yongjunc4814202013-04-12 11:05:54 +08008898 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008899 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8900 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008901 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008902
8903 for_each_possible_cpu(cpu) {
8904 struct perf_cpu_context *cpuctx;
8905
8906 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008907 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008908 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008909 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008910 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008911
Peter Zijlstra272325c2015-04-15 11:41:58 +02008912 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008913
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008914 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008915 }
8916
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008917got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008918 if (!pmu->start_txn) {
8919 if (pmu->pmu_enable) {
8920 /*
8921 * If we have pmu_enable/pmu_disable calls, install
8922 * transaction stubs that use that to try and batch
8923 * hardware accesses.
8924 */
8925 pmu->start_txn = perf_pmu_start_txn;
8926 pmu->commit_txn = perf_pmu_commit_txn;
8927 pmu->cancel_txn = perf_pmu_cancel_txn;
8928 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008929 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008930 pmu->commit_txn = perf_pmu_nop_int;
8931 pmu->cancel_txn = perf_pmu_nop_void;
8932 }
8933 }
8934
8935 if (!pmu->pmu_enable) {
8936 pmu->pmu_enable = perf_pmu_nop_void;
8937 pmu->pmu_disable = perf_pmu_nop_void;
8938 }
8939
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008940 if (!pmu->event_idx)
8941 pmu->event_idx = perf_event_idx_default;
8942
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008943 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008944 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008945 ret = 0;
8946unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008947 mutex_unlock(&pmus_lock);
8948
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008949 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008950
Peter Zijlstraabe43402010-11-17 23:17:37 +01008951free_dev:
8952 device_del(pmu->dev);
8953 put_device(pmu->dev);
8954
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008955free_idr:
8956 if (pmu->type >= PERF_TYPE_MAX)
8957 idr_remove(&pmu_idr, pmu->type);
8958
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008959free_pdc:
8960 free_percpu(pmu->pmu_disable_count);
8961 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008962}
Yan, Zhengc464c762014-03-18 16:56:41 +08008963EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008964
8965void perf_pmu_unregister(struct pmu *pmu)
8966{
Jiri Olsa09338402016-10-20 13:10:11 +02008967 int remove_device;
8968
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008969 mutex_lock(&pmus_lock);
Jiri Olsa09338402016-10-20 13:10:11 +02008970 remove_device = pmu_bus_running;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008971 list_del_rcu(&pmu->entry);
8972 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008973
8974 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02008975 * We dereference the pmu list under both SRCU and regular RCU, so
8976 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008977 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008978 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02008979 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008980
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008981 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008982 if (pmu->type >= PERF_TYPE_MAX)
8983 idr_remove(&pmu_idr, pmu->type);
Jiri Olsa09338402016-10-20 13:10:11 +02008984 if (remove_device) {
8985 if (pmu->nr_addr_filters)
8986 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
8987 device_del(pmu->dev);
8988 put_device(pmu->dev);
8989 }
Peter Zijlstra51676952010-12-07 14:18:20 +01008990 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008991}
Yan, Zhengc464c762014-03-18 16:56:41 +08008992EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008993
Mark Rutlandcc34b982015-01-07 14:56:51 +00008994static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
8995{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008996 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00008997 int ret;
8998
8999 if (!try_module_get(pmu->module))
9000 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009001
9002 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02009003 /*
9004 * This ctx->mutex can nest when we're called through
9005 * inheritance. See the perf_event_ctx_lock_nested() comment.
9006 */
9007 ctx = perf_event_ctx_lock_nested(event->group_leader,
9008 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009009 BUG_ON(!ctx);
9010 }
9011
Mark Rutlandcc34b982015-01-07 14:56:51 +00009012 event->pmu = pmu;
9013 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009014
9015 if (ctx)
9016 perf_event_ctx_unlock(event->group_leader, ctx);
9017
Mark Rutlandcc34b982015-01-07 14:56:51 +00009018 if (ret)
9019 module_put(pmu->module);
9020
9021 return ret;
9022}
9023
Geliang Tang18ab2cd2015-09-27 23:25:50 +08009024static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009025{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009026 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009027 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08009028 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009029
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009030 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009031
9032 rcu_read_lock();
9033 pmu = idr_find(&pmu_idr, event->attr.type);
9034 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08009035 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009036 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08009037 if (ret)
9038 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009039 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08009040 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009041
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009042 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009043 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009044 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009045 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009046
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009047 if (ret != -ENOENT) {
9048 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009049 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009050 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009051 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009052 pmu = ERR_PTR(-ENOENT);
9053unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009054 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009055
9056 return pmu;
9057}
9058
Kan Liangf2fb6be2016-03-23 11:24:37 -07009059static void attach_sb_event(struct perf_event *event)
9060{
9061 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
9062
9063 raw_spin_lock(&pel->lock);
9064 list_add_rcu(&event->sb_list, &pel->list);
9065 raw_spin_unlock(&pel->lock);
9066}
9067
Peter Zijlstraaab5b712016-05-12 17:26:46 +02009068/*
9069 * We keep a list of all !task (and therefore per-cpu) events
9070 * that need to receive side-band records.
9071 *
9072 * This avoids having to scan all the various PMU per-cpu contexts
9073 * looking for them.
9074 */
Kan Liangf2fb6be2016-03-23 11:24:37 -07009075static void account_pmu_sb_event(struct perf_event *event)
9076{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07009077 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -07009078 attach_sb_event(event);
9079}
9080
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009081static void account_event_cpu(struct perf_event *event, int cpu)
9082{
9083 if (event->parent)
9084 return;
9085
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009086 if (is_cgroup_event(event))
9087 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
9088}
9089
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009090/* Freq events need the tick to stay alive (see perf_event_task_tick). */
9091static void account_freq_event_nohz(void)
9092{
9093#ifdef CONFIG_NO_HZ_FULL
9094 /* Lock so we don't race with concurrent unaccount */
9095 spin_lock(&nr_freq_lock);
9096 if (atomic_inc_return(&nr_freq_events) == 1)
9097 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
9098 spin_unlock(&nr_freq_lock);
9099#endif
9100}
9101
9102static void account_freq_event(void)
9103{
9104 if (tick_nohz_full_enabled())
9105 account_freq_event_nohz();
9106 else
9107 atomic_inc(&nr_freq_events);
9108}
9109
9110
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009111static void account_event(struct perf_event *event)
9112{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009113 bool inc = false;
9114
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009115 if (event->parent)
9116 return;
9117
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009118 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009119 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009120 if (event->attr.mmap || event->attr.mmap_data)
9121 atomic_inc(&nr_mmap_events);
9122 if (event->attr.comm)
9123 atomic_inc(&nr_comm_events);
9124 if (event->attr.task)
9125 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009126 if (event->attr.freq)
9127 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03009128 if (event->attr.context_switch) {
9129 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009130 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03009131 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009132 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009133 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009134 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009135 inc = true;
9136
Peter Zijlstra9107c892016-02-24 18:45:45 +01009137 if (inc) {
9138 if (atomic_inc_not_zero(&perf_sched_count))
9139 goto enabled;
9140
9141 mutex_lock(&perf_sched_mutex);
9142 if (!atomic_read(&perf_sched_count)) {
9143 static_branch_enable(&perf_sched_events);
9144 /*
9145 * Guarantee that all CPUs observe they key change and
9146 * call the perf scheduling hooks before proceeding to
9147 * install events that need them.
9148 */
9149 synchronize_sched();
9150 }
9151 /*
9152 * Now that we have waited for the sync_sched(), allow further
9153 * increments to by-pass the mutex.
9154 */
9155 atomic_inc(&perf_sched_count);
9156 mutex_unlock(&perf_sched_mutex);
9157 }
9158enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009159
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009160 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07009161
9162 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009163}
9164
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009165/*
9166 * Allocate and initialize a event structure
9167 */
9168static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009169perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009170 struct task_struct *task,
9171 struct perf_event *group_leader,
9172 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009173 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00009174 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009175{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009176 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009177 struct perf_event *event;
9178 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009179 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009180
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009181 if ((unsigned)cpu >= nr_cpu_ids) {
9182 if (!task || cpu != -1)
9183 return ERR_PTR(-EINVAL);
9184 }
9185
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009186 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009187 if (!event)
9188 return ERR_PTR(-ENOMEM);
9189
9190 /*
9191 * Single events are their own group leaders, with an
9192 * empty sibling list:
9193 */
9194 if (!group_leader)
9195 group_leader = event;
9196
9197 mutex_init(&event->child_mutex);
9198 INIT_LIST_HEAD(&event->child_list);
9199
9200 INIT_LIST_HEAD(&event->group_entry);
9201 INIT_LIST_HEAD(&event->event_entry);
9202 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009203 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01009204 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009205 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01009206 INIT_HLIST_NODE(&event->hlist_entry);
9207
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009208
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009209 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08009210 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009211
9212 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009213 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009214
Al Viroa6fa9412012-08-20 14:59:25 +01009215 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009216 event->cpu = cpu;
9217 event->attr = *attr;
9218 event->group_leader = group_leader;
9219 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009220 event->oncpu = -1;
9221
9222 event->parent = parent_event;
9223
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08009224 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009225 event->id = atomic64_inc_return(&perf_event_id);
9226
9227 event->state = PERF_EVENT_STATE_INACTIVE;
9228
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009229 if (task) {
9230 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009231 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009232 * XXX pmu::event_init needs to know what task to account to
9233 * and we cannot use the ctx information because we need the
9234 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009235 */
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009236 get_task_struct(task);
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009237 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009238 }
9239
Peter Zijlstra34f43922015-02-20 14:05:38 +01009240 event->clock = &local_clock;
9241 if (parent_event)
9242 event->clock = parent_event->clock;
9243
Avi Kivity4dc0da82011-06-29 18:42:35 +03009244 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01009245 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009246 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +02009247#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009248 if (overflow_handler == bpf_overflow_handler) {
9249 struct bpf_prog *prog = bpf_prog_inc(parent_event->prog);
9250
9251 if (IS_ERR(prog)) {
9252 err = PTR_ERR(prog);
9253 goto err_ns;
9254 }
9255 event->prog = prog;
9256 event->orig_overflow_handler =
9257 parent_event->orig_overflow_handler;
9258 }
9259#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +03009260 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009261
Wang Nan18794452016-03-28 06:41:30 +00009262 if (overflow_handler) {
9263 event->overflow_handler = overflow_handler;
9264 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00009265 } else if (is_write_backward(event)){
9266 event->overflow_handler = perf_event_output_backward;
9267 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00009268 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00009269 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00009270 event->overflow_handler_context = NULL;
9271 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02009272
Jiri Olsa0231bb52013-02-01 11:23:45 +01009273 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009274
9275 pmu = NULL;
9276
9277 hwc = &event->hw;
9278 hwc->sample_period = attr->sample_period;
9279 if (attr->freq && attr->sample_freq)
9280 hwc->sample_period = 1;
9281 hwc->last_period = hwc->sample_period;
9282
Peter Zijlstrae7850592010-05-21 14:43:08 +02009283 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009284
9285 /*
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009286 * We currently do not support PERF_SAMPLE_READ on inherited events.
9287 * See perf_output_read().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009288 */
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009289 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009290 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009291
Yan, Zhenga46a2302014-11-04 21:56:06 -05009292 if (!has_branch_stack(event))
9293 event->attr.branch_sample_type = 0;
9294
Matt Fleming79dff512015-01-23 18:45:42 +00009295 if (cgroup_fd != -1) {
9296 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
9297 if (err)
9298 goto err_ns;
9299 }
9300
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009301 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009302 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009303 goto err_ns;
9304 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009305 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009306 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009307 }
9308
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009309 err = exclusive_event_init(event);
9310 if (err)
9311 goto err_pmu;
9312
Alexander Shishkin375637b2016-04-27 18:44:46 +03009313 if (has_addr_filter(event)) {
9314 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
9315 sizeof(unsigned long),
9316 GFP_KERNEL);
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009317 if (!event->addr_filters_offs) {
9318 err = -ENOMEM;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009319 goto err_per_task;
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009320 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009321
9322 /* force hw sync on the address filters */
9323 event->addr_filters_gen = 1;
9324 }
9325
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009326 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009327 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009328 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009329 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009330 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01009331 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009332 }
9333
Alexander Shishkin927a5572016-03-02 13:24:14 +02009334 /* symmetric to unaccount_event() in _free_event() */
9335 account_event(event);
9336
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009337 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009338
Alexander Shishkin375637b2016-04-27 18:44:46 +03009339err_addr_filters:
9340 kfree(event->addr_filters_offs);
9341
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009342err_per_task:
9343 exclusive_event_destroy(event);
9344
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009345err_pmu:
9346 if (event->destroy)
9347 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08009348 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009349err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00009350 if (is_cgroup_event(event))
9351 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009352 if (event->ns)
9353 put_pid_ns(event->ns);
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009354 if (event->hw.target)
9355 put_task_struct(event->hw.target);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009356 kfree(event);
9357
9358 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009359}
9360
9361static int perf_copy_attr(struct perf_event_attr __user *uattr,
9362 struct perf_event_attr *attr)
9363{
9364 u32 size;
9365 int ret;
9366
9367 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
9368 return -EFAULT;
9369
9370 /*
9371 * zero the full structure, so that a short copy will be nice.
9372 */
9373 memset(attr, 0, sizeof(*attr));
9374
9375 ret = get_user(size, &uattr->size);
9376 if (ret)
9377 return ret;
9378
9379 if (size > PAGE_SIZE) /* silly large */
9380 goto err_size;
9381
9382 if (!size) /* abi compat */
9383 size = PERF_ATTR_SIZE_VER0;
9384
9385 if (size < PERF_ATTR_SIZE_VER0)
9386 goto err_size;
9387
9388 /*
9389 * If we're handed a bigger struct than we know of,
9390 * ensure all the unknown bits are 0 - i.e. new
9391 * user-space does not rely on any kernel feature
9392 * extensions we dont know about yet.
9393 */
9394 if (size > sizeof(*attr)) {
9395 unsigned char __user *addr;
9396 unsigned char __user *end;
9397 unsigned char val;
9398
9399 addr = (void __user *)uattr + sizeof(*attr);
9400 end = (void __user *)uattr + size;
9401
9402 for (; addr < end; addr++) {
9403 ret = get_user(val, addr);
9404 if (ret)
9405 return ret;
9406 if (val)
9407 goto err_size;
9408 }
9409 size = sizeof(*attr);
9410 }
9411
9412 ret = copy_from_user(attr, uattr, size);
9413 if (ret)
9414 return -EFAULT;
9415
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05309416 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009417 return -EINVAL;
9418
9419 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
9420 return -EINVAL;
9421
9422 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
9423 return -EINVAL;
9424
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009425 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
9426 u64 mask = attr->branch_sample_type;
9427
9428 /* only using defined bits */
9429 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
9430 return -EINVAL;
9431
9432 /* at least one branch bit must be set */
9433 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9434 return -EINVAL;
9435
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009436 /* propagate priv level, when not set for branch */
9437 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9438
9439 /* exclude_kernel checked on syscall entry */
9440 if (!attr->exclude_kernel)
9441 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9442
9443 if (!attr->exclude_user)
9444 mask |= PERF_SAMPLE_BRANCH_USER;
9445
9446 if (!attr->exclude_hv)
9447 mask |= PERF_SAMPLE_BRANCH_HV;
9448 /*
9449 * adjust user setting (for HW filter setup)
9450 */
9451 attr->branch_sample_type = mask;
9452 }
Stephane Eraniane7122092013-06-06 11:02:04 +02009453 /* privileged levels capture (kernel, hv): check permissions */
9454 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02009455 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9456 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009457 }
Jiri Olsa40189942012-08-07 15:20:37 +02009458
Jiri Olsac5ebced2012-08-07 15:20:40 +02009459 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02009460 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02009461 if (ret)
9462 return ret;
9463 }
9464
9465 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9466 if (!arch_perf_have_user_stack_dump())
9467 return -ENOSYS;
9468
9469 /*
9470 * We have __u32 type for the size, but so far
9471 * we can only use __u16 as maximum due to the
9472 * __u16 sample size limit.
9473 */
9474 if (attr->sample_stack_user >= USHRT_MAX)
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009475 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009476 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009477 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009478 }
Jiri Olsa40189942012-08-07 15:20:37 +02009479
Stephane Eranian60e23642014-09-24 13:48:37 +02009480 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9481 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009482out:
9483 return ret;
9484
9485err_size:
9486 put_user(sizeof(*attr), &uattr->size);
9487 ret = -E2BIG;
9488 goto out;
9489}
9490
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009491static int
9492perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009493{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009494 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009495 int ret = -EINVAL;
9496
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009497 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009498 goto set;
9499
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009500 /* don't allow circular references */
9501 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009502 goto out;
9503
Peter Zijlstra0f139302010-05-20 14:35:15 +02009504 /*
9505 * Don't allow cross-cpu buffers
9506 */
9507 if (output_event->cpu != event->cpu)
9508 goto out;
9509
9510 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009511 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009512 */
9513 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9514 goto out;
9515
Peter Zijlstra34f43922015-02-20 14:05:38 +01009516 /*
9517 * Mixing clocks in the same buffer is trouble you don't need.
9518 */
9519 if (output_event->clock != event->clock)
9520 goto out;
9521
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009522 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009523 * Either writing ring buffer from beginning or from end.
9524 * Mixing is not allowed.
9525 */
9526 if (is_write_backward(output_event) != is_write_backward(event))
9527 goto out;
9528
9529 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009530 * If both events generate aux data, they must be on the same PMU
9531 */
9532 if (has_aux(event) && has_aux(output_event) &&
9533 event->pmu != output_event->pmu)
9534 goto out;
9535
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009536set:
9537 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009538 /* Can't redirect output if we've got an active mmap() */
9539 if (atomic_read(&event->mmap_count))
9540 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009541
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009542 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009543 /* get the rb we want to redirect to */
9544 rb = ring_buffer_get(output_event);
9545 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009546 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009547 }
9548
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009549 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009550
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009551 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009552unlock:
9553 mutex_unlock(&event->mmap_mutex);
9554
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009555out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009556 return ret;
9557}
9558
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009559static void mutex_lock_double(struct mutex *a, struct mutex *b)
9560{
9561 if (b < a)
9562 swap(a, b);
9563
9564 mutex_lock(a);
9565 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9566}
9567
Peter Zijlstra34f43922015-02-20 14:05:38 +01009568static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9569{
9570 bool nmi_safe = false;
9571
9572 switch (clk_id) {
9573 case CLOCK_MONOTONIC:
9574 event->clock = &ktime_get_mono_fast_ns;
9575 nmi_safe = true;
9576 break;
9577
9578 case CLOCK_MONOTONIC_RAW:
9579 event->clock = &ktime_get_raw_fast_ns;
9580 nmi_safe = true;
9581 break;
9582
9583 case CLOCK_REALTIME:
9584 event->clock = &ktime_get_real_ns;
9585 break;
9586
9587 case CLOCK_BOOTTIME:
9588 event->clock = &ktime_get_boot_ns;
9589 break;
9590
9591 case CLOCK_TAI:
9592 event->clock = &ktime_get_tai_ns;
9593 break;
9594
9595 default:
9596 return -EINVAL;
9597 }
9598
9599 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9600 return -EINVAL;
9601
9602 return 0;
9603}
9604
Peter Zijlstra922813f2017-01-11 21:09:50 +01009605/*
9606 * Variation on perf_event_ctx_lock_nested(), except we take two context
9607 * mutexes.
9608 */
9609static struct perf_event_context *
9610__perf_event_ctx_lock_double(struct perf_event *group_leader,
9611 struct perf_event_context *ctx)
9612{
9613 struct perf_event_context *gctx;
9614
9615again:
9616 rcu_read_lock();
9617 gctx = READ_ONCE(group_leader->ctx);
9618 if (!atomic_inc_not_zero(&gctx->refcount)) {
9619 rcu_read_unlock();
9620 goto again;
9621 }
9622 rcu_read_unlock();
9623
9624 mutex_lock_double(&gctx->mutex, &ctx->mutex);
9625
9626 if (group_leader->ctx != gctx) {
9627 mutex_unlock(&ctx->mutex);
9628 mutex_unlock(&gctx->mutex);
9629 put_ctx(gctx);
9630 goto again;
9631 }
9632
9633 return gctx;
9634}
9635
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009636/**
9637 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9638 *
9639 * @attr_uptr: event_id type attributes for monitoring/sampling
9640 * @pid: target pid
9641 * @cpu: target cpu
9642 * @group_fd: group leader event fd
9643 */
9644SYSCALL_DEFINE5(perf_event_open,
9645 struct perf_event_attr __user *, attr_uptr,
9646 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9647{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009648 struct perf_event *group_leader = NULL, *output_event = NULL;
9649 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009650 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009651 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009652 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009653 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009654 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009655 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009656 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009657 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009658 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009659 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009660 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009661
9662 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009663 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009664 return -EINVAL;
9665
9666 err = perf_copy_attr(attr_uptr, &attr);
9667 if (err)
9668 return err;
9669
9670 if (!attr.exclude_kernel) {
9671 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9672 return -EACCES;
9673 }
9674
9675 if (attr.freq) {
9676 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9677 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009678 } else {
9679 if (attr.sample_period & (1ULL << 63))
9680 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009681 }
9682
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009683 if (!attr.sample_max_stack)
9684 attr.sample_max_stack = sysctl_perf_event_max_stack;
9685
Stephane Eraniane5d13672011-02-14 11:20:01 +02009686 /*
9687 * In cgroup mode, the pid argument is used to pass the fd
9688 * opened to the cgroup directory in cgroupfs. The cpu argument
9689 * designates the cpu on which to monitor threads from that
9690 * cgroup.
9691 */
9692 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9693 return -EINVAL;
9694
Yann Droneauda21b0b32014-01-05 21:36:33 +01009695 if (flags & PERF_FLAG_FD_CLOEXEC)
9696 f_flags |= O_CLOEXEC;
9697
9698 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009699 if (event_fd < 0)
9700 return event_fd;
9701
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009702 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009703 err = perf_fget_light(group_fd, &group);
9704 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009705 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009706 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009707 if (flags & PERF_FLAG_FD_OUTPUT)
9708 output_event = group_leader;
9709 if (flags & PERF_FLAG_FD_NO_GROUP)
9710 group_leader = NULL;
9711 }
9712
Stephane Eraniane5d13672011-02-14 11:20:01 +02009713 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009714 task = find_lively_task_by_vpid(pid);
9715 if (IS_ERR(task)) {
9716 err = PTR_ERR(task);
9717 goto err_group_fd;
9718 }
9719 }
9720
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009721 if (task && group_leader &&
9722 group_leader->attr.inherit != attr.inherit) {
9723 err = -EINVAL;
9724 goto err_task;
9725 }
9726
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009727 get_online_cpus();
9728
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009729 if (task) {
9730 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9731 if (err)
9732 goto err_cpus;
9733
9734 /*
9735 * Reuse ptrace permission checks for now.
9736 *
9737 * We must hold cred_guard_mutex across this and any potential
9738 * perf_install_in_context() call for this new event to
9739 * serialize against exec() altering our credentials (and the
9740 * perf_event_exit_task() that could imply).
9741 */
9742 err = -EACCES;
9743 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9744 goto err_cred;
9745 }
9746
Matt Fleming79dff512015-01-23 18:45:42 +00009747 if (flags & PERF_FLAG_PID_CGROUP)
9748 cgroup_fd = pid;
9749
Avi Kivity4dc0da82011-06-29 18:42:35 +03009750 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009751 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009752 if (IS_ERR(event)) {
9753 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009754 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009755 }
9756
Vince Weaver53b25332014-05-16 17:12:12 -04009757 if (is_sampling_event(event)) {
9758 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +05309759 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -04009760 goto err_alloc;
9761 }
9762 }
9763
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009764 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009765 * Special case software events and allow them to be part of
9766 * any hardware group.
9767 */
9768 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009769
Peter Zijlstra34f43922015-02-20 14:05:38 +01009770 if (attr.use_clockid) {
9771 err = perf_event_set_clock(event, attr.clockid);
9772 if (err)
9773 goto err_alloc;
9774 }
9775
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009776 if (pmu->task_ctx_nr == perf_sw_context)
9777 event->event_caps |= PERF_EV_CAP_SOFTWARE;
9778
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009779 if (group_leader &&
9780 (is_software_event(event) != is_software_event(group_leader))) {
9781 if (is_software_event(event)) {
9782 /*
9783 * If event and group_leader are not both a software
9784 * event, and event is, then group leader is not.
9785 *
9786 * Allow the addition of software events to !software
9787 * groups, this is safe because software events never
9788 * fail to schedule.
9789 */
9790 pmu = group_leader->pmu;
9791 } else if (is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009792 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009793 /*
9794 * In case the group is a pure software group, and we
9795 * try to add a hardware event, move the whole group to
9796 * the hardware context.
9797 */
9798 move_group = 1;
9799 }
9800 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009801
9802 /*
9803 * Get the target context (task or percpu):
9804 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009805 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009806 if (IS_ERR(ctx)) {
9807 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009808 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009809 }
9810
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009811 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9812 err = -EBUSY;
9813 goto err_context;
9814 }
9815
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009816 /*
9817 * Look up the group leader (we will attach this event to it):
9818 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009819 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009820 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009821
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009822 /*
9823 * Do not allow a recursive hierarchy (this new sibling
9824 * becoming part of another group-sibling):
9825 */
9826 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009827 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009828
9829 /* All events in a group should have the same clock */
9830 if (group_leader->clock != event->clock)
9831 goto err_context;
9832
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009833 /*
Mark Rutlandbde66082017-06-22 15:41:38 +01009834 * Make sure we're both events for the same CPU;
9835 * grouping events for different CPUs is broken; since
9836 * you can never concurrently schedule them anyhow.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009837 */
Mark Rutlandbde66082017-06-22 15:41:38 +01009838 if (group_leader->cpu != event->cpu)
9839 goto err_context;
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009840
Mark Rutlandbde66082017-06-22 15:41:38 +01009841 /*
9842 * Make sure we're both on the same task, or both
9843 * per-CPU events.
9844 */
9845 if (group_leader->ctx->task != ctx->task)
9846 goto err_context;
9847
9848 /*
9849 * Do not allow to attach to a group in a different task
9850 * or CPU context. If we're moving SW events, we'll fix
9851 * this up later, so allow that.
9852 */
9853 if (!move_group && group_leader->ctx != ctx)
9854 goto err_context;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009855
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009856 /*
9857 * Only a group leader can be exclusive or pinned
9858 */
9859 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009860 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009861 }
9862
9863 if (output_event) {
9864 err = perf_event_set_output(event, output_event);
9865 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009866 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009867 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009868
Yann Droneauda21b0b32014-01-05 21:36:33 +01009869 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9870 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009871 if (IS_ERR(event_file)) {
9872 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009873 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009874 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009875 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009876
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009877 if (move_group) {
Peter Zijlstra922813f2017-01-11 21:09:50 +01009878 gctx = __perf_event_ctx_lock_double(group_leader, ctx);
9879
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009880 if (gctx->task == TASK_TOMBSTONE) {
9881 err = -ESRCH;
9882 goto err_locked;
9883 }
Peter Zijlstra922813f2017-01-11 21:09:50 +01009884
9885 /*
9886 * Check if we raced against another sys_perf_event_open() call
9887 * moving the software group underneath us.
9888 */
9889 if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
9890 /*
9891 * If someone moved the group out from under us, check
9892 * if this new event wound up on the same ctx, if so
9893 * its the regular !move_group case, otherwise fail.
9894 */
9895 if (gctx != ctx) {
9896 err = -EINVAL;
9897 goto err_locked;
9898 } else {
9899 perf_event_ctx_unlock(group_leader, gctx);
9900 move_group = 0;
9901 }
9902 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009903 } else {
9904 mutex_lock(&ctx->mutex);
9905 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009906
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009907 if (ctx->task == TASK_TOMBSTONE) {
9908 err = -ESRCH;
9909 goto err_locked;
9910 }
9911
Peter Zijlstraa7239682015-09-09 19:06:33 +02009912 if (!perf_event_validate_size(event)) {
9913 err = -E2BIG;
9914 goto err_locked;
9915 }
9916
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009917 /*
9918 * Must be under the same ctx::mutex as perf_install_in_context(),
9919 * because we need to serialize with concurrent event creation.
9920 */
9921 if (!exclusive_event_installable(event, ctx)) {
9922 /* exclusive and group stuff are assumed mutually exclusive */
9923 WARN_ON_ONCE(move_group);
9924
9925 err = -EBUSY;
9926 goto err_locked;
9927 }
9928
9929 WARN_ON_ONCE(ctx->parent_ctx);
9930
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009931 /*
9932 * This is the point on no return; we cannot fail hereafter. This is
9933 * where we start modifying current state.
9934 */
9935
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009936 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009937 /*
9938 * See perf_event_ctx_lock() for comments on the details
9939 * of swizzling perf_event::ctx.
9940 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009941 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009942
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009943 list_for_each_entry(sibling, &group_leader->sibling_list,
9944 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009945 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009946 put_ctx(gctx);
9947 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009948
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009949 /*
9950 * Wait for everybody to stop referencing the events through
9951 * the old lists, before installing it on new lists.
9952 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009953 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009954
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009955 /*
9956 * Install the group siblings before the group leader.
9957 *
9958 * Because a group leader will try and install the entire group
9959 * (through the sibling list, which is still in-tact), we can
9960 * end up with siblings installed in the wrong context.
9961 *
9962 * By installing siblings first we NO-OP because they're not
9963 * reachable through the group lists.
9964 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009965 list_for_each_entry(sibling, &group_leader->sibling_list,
9966 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009967 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01009968 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009969 get_ctx(ctx);
9970 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009971
9972 /*
9973 * Removing from the context ends up with disabled
9974 * event. What we want here is event in the initial
9975 * startup state, ready to be add into new context.
9976 */
9977 perf_event__state_init(group_leader);
9978 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9979 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009980
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009981 /*
9982 * Now that all events are installed in @ctx, nothing
9983 * references @gctx anymore, so drop the last reference we have
9984 * on it.
9985 */
9986 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009987 }
9988
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02009989 /*
9990 * Precalculate sample_data sizes; do while holding ctx::mutex such
9991 * that we're serialized against further additions and before
9992 * perf_install_in_context() which is the point the event is active and
9993 * can use these values.
9994 */
9995 perf_event__header_size(event);
9996 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009997
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01009998 event->owner = current;
9999
Yan, Zhenge2d37cd2012-06-15 14:31:32 +080010000 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010001 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010002
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010003 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010004 perf_event_ctx_unlock(group_leader, gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010005 mutex_unlock(&ctx->mutex);
10006
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010007 if (task) {
10008 mutex_unlock(&task->signal->cred_guard_mutex);
10009 put_task_struct(task);
10010 }
10011
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010012 put_online_cpus();
10013
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010014 mutex_lock(&current->perf_event_mutex);
10015 list_add_tail(&event->owner_entry, &current->perf_event_list);
10016 mutex_unlock(&current->perf_event_mutex);
10017
Peter Zijlstra8a495422010-05-27 15:47:49 +020010018 /*
10019 * Drop the reference on the group_event after placing the
10020 * new event on the sibling_list. This ensures destruction
10021 * of the group leader will find the pointer to itself in
10022 * perf_group_detach().
10023 */
Al Viro2903ff02012-08-28 12:52:22 -040010024 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010025 fd_install(event_fd, event_file);
10026 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010027
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010028err_locked:
10029 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010030 perf_event_ctx_unlock(group_leader, gctx);
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010031 mutex_unlock(&ctx->mutex);
10032/* err_file: */
10033 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010034err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010035 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -040010036 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020010037err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +010010038 /*
10039 * If event_file is set, the fput() above will have called ->release()
10040 * and that will take care of freeing the event.
10041 */
10042 if (!event_file)
10043 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010044err_cred:
10045 if (task)
10046 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010047err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010048 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010049err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +020010050 if (task)
10051 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010052err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -040010053 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010054err_fd:
10055 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010056 return err;
10057}
10058
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010059/**
10060 * perf_event_create_kernel_counter
10061 *
10062 * @attr: attributes of the counter to create
10063 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -070010064 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010065 */
10066struct perf_event *
10067perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -070010068 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +030010069 perf_overflow_handler_t overflow_handler,
10070 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010071{
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010072 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010073 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010074 int err;
10075
10076 /*
10077 * Get the target context (task or percpu):
10078 */
10079
Avi Kivity4dc0da82011-06-29 18:42:35 +030010080 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000010081 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010082 if (IS_ERR(event)) {
10083 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010084 goto err;
10085 }
10086
Jiri Olsaf8697762014-08-01 14:33:01 +020010087 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010088 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +020010089
Yan, Zheng4af57ef282014-11-04 21:56:01 -050010090 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010091 if (IS_ERR(ctx)) {
10092 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010093 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010094 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010095
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010096 WARN_ON_ONCE(ctx->parent_ctx);
10097 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010098 if (ctx->task == TASK_TOMBSTONE) {
10099 err = -ESRCH;
10100 goto err_unlock;
10101 }
10102
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010103 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010104 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010105 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010106 }
10107
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010108 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010109 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010110 mutex_unlock(&ctx->mutex);
10111
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010112 return event;
10113
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010114err_unlock:
10115 mutex_unlock(&ctx->mutex);
10116 perf_unpin_context(ctx);
10117 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010118err_free:
10119 free_event(event);
10120err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010121 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010122}
10123EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
10124
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010125void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
10126{
10127 struct perf_event_context *src_ctx;
10128 struct perf_event_context *dst_ctx;
10129 struct perf_event *event, *tmp;
10130 LIST_HEAD(events);
10131
10132 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
10133 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
10134
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010135 /*
10136 * See perf_event_ctx_lock() for comments on the details
10137 * of swizzling perf_event::ctx.
10138 */
10139 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010140 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
10141 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010142 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010143 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010144 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020010145 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010146 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010147
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010148 /*
10149 * Wait for the events to quiesce before re-instating them.
10150 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010151 synchronize_rcu();
10152
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010153 /*
10154 * Re-instate events in 2 passes.
10155 *
10156 * Skip over group leaders and only install siblings on this first
10157 * pass, siblings will not get enabled without a leader, however a
10158 * leader will enable its siblings, even if those are still on the old
10159 * context.
10160 */
10161 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10162 if (event->group_leader == event)
10163 continue;
10164
10165 list_del(&event->migrate_entry);
10166 if (event->state >= PERF_EVENT_STATE_OFF)
10167 event->state = PERF_EVENT_STATE_INACTIVE;
10168 account_event_cpu(event, dst_cpu);
10169 perf_install_in_context(dst_ctx, event, dst_cpu);
10170 get_ctx(dst_ctx);
10171 }
10172
10173 /*
10174 * Once all the siblings are setup properly, install the group leaders
10175 * to make it go.
10176 */
Peter Zijlstra98861672013-10-03 16:02:23 +020010177 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10178 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010179 if (event->state >= PERF_EVENT_STATE_OFF)
10180 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010181 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010182 perf_install_in_context(dst_ctx, event, dst_cpu);
10183 get_ctx(dst_ctx);
10184 }
10185 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010186 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010187}
10188EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
10189
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010190static void sync_child_event(struct perf_event *child_event,
10191 struct task_struct *child)
10192{
10193 struct perf_event *parent_event = child_event->parent;
10194 u64 child_val;
10195
10196 if (child_event->attr.inherit_stat)
10197 perf_event_read_event(child_event, child);
10198
Peter Zijlstrab5e58792010-05-21 14:43:12 +020010199 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010200
10201 /*
10202 * Add back the child's count to the parent's count:
10203 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020010204 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010205 atomic64_add(child_event->total_time_enabled,
10206 &parent_event->child_total_time_enabled);
10207 atomic64_add(child_event->total_time_running,
10208 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010209}
10210
10211static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010212perf_event_exit_event(struct perf_event *child_event,
10213 struct perf_event_context *child_ctx,
10214 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010215{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010216 struct perf_event *parent_event = child_event->parent;
10217
Peter Zijlstra1903d502014-07-15 17:27:27 +020010218 /*
10219 * Do not destroy the 'original' grouping; because of the context
10220 * switch optimization the original events could've ended up in a
10221 * random child task.
10222 *
10223 * If we were to destroy the original group, all group related
10224 * operations would cease to function properly after this random
10225 * child dies.
10226 *
10227 * Do destroy all inherited groups, we don't care about those
10228 * and being thorough is better.
10229 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010230 raw_spin_lock_irq(&child_ctx->lock);
10231 WARN_ON_ONCE(child_ctx->is_active);
10232
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010233 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010010234 perf_group_detach(child_event);
10235 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +010010236 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010237 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010238
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010239 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010240 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010241 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010242 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040010243 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010244 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010245 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010246 /*
10247 * Child events can be cleaned up.
10248 */
10249
10250 sync_child_event(child_event, child);
10251
10252 /*
10253 * Remove this event from the parent's list
10254 */
10255 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
10256 mutex_lock(&parent_event->child_mutex);
10257 list_del_init(&child_event->child_list);
10258 mutex_unlock(&parent_event->child_mutex);
10259
10260 /*
10261 * Kick perf_poll() for is_event_hup().
10262 */
10263 perf_event_wakeup(parent_event);
10264 free_event(child_event);
10265 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010266}
10267
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010268static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010269{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010270 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010271 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010272
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010273 WARN_ON_ONCE(child != current);
10274
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010275 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010276 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010277 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010278
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010279 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010280 * In order to reduce the amount of tricky in ctx tear-down, we hold
10281 * ctx::mutex over the entire thing. This serializes against almost
10282 * everything that wants to access the ctx.
10283 *
10284 * The exception is sys_perf_event_open() /
10285 * perf_event_create_kernel_count() which does find_get_context()
10286 * without ctx::mutex (it cannot because of the move_group double mutex
10287 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010288 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010289 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010290
10291 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010292 * In a single ctx::lock section, de-schedule the events and detach the
10293 * context from the task such that we cannot ever get it scheduled back
10294 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010295 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010296 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010297 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010298
10299 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010300 * Now that the context is inactive, destroy the task <-> ctx relation
10301 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010302 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010303 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
10304 put_ctx(child_ctx); /* cannot be last */
10305 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
10306 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010307
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010308 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010309 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010310
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010311 if (clone_ctx)
10312 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010313
10314 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010315 * Report the task dead after unscheduling the events so that we
10316 * won't get any samples after PERF_RECORD_EXIT. We can however still
10317 * get a few PERF_RECORD_READ events.
10318 */
10319 perf_event_task(child, child_ctx, 0);
10320
Peter Zijlstraebf905f2014-05-29 19:00:24 +020010321 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010322 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010323
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010324 mutex_unlock(&child_ctx->mutex);
10325
10326 put_ctx(child_ctx);
10327}
10328
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010329/*
10330 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010331 *
10332 * Can be called with cred_guard_mutex held when called from
10333 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010334 */
10335void perf_event_exit_task(struct task_struct *child)
10336{
Peter Zijlstra88821352010-11-09 19:01:43 +010010337 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010338 int ctxn;
10339
Peter Zijlstra88821352010-11-09 19:01:43 +010010340 mutex_lock(&child->perf_event_mutex);
10341 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
10342 owner_entry) {
10343 list_del_init(&event->owner_entry);
10344
10345 /*
10346 * Ensure the list deletion is visible before we clear
10347 * the owner, closes a race against perf_release() where
10348 * we need to serialize on the owner->perf_event_mutex.
10349 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010010350 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010010351 }
10352 mutex_unlock(&child->perf_event_mutex);
10353
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010354 for_each_task_context_nr(ctxn)
10355 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010010356
10357 /*
10358 * The perf_event_exit_task_context calls perf_event_task
10359 * with child's task_ctx, which generates EXIT events for
10360 * child contexts and sets child->perf_event_ctxp[] to NULL.
10361 * At this point we need to send EXIT events to cpu contexts.
10362 */
10363 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010364}
10365
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010366static void perf_free_event(struct perf_event *event,
10367 struct perf_event_context *ctx)
10368{
10369 struct perf_event *parent = event->parent;
10370
10371 if (WARN_ON_ONCE(!parent))
10372 return;
10373
10374 mutex_lock(&parent->child_mutex);
10375 list_del_init(&event->child_list);
10376 mutex_unlock(&parent->child_mutex);
10377
Al Viroa6fa9412012-08-20 14:59:25 +010010378 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010379
Peter Zijlstra652884f2015-01-23 11:20:10 +010010380 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020010381 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010382 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010010383 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010384 free_event(event);
10385}
10386
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010387/*
Peter Zijlstra652884f2015-01-23 11:20:10 +010010388 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010389 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010010390 *
10391 * Not all locks are strictly required, but take them anyway to be nice and
10392 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010393 */
10394void perf_event_free_task(struct task_struct *task)
10395{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010396 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010397 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010398 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010399
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010400 for_each_task_context_nr(ctxn) {
10401 ctx = task->perf_event_ctxp[ctxn];
10402 if (!ctx)
10403 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010404
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010405 mutex_lock(&ctx->mutex);
Peter Zijlstrac04a9382017-03-16 13:47:48 +010010406 raw_spin_lock_irq(&ctx->lock);
10407 /*
10408 * Destroy the task <-> ctx relation and mark the context dead.
10409 *
10410 * This is important because even though the task hasn't been
10411 * exposed yet the context has been (through child_list).
10412 */
10413 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
10414 WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
10415 put_task_struct(task); /* cannot be last */
10416 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010417again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010418 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
10419 group_entry)
10420 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010421
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010422 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
10423 group_entry)
10424 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010425
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010426 if (!list_empty(&ctx->pinned_groups) ||
10427 !list_empty(&ctx->flexible_groups))
10428 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010429
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010430 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010431
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010432 put_ctx(ctx);
10433 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010434}
10435
Peter Zijlstra4e231c72010-09-09 21:01:59 +020010436void perf_event_delayed_put(struct task_struct *task)
10437{
10438 int ctxn;
10439
10440 for_each_task_context_nr(ctxn)
10441 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
10442}
10443
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010444struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000010445{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010446 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010447
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010448 file = fget_raw(fd);
10449 if (!file)
10450 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000010451
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010452 if (file->f_op != &perf_fops) {
10453 fput(file);
10454 return ERR_PTR(-EBADF);
10455 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000010456
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010457 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010458}
10459
10460const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
10461{
10462 if (!event)
10463 return ERR_PTR(-EINVAL);
10464
10465 return &event->attr;
10466}
10467
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010468/*
10469 * inherit a event from parent task to child task:
10470 */
10471static struct perf_event *
10472inherit_event(struct perf_event *parent_event,
10473 struct task_struct *parent,
10474 struct perf_event_context *parent_ctx,
10475 struct task_struct *child,
10476 struct perf_event *group_leader,
10477 struct perf_event_context *child_ctx)
10478{
Jiri Olsa1929def2014-09-12 13:18:27 +020010479 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010480 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020010481 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010482
10483 /*
10484 * Instead of creating recursive hierarchies of events,
10485 * we link inherited events back to the original parent,
10486 * which has a filp for sure, which we use as the reference
10487 * count:
10488 */
10489 if (parent_event->parent)
10490 parent_event = parent_event->parent;
10491
10492 child_event = perf_event_alloc(&parent_event->attr,
10493 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010494 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010495 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000010496 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010497 if (IS_ERR(child_event))
10498 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010010499
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010500 /*
10501 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10502 * must be under the same lock in order to serialize against
10503 * perf_event_release_kernel(), such that either we must observe
10504 * is_orphaned_event() or they will observe us on the child_list.
10505 */
10506 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020010507 if (is_orphaned_event(parent_event) ||
10508 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010509 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +010010510 free_event(child_event);
10511 return NULL;
10512 }
10513
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010514 get_ctx(child_ctx);
10515
10516 /*
10517 * Make the child state follow the state of the parent event,
10518 * not its attr.disabled bit. We hold the parent's mutex,
10519 * so we won't race with perf_event_{en, dis}able_family.
10520 */
Jiri Olsa1929def2014-09-12 13:18:27 +020010521 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010522 child_event->state = PERF_EVENT_STATE_INACTIVE;
10523 else
10524 child_event->state = PERF_EVENT_STATE_OFF;
10525
10526 if (parent_event->attr.freq) {
10527 u64 sample_period = parent_event->hw.sample_period;
10528 struct hw_perf_event *hwc = &child_event->hw;
10529
10530 hwc->sample_period = sample_period;
10531 hwc->last_period = sample_period;
10532
10533 local64_set(&hwc->period_left, sample_period);
10534 }
10535
10536 child_event->ctx = child_ctx;
10537 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010538 child_event->overflow_handler_context
10539 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010540
10541 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020010542 * Precalculate sample_data sizes
10543 */
10544 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020010545 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020010546
10547 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010548 * Link it up in the child's context:
10549 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020010550 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010551 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010552 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010553
10554 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010555 * Link this into the parent event's child list
10556 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010557 list_add_tail(&child_event->child_list, &parent_event->child_list);
10558 mutex_unlock(&parent_event->child_mutex);
10559
10560 return child_event;
10561}
10562
10563static int inherit_group(struct perf_event *parent_event,
10564 struct task_struct *parent,
10565 struct perf_event_context *parent_ctx,
10566 struct task_struct *child,
10567 struct perf_event_context *child_ctx)
10568{
10569 struct perf_event *leader;
10570 struct perf_event *sub;
10571 struct perf_event *child_ctr;
10572
10573 leader = inherit_event(parent_event, parent, parent_ctx,
10574 child, NULL, child_ctx);
10575 if (IS_ERR(leader))
10576 return PTR_ERR(leader);
10577 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10578 child_ctr = inherit_event(sub, parent, parent_ctx,
10579 child, leader, child_ctx);
10580 if (IS_ERR(child_ctr))
10581 return PTR_ERR(child_ctr);
10582 }
10583 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010584}
10585
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010586static int
10587inherit_task_group(struct perf_event *event, struct task_struct *parent,
10588 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010589 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010590 int *inherited_all)
10591{
10592 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010593 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010594
10595 if (!event->attr.inherit) {
10596 *inherited_all = 0;
10597 return 0;
10598 }
10599
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010600 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010601 if (!child_ctx) {
10602 /*
10603 * This is executed from the parent task context, so
10604 * inherit events that have been marked for cloning.
10605 * First allocate and initialize a context for the
10606 * child.
10607 */
10608
Jiri Olsa734df5a2013-07-09 17:44:10 +020010609 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010610 if (!child_ctx)
10611 return -ENOMEM;
10612
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010613 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010614 }
10615
10616 ret = inherit_group(event, parent, parent_ctx,
10617 child, child_ctx);
10618
10619 if (ret)
10620 *inherited_all = 0;
10621
10622 return ret;
10623}
10624
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010625/*
10626 * Initialize the perf_event context in task_struct
10627 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010628static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010629{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010630 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010631 struct perf_event_context *cloned_ctx;
10632 struct perf_event *event;
10633 struct task_struct *parent = current;
10634 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010635 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010636 int ret = 0;
10637
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010638 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010639 return 0;
10640
10641 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010642 * If the parent's context is a clone, pin it so it won't get
10643 * swapped under us.
10644 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010645 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010646 if (!parent_ctx)
10647 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010648
10649 /*
10650 * No need to check if parent_ctx != NULL here; since we saw
10651 * it non-NULL earlier, the only reason for it to become NULL
10652 * is if we exit, and since we're currently in the middle of
10653 * a fork we can't be exiting at the same time.
10654 */
10655
10656 /*
10657 * Lock the parent list. No need to lock the child - not PID
10658 * hashed yet and not running, so nobody can access it.
10659 */
10660 mutex_lock(&parent_ctx->mutex);
10661
10662 /*
10663 * We dont have to disable NMIs - we are only looking at
10664 * the list, not manipulating it:
10665 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010666 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010667 ret = inherit_task_group(event, parent, parent_ctx,
10668 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010669 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010670 goto out_unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010671 }
10672
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010673 /*
10674 * We can't hold ctx->lock when iterating the ->flexible_group list due
10675 * to allocations, but we need to prevent rotation because
10676 * rotate_ctx() will change the list from interrupt context.
10677 */
10678 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10679 parent_ctx->rotate_disable = 1;
10680 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10681
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010682 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010683 ret = inherit_task_group(event, parent, parent_ctx,
10684 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010685 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010686 goto out_unlock;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010687 }
10688
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010689 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10690 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010691
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010692 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010693
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010694 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010695 /*
10696 * Mark the child context as a clone of the parent
10697 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010698 *
10699 * Note that if the parent is a clone, the holding of
10700 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010701 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010702 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010703 if (cloned_ctx) {
10704 child_ctx->parent_ctx = cloned_ctx;
10705 child_ctx->parent_gen = parent_ctx->parent_gen;
10706 } else {
10707 child_ctx->parent_ctx = parent_ctx;
10708 child_ctx->parent_gen = parent_ctx->generation;
10709 }
10710 get_ctx(child_ctx->parent_ctx);
10711 }
10712
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010713 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010714out_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010715 mutex_unlock(&parent_ctx->mutex);
10716
10717 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010718 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010719
10720 return ret;
10721}
10722
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010723/*
10724 * Initialize the perf_event context in task_struct
10725 */
10726int perf_event_init_task(struct task_struct *child)
10727{
10728 int ctxn, ret;
10729
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010730 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10731 mutex_init(&child->perf_event_mutex);
10732 INIT_LIST_HEAD(&child->perf_event_list);
10733
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010734 for_each_task_context_nr(ctxn) {
10735 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010736 if (ret) {
10737 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010738 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010739 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010740 }
10741
10742 return 0;
10743}
10744
Paul Mackerras220b1402010-03-10 20:45:52 +110010745static void __init perf_event_init_all_cpus(void)
10746{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010747 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010748 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010749
10750 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010751 swhash = &per_cpu(swevent_htable, cpu);
10752 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010753 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070010754
10755 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
10756 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020010757
10758 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010759 }
10760}
10761
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010762int perf_event_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010763{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010764 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010765
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010766 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010767 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010768 struct swevent_hlist *hlist;
10769
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010770 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10771 WARN_ON(!hlist);
10772 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010773 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010774 mutex_unlock(&swhash->hlist_mutex);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010775 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010776}
10777
Dave Young2965faa2015-09-09 15:38:55 -070010778#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010779static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010780{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010781 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010782 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10783 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010784
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010785 raw_spin_lock(&ctx->lock);
10786 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010787 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010788 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010789}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010790
10791static void perf_event_exit_cpu_context(int cpu)
10792{
10793 struct perf_event_context *ctx;
10794 struct pmu *pmu;
10795 int idx;
10796
10797 idx = srcu_read_lock(&pmus_srcu);
10798 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010799 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010800
10801 mutex_lock(&ctx->mutex);
10802 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10803 mutex_unlock(&ctx->mutex);
10804 }
10805 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010806}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010807#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010808
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010809static void perf_event_exit_cpu_context(int cpu) { }
10810
10811#endif
10812
10813int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010814{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010815 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010816 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010817}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010818
Peter Zijlstrac2774432010-12-08 15:29:02 +010010819static int
10820perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10821{
10822 int cpu;
10823
10824 for_each_online_cpu(cpu)
10825 perf_event_exit_cpu(cpu);
10826
10827 return NOTIFY_OK;
10828}
10829
10830/*
10831 * Run the perf reboot notifier at the very last possible moment so that
10832 * the generic watchdog code runs as long as possible.
10833 */
10834static struct notifier_block perf_reboot_notifier = {
10835 .notifier_call = perf_reboot,
10836 .priority = INT_MIN,
10837};
10838
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010839void __init perf_event_init(void)
10840{
Jason Wessel3c502e72010-11-04 17:33:01 -050010841 int ret;
10842
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010843 idr_init(&pmu_idr);
10844
Paul Mackerras220b1402010-03-10 20:45:52 +110010845 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010846 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010847 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10848 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10849 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010850 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010851 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010010852 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010853
10854 ret = init_hw_breakpoint();
10855 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010856
Jiri Olsab01c3a02012-03-23 15:41:20 +010010857 /*
10858 * Build time assertion that we keep the data_head at the intended
10859 * location. IOW, validation we got the __reserved[] size right.
10860 */
10861 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10862 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010863}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010864
Cody P Schaferfd979c02015-01-30 13:45:57 -080010865ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10866 char *page)
10867{
10868 struct perf_pmu_events_attr *pmu_attr =
10869 container_of(attr, struct perf_pmu_events_attr, attr);
10870
10871 if (pmu_attr->event_str)
10872 return sprintf(page, "%s\n", pmu_attr->event_str);
10873
10874 return 0;
10875}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010876EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010877
Peter Zijlstraabe43402010-11-17 23:17:37 +010010878static int __init perf_event_sysfs_init(void)
10879{
10880 struct pmu *pmu;
10881 int ret;
10882
10883 mutex_lock(&pmus_lock);
10884
10885 ret = bus_register(&pmu_bus);
10886 if (ret)
10887 goto unlock;
10888
10889 list_for_each_entry(pmu, &pmus, entry) {
10890 if (!pmu->name || pmu->type < 0)
10891 continue;
10892
10893 ret = pmu_dev_alloc(pmu);
10894 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10895 }
10896 pmu_bus_running = 1;
10897 ret = 0;
10898
10899unlock:
10900 mutex_unlock(&pmus_lock);
10901
10902 return ret;
10903}
10904device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010905
10906#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010907static struct cgroup_subsys_state *
10908perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010909{
10910 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010911
Li Zefan1b15d052011-03-03 14:26:06 +080010912 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010913 if (!jc)
10914 return ERR_PTR(-ENOMEM);
10915
Stephane Eraniane5d13672011-02-14 11:20:01 +020010916 jc->info = alloc_percpu(struct perf_cgroup_info);
10917 if (!jc->info) {
10918 kfree(jc);
10919 return ERR_PTR(-ENOMEM);
10920 }
10921
Stephane Eraniane5d13672011-02-14 11:20:01 +020010922 return &jc->css;
10923}
10924
Tejun Heoeb954192013-08-08 20:11:23 -040010925static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010926{
Tejun Heoeb954192013-08-08 20:11:23 -040010927 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10928
Stephane Eraniane5d13672011-02-14 11:20:01 +020010929 free_percpu(jc->info);
10930 kfree(jc);
10931}
10932
10933static int __perf_cgroup_move(void *info)
10934{
10935 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010936 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010937 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010938 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010939 return 0;
10940}
10941
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010942static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010943{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010944 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010945 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010946
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010947 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010948 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010949}
10950
Tejun Heo073219e2014-02-08 10:36:58 -050010951struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010952 .css_alloc = perf_cgroup_css_alloc,
10953 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010954 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010955};
10956#endif /* CONFIG_CGROUP_PERF */