blob: 8c42a5ae90308e41ccd8167f89847e408e7bb9a0 [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{
456 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
457
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{
637 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
638 if (cgrp_out)
639 __update_cgrp_time(cgrp_out);
640}
641
642static inline void update_cgrp_time_from_event(struct perf_event *event)
643{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200644 struct perf_cgroup *cgrp;
645
Stephane Eraniane5d13672011-02-14 11:20:01 +0200646 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200647 * ensure we access cgroup data only when needed and
648 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200649 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200650 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200651 return;
652
Stephane Eranian614e4c42015-11-12 11:00:04 +0100653 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200654 /*
655 * Do not update time when cgroup is not active
656 */
657 if (cgrp == event->cgrp)
658 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200659}
660
661static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200662perf_cgroup_set_timestamp(struct task_struct *task,
663 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200664{
665 struct perf_cgroup *cgrp;
666 struct perf_cgroup_info *info;
667
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200668 /*
669 * ctx->lock held by caller
670 * ensure we do not access cgroup data
671 * unless we have the cgroup pinned (css_get)
672 */
673 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200674 return;
675
Stephane Eranian614e4c42015-11-12 11:00:04 +0100676 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200677 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200678 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200679}
680
681#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
682#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
683
684/*
685 * reschedule events based on the cgroup constraint of task.
686 *
687 * mode SWOUT : schedule out everything
688 * mode SWIN : schedule in based on cgroup for next
689 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800690static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200691{
692 struct perf_cpu_context *cpuctx;
693 struct pmu *pmu;
694 unsigned long flags;
695
696 /*
697 * disable interrupts to avoid geting nr_cgroup
698 * changes via __perf_event_disable(). Also
699 * avoids preemption.
700 */
701 local_irq_save(flags);
702
703 /*
704 * we reschedule only in the presence of cgroup
705 * constrained events.
706 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200707
708 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200709 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200710 if (cpuctx->unique_pmu != pmu)
711 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200712
Stephane Eraniane5d13672011-02-14 11:20:01 +0200713 /*
714 * perf_cgroup_events says at least one
715 * context on this CPU has cgroup events.
716 *
717 * ctx->nr_cgroups reports the number of cgroup
718 * events for a context.
719 */
720 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200721 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
722 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200723
724 if (mode & PERF_CGROUP_SWOUT) {
725 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
726 /*
727 * must not be done before ctxswout due
728 * to event_filter_match() in event_sched_out()
729 */
730 cpuctx->cgrp = NULL;
731 }
732
733 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200734 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200735 /*
736 * set cgrp before ctxsw in to allow
737 * event_filter_match() to not have to pass
738 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100739 * we pass the cpuctx->ctx to perf_cgroup_from_task()
740 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200741 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100742 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200743 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
744 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200745 perf_pmu_enable(cpuctx->ctx.pmu);
746 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200747 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200748 }
749
Stephane Eraniane5d13672011-02-14 11:20:01 +0200750 local_irq_restore(flags);
751}
752
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200753static inline void perf_cgroup_sched_out(struct task_struct *task,
754 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200755{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200756 struct perf_cgroup *cgrp1;
757 struct perf_cgroup *cgrp2 = NULL;
758
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100759 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200760 /*
761 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100762 * we do not need to pass the ctx here because we know
763 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200764 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100765 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100766 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200767
768 /*
769 * only schedule out current cgroup events if we know
770 * that we are switching to a different cgroup. Otherwise,
771 * do no touch the cgroup events.
772 */
773 if (cgrp1 != cgrp2)
774 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100775
776 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200777}
778
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200779static inline void perf_cgroup_sched_in(struct task_struct *prev,
780 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200781{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200782 struct perf_cgroup *cgrp1;
783 struct perf_cgroup *cgrp2 = NULL;
784
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100785 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200786 /*
787 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100788 * we do not need to pass the ctx here because we know
789 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200790 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100791 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100792 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200793
794 /*
795 * only need to schedule in cgroup events if we are changing
796 * cgroup during ctxsw. Cgroup events were not scheduled
797 * out of ctxsw out if that was not the case.
798 */
799 if (cgrp1 != cgrp2)
800 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100801
802 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200803}
804
805static inline int perf_cgroup_connect(int fd, struct perf_event *event,
806 struct perf_event_attr *attr,
807 struct perf_event *group_leader)
808{
809 struct perf_cgroup *cgrp;
810 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400811 struct fd f = fdget(fd);
812 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200813
Al Viro2903ff02012-08-28 12:52:22 -0400814 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200815 return -EBADF;
816
Al Virob5830432014-10-31 01:22:04 -0400817 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400818 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800819 if (IS_ERR(css)) {
820 ret = PTR_ERR(css);
821 goto out;
822 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200823
824 cgrp = container_of(css, struct perf_cgroup, css);
825 event->cgrp = cgrp;
826
827 /*
828 * all events in a group must monitor
829 * the same cgroup because a task belongs
830 * to only one perf cgroup at a time
831 */
832 if (group_leader && group_leader->cgrp != cgrp) {
833 perf_detach_cgroup(event);
834 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200835 }
Li Zefan3db272c2011-03-03 14:25:37 +0800836out:
Al Viro2903ff02012-08-28 12:52:22 -0400837 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200838 return ret;
839}
840
841static inline void
842perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
843{
844 struct perf_cgroup_info *t;
845 t = per_cpu_ptr(event->cgrp->info, event->cpu);
846 event->shadow_ctx_time = now - t->timestamp;
847}
848
849static inline void
850perf_cgroup_defer_enabled(struct perf_event *event)
851{
852 /*
853 * when the current task's perf cgroup does not match
854 * the event's, we need to remember to call the
855 * perf_mark_enable() function the first time a task with
856 * a matching perf cgroup is scheduled in.
857 */
858 if (is_cgroup_event(event) && !perf_cgroup_match(event))
859 event->cgrp_defer_enabled = 1;
860}
861
862static inline void
863perf_cgroup_mark_enabled(struct perf_event *event,
864 struct perf_event_context *ctx)
865{
866 struct perf_event *sub;
867 u64 tstamp = perf_event_time(event);
868
869 if (!event->cgrp_defer_enabled)
870 return;
871
872 event->cgrp_defer_enabled = 0;
873
874 event->tstamp_enabled = tstamp - event->total_time_enabled;
875 list_for_each_entry(sub, &event->sibling_list, group_entry) {
876 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
877 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
878 sub->cgrp_defer_enabled = 0;
879 }
880 }
881}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700882
883/*
884 * Update cpuctx->cgrp so that it is set when first cgroup event is added and
885 * cleared when last cgroup event is removed.
886 */
887static inline void
888list_update_cgroup_event(struct perf_event *event,
889 struct perf_event_context *ctx, bool add)
890{
891 struct perf_cpu_context *cpuctx;
892
893 if (!is_cgroup_event(event))
894 return;
895
896 if (add && ctx->nr_cgroups++)
897 return;
898 else if (!add && --ctx->nr_cgroups)
899 return;
900 /*
901 * Because cgroup events are always per-cpu events,
902 * this will always be called from the right CPU.
903 */
904 cpuctx = __get_cpu_context(ctx);
905 cpuctx->cgrp = add ? event->cgrp : NULL;
906}
907
Stephane Eraniane5d13672011-02-14 11:20:01 +0200908#else /* !CONFIG_CGROUP_PERF */
909
910static inline bool
911perf_cgroup_match(struct perf_event *event)
912{
913 return true;
914}
915
916static inline void perf_detach_cgroup(struct perf_event *event)
917{}
918
919static inline int is_cgroup_event(struct perf_event *event)
920{
921 return 0;
922}
923
924static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
925{
926 return 0;
927}
928
929static inline void update_cgrp_time_from_event(struct perf_event *event)
930{
931}
932
933static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
934{
935}
936
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200937static inline void perf_cgroup_sched_out(struct task_struct *task,
938 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200939{
940}
941
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200942static inline void perf_cgroup_sched_in(struct task_struct *prev,
943 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200944{
945}
946
947static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
948 struct perf_event_attr *attr,
949 struct perf_event *group_leader)
950{
951 return -EINVAL;
952}
953
954static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200955perf_cgroup_set_timestamp(struct task_struct *task,
956 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200957{
958}
959
960void
961perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
962{
963}
964
965static inline void
966perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
967{
968}
969
970static inline u64 perf_cgroup_event_time(struct perf_event *event)
971{
972 return 0;
973}
974
975static inline void
976perf_cgroup_defer_enabled(struct perf_event *event)
977{
978}
979
980static inline void
981perf_cgroup_mark_enabled(struct perf_event *event,
982 struct perf_event_context *ctx)
983{
984}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700985
986static inline void
987list_update_cgroup_event(struct perf_event *event,
988 struct perf_event_context *ctx, bool add)
989{
990}
991
Stephane Eraniane5d13672011-02-14 11:20:01 +0200992#endif
993
Stephane Eranian9e630202013-04-03 14:21:33 +0200994/*
995 * set default to be dependent on timer tick just
996 * like original code
997 */
998#define PERF_CPU_HRTIMER (1000 / HZ)
999/*
1000 * function must be called with interrupts disbled
1001 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001002static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +02001003{
1004 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +02001005 int rotations = 0;
1006
1007 WARN_ON(!irqs_disabled());
1008
1009 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +02001010 rotations = perf_rotate_context(cpuctx);
1011
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001012 raw_spin_lock(&cpuctx->hrtimer_lock);
1013 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +02001014 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001015 else
1016 cpuctx->hrtimer_active = 0;
1017 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +02001018
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001019 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +02001020}
1021
Peter Zijlstra272325c2015-04-15 11:41:58 +02001022static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +02001023{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001024 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001025 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +02001026 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +02001027
1028 /* no multiplexing needed for SW PMU */
1029 if (pmu->task_ctx_nr == perf_sw_context)
1030 return;
1031
Stephane Eranian62b85632013-04-03 14:21:34 +02001032 /*
1033 * check default is sane, if not set then force to
1034 * default interval (1/tick)
1035 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001036 interval = pmu->hrtimer_interval_ms;
1037 if (interval < 1)
1038 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001039
Peter Zijlstra272325c2015-04-15 11:41:58 +02001040 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001041
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001042 raw_spin_lock_init(&cpuctx->hrtimer_lock);
1043 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001044 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001045}
1046
Peter Zijlstra272325c2015-04-15 11:41:58 +02001047static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001048{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001049 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001050 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001051 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001052
1053 /* not for SW PMU */
1054 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001055 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001056
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001057 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1058 if (!cpuctx->hrtimer_active) {
1059 cpuctx->hrtimer_active = 1;
1060 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
1061 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
1062 }
1063 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001064
Peter Zijlstra272325c2015-04-15 11:41:58 +02001065 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001066}
1067
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001068void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001069{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001070 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1071 if (!(*count)++)
1072 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001073}
1074
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001075void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001076{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001077 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1078 if (!--(*count))
1079 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001080}
1081
Mark Rutland2fde4f92015-01-07 15:01:54 +00001082static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001083
1084/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001085 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1086 * perf_event_task_tick() are fully serialized because they're strictly cpu
1087 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1088 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001089 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001090static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001091{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001092 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001093
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001094 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001095
Mark Rutland2fde4f92015-01-07 15:01:54 +00001096 WARN_ON(!list_empty(&ctx->active_ctx_list));
1097
1098 list_add(&ctx->active_ctx_list, head);
1099}
1100
1101static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1102{
1103 WARN_ON(!irqs_disabled());
1104
1105 WARN_ON(list_empty(&ctx->active_ctx_list));
1106
1107 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001108}
1109
1110static void get_ctx(struct perf_event_context *ctx)
1111{
1112 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1113}
1114
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001115static void free_ctx(struct rcu_head *head)
1116{
1117 struct perf_event_context *ctx;
1118
1119 ctx = container_of(head, struct perf_event_context, rcu_head);
1120 kfree(ctx->task_ctx_data);
1121 kfree(ctx);
1122}
1123
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001124static void put_ctx(struct perf_event_context *ctx)
1125{
1126 if (atomic_dec_and_test(&ctx->refcount)) {
1127 if (ctx->parent_ctx)
1128 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001129 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001130 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001131 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132 }
1133}
1134
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001135/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001136 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1137 * perf_pmu_migrate_context() we need some magic.
1138 *
1139 * Those places that change perf_event::ctx will hold both
1140 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1141 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001142 * Lock ordering is by mutex address. There are two other sites where
1143 * perf_event_context::mutex nests and those are:
1144 *
1145 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001146 * perf_event_exit_event()
1147 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001148 *
1149 * - perf_event_init_context() [ parent, 0 ]
1150 * inherit_task_group()
1151 * inherit_group()
1152 * inherit_event()
1153 * perf_event_alloc()
1154 * perf_init_event()
1155 * perf_try_init_event() [ child , 1 ]
1156 *
1157 * While it appears there is an obvious deadlock here -- the parent and child
1158 * nesting levels are inverted between the two. This is in fact safe because
1159 * life-time rules separate them. That is an exiting task cannot fork, and a
1160 * spawning task cannot (yet) exit.
1161 *
1162 * But remember that that these are parent<->child context relations, and
1163 * migration does not affect children, therefore these two orderings should not
1164 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001165 *
1166 * The change in perf_event::ctx does not affect children (as claimed above)
1167 * because the sys_perf_event_open() case will install a new event and break
1168 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1169 * concerned with cpuctx and that doesn't have children.
1170 *
1171 * The places that change perf_event::ctx will issue:
1172 *
1173 * perf_remove_from_context();
1174 * synchronize_rcu();
1175 * perf_install_in_context();
1176 *
1177 * to affect the change. The remove_from_context() + synchronize_rcu() should
1178 * quiesce the event, after which we can install it in the new location. This
1179 * means that only external vectors (perf_fops, prctl) can perturb the event
1180 * while in transit. Therefore all such accessors should also acquire
1181 * perf_event_context::mutex to serialize against this.
1182 *
1183 * However; because event->ctx can change while we're waiting to acquire
1184 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1185 * function.
1186 *
1187 * Lock order:
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02001188 * cred_guard_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001189 * task_struct::perf_event_mutex
1190 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001191 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001192 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001193 * perf_event::mmap_mutex
1194 * mmap_sem
1195 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001196static struct perf_event_context *
1197perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001198{
1199 struct perf_event_context *ctx;
1200
1201again:
1202 rcu_read_lock();
1203 ctx = ACCESS_ONCE(event->ctx);
1204 if (!atomic_inc_not_zero(&ctx->refcount)) {
1205 rcu_read_unlock();
1206 goto again;
1207 }
1208 rcu_read_unlock();
1209
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001210 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001211 if (event->ctx != ctx) {
1212 mutex_unlock(&ctx->mutex);
1213 put_ctx(ctx);
1214 goto again;
1215 }
1216
1217 return ctx;
1218}
1219
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001220static inline struct perf_event_context *
1221perf_event_ctx_lock(struct perf_event *event)
1222{
1223 return perf_event_ctx_lock_nested(event, 0);
1224}
1225
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001226static void perf_event_ctx_unlock(struct perf_event *event,
1227 struct perf_event_context *ctx)
1228{
1229 mutex_unlock(&ctx->mutex);
1230 put_ctx(ctx);
1231}
1232
1233/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001234 * This must be done under the ctx->lock, such as to serialize against
1235 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1236 * calling scheduler related locks and ctx->lock nests inside those.
1237 */
1238static __must_check struct perf_event_context *
1239unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001240{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001241 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1242
1243 lockdep_assert_held(&ctx->lock);
1244
1245 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001246 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001247 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001248
1249 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001250}
1251
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001252static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1253{
1254 /*
1255 * only top level events have the pid namespace they were created in
1256 */
1257 if (event->parent)
1258 event = event->parent;
1259
1260 return task_tgid_nr_ns(p, event->ns);
1261}
1262
1263static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1264{
1265 /*
1266 * only top level events have the pid namespace they were created in
1267 */
1268 if (event->parent)
1269 event = event->parent;
1270
1271 return task_pid_nr_ns(p, event->ns);
1272}
1273
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001274/*
1275 * If we inherit events we want to return the parent event id
1276 * to userspace.
1277 */
1278static u64 primary_event_id(struct perf_event *event)
1279{
1280 u64 id = event->id;
1281
1282 if (event->parent)
1283 id = event->parent->id;
1284
1285 return id;
1286}
1287
1288/*
1289 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001290 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291 * This has to cope with with the fact that until it is locked,
1292 * the context could get moved to another task.
1293 */
1294static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001295perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001296{
1297 struct perf_event_context *ctx;
1298
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001299retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001300 /*
1301 * One of the few rules of preemptible RCU is that one cannot do
1302 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001303 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001304 * rcu_read_unlock_special().
1305 *
1306 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001307 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001308 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001309 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001310 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001311 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001312 if (ctx) {
1313 /*
1314 * If this context is a clone of another, it might
1315 * get swapped for another underneath us by
1316 * perf_event_task_sched_out, though the
1317 * rcu_read_lock() protects us from any context
1318 * getting freed. Lock the context and check if it
1319 * got swapped before we could get the lock, and retry
1320 * if so. If we locked the right context, then it
1321 * can't get swapped on us any more.
1322 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001323 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001324 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001325 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001326 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001327 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001328 goto retry;
1329 }
1330
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001331 if (ctx->task == TASK_TOMBSTONE ||
1332 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001333 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001334 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001335 } else {
1336 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001337 }
1338 }
1339 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001340 if (!ctx)
1341 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001342 return ctx;
1343}
1344
1345/*
1346 * Get the context for a task and increment its pin_count so it
1347 * can't get swapped to another task. This also increments its
1348 * reference count so that the context can't get freed.
1349 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001350static struct perf_event_context *
1351perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001352{
1353 struct perf_event_context *ctx;
1354 unsigned long flags;
1355
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001356 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001357 if (ctx) {
1358 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001359 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001360 }
1361 return ctx;
1362}
1363
1364static void perf_unpin_context(struct perf_event_context *ctx)
1365{
1366 unsigned long flags;
1367
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001368 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001369 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001370 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371}
1372
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001373/*
1374 * Update the record of the current time in a context.
1375 */
1376static void update_context_time(struct perf_event_context *ctx)
1377{
1378 u64 now = perf_clock();
1379
1380 ctx->time += now - ctx->timestamp;
1381 ctx->timestamp = now;
1382}
1383
Stephane Eranian41587552011-01-03 18:20:01 +02001384static u64 perf_event_time(struct perf_event *event)
1385{
1386 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001387
1388 if (is_cgroup_event(event))
1389 return perf_cgroup_event_time(event);
1390
Stephane Eranian41587552011-01-03 18:20:01 +02001391 return ctx ? ctx->time : 0;
1392}
1393
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001394/*
1395 * Update the total_time_enabled and total_time_running fields for a event.
1396 */
1397static void update_event_times(struct perf_event *event)
1398{
1399 struct perf_event_context *ctx = event->ctx;
1400 u64 run_end;
1401
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001402 lockdep_assert_held(&ctx->lock);
1403
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001404 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1405 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1406 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001407
Stephane Eraniane5d13672011-02-14 11:20:01 +02001408 /*
1409 * in cgroup mode, time_enabled represents
1410 * the time the event was enabled AND active
1411 * tasks were in the monitored cgroup. This is
1412 * independent of the activity of the context as
1413 * there may be a mix of cgroup and non-cgroup events.
1414 *
1415 * That is why we treat cgroup events differently
1416 * here.
1417 */
1418 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001419 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001420 else if (ctx->is_active)
1421 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001422 else
1423 run_end = event->tstamp_stopped;
1424
1425 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001426
1427 if (event->state == PERF_EVENT_STATE_INACTIVE)
1428 run_end = event->tstamp_stopped;
1429 else
Stephane Eranian41587552011-01-03 18:20:01 +02001430 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001431
1432 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001433
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001434}
1435
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001436/*
1437 * Update total_time_enabled and total_time_running for all events in a group.
1438 */
1439static void update_group_times(struct perf_event *leader)
1440{
1441 struct perf_event *event;
1442
1443 update_event_times(leader);
1444 list_for_each_entry(event, &leader->sibling_list, group_entry)
1445 update_event_times(event);
1446}
1447
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001448static struct list_head *
1449ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1450{
1451 if (event->attr.pinned)
1452 return &ctx->pinned_groups;
1453 else
1454 return &ctx->flexible_groups;
1455}
1456
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001457/*
1458 * Add a event from the lists for its context.
1459 * Must be called with ctx->mutex and ctx->lock held.
1460 */
1461static void
1462list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1463{
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001464
Peter Zijlstrac994d612016-01-08 09:20:23 +01001465 lockdep_assert_held(&ctx->lock);
1466
Peter Zijlstra8a495422010-05-27 15:47:49 +02001467 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1468 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001469
1470 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001471 * If we're a stand alone event or group leader, we go to the context
1472 * list, group events are kept attached to the group so that
1473 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001474 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001475 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001476 struct list_head *list;
1477
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001478 event->group_caps = event->event_caps;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001479
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001480 list = ctx_group_list(event, ctx);
1481 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001482 }
1483
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001484 list_update_cgroup_event(event, ctx, true);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001485
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001486 list_add_rcu(&event->event_entry, &ctx->event_list);
1487 ctx->nr_events++;
1488 if (event->attr.inherit_stat)
1489 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001490
1491 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492}
1493
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001494/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001495 * Initialize event state based on the perf_event_attr::disabled.
1496 */
1497static inline void perf_event__state_init(struct perf_event *event)
1498{
1499 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1500 PERF_EVENT_STATE_INACTIVE;
1501}
1502
Peter Zijlstraa7239682015-09-09 19:06:33 +02001503static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001504{
1505 int entry = sizeof(u64); /* value */
1506 int size = 0;
1507 int nr = 1;
1508
1509 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1510 size += sizeof(u64);
1511
1512 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1513 size += sizeof(u64);
1514
1515 if (event->attr.read_format & PERF_FORMAT_ID)
1516 entry += sizeof(u64);
1517
1518 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001519 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001520 size += sizeof(u64);
1521 }
1522
1523 size += entry * nr;
1524 event->read_size = size;
1525}
1526
Peter Zijlstraa7239682015-09-09 19:06:33 +02001527static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001528{
1529 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001530 u16 size = 0;
1531
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001532 if (sample_type & PERF_SAMPLE_IP)
1533 size += sizeof(data->ip);
1534
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001535 if (sample_type & PERF_SAMPLE_ADDR)
1536 size += sizeof(data->addr);
1537
1538 if (sample_type & PERF_SAMPLE_PERIOD)
1539 size += sizeof(data->period);
1540
Andi Kleenc3feedf2013-01-24 16:10:28 +01001541 if (sample_type & PERF_SAMPLE_WEIGHT)
1542 size += sizeof(data->weight);
1543
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001544 if (sample_type & PERF_SAMPLE_READ)
1545 size += event->read_size;
1546
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001547 if (sample_type & PERF_SAMPLE_DATA_SRC)
1548 size += sizeof(data->data_src.val);
1549
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001550 if (sample_type & PERF_SAMPLE_TRANSACTION)
1551 size += sizeof(data->txn);
1552
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001553 event->header_size = size;
1554}
1555
Peter Zijlstraa7239682015-09-09 19:06:33 +02001556/*
1557 * Called at perf_event creation and when events are attached/detached from a
1558 * group.
1559 */
1560static void perf_event__header_size(struct perf_event *event)
1561{
1562 __perf_event_read_size(event,
1563 event->group_leader->nr_siblings);
1564 __perf_event_header_size(event, event->attr.sample_type);
1565}
1566
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001567static void perf_event__id_header_size(struct perf_event *event)
1568{
1569 struct perf_sample_data *data;
1570 u64 sample_type = event->attr.sample_type;
1571 u16 size = 0;
1572
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001573 if (sample_type & PERF_SAMPLE_TID)
1574 size += sizeof(data->tid_entry);
1575
1576 if (sample_type & PERF_SAMPLE_TIME)
1577 size += sizeof(data->time);
1578
Adrian Hunterff3d5272013-08-27 11:23:07 +03001579 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1580 size += sizeof(data->id);
1581
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001582 if (sample_type & PERF_SAMPLE_ID)
1583 size += sizeof(data->id);
1584
1585 if (sample_type & PERF_SAMPLE_STREAM_ID)
1586 size += sizeof(data->stream_id);
1587
1588 if (sample_type & PERF_SAMPLE_CPU)
1589 size += sizeof(data->cpu_entry);
1590
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001591 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001592}
1593
Peter Zijlstraa7239682015-09-09 19:06:33 +02001594static bool perf_event_validate_size(struct perf_event *event)
1595{
1596 /*
1597 * The values computed here will be over-written when we actually
1598 * attach the event.
1599 */
1600 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1601 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1602 perf_event__id_header_size(event);
1603
1604 /*
1605 * Sum the lot; should not exceed the 64k limit we have on records.
1606 * Conservative limit to allow for callchains and other variable fields.
1607 */
1608 if (event->read_size + event->header_size +
1609 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1610 return false;
1611
1612 return true;
1613}
1614
Peter Zijlstra8a495422010-05-27 15:47:49 +02001615static void perf_group_attach(struct perf_event *event)
1616{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001617 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001618
Peter Zijlstra74c33372010-10-15 11:40:29 +02001619 /*
1620 * We can have double attach due to group movement in perf_event_open.
1621 */
1622 if (event->attach_state & PERF_ATTACH_GROUP)
1623 return;
1624
Peter Zijlstra8a495422010-05-27 15:47:49 +02001625 event->attach_state |= PERF_ATTACH_GROUP;
1626
1627 if (group_leader == event)
1628 return;
1629
Peter Zijlstra652884f2015-01-23 11:20:10 +01001630 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1631
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001632 group_leader->group_caps &= event->event_caps;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001633
1634 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1635 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001636
1637 perf_event__header_size(group_leader);
1638
1639 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1640 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001641}
1642
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001643/*
1644 * Remove a event from the lists for its context.
1645 * Must be called with ctx->mutex and ctx->lock held.
1646 */
1647static void
1648list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1649{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001650 WARN_ON_ONCE(event->ctx != ctx);
1651 lockdep_assert_held(&ctx->lock);
1652
Peter Zijlstra8a495422010-05-27 15:47:49 +02001653 /*
1654 * We can have double detach due to exit/hot-unplug + close.
1655 */
1656 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001658
1659 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1660
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001661 list_update_cgroup_event(event, ctx, false);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663 ctx->nr_events--;
1664 if (event->attr.inherit_stat)
1665 ctx->nr_stat--;
1666
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001667 list_del_rcu(&event->event_entry);
1668
Peter Zijlstra8a495422010-05-27 15:47:49 +02001669 if (event->group_leader == event)
1670 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001671
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001672 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001673
1674 /*
1675 * If event was in error state, then keep it
1676 * that way, otherwise bogus counts will be
1677 * returned on read(). The only way to get out
1678 * of error state is by explicit re-enabling
1679 * of the event
1680 */
1681 if (event->state > PERF_EVENT_STATE_OFF)
1682 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001683
1684 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001685}
1686
Peter Zijlstra8a495422010-05-27 15:47:49 +02001687static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001688{
1689 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001690 struct list_head *list = NULL;
1691
1692 /*
1693 * We can have double detach due to exit/hot-unplug + close.
1694 */
1695 if (!(event->attach_state & PERF_ATTACH_GROUP))
1696 return;
1697
1698 event->attach_state &= ~PERF_ATTACH_GROUP;
1699
1700 /*
1701 * If this is a sibling, remove it from its group.
1702 */
1703 if (event->group_leader != event) {
1704 list_del_init(&event->group_entry);
1705 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001706 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001707 }
1708
1709 if (!list_empty(&event->group_entry))
1710 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001711
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001712 /*
1713 * If this was a group event with sibling events then
1714 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001715 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716 */
1717 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001718 if (list)
1719 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001721
1722 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001723 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001724
1725 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001726 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001727
1728out:
1729 perf_event__header_size(event->group_leader);
1730
1731 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1732 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001733}
1734
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001735static bool is_orphaned_event(struct perf_event *event)
1736{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001737 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001738}
1739
Mark Rutland2c81a642016-06-14 16:10:41 +01001740static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01001741{
1742 struct pmu *pmu = event->pmu;
1743 return pmu->filter_match ? pmu->filter_match(event) : 1;
1744}
1745
Mark Rutland2c81a642016-06-14 16:10:41 +01001746/*
1747 * Check whether we should attempt to schedule an event group based on
1748 * PMU-specific filtering. An event group can consist of HW and SW events,
1749 * potentially with a SW leader, so we must check all the filters, to
1750 * determine whether a group is schedulable:
1751 */
1752static inline int pmu_filter_match(struct perf_event *event)
1753{
1754 struct perf_event *child;
1755
1756 if (!__pmu_filter_match(event))
1757 return 0;
1758
1759 list_for_each_entry(child, &event->sibling_list, group_entry) {
1760 if (!__pmu_filter_match(child))
1761 return 0;
1762 }
1763
1764 return 1;
1765}
1766
Stephane Eranianfa66f072010-08-26 16:40:01 +02001767static inline int
1768event_filter_match(struct perf_event *event)
1769{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001770 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
1771 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001772}
1773
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001774static void
1775event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001776 struct perf_cpu_context *cpuctx,
1777 struct perf_event_context *ctx)
1778{
Stephane Eranian41587552011-01-03 18:20:01 +02001779 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001780 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001781
1782 WARN_ON_ONCE(event->ctx != ctx);
1783 lockdep_assert_held(&ctx->lock);
1784
Stephane Eranianfa66f072010-08-26 16:40:01 +02001785 /*
1786 * An event which could not be activated because of
1787 * filter mismatch still needs to have its timings
1788 * maintained, otherwise bogus information is return
1789 * via read() for time_enabled, time_running:
1790 */
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001791 if (event->state == PERF_EVENT_STATE_INACTIVE &&
1792 !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001793 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001794 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001795 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001796 }
1797
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001798 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001799 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001800
Alexander Shishkin44377272013-12-16 14:17:36 +02001801 perf_pmu_disable(event->pmu);
1802
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001803 event->tstamp_stopped = tstamp;
1804 event->pmu->del(event, 0);
1805 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001806 event->state = PERF_EVENT_STATE_INACTIVE;
1807 if (event->pending_disable) {
1808 event->pending_disable = 0;
1809 event->state = PERF_EVENT_STATE_OFF;
1810 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001811
1812 if (!is_software_event(event))
1813 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001814 if (!--ctx->nr_active)
1815 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001816 if (event->attr.freq && event->attr.sample_freq)
1817 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001818 if (event->attr.exclusive || !cpuctx->active_oncpu)
1819 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001820
1821 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001822}
1823
1824static void
1825group_sched_out(struct perf_event *group_event,
1826 struct perf_cpu_context *cpuctx,
1827 struct perf_event_context *ctx)
1828{
1829 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001830 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001831
Mark Rutland3f005e72016-07-26 18:12:21 +01001832 perf_pmu_disable(ctx->pmu);
1833
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001834 event_sched_out(group_event, cpuctx, ctx);
1835
1836 /*
1837 * Schedule out siblings (if any):
1838 */
1839 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1840 event_sched_out(event, cpuctx, ctx);
1841
Mark Rutland3f005e72016-07-26 18:12:21 +01001842 perf_pmu_enable(ctx->pmu);
1843
Stephane Eranianfa66f072010-08-26 16:40:01 +02001844 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001845 cpuctx->exclusive = 0;
1846}
1847
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001848#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001849
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001850/*
1851 * Cross CPU call to remove a performance event
1852 *
1853 * We disable the event on the hardware level first. After that we
1854 * remove it from the context list.
1855 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001856static void
1857__perf_remove_from_context(struct perf_event *event,
1858 struct perf_cpu_context *cpuctx,
1859 struct perf_event_context *ctx,
1860 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001861{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001862 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001864 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001865 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001866 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001867 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001868
Peter Zijlstra39a43642016-01-11 12:46:35 +01001869 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001870 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001871 if (ctx->task) {
1872 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1873 cpuctx->task_ctx = NULL;
1874 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001875 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001876}
1877
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001878/*
1879 * Remove the event from a task's (or a CPU's) list of events.
1880 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001881 * If event->ctx is a cloned context, callers must make sure that
1882 * every task struct that event->ctx->task could possibly point to
1883 * remains valid. This is OK when called from perf_release since
1884 * that only calls us on the top-level context, which can't be a clone.
1885 * When called from perf_event_exit_task, it's OK because the
1886 * context has been detached from its task.
1887 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001888static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001890 lockdep_assert_held(&event->ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001891
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001892 event_function_call(event, __perf_remove_from_context, (void *)flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001893}
1894
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001895/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001896 * Cross CPU call to disable a performance event
1897 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001898static void __perf_event_disable(struct perf_event *event,
1899 struct perf_cpu_context *cpuctx,
1900 struct perf_event_context *ctx,
1901 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001903 if (event->state < PERF_EVENT_STATE_INACTIVE)
1904 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001905
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001906 update_context_time(ctx);
1907 update_cgrp_time_from_event(event);
1908 update_group_times(event);
1909 if (event == event->group_leader)
1910 group_sched_out(event, cpuctx, ctx);
1911 else
1912 event_sched_out(event, cpuctx, ctx);
1913 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001914}
1915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916/*
1917 * Disable a event.
1918 *
1919 * If event->ctx is a cloned context, callers must make sure that
1920 * every task struct that event->ctx->task could possibly point to
1921 * remains valid. This condition is satisifed when called through
1922 * perf_event_for_each_child or perf_event_for_each because they
1923 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001924 * goes to exit will block in perf_event_exit_event().
1925 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926 * When called from perf_pending_event it's OK because event->ctx
1927 * is the current context on this CPU and preemption is disabled,
1928 * hence we can't get into perf_event_task_sched_out for this context.
1929 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001930static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001931{
1932 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001933
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001934 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001935 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001936 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001937 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001939 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001940
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001941 event_function_call(event, __perf_event_disable, NULL);
1942}
1943
1944void perf_event_disable_local(struct perf_event *event)
1945{
1946 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001947}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001948
1949/*
1950 * Strictly speaking kernel users cannot create groups and therefore this
1951 * interface does not need the perf_event_ctx_lock() magic.
1952 */
1953void perf_event_disable(struct perf_event *event)
1954{
1955 struct perf_event_context *ctx;
1956
1957 ctx = perf_event_ctx_lock(event);
1958 _perf_event_disable(event);
1959 perf_event_ctx_unlock(event, ctx);
1960}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001961EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001962
Stephane Eraniane5d13672011-02-14 11:20:01 +02001963static void perf_set_shadow_time(struct perf_event *event,
1964 struct perf_event_context *ctx,
1965 u64 tstamp)
1966{
1967 /*
1968 * use the correct time source for the time snapshot
1969 *
1970 * We could get by without this by leveraging the
1971 * fact that to get to this function, the caller
1972 * has most likely already called update_context_time()
1973 * and update_cgrp_time_xx() and thus both timestamp
1974 * are identical (or very close). Given that tstamp is,
1975 * already adjusted for cgroup, we could say that:
1976 * tstamp - ctx->timestamp
1977 * is equivalent to
1978 * tstamp - cgrp->timestamp.
1979 *
1980 * Then, in perf_output_read(), the calculation would
1981 * work with no changes because:
1982 * - event is guaranteed scheduled in
1983 * - no scheduled out in between
1984 * - thus the timestamp would be the same
1985 *
1986 * But this is a bit hairy.
1987 *
1988 * So instead, we have an explicit cgroup call to remain
1989 * within the time time source all along. We believe it
1990 * is cleaner and simpler to understand.
1991 */
1992 if (is_cgroup_event(event))
1993 perf_cgroup_set_shadow_time(event, tstamp);
1994 else
1995 event->shadow_ctx_time = tstamp - ctx->timestamp;
1996}
1997
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001998#define MAX_INTERRUPTS (~0ULL)
1999
2000static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002001static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002002
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002003static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002004event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002005 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002006 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002007{
Stephane Eranian41587552011-01-03 18:20:01 +02002008 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02002009 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002010
Peter Zijlstra63342412014-05-05 11:49:16 +02002011 lockdep_assert_held(&ctx->lock);
2012
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002013 if (event->state <= PERF_EVENT_STATE_OFF)
2014 return 0;
2015
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002016 WRITE_ONCE(event->oncpu, smp_processor_id());
2017 /*
2018 * Order event::oncpu write to happen before the ACTIVE state
2019 * is visible.
2020 */
2021 smp_wmb();
2022 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002023
2024 /*
2025 * Unthrottle events, since we scheduled we might have missed several
2026 * ticks already, also for a heavily scheduling task there is little
2027 * guarantee it'll get a tick in a timely manner.
2028 */
2029 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2030 perf_log_throttle(event, 1);
2031 event->hw.interrupts = 0;
2032 }
2033
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034 /*
2035 * The new state must be visible before we turn it on in the hardware:
2036 */
2037 smp_wmb();
2038
Alexander Shishkin44377272013-12-16 14:17:36 +02002039 perf_pmu_disable(event->pmu);
2040
Shaohua Li72f669c2015-02-05 15:55:31 -08002041 perf_set_shadow_time(event, ctx, tstamp);
2042
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002043 perf_log_itrace_start(event);
2044
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002045 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002046 event->state = PERF_EVENT_STATE_INACTIVE;
2047 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002048 ret = -EAGAIN;
2049 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002050 }
2051
Peter Zijlstra00a29162015-07-27 10:35:07 +02002052 event->tstamp_running += tstamp - event->tstamp_stopped;
2053
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002054 if (!is_software_event(event))
2055 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002056 if (!ctx->nr_active++)
2057 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002058 if (event->attr.freq && event->attr.sample_freq)
2059 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060
2061 if (event->attr.exclusive)
2062 cpuctx->exclusive = 1;
2063
Alexander Shishkin44377272013-12-16 14:17:36 +02002064out:
2065 perf_pmu_enable(event->pmu);
2066
2067 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002068}
2069
2070static int
2071group_sched_in(struct perf_event *group_event,
2072 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002073 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002074{
Lin Ming6bde9b62010-04-23 13:56:00 +08002075 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002076 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002077 u64 now = ctx->time;
2078 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002079
2080 if (group_event->state == PERF_EVENT_STATE_OFF)
2081 return 0;
2082
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002083 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002084
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002085 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002086 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002087 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002088 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002089 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002090
2091 /*
2092 * Schedule in siblings as one group (if any):
2093 */
2094 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002095 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002096 partial_group = event;
2097 goto group_error;
2098 }
2099 }
2100
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002101 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002102 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002103
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002104group_error:
2105 /*
2106 * Groups can be scheduled in as one unit only, so undo any
2107 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002108 * The events up to the failed event are scheduled out normally,
2109 * tstamp_stopped will be updated.
2110 *
2111 * The failed events and the remaining siblings need to have
2112 * their timings updated as if they had gone thru event_sched_in()
2113 * and event_sched_out(). This is required to get consistent timings
2114 * across the group. This also takes care of the case where the group
2115 * could never be scheduled by ensuring tstamp_stopped is set to mark
2116 * the time the event was actually stopped, such that time delta
2117 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002118 */
2119 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2120 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002121 simulate = true;
2122
2123 if (simulate) {
2124 event->tstamp_running += now - event->tstamp_stopped;
2125 event->tstamp_stopped = now;
2126 } else {
2127 event_sched_out(event, cpuctx, ctx);
2128 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002129 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002130 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002131
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002132 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002133
Peter Zijlstra272325c2015-04-15 11:41:58 +02002134 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002135
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 return -EAGAIN;
2137}
2138
2139/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002140 * Work out whether we can put this event group on the CPU now.
2141 */
2142static int group_can_go_on(struct perf_event *event,
2143 struct perf_cpu_context *cpuctx,
2144 int can_add_hw)
2145{
2146 /*
2147 * Groups consisting entirely of software events can always go on.
2148 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002149 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002150 return 1;
2151 /*
2152 * If an exclusive group is already on, no other hardware
2153 * events can go on.
2154 */
2155 if (cpuctx->exclusive)
2156 return 0;
2157 /*
2158 * If this group is exclusive and there are already
2159 * events on the CPU, it can't go on.
2160 */
2161 if (event->attr.exclusive && cpuctx->active_oncpu)
2162 return 0;
2163 /*
2164 * Otherwise, try to add it if all previous groups were able
2165 * to go on.
2166 */
2167 return can_add_hw;
2168}
2169
2170static void add_event_to_ctx(struct perf_event *event,
2171 struct perf_event_context *ctx)
2172{
Stephane Eranian41587552011-01-03 18:20:01 +02002173 u64 tstamp = perf_event_time(event);
2174
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002175 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002176 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002177 event->tstamp_enabled = tstamp;
2178 event->tstamp_running = tstamp;
2179 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002180}
2181
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002182static void ctx_sched_out(struct perf_event_context *ctx,
2183 struct perf_cpu_context *cpuctx,
2184 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002185static void
2186ctx_sched_in(struct perf_event_context *ctx,
2187 struct perf_cpu_context *cpuctx,
2188 enum event_type_t event_type,
2189 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002190
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002191static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2192 struct perf_event_context *ctx)
2193{
2194 if (!cpuctx->task_ctx)
2195 return;
2196
2197 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2198 return;
2199
2200 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2201}
2202
Peter Zijlstradce58552011-04-09 21:17:46 +02002203static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2204 struct perf_event_context *ctx,
2205 struct task_struct *task)
2206{
2207 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2208 if (ctx)
2209 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2210 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2211 if (ctx)
2212 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2213}
2214
Peter Zijlstra3e349502016-01-08 10:01:18 +01002215static void ctx_resched(struct perf_cpu_context *cpuctx,
2216 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002217{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002218 perf_pmu_disable(cpuctx->ctx.pmu);
2219 if (task_ctx)
2220 task_ctx_sched_out(cpuctx, task_ctx);
2221 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2222 perf_event_sched_in(cpuctx, task_ctx, current);
2223 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002224}
2225
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002226/*
2227 * Cross CPU call to install and enable a performance event
2228 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002229 * Very similar to remote_function() + event_function() but cannot assume that
2230 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002231 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002232static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002233{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002234 struct perf_event *event = info;
2235 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002236 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002237 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002238 bool activate = true;
2239 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002240
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002241 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002242 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002243 raw_spin_lock(&ctx->lock);
2244 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002245
2246 /* If we're on the wrong CPU, try again */
2247 if (task_cpu(ctx->task) != smp_processor_id()) {
2248 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002249 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002250 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002251
Peter Zijlstra39a43642016-01-11 12:46:35 +01002252 /*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002253 * If we're on the right CPU, see if the task we target is
2254 * current, if not we don't have to activate the ctx, a future
2255 * context switch will do that for us.
Peter Zijlstra39a43642016-01-11 12:46:35 +01002256 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002257 if (ctx->task != current)
2258 activate = false;
2259 else
2260 WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2261
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002262 } else if (task_ctx) {
2263 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002264 }
2265
Peter Zijlstraa0963092016-02-24 18:45:50 +01002266 if (activate) {
2267 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2268 add_event_to_ctx(event, ctx);
2269 ctx_resched(cpuctx, task_ctx);
2270 } else {
2271 add_event_to_ctx(event, ctx);
2272 }
2273
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002274unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002275 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002276
Peter Zijlstraa0963092016-02-24 18:45:50 +01002277 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002278}
2279
2280/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002281 * Attach a performance event to a context.
2282 *
2283 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284 */
2285static void
2286perf_install_in_context(struct perf_event_context *ctx,
2287 struct perf_event *event,
2288 int cpu)
2289{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002290 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002291
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002292 lockdep_assert_held(&ctx->mutex);
2293
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002294 if (event->cpu != -1)
2295 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002296
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002297 /*
2298 * Ensures that if we can observe event->ctx, both the event and ctx
2299 * will be 'complete'. See perf_iterate_sb_cpu().
2300 */
2301 smp_store_release(&event->ctx, ctx);
2302
Peter Zijlstraa0963092016-02-24 18:45:50 +01002303 if (!task) {
2304 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002305 return;
2306 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002307
Peter Zijlstraa0963092016-02-24 18:45:50 +01002308 /*
2309 * Should not happen, we validate the ctx is still alive before calling.
2310 */
2311 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2312 return;
2313
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002314 /*
2315 * Installing events is tricky because we cannot rely on ctx->is_active
2316 * to be set in case this is the nr_events 0 -> 1 transition.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002317 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002318again:
2319 /*
2320 * Cannot use task_function_call() because we need to run on the task's
2321 * CPU regardless of whether its current or not.
2322 */
2323 if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
2324 return;
2325
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002326 raw_spin_lock_irq(&ctx->lock);
2327 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002328 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2329 /*
2330 * Cannot happen because we already checked above (which also
2331 * cannot happen), and we hold ctx->mutex, which serializes us
2332 * against perf_event_exit_task_context().
2333 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002334 raw_spin_unlock_irq(&ctx->lock);
2335 return;
2336 }
Peter Zijlstra39a43642016-01-11 12:46:35 +01002337 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstraa0963092016-02-24 18:45:50 +01002338 /*
2339 * Since !ctx->is_active doesn't mean anything, we must IPI
2340 * unconditionally.
2341 */
2342 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002343}
2344
2345/*
2346 * Put a event into inactive state and update time fields.
2347 * Enabling the leader of a group effectively enables all
2348 * the group members that aren't explicitly disabled, so we
2349 * have to update their ->tstamp_enabled also.
2350 * Note: this works for group members as well as group leaders
2351 * since the non-leader members' sibling_lists will be empty.
2352 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002353static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002354{
2355 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002356 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002357
2358 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002359 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002360 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002361 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2362 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002363 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002364}
2365
2366/*
2367 * Cross CPU call to enable a performance event
2368 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002369static void __perf_event_enable(struct perf_event *event,
2370 struct perf_cpu_context *cpuctx,
2371 struct perf_event_context *ctx,
2372 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002373{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002374 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002375 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002376
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002377 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2378 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002379 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002380
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002381 if (ctx->is_active)
2382 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2383
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002384 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002385
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002386 if (!ctx->is_active)
2387 return;
2388
Stephane Eraniane5d13672011-02-14 11:20:01 +02002389 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002390 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002391 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002392 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002393 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002394 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002395
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002396 /*
2397 * If the event is in a group and isn't the group leader,
2398 * then don't put it on unless the group is on.
2399 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002400 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2401 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002402 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002403 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002404
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002405 task_ctx = cpuctx->task_ctx;
2406 if (ctx->task)
2407 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002408
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002409 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002410}
2411
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002412/*
2413 * Enable a event.
2414 *
2415 * If event->ctx is a cloned context, callers must make sure that
2416 * every task struct that event->ctx->task could possibly point to
2417 * remains valid. This condition is satisfied when called through
2418 * perf_event_for_each_child or perf_event_for_each as described
2419 * for perf_event_disable.
2420 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002421static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002422{
2423 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002424
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002425 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002426 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2427 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002428 raw_spin_unlock_irq(&ctx->lock);
2429 return;
2430 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002431
2432 /*
2433 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002434 *
2435 * That way, if we see the event in error state below, we know that it
2436 * has gone back into error state, as distinct from the task having
2437 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002438 */
2439 if (event->state == PERF_EVENT_STATE_ERROR)
2440 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002441 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002442
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002443 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002444}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002445
2446/*
2447 * See perf_event_disable();
2448 */
2449void perf_event_enable(struct perf_event *event)
2450{
2451 struct perf_event_context *ctx;
2452
2453 ctx = perf_event_ctx_lock(event);
2454 _perf_event_enable(event);
2455 perf_event_ctx_unlock(event, ctx);
2456}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002457EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002458
Alexander Shishkin375637b2016-04-27 18:44:46 +03002459struct stop_event_data {
2460 struct perf_event *event;
2461 unsigned int restart;
2462};
2463
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002464static int __perf_event_stop(void *info)
2465{
Alexander Shishkin375637b2016-04-27 18:44:46 +03002466 struct stop_event_data *sd = info;
2467 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002468
Alexander Shishkin375637b2016-04-27 18:44:46 +03002469 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002470 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2471 return 0;
2472
2473 /* matches smp_wmb() in event_sched_in() */
2474 smp_rmb();
2475
2476 /*
2477 * There is a window with interrupts enabled before we get here,
2478 * so we need to check again lest we try to stop another CPU's event.
2479 */
2480 if (READ_ONCE(event->oncpu) != smp_processor_id())
2481 return -EAGAIN;
2482
2483 event->pmu->stop(event, PERF_EF_UPDATE);
2484
Alexander Shishkin375637b2016-04-27 18:44:46 +03002485 /*
2486 * May race with the actual stop (through perf_pmu_output_stop()),
2487 * but it is only used for events with AUX ring buffer, and such
2488 * events will refuse to restart because of rb::aux_mmap_count==0,
2489 * see comments in perf_aux_output_begin().
2490 *
2491 * Since this is happening on a event-local CPU, no trace is lost
2492 * while restarting.
2493 */
2494 if (sd->restart)
2495 event->pmu->start(event, PERF_EF_START);
2496
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002497 return 0;
2498}
2499
Alexander Shishkin375637b2016-04-27 18:44:46 +03002500static int perf_event_restart(struct perf_event *event)
2501{
2502 struct stop_event_data sd = {
2503 .event = event,
2504 .restart = 1,
2505 };
2506 int ret = 0;
2507
2508 do {
2509 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2510 return 0;
2511
2512 /* matches smp_wmb() in event_sched_in() */
2513 smp_rmb();
2514
2515 /*
2516 * We only want to restart ACTIVE events, so if the event goes
2517 * inactive here (event->oncpu==-1), there's nothing more to do;
2518 * fall through with ret==-ENXIO.
2519 */
2520 ret = cpu_function_call(READ_ONCE(event->oncpu),
2521 __perf_event_stop, &sd);
2522 } while (ret == -EAGAIN);
2523
2524 return ret;
2525}
2526
2527/*
2528 * In order to contain the amount of racy and tricky in the address filter
2529 * configuration management, it is a two part process:
2530 *
2531 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2532 * we update the addresses of corresponding vmas in
2533 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2534 * (p2) when an event is scheduled in (pmu::add), it calls
2535 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2536 * if the generation has changed since the previous call.
2537 *
2538 * If (p1) happens while the event is active, we restart it to force (p2).
2539 *
2540 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2541 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2542 * ioctl;
2543 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2544 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2545 * for reading;
2546 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2547 * of exec.
2548 */
2549void perf_event_addr_filters_sync(struct perf_event *event)
2550{
2551 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2552
2553 if (!has_addr_filter(event))
2554 return;
2555
2556 raw_spin_lock(&ifh->lock);
2557 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2558 event->pmu->addr_filters_sync(event);
2559 event->hw.addr_filters_gen = event->addr_filters_gen;
2560 }
2561 raw_spin_unlock(&ifh->lock);
2562}
2563EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2564
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002565static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002566{
2567 /*
2568 * not supported on inherited events
2569 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002570 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002571 return -EINVAL;
2572
2573 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002574 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002575
2576 return 0;
2577}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002578
2579/*
2580 * See perf_event_disable()
2581 */
2582int perf_event_refresh(struct perf_event *event, int refresh)
2583{
2584 struct perf_event_context *ctx;
2585 int ret;
2586
2587 ctx = perf_event_ctx_lock(event);
2588 ret = _perf_event_refresh(event, refresh);
2589 perf_event_ctx_unlock(event, ctx);
2590
2591 return ret;
2592}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002593EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002594
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002595static void ctx_sched_out(struct perf_event_context *ctx,
2596 struct perf_cpu_context *cpuctx,
2597 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002598{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002599 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002600 struct perf_event *event;
2601
2602 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002603
Peter Zijlstra39a43642016-01-11 12:46:35 +01002604 if (likely(!ctx->nr_events)) {
2605 /*
2606 * See __perf_remove_from_context().
2607 */
2608 WARN_ON_ONCE(ctx->is_active);
2609 if (ctx->task)
2610 WARN_ON_ONCE(cpuctx->task_ctx);
2611 return;
2612 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002613
Peter Zijlstradb24d332011-04-09 21:17:45 +02002614 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002615 if (!(ctx->is_active & EVENT_ALL))
2616 ctx->is_active = 0;
2617
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002618 if (ctx->task) {
2619 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2620 if (!ctx->is_active)
2621 cpuctx->task_ctx = NULL;
2622 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002623
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002624 /*
2625 * Always update time if it was set; not only when it changes.
2626 * Otherwise we can 'forget' to update time for any but the last
2627 * context we sched out. For example:
2628 *
2629 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2630 * ctx_sched_out(.event_type = EVENT_PINNED)
2631 *
2632 * would only update time for the pinned events.
2633 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002634 if (is_active & EVENT_TIME) {
2635 /* update (and stop) ctx time */
2636 update_context_time(ctx);
2637 update_cgrp_time_from_cpuctx(cpuctx);
2638 }
2639
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002640 is_active ^= ctx->is_active; /* changed bits */
2641
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002642 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002643 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002644
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002645 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002646 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002647 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2648 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002649 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002650
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002651 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002652 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002653 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002654 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002655 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002656}
2657
2658/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002659 * Test whether two contexts are equivalent, i.e. whether they have both been
2660 * cloned from the same version of the same context.
2661 *
2662 * Equivalence is measured using a generation number in the context that is
2663 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2664 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002665 */
2666static int context_equiv(struct perf_event_context *ctx1,
2667 struct perf_event_context *ctx2)
2668{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002669 lockdep_assert_held(&ctx1->lock);
2670 lockdep_assert_held(&ctx2->lock);
2671
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002672 /* Pinning disables the swap optimization */
2673 if (ctx1->pin_count || ctx2->pin_count)
2674 return 0;
2675
2676 /* If ctx1 is the parent of ctx2 */
2677 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2678 return 1;
2679
2680 /* If ctx2 is the parent of ctx1 */
2681 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2682 return 1;
2683
2684 /*
2685 * If ctx1 and ctx2 have the same parent; we flatten the parent
2686 * hierarchy, see perf_event_init_context().
2687 */
2688 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2689 ctx1->parent_gen == ctx2->parent_gen)
2690 return 1;
2691
2692 /* Unmatched */
2693 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002694}
2695
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696static void __perf_event_sync_stat(struct perf_event *event,
2697 struct perf_event *next_event)
2698{
2699 u64 value;
2700
2701 if (!event->attr.inherit_stat)
2702 return;
2703
2704 /*
2705 * Update the event value, we cannot use perf_event_read()
2706 * because we're in the middle of a context switch and have IRQs
2707 * disabled, which upsets smp_call_function_single(), however
2708 * we know the event must be on the current CPU, therefore we
2709 * don't need to use it.
2710 */
2711 switch (event->state) {
2712 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002713 event->pmu->read(event);
2714 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002715
2716 case PERF_EVENT_STATE_INACTIVE:
2717 update_event_times(event);
2718 break;
2719
2720 default:
2721 break;
2722 }
2723
2724 /*
2725 * In order to keep per-task stats reliable we need to flip the event
2726 * values when we flip the contexts.
2727 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002728 value = local64_read(&next_event->count);
2729 value = local64_xchg(&event->count, value);
2730 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002731
2732 swap(event->total_time_enabled, next_event->total_time_enabled);
2733 swap(event->total_time_running, next_event->total_time_running);
2734
2735 /*
2736 * Since we swizzled the values, update the user visible data too.
2737 */
2738 perf_event_update_userpage(event);
2739 perf_event_update_userpage(next_event);
2740}
2741
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002742static void perf_event_sync_stat(struct perf_event_context *ctx,
2743 struct perf_event_context *next_ctx)
2744{
2745 struct perf_event *event, *next_event;
2746
2747 if (!ctx->nr_stat)
2748 return;
2749
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002750 update_context_time(ctx);
2751
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002752 event = list_first_entry(&ctx->event_list,
2753 struct perf_event, event_entry);
2754
2755 next_event = list_first_entry(&next_ctx->event_list,
2756 struct perf_event, event_entry);
2757
2758 while (&event->event_entry != &ctx->event_list &&
2759 &next_event->event_entry != &next_ctx->event_list) {
2760
2761 __perf_event_sync_stat(event, next_event);
2762
2763 event = list_next_entry(event, event_entry);
2764 next_event = list_next_entry(next_event, event_entry);
2765 }
2766}
2767
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002768static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2769 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002770{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002771 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002772 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002773 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002774 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002775 int do_switch = 1;
2776
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002777 if (likely(!ctx))
2778 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002779
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002780 cpuctx = __get_cpu_context(ctx);
2781 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002782 return;
2783
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002784 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002785 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002786 if (!next_ctx)
2787 goto unlock;
2788
2789 parent = rcu_dereference(ctx->parent_ctx);
2790 next_parent = rcu_dereference(next_ctx->parent_ctx);
2791
2792 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002793 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002794 goto unlock;
2795
2796 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002797 /*
2798 * Looks like the two contexts are clones, so we might be
2799 * able to optimize the context switch. We lock both
2800 * contexts and check that they are clones under the
2801 * lock (including re-checking that neither has been
2802 * uncloned in the meantime). It doesn't matter which
2803 * order we take the locks because no other cpu could
2804 * be trying to lock both of these tasks.
2805 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002806 raw_spin_lock(&ctx->lock);
2807 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002808 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002809 WRITE_ONCE(ctx->task, next);
2810 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002811
2812 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2813
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002814 /*
2815 * RCU_INIT_POINTER here is safe because we've not
2816 * modified the ctx and the above modification of
2817 * ctx->task and ctx->task_ctx_data are immaterial
2818 * since those values are always verified under
2819 * ctx->lock which we're now holding.
2820 */
2821 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2822 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2823
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002824 do_switch = 0;
2825
2826 perf_event_sync_stat(ctx, next_ctx);
2827 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002828 raw_spin_unlock(&next_ctx->lock);
2829 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002830 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002831unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002832 rcu_read_unlock();
2833
2834 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002835 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002836 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002837 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002838 }
2839}
2840
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002841static DEFINE_PER_CPU(struct list_head, sched_cb_list);
2842
Yan, Zhengba532502014-11-04 21:55:58 -05002843void perf_sched_cb_dec(struct pmu *pmu)
2844{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002845 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2846
Yan, Zhengba532502014-11-04 21:55:58 -05002847 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002848
2849 if (!--cpuctx->sched_cb_usage)
2850 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05002851}
2852
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002853
Yan, Zhengba532502014-11-04 21:55:58 -05002854void perf_sched_cb_inc(struct pmu *pmu)
2855{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002856 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2857
2858 if (!cpuctx->sched_cb_usage++)
2859 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
2860
Yan, Zhengba532502014-11-04 21:55:58 -05002861 this_cpu_inc(perf_sched_cb_usages);
2862}
2863
2864/*
2865 * This function provides the context switch callback to the lower code
2866 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02002867 *
2868 * This callback is relevant even to per-cpu events; for example multi event
2869 * PEBS requires this to provide PID/TID information. This requires we flush
2870 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05002871 */
2872static void perf_pmu_sched_task(struct task_struct *prev,
2873 struct task_struct *next,
2874 bool sched_in)
2875{
2876 struct perf_cpu_context *cpuctx;
2877 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05002878
2879 if (prev == next)
2880 return;
2881
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002882 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
2883 pmu = cpuctx->unique_pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05002884
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002885 if (WARN_ON_ONCE(!pmu->sched_task))
2886 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05002887
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002888 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2889 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05002890
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002891 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05002892
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002893 perf_pmu_enable(pmu);
2894 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05002895 }
Yan, Zhengba532502014-11-04 21:55:58 -05002896}
2897
Adrian Hunter45ac1402015-07-21 12:44:02 +03002898static void perf_event_switch(struct task_struct *task,
2899 struct task_struct *next_prev, bool sched_in);
2900
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002901#define for_each_task_context_nr(ctxn) \
2902 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2903
2904/*
2905 * Called from scheduler to remove the events of the current task,
2906 * with interrupts disabled.
2907 *
2908 * We stop each event and update the event value in event->count.
2909 *
2910 * This does not protect us against NMI, but disable()
2911 * sets the disabled bit in the control field of event _before_
2912 * accessing the event control register. If a NMI hits, then it will
2913 * not restart the event.
2914 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002915void __perf_event_task_sched_out(struct task_struct *task,
2916 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002917{
2918 int ctxn;
2919
Yan, Zhengba532502014-11-04 21:55:58 -05002920 if (__this_cpu_read(perf_sched_cb_usages))
2921 perf_pmu_sched_task(task, next, false);
2922
Adrian Hunter45ac1402015-07-21 12:44:02 +03002923 if (atomic_read(&nr_switch_events))
2924 perf_event_switch(task, next, false);
2925
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002926 for_each_task_context_nr(ctxn)
2927 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002928
2929 /*
2930 * if cgroup events exist on this CPU, then we need
2931 * to check if we have to switch out PMU state.
2932 * cgroup event are system-wide mode only
2933 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002934 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002935 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002936}
2937
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002938/*
2939 * Called with IRQs disabled
2940 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002941static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2942 enum event_type_t event_type)
2943{
2944 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002945}
2946
2947static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002948ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002949 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002950{
2951 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002952
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002953 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2954 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002955 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002956 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002957 continue;
2958
Stephane Eraniane5d13672011-02-14 11:20:01 +02002959 /* may need to reset tstamp_enabled */
2960 if (is_cgroup_event(event))
2961 perf_cgroup_mark_enabled(event, ctx);
2962
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002963 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002964 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002965
2966 /*
2967 * If this pinned group hasn't been scheduled,
2968 * put it in error state.
2969 */
2970 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2971 update_group_times(event);
2972 event->state = PERF_EVENT_STATE_ERROR;
2973 }
2974 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002975}
2976
2977static void
2978ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002979 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002980{
2981 struct perf_event *event;
2982 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002983
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002984 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2985 /* Ignore events in OFF or ERROR state */
2986 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002987 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002988 /*
2989 * Listen to the 'cpu' scheduling filter constraint
2990 * of events:
2991 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002992 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002993 continue;
2994
Stephane Eraniane5d13672011-02-14 11:20:01 +02002995 /* may need to reset tstamp_enabled */
2996 if (is_cgroup_event(event))
2997 perf_cgroup_mark_enabled(event, ctx);
2998
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002999 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01003000 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003001 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003002 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003003 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003004}
3005
3006static void
3007ctx_sched_in(struct perf_event_context *ctx,
3008 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003009 enum event_type_t event_type,
3010 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003011{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003012 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003013 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003014
Peter Zijlstrac994d612016-01-08 09:20:23 +01003015 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003016
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003017 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003018 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003019
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003020 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003021 if (ctx->task) {
3022 if (!is_active)
3023 cpuctx->task_ctx = ctx;
3024 else
3025 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3026 }
3027
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003028 is_active ^= ctx->is_active; /* changed bits */
3029
3030 if (is_active & EVENT_TIME) {
3031 /* start ctx time */
3032 now = perf_clock();
3033 ctx->timestamp = now;
3034 perf_cgroup_set_timestamp(task, ctx);
3035 }
3036
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003037 /*
3038 * First go through the list and put on any pinned groups
3039 * in order to give them the best chance of going on.
3040 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003041 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003042 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003043
3044 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003045 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003046 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047}
3048
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003049static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003050 enum event_type_t event_type,
3051 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003052{
3053 struct perf_event_context *ctx = &cpuctx->ctx;
3054
Stephane Eraniane5d13672011-02-14 11:20:01 +02003055 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003056}
3057
Stephane Eraniane5d13672011-02-14 11:20:01 +02003058static void perf_event_context_sched_in(struct perf_event_context *ctx,
3059 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003060{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003061 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003062
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003063 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003064 if (cpuctx->task_ctx == ctx)
3065 return;
3066
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003067 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003068 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003069 /*
3070 * We want to keep the following priority order:
3071 * cpu pinned (that don't need to move), task pinned,
3072 * cpu flexible, task flexible.
3073 */
3074 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003075 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003076 perf_pmu_enable(ctx->pmu);
3077 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003078}
3079
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003080/*
3081 * Called from scheduler to add the events of the current task
3082 * with interrupts disabled.
3083 *
3084 * We restore the event value and then enable it.
3085 *
3086 * This does not protect us against NMI, but enable()
3087 * sets the enabled bit in the control field of event _before_
3088 * accessing the event control register. If a NMI hits, then it will
3089 * keep the event running.
3090 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003091void __perf_event_task_sched_in(struct task_struct *prev,
3092 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003093{
3094 struct perf_event_context *ctx;
3095 int ctxn;
3096
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003097 /*
3098 * If cgroup events exist on this CPU, then we need to check if we have
3099 * to switch in PMU state; cgroup event are system-wide mode only.
3100 *
3101 * Since cgroup events are CPU events, we must schedule these in before
3102 * we schedule in the task events.
3103 */
3104 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3105 perf_cgroup_sched_in(prev, task);
3106
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003107 for_each_task_context_nr(ctxn) {
3108 ctx = task->perf_event_ctxp[ctxn];
3109 if (likely(!ctx))
3110 continue;
3111
Stephane Eraniane5d13672011-02-14 11:20:01 +02003112 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003113 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003114
Adrian Hunter45ac1402015-07-21 12:44:02 +03003115 if (atomic_read(&nr_switch_events))
3116 perf_event_switch(task, prev, true);
3117
Yan, Zhengba532502014-11-04 21:55:58 -05003118 if (__this_cpu_read(perf_sched_cb_usages))
3119 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003120}
3121
Peter Zijlstraabd50712010-01-26 18:50:16 +01003122static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3123{
3124 u64 frequency = event->attr.sample_freq;
3125 u64 sec = NSEC_PER_SEC;
3126 u64 divisor, dividend;
3127
3128 int count_fls, nsec_fls, frequency_fls, sec_fls;
3129
3130 count_fls = fls64(count);
3131 nsec_fls = fls64(nsec);
3132 frequency_fls = fls64(frequency);
3133 sec_fls = 30;
3134
3135 /*
3136 * We got @count in @nsec, with a target of sample_freq HZ
3137 * the target period becomes:
3138 *
3139 * @count * 10^9
3140 * period = -------------------
3141 * @nsec * sample_freq
3142 *
3143 */
3144
3145 /*
3146 * Reduce accuracy by one bit such that @a and @b converge
3147 * to a similar magnitude.
3148 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003149#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003150do { \
3151 if (a##_fls > b##_fls) { \
3152 a >>= 1; \
3153 a##_fls--; \
3154 } else { \
3155 b >>= 1; \
3156 b##_fls--; \
3157 } \
3158} while (0)
3159
3160 /*
3161 * Reduce accuracy until either term fits in a u64, then proceed with
3162 * the other, so that finally we can do a u64/u64 division.
3163 */
3164 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3165 REDUCE_FLS(nsec, frequency);
3166 REDUCE_FLS(sec, count);
3167 }
3168
3169 if (count_fls + sec_fls > 64) {
3170 divisor = nsec * frequency;
3171
3172 while (count_fls + sec_fls > 64) {
3173 REDUCE_FLS(count, sec);
3174 divisor >>= 1;
3175 }
3176
3177 dividend = count * sec;
3178 } else {
3179 dividend = count * sec;
3180
3181 while (nsec_fls + frequency_fls > 64) {
3182 REDUCE_FLS(nsec, frequency);
3183 dividend >>= 1;
3184 }
3185
3186 divisor = nsec * frequency;
3187 }
3188
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003189 if (!divisor)
3190 return dividend;
3191
Peter Zijlstraabd50712010-01-26 18:50:16 +01003192 return div64_u64(dividend, divisor);
3193}
3194
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003195static DEFINE_PER_CPU(int, perf_throttled_count);
3196static DEFINE_PER_CPU(u64, perf_throttled_seq);
3197
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003198static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199{
3200 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003201 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003202 s64 delta;
3203
Peter Zijlstraabd50712010-01-26 18:50:16 +01003204 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003205
3206 delta = (s64)(period - hwc->sample_period);
3207 delta = (delta + 7) / 8; /* low pass filter */
3208
3209 sample_period = hwc->sample_period + delta;
3210
3211 if (!sample_period)
3212 sample_period = 1;
3213
3214 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003215
Peter Zijlstrae7850592010-05-21 14:43:08 +02003216 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003217 if (disable)
3218 event->pmu->stop(event, PERF_EF_UPDATE);
3219
Peter Zijlstrae7850592010-05-21 14:43:08 +02003220 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003221
3222 if (disable)
3223 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003224 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003225}
3226
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003227/*
3228 * combine freq adjustment with unthrottling to avoid two passes over the
3229 * events. At the same time, make sure, having freq events does not change
3230 * the rate of unthrottling as that would introduce bias.
3231 */
3232static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3233 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003234{
3235 struct perf_event *event;
3236 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003237 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003238 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003239
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003240 /*
3241 * only need to iterate over all events iff:
3242 * - context have events in frequency mode (needs freq adjust)
3243 * - there are events to unthrottle on this cpu
3244 */
3245 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003246 return;
3247
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003248 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003249 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003250
Paul Mackerras03541f82009-10-14 16:58:03 +11003251 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003252 if (event->state != PERF_EVENT_STATE_ACTIVE)
3253 continue;
3254
Stephane Eranian5632ab12011-01-03 18:20:01 +02003255 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003256 continue;
3257
Alexander Shishkin44377272013-12-16 14:17:36 +02003258 perf_pmu_disable(event->pmu);
3259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003260 hwc = &event->hw;
3261
Jiri Olsaae23bff2013-08-24 16:45:54 +02003262 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003263 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003264 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003265 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003266 }
3267
3268 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003269 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003270
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003271 /*
3272 * stop the event and update event->count
3273 */
3274 event->pmu->stop(event, PERF_EF_UPDATE);
3275
Peter Zijlstrae7850592010-05-21 14:43:08 +02003276 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003277 delta = now - hwc->freq_count_stamp;
3278 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003279
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003280 /*
3281 * restart the event
3282 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003283 * we have stopped the event so tell that
3284 * to perf_adjust_period() to avoid stopping it
3285 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003286 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003287 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003288 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003289
3290 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003291 next:
3292 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003294
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003295 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003296 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003297}
3298
3299/*
3300 * Round-robin a context's events:
3301 */
3302static void rotate_ctx(struct perf_event_context *ctx)
3303{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003304 /*
3305 * Rotate the first entry last of non-pinned groups. Rotation might be
3306 * disabled by the inheritance code.
3307 */
3308 if (!ctx->rotate_disable)
3309 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003310}
3311
Stephane Eranian9e630202013-04-03 14:21:33 +02003312static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003313{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003314 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003315 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003316
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003317 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003318 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3319 rotate = 1;
3320 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003321
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003322 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003323 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003324 if (ctx->nr_events != ctx->nr_active)
3325 rotate = 1;
3326 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003327
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003328 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003329 goto done;
3330
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003331 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003332 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003333
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003334 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3335 if (ctx)
3336 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003337
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003338 rotate_ctx(&cpuctx->ctx);
3339 if (ctx)
3340 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003341
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003342 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003343
3344 perf_pmu_enable(cpuctx->ctx.pmu);
3345 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003346done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003347
3348 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003349}
3350
3351void perf_event_task_tick(void)
3352{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003353 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3354 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003355 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003356
3357 WARN_ON(!irqs_disabled());
3358
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003359 __this_cpu_inc(perf_throttled_seq);
3360 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003361 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003362
Mark Rutland2fde4f92015-01-07 15:01:54 +00003363 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003364 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003365}
3366
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003367static int event_enable_on_exec(struct perf_event *event,
3368 struct perf_event_context *ctx)
3369{
3370 if (!event->attr.enable_on_exec)
3371 return 0;
3372
3373 event->attr.enable_on_exec = 0;
3374 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3375 return 0;
3376
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003377 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003378
3379 return 1;
3380}
3381
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003382/*
3383 * Enable all of a task's events that have been marked enable-on-exec.
3384 * This expects task == current.
3385 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003386static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003388 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003389 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390 struct perf_event *event;
3391 unsigned long flags;
3392 int enabled = 0;
3393
3394 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003395 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003396 if (!ctx || !ctx->nr_events)
3397 goto out;
3398
Peter Zijlstra3e349502016-01-08 10:01:18 +01003399 cpuctx = __get_cpu_context(ctx);
3400 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003401 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003402 list_for_each_entry(event, &ctx->event_list, event_entry)
3403 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003404
3405 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003406 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003408 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003409 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003410 ctx_resched(cpuctx, ctx);
3411 }
3412 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003413
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003414out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003415 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003416
3417 if (clone_ctx)
3418 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003419}
3420
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003421struct perf_read_data {
3422 struct perf_event *event;
3423 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003424 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003425};
3426
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003427/*
3428 * Cross CPU call to read the hardware event
3429 */
3430static void __perf_event_read(void *info)
3431{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003432 struct perf_read_data *data = info;
3433 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003434 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003435 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003436 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003437
3438 /*
3439 * If this is a task context, we need to check whether it is
3440 * the current task context of this cpu. If not it has been
3441 * scheduled out before the smp call arrived. In that case
3442 * event->count would have been updated to a recent sample
3443 * when the event was scheduled out.
3444 */
3445 if (ctx->task && cpuctx->task_ctx != ctx)
3446 return;
3447
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003448 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003449 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003450 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003451 update_cgrp_time_from_event(event);
3452 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003453
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003454 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003455 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003456 goto unlock;
3457
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003458 if (!data->group) {
3459 pmu->read(event);
3460 data->ret = 0;
3461 goto unlock;
3462 }
3463
3464 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3465
3466 pmu->read(event);
3467
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003468 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3469 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003470 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3471 /*
3472 * Use sibling's PMU rather than @event's since
3473 * sibling could be on different (eg: software) PMU.
3474 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003475 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003476 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003477 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003478
3479 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003480
3481unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003482 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003483}
3484
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003485static inline u64 perf_event_count(struct perf_event *event)
3486{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003487 if (event->pmu->count)
3488 return event->pmu->count(event);
3489
3490 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003491}
3492
Kaixu Xiaffe86902015-08-06 07:02:32 +00003493/*
3494 * NMI-safe method to read a local event, that is an event that
3495 * is:
3496 * - either for the current task, or for this CPU
3497 * - does not have inherit set, for inherited task events
3498 * will not be local and we cannot read them atomically
3499 * - must not have a pmu::count method
3500 */
3501u64 perf_event_read_local(struct perf_event *event)
3502{
3503 unsigned long flags;
3504 u64 val;
3505
3506 /*
3507 * Disabling interrupts avoids all counter scheduling (context
3508 * switches, timer based rotation and IPIs).
3509 */
3510 local_irq_save(flags);
3511
3512 /* If this is a per-task event, it must be for current */
3513 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3514 event->hw.target != current);
3515
3516 /* If this is a per-CPU event, it must be for this CPU */
3517 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3518 event->cpu != smp_processor_id());
3519
3520 /*
3521 * It must not be an event with inherit set, we cannot read
3522 * all child counters from atomic context.
3523 */
3524 WARN_ON_ONCE(event->attr.inherit);
3525
3526 /*
3527 * It must not have a pmu::count method, those are not
3528 * NMI safe.
3529 */
3530 WARN_ON_ONCE(event->pmu->count);
3531
3532 /*
3533 * If the event is currently on this CPU, its either a per-task event,
3534 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3535 * oncpu == -1).
3536 */
3537 if (event->oncpu == smp_processor_id())
3538 event->pmu->read(event);
3539
3540 val = local64_read(&event->count);
3541 local_irq_restore(flags);
3542
3543 return val;
3544}
3545
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003546static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003547{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003548 int ret = 0;
3549
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003550 /*
3551 * If event is enabled and currently active on a CPU, update the
3552 * value in the event structure:
3553 */
3554 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003555 struct perf_read_data data = {
3556 .event = event,
3557 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003558 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003559 };
David Carrillo-Cisneros71e7bc22016-08-17 13:55:04 -07003560 ret = smp_call_function_single(event->oncpu, __perf_event_read, &data, 1);
3561 /* The event must have been read from an online CPU: */
3562 WARN_ON_ONCE(ret);
3563 ret = ret ? : data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003564 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003565 struct perf_event_context *ctx = event->ctx;
3566 unsigned long flags;
3567
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003568 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003569 /*
3570 * may read while context is not active
3571 * (e.g., thread is blocked), in that case
3572 * we cannot update context time
3573 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003574 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003575 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003576 update_cgrp_time_from_event(event);
3577 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003578 if (group)
3579 update_group_times(event);
3580 else
3581 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003582 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003583 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003584
3585 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003586}
3587
3588/*
3589 * Initialize the perf_event context in a task_struct:
3590 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003591static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003592{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003593 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003594 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003595 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003596 INIT_LIST_HEAD(&ctx->pinned_groups);
3597 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003598 INIT_LIST_HEAD(&ctx->event_list);
3599 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003600}
3601
Peter Zijlstraeb184472010-09-07 15:55:13 +02003602static struct perf_event_context *
3603alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003604{
3605 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003606
3607 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3608 if (!ctx)
3609 return NULL;
3610
3611 __perf_event_init_context(ctx);
3612 if (task) {
3613 ctx->task = task;
3614 get_task_struct(task);
3615 }
3616 ctx->pmu = pmu;
3617
3618 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003619}
3620
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003621static struct task_struct *
3622find_lively_task_by_vpid(pid_t vpid)
3623{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003624 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003625
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003626 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003627 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003628 task = current;
3629 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003630 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003631 if (task)
3632 get_task_struct(task);
3633 rcu_read_unlock();
3634
3635 if (!task)
3636 return ERR_PTR(-ESRCH);
3637
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003638 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003639}
3640
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003641/*
3642 * Returns a matching context with refcount and pincount.
3643 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003644static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003645find_get_context(struct pmu *pmu, struct task_struct *task,
3646 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003647{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003648 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003649 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003650 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003651 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003652 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003653 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003654
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003655 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003656 /* Must be root to operate on a CPU event: */
3657 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3658 return ERR_PTR(-EACCES);
3659
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003660 /*
3661 * We could be clever and allow to attach a event to an
3662 * offline CPU and activate it when the CPU comes up, but
3663 * that's for later.
3664 */
3665 if (!cpu_online(cpu))
3666 return ERR_PTR(-ENODEV);
3667
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003668 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003669 ctx = &cpuctx->ctx;
3670 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003671 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003672
3673 return ctx;
3674 }
3675
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003676 err = -EINVAL;
3677 ctxn = pmu->task_ctx_nr;
3678 if (ctxn < 0)
3679 goto errout;
3680
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003681 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3682 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3683 if (!task_ctx_data) {
3684 err = -ENOMEM;
3685 goto errout;
3686 }
3687 }
3688
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003689retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003690 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003691 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003692 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003693 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003694
3695 if (task_ctx_data && !ctx->task_ctx_data) {
3696 ctx->task_ctx_data = task_ctx_data;
3697 task_ctx_data = NULL;
3698 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003699 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003700
3701 if (clone_ctx)
3702 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003703 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003704 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003705 err = -ENOMEM;
3706 if (!ctx)
3707 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003708
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003709 if (task_ctx_data) {
3710 ctx->task_ctx_data = task_ctx_data;
3711 task_ctx_data = NULL;
3712 }
3713
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003714 err = 0;
3715 mutex_lock(&task->perf_event_mutex);
3716 /*
3717 * If it has already passed perf_event_exit_task().
3718 * we must see PF_EXITING, it takes this mutex too.
3719 */
3720 if (task->flags & PF_EXITING)
3721 err = -ESRCH;
3722 else if (task->perf_event_ctxp[ctxn])
3723 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003724 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003725 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003726 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003727 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003728 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003729 mutex_unlock(&task->perf_event_mutex);
3730
3731 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003732 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003733
3734 if (err == -EAGAIN)
3735 goto retry;
3736 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003737 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003738 }
3739
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003740 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003741 return ctx;
3742
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003743errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003744 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003745 return ERR_PTR(err);
3746}
3747
Li Zefan6fb29152009-10-15 11:21:42 +08003748static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003749static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003750
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003751static void free_event_rcu(struct rcu_head *head)
3752{
3753 struct perf_event *event;
3754
3755 event = container_of(head, struct perf_event, rcu_head);
3756 if (event->ns)
3757 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003758 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759 kfree(event);
3760}
3761
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003762static void ring_buffer_attach(struct perf_event *event,
3763 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003764
Kan Liangf2fb6be2016-03-23 11:24:37 -07003765static void detach_sb_event(struct perf_event *event)
3766{
3767 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
3768
3769 raw_spin_lock(&pel->lock);
3770 list_del_rcu(&event->sb_list);
3771 raw_spin_unlock(&pel->lock);
3772}
3773
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003774static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07003775{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003776 struct perf_event_attr *attr = &event->attr;
3777
Kan Liangf2fb6be2016-03-23 11:24:37 -07003778 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003779 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003780
3781 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003782 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003783
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003784 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
3785 attr->comm || attr->comm_exec ||
3786 attr->task ||
3787 attr->context_switch)
3788 return true;
3789 return false;
3790}
3791
3792static void unaccount_pmu_sb_event(struct perf_event *event)
3793{
3794 if (is_sb_event(event))
3795 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003796}
3797
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003798static void unaccount_event_cpu(struct perf_event *event, int cpu)
3799{
3800 if (event->parent)
3801 return;
3802
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003803 if (is_cgroup_event(event))
3804 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3805}
3806
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003807#ifdef CONFIG_NO_HZ_FULL
3808static DEFINE_SPINLOCK(nr_freq_lock);
3809#endif
3810
3811static void unaccount_freq_event_nohz(void)
3812{
3813#ifdef CONFIG_NO_HZ_FULL
3814 spin_lock(&nr_freq_lock);
3815 if (atomic_dec_and_test(&nr_freq_events))
3816 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3817 spin_unlock(&nr_freq_lock);
3818#endif
3819}
3820
3821static void unaccount_freq_event(void)
3822{
3823 if (tick_nohz_full_enabled())
3824 unaccount_freq_event_nohz();
3825 else
3826 atomic_dec(&nr_freq_events);
3827}
3828
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003829static void unaccount_event(struct perf_event *event)
3830{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003831 bool dec = false;
3832
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003833 if (event->parent)
3834 return;
3835
3836 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003837 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003838 if (event->attr.mmap || event->attr.mmap_data)
3839 atomic_dec(&nr_mmap_events);
3840 if (event->attr.comm)
3841 atomic_dec(&nr_comm_events);
3842 if (event->attr.task)
3843 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003844 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003845 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003846 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003847 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003848 atomic_dec(&nr_switch_events);
3849 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003850 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003851 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003852 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003853 dec = true;
3854
Peter Zijlstra9107c892016-02-24 18:45:45 +01003855 if (dec) {
3856 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3857 schedule_delayed_work(&perf_sched_work, HZ);
3858 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003859
3860 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003861
3862 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003863}
3864
Peter Zijlstra9107c892016-02-24 18:45:45 +01003865static void perf_sched_delayed(struct work_struct *work)
3866{
3867 mutex_lock(&perf_sched_mutex);
3868 if (atomic_dec_and_test(&perf_sched_count))
3869 static_branch_disable(&perf_sched_events);
3870 mutex_unlock(&perf_sched_mutex);
3871}
3872
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003873/*
3874 * The following implement mutual exclusion of events on "exclusive" pmus
3875 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3876 * at a time, so we disallow creating events that might conflict, namely:
3877 *
3878 * 1) cpu-wide events in the presence of per-task events,
3879 * 2) per-task events in the presence of cpu-wide events,
3880 * 3) two matching events on the same context.
3881 *
3882 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003883 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003884 */
3885static int exclusive_event_init(struct perf_event *event)
3886{
3887 struct pmu *pmu = event->pmu;
3888
3889 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3890 return 0;
3891
3892 /*
3893 * Prevent co-existence of per-task and cpu-wide events on the
3894 * same exclusive pmu.
3895 *
3896 * Negative pmu::exclusive_cnt means there are cpu-wide
3897 * events on this "exclusive" pmu, positive means there are
3898 * per-task events.
3899 *
3900 * Since this is called in perf_event_alloc() path, event::ctx
3901 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3902 * to mean "per-task event", because unlike other attach states it
3903 * never gets cleared.
3904 */
3905 if (event->attach_state & PERF_ATTACH_TASK) {
3906 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3907 return -EBUSY;
3908 } else {
3909 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3910 return -EBUSY;
3911 }
3912
3913 return 0;
3914}
3915
3916static void exclusive_event_destroy(struct perf_event *event)
3917{
3918 struct pmu *pmu = event->pmu;
3919
3920 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3921 return;
3922
3923 /* see comment in exclusive_event_init() */
3924 if (event->attach_state & PERF_ATTACH_TASK)
3925 atomic_dec(&pmu->exclusive_cnt);
3926 else
3927 atomic_inc(&pmu->exclusive_cnt);
3928}
3929
3930static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3931{
3932 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3933 (e1->cpu == e2->cpu ||
3934 e1->cpu == -1 ||
3935 e2->cpu == -1))
3936 return true;
3937 return false;
3938}
3939
3940/* Called under the same ctx::mutex as perf_install_in_context() */
3941static bool exclusive_event_installable(struct perf_event *event,
3942 struct perf_event_context *ctx)
3943{
3944 struct perf_event *iter_event;
3945 struct pmu *pmu = event->pmu;
3946
3947 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3948 return true;
3949
3950 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3951 if (exclusive_event_match(iter_event, event))
3952 return false;
3953 }
3954
3955 return true;
3956}
3957
Alexander Shishkin375637b2016-04-27 18:44:46 +03003958static void perf_addr_filters_splice(struct perf_event *event,
3959 struct list_head *head);
3960
Peter Zijlstra683ede42014-05-05 12:11:24 +02003961static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003962{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003963 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003964
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003965 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003966
Frederic Weisbecker76369132011-05-19 19:55:04 +02003967 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003968 /*
3969 * Can happen when we close an event with re-directed output.
3970 *
3971 * Since we have a 0 refcount, perf_mmap_close() will skip
3972 * over us; possibly making our ring_buffer_put() the last.
3973 */
3974 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003975 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003976 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003977 }
3978
Stephane Eraniane5d13672011-02-14 11:20:01 +02003979 if (is_cgroup_event(event))
3980 perf_detach_cgroup(event);
3981
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003982 if (!event->parent) {
3983 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3984 put_callchain_buffers();
3985 }
3986
3987 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03003988 perf_addr_filters_splice(event, NULL);
3989 kfree(event->addr_filters_offs);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003990
3991 if (event->destroy)
3992 event->destroy(event);
3993
3994 if (event->ctx)
3995 put_ctx(event->ctx);
3996
Alexander Shishkin62a92c82016-06-07 15:44:15 +03003997 exclusive_event_destroy(event);
3998 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003999
4000 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004001}
4002
Peter Zijlstra683ede42014-05-05 12:11:24 +02004003/*
4004 * Used to free events which have a known refcount of 1, such as in error paths
4005 * where the event isn't exposed yet and inherited events.
4006 */
4007static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004008{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004009 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4010 "unexpected event refcount: %ld; ptr=%p\n",
4011 atomic_long_read(&event->refcount), event)) {
4012 /* leak to avoid use-after-free */
4013 return;
4014 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004015
Peter Zijlstra683ede42014-05-05 12:11:24 +02004016 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004017}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004018
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004019/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004020 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004021 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004022static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004023{
Peter Zijlstra88821352010-11-09 19:01:43 +01004024 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004025
Peter Zijlstra88821352010-11-09 19:01:43 +01004026 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004027 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004028 * Matches the smp_store_release() in perf_event_exit_task(). If we
4029 * observe !owner it means the list deletion is complete and we can
4030 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004031 * owner->perf_event_mutex.
4032 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004033 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004034 if (owner) {
4035 /*
4036 * Since delayed_put_task_struct() also drops the last
4037 * task reference we can safely take a new reference
4038 * while holding the rcu_read_lock().
4039 */
4040 get_task_struct(owner);
4041 }
4042 rcu_read_unlock();
4043
4044 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004045 /*
4046 * If we're here through perf_event_exit_task() we're already
4047 * holding ctx->mutex which would be an inversion wrt. the
4048 * normal lock order.
4049 *
4050 * However we can safely take this lock because its the child
4051 * ctx->mutex.
4052 */
4053 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4054
Peter Zijlstra88821352010-11-09 19:01:43 +01004055 /*
4056 * We have to re-check the event->owner field, if it is cleared
4057 * we raced with perf_event_exit_task(), acquiring the mutex
4058 * ensured they're done, and we can proceed with freeing the
4059 * event.
4060 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004061 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004062 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004063 smp_store_release(&event->owner, NULL);
4064 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004065 mutex_unlock(&owner->perf_event_mutex);
4066 put_task_struct(owner);
4067 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004068}
4069
Jiri Olsaf8697762014-08-01 14:33:01 +02004070static void put_event(struct perf_event *event)
4071{
Jiri Olsaf8697762014-08-01 14:33:01 +02004072 if (!atomic_long_dec_and_test(&event->refcount))
4073 return;
4074
Peter Zijlstra683ede42014-05-05 12:11:24 +02004075 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004076}
4077
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004078/*
4079 * Kill an event dead; while event:refcount will preserve the event
4080 * object, it will not preserve its functionality. Once the last 'user'
4081 * gives up the object, we'll destroy the thing.
4082 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004083int perf_event_release_kernel(struct perf_event *event)
4084{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004085 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004086 struct perf_event *child, *tmp;
4087
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004088 /*
4089 * If we got here through err_file: fput(event_file); we will not have
4090 * attached to a context yet.
4091 */
4092 if (!ctx) {
4093 WARN_ON_ONCE(event->attach_state &
4094 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4095 goto no_ctx;
4096 }
4097
Peter Zijlstra88821352010-11-09 19:01:43 +01004098 if (!is_kernel_event(event))
4099 perf_remove_from_owner(event);
4100
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004101 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004102 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004103 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004104
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004105 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004106 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004107 * Mark this even as STATE_DEAD, there is no external reference to it
4108 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004109 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004110 * Anybody acquiring event->child_mutex after the below loop _must_
4111 * also see this, most importantly inherit_event() which will avoid
4112 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004113 *
4114 * Thus this guarantees that we will in fact observe and kill _ALL_
4115 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004116 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004117 event->state = PERF_EVENT_STATE_DEAD;
4118 raw_spin_unlock_irq(&ctx->lock);
4119
4120 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004121
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004122again:
4123 mutex_lock(&event->child_mutex);
4124 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004125
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004126 /*
4127 * Cannot change, child events are not migrated, see the
4128 * comment with perf_event_ctx_lock_nested().
4129 */
4130 ctx = lockless_dereference(child->ctx);
4131 /*
4132 * Since child_mutex nests inside ctx::mutex, we must jump
4133 * through hoops. We start by grabbing a reference on the ctx.
4134 *
4135 * Since the event cannot get freed while we hold the
4136 * child_mutex, the context must also exist and have a !0
4137 * reference count.
4138 */
4139 get_ctx(ctx);
4140
4141 /*
4142 * Now that we have a ctx ref, we can drop child_mutex, and
4143 * acquire ctx::mutex without fear of it going away. Then we
4144 * can re-acquire child_mutex.
4145 */
4146 mutex_unlock(&event->child_mutex);
4147 mutex_lock(&ctx->mutex);
4148 mutex_lock(&event->child_mutex);
4149
4150 /*
4151 * Now that we hold ctx::mutex and child_mutex, revalidate our
4152 * state, if child is still the first entry, it didn't get freed
4153 * and we can continue doing so.
4154 */
4155 tmp = list_first_entry_or_null(&event->child_list,
4156 struct perf_event, child_list);
4157 if (tmp == child) {
4158 perf_remove_from_context(child, DETACH_GROUP);
4159 list_del(&child->child_list);
4160 free_event(child);
4161 /*
4162 * This matches the refcount bump in inherit_event();
4163 * this can't be the last reference.
4164 */
4165 put_event(event);
4166 }
4167
4168 mutex_unlock(&event->child_mutex);
4169 mutex_unlock(&ctx->mutex);
4170 put_ctx(ctx);
4171 goto again;
4172 }
4173 mutex_unlock(&event->child_mutex);
4174
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004175no_ctx:
4176 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004177 return 0;
4178}
4179EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4180
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004181/*
4182 * Called when the last reference to the file is gone.
4183 */
Al Viroa6fa9412012-08-20 14:59:25 +01004184static int perf_release(struct inode *inode, struct file *file)
4185{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004186 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004187 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004188}
4189
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004190u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004191{
4192 struct perf_event *child;
4193 u64 total = 0;
4194
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004195 *enabled = 0;
4196 *running = 0;
4197
Peter Zijlstra6f105812009-11-20 22:19:56 +01004198 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004199
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004200 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004201 total += perf_event_count(event);
4202
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004203 *enabled += event->total_time_enabled +
4204 atomic64_read(&event->child_total_time_enabled);
4205 *running += event->total_time_running +
4206 atomic64_read(&event->child_total_time_running);
4207
4208 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004209 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004210 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004211 *enabled += child->total_time_enabled;
4212 *running += child->total_time_running;
4213 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004214 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004215
4216 return total;
4217}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004218EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004219
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004220static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004221 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004222{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004223 struct perf_event *sub;
4224 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004225 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004226
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004227 ret = perf_event_read(leader, true);
4228 if (ret)
4229 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004230
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004231 /*
4232 * Since we co-schedule groups, {enabled,running} times of siblings
4233 * will be identical to those of the leader, so we only publish one
4234 * set.
4235 */
4236 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4237 values[n++] += leader->total_time_enabled +
4238 atomic64_read(&leader->child_total_time_enabled);
4239 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004240
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004241 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4242 values[n++] += leader->total_time_running +
4243 atomic64_read(&leader->child_total_time_running);
4244 }
4245
4246 /*
4247 * Write {count,id} tuples for every sibling.
4248 */
4249 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004250 if (read_format & PERF_FORMAT_ID)
4251 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004252
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004253 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004254 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004255 if (read_format & PERF_FORMAT_ID)
4256 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004257 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004258
4259 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004260}
4261
4262static int perf_read_group(struct perf_event *event,
4263 u64 read_format, char __user *buf)
4264{
4265 struct perf_event *leader = event->group_leader, *child;
4266 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004267 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004268 u64 *values;
4269
4270 lockdep_assert_held(&ctx->mutex);
4271
4272 values = kzalloc(event->read_size, GFP_KERNEL);
4273 if (!values)
4274 return -ENOMEM;
4275
4276 values[0] = 1 + leader->nr_siblings;
4277
4278 /*
4279 * By locking the child_mutex of the leader we effectively
4280 * lock the child list of all siblings.. XXX explain how.
4281 */
4282 mutex_lock(&leader->child_mutex);
4283
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004284 ret = __perf_read_group_add(leader, read_format, values);
4285 if (ret)
4286 goto unlock;
4287
4288 list_for_each_entry(child, &leader->child_list, child_list) {
4289 ret = __perf_read_group_add(child, read_format, values);
4290 if (ret)
4291 goto unlock;
4292 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004293
4294 mutex_unlock(&leader->child_mutex);
4295
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004296 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004297 if (copy_to_user(buf, values, event->read_size))
4298 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004299 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004300
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004301unlock:
4302 mutex_unlock(&leader->child_mutex);
4303out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004304 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004305 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004306}
4307
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004308static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004309 u64 read_format, char __user *buf)
4310{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004311 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004312 u64 values[4];
4313 int n = 0;
4314
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004315 values[n++] = perf_event_read_value(event, &enabled, &running);
4316 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4317 values[n++] = enabled;
4318 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4319 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004320 if (read_format & PERF_FORMAT_ID)
4321 values[n++] = primary_event_id(event);
4322
4323 if (copy_to_user(buf, values, n * sizeof(u64)))
4324 return -EFAULT;
4325
4326 return n * sizeof(u64);
4327}
4328
Jiri Olsadc633982014-09-12 13:18:26 +02004329static bool is_event_hup(struct perf_event *event)
4330{
4331 bool no_children;
4332
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004333 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004334 return false;
4335
4336 mutex_lock(&event->child_mutex);
4337 no_children = list_empty(&event->child_list);
4338 mutex_unlock(&event->child_mutex);
4339 return no_children;
4340}
4341
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004342/*
4343 * Read the performance event - simple non blocking version for now
4344 */
4345static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004346__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004347{
4348 u64 read_format = event->attr.read_format;
4349 int ret;
4350
4351 /*
4352 * Return end-of-file for a read on a event that is in
4353 * error state (i.e. because it was pinned but it couldn't be
4354 * scheduled on to the CPU at some point).
4355 */
4356 if (event->state == PERF_EVENT_STATE_ERROR)
4357 return 0;
4358
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004359 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004360 return -ENOSPC;
4361
4362 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004364 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004365 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004366 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004367
4368 return ret;
4369}
4370
4371static ssize_t
4372perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4373{
4374 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004375 struct perf_event_context *ctx;
4376 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004377
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004378 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004379 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004380 perf_event_ctx_unlock(event, ctx);
4381
4382 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004383}
4384
4385static unsigned int perf_poll(struct file *file, poll_table *wait)
4386{
4387 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004388 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004389 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004390
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004391 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004392
Jiri Olsadc633982014-09-12 13:18:26 +02004393 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004394 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004395
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004396 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004397 * Pin the event->rb by taking event->mmap_mutex; otherwise
4398 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004399 */
4400 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004401 rb = event->rb;
4402 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004403 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004404 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004405 return events;
4406}
4407
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004408static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004409{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004410 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004411 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004412 perf_event_update_userpage(event);
4413}
4414
4415/*
4416 * Holding the top-level event's child_mutex means that any
4417 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004418 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004419 * task existence requirements of perf_event_enable/disable.
4420 */
4421static void perf_event_for_each_child(struct perf_event *event,
4422 void (*func)(struct perf_event *))
4423{
4424 struct perf_event *child;
4425
4426 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004427
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004428 mutex_lock(&event->child_mutex);
4429 func(event);
4430 list_for_each_entry(child, &event->child_list, child_list)
4431 func(child);
4432 mutex_unlock(&event->child_mutex);
4433}
4434
4435static void perf_event_for_each(struct perf_event *event,
4436 void (*func)(struct perf_event *))
4437{
4438 struct perf_event_context *ctx = event->ctx;
4439 struct perf_event *sibling;
4440
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004441 lockdep_assert_held(&ctx->mutex);
4442
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004443 event = event->group_leader;
4444
4445 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004446 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004447 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004448}
4449
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004450static void __perf_event_period(struct perf_event *event,
4451 struct perf_cpu_context *cpuctx,
4452 struct perf_event_context *ctx,
4453 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004454{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004455 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004456 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004457
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004458 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004459 event->attr.sample_freq = value;
4460 } else {
4461 event->attr.sample_period = value;
4462 event->hw.sample_period = value;
4463 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004464
4465 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4466 if (active) {
4467 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004468 /*
4469 * We could be throttled; unthrottle now to avoid the tick
4470 * trying to unthrottle while we already re-started the event.
4471 */
4472 if (event->hw.interrupts == MAX_INTERRUPTS) {
4473 event->hw.interrupts = 0;
4474 perf_log_throttle(event, 1);
4475 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004476 event->pmu->stop(event, PERF_EF_UPDATE);
4477 }
4478
4479 local64_set(&event->hw.period_left, 0);
4480
4481 if (active) {
4482 event->pmu->start(event, PERF_EF_RELOAD);
4483 perf_pmu_enable(ctx->pmu);
4484 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004485}
4486
4487static int perf_event_period(struct perf_event *event, u64 __user *arg)
4488{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004489 u64 value;
4490
4491 if (!is_sampling_event(event))
4492 return -EINVAL;
4493
4494 if (copy_from_user(&value, arg, sizeof(value)))
4495 return -EFAULT;
4496
4497 if (!value)
4498 return -EINVAL;
4499
4500 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4501 return -EINVAL;
4502
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004503 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004504
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004505 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506}
4507
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004508static const struct file_operations perf_fops;
4509
Al Viro2903ff02012-08-28 12:52:22 -04004510static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004511{
Al Viro2903ff02012-08-28 12:52:22 -04004512 struct fd f = fdget(fd);
4513 if (!f.file)
4514 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004515
Al Viro2903ff02012-08-28 12:52:22 -04004516 if (f.file->f_op != &perf_fops) {
4517 fdput(f);
4518 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004519 }
Al Viro2903ff02012-08-28 12:52:22 -04004520 *p = f;
4521 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004522}
4523
4524static int perf_event_set_output(struct perf_event *event,
4525 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004526static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004527static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004528
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004529static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004531 void (*func)(struct perf_event *);
4532 u32 flags = arg;
4533
4534 switch (cmd) {
4535 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004536 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004537 break;
4538 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004539 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004540 break;
4541 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004542 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004543 break;
4544
4545 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004546 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004547
4548 case PERF_EVENT_IOC_PERIOD:
4549 return perf_event_period(event, (u64 __user *)arg);
4550
Jiri Olsacf4957f2012-10-24 13:37:58 +02004551 case PERF_EVENT_IOC_ID:
4552 {
4553 u64 id = primary_event_id(event);
4554
4555 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4556 return -EFAULT;
4557 return 0;
4558 }
4559
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004560 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004561 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004562 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004563 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004564 struct perf_event *output_event;
4565 struct fd output;
4566 ret = perf_fget_light(arg, &output);
4567 if (ret)
4568 return ret;
4569 output_event = output.file->private_data;
4570 ret = perf_event_set_output(event, output_event);
4571 fdput(output);
4572 } else {
4573 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004574 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004575 return ret;
4576 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004577
Li Zefan6fb29152009-10-15 11:21:42 +08004578 case PERF_EVENT_IOC_SET_FILTER:
4579 return perf_event_set_filter(event, (void __user *)arg);
4580
Alexei Starovoitov25415172015-03-25 12:49:20 -07004581 case PERF_EVENT_IOC_SET_BPF:
4582 return perf_event_set_bpf_prog(event, arg);
4583
Wang Nan86e79722016-03-28 06:41:29 +00004584 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4585 struct ring_buffer *rb;
4586
4587 rcu_read_lock();
4588 rb = rcu_dereference(event->rb);
4589 if (!rb || !rb->nr_pages) {
4590 rcu_read_unlock();
4591 return -EINVAL;
4592 }
4593 rb_toggle_paused(rb, !!arg);
4594 rcu_read_unlock();
4595 return 0;
4596 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004597 default:
4598 return -ENOTTY;
4599 }
4600
4601 if (flags & PERF_IOC_FLAG_GROUP)
4602 perf_event_for_each(event, func);
4603 else
4604 perf_event_for_each_child(event, func);
4605
4606 return 0;
4607}
4608
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004609static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4610{
4611 struct perf_event *event = file->private_data;
4612 struct perf_event_context *ctx;
4613 long ret;
4614
4615 ctx = perf_event_ctx_lock(event);
4616 ret = _perf_ioctl(event, cmd, arg);
4617 perf_event_ctx_unlock(event, ctx);
4618
4619 return ret;
4620}
4621
Pawel Mollb3f20782014-06-13 16:03:32 +01004622#ifdef CONFIG_COMPAT
4623static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4624 unsigned long arg)
4625{
4626 switch (_IOC_NR(cmd)) {
4627 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4628 case _IOC_NR(PERF_EVENT_IOC_ID):
4629 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4630 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4631 cmd &= ~IOCSIZE_MASK;
4632 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4633 }
4634 break;
4635 }
4636 return perf_ioctl(file, cmd, arg);
4637}
4638#else
4639# define perf_compat_ioctl NULL
4640#endif
4641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642int perf_event_task_enable(void)
4643{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004644 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004645 struct perf_event *event;
4646
4647 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004648 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4649 ctx = perf_event_ctx_lock(event);
4650 perf_event_for_each_child(event, _perf_event_enable);
4651 perf_event_ctx_unlock(event, ctx);
4652 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004653 mutex_unlock(&current->perf_event_mutex);
4654
4655 return 0;
4656}
4657
4658int perf_event_task_disable(void)
4659{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004660 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004661 struct perf_event *event;
4662
4663 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004664 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4665 ctx = perf_event_ctx_lock(event);
4666 perf_event_for_each_child(event, _perf_event_disable);
4667 perf_event_ctx_unlock(event, ctx);
4668 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004669 mutex_unlock(&current->perf_event_mutex);
4670
4671 return 0;
4672}
4673
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004674static int perf_event_index(struct perf_event *event)
4675{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004676 if (event->hw.state & PERF_HES_STOPPED)
4677 return 0;
4678
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004679 if (event->state != PERF_EVENT_STATE_ACTIVE)
4680 return 0;
4681
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004682 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004683}
4684
Eric B Munsonc4794292011-06-23 16:34:38 -04004685static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004686 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004687 u64 *enabled,
4688 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004689{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004690 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004691
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004692 *now = perf_clock();
4693 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004694 *enabled = ctx_time - event->tstamp_enabled;
4695 *running = ctx_time - event->tstamp_running;
4696}
4697
Peter Zijlstrafa731582013-09-19 10:16:42 +02004698static void perf_event_init_userpage(struct perf_event *event)
4699{
4700 struct perf_event_mmap_page *userpg;
4701 struct ring_buffer *rb;
4702
4703 rcu_read_lock();
4704 rb = rcu_dereference(event->rb);
4705 if (!rb)
4706 goto unlock;
4707
4708 userpg = rb->user_page;
4709
4710 /* Allow new userspace to detect that bit 0 is deprecated */
4711 userpg->cap_bit0_is_deprecated = 1;
4712 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004713 userpg->data_offset = PAGE_SIZE;
4714 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004715
4716unlock:
4717 rcu_read_unlock();
4718}
4719
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004720void __weak arch_perf_update_userpage(
4721 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004722{
4723}
4724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004725/*
4726 * Callers need to ensure there can be no nesting of this function, otherwise
4727 * the seqlock logic goes bad. We can not serialize this because the arch
4728 * code calls this from NMI context.
4729 */
4730void perf_event_update_userpage(struct perf_event *event)
4731{
4732 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004733 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004734 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004735
4736 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004737 rb = rcu_dereference(event->rb);
4738 if (!rb)
4739 goto unlock;
4740
Eric B Munson0d641202011-06-24 12:26:26 -04004741 /*
4742 * compute total_time_enabled, total_time_running
4743 * based on snapshot values taken when the event
4744 * was last scheduled in.
4745 *
4746 * we cannot simply called update_context_time()
4747 * because of locking issue as we can be called in
4748 * NMI context
4749 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004750 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004751
Frederic Weisbecker76369132011-05-19 19:55:04 +02004752 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004753 /*
4754 * Disable preemption so as to not let the corresponding user-space
4755 * spin too long if we get preempted.
4756 */
4757 preempt_disable();
4758 ++userpg->lock;
4759 barrier();
4760 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004761 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004762 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004763 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764
Eric B Munson0d641202011-06-24 12:26:26 -04004765 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004766 atomic64_read(&event->child_total_time_enabled);
4767
Eric B Munson0d641202011-06-24 12:26:26 -04004768 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004769 atomic64_read(&event->child_total_time_running);
4770
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004771 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004773 barrier();
4774 ++userpg->lock;
4775 preempt_enable();
4776unlock:
4777 rcu_read_unlock();
4778}
4779
Peter Zijlstra906010b2009-09-21 16:08:49 +02004780static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4781{
4782 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004783 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004784 int ret = VM_FAULT_SIGBUS;
4785
4786 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4787 if (vmf->pgoff == 0)
4788 ret = 0;
4789 return ret;
4790 }
4791
4792 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004793 rb = rcu_dereference(event->rb);
4794 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004795 goto unlock;
4796
4797 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4798 goto unlock;
4799
Frederic Weisbecker76369132011-05-19 19:55:04 +02004800 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004801 if (!vmf->page)
4802 goto unlock;
4803
4804 get_page(vmf->page);
4805 vmf->page->mapping = vma->vm_file->f_mapping;
4806 vmf->page->index = vmf->pgoff;
4807
4808 ret = 0;
4809unlock:
4810 rcu_read_unlock();
4811
4812 return ret;
4813}
4814
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004815static void ring_buffer_attach(struct perf_event *event,
4816 struct ring_buffer *rb)
4817{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004818 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004819 unsigned long flags;
4820
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004821 if (event->rb) {
4822 /*
4823 * Should be impossible, we set this when removing
4824 * event->rb_entry and wait/clear when adding event->rb_entry.
4825 */
4826 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004827
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004828 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004829 spin_lock_irqsave(&old_rb->event_lock, flags);
4830 list_del_rcu(&event->rb_entry);
4831 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004832
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004833 event->rcu_batches = get_state_synchronize_rcu();
4834 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004835 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004836
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004837 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004838 if (event->rcu_pending) {
4839 cond_synchronize_rcu(event->rcu_batches);
4840 event->rcu_pending = 0;
4841 }
4842
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004843 spin_lock_irqsave(&rb->event_lock, flags);
4844 list_add_rcu(&event->rb_entry, &rb->event_list);
4845 spin_unlock_irqrestore(&rb->event_lock, flags);
4846 }
4847
4848 rcu_assign_pointer(event->rb, rb);
4849
4850 if (old_rb) {
4851 ring_buffer_put(old_rb);
4852 /*
4853 * Since we detached before setting the new rb, so that we
4854 * could attach the new rb, we could have missed a wakeup.
4855 * Provide it now.
4856 */
4857 wake_up_all(&event->waitq);
4858 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004859}
4860
4861static void ring_buffer_wakeup(struct perf_event *event)
4862{
4863 struct ring_buffer *rb;
4864
4865 rcu_read_lock();
4866 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004867 if (rb) {
4868 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4869 wake_up_all(&event->waitq);
4870 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004871 rcu_read_unlock();
4872}
4873
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004874struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004875{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004876 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004877
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004878 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004879 rb = rcu_dereference(event->rb);
4880 if (rb) {
4881 if (!atomic_inc_not_zero(&rb->refcount))
4882 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004883 }
4884 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004885
Frederic Weisbecker76369132011-05-19 19:55:04 +02004886 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004887}
4888
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004889void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004890{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004891 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004892 return;
4893
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004894 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004895
Frederic Weisbecker76369132011-05-19 19:55:04 +02004896 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004897}
4898
4899static void perf_mmap_open(struct vm_area_struct *vma)
4900{
4901 struct perf_event *event = vma->vm_file->private_data;
4902
4903 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004904 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004905
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004906 if (vma->vm_pgoff)
4907 atomic_inc(&event->rb->aux_mmap_count);
4908
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004909 if (event->pmu->event_mapped)
4910 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004911}
4912
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004913static void perf_pmu_output_stop(struct perf_event *event);
4914
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004915/*
4916 * A buffer can be mmap()ed multiple times; either directly through the same
4917 * event, or through other events by use of perf_event_set_output().
4918 *
4919 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4920 * the buffer here, where we still have a VM context. This means we need
4921 * to detach all events redirecting to us.
4922 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004923static void perf_mmap_close(struct vm_area_struct *vma)
4924{
4925 struct perf_event *event = vma->vm_file->private_data;
4926
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004927 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004928 struct user_struct *mmap_user = rb->mmap_user;
4929 int mmap_locked = rb->mmap_locked;
4930 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004931
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004932 if (event->pmu->event_unmapped)
4933 event->pmu->event_unmapped(event);
4934
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004935 /*
4936 * rb->aux_mmap_count will always drop before rb->mmap_count and
4937 * event->mmap_count, so it is ok to use event->mmap_mutex to
4938 * serialize with perf_mmap here.
4939 */
4940 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4941 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004942 /*
4943 * Stop all AUX events that are writing to this buffer,
4944 * so that we can free its AUX pages and corresponding PMU
4945 * data. Note that after rb::aux_mmap_count dropped to zero,
4946 * they won't start any more (see perf_aux_output_begin()).
4947 */
4948 perf_pmu_output_stop(event);
4949
4950 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004951 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4952 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4953
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004954 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004955 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004956 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
4957
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004958 mutex_unlock(&event->mmap_mutex);
4959 }
4960
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004961 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004962
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004963 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004964 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004965
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004966 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004967 mutex_unlock(&event->mmap_mutex);
4968
4969 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004970 if (atomic_read(&rb->mmap_count))
4971 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004972
4973 /*
4974 * No other mmap()s, detach from all other events that might redirect
4975 * into the now unreachable buffer. Somewhat complicated by the
4976 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4977 */
4978again:
4979 rcu_read_lock();
4980 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4981 if (!atomic_long_inc_not_zero(&event->refcount)) {
4982 /*
4983 * This event is en-route to free_event() which will
4984 * detach it and remove it from the list.
4985 */
4986 continue;
4987 }
4988 rcu_read_unlock();
4989
4990 mutex_lock(&event->mmap_mutex);
4991 /*
4992 * Check we didn't race with perf_event_set_output() which can
4993 * swizzle the rb from under us while we were waiting to
4994 * acquire mmap_mutex.
4995 *
4996 * If we find a different rb; ignore this event, a next
4997 * iteration will no longer find it on the list. We have to
4998 * still restart the iteration to make sure we're not now
4999 * iterating the wrong list.
5000 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005001 if (event->rb == rb)
5002 ring_buffer_attach(event, NULL);
5003
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005004 mutex_unlock(&event->mmap_mutex);
5005 put_event(event);
5006
5007 /*
5008 * Restart the iteration; either we're on the wrong list or
5009 * destroyed its integrity by doing a deletion.
5010 */
5011 goto again;
5012 }
5013 rcu_read_unlock();
5014
5015 /*
5016 * It could be there's still a few 0-ref events on the list; they'll
5017 * get cleaned up by free_event() -- they'll also still have their
5018 * ref on the rb and will free it whenever they are done with it.
5019 *
5020 * Aside from that, this buffer is 'fully' detached and unmapped,
5021 * undo the VM accounting.
5022 */
5023
5024 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
5025 vma->vm_mm->pinned_vm -= mmap_locked;
5026 free_uid(mmap_user);
5027
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005028out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005029 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005030}
5031
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04005032static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005033 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005034 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005035 .fault = perf_mmap_fault,
5036 .page_mkwrite = perf_mmap_fault,
5037};
5038
5039static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5040{
5041 struct perf_event *event = file->private_data;
5042 unsigned long user_locked, user_lock_limit;
5043 struct user_struct *user = current_user();
5044 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005045 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005046 unsigned long vma_size;
5047 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005048 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005049 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005050
Peter Zijlstrac7920612010-05-18 10:33:24 +02005051 /*
5052 * Don't allow mmap() of inherited per-task counters. This would
5053 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005054 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005055 */
5056 if (event->cpu == -1 && event->attr.inherit)
5057 return -EINVAL;
5058
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005059 if (!(vma->vm_flags & VM_SHARED))
5060 return -EINVAL;
5061
5062 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005063
5064 if (vma->vm_pgoff == 0) {
5065 nr_pages = (vma_size / PAGE_SIZE) - 1;
5066 } else {
5067 /*
5068 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5069 * mapped, all subsequent mappings should have the same size
5070 * and offset. Must be above the normal perf buffer.
5071 */
5072 u64 aux_offset, aux_size;
5073
5074 if (!event->rb)
5075 return -EINVAL;
5076
5077 nr_pages = vma_size / PAGE_SIZE;
5078
5079 mutex_lock(&event->mmap_mutex);
5080 ret = -EINVAL;
5081
5082 rb = event->rb;
5083 if (!rb)
5084 goto aux_unlock;
5085
5086 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
5087 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
5088
5089 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5090 goto aux_unlock;
5091
5092 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5093 goto aux_unlock;
5094
5095 /* already mapped with a different offset */
5096 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5097 goto aux_unlock;
5098
5099 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5100 goto aux_unlock;
5101
5102 /* already mapped with a different size */
5103 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5104 goto aux_unlock;
5105
5106 if (!is_power_of_2(nr_pages))
5107 goto aux_unlock;
5108
5109 if (!atomic_inc_not_zero(&rb->mmap_count))
5110 goto aux_unlock;
5111
5112 if (rb_has_aux(rb)) {
5113 atomic_inc(&rb->aux_mmap_count);
5114 ret = 0;
5115 goto unlock;
5116 }
5117
5118 atomic_set(&rb->aux_mmap_count, 1);
5119 user_extra = nr_pages;
5120
5121 goto accounting;
5122 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005123
5124 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005125 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005126 * can do bitmasks instead of modulo.
5127 */
Kan Liang2ed11312015-03-02 02:14:26 -05005128 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005129 return -EINVAL;
5130
5131 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5132 return -EINVAL;
5133
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005134 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005135again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005136 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005137 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005138 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005139 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005140 goto unlock;
5141 }
5142
5143 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5144 /*
5145 * Raced against perf_mmap_close() through
5146 * perf_event_set_output(). Try again, hope for better
5147 * luck.
5148 */
5149 mutex_unlock(&event->mmap_mutex);
5150 goto again;
5151 }
5152
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005153 goto unlock;
5154 }
5155
5156 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005157
5158accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005159 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5160
5161 /*
5162 * Increase the limit linearly with more CPUs:
5163 */
5164 user_lock_limit *= num_online_cpus();
5165
5166 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5167
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005168 if (user_locked > user_lock_limit)
5169 extra = user_locked - user_lock_limit;
5170
Jiri Slaby78d7d402010-03-05 13:42:54 -08005171 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005172 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005173 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005174
5175 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5176 !capable(CAP_IPC_LOCK)) {
5177 ret = -EPERM;
5178 goto unlock;
5179 }
5180
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005181 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005182
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005183 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005184 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005185
Frederic Weisbecker76369132011-05-19 19:55:04 +02005186 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005187 rb = rb_alloc(nr_pages,
5188 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5189 event->cpu, flags);
5190
5191 if (!rb) {
5192 ret = -ENOMEM;
5193 goto unlock;
5194 }
5195
5196 atomic_set(&rb->mmap_count, 1);
5197 rb->mmap_user = get_current_user();
5198 rb->mmap_locked = extra;
5199
5200 ring_buffer_attach(event, rb);
5201
5202 perf_event_init_userpage(event);
5203 perf_event_update_userpage(event);
5204 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005205 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5206 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005207 if (!ret)
5208 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005209 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005210
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005211unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005212 if (!ret) {
5213 atomic_long_add(user_extra, &user->locked_vm);
5214 vma->vm_mm->pinned_vm += extra;
5215
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005216 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005217 } else if (rb) {
5218 atomic_dec(&rb->mmap_count);
5219 }
5220aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005221 mutex_unlock(&event->mmap_mutex);
5222
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005223 /*
5224 * Since pinned accounting is per vm we cannot allow fork() to copy our
5225 * vma.
5226 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005227 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005228 vma->vm_ops = &perf_mmap_vmops;
5229
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005230 if (event->pmu->event_mapped)
5231 event->pmu->event_mapped(event);
5232
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233 return ret;
5234}
5235
5236static int perf_fasync(int fd, struct file *filp, int on)
5237{
Al Viro496ad9a2013-01-23 17:07:38 -05005238 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239 struct perf_event *event = filp->private_data;
5240 int retval;
5241
Al Viro59551022016-01-22 15:40:57 -05005242 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005243 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005244 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245
5246 if (retval < 0)
5247 return retval;
5248
5249 return 0;
5250}
5251
5252static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005253 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005254 .release = perf_release,
5255 .read = perf_read,
5256 .poll = perf_poll,
5257 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005258 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005259 .mmap = perf_mmap,
5260 .fasync = perf_fasync,
5261};
5262
5263/*
5264 * Perf event wakeup
5265 *
5266 * If there's data, ensure we set the poll() state and publish everything
5267 * to user-space before waking everybody up.
5268 */
5269
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005270static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5271{
5272 /* only the parent has fasync state */
5273 if (event->parent)
5274 event = event->parent;
5275 return &event->fasync;
5276}
5277
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278void perf_event_wakeup(struct perf_event *event)
5279{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005280 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005281
5282 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005283 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005284 event->pending_kill = 0;
5285 }
5286}
5287
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005288static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005289{
5290 struct perf_event *event = container_of(entry,
5291 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005292 int rctx;
5293
5294 rctx = perf_swevent_get_recursion_context();
5295 /*
5296 * If we 'fail' here, that's OK, it means recursion is already disabled
5297 * and we won't recurse 'further'.
5298 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005299
5300 if (event->pending_disable) {
5301 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005302 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005303 }
5304
5305 if (event->pending_wakeup) {
5306 event->pending_wakeup = 0;
5307 perf_event_wakeup(event);
5308 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005309
5310 if (rctx >= 0)
5311 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005312}
5313
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005314/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005315 * We assume there is only KVM supporting the callbacks.
5316 * Later on, we might change it to a list if there is
5317 * another virtualization implementation supporting the callbacks.
5318 */
5319struct perf_guest_info_callbacks *perf_guest_cbs;
5320
5321int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5322{
5323 perf_guest_cbs = cbs;
5324 return 0;
5325}
5326EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5327
5328int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5329{
5330 perf_guest_cbs = NULL;
5331 return 0;
5332}
5333EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5334
Jiri Olsa40189942012-08-07 15:20:37 +02005335static void
5336perf_output_sample_regs(struct perf_output_handle *handle,
5337 struct pt_regs *regs, u64 mask)
5338{
5339 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305340 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02005341
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305342 bitmap_from_u64(_mask, mask);
5343 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02005344 u64 val;
5345
5346 val = perf_reg_value(regs, bit);
5347 perf_output_put(handle, val);
5348 }
5349}
5350
Stephane Eranian60e23642014-09-24 13:48:37 +02005351static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005352 struct pt_regs *regs,
5353 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005354{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005355 if (user_mode(regs)) {
5356 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005357 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005358 } else if (current->mm) {
5359 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005360 } else {
5361 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5362 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005363 }
5364}
5365
Stephane Eranian60e23642014-09-24 13:48:37 +02005366static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5367 struct pt_regs *regs)
5368{
5369 regs_intr->regs = regs;
5370 regs_intr->abi = perf_reg_abi(current);
5371}
5372
5373
Jiri Olsac5ebced2012-08-07 15:20:40 +02005374/*
5375 * Get remaining task size from user stack pointer.
5376 *
5377 * It'd be better to take stack vma map and limit this more
5378 * precisly, but there's no way to get it safely under interrupt,
5379 * so using TASK_SIZE as limit.
5380 */
5381static u64 perf_ustack_task_size(struct pt_regs *regs)
5382{
5383 unsigned long addr = perf_user_stack_pointer(regs);
5384
5385 if (!addr || addr >= TASK_SIZE)
5386 return 0;
5387
5388 return TASK_SIZE - addr;
5389}
5390
5391static u16
5392perf_sample_ustack_size(u16 stack_size, u16 header_size,
5393 struct pt_regs *regs)
5394{
5395 u64 task_size;
5396
5397 /* No regs, no stack pointer, no dump. */
5398 if (!regs)
5399 return 0;
5400
5401 /*
5402 * Check if we fit in with the requested stack size into the:
5403 * - TASK_SIZE
5404 * If we don't, we limit the size to the TASK_SIZE.
5405 *
5406 * - remaining sample size
5407 * If we don't, we customize the stack size to
5408 * fit in to the remaining sample size.
5409 */
5410
5411 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5412 stack_size = min(stack_size, (u16) task_size);
5413
5414 /* Current header size plus static size and dynamic size. */
5415 header_size += 2 * sizeof(u64);
5416
5417 /* Do we fit in with the current stack dump size? */
5418 if ((u16) (header_size + stack_size) < header_size) {
5419 /*
5420 * If we overflow the maximum size for the sample,
5421 * we customize the stack dump size to fit in.
5422 */
5423 stack_size = USHRT_MAX - header_size - sizeof(u64);
5424 stack_size = round_up(stack_size, sizeof(u64));
5425 }
5426
5427 return stack_size;
5428}
5429
5430static void
5431perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5432 struct pt_regs *regs)
5433{
5434 /* Case of a kernel thread, nothing to dump */
5435 if (!regs) {
5436 u64 size = 0;
5437 perf_output_put(handle, size);
5438 } else {
5439 unsigned long sp;
5440 unsigned int rem;
5441 u64 dyn_size;
5442
5443 /*
5444 * We dump:
5445 * static size
5446 * - the size requested by user or the best one we can fit
5447 * in to the sample max size
5448 * data
5449 * - user stack dump data
5450 * dynamic size
5451 * - the actual dumped size
5452 */
5453
5454 /* Static size. */
5455 perf_output_put(handle, dump_size);
5456
5457 /* Data. */
5458 sp = perf_user_stack_pointer(regs);
5459 rem = __output_copy_user(handle, (void *) sp, dump_size);
5460 dyn_size = dump_size - rem;
5461
5462 perf_output_skip(handle, rem);
5463
5464 /* Dynamic size. */
5465 perf_output_put(handle, dyn_size);
5466 }
5467}
5468
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005469static void __perf_event_header__init_id(struct perf_event_header *header,
5470 struct perf_sample_data *data,
5471 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005472{
5473 u64 sample_type = event->attr.sample_type;
5474
5475 data->type = sample_type;
5476 header->size += event->id_header_size;
5477
5478 if (sample_type & PERF_SAMPLE_TID) {
5479 /* namespace issues */
5480 data->tid_entry.pid = perf_event_pid(event, current);
5481 data->tid_entry.tid = perf_event_tid(event, current);
5482 }
5483
5484 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005485 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005486
Adrian Hunterff3d5272013-08-27 11:23:07 +03005487 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005488 data->id = primary_event_id(event);
5489
5490 if (sample_type & PERF_SAMPLE_STREAM_ID)
5491 data->stream_id = event->id;
5492
5493 if (sample_type & PERF_SAMPLE_CPU) {
5494 data->cpu_entry.cpu = raw_smp_processor_id();
5495 data->cpu_entry.reserved = 0;
5496 }
5497}
5498
Frederic Weisbecker76369132011-05-19 19:55:04 +02005499void perf_event_header__init_id(struct perf_event_header *header,
5500 struct perf_sample_data *data,
5501 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005502{
5503 if (event->attr.sample_id_all)
5504 __perf_event_header__init_id(header, data, event);
5505}
5506
5507static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5508 struct perf_sample_data *data)
5509{
5510 u64 sample_type = data->type;
5511
5512 if (sample_type & PERF_SAMPLE_TID)
5513 perf_output_put(handle, data->tid_entry);
5514
5515 if (sample_type & PERF_SAMPLE_TIME)
5516 perf_output_put(handle, data->time);
5517
5518 if (sample_type & PERF_SAMPLE_ID)
5519 perf_output_put(handle, data->id);
5520
5521 if (sample_type & PERF_SAMPLE_STREAM_ID)
5522 perf_output_put(handle, data->stream_id);
5523
5524 if (sample_type & PERF_SAMPLE_CPU)
5525 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005526
5527 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5528 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005529}
5530
Frederic Weisbecker76369132011-05-19 19:55:04 +02005531void perf_event__output_id_sample(struct perf_event *event,
5532 struct perf_output_handle *handle,
5533 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005534{
5535 if (event->attr.sample_id_all)
5536 __perf_event__output_id_sample(handle, sample);
5537}
5538
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005539static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005540 struct perf_event *event,
5541 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005542{
5543 u64 read_format = event->attr.read_format;
5544 u64 values[4];
5545 int n = 0;
5546
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005547 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005548 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005549 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005550 atomic64_read(&event->child_total_time_enabled);
5551 }
5552 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005553 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005554 atomic64_read(&event->child_total_time_running);
5555 }
5556 if (read_format & PERF_FORMAT_ID)
5557 values[n++] = primary_event_id(event);
5558
Frederic Weisbecker76369132011-05-19 19:55:04 +02005559 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005560}
5561
5562/*
5563 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5564 */
5565static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005566 struct perf_event *event,
5567 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005568{
5569 struct perf_event *leader = event->group_leader, *sub;
5570 u64 read_format = event->attr.read_format;
5571 u64 values[5];
5572 int n = 0;
5573
5574 values[n++] = 1 + leader->nr_siblings;
5575
5576 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005577 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005578
5579 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005580 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005581
5582 if (leader != event)
5583 leader->pmu->read(leader);
5584
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005585 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005586 if (read_format & PERF_FORMAT_ID)
5587 values[n++] = primary_event_id(leader);
5588
Frederic Weisbecker76369132011-05-19 19:55:04 +02005589 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005590
5591 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5592 n = 0;
5593
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005594 if ((sub != event) &&
5595 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005596 sub->pmu->read(sub);
5597
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005598 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005599 if (read_format & PERF_FORMAT_ID)
5600 values[n++] = primary_event_id(sub);
5601
Frederic Weisbecker76369132011-05-19 19:55:04 +02005602 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005603 }
5604}
5605
Stephane Eranianeed01522010-10-26 16:08:01 +02005606#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5607 PERF_FORMAT_TOTAL_TIME_RUNNING)
5608
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005609static void perf_output_read(struct perf_output_handle *handle,
5610 struct perf_event *event)
5611{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005612 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005613 u64 read_format = event->attr.read_format;
5614
5615 /*
5616 * compute total_time_enabled, total_time_running
5617 * based on snapshot values taken when the event
5618 * was last scheduled in.
5619 *
5620 * we cannot simply called update_context_time()
5621 * because of locking issue as we are called in
5622 * NMI context
5623 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005624 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005625 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005626
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005627 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005628 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005629 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005630 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005631}
5632
5633void perf_output_sample(struct perf_output_handle *handle,
5634 struct perf_event_header *header,
5635 struct perf_sample_data *data,
5636 struct perf_event *event)
5637{
5638 u64 sample_type = data->type;
5639
5640 perf_output_put(handle, *header);
5641
Adrian Hunterff3d5272013-08-27 11:23:07 +03005642 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5643 perf_output_put(handle, data->id);
5644
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005645 if (sample_type & PERF_SAMPLE_IP)
5646 perf_output_put(handle, data->ip);
5647
5648 if (sample_type & PERF_SAMPLE_TID)
5649 perf_output_put(handle, data->tid_entry);
5650
5651 if (sample_type & PERF_SAMPLE_TIME)
5652 perf_output_put(handle, data->time);
5653
5654 if (sample_type & PERF_SAMPLE_ADDR)
5655 perf_output_put(handle, data->addr);
5656
5657 if (sample_type & PERF_SAMPLE_ID)
5658 perf_output_put(handle, data->id);
5659
5660 if (sample_type & PERF_SAMPLE_STREAM_ID)
5661 perf_output_put(handle, data->stream_id);
5662
5663 if (sample_type & PERF_SAMPLE_CPU)
5664 perf_output_put(handle, data->cpu_entry);
5665
5666 if (sample_type & PERF_SAMPLE_PERIOD)
5667 perf_output_put(handle, data->period);
5668
5669 if (sample_type & PERF_SAMPLE_READ)
5670 perf_output_read(handle, event);
5671
5672 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5673 if (data->callchain) {
5674 int size = 1;
5675
5676 if (data->callchain)
5677 size += data->callchain->nr;
5678
5679 size *= sizeof(u64);
5680
Frederic Weisbecker76369132011-05-19 19:55:04 +02005681 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682 } else {
5683 u64 nr = 0;
5684 perf_output_put(handle, nr);
5685 }
5686 }
5687
5688 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005689 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005690
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005691 if (raw) {
5692 struct perf_raw_frag *frag = &raw->frag;
5693
5694 perf_output_put(handle, raw->size);
5695 do {
5696 if (frag->copy) {
5697 __output_custom(handle, frag->copy,
5698 frag->data, frag->size);
5699 } else {
5700 __output_copy(handle, frag->data,
5701 frag->size);
5702 }
5703 if (perf_raw_frag_last(frag))
5704 break;
5705 frag = frag->next;
5706 } while (1);
5707 if (frag->pad)
5708 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005709 } else {
5710 struct {
5711 u32 size;
5712 u32 data;
5713 } raw = {
5714 .size = sizeof(u32),
5715 .data = 0,
5716 };
5717 perf_output_put(handle, raw);
5718 }
5719 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005720
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005721 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5722 if (data->br_stack) {
5723 size_t size;
5724
5725 size = data->br_stack->nr
5726 * sizeof(struct perf_branch_entry);
5727
5728 perf_output_put(handle, data->br_stack->nr);
5729 perf_output_copy(handle, data->br_stack->entries, size);
5730 } else {
5731 /*
5732 * we always store at least the value of nr
5733 */
5734 u64 nr = 0;
5735 perf_output_put(handle, nr);
5736 }
5737 }
Jiri Olsa40189942012-08-07 15:20:37 +02005738
5739 if (sample_type & PERF_SAMPLE_REGS_USER) {
5740 u64 abi = data->regs_user.abi;
5741
5742 /*
5743 * If there are no regs to dump, notice it through
5744 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5745 */
5746 perf_output_put(handle, abi);
5747
5748 if (abi) {
5749 u64 mask = event->attr.sample_regs_user;
5750 perf_output_sample_regs(handle,
5751 data->regs_user.regs,
5752 mask);
5753 }
5754 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005755
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005756 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005757 perf_output_sample_ustack(handle,
5758 data->stack_user_size,
5759 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005760 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005761
5762 if (sample_type & PERF_SAMPLE_WEIGHT)
5763 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005764
5765 if (sample_type & PERF_SAMPLE_DATA_SRC)
5766 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005767
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005768 if (sample_type & PERF_SAMPLE_TRANSACTION)
5769 perf_output_put(handle, data->txn);
5770
Stephane Eranian60e23642014-09-24 13:48:37 +02005771 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5772 u64 abi = data->regs_intr.abi;
5773 /*
5774 * If there are no regs to dump, notice it through
5775 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5776 */
5777 perf_output_put(handle, abi);
5778
5779 if (abi) {
5780 u64 mask = event->attr.sample_regs_intr;
5781
5782 perf_output_sample_regs(handle,
5783 data->regs_intr.regs,
5784 mask);
5785 }
5786 }
5787
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005788 if (!event->attr.watermark) {
5789 int wakeup_events = event->attr.wakeup_events;
5790
5791 if (wakeup_events) {
5792 struct ring_buffer *rb = handle->rb;
5793 int events = local_inc_return(&rb->events);
5794
5795 if (events >= wakeup_events) {
5796 local_sub(wakeup_events, &rb->events);
5797 local_inc(&rb->wakeup);
5798 }
5799 }
5800 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005801}
5802
5803void perf_prepare_sample(struct perf_event_header *header,
5804 struct perf_sample_data *data,
5805 struct perf_event *event,
5806 struct pt_regs *regs)
5807{
5808 u64 sample_type = event->attr.sample_type;
5809
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005810 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005811 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005812
5813 header->misc = 0;
5814 header->misc |= perf_misc_flags(regs);
5815
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005816 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005817
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005818 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005819 data->ip = perf_instruction_pointer(regs);
5820
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005821 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5822 int size = 1;
5823
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005824 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005825
5826 if (data->callchain)
5827 size += data->callchain->nr;
5828
5829 header->size += size * sizeof(u64);
5830 }
5831
5832 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005833 struct perf_raw_record *raw = data->raw;
5834 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005835
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005836 if (raw) {
5837 struct perf_raw_frag *frag = &raw->frag;
5838 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005839
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005840 do {
5841 sum += frag->size;
5842 if (perf_raw_frag_last(frag))
5843 break;
5844 frag = frag->next;
5845 } while (1);
5846
5847 size = round_up(sum + sizeof(u32), sizeof(u64));
5848 raw->size = size - sizeof(u32);
5849 frag->pad = raw->size - sum;
5850 } else {
5851 size = sizeof(u64);
5852 }
5853
5854 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005855 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005856
5857 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5858 int size = sizeof(u64); /* nr */
5859 if (data->br_stack) {
5860 size += data->br_stack->nr
5861 * sizeof(struct perf_branch_entry);
5862 }
5863 header->size += size;
5864 }
Jiri Olsa40189942012-08-07 15:20:37 +02005865
Peter Zijlstra25657112014-09-24 13:48:42 +02005866 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005867 perf_sample_regs_user(&data->regs_user, regs,
5868 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005869
Jiri Olsa40189942012-08-07 15:20:37 +02005870 if (sample_type & PERF_SAMPLE_REGS_USER) {
5871 /* regs dump ABI info */
5872 int size = sizeof(u64);
5873
Jiri Olsa40189942012-08-07 15:20:37 +02005874 if (data->regs_user.regs) {
5875 u64 mask = event->attr.sample_regs_user;
5876 size += hweight64(mask) * sizeof(u64);
5877 }
5878
5879 header->size += size;
5880 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005881
5882 if (sample_type & PERF_SAMPLE_STACK_USER) {
5883 /*
5884 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5885 * processed as the last one or have additional check added
5886 * in case new sample type is added, because we could eat
5887 * up the rest of the sample size.
5888 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005889 u16 stack_size = event->attr.sample_stack_user;
5890 u16 size = sizeof(u64);
5891
Jiri Olsac5ebced2012-08-07 15:20:40 +02005892 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005893 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005894
5895 /*
5896 * If there is something to dump, add space for the dump
5897 * itself and for the field that tells the dynamic size,
5898 * which is how many have been actually dumped.
5899 */
5900 if (stack_size)
5901 size += sizeof(u64) + stack_size;
5902
5903 data->stack_user_size = stack_size;
5904 header->size += size;
5905 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005906
5907 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5908 /* regs dump ABI info */
5909 int size = sizeof(u64);
5910
5911 perf_sample_regs_intr(&data->regs_intr, regs);
5912
5913 if (data->regs_intr.regs) {
5914 u64 mask = event->attr.sample_regs_intr;
5915
5916 size += hweight64(mask) * sizeof(u64);
5917 }
5918
5919 header->size += size;
5920 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005921}
5922
Wang Nan9ecda412016-04-05 14:11:18 +00005923static void __always_inline
5924__perf_event_output(struct perf_event *event,
5925 struct perf_sample_data *data,
5926 struct pt_regs *regs,
5927 int (*output_begin)(struct perf_output_handle *,
5928 struct perf_event *,
5929 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005930{
5931 struct perf_output_handle handle;
5932 struct perf_event_header header;
5933
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005934 /* protect the callchain buffers */
5935 rcu_read_lock();
5936
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005937 perf_prepare_sample(&header, data, event, regs);
5938
Wang Nan9ecda412016-04-05 14:11:18 +00005939 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005940 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005941
5942 perf_output_sample(&handle, &header, data, event);
5943
5944 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005945
5946exit:
5947 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005948}
5949
Wang Nan9ecda412016-04-05 14:11:18 +00005950void
5951perf_event_output_forward(struct perf_event *event,
5952 struct perf_sample_data *data,
5953 struct pt_regs *regs)
5954{
5955 __perf_event_output(event, data, regs, perf_output_begin_forward);
5956}
5957
5958void
5959perf_event_output_backward(struct perf_event *event,
5960 struct perf_sample_data *data,
5961 struct pt_regs *regs)
5962{
5963 __perf_event_output(event, data, regs, perf_output_begin_backward);
5964}
5965
5966void
5967perf_event_output(struct perf_event *event,
5968 struct perf_sample_data *data,
5969 struct pt_regs *regs)
5970{
5971 __perf_event_output(event, data, regs, perf_output_begin);
5972}
5973
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005974/*
5975 * read event_id
5976 */
5977
5978struct perf_read_event {
5979 struct perf_event_header header;
5980
5981 u32 pid;
5982 u32 tid;
5983};
5984
5985static void
5986perf_event_read_event(struct perf_event *event,
5987 struct task_struct *task)
5988{
5989 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005990 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005991 struct perf_read_event read_event = {
5992 .header = {
5993 .type = PERF_RECORD_READ,
5994 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005995 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005996 },
5997 .pid = perf_event_pid(event, task),
5998 .tid = perf_event_tid(event, task),
5999 };
6000 int ret;
6001
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006002 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006003 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006004 if (ret)
6005 return;
6006
6007 perf_output_put(&handle, read_event);
6008 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006009 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006010
6011 perf_output_end(&handle);
6012}
6013
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006014typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006015
6016static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006017perf_iterate_ctx(struct perf_event_context *ctx,
6018 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006019 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02006020{
6021 struct perf_event *event;
6022
6023 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006024 if (!all) {
6025 if (event->state < PERF_EVENT_STATE_INACTIVE)
6026 continue;
6027 if (!event_filter_match(event))
6028 continue;
6029 }
6030
Jiri Olsa67516842013-07-09 18:56:31 +02006031 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006032 }
6033}
6034
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006035static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07006036{
6037 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
6038 struct perf_event *event;
6039
6040 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02006041 /*
6042 * Skip events that are not fully formed yet; ensure that
6043 * if we observe event->ctx, both event and ctx will be
6044 * complete enough. See perf_install_in_context().
6045 */
6046 if (!smp_load_acquire(&event->ctx))
6047 continue;
6048
Kan Liangf2fb6be2016-03-23 11:24:37 -07006049 if (event->state < PERF_EVENT_STATE_INACTIVE)
6050 continue;
6051 if (!event_filter_match(event))
6052 continue;
6053 output(event, data);
6054 }
6055}
6056
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006057/*
6058 * Iterate all events that need to receive side-band events.
6059 *
6060 * For new callers; ensure that account_pmu_sb_event() includes
6061 * your event, otherwise it might not get delivered.
6062 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006063static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006064perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006065 struct perf_event_context *task_ctx)
6066{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006067 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02006068 int ctxn;
6069
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006070 rcu_read_lock();
6071 preempt_disable();
6072
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006073 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006074 * If we have task_ctx != NULL we only notify the task context itself.
6075 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006076 * context.
6077 */
6078 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006079 perf_iterate_ctx(task_ctx, output, data, false);
6080 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006081 }
6082
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006083 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07006084
6085 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02006086 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6087 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006088 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006089 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006090done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07006091 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02006092 rcu_read_unlock();
6093}
6094
Alexander Shishkin375637b2016-04-27 18:44:46 +03006095/*
6096 * Clear all file-based filters at exec, they'll have to be
6097 * re-instated when/if these objects are mmapped again.
6098 */
6099static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
6100{
6101 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6102 struct perf_addr_filter *filter;
6103 unsigned int restart = 0, count = 0;
6104 unsigned long flags;
6105
6106 if (!has_addr_filter(event))
6107 return;
6108
6109 raw_spin_lock_irqsave(&ifh->lock, flags);
6110 list_for_each_entry(filter, &ifh->list, entry) {
6111 if (filter->inode) {
6112 event->addr_filters_offs[count] = 0;
6113 restart++;
6114 }
6115
6116 count++;
6117 }
6118
6119 if (restart)
6120 event->addr_filters_gen++;
6121 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6122
6123 if (restart)
6124 perf_event_restart(event);
6125}
6126
6127void perf_event_exec(void)
6128{
6129 struct perf_event_context *ctx;
6130 int ctxn;
6131
6132 rcu_read_lock();
6133 for_each_task_context_nr(ctxn) {
6134 ctx = current->perf_event_ctxp[ctxn];
6135 if (!ctx)
6136 continue;
6137
6138 perf_event_enable_on_exec(ctxn);
6139
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006140 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03006141 true);
6142 }
6143 rcu_read_unlock();
6144}
6145
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006146struct remote_output {
6147 struct ring_buffer *rb;
6148 int err;
6149};
6150
6151static void __perf_event_output_stop(struct perf_event *event, void *data)
6152{
6153 struct perf_event *parent = event->parent;
6154 struct remote_output *ro = data;
6155 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03006156 struct stop_event_data sd = {
6157 .event = event,
6158 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006159
6160 if (!has_aux(event))
6161 return;
6162
6163 if (!parent)
6164 parent = event;
6165
6166 /*
6167 * In case of inheritance, it will be the parent that links to the
6168 * ring-buffer, but it will be the child that's actually using it:
6169 */
6170 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006171 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006172}
6173
6174static int __perf_pmu_output_stop(void *info)
6175{
6176 struct perf_event *event = info;
6177 struct pmu *pmu = event->pmu;
6178 struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
6179 struct remote_output ro = {
6180 .rb = event->rb,
6181 };
6182
6183 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006184 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006185 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006186 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006187 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006188 rcu_read_unlock();
6189
6190 return ro.err;
6191}
6192
6193static void perf_pmu_output_stop(struct perf_event *event)
6194{
6195 struct perf_event *iter;
6196 int err, cpu;
6197
6198restart:
6199 rcu_read_lock();
6200 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6201 /*
6202 * For per-CPU events, we need to make sure that neither they
6203 * nor their children are running; for cpu==-1 events it's
6204 * sufficient to stop the event itself if it's active, since
6205 * it can't have children.
6206 */
6207 cpu = iter->cpu;
6208 if (cpu == -1)
6209 cpu = READ_ONCE(iter->oncpu);
6210
6211 if (cpu == -1)
6212 continue;
6213
6214 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6215 if (err == -EAGAIN) {
6216 rcu_read_unlock();
6217 goto restart;
6218 }
6219 }
6220 rcu_read_unlock();
6221}
6222
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006223/*
6224 * task tracking -- fork/exit
6225 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006226 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006227 */
6228
6229struct perf_task_event {
6230 struct task_struct *task;
6231 struct perf_event_context *task_ctx;
6232
6233 struct {
6234 struct perf_event_header header;
6235
6236 u32 pid;
6237 u32 ppid;
6238 u32 tid;
6239 u32 ptid;
6240 u64 time;
6241 } event_id;
6242};
6243
Jiri Olsa67516842013-07-09 18:56:31 +02006244static int perf_event_task_match(struct perf_event *event)
6245{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006246 return event->attr.comm || event->attr.mmap ||
6247 event->attr.mmap2 || event->attr.mmap_data ||
6248 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006249}
6250
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006251static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006252 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006253{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006254 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006255 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006256 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006257 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006258 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006259
Jiri Olsa67516842013-07-09 18:56:31 +02006260 if (!perf_event_task_match(event))
6261 return;
6262
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006263 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006264
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006265 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006266 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006267 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006268 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006269
6270 task_event->event_id.pid = perf_event_pid(event, task);
6271 task_event->event_id.ppid = perf_event_pid(event, current);
6272
6273 task_event->event_id.tid = perf_event_tid(event, task);
6274 task_event->event_id.ptid = perf_event_tid(event, current);
6275
Peter Zijlstra34f43922015-02-20 14:05:38 +01006276 task_event->event_id.time = perf_event_clock(event);
6277
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006278 perf_output_put(&handle, task_event->event_id);
6279
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006280 perf_event__output_id_sample(event, &handle, &sample);
6281
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006282 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006283out:
6284 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006285}
6286
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006287static void perf_event_task(struct task_struct *task,
6288 struct perf_event_context *task_ctx,
6289 int new)
6290{
6291 struct perf_task_event task_event;
6292
6293 if (!atomic_read(&nr_comm_events) &&
6294 !atomic_read(&nr_mmap_events) &&
6295 !atomic_read(&nr_task_events))
6296 return;
6297
6298 task_event = (struct perf_task_event){
6299 .task = task,
6300 .task_ctx = task_ctx,
6301 .event_id = {
6302 .header = {
6303 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6304 .misc = 0,
6305 .size = sizeof(task_event.event_id),
6306 },
6307 /* .pid */
6308 /* .ppid */
6309 /* .tid */
6310 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006311 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006312 },
6313 };
6314
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006315 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006316 &task_event,
6317 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006318}
6319
6320void perf_event_fork(struct task_struct *task)
6321{
6322 perf_event_task(task, NULL, 1);
6323}
6324
6325/*
6326 * comm tracking
6327 */
6328
6329struct perf_comm_event {
6330 struct task_struct *task;
6331 char *comm;
6332 int comm_size;
6333
6334 struct {
6335 struct perf_event_header header;
6336
6337 u32 pid;
6338 u32 tid;
6339 } event_id;
6340};
6341
Jiri Olsa67516842013-07-09 18:56:31 +02006342static int perf_event_comm_match(struct perf_event *event)
6343{
6344 return event->attr.comm;
6345}
6346
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006347static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006348 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006349{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006350 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006351 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006352 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006353 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006354 int ret;
6355
Jiri Olsa67516842013-07-09 18:56:31 +02006356 if (!perf_event_comm_match(event))
6357 return;
6358
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006359 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6360 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006361 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006362
6363 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006364 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006365
6366 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6367 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6368
6369 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006370 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006371 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006372
6373 perf_event__output_id_sample(event, &handle, &sample);
6374
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006375 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006376out:
6377 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006378}
6379
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006380static void perf_event_comm_event(struct perf_comm_event *comm_event)
6381{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006382 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006383 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006384
6385 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006386 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006387 size = ALIGN(strlen(comm)+1, sizeof(u64));
6388
6389 comm_event->comm = comm;
6390 comm_event->comm_size = size;
6391
6392 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006393
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006394 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006395 comm_event,
6396 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006397}
6398
Adrian Hunter82b89772014-05-28 11:45:04 +03006399void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006400{
6401 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006402
6403 if (!atomic_read(&nr_comm_events))
6404 return;
6405
6406 comm_event = (struct perf_comm_event){
6407 .task = task,
6408 /* .comm */
6409 /* .comm_size */
6410 .event_id = {
6411 .header = {
6412 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006413 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006414 /* .size */
6415 },
6416 /* .pid */
6417 /* .tid */
6418 },
6419 };
6420
6421 perf_event_comm_event(&comm_event);
6422}
6423
6424/*
6425 * mmap tracking
6426 */
6427
6428struct perf_mmap_event {
6429 struct vm_area_struct *vma;
6430
6431 const char *file_name;
6432 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006433 int maj, min;
6434 u64 ino;
6435 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006436 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006437
6438 struct {
6439 struct perf_event_header header;
6440
6441 u32 pid;
6442 u32 tid;
6443 u64 start;
6444 u64 len;
6445 u64 pgoff;
6446 } event_id;
6447};
6448
Jiri Olsa67516842013-07-09 18:56:31 +02006449static int perf_event_mmap_match(struct perf_event *event,
6450 void *data)
6451{
6452 struct perf_mmap_event *mmap_event = data;
6453 struct vm_area_struct *vma = mmap_event->vma;
6454 int executable = vma->vm_flags & VM_EXEC;
6455
6456 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006457 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006458}
6459
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006460static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006461 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006462{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006463 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006464 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006465 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006466 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006467 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006468
Jiri Olsa67516842013-07-09 18:56:31 +02006469 if (!perf_event_mmap_match(event, data))
6470 return;
6471
Stephane Eranian13d7a242013-08-21 12:10:24 +02006472 if (event->attr.mmap2) {
6473 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6474 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6475 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6476 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006477 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006478 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6479 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006480 }
6481
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006482 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6483 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006484 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006485 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006486 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006487
6488 mmap_event->event_id.pid = perf_event_pid(event, current);
6489 mmap_event->event_id.tid = perf_event_tid(event, current);
6490
6491 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006492
6493 if (event->attr.mmap2) {
6494 perf_output_put(&handle, mmap_event->maj);
6495 perf_output_put(&handle, mmap_event->min);
6496 perf_output_put(&handle, mmap_event->ino);
6497 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006498 perf_output_put(&handle, mmap_event->prot);
6499 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006500 }
6501
Frederic Weisbecker76369132011-05-19 19:55:04 +02006502 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006503 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006504
6505 perf_event__output_id_sample(event, &handle, &sample);
6506
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006507 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006508out:
6509 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006510}
6511
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006512static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6513{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006514 struct vm_area_struct *vma = mmap_event->vma;
6515 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006516 int maj = 0, min = 0;
6517 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006518 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006519 unsigned int size;
6520 char tmp[16];
6521 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006522 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006523
6524 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006525 struct inode *inode;
6526 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006527
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006528 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006529 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006530 name = "//enomem";
6531 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006532 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006534 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006535 * need to add enough zero bytes after the string to handle
6536 * the 64bit alignment we do later.
6537 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006538 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006539 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006540 name = "//toolong";
6541 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006542 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006543 inode = file_inode(vma->vm_file);
6544 dev = inode->i_sb->s_dev;
6545 ino = inode->i_ino;
6546 gen = inode->i_generation;
6547 maj = MAJOR(dev);
6548 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006549
6550 if (vma->vm_flags & VM_READ)
6551 prot |= PROT_READ;
6552 if (vma->vm_flags & VM_WRITE)
6553 prot |= PROT_WRITE;
6554 if (vma->vm_flags & VM_EXEC)
6555 prot |= PROT_EXEC;
6556
6557 if (vma->vm_flags & VM_MAYSHARE)
6558 flags = MAP_SHARED;
6559 else
6560 flags = MAP_PRIVATE;
6561
6562 if (vma->vm_flags & VM_DENYWRITE)
6563 flags |= MAP_DENYWRITE;
6564 if (vma->vm_flags & VM_MAYEXEC)
6565 flags |= MAP_EXECUTABLE;
6566 if (vma->vm_flags & VM_LOCKED)
6567 flags |= MAP_LOCKED;
6568 if (vma->vm_flags & VM_HUGETLB)
6569 flags |= MAP_HUGETLB;
6570
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006571 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006572 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006573 if (vma->vm_ops && vma->vm_ops->name) {
6574 name = (char *) vma->vm_ops->name(vma);
6575 if (name)
6576 goto cpy_name;
6577 }
6578
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006579 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006580 if (name)
6581 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006582
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006583 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006584 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006585 name = "[heap]";
6586 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006587 }
6588 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006589 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006590 name = "[stack]";
6591 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006592 }
6593
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006594 name = "//anon";
6595 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006596 }
6597
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006598cpy_name:
6599 strlcpy(tmp, name, sizeof(tmp));
6600 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006601got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006602 /*
6603 * Since our buffer works in 8 byte units we need to align our string
6604 * size to a multiple of 8. However, we must guarantee the tail end is
6605 * zero'd out to avoid leaking random bits to userspace.
6606 */
6607 size = strlen(name)+1;
6608 while (!IS_ALIGNED(size, sizeof(u64)))
6609 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006610
6611 mmap_event->file_name = name;
6612 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006613 mmap_event->maj = maj;
6614 mmap_event->min = min;
6615 mmap_event->ino = ino;
6616 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006617 mmap_event->prot = prot;
6618 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006619
Stephane Eranian2fe85422013-01-24 16:10:39 +01006620 if (!(vma->vm_flags & VM_EXEC))
6621 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6622
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006623 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6624
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006625 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006626 mmap_event,
6627 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006628
6629 kfree(buf);
6630}
6631
Alexander Shishkin375637b2016-04-27 18:44:46 +03006632/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03006633 * Check whether inode and address range match filter criteria.
6634 */
6635static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6636 struct file *file, unsigned long offset,
6637 unsigned long size)
6638{
6639 if (filter->inode != file->f_inode)
6640 return false;
6641
6642 if (filter->offset > offset + size)
6643 return false;
6644
6645 if (filter->offset + filter->size < offset)
6646 return false;
6647
6648 return true;
6649}
6650
6651static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6652{
6653 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6654 struct vm_area_struct *vma = data;
6655 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6656 struct file *file = vma->vm_file;
6657 struct perf_addr_filter *filter;
6658 unsigned int restart = 0, count = 0;
6659
6660 if (!has_addr_filter(event))
6661 return;
6662
6663 if (!file)
6664 return;
6665
6666 raw_spin_lock_irqsave(&ifh->lock, flags);
6667 list_for_each_entry(filter, &ifh->list, entry) {
6668 if (perf_addr_filter_match(filter, file, off,
6669 vma->vm_end - vma->vm_start)) {
6670 event->addr_filters_offs[count] = vma->vm_start;
6671 restart++;
6672 }
6673
6674 count++;
6675 }
6676
6677 if (restart)
6678 event->addr_filters_gen++;
6679 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6680
6681 if (restart)
6682 perf_event_restart(event);
6683}
6684
6685/*
6686 * Adjust all task's events' filters to the new vma
6687 */
6688static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6689{
6690 struct perf_event_context *ctx;
6691 int ctxn;
6692
Mathieu Poirier12b40a22016-07-18 10:43:06 -06006693 /*
6694 * Data tracing isn't supported yet and as such there is no need
6695 * to keep track of anything that isn't related to executable code:
6696 */
6697 if (!(vma->vm_flags & VM_EXEC))
6698 return;
6699
Alexander Shishkin375637b2016-04-27 18:44:46 +03006700 rcu_read_lock();
6701 for_each_task_context_nr(ctxn) {
6702 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6703 if (!ctx)
6704 continue;
6705
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006706 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006707 }
6708 rcu_read_unlock();
6709}
6710
Eric B Munson3af9e852010-05-18 15:30:49 +01006711void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006712{
6713 struct perf_mmap_event mmap_event;
6714
6715 if (!atomic_read(&nr_mmap_events))
6716 return;
6717
6718 mmap_event = (struct perf_mmap_event){
6719 .vma = vma,
6720 /* .file_name */
6721 /* .file_size */
6722 .event_id = {
6723 .header = {
6724 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006725 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006726 /* .size */
6727 },
6728 /* .pid */
6729 /* .tid */
6730 .start = vma->vm_start,
6731 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006732 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006733 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006734 /* .maj (attr_mmap2 only) */
6735 /* .min (attr_mmap2 only) */
6736 /* .ino (attr_mmap2 only) */
6737 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006738 /* .prot (attr_mmap2 only) */
6739 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006740 };
6741
Alexander Shishkin375637b2016-04-27 18:44:46 +03006742 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006743 perf_event_mmap_event(&mmap_event);
6744}
6745
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006746void perf_event_aux_event(struct perf_event *event, unsigned long head,
6747 unsigned long size, u64 flags)
6748{
6749 struct perf_output_handle handle;
6750 struct perf_sample_data sample;
6751 struct perf_aux_event {
6752 struct perf_event_header header;
6753 u64 offset;
6754 u64 size;
6755 u64 flags;
6756 } rec = {
6757 .header = {
6758 .type = PERF_RECORD_AUX,
6759 .misc = 0,
6760 .size = sizeof(rec),
6761 },
6762 .offset = head,
6763 .size = size,
6764 .flags = flags,
6765 };
6766 int ret;
6767
6768 perf_event_header__init_id(&rec.header, &sample, event);
6769 ret = perf_output_begin(&handle, event, rec.header.size);
6770
6771 if (ret)
6772 return;
6773
6774 perf_output_put(&handle, rec);
6775 perf_event__output_id_sample(event, &handle, &sample);
6776
6777 perf_output_end(&handle);
6778}
6779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006780/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006781 * Lost/dropped samples logging
6782 */
6783void perf_log_lost_samples(struct perf_event *event, u64 lost)
6784{
6785 struct perf_output_handle handle;
6786 struct perf_sample_data sample;
6787 int ret;
6788
6789 struct {
6790 struct perf_event_header header;
6791 u64 lost;
6792 } lost_samples_event = {
6793 .header = {
6794 .type = PERF_RECORD_LOST_SAMPLES,
6795 .misc = 0,
6796 .size = sizeof(lost_samples_event),
6797 },
6798 .lost = lost,
6799 };
6800
6801 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6802
6803 ret = perf_output_begin(&handle, event,
6804 lost_samples_event.header.size);
6805 if (ret)
6806 return;
6807
6808 perf_output_put(&handle, lost_samples_event);
6809 perf_event__output_id_sample(event, &handle, &sample);
6810 perf_output_end(&handle);
6811}
6812
6813/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006814 * context_switch tracking
6815 */
6816
6817struct perf_switch_event {
6818 struct task_struct *task;
6819 struct task_struct *next_prev;
6820
6821 struct {
6822 struct perf_event_header header;
6823 u32 next_prev_pid;
6824 u32 next_prev_tid;
6825 } event_id;
6826};
6827
6828static int perf_event_switch_match(struct perf_event *event)
6829{
6830 return event->attr.context_switch;
6831}
6832
6833static void perf_event_switch_output(struct perf_event *event, void *data)
6834{
6835 struct perf_switch_event *se = data;
6836 struct perf_output_handle handle;
6837 struct perf_sample_data sample;
6838 int ret;
6839
6840 if (!perf_event_switch_match(event))
6841 return;
6842
6843 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6844 if (event->ctx->task) {
6845 se->event_id.header.type = PERF_RECORD_SWITCH;
6846 se->event_id.header.size = sizeof(se->event_id.header);
6847 } else {
6848 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6849 se->event_id.header.size = sizeof(se->event_id);
6850 se->event_id.next_prev_pid =
6851 perf_event_pid(event, se->next_prev);
6852 se->event_id.next_prev_tid =
6853 perf_event_tid(event, se->next_prev);
6854 }
6855
6856 perf_event_header__init_id(&se->event_id.header, &sample, event);
6857
6858 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6859 if (ret)
6860 return;
6861
6862 if (event->ctx->task)
6863 perf_output_put(&handle, se->event_id.header);
6864 else
6865 perf_output_put(&handle, se->event_id);
6866
6867 perf_event__output_id_sample(event, &handle, &sample);
6868
6869 perf_output_end(&handle);
6870}
6871
6872static void perf_event_switch(struct task_struct *task,
6873 struct task_struct *next_prev, bool sched_in)
6874{
6875 struct perf_switch_event switch_event;
6876
6877 /* N.B. caller checks nr_switch_events != 0 */
6878
6879 switch_event = (struct perf_switch_event){
6880 .task = task,
6881 .next_prev = next_prev,
6882 .event_id = {
6883 .header = {
6884 /* .type */
6885 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6886 /* .size */
6887 },
6888 /* .next_prev_pid */
6889 /* .next_prev_tid */
6890 },
6891 };
6892
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006893 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03006894 &switch_event,
6895 NULL);
6896}
6897
6898/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006899 * IRQ throttle logging
6900 */
6901
6902static void perf_log_throttle(struct perf_event *event, int enable)
6903{
6904 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006905 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006906 int ret;
6907
6908 struct {
6909 struct perf_event_header header;
6910 u64 time;
6911 u64 id;
6912 u64 stream_id;
6913 } throttle_event = {
6914 .header = {
6915 .type = PERF_RECORD_THROTTLE,
6916 .misc = 0,
6917 .size = sizeof(throttle_event),
6918 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006919 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006920 .id = primary_event_id(event),
6921 .stream_id = event->id,
6922 };
6923
6924 if (enable)
6925 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6926
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006927 perf_event_header__init_id(&throttle_event.header, &sample, event);
6928
6929 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006930 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006931 if (ret)
6932 return;
6933
6934 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006935 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006936 perf_output_end(&handle);
6937}
6938
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006939static void perf_log_itrace_start(struct perf_event *event)
6940{
6941 struct perf_output_handle handle;
6942 struct perf_sample_data sample;
6943 struct perf_aux_event {
6944 struct perf_event_header header;
6945 u32 pid;
6946 u32 tid;
6947 } rec;
6948 int ret;
6949
6950 if (event->parent)
6951 event = event->parent;
6952
6953 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6954 event->hw.itrace_started)
6955 return;
6956
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006957 rec.header.type = PERF_RECORD_ITRACE_START;
6958 rec.header.misc = 0;
6959 rec.header.size = sizeof(rec);
6960 rec.pid = perf_event_pid(event, current);
6961 rec.tid = perf_event_tid(event, current);
6962
6963 perf_event_header__init_id(&rec.header, &sample, event);
6964 ret = perf_output_begin(&handle, event, rec.header.size);
6965
6966 if (ret)
6967 return;
6968
6969 perf_output_put(&handle, rec);
6970 perf_event__output_id_sample(event, &handle, &sample);
6971
6972 perf_output_end(&handle);
6973}
6974
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006975/*
6976 * Generic event overflow handling, sampling.
6977 */
6978
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006979static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006980 int throttle, struct perf_sample_data *data,
6981 struct pt_regs *regs)
6982{
6983 int events = atomic_read(&event->event_limit);
6984 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006985 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006986 int ret = 0;
6987
Peter Zijlstra96398822010-11-24 18:55:29 +01006988 /*
6989 * Non-sampling counters might still use the PMI to fold short
6990 * hardware counters, ignore those.
6991 */
6992 if (unlikely(!is_sampling_event(event)))
6993 return 0;
6994
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006995 seq = __this_cpu_read(perf_throttled_seq);
6996 if (seq != hwc->interrupts_seq) {
6997 hwc->interrupts_seq = seq;
6998 hwc->interrupts = 1;
6999 } else {
7000 hwc->interrupts++;
7001 if (unlikely(throttle
7002 && hwc->interrupts >= max_samples_per_tick)) {
7003 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007004 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01007005 hwc->interrupts = MAX_INTERRUPTS;
7006 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007007 ret = 1;
7008 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007009 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007010
7011 if (event->attr.freq) {
7012 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01007013 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007014
Peter Zijlstraabd50712010-01-26 18:50:16 +01007015 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007016
Peter Zijlstraabd50712010-01-26 18:50:16 +01007017 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01007018 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007019 }
7020
7021 /*
7022 * XXX event_limit might not quite work as expected on inherited
7023 * events
7024 */
7025
7026 event->pending_kill = POLL_IN;
7027 if (events && atomic_dec_and_test(&event->event_limit)) {
7028 ret = 1;
7029 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007030 event->pending_disable = 1;
7031 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007032 }
7033
Wang Nan18794452016-03-28 06:41:30 +00007034 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01007035
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02007036 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007037 event->pending_wakeup = 1;
7038 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02007039 }
7040
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007041 return ret;
7042}
7043
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007044int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007045 struct perf_sample_data *data,
7046 struct pt_regs *regs)
7047{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007048 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007049}
7050
7051/*
7052 * Generic software event infrastructure
7053 */
7054
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007055struct swevent_htable {
7056 struct swevent_hlist *swevent_hlist;
7057 struct mutex hlist_mutex;
7058 int hlist_refcount;
7059
7060 /* Recursion avoidance in each contexts */
7061 int recursion[PERF_NR_CONTEXTS];
7062};
7063
7064static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7065
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007066/*
7067 * We directly increment event->count and keep a second value in
7068 * event->hw.period_left to count intervals. This period event
7069 * is kept in the range [-sample_period, 0] so that we can use the
7070 * sign as trigger.
7071 */
7072
Jiri Olsaab573842013-05-01 17:25:44 +02007073u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007074{
7075 struct hw_perf_event *hwc = &event->hw;
7076 u64 period = hwc->last_period;
7077 u64 nr, offset;
7078 s64 old, val;
7079
7080 hwc->last_period = hwc->sample_period;
7081
7082again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02007083 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007084 if (val < 0)
7085 return 0;
7086
7087 nr = div64_u64(period + val, period);
7088 offset = nr * period;
7089 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02007090 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007091 goto again;
7092
7093 return nr;
7094}
7095
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007096static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007097 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007098 struct pt_regs *regs)
7099{
7100 struct hw_perf_event *hwc = &event->hw;
7101 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007102
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007103 if (!overflow)
7104 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007105
7106 if (hwc->interrupts == MAX_INTERRUPTS)
7107 return;
7108
7109 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007110 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007111 data, regs)) {
7112 /*
7113 * We inhibit the overflow from happening when
7114 * hwc->interrupts == MAX_INTERRUPTS.
7115 */
7116 break;
7117 }
7118 throttle = 1;
7119 }
7120}
7121
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007122static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007123 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007124 struct pt_regs *regs)
7125{
7126 struct hw_perf_event *hwc = &event->hw;
7127
Peter Zijlstrae7850592010-05-21 14:43:08 +02007128 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007129
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007130 if (!regs)
7131 return;
7132
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007133 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007134 return;
7135
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03007136 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
7137 data->period = nr;
7138 return perf_swevent_overflow(event, 1, data, regs);
7139 } else
7140 data->period = event->hw.last_period;
7141
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007142 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007143 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007144
Peter Zijlstrae7850592010-05-21 14:43:08 +02007145 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007146 return;
7147
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007148 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007149}
7150
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007151static int perf_exclude_event(struct perf_event *event,
7152 struct pt_regs *regs)
7153{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007154 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01007155 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007156
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007157 if (regs) {
7158 if (event->attr.exclude_user && user_mode(regs))
7159 return 1;
7160
7161 if (event->attr.exclude_kernel && !user_mode(regs))
7162 return 1;
7163 }
7164
7165 return 0;
7166}
7167
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007168static int perf_swevent_match(struct perf_event *event,
7169 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007170 u32 event_id,
7171 struct perf_sample_data *data,
7172 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007173{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007174 if (event->attr.type != type)
7175 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007176
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007177 if (event->attr.config != event_id)
7178 return 0;
7179
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007180 if (perf_exclude_event(event, regs))
7181 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007182
7183 return 1;
7184}
7185
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007186static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007187{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007188 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007189
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007190 return hash_64(val, SWEVENT_HLIST_BITS);
7191}
7192
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007193static inline struct hlist_head *
7194__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007195{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007196 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007197
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007198 return &hlist->heads[hash];
7199}
7200
7201/* For the read side: events when they trigger */
7202static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007203find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007204{
7205 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007206
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007207 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007208 if (!hlist)
7209 return NULL;
7210
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007211 return __find_swevent_head(hlist, type, event_id);
7212}
7213
7214/* For the event head insertion and removal in the hlist */
7215static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007216find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007217{
7218 struct swevent_hlist *hlist;
7219 u32 event_id = event->attr.config;
7220 u64 type = event->attr.type;
7221
7222 /*
7223 * Event scheduling is always serialized against hlist allocation
7224 * and release. Which makes the protected version suitable here.
7225 * The context lock guarantees that.
7226 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007227 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007228 lockdep_is_held(&event->ctx->lock));
7229 if (!hlist)
7230 return NULL;
7231
7232 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007233}
7234
7235static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007236 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007237 struct perf_sample_data *data,
7238 struct pt_regs *regs)
7239{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007240 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007241 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007242 struct hlist_head *head;
7243
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007244 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007245 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007246 if (!head)
7247 goto end;
7248
Sasha Levinb67bfe02013-02-27 17:06:00 -08007249 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007250 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007251 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007252 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007253end:
7254 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007255}
7256
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007257DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7258
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007259int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007260{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007261 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007262
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007263 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007264}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007265EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007266
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007267void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007268{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007269 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007270
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007271 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007272}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007273
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007274void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007275{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007276 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007277
7278 if (WARN_ON_ONCE(!regs))
7279 return;
7280
7281 perf_sample_data_init(&data, addr, 0);
7282 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7283}
7284
7285void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7286{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007287 int rctx;
7288
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007289 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007290 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007291 if (unlikely(rctx < 0))
7292 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007293
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007294 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007295
7296 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007297fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007298 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007299}
7300
7301static void perf_swevent_read(struct perf_event *event)
7302{
7303}
7304
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007305static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007306{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007307 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007308 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007309 struct hlist_head *head;
7310
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007311 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007312 hwc->last_period = hwc->sample_period;
7313 perf_swevent_set_period(event);
7314 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007315
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007316 hwc->state = !(flags & PERF_EF_START);
7317
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007318 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007319 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007320 return -EINVAL;
7321
7322 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007323 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007324
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007325 return 0;
7326}
7327
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007328static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007329{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007330 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007331}
7332
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007333static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007334{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007335 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007336}
7337
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007338static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007339{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007340 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007341}
7342
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007343/* Deref the hlist from the update side */
7344static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007345swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007346{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007347 return rcu_dereference_protected(swhash->swevent_hlist,
7348 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007349}
7350
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007351static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007352{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007353 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007354
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007355 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007356 return;
7357
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007358 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007359 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007360}
7361
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007362static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007363{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007364 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007365
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007366 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007367
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007368 if (!--swhash->hlist_refcount)
7369 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007370
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007371 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007372}
7373
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007374static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007375{
7376 int cpu;
7377
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007378 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007379 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007380}
7381
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007382static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007383{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007384 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007385 int err = 0;
7386
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007387 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007388 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007389 struct swevent_hlist *hlist;
7390
7391 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7392 if (!hlist) {
7393 err = -ENOMEM;
7394 goto exit;
7395 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007396 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007397 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007398 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007399exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007400 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007401
7402 return err;
7403}
7404
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007405static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007406{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007407 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007408
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007409 get_online_cpus();
7410 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007411 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007412 if (err) {
7413 failed_cpu = cpu;
7414 goto fail;
7415 }
7416 }
7417 put_online_cpus();
7418
7419 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007420fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007421 for_each_possible_cpu(cpu) {
7422 if (cpu == failed_cpu)
7423 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007424 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007425 }
7426
7427 put_online_cpus();
7428 return err;
7429}
7430
Ingo Molnarc5905af2012-02-24 08:31:31 +01007431struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007432
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007433static void sw_perf_event_destroy(struct perf_event *event)
7434{
7435 u64 event_id = event->attr.config;
7436
7437 WARN_ON(event->parent);
7438
Ingo Molnarc5905af2012-02-24 08:31:31 +01007439 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007440 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007441}
7442
7443static int perf_swevent_init(struct perf_event *event)
7444{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007445 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007446
7447 if (event->attr.type != PERF_TYPE_SOFTWARE)
7448 return -ENOENT;
7449
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007450 /*
7451 * no branch sampling for software events
7452 */
7453 if (has_branch_stack(event))
7454 return -EOPNOTSUPP;
7455
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007456 switch (event_id) {
7457 case PERF_COUNT_SW_CPU_CLOCK:
7458 case PERF_COUNT_SW_TASK_CLOCK:
7459 return -ENOENT;
7460
7461 default:
7462 break;
7463 }
7464
Dan Carpenterce677832010-10-24 21:50:42 +02007465 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007466 return -ENOENT;
7467
7468 if (!event->parent) {
7469 int err;
7470
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007471 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007472 if (err)
7473 return err;
7474
Ingo Molnarc5905af2012-02-24 08:31:31 +01007475 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007476 event->destroy = sw_perf_event_destroy;
7477 }
7478
7479 return 0;
7480}
7481
7482static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007483 .task_ctx_nr = perf_sw_context,
7484
Peter Zijlstra34f43922015-02-20 14:05:38 +01007485 .capabilities = PERF_PMU_CAP_NO_NMI,
7486
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007487 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007488 .add = perf_swevent_add,
7489 .del = perf_swevent_del,
7490 .start = perf_swevent_start,
7491 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007492 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007493};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007494
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007495#ifdef CONFIG_EVENT_TRACING
7496
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007497static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007498 struct perf_sample_data *data)
7499{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007500 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007501
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007502 /* only top level events have filters set */
7503 if (event->parent)
7504 event = event->parent;
7505
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007506 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7507 return 1;
7508 return 0;
7509}
7510
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007511static int perf_tp_event_match(struct perf_event *event,
7512 struct perf_sample_data *data,
7513 struct pt_regs *regs)
7514{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007515 if (event->hw.state & PERF_HES_STOPPED)
7516 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007517 /*
7518 * All tracepoints are from kernel-space.
7519 */
7520 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007521 return 0;
7522
7523 if (!perf_tp_filter_match(event, data))
7524 return 0;
7525
7526 return 1;
7527}
7528
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07007529void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7530 struct trace_event_call *call, u64 count,
7531 struct pt_regs *regs, struct hlist_head *head,
7532 struct task_struct *task)
7533{
7534 struct bpf_prog *prog = call->prog;
7535
7536 if (prog) {
7537 *(struct pt_regs **)raw_data = regs;
7538 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7539 perf_swevent_put_recursion_context(rctx);
7540 return;
7541 }
7542 }
7543 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7544 rctx, task);
7545}
7546EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7547
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007548void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007549 struct pt_regs *regs, struct hlist_head *head, int rctx,
7550 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007551{
7552 struct perf_sample_data data;
7553 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007554
7555 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007556 .frag = {
7557 .size = entry_size,
7558 .data = record,
7559 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007560 };
7561
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007562 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007563 data.raw = &raw;
7564
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007565 perf_trace_buf_update(record, event_type);
7566
Sasha Levinb67bfe02013-02-27 17:06:00 -08007567 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007568 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007569 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007570 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007571
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007572 /*
7573 * If we got specified a target task, also iterate its context and
7574 * deliver this event there too.
7575 */
7576 if (task && task != current) {
7577 struct perf_event_context *ctx;
7578 struct trace_entry *entry = record;
7579
7580 rcu_read_lock();
7581 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7582 if (!ctx)
7583 goto unlock;
7584
7585 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7586 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7587 continue;
7588 if (event->attr.config != entry->type)
7589 continue;
7590 if (perf_tp_event_match(event, &data, regs))
7591 perf_swevent_event(event, count, &data, regs);
7592 }
7593unlock:
7594 rcu_read_unlock();
7595 }
7596
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007597 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007598}
7599EXPORT_SYMBOL_GPL(perf_tp_event);
7600
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007601static void tp_perf_event_destroy(struct perf_event *event)
7602{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007603 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007604}
7605
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007606static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007607{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007608 int err;
7609
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007610 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7611 return -ENOENT;
7612
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007613 /*
7614 * no branch sampling for tracepoint events
7615 */
7616 if (has_branch_stack(event))
7617 return -EOPNOTSUPP;
7618
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007619 err = perf_trace_init(event);
7620 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007621 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007622
7623 event->destroy = tp_perf_event_destroy;
7624
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007625 return 0;
7626}
7627
7628static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007629 .task_ctx_nr = perf_sw_context,
7630
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007631 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007632 .add = perf_trace_add,
7633 .del = perf_trace_del,
7634 .start = perf_swevent_start,
7635 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007636 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007637};
7638
7639static inline void perf_tp_register(void)
7640{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007641 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007642}
Li Zefan6fb29152009-10-15 11:21:42 +08007643
Li Zefan6fb29152009-10-15 11:21:42 +08007644static void perf_event_free_filter(struct perf_event *event)
7645{
7646 ftrace_profile_free_filter(event);
7647}
7648
Alexei Starovoitov25415172015-03-25 12:49:20 -07007649static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7650{
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007651 bool is_kprobe, is_tracepoint;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007652 struct bpf_prog *prog;
7653
7654 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7655 return -EINVAL;
7656
7657 if (event->tp_event->prog)
7658 return -EEXIST;
7659
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007660 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7661 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7662 if (!is_kprobe && !is_tracepoint)
7663 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007664 return -EINVAL;
7665
7666 prog = bpf_prog_get(prog_fd);
7667 if (IS_ERR(prog))
7668 return PTR_ERR(prog);
7669
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007670 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7671 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007672 /* valid fd, but invalid bpf program type */
7673 bpf_prog_put(prog);
7674 return -EINVAL;
7675 }
7676
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07007677 if (is_tracepoint) {
7678 int off = trace_event_get_offsets(event->tp_event);
7679
7680 if (prog->aux->max_ctx_offset > off) {
7681 bpf_prog_put(prog);
7682 return -EACCES;
7683 }
7684 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07007685 event->tp_event->prog = prog;
7686
7687 return 0;
7688}
7689
7690static void perf_event_free_bpf_prog(struct perf_event *event)
7691{
7692 struct bpf_prog *prog;
7693
7694 if (!event->tp_event)
7695 return;
7696
7697 prog = event->tp_event->prog;
7698 if (prog) {
7699 event->tp_event->prog = NULL;
Daniel Borkmann1aacde32016-06-30 17:24:43 +02007700 bpf_prog_put(prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07007701 }
7702}
7703
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007704#else
Li Zefan6fb29152009-10-15 11:21:42 +08007705
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007706static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007707{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007708}
Li Zefan6fb29152009-10-15 11:21:42 +08007709
Li Zefan6fb29152009-10-15 11:21:42 +08007710static void perf_event_free_filter(struct perf_event *event)
7711{
7712}
7713
Alexei Starovoitov25415172015-03-25 12:49:20 -07007714static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7715{
7716 return -ENOENT;
7717}
7718
7719static void perf_event_free_bpf_prog(struct perf_event *event)
7720{
7721}
Li Zefan07b139c2009-12-21 14:27:35 +08007722#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007723
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007724#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007725void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007726{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007727 struct perf_sample_data sample;
7728 struct pt_regs *regs = data;
7729
Robert Richterfd0d0002012-04-02 20:19:08 +02007730 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007731
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007732 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007733 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007734}
7735#endif
7736
Alexander Shishkin375637b2016-04-27 18:44:46 +03007737/*
7738 * Allocate a new address filter
7739 */
7740static struct perf_addr_filter *
7741perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7742{
7743 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7744 struct perf_addr_filter *filter;
7745
7746 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7747 if (!filter)
7748 return NULL;
7749
7750 INIT_LIST_HEAD(&filter->entry);
7751 list_add_tail(&filter->entry, filters);
7752
7753 return filter;
7754}
7755
7756static void free_filters_list(struct list_head *filters)
7757{
7758 struct perf_addr_filter *filter, *iter;
7759
7760 list_for_each_entry_safe(filter, iter, filters, entry) {
7761 if (filter->inode)
7762 iput(filter->inode);
7763 list_del(&filter->entry);
7764 kfree(filter);
7765 }
7766}
7767
7768/*
7769 * Free existing address filters and optionally install new ones
7770 */
7771static void perf_addr_filters_splice(struct perf_event *event,
7772 struct list_head *head)
7773{
7774 unsigned long flags;
7775 LIST_HEAD(list);
7776
7777 if (!has_addr_filter(event))
7778 return;
7779
7780 /* don't bother with children, they don't have their own filters */
7781 if (event->parent)
7782 return;
7783
7784 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
7785
7786 list_splice_init(&event->addr_filters.list, &list);
7787 if (head)
7788 list_splice(head, &event->addr_filters.list);
7789
7790 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
7791
7792 free_filters_list(&list);
7793}
7794
7795/*
7796 * Scan through mm's vmas and see if one of them matches the
7797 * @filter; if so, adjust filter's address range.
7798 * Called with mm::mmap_sem down for reading.
7799 */
7800static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
7801 struct mm_struct *mm)
7802{
7803 struct vm_area_struct *vma;
7804
7805 for (vma = mm->mmap; vma; vma = vma->vm_next) {
7806 struct file *file = vma->vm_file;
7807 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
7808 unsigned long vma_size = vma->vm_end - vma->vm_start;
7809
7810 if (!file)
7811 continue;
7812
7813 if (!perf_addr_filter_match(filter, file, off, vma_size))
7814 continue;
7815
7816 return vma->vm_start;
7817 }
7818
7819 return 0;
7820}
7821
7822/*
7823 * Update event's address range filters based on the
7824 * task's existing mappings, if any.
7825 */
7826static void perf_event_addr_filters_apply(struct perf_event *event)
7827{
7828 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7829 struct task_struct *task = READ_ONCE(event->ctx->task);
7830 struct perf_addr_filter *filter;
7831 struct mm_struct *mm = NULL;
7832 unsigned int count = 0;
7833 unsigned long flags;
7834
7835 /*
7836 * We may observe TASK_TOMBSTONE, which means that the event tear-down
7837 * will stop on the parent's child_mutex that our caller is also holding
7838 */
7839 if (task == TASK_TOMBSTONE)
7840 return;
7841
7842 mm = get_task_mm(event->ctx->task);
7843 if (!mm)
7844 goto restart;
7845
7846 down_read(&mm->mmap_sem);
7847
7848 raw_spin_lock_irqsave(&ifh->lock, flags);
7849 list_for_each_entry(filter, &ifh->list, entry) {
7850 event->addr_filters_offs[count] = 0;
7851
Mathieu Poirier99f5bc92016-07-18 10:43:07 -06007852 /*
7853 * Adjust base offset if the filter is associated to a binary
7854 * that needs to be mapped:
7855 */
7856 if (filter->inode)
Alexander Shishkin375637b2016-04-27 18:44:46 +03007857 event->addr_filters_offs[count] =
7858 perf_addr_filter_apply(filter, mm);
7859
7860 count++;
7861 }
7862
7863 event->addr_filters_gen++;
7864 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7865
7866 up_read(&mm->mmap_sem);
7867
7868 mmput(mm);
7869
7870restart:
7871 perf_event_restart(event);
7872}
7873
7874/*
7875 * Address range filtering: limiting the data to certain
7876 * instruction address ranges. Filters are ioctl()ed to us from
7877 * userspace as ascii strings.
7878 *
7879 * Filter string format:
7880 *
7881 * ACTION RANGE_SPEC
7882 * where ACTION is one of the
7883 * * "filter": limit the trace to this region
7884 * * "start": start tracing from this address
7885 * * "stop": stop tracing at this address/region;
7886 * RANGE_SPEC is
7887 * * for kernel addresses: <start address>[/<size>]
7888 * * for object files: <start address>[/<size>]@</path/to/object/file>
7889 *
7890 * if <size> is not specified, the range is treated as a single address.
7891 */
7892enum {
7893 IF_ACT_FILTER,
7894 IF_ACT_START,
7895 IF_ACT_STOP,
7896 IF_SRC_FILE,
7897 IF_SRC_KERNEL,
7898 IF_SRC_FILEADDR,
7899 IF_SRC_KERNELADDR,
7900};
7901
7902enum {
7903 IF_STATE_ACTION = 0,
7904 IF_STATE_SOURCE,
7905 IF_STATE_END,
7906};
7907
7908static const match_table_t if_tokens = {
7909 { IF_ACT_FILTER, "filter" },
7910 { IF_ACT_START, "start" },
7911 { IF_ACT_STOP, "stop" },
7912 { IF_SRC_FILE, "%u/%u@%s" },
7913 { IF_SRC_KERNEL, "%u/%u" },
7914 { IF_SRC_FILEADDR, "%u@%s" },
7915 { IF_SRC_KERNELADDR, "%u" },
7916};
7917
7918/*
7919 * Address filter string parser
7920 */
7921static int
7922perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
7923 struct list_head *filters)
7924{
7925 struct perf_addr_filter *filter = NULL;
7926 char *start, *orig, *filename = NULL;
7927 struct path path;
7928 substring_t args[MAX_OPT_ARGS];
7929 int state = IF_STATE_ACTION, token;
7930 unsigned int kernel = 0;
7931 int ret = -EINVAL;
7932
7933 orig = fstr = kstrdup(fstr, GFP_KERNEL);
7934 if (!fstr)
7935 return -ENOMEM;
7936
7937 while ((start = strsep(&fstr, " ,\n")) != NULL) {
7938 ret = -EINVAL;
7939
7940 if (!*start)
7941 continue;
7942
7943 /* filter definition begins */
7944 if (state == IF_STATE_ACTION) {
7945 filter = perf_addr_filter_new(event, filters);
7946 if (!filter)
7947 goto fail;
7948 }
7949
7950 token = match_token(start, if_tokens, args);
7951 switch (token) {
7952 case IF_ACT_FILTER:
7953 case IF_ACT_START:
7954 filter->filter = 1;
7955
7956 case IF_ACT_STOP:
7957 if (state != IF_STATE_ACTION)
7958 goto fail;
7959
7960 state = IF_STATE_SOURCE;
7961 break;
7962
7963 case IF_SRC_KERNELADDR:
7964 case IF_SRC_KERNEL:
7965 kernel = 1;
7966
7967 case IF_SRC_FILEADDR:
7968 case IF_SRC_FILE:
7969 if (state != IF_STATE_SOURCE)
7970 goto fail;
7971
7972 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
7973 filter->range = 1;
7974
7975 *args[0].to = 0;
7976 ret = kstrtoul(args[0].from, 0, &filter->offset);
7977 if (ret)
7978 goto fail;
7979
7980 if (filter->range) {
7981 *args[1].to = 0;
7982 ret = kstrtoul(args[1].from, 0, &filter->size);
7983 if (ret)
7984 goto fail;
7985 }
7986
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06007987 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
7988 int fpos = filter->range ? 2 : 1;
7989
7990 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +03007991 if (!filename) {
7992 ret = -ENOMEM;
7993 goto fail;
7994 }
7995 }
7996
7997 state = IF_STATE_END;
7998 break;
7999
8000 default:
8001 goto fail;
8002 }
8003
8004 /*
8005 * Filter definition is fully parsed, validate and install it.
8006 * Make sure that it doesn't contradict itself or the event's
8007 * attribute.
8008 */
8009 if (state == IF_STATE_END) {
8010 if (kernel && event->attr.exclude_kernel)
8011 goto fail;
8012
8013 if (!kernel) {
8014 if (!filename)
8015 goto fail;
8016
8017 /* look up the path and grab its inode */
8018 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
8019 if (ret)
8020 goto fail_free_name;
8021
8022 filter->inode = igrab(d_inode(path.dentry));
8023 path_put(&path);
8024 kfree(filename);
8025 filename = NULL;
8026
8027 ret = -EINVAL;
8028 if (!filter->inode ||
8029 !S_ISREG(filter->inode->i_mode))
8030 /* free_filters_list() will iput() */
8031 goto fail;
8032 }
8033
8034 /* ready to consume more filters */
8035 state = IF_STATE_ACTION;
8036 filter = NULL;
8037 }
8038 }
8039
8040 if (state != IF_STATE_ACTION)
8041 goto fail;
8042
8043 kfree(orig);
8044
8045 return 0;
8046
8047fail_free_name:
8048 kfree(filename);
8049fail:
8050 free_filters_list(filters);
8051 kfree(orig);
8052
8053 return ret;
8054}
8055
8056static int
8057perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
8058{
8059 LIST_HEAD(filters);
8060 int ret;
8061
8062 /*
8063 * Since this is called in perf_ioctl() path, we're already holding
8064 * ctx::mutex.
8065 */
8066 lockdep_assert_held(&event->ctx->mutex);
8067
8068 if (WARN_ON_ONCE(event->parent))
8069 return -EINVAL;
8070
8071 /*
8072 * For now, we only support filtering in per-task events; doing so
8073 * for CPU-wide events requires additional context switching trickery,
8074 * since same object code will be mapped at different virtual
8075 * addresses in different processes.
8076 */
8077 if (!event->ctx->task)
8078 return -EOPNOTSUPP;
8079
8080 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
8081 if (ret)
8082 return ret;
8083
8084 ret = event->pmu->addr_filters_validate(&filters);
8085 if (ret) {
8086 free_filters_list(&filters);
8087 return ret;
8088 }
8089
8090 /* remove existing filters, if any */
8091 perf_addr_filters_splice(event, &filters);
8092
8093 /* install new filters */
8094 perf_event_for_each_child(event, perf_event_addr_filters_apply);
8095
8096 return ret;
8097}
8098
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008099static int perf_event_set_filter(struct perf_event *event, void __user *arg)
8100{
8101 char *filter_str;
8102 int ret = -EINVAL;
8103
Alexander Shishkin375637b2016-04-27 18:44:46 +03008104 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
8105 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
8106 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008107 return -EINVAL;
8108
8109 filter_str = strndup_user(arg, PAGE_SIZE);
8110 if (IS_ERR(filter_str))
8111 return PTR_ERR(filter_str);
8112
8113 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
8114 event->attr.type == PERF_TYPE_TRACEPOINT)
8115 ret = ftrace_profile_set_filter(event, event->attr.config,
8116 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008117 else if (has_addr_filter(event))
8118 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008119
8120 kfree(filter_str);
8121 return ret;
8122}
8123
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008124/*
8125 * hrtimer based swevent callback
8126 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008127
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008128static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008129{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008130 enum hrtimer_restart ret = HRTIMER_RESTART;
8131 struct perf_sample_data data;
8132 struct pt_regs *regs;
8133 struct perf_event *event;
8134 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008135
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008136 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008137
8138 if (event->state != PERF_EVENT_STATE_ACTIVE)
8139 return HRTIMER_NORESTART;
8140
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008141 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008142
Robert Richterfd0d0002012-04-02 20:19:08 +02008143 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008144 regs = get_irq_regs();
8145
8146 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08008147 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02008148 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008149 ret = HRTIMER_NORESTART;
8150 }
8151
8152 period = max_t(u64, 10000, event->hw.sample_period);
8153 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
8154
8155 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008156}
8157
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008158static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008159{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008160 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008161 s64 period;
8162
8163 if (!is_sampling_event(event))
8164 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008165
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008166 period = local64_read(&hwc->period_left);
8167 if (period) {
8168 if (period < 0)
8169 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008170
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008171 local64_set(&hwc->period_left, 0);
8172 } else {
8173 period = max_t(u64, 10000, hwc->sample_period);
8174 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00008175 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8176 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008177}
8178
8179static void perf_swevent_cancel_hrtimer(struct perf_event *event)
8180{
8181 struct hw_perf_event *hwc = &event->hw;
8182
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008183 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008184 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008185 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008186
8187 hrtimer_cancel(&hwc->hrtimer);
8188 }
8189}
8190
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008191static void perf_swevent_init_hrtimer(struct perf_event *event)
8192{
8193 struct hw_perf_event *hwc = &event->hw;
8194
8195 if (!is_sampling_event(event))
8196 return;
8197
8198 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8199 hwc->hrtimer.function = perf_swevent_hrtimer;
8200
8201 /*
8202 * Since hrtimers have a fixed rate, we can do a static freq->period
8203 * mapping and avoid the whole period adjust feedback stuff.
8204 */
8205 if (event->attr.freq) {
8206 long freq = event->attr.sample_freq;
8207
8208 event->attr.sample_period = NSEC_PER_SEC / freq;
8209 hwc->sample_period = event->attr.sample_period;
8210 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008211 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008212 event->attr.freq = 0;
8213 }
8214}
8215
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008216/*
8217 * Software event: cpu wall time clock
8218 */
8219
8220static void cpu_clock_event_update(struct perf_event *event)
8221{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008222 s64 prev;
8223 u64 now;
8224
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008225 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008226 prev = local64_xchg(&event->hw.prev_count, now);
8227 local64_add(now - prev, &event->count);
8228}
8229
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008230static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008231{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008232 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008233 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008234}
8235
8236static void cpu_clock_event_stop(struct perf_event *event, int flags)
8237{
8238 perf_swevent_cancel_hrtimer(event);
8239 cpu_clock_event_update(event);
8240}
8241
8242static int cpu_clock_event_add(struct perf_event *event, int flags)
8243{
8244 if (flags & PERF_EF_START)
8245 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008246 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008247
8248 return 0;
8249}
8250
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008251static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008252{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008253 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008254}
8255
8256static void cpu_clock_event_read(struct perf_event *event)
8257{
8258 cpu_clock_event_update(event);
8259}
8260
8261static int cpu_clock_event_init(struct perf_event *event)
8262{
8263 if (event->attr.type != PERF_TYPE_SOFTWARE)
8264 return -ENOENT;
8265
8266 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8267 return -ENOENT;
8268
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008269 /*
8270 * no branch sampling for software events
8271 */
8272 if (has_branch_stack(event))
8273 return -EOPNOTSUPP;
8274
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008275 perf_swevent_init_hrtimer(event);
8276
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008277 return 0;
8278}
8279
8280static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008281 .task_ctx_nr = perf_sw_context,
8282
Peter Zijlstra34f43922015-02-20 14:05:38 +01008283 .capabilities = PERF_PMU_CAP_NO_NMI,
8284
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008285 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008286 .add = cpu_clock_event_add,
8287 .del = cpu_clock_event_del,
8288 .start = cpu_clock_event_start,
8289 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008290 .read = cpu_clock_event_read,
8291};
8292
8293/*
8294 * Software event: task time clock
8295 */
8296
8297static void task_clock_event_update(struct perf_event *event, u64 now)
8298{
8299 u64 prev;
8300 s64 delta;
8301
8302 prev = local64_xchg(&event->hw.prev_count, now);
8303 delta = now - prev;
8304 local64_add(delta, &event->count);
8305}
8306
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008307static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008308{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008309 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008310 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008311}
8312
8313static void task_clock_event_stop(struct perf_event *event, int flags)
8314{
8315 perf_swevent_cancel_hrtimer(event);
8316 task_clock_event_update(event, event->ctx->time);
8317}
8318
8319static int task_clock_event_add(struct perf_event *event, int flags)
8320{
8321 if (flags & PERF_EF_START)
8322 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008323 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008324
8325 return 0;
8326}
8327
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008328static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008329{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008330 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008331}
8332
8333static void task_clock_event_read(struct perf_event *event)
8334{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008335 u64 now = perf_clock();
8336 u64 delta = now - event->ctx->timestamp;
8337 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008338
8339 task_clock_event_update(event, time);
8340}
8341
8342static int task_clock_event_init(struct perf_event *event)
8343{
8344 if (event->attr.type != PERF_TYPE_SOFTWARE)
8345 return -ENOENT;
8346
8347 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8348 return -ENOENT;
8349
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008350 /*
8351 * no branch sampling for software events
8352 */
8353 if (has_branch_stack(event))
8354 return -EOPNOTSUPP;
8355
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008356 perf_swevent_init_hrtimer(event);
8357
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008358 return 0;
8359}
8360
8361static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008362 .task_ctx_nr = perf_sw_context,
8363
Peter Zijlstra34f43922015-02-20 14:05:38 +01008364 .capabilities = PERF_PMU_CAP_NO_NMI,
8365
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008366 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008367 .add = task_clock_event_add,
8368 .del = task_clock_event_del,
8369 .start = task_clock_event_start,
8370 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008371 .read = task_clock_event_read,
8372};
8373
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008374static void perf_pmu_nop_void(struct pmu *pmu)
8375{
8376}
8377
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008378static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8379{
8380}
8381
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008382static int perf_pmu_nop_int(struct pmu *pmu)
8383{
8384 return 0;
8385}
8386
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008387static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008388
8389static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008390{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008391 __this_cpu_write(nop_txn_flags, flags);
8392
8393 if (flags & ~PERF_PMU_TXN_ADD)
8394 return;
8395
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008396 perf_pmu_disable(pmu);
8397}
8398
8399static int perf_pmu_commit_txn(struct pmu *pmu)
8400{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008401 unsigned int flags = __this_cpu_read(nop_txn_flags);
8402
8403 __this_cpu_write(nop_txn_flags, 0);
8404
8405 if (flags & ~PERF_PMU_TXN_ADD)
8406 return 0;
8407
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008408 perf_pmu_enable(pmu);
8409 return 0;
8410}
8411
8412static void perf_pmu_cancel_txn(struct pmu *pmu)
8413{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008414 unsigned int flags = __this_cpu_read(nop_txn_flags);
8415
8416 __this_cpu_write(nop_txn_flags, 0);
8417
8418 if (flags & ~PERF_PMU_TXN_ADD)
8419 return;
8420
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008421 perf_pmu_enable(pmu);
8422}
8423
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008424static int perf_event_idx_default(struct perf_event *event)
8425{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008426 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008427}
8428
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008429/*
8430 * Ensures all contexts with the same task_ctx_nr have the same
8431 * pmu_cpu_context too.
8432 */
Mark Rutland9e317042014-02-10 17:44:18 +00008433static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008434{
8435 struct pmu *pmu;
8436
8437 if (ctxn < 0)
8438 return NULL;
8439
8440 list_for_each_entry(pmu, &pmus, entry) {
8441 if (pmu->task_ctx_nr == ctxn)
8442 return pmu->pmu_cpu_context;
8443 }
8444
8445 return NULL;
8446}
8447
Peter Zijlstra51676952010-12-07 14:18:20 +01008448static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008449{
Peter Zijlstra51676952010-12-07 14:18:20 +01008450 int cpu;
8451
8452 for_each_possible_cpu(cpu) {
8453 struct perf_cpu_context *cpuctx;
8454
8455 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8456
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008457 if (cpuctx->unique_pmu == old_pmu)
8458 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008459 }
8460}
8461
8462static void free_pmu_context(struct pmu *pmu)
8463{
8464 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008465
8466 mutex_lock(&pmus_lock);
8467 /*
8468 * Like a real lame refcount.
8469 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008470 list_for_each_entry(i, &pmus, entry) {
8471 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8472 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008473 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008474 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008475 }
8476
Peter Zijlstra51676952010-12-07 14:18:20 +01008477 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008478out:
8479 mutex_unlock(&pmus_lock);
8480}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008481
8482/*
8483 * Let userspace know that this PMU supports address range filtering:
8484 */
8485static ssize_t nr_addr_filters_show(struct device *dev,
8486 struct device_attribute *attr,
8487 char *page)
8488{
8489 struct pmu *pmu = dev_get_drvdata(dev);
8490
8491 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8492}
8493DEVICE_ATTR_RO(nr_addr_filters);
8494
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008495static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008496
Peter Zijlstraabe43402010-11-17 23:17:37 +01008497static ssize_t
8498type_show(struct device *dev, struct device_attribute *attr, char *page)
8499{
8500 struct pmu *pmu = dev_get_drvdata(dev);
8501
8502 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8503}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008504static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008505
Stephane Eranian62b85632013-04-03 14:21:34 +02008506static ssize_t
8507perf_event_mux_interval_ms_show(struct device *dev,
8508 struct device_attribute *attr,
8509 char *page)
8510{
8511 struct pmu *pmu = dev_get_drvdata(dev);
8512
8513 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8514}
8515
Peter Zijlstra272325c2015-04-15 11:41:58 +02008516static DEFINE_MUTEX(mux_interval_mutex);
8517
Stephane Eranian62b85632013-04-03 14:21:34 +02008518static ssize_t
8519perf_event_mux_interval_ms_store(struct device *dev,
8520 struct device_attribute *attr,
8521 const char *buf, size_t count)
8522{
8523 struct pmu *pmu = dev_get_drvdata(dev);
8524 int timer, cpu, ret;
8525
8526 ret = kstrtoint(buf, 0, &timer);
8527 if (ret)
8528 return ret;
8529
8530 if (timer < 1)
8531 return -EINVAL;
8532
8533 /* same value, noting to do */
8534 if (timer == pmu->hrtimer_interval_ms)
8535 return count;
8536
Peter Zijlstra272325c2015-04-15 11:41:58 +02008537 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008538 pmu->hrtimer_interval_ms = timer;
8539
8540 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008541 get_online_cpus();
8542 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008543 struct perf_cpu_context *cpuctx;
8544 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8545 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8546
Peter Zijlstra272325c2015-04-15 11:41:58 +02008547 cpu_function_call(cpu,
8548 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008549 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008550 put_online_cpus();
8551 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008552
8553 return count;
8554}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008555static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008556
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008557static struct attribute *pmu_dev_attrs[] = {
8558 &dev_attr_type.attr,
8559 &dev_attr_perf_event_mux_interval_ms.attr,
8560 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008561};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008562ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008563
8564static int pmu_bus_running;
8565static struct bus_type pmu_bus = {
8566 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008567 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008568};
8569
8570static void pmu_dev_release(struct device *dev)
8571{
8572 kfree(dev);
8573}
8574
8575static int pmu_dev_alloc(struct pmu *pmu)
8576{
8577 int ret = -ENOMEM;
8578
8579 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8580 if (!pmu->dev)
8581 goto out;
8582
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008583 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008584 device_initialize(pmu->dev);
8585 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8586 if (ret)
8587 goto free_dev;
8588
8589 dev_set_drvdata(pmu->dev, pmu);
8590 pmu->dev->bus = &pmu_bus;
8591 pmu->dev->release = pmu_dev_release;
8592 ret = device_add(pmu->dev);
8593 if (ret)
8594 goto free_dev;
8595
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008596 /* For PMUs with address filters, throw in an extra attribute: */
8597 if (pmu->nr_addr_filters)
8598 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8599
8600 if (ret)
8601 goto del_dev;
8602
Peter Zijlstraabe43402010-11-17 23:17:37 +01008603out:
8604 return ret;
8605
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008606del_dev:
8607 device_del(pmu->dev);
8608
Peter Zijlstraabe43402010-11-17 23:17:37 +01008609free_dev:
8610 put_device(pmu->dev);
8611 goto out;
8612}
8613
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008614static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008615static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008616
Mischa Jonker03d8e802013-06-04 11:45:48 +02008617int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008618{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008619 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008620
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008621 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008622 ret = -ENOMEM;
8623 pmu->pmu_disable_count = alloc_percpu(int);
8624 if (!pmu->pmu_disable_count)
8625 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008626
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008627 pmu->type = -1;
8628 if (!name)
8629 goto skip_type;
8630 pmu->name = name;
8631
8632 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008633 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8634 if (type < 0) {
8635 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008636 goto free_pdc;
8637 }
8638 }
8639 pmu->type = type;
8640
Peter Zijlstraabe43402010-11-17 23:17:37 +01008641 if (pmu_bus_running) {
8642 ret = pmu_dev_alloc(pmu);
8643 if (ret)
8644 goto free_idr;
8645 }
8646
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008647skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008648 if (pmu->task_ctx_nr == perf_hw_context) {
8649 static int hw_context_taken = 0;
8650
Mark Rutland5101ef22016-04-26 11:33:46 +01008651 /*
8652 * Other than systems with heterogeneous CPUs, it never makes
8653 * sense for two PMUs to share perf_hw_context. PMUs which are
8654 * uncore must use perf_invalid_context.
8655 */
8656 if (WARN_ON_ONCE(hw_context_taken &&
8657 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008658 pmu->task_ctx_nr = perf_invalid_context;
8659
8660 hw_context_taken = 1;
8661 }
8662
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008663 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8664 if (pmu->pmu_cpu_context)
8665 goto got_cpu_context;
8666
Wei Yongjunc4814202013-04-12 11:05:54 +08008667 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008668 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8669 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008670 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008671
8672 for_each_possible_cpu(cpu) {
8673 struct perf_cpu_context *cpuctx;
8674
8675 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008676 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008677 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008678 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008679 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008680
Peter Zijlstra272325c2015-04-15 11:41:58 +02008681 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008682
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008683 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008684 }
8685
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008686got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008687 if (!pmu->start_txn) {
8688 if (pmu->pmu_enable) {
8689 /*
8690 * If we have pmu_enable/pmu_disable calls, install
8691 * transaction stubs that use that to try and batch
8692 * hardware accesses.
8693 */
8694 pmu->start_txn = perf_pmu_start_txn;
8695 pmu->commit_txn = perf_pmu_commit_txn;
8696 pmu->cancel_txn = perf_pmu_cancel_txn;
8697 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008698 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008699 pmu->commit_txn = perf_pmu_nop_int;
8700 pmu->cancel_txn = perf_pmu_nop_void;
8701 }
8702 }
8703
8704 if (!pmu->pmu_enable) {
8705 pmu->pmu_enable = perf_pmu_nop_void;
8706 pmu->pmu_disable = perf_pmu_nop_void;
8707 }
8708
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008709 if (!pmu->event_idx)
8710 pmu->event_idx = perf_event_idx_default;
8711
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008712 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008713 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008714 ret = 0;
8715unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008716 mutex_unlock(&pmus_lock);
8717
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008718 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008719
Peter Zijlstraabe43402010-11-17 23:17:37 +01008720free_dev:
8721 device_del(pmu->dev);
8722 put_device(pmu->dev);
8723
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008724free_idr:
8725 if (pmu->type >= PERF_TYPE_MAX)
8726 idr_remove(&pmu_idr, pmu->type);
8727
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008728free_pdc:
8729 free_percpu(pmu->pmu_disable_count);
8730 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008731}
Yan, Zhengc464c762014-03-18 16:56:41 +08008732EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008733
8734void perf_pmu_unregister(struct pmu *pmu)
8735{
8736 mutex_lock(&pmus_lock);
8737 list_del_rcu(&pmu->entry);
8738 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008739
8740 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02008741 * We dereference the pmu list under both SRCU and regular RCU, so
8742 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008743 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008744 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02008745 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008746
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008747 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008748 if (pmu->type >= PERF_TYPE_MAX)
8749 idr_remove(&pmu_idr, pmu->type);
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008750 if (pmu->nr_addr_filters)
8751 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008752 device_del(pmu->dev);
8753 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01008754 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008755}
Yan, Zhengc464c762014-03-18 16:56:41 +08008756EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008757
Mark Rutlandcc34b982015-01-07 14:56:51 +00008758static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
8759{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008760 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00008761 int ret;
8762
8763 if (!try_module_get(pmu->module))
8764 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008765
8766 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02008767 /*
8768 * This ctx->mutex can nest when we're called through
8769 * inheritance. See the perf_event_ctx_lock_nested() comment.
8770 */
8771 ctx = perf_event_ctx_lock_nested(event->group_leader,
8772 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008773 BUG_ON(!ctx);
8774 }
8775
Mark Rutlandcc34b982015-01-07 14:56:51 +00008776 event->pmu = pmu;
8777 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008778
8779 if (ctx)
8780 perf_event_ctx_unlock(event->group_leader, ctx);
8781
Mark Rutlandcc34b982015-01-07 14:56:51 +00008782 if (ret)
8783 module_put(pmu->module);
8784
8785 return ret;
8786}
8787
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008788static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008789{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008790 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008791 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08008792 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008793
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008794 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008795
8796 rcu_read_lock();
8797 pmu = idr_find(&pmu_idr, event->attr.type);
8798 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08008799 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008800 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08008801 if (ret)
8802 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008803 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08008804 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008805
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008806 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008807 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008808 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008809 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008810
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008811 if (ret != -ENOENT) {
8812 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008813 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008814 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008815 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008816 pmu = ERR_PTR(-ENOENT);
8817unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008818 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008819
8820 return pmu;
8821}
8822
Kan Liangf2fb6be2016-03-23 11:24:37 -07008823static void attach_sb_event(struct perf_event *event)
8824{
8825 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
8826
8827 raw_spin_lock(&pel->lock);
8828 list_add_rcu(&event->sb_list, &pel->list);
8829 raw_spin_unlock(&pel->lock);
8830}
8831
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008832/*
8833 * We keep a list of all !task (and therefore per-cpu) events
8834 * that need to receive side-band records.
8835 *
8836 * This avoids having to scan all the various PMU per-cpu contexts
8837 * looking for them.
8838 */
Kan Liangf2fb6be2016-03-23 11:24:37 -07008839static void account_pmu_sb_event(struct perf_event *event)
8840{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07008841 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -07008842 attach_sb_event(event);
8843}
8844
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008845static void account_event_cpu(struct perf_event *event, int cpu)
8846{
8847 if (event->parent)
8848 return;
8849
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008850 if (is_cgroup_event(event))
8851 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
8852}
8853
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008854/* Freq events need the tick to stay alive (see perf_event_task_tick). */
8855static void account_freq_event_nohz(void)
8856{
8857#ifdef CONFIG_NO_HZ_FULL
8858 /* Lock so we don't race with concurrent unaccount */
8859 spin_lock(&nr_freq_lock);
8860 if (atomic_inc_return(&nr_freq_events) == 1)
8861 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
8862 spin_unlock(&nr_freq_lock);
8863#endif
8864}
8865
8866static void account_freq_event(void)
8867{
8868 if (tick_nohz_full_enabled())
8869 account_freq_event_nohz();
8870 else
8871 atomic_inc(&nr_freq_events);
8872}
8873
8874
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008875static void account_event(struct perf_event *event)
8876{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008877 bool inc = false;
8878
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008879 if (event->parent)
8880 return;
8881
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008882 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008883 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008884 if (event->attr.mmap || event->attr.mmap_data)
8885 atomic_inc(&nr_mmap_events);
8886 if (event->attr.comm)
8887 atomic_inc(&nr_comm_events);
8888 if (event->attr.task)
8889 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008890 if (event->attr.freq)
8891 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03008892 if (event->attr.context_switch) {
8893 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008894 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03008895 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008896 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008897 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008898 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008899 inc = true;
8900
Peter Zijlstra9107c892016-02-24 18:45:45 +01008901 if (inc) {
8902 if (atomic_inc_not_zero(&perf_sched_count))
8903 goto enabled;
8904
8905 mutex_lock(&perf_sched_mutex);
8906 if (!atomic_read(&perf_sched_count)) {
8907 static_branch_enable(&perf_sched_events);
8908 /*
8909 * Guarantee that all CPUs observe they key change and
8910 * call the perf scheduling hooks before proceeding to
8911 * install events that need them.
8912 */
8913 synchronize_sched();
8914 }
8915 /*
8916 * Now that we have waited for the sync_sched(), allow further
8917 * increments to by-pass the mutex.
8918 */
8919 atomic_inc(&perf_sched_count);
8920 mutex_unlock(&perf_sched_mutex);
8921 }
8922enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008923
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008924 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07008925
8926 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008927}
8928
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008929/*
8930 * Allocate and initialize a event structure
8931 */
8932static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008933perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008934 struct task_struct *task,
8935 struct perf_event *group_leader,
8936 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008937 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00008938 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008939{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008940 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008941 struct perf_event *event;
8942 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008943 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008944
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008945 if ((unsigned)cpu >= nr_cpu_ids) {
8946 if (!task || cpu != -1)
8947 return ERR_PTR(-EINVAL);
8948 }
8949
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008950 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008951 if (!event)
8952 return ERR_PTR(-ENOMEM);
8953
8954 /*
8955 * Single events are their own group leaders, with an
8956 * empty sibling list:
8957 */
8958 if (!group_leader)
8959 group_leader = event;
8960
8961 mutex_init(&event->child_mutex);
8962 INIT_LIST_HEAD(&event->child_list);
8963
8964 INIT_LIST_HEAD(&event->group_entry);
8965 INIT_LIST_HEAD(&event->event_entry);
8966 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008967 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01008968 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008969 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01008970 INIT_HLIST_NODE(&event->hlist_entry);
8971
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008972
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008973 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08008974 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008975
8976 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008977 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008978
Al Viroa6fa9412012-08-20 14:59:25 +01008979 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008980 event->cpu = cpu;
8981 event->attr = *attr;
8982 event->group_leader = group_leader;
8983 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008984 event->oncpu = -1;
8985
8986 event->parent = parent_event;
8987
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08008988 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008989 event->id = atomic64_inc_return(&perf_event_id);
8990
8991 event->state = PERF_EVENT_STATE_INACTIVE;
8992
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008993 if (task) {
8994 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008995 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008996 * XXX pmu::event_init needs to know what task to account to
8997 * and we cannot use the ctx information because we need the
8998 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008999 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009000 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009001 }
9002
Peter Zijlstra34f43922015-02-20 14:05:38 +01009003 event->clock = &local_clock;
9004 if (parent_event)
9005 event->clock = parent_event->clock;
9006
Avi Kivity4dc0da82011-06-29 18:42:35 +03009007 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01009008 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009009 context = parent_event->overflow_handler_context;
9010 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009011
Wang Nan18794452016-03-28 06:41:30 +00009012 if (overflow_handler) {
9013 event->overflow_handler = overflow_handler;
9014 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00009015 } else if (is_write_backward(event)){
9016 event->overflow_handler = perf_event_output_backward;
9017 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00009018 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00009019 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00009020 event->overflow_handler_context = NULL;
9021 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02009022
Jiri Olsa0231bb52013-02-01 11:23:45 +01009023 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009024
9025 pmu = NULL;
9026
9027 hwc = &event->hw;
9028 hwc->sample_period = attr->sample_period;
9029 if (attr->freq && attr->sample_freq)
9030 hwc->sample_period = 1;
9031 hwc->last_period = hwc->sample_period;
9032
Peter Zijlstrae7850592010-05-21 14:43:08 +02009033 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009034
9035 /*
9036 * we currently do not support PERF_FORMAT_GROUP on inherited events
9037 */
9038 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009039 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009040
Yan, Zhenga46a2302014-11-04 21:56:06 -05009041 if (!has_branch_stack(event))
9042 event->attr.branch_sample_type = 0;
9043
Matt Fleming79dff512015-01-23 18:45:42 +00009044 if (cgroup_fd != -1) {
9045 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
9046 if (err)
9047 goto err_ns;
9048 }
9049
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009050 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009051 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009052 goto err_ns;
9053 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009054 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009055 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009056 }
9057
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009058 err = exclusive_event_init(event);
9059 if (err)
9060 goto err_pmu;
9061
Alexander Shishkin375637b2016-04-27 18:44:46 +03009062 if (has_addr_filter(event)) {
9063 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
9064 sizeof(unsigned long),
9065 GFP_KERNEL);
9066 if (!event->addr_filters_offs)
9067 goto err_per_task;
9068
9069 /* force hw sync on the address filters */
9070 event->addr_filters_gen = 1;
9071 }
9072
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009073 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009074 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009075 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009076 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009077 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01009078 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009079 }
9080
Alexander Shishkin927a5572016-03-02 13:24:14 +02009081 /* symmetric to unaccount_event() in _free_event() */
9082 account_event(event);
9083
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009084 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009085
Alexander Shishkin375637b2016-04-27 18:44:46 +03009086err_addr_filters:
9087 kfree(event->addr_filters_offs);
9088
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009089err_per_task:
9090 exclusive_event_destroy(event);
9091
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009092err_pmu:
9093 if (event->destroy)
9094 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08009095 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009096err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00009097 if (is_cgroup_event(event))
9098 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009099 if (event->ns)
9100 put_pid_ns(event->ns);
9101 kfree(event);
9102
9103 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009104}
9105
9106static int perf_copy_attr(struct perf_event_attr __user *uattr,
9107 struct perf_event_attr *attr)
9108{
9109 u32 size;
9110 int ret;
9111
9112 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
9113 return -EFAULT;
9114
9115 /*
9116 * zero the full structure, so that a short copy will be nice.
9117 */
9118 memset(attr, 0, sizeof(*attr));
9119
9120 ret = get_user(size, &uattr->size);
9121 if (ret)
9122 return ret;
9123
9124 if (size > PAGE_SIZE) /* silly large */
9125 goto err_size;
9126
9127 if (!size) /* abi compat */
9128 size = PERF_ATTR_SIZE_VER0;
9129
9130 if (size < PERF_ATTR_SIZE_VER0)
9131 goto err_size;
9132
9133 /*
9134 * If we're handed a bigger struct than we know of,
9135 * ensure all the unknown bits are 0 - i.e. new
9136 * user-space does not rely on any kernel feature
9137 * extensions we dont know about yet.
9138 */
9139 if (size > sizeof(*attr)) {
9140 unsigned char __user *addr;
9141 unsigned char __user *end;
9142 unsigned char val;
9143
9144 addr = (void __user *)uattr + sizeof(*attr);
9145 end = (void __user *)uattr + size;
9146
9147 for (; addr < end; addr++) {
9148 ret = get_user(val, addr);
9149 if (ret)
9150 return ret;
9151 if (val)
9152 goto err_size;
9153 }
9154 size = sizeof(*attr);
9155 }
9156
9157 ret = copy_from_user(attr, uattr, size);
9158 if (ret)
9159 return -EFAULT;
9160
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05309161 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009162 return -EINVAL;
9163
9164 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
9165 return -EINVAL;
9166
9167 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
9168 return -EINVAL;
9169
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009170 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
9171 u64 mask = attr->branch_sample_type;
9172
9173 /* only using defined bits */
9174 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
9175 return -EINVAL;
9176
9177 /* at least one branch bit must be set */
9178 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9179 return -EINVAL;
9180
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009181 /* propagate priv level, when not set for branch */
9182 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9183
9184 /* exclude_kernel checked on syscall entry */
9185 if (!attr->exclude_kernel)
9186 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9187
9188 if (!attr->exclude_user)
9189 mask |= PERF_SAMPLE_BRANCH_USER;
9190
9191 if (!attr->exclude_hv)
9192 mask |= PERF_SAMPLE_BRANCH_HV;
9193 /*
9194 * adjust user setting (for HW filter setup)
9195 */
9196 attr->branch_sample_type = mask;
9197 }
Stephane Eraniane7122092013-06-06 11:02:04 +02009198 /* privileged levels capture (kernel, hv): check permissions */
9199 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02009200 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9201 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009202 }
Jiri Olsa40189942012-08-07 15:20:37 +02009203
Jiri Olsac5ebced2012-08-07 15:20:40 +02009204 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02009205 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02009206 if (ret)
9207 return ret;
9208 }
9209
9210 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9211 if (!arch_perf_have_user_stack_dump())
9212 return -ENOSYS;
9213
9214 /*
9215 * We have __u32 type for the size, but so far
9216 * we can only use __u16 as maximum due to the
9217 * __u16 sample size limit.
9218 */
9219 if (attr->sample_stack_user >= USHRT_MAX)
9220 ret = -EINVAL;
9221 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
9222 ret = -EINVAL;
9223 }
Jiri Olsa40189942012-08-07 15:20:37 +02009224
Stephane Eranian60e23642014-09-24 13:48:37 +02009225 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9226 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009227out:
9228 return ret;
9229
9230err_size:
9231 put_user(sizeof(*attr), &uattr->size);
9232 ret = -E2BIG;
9233 goto out;
9234}
9235
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009236static int
9237perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009238{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009239 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009240 int ret = -EINVAL;
9241
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009242 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009243 goto set;
9244
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009245 /* don't allow circular references */
9246 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009247 goto out;
9248
Peter Zijlstra0f139302010-05-20 14:35:15 +02009249 /*
9250 * Don't allow cross-cpu buffers
9251 */
9252 if (output_event->cpu != event->cpu)
9253 goto out;
9254
9255 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009256 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009257 */
9258 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9259 goto out;
9260
Peter Zijlstra34f43922015-02-20 14:05:38 +01009261 /*
9262 * Mixing clocks in the same buffer is trouble you don't need.
9263 */
9264 if (output_event->clock != event->clock)
9265 goto out;
9266
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009267 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009268 * Either writing ring buffer from beginning or from end.
9269 * Mixing is not allowed.
9270 */
9271 if (is_write_backward(output_event) != is_write_backward(event))
9272 goto out;
9273
9274 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009275 * If both events generate aux data, they must be on the same PMU
9276 */
9277 if (has_aux(event) && has_aux(output_event) &&
9278 event->pmu != output_event->pmu)
9279 goto out;
9280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009281set:
9282 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009283 /* Can't redirect output if we've got an active mmap() */
9284 if (atomic_read(&event->mmap_count))
9285 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009286
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009287 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009288 /* get the rb we want to redirect to */
9289 rb = ring_buffer_get(output_event);
9290 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009291 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009292 }
9293
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009294 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009295
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009296 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009297unlock:
9298 mutex_unlock(&event->mmap_mutex);
9299
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009300out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009301 return ret;
9302}
9303
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009304static void mutex_lock_double(struct mutex *a, struct mutex *b)
9305{
9306 if (b < a)
9307 swap(a, b);
9308
9309 mutex_lock(a);
9310 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9311}
9312
Peter Zijlstra34f43922015-02-20 14:05:38 +01009313static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9314{
9315 bool nmi_safe = false;
9316
9317 switch (clk_id) {
9318 case CLOCK_MONOTONIC:
9319 event->clock = &ktime_get_mono_fast_ns;
9320 nmi_safe = true;
9321 break;
9322
9323 case CLOCK_MONOTONIC_RAW:
9324 event->clock = &ktime_get_raw_fast_ns;
9325 nmi_safe = true;
9326 break;
9327
9328 case CLOCK_REALTIME:
9329 event->clock = &ktime_get_real_ns;
9330 break;
9331
9332 case CLOCK_BOOTTIME:
9333 event->clock = &ktime_get_boot_ns;
9334 break;
9335
9336 case CLOCK_TAI:
9337 event->clock = &ktime_get_tai_ns;
9338 break;
9339
9340 default:
9341 return -EINVAL;
9342 }
9343
9344 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9345 return -EINVAL;
9346
9347 return 0;
9348}
9349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009350/**
9351 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9352 *
9353 * @attr_uptr: event_id type attributes for monitoring/sampling
9354 * @pid: target pid
9355 * @cpu: target cpu
9356 * @group_fd: group leader event fd
9357 */
9358SYSCALL_DEFINE5(perf_event_open,
9359 struct perf_event_attr __user *, attr_uptr,
9360 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9361{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009362 struct perf_event *group_leader = NULL, *output_event = NULL;
9363 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009364 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009365 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009366 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009367 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009368 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009369 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009370 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009371 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009372 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009373 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009374 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009375
9376 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009377 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009378 return -EINVAL;
9379
9380 err = perf_copy_attr(attr_uptr, &attr);
9381 if (err)
9382 return err;
9383
9384 if (!attr.exclude_kernel) {
9385 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9386 return -EACCES;
9387 }
9388
9389 if (attr.freq) {
9390 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9391 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009392 } else {
9393 if (attr.sample_period & (1ULL << 63))
9394 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009395 }
9396
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009397 if (!attr.sample_max_stack)
9398 attr.sample_max_stack = sysctl_perf_event_max_stack;
9399
Stephane Eraniane5d13672011-02-14 11:20:01 +02009400 /*
9401 * In cgroup mode, the pid argument is used to pass the fd
9402 * opened to the cgroup directory in cgroupfs. The cpu argument
9403 * designates the cpu on which to monitor threads from that
9404 * cgroup.
9405 */
9406 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9407 return -EINVAL;
9408
Yann Droneauda21b0b32014-01-05 21:36:33 +01009409 if (flags & PERF_FLAG_FD_CLOEXEC)
9410 f_flags |= O_CLOEXEC;
9411
9412 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009413 if (event_fd < 0)
9414 return event_fd;
9415
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009416 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009417 err = perf_fget_light(group_fd, &group);
9418 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009419 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009420 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009421 if (flags & PERF_FLAG_FD_OUTPUT)
9422 output_event = group_leader;
9423 if (flags & PERF_FLAG_FD_NO_GROUP)
9424 group_leader = NULL;
9425 }
9426
Stephane Eraniane5d13672011-02-14 11:20:01 +02009427 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009428 task = find_lively_task_by_vpid(pid);
9429 if (IS_ERR(task)) {
9430 err = PTR_ERR(task);
9431 goto err_group_fd;
9432 }
9433 }
9434
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009435 if (task && group_leader &&
9436 group_leader->attr.inherit != attr.inherit) {
9437 err = -EINVAL;
9438 goto err_task;
9439 }
9440
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009441 get_online_cpus();
9442
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009443 if (task) {
9444 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9445 if (err)
9446 goto err_cpus;
9447
9448 /*
9449 * Reuse ptrace permission checks for now.
9450 *
9451 * We must hold cred_guard_mutex across this and any potential
9452 * perf_install_in_context() call for this new event to
9453 * serialize against exec() altering our credentials (and the
9454 * perf_event_exit_task() that could imply).
9455 */
9456 err = -EACCES;
9457 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9458 goto err_cred;
9459 }
9460
Matt Fleming79dff512015-01-23 18:45:42 +00009461 if (flags & PERF_FLAG_PID_CGROUP)
9462 cgroup_fd = pid;
9463
Avi Kivity4dc0da82011-06-29 18:42:35 +03009464 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009465 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009466 if (IS_ERR(event)) {
9467 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009468 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009469 }
9470
Vince Weaver53b25332014-05-16 17:12:12 -04009471 if (is_sampling_event(event)) {
9472 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +05309473 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -04009474 goto err_alloc;
9475 }
9476 }
9477
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009478 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009479 * Special case software events and allow them to be part of
9480 * any hardware group.
9481 */
9482 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009483
Peter Zijlstra34f43922015-02-20 14:05:38 +01009484 if (attr.use_clockid) {
9485 err = perf_event_set_clock(event, attr.clockid);
9486 if (err)
9487 goto err_alloc;
9488 }
9489
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009490 if (pmu->task_ctx_nr == perf_sw_context)
9491 event->event_caps |= PERF_EV_CAP_SOFTWARE;
9492
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009493 if (group_leader &&
9494 (is_software_event(event) != is_software_event(group_leader))) {
9495 if (is_software_event(event)) {
9496 /*
9497 * If event and group_leader are not both a software
9498 * event, and event is, then group leader is not.
9499 *
9500 * Allow the addition of software events to !software
9501 * groups, this is safe because software events never
9502 * fail to schedule.
9503 */
9504 pmu = group_leader->pmu;
9505 } else if (is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009506 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009507 /*
9508 * In case the group is a pure software group, and we
9509 * try to add a hardware event, move the whole group to
9510 * the hardware context.
9511 */
9512 move_group = 1;
9513 }
9514 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009515
9516 /*
9517 * Get the target context (task or percpu):
9518 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009519 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009520 if (IS_ERR(ctx)) {
9521 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009522 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009523 }
9524
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009525 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9526 err = -EBUSY;
9527 goto err_context;
9528 }
9529
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009530 /*
9531 * Look up the group leader (we will attach this event to it):
9532 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009533 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009534 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009535
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009536 /*
9537 * Do not allow a recursive hierarchy (this new sibling
9538 * becoming part of another group-sibling):
9539 */
9540 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009541 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009542
9543 /* All events in a group should have the same clock */
9544 if (group_leader->clock != event->clock)
9545 goto err_context;
9546
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009547 /*
9548 * Do not allow to attach to a group in a different
9549 * task or CPU context:
9550 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009551 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009552 /*
9553 * Make sure we're both on the same task, or both
9554 * per-cpu events.
9555 */
9556 if (group_leader->ctx->task != ctx->task)
9557 goto err_context;
9558
9559 /*
9560 * Make sure we're both events for the same CPU;
9561 * grouping events for different CPUs is broken; since
9562 * you can never concurrently schedule them anyhow.
9563 */
9564 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009565 goto err_context;
9566 } else {
9567 if (group_leader->ctx != ctx)
9568 goto err_context;
9569 }
9570
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009571 /*
9572 * Only a group leader can be exclusive or pinned
9573 */
9574 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009575 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009576 }
9577
9578 if (output_event) {
9579 err = perf_event_set_output(event, output_event);
9580 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009581 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009582 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009583
Yann Droneauda21b0b32014-01-05 21:36:33 +01009584 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9585 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009586 if (IS_ERR(event_file)) {
9587 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009588 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009589 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009590 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009591
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009592 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009593 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009594 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009595 if (gctx->task == TASK_TOMBSTONE) {
9596 err = -ESRCH;
9597 goto err_locked;
9598 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009599 } else {
9600 mutex_lock(&ctx->mutex);
9601 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009602
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009603 if (ctx->task == TASK_TOMBSTONE) {
9604 err = -ESRCH;
9605 goto err_locked;
9606 }
9607
Peter Zijlstraa7239682015-09-09 19:06:33 +02009608 if (!perf_event_validate_size(event)) {
9609 err = -E2BIG;
9610 goto err_locked;
9611 }
9612
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009613 /*
9614 * Must be under the same ctx::mutex as perf_install_in_context(),
9615 * because we need to serialize with concurrent event creation.
9616 */
9617 if (!exclusive_event_installable(event, ctx)) {
9618 /* exclusive and group stuff are assumed mutually exclusive */
9619 WARN_ON_ONCE(move_group);
9620
9621 err = -EBUSY;
9622 goto err_locked;
9623 }
9624
9625 WARN_ON_ONCE(ctx->parent_ctx);
9626
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009627 /*
9628 * This is the point on no return; we cannot fail hereafter. This is
9629 * where we start modifying current state.
9630 */
9631
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009632 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009633 /*
9634 * See perf_event_ctx_lock() for comments on the details
9635 * of swizzling perf_event::ctx.
9636 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009637 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009638
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009639 list_for_each_entry(sibling, &group_leader->sibling_list,
9640 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009641 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009642 put_ctx(gctx);
9643 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009644
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009645 /*
9646 * Wait for everybody to stop referencing the events through
9647 * the old lists, before installing it on new lists.
9648 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009649 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009650
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009651 /*
9652 * Install the group siblings before the group leader.
9653 *
9654 * Because a group leader will try and install the entire group
9655 * (through the sibling list, which is still in-tact), we can
9656 * end up with siblings installed in the wrong context.
9657 *
9658 * By installing siblings first we NO-OP because they're not
9659 * reachable through the group lists.
9660 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009661 list_for_each_entry(sibling, &group_leader->sibling_list,
9662 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009663 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01009664 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009665 get_ctx(ctx);
9666 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009667
9668 /*
9669 * Removing from the context ends up with disabled
9670 * event. What we want here is event in the initial
9671 * startup state, ready to be add into new context.
9672 */
9673 perf_event__state_init(group_leader);
9674 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9675 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009676
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009677 /*
9678 * Now that all events are installed in @ctx, nothing
9679 * references @gctx anymore, so drop the last reference we have
9680 * on it.
9681 */
9682 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009683 }
9684
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02009685 /*
9686 * Precalculate sample_data sizes; do while holding ctx::mutex such
9687 * that we're serialized against further additions and before
9688 * perf_install_in_context() which is the point the event is active and
9689 * can use these values.
9690 */
9691 perf_event__header_size(event);
9692 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009693
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01009694 event->owner = current;
9695
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08009696 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009697 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009698
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009699 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009700 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009701 mutex_unlock(&ctx->mutex);
9702
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009703 if (task) {
9704 mutex_unlock(&task->signal->cred_guard_mutex);
9705 put_task_struct(task);
9706 }
9707
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009708 put_online_cpus();
9709
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009710 mutex_lock(&current->perf_event_mutex);
9711 list_add_tail(&event->owner_entry, &current->perf_event_list);
9712 mutex_unlock(&current->perf_event_mutex);
9713
Peter Zijlstra8a495422010-05-27 15:47:49 +02009714 /*
9715 * Drop the reference on the group_event after placing the
9716 * new event on the sibling_list. This ensures destruction
9717 * of the group leader will find the pointer to itself in
9718 * perf_group_detach().
9719 */
Al Viro2903ff02012-08-28 12:52:22 -04009720 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009721 fd_install(event_fd, event_file);
9722 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009723
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009724err_locked:
9725 if (move_group)
9726 mutex_unlock(&gctx->mutex);
9727 mutex_unlock(&ctx->mutex);
9728/* err_file: */
9729 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009730err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009731 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04009732 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009733err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01009734 /*
9735 * If event_file is set, the fput() above will have called ->release()
9736 * and that will take care of freeing the event.
9737 */
9738 if (!event_file)
9739 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009740err_cred:
9741 if (task)
9742 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009743err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009744 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009745err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02009746 if (task)
9747 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009748err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04009749 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009750err_fd:
9751 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009752 return err;
9753}
9754
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009755/**
9756 * perf_event_create_kernel_counter
9757 *
9758 * @attr: attributes of the counter to create
9759 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07009760 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009761 */
9762struct perf_event *
9763perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07009764 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009765 perf_overflow_handler_t overflow_handler,
9766 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009767{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009768 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009769 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009770 int err;
9771
9772 /*
9773 * Get the target context (task or percpu):
9774 */
9775
Avi Kivity4dc0da82011-06-29 18:42:35 +03009776 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009777 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009778 if (IS_ERR(event)) {
9779 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009780 goto err;
9781 }
9782
Jiri Olsaf8697762014-08-01 14:33:01 +02009783 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009784 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02009785
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009786 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009787 if (IS_ERR(ctx)) {
9788 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009789 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009790 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009791
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009792 WARN_ON_ONCE(ctx->parent_ctx);
9793 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009794 if (ctx->task == TASK_TOMBSTONE) {
9795 err = -ESRCH;
9796 goto err_unlock;
9797 }
9798
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009799 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009800 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009801 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009802 }
9803
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009804 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009805 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009806 mutex_unlock(&ctx->mutex);
9807
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009808 return event;
9809
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009810err_unlock:
9811 mutex_unlock(&ctx->mutex);
9812 perf_unpin_context(ctx);
9813 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009814err_free:
9815 free_event(event);
9816err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009817 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009818}
9819EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
9820
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009821void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
9822{
9823 struct perf_event_context *src_ctx;
9824 struct perf_event_context *dst_ctx;
9825 struct perf_event *event, *tmp;
9826 LIST_HEAD(events);
9827
9828 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
9829 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
9830
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009831 /*
9832 * See perf_event_ctx_lock() for comments on the details
9833 * of swizzling perf_event::ctx.
9834 */
9835 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009836 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
9837 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009838 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02009839 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009840 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02009841 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009842 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009843
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009844 /*
9845 * Wait for the events to quiesce before re-instating them.
9846 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009847 synchronize_rcu();
9848
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009849 /*
9850 * Re-instate events in 2 passes.
9851 *
9852 * Skip over group leaders and only install siblings on this first
9853 * pass, siblings will not get enabled without a leader, however a
9854 * leader will enable its siblings, even if those are still on the old
9855 * context.
9856 */
9857 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9858 if (event->group_leader == event)
9859 continue;
9860
9861 list_del(&event->migrate_entry);
9862 if (event->state >= PERF_EVENT_STATE_OFF)
9863 event->state = PERF_EVENT_STATE_INACTIVE;
9864 account_event_cpu(event, dst_cpu);
9865 perf_install_in_context(dst_ctx, event, dst_cpu);
9866 get_ctx(dst_ctx);
9867 }
9868
9869 /*
9870 * Once all the siblings are setup properly, install the group leaders
9871 * to make it go.
9872 */
Peter Zijlstra98861672013-10-03 16:02:23 +02009873 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9874 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009875 if (event->state >= PERF_EVENT_STATE_OFF)
9876 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02009877 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009878 perf_install_in_context(dst_ctx, event, dst_cpu);
9879 get_ctx(dst_ctx);
9880 }
9881 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009882 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009883}
9884EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
9885
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009886static void sync_child_event(struct perf_event *child_event,
9887 struct task_struct *child)
9888{
9889 struct perf_event *parent_event = child_event->parent;
9890 u64 child_val;
9891
9892 if (child_event->attr.inherit_stat)
9893 perf_event_read_event(child_event, child);
9894
Peter Zijlstrab5e58792010-05-21 14:43:12 +02009895 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009896
9897 /*
9898 * Add back the child's count to the parent's count:
9899 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02009900 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009901 atomic64_add(child_event->total_time_enabled,
9902 &parent_event->child_total_time_enabled);
9903 atomic64_add(child_event->total_time_running,
9904 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009905}
9906
9907static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009908perf_event_exit_event(struct perf_event *child_event,
9909 struct perf_event_context *child_ctx,
9910 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009911{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009912 struct perf_event *parent_event = child_event->parent;
9913
Peter Zijlstra1903d502014-07-15 17:27:27 +02009914 /*
9915 * Do not destroy the 'original' grouping; because of the context
9916 * switch optimization the original events could've ended up in a
9917 * random child task.
9918 *
9919 * If we were to destroy the original group, all group related
9920 * operations would cease to function properly after this random
9921 * child dies.
9922 *
9923 * Do destroy all inherited groups, we don't care about those
9924 * and being thorough is better.
9925 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009926 raw_spin_lock_irq(&child_ctx->lock);
9927 WARN_ON_ONCE(child_ctx->is_active);
9928
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009929 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01009930 perf_group_detach(child_event);
9931 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01009932 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009933 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009934
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009935 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009936 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009937 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009938 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04009939 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009940 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009941 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009942 /*
9943 * Child events can be cleaned up.
9944 */
9945
9946 sync_child_event(child_event, child);
9947
9948 /*
9949 * Remove this event from the parent's list
9950 */
9951 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
9952 mutex_lock(&parent_event->child_mutex);
9953 list_del_init(&child_event->child_list);
9954 mutex_unlock(&parent_event->child_mutex);
9955
9956 /*
9957 * Kick perf_poll() for is_event_hup().
9958 */
9959 perf_event_wakeup(parent_event);
9960 free_event(child_event);
9961 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009962}
9963
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009964static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009965{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009966 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009967 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009968
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009969 WARN_ON_ONCE(child != current);
9970
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009971 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009972 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009973 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009974
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009975 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009976 * In order to reduce the amount of tricky in ctx tear-down, we hold
9977 * ctx::mutex over the entire thing. This serializes against almost
9978 * everything that wants to access the ctx.
9979 *
9980 * The exception is sys_perf_event_open() /
9981 * perf_event_create_kernel_count() which does find_get_context()
9982 * without ctx::mutex (it cannot because of the move_group double mutex
9983 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009984 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009985 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009986
9987 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009988 * In a single ctx::lock section, de-schedule the events and detach the
9989 * context from the task such that we cannot ever get it scheduled back
9990 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009991 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009992 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009993 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02009994
9995 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009996 * Now that the context is inactive, destroy the task <-> ctx relation
9997 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009998 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009999 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
10000 put_ctx(child_ctx); /* cannot be last */
10001 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
10002 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010003
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010004 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010005 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010006
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010007 if (clone_ctx)
10008 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010009
10010 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010011 * Report the task dead after unscheduling the events so that we
10012 * won't get any samples after PERF_RECORD_EXIT. We can however still
10013 * get a few PERF_RECORD_READ events.
10014 */
10015 perf_event_task(child, child_ctx, 0);
10016
Peter Zijlstraebf905f2014-05-29 19:00:24 +020010017 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010018 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010019
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010020 mutex_unlock(&child_ctx->mutex);
10021
10022 put_ctx(child_ctx);
10023}
10024
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010025/*
10026 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010027 *
10028 * Can be called with cred_guard_mutex held when called from
10029 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010030 */
10031void perf_event_exit_task(struct task_struct *child)
10032{
Peter Zijlstra88821352010-11-09 19:01:43 +010010033 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010034 int ctxn;
10035
Peter Zijlstra88821352010-11-09 19:01:43 +010010036 mutex_lock(&child->perf_event_mutex);
10037 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
10038 owner_entry) {
10039 list_del_init(&event->owner_entry);
10040
10041 /*
10042 * Ensure the list deletion is visible before we clear
10043 * the owner, closes a race against perf_release() where
10044 * we need to serialize on the owner->perf_event_mutex.
10045 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010010046 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010010047 }
10048 mutex_unlock(&child->perf_event_mutex);
10049
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010050 for_each_task_context_nr(ctxn)
10051 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010010052
10053 /*
10054 * The perf_event_exit_task_context calls perf_event_task
10055 * with child's task_ctx, which generates EXIT events for
10056 * child contexts and sets child->perf_event_ctxp[] to NULL.
10057 * At this point we need to send EXIT events to cpu contexts.
10058 */
10059 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010060}
10061
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010062static void perf_free_event(struct perf_event *event,
10063 struct perf_event_context *ctx)
10064{
10065 struct perf_event *parent = event->parent;
10066
10067 if (WARN_ON_ONCE(!parent))
10068 return;
10069
10070 mutex_lock(&parent->child_mutex);
10071 list_del_init(&event->child_list);
10072 mutex_unlock(&parent->child_mutex);
10073
Al Viroa6fa9412012-08-20 14:59:25 +010010074 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010075
Peter Zijlstra652884f2015-01-23 11:20:10 +010010076 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020010077 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010078 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010010079 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010080 free_event(event);
10081}
10082
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010083/*
Peter Zijlstra652884f2015-01-23 11:20:10 +010010084 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010085 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010010086 *
10087 * Not all locks are strictly required, but take them anyway to be nice and
10088 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010089 */
10090void perf_event_free_task(struct task_struct *task)
10091{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010092 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010093 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010094 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010095
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010096 for_each_task_context_nr(ctxn) {
10097 ctx = task->perf_event_ctxp[ctxn];
10098 if (!ctx)
10099 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010100
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010101 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010102again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010103 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
10104 group_entry)
10105 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010106
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010107 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
10108 group_entry)
10109 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010110
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010111 if (!list_empty(&ctx->pinned_groups) ||
10112 !list_empty(&ctx->flexible_groups))
10113 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010114
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010115 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010116
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010117 put_ctx(ctx);
10118 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010119}
10120
Peter Zijlstra4e231c72010-09-09 21:01:59 +020010121void perf_event_delayed_put(struct task_struct *task)
10122{
10123 int ctxn;
10124
10125 for_each_task_context_nr(ctxn)
10126 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
10127}
10128
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010129struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000010130{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010131 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010132
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010133 file = fget_raw(fd);
10134 if (!file)
10135 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000010136
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010137 if (file->f_op != &perf_fops) {
10138 fput(file);
10139 return ERR_PTR(-EBADF);
10140 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000010141
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010142 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010143}
10144
10145const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
10146{
10147 if (!event)
10148 return ERR_PTR(-EINVAL);
10149
10150 return &event->attr;
10151}
10152
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010153/*
10154 * inherit a event from parent task to child task:
10155 */
10156static struct perf_event *
10157inherit_event(struct perf_event *parent_event,
10158 struct task_struct *parent,
10159 struct perf_event_context *parent_ctx,
10160 struct task_struct *child,
10161 struct perf_event *group_leader,
10162 struct perf_event_context *child_ctx)
10163{
Jiri Olsa1929def2014-09-12 13:18:27 +020010164 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010165 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020010166 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010167
10168 /*
10169 * Instead of creating recursive hierarchies of events,
10170 * we link inherited events back to the original parent,
10171 * which has a filp for sure, which we use as the reference
10172 * count:
10173 */
10174 if (parent_event->parent)
10175 parent_event = parent_event->parent;
10176
10177 child_event = perf_event_alloc(&parent_event->attr,
10178 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010179 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010180 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000010181 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010182 if (IS_ERR(child_event))
10183 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010010184
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010185 /*
10186 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10187 * must be under the same lock in order to serialize against
10188 * perf_event_release_kernel(), such that either we must observe
10189 * is_orphaned_event() or they will observe us on the child_list.
10190 */
10191 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020010192 if (is_orphaned_event(parent_event) ||
10193 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010194 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +010010195 free_event(child_event);
10196 return NULL;
10197 }
10198
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010199 get_ctx(child_ctx);
10200
10201 /*
10202 * Make the child state follow the state of the parent event,
10203 * not its attr.disabled bit. We hold the parent's mutex,
10204 * so we won't race with perf_event_{en, dis}able_family.
10205 */
Jiri Olsa1929def2014-09-12 13:18:27 +020010206 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010207 child_event->state = PERF_EVENT_STATE_INACTIVE;
10208 else
10209 child_event->state = PERF_EVENT_STATE_OFF;
10210
10211 if (parent_event->attr.freq) {
10212 u64 sample_period = parent_event->hw.sample_period;
10213 struct hw_perf_event *hwc = &child_event->hw;
10214
10215 hwc->sample_period = sample_period;
10216 hwc->last_period = sample_period;
10217
10218 local64_set(&hwc->period_left, sample_period);
10219 }
10220
10221 child_event->ctx = child_ctx;
10222 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010223 child_event->overflow_handler_context
10224 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010225
10226 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020010227 * Precalculate sample_data sizes
10228 */
10229 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020010230 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020010231
10232 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010233 * Link it up in the child's context:
10234 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020010235 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010236 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010237 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010238
10239 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010240 * Link this into the parent event's child list
10241 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010242 list_add_tail(&child_event->child_list, &parent_event->child_list);
10243 mutex_unlock(&parent_event->child_mutex);
10244
10245 return child_event;
10246}
10247
10248static int inherit_group(struct perf_event *parent_event,
10249 struct task_struct *parent,
10250 struct perf_event_context *parent_ctx,
10251 struct task_struct *child,
10252 struct perf_event_context *child_ctx)
10253{
10254 struct perf_event *leader;
10255 struct perf_event *sub;
10256 struct perf_event *child_ctr;
10257
10258 leader = inherit_event(parent_event, parent, parent_ctx,
10259 child, NULL, child_ctx);
10260 if (IS_ERR(leader))
10261 return PTR_ERR(leader);
10262 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10263 child_ctr = inherit_event(sub, parent, parent_ctx,
10264 child, leader, child_ctx);
10265 if (IS_ERR(child_ctr))
10266 return PTR_ERR(child_ctr);
10267 }
10268 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010269}
10270
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010271static int
10272inherit_task_group(struct perf_event *event, struct task_struct *parent,
10273 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010274 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010275 int *inherited_all)
10276{
10277 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010278 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010279
10280 if (!event->attr.inherit) {
10281 *inherited_all = 0;
10282 return 0;
10283 }
10284
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010285 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010286 if (!child_ctx) {
10287 /*
10288 * This is executed from the parent task context, so
10289 * inherit events that have been marked for cloning.
10290 * First allocate and initialize a context for the
10291 * child.
10292 */
10293
Jiri Olsa734df5a2013-07-09 17:44:10 +020010294 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010295 if (!child_ctx)
10296 return -ENOMEM;
10297
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010298 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010299 }
10300
10301 ret = inherit_group(event, parent, parent_ctx,
10302 child, child_ctx);
10303
10304 if (ret)
10305 *inherited_all = 0;
10306
10307 return ret;
10308}
10309
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010310/*
10311 * Initialize the perf_event context in task_struct
10312 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010313static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010314{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010315 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010316 struct perf_event_context *cloned_ctx;
10317 struct perf_event *event;
10318 struct task_struct *parent = current;
10319 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010320 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010321 int ret = 0;
10322
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010323 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010324 return 0;
10325
10326 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010327 * If the parent's context is a clone, pin it so it won't get
10328 * swapped under us.
10329 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010330 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010331 if (!parent_ctx)
10332 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010333
10334 /*
10335 * No need to check if parent_ctx != NULL here; since we saw
10336 * it non-NULL earlier, the only reason for it to become NULL
10337 * is if we exit, and since we're currently in the middle of
10338 * a fork we can't be exiting at the same time.
10339 */
10340
10341 /*
10342 * Lock the parent list. No need to lock the child - not PID
10343 * hashed yet and not running, so nobody can access it.
10344 */
10345 mutex_lock(&parent_ctx->mutex);
10346
10347 /*
10348 * We dont have to disable NMIs - we are only looking at
10349 * the list, not manipulating it:
10350 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010351 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010352 ret = inherit_task_group(event, parent, parent_ctx,
10353 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010354 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010355 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010356 }
10357
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010358 /*
10359 * We can't hold ctx->lock when iterating the ->flexible_group list due
10360 * to allocations, but we need to prevent rotation because
10361 * rotate_ctx() will change the list from interrupt context.
10362 */
10363 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10364 parent_ctx->rotate_disable = 1;
10365 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10366
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010367 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010368 ret = inherit_task_group(event, parent, parent_ctx,
10369 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010370 if (ret)
10371 break;
10372 }
10373
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010374 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10375 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010376
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010377 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010378
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010379 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010380 /*
10381 * Mark the child context as a clone of the parent
10382 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010383 *
10384 * Note that if the parent is a clone, the holding of
10385 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010386 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010387 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010388 if (cloned_ctx) {
10389 child_ctx->parent_ctx = cloned_ctx;
10390 child_ctx->parent_gen = parent_ctx->parent_gen;
10391 } else {
10392 child_ctx->parent_ctx = parent_ctx;
10393 child_ctx->parent_gen = parent_ctx->generation;
10394 }
10395 get_ctx(child_ctx->parent_ctx);
10396 }
10397
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010398 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010399 mutex_unlock(&parent_ctx->mutex);
10400
10401 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010402 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010403
10404 return ret;
10405}
10406
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010407/*
10408 * Initialize the perf_event context in task_struct
10409 */
10410int perf_event_init_task(struct task_struct *child)
10411{
10412 int ctxn, ret;
10413
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010414 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10415 mutex_init(&child->perf_event_mutex);
10416 INIT_LIST_HEAD(&child->perf_event_list);
10417
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010418 for_each_task_context_nr(ctxn) {
10419 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010420 if (ret) {
10421 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010422 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010423 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010424 }
10425
10426 return 0;
10427}
10428
Paul Mackerras220b1402010-03-10 20:45:52 +110010429static void __init perf_event_init_all_cpus(void)
10430{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010431 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010432 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010433
10434 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010435 swhash = &per_cpu(swevent_htable, cpu);
10436 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010437 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070010438
10439 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
10440 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020010441
10442 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010443 }
10444}
10445
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010446int perf_event_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010447{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010448 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010449
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010450 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010451 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010452 struct swevent_hlist *hlist;
10453
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010454 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10455 WARN_ON(!hlist);
10456 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010457 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010458 mutex_unlock(&swhash->hlist_mutex);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010459 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010460}
10461
Dave Young2965faa2015-09-09 15:38:55 -070010462#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010463static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010464{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010465 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010466 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10467 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010468
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010469 raw_spin_lock(&ctx->lock);
10470 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010471 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010472 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010473}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010474
10475static void perf_event_exit_cpu_context(int cpu)
10476{
10477 struct perf_event_context *ctx;
10478 struct pmu *pmu;
10479 int idx;
10480
10481 idx = srcu_read_lock(&pmus_srcu);
10482 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010483 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010484
10485 mutex_lock(&ctx->mutex);
10486 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10487 mutex_unlock(&ctx->mutex);
10488 }
10489 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010490}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010491#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010492
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010493static void perf_event_exit_cpu_context(int cpu) { }
10494
10495#endif
10496
10497int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010498{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010499 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010500 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010501}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010502
Peter Zijlstrac2774432010-12-08 15:29:02 +010010503static int
10504perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10505{
10506 int cpu;
10507
10508 for_each_online_cpu(cpu)
10509 perf_event_exit_cpu(cpu);
10510
10511 return NOTIFY_OK;
10512}
10513
10514/*
10515 * Run the perf reboot notifier at the very last possible moment so that
10516 * the generic watchdog code runs as long as possible.
10517 */
10518static struct notifier_block perf_reboot_notifier = {
10519 .notifier_call = perf_reboot,
10520 .priority = INT_MIN,
10521};
10522
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010523void __init perf_event_init(void)
10524{
Jason Wessel3c502e72010-11-04 17:33:01 -050010525 int ret;
10526
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010527 idr_init(&pmu_idr);
10528
Paul Mackerras220b1402010-03-10 20:45:52 +110010529 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010530 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010531 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10532 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10533 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010534 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010535 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010010536 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010537
10538 ret = init_hw_breakpoint();
10539 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010540
Jiri Olsab01c3a02012-03-23 15:41:20 +010010541 /*
10542 * Build time assertion that we keep the data_head at the intended
10543 * location. IOW, validation we got the __reserved[] size right.
10544 */
10545 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10546 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010547}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010548
Cody P Schaferfd979c02015-01-30 13:45:57 -080010549ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10550 char *page)
10551{
10552 struct perf_pmu_events_attr *pmu_attr =
10553 container_of(attr, struct perf_pmu_events_attr, attr);
10554
10555 if (pmu_attr->event_str)
10556 return sprintf(page, "%s\n", pmu_attr->event_str);
10557
10558 return 0;
10559}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010560EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010561
Peter Zijlstraabe43402010-11-17 23:17:37 +010010562static int __init perf_event_sysfs_init(void)
10563{
10564 struct pmu *pmu;
10565 int ret;
10566
10567 mutex_lock(&pmus_lock);
10568
10569 ret = bus_register(&pmu_bus);
10570 if (ret)
10571 goto unlock;
10572
10573 list_for_each_entry(pmu, &pmus, entry) {
10574 if (!pmu->name || pmu->type < 0)
10575 continue;
10576
10577 ret = pmu_dev_alloc(pmu);
10578 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10579 }
10580 pmu_bus_running = 1;
10581 ret = 0;
10582
10583unlock:
10584 mutex_unlock(&pmus_lock);
10585
10586 return ret;
10587}
10588device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010589
10590#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010591static struct cgroup_subsys_state *
10592perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010593{
10594 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010595
Li Zefan1b15d052011-03-03 14:26:06 +080010596 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010597 if (!jc)
10598 return ERR_PTR(-ENOMEM);
10599
Stephane Eraniane5d13672011-02-14 11:20:01 +020010600 jc->info = alloc_percpu(struct perf_cgroup_info);
10601 if (!jc->info) {
10602 kfree(jc);
10603 return ERR_PTR(-ENOMEM);
10604 }
10605
Stephane Eraniane5d13672011-02-14 11:20:01 +020010606 return &jc->css;
10607}
10608
Tejun Heoeb954192013-08-08 20:11:23 -040010609static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010610{
Tejun Heoeb954192013-08-08 20:11:23 -040010611 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10612
Stephane Eraniane5d13672011-02-14 11:20:01 +020010613 free_percpu(jc->info);
10614 kfree(jc);
10615}
10616
10617static int __perf_cgroup_move(void *info)
10618{
10619 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010620 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010621 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010622 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010623 return 0;
10624}
10625
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010626static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010627{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010628 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010629 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010630
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010631 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010632 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010633}
10634
Tejun Heo073219e2014-02-08 10:36:58 -050010635struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010636 .css_alloc = perf_cgroup_css_alloc,
10637 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010638 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010639};
10640#endif /* CONFIG_CGROUP_PERF */